Fix language when user logs out
[staging/HomeScreen.git] / HomeScreen / src2 / applicationmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "applicationmodel.h"
19 #include "appinfo.h"
20
21 #include <QtDBus/QDBusInterface>
22 #include <QtDBus/QDBusReply>
23
24 class ApplicationModel::Private
25 {
26 public:
27     Private(ApplicationModel *parent);
28     QList<QList<int> > orders;
29     QList<AppInfo> originalData;
30 private:
31     ApplicationModel *q;
32 public:
33     QDBusInterface proxy;
34     QList<AppInfo> data;
35 };
36
37 ApplicationModel::Private::Private(ApplicationModel *parent)
38     : q(parent)
39     , proxy(QStringLiteral("org.agl.homescreenappframeworkbinder"), QStringLiteral("/AppFramework"), QStringLiteral("org.agl.appframework"), QDBusConnection::sessionBus())
40 {
41     QDBusReply<QList<AppInfo>> reply = proxy.call("getAvailableApps");
42     if (false)/*reply.isValid()) TODO: test for CES!  */ {
43         data = reply.value();
44     } else {
45         data.append(AppInfo(QStringLiteral("HVAC"), QStringLiteral("HVAC"), QStringLiteral("hvac@0.1")));
46         data.append(AppInfo(QStringLiteral("Navigation"), QStringLiteral("NAVIGATION"), QStringLiteral("navigation@0.1")));
47         data.append(AppInfo(QStringLiteral("Phone"), QStringLiteral("PHONE"), QStringLiteral("phone@0.1")));
48         data.append(AppInfo(QStringLiteral("Radio"), QStringLiteral("RADIO"), QStringLiteral("radio@0.1")));
49         data.append(AppInfo(QStringLiteral("Multimedia"), QStringLiteral("MULTIMEDIA"), QStringLiteral("mediaplayer@0.1")));
50         data.append(AppInfo(QStringLiteral("Connectivity"), QStringLiteral("CONNECTIVITY"), QStringLiteral("connectivity@0.1")));
51         data.append(AppInfo(QStringLiteral("Dashboard"), QStringLiteral("DASHBOARD"), QStringLiteral("dashboard@0.1")));
52         data.append(AppInfo(QStringLiteral("Settings"), QStringLiteral("SETTINGS"), QStringLiteral("settings@0.1")));
53         data.append(AppInfo(QStringLiteral("POI"), QStringLiteral("POINT OF\nINTEREST"), QStringLiteral("poi@0.1")));
54     }
55     originalData = data;
56     QList<int> o;
57     o << 5 << 4 << 3 << 7 << 8  << 0 << 2 << 1 << 6;
58     orders.append(o);
59     o.clear();
60     o << 0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 ;
61     orders.append(o);
62     o.clear();
63     o << 3 << 8 << 1 << 0 << 2 << 7 << 5 << 4 << 6;
64     orders.append(o);
65     o.clear();
66     o << 2 << 7 << 3 << 8 << 4 << 0 << 1 << 5 << 6;
67     orders.append(o);
68     o.clear();
69     o << 2 << 7 << 3 << 8 << 4 << 0 << 1 << 5 << 6;
70     orders.append(o);
71     o.clear();
72     o << 6 << 0 << 2 << 1 << 7 << 3 << 5 << 4 << 8;
73     orders.append(o);
74 }
75 void ApplicationModel::changeOrder(const int &hash)
76 {
77     if(hash < 0) {
78         d->data = d->originalData;
79         return;
80     }
81     int order = qAbs(hash) % 7;
82     QList<int> o = d->orders.at(order);
83     QList<AppInfo> newData;
84     for(int i = 0; i < o.size(); ++i) {
85         newData.append(d->originalData.at(o.at(i)));
86     }
87     d->data = newData;
88 }
89
90 void ApplicationModel::changeLanguage(const QString &lang)
91 { //todo: use QT translator instead of hardcoded strings.
92     if(lang == "fr") {
93         d->originalData[0].setName("CLIMATISATION");
94         d->originalData[1].setName("NAVIGATION");
95         d->originalData[2].setName("TÉLÉPHONE");
96         d->originalData[3].setName("RADIO");
97         d->originalData[4].setName("MULTIMÉDIA");
98         d->originalData[5].setName("CONNEXIONS");
99         d->originalData[6].setName("TABLEAU DE\nBORD");
100         d->originalData[7].setName("PARAMÈTRES");
101         d->originalData[8].setName("POINT D'INTÉRÊT");
102     } else {
103         d->originalData[0].setName("HVAC");
104         d->originalData[1].setName("NAVIGATION");
105         d->originalData[2].setName("PHONE");
106         d->originalData[3].setName("RADIO");
107         d->originalData[4].setName("MULTIMEDIA");
108         d->originalData[5].setName("CONNECTIVITY");
109         d->originalData[6].setName("DASHBOARD");
110         d->originalData[7].setName("SETTINGS");
111         d->originalData[8].setName("POINT OF\nINTEREST");
112     }
113 }
114
115 ApplicationModel::ApplicationModel(QObject *parent)
116     : QAbstractListModel(parent)
117     , d(new Private(this))
118 {
119     setObjectName("ApplicationModel");
120 }
121
122 ApplicationModel::~ApplicationModel()
123 {
124     delete d;
125 }
126
127 int ApplicationModel::rowCount(const QModelIndex &parent) const
128 {
129     if (parent.isValid())
130         return 0;
131
132     return d->data.count();
133 }
134
135 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
136 {
137     QVariant ret;
138     if (!index.isValid())
139         return ret;
140
141     switch (role) {
142     case Qt::DecorationRole:
143         ret = d->data[index.row()].iconPath();
144         break;
145     case Qt::DisplayRole:
146         ret = d->data[index.row()].name();
147         break;
148     case Qt::UserRole:
149         ret = d->data[index.row()].id();
150         break;
151     default:
152         break;
153     }
154
155     return ret;
156 }
157
158 QHash<int, QByteArray> ApplicationModel::roleNames() const
159 {
160     QHash<int, QByteArray> roles;
161     roles[Qt::DecorationRole] = "icon";
162     roles[Qt::DisplayRole] = "name";
163     roles[Qt::UserRole] = "id";
164     return roles;
165 }