various changes in user language settings
[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     int order = qAbs(hash) % 7;
78     QList<int> o = d->orders.at(order);
79     QList<AppInfo> newData;
80     for(int i = 0; i < o.size(); ++i) {
81         newData.append(d->originalData.at(o.at(i)));
82     }
83     d->data = newData;
84 }
85
86 void ApplicationModel::changeLanguage(const QString &lang)
87 { //todo: use QT translator instead of hardcoded strings.
88     if(lang == "fr") {
89         d->originalData[0].setName("CLIMATISATION");
90         d->originalData[1].setName("NAVIGATION");
91         d->originalData[2].setName("TÉLÉPHONE");
92         d->originalData[3].setName("RADIO");
93         d->originalData[4].setName("MULTIMÉDIA");
94         d->originalData[5].setName("CONNEXIONS");
95         d->originalData[6].setName("TABLEAU DE\nBORD");
96         d->originalData[7].setName("PARAMÈTRES");
97         d->originalData[8].setName("POINT D'INTÉRÊT");
98     } else {
99         d->originalData[0].setName("HVAC");
100         d->originalData[1].setName("NAVIGATION");
101         d->originalData[2].setName("PHONE");
102         d->originalData[3].setName("RADIO");
103         d->originalData[4].setName("MULTIMEDIA");
104         d->originalData[5].setName("CONNECTIVITY");
105         d->originalData[6].setName("DASHBOARD");
106         d->originalData[7].setName("SETTINGS");
107         d->originalData[8].setName("POINT OF\nINTEREST");
108     }
109 }
110
111 ApplicationModel::ApplicationModel(QObject *parent)
112     : QAbstractListModel(parent)
113     , d(new Private(this))
114 {
115     setObjectName("ApplicationModel");
116 }
117
118 ApplicationModel::~ApplicationModel()
119 {
120     delete d;
121 }
122
123 int ApplicationModel::rowCount(const QModelIndex &parent) const
124 {
125     if (parent.isValid())
126         return 0;
127
128     return d->data.count();
129 }
130
131 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
132 {
133     QVariant ret;
134     if (!index.isValid())
135         return ret;
136
137     switch (role) {
138     case Qt::DecorationRole:
139         ret = d->data[index.row()].iconPath();
140         break;
141     case Qt::DisplayRole:
142         ret = d->data[index.row()].name();
143         break;
144     case Qt::UserRole:
145         ret = d->data[index.row()].id();
146         break;
147     default:
148         break;
149     }
150
151     return ret;
152 }
153
154 QHash<int, QByteArray> ApplicationModel::roleNames() const
155 {
156     QHash<int, QByteArray> roles;
157     roles[Qt::DecorationRole] = "icon";
158     roles[Qt::DisplayRole] = "name";
159     roles[Qt::UserRole] = "id";
160     return roles;
161 }