Add env var IDENTITY_WEBSOCKET
[apps/homescreen.git] / homescreen / src / applicationmodel.cpp
index 417bc4c..6174b9b 100644 (file)
 #include "applicationmodel.h"
 #include "appinfo.h"
 
-#include <QtCore/QDebug>
-
 #include <QtDBus/QDBusInterface>
 #include <QtDBus/QDBusReply>
 
-#include "afm_user_daemon_proxy.h"
-
-extern org::AGL::afm::user *afm_user_daemon_proxy;
-
 class ApplicationModel::Private
 {
 public:
-    Private();
-
+    Private(ApplicationModel *parent);
+    QList<QList<int> > orders;
+    QList<AppInfo> originalData;
+private:
+    ApplicationModel *q;
+public:
+    QDBusInterface proxy;
     QList<AppInfo> data;
 };
 
-namespace {
-    QString get_icon_name(QJsonObject const &i)
-    {
-        QString icon = i["id"].toString().split("@").front();
-        if (icon == "hvac" || icon == "poi") {
-            icon = icon.toUpper();
-        } else if (icon == "mediaplayer") {
-            icon = "Multimedia";
-        } else {
-            icon[0] = icon[0].toUpper();
-        }
-        return icon;
+ApplicationModel::Private::Private(ApplicationModel *parent)
+    : q(parent)
+    , proxy(QStringLiteral("org.agl.homescreenappframeworkbinder"), QStringLiteral("/AppFramework"), QStringLiteral("org.agl.appframework"), QDBusConnection::sessionBus())
+{
+    QDBusReply<QList<AppInfo>> reply = proxy.call("getAvailableApps");
+    if (false)/*reply.isValid()) TODO: test for CES!  */ {
+        data = reply.value();
+    } else {
+        data.append(AppInfo(QStringLiteral("HVAC"), QStringLiteral("HVAC"), QStringLiteral("hvac@0.1")));
+        data.append(AppInfo(QStringLiteral("Navigation"), QStringLiteral("NAVIGATION"), QStringLiteral("navigation@0.1")));
+        data.append(AppInfo(QStringLiteral("Phone"), QStringLiteral("PHONE"), QStringLiteral("phone@0.1")));
+        data.append(AppInfo(QStringLiteral("Radio"), QStringLiteral("RADIO"), QStringLiteral("radio@0.1")));
+        data.append(AppInfo(QStringLiteral("Multimedia"), QStringLiteral("MULTIMEDIA"), QStringLiteral("mediaplayer@0.1")));
+        data.append(AppInfo(QStringLiteral("Mixer"), QStringLiteral("MIXER"), QStringLiteral("mixer@0.1")));
+        data.append(AppInfo(QStringLiteral("Dashboard"), QStringLiteral("DASHBOARD"), QStringLiteral("dashboard@0.1")));
+        data.append(AppInfo(QStringLiteral("Settings"), QStringLiteral("SETTINGS"), QStringLiteral("settings@0.1")));
+        data.append(AppInfo(QStringLiteral("POI"), QStringLiteral("POINT OF\nINTEREST"), QStringLiteral("poi@0.1")));
     }
+    originalData = data;
+    QList<int> o;
+    o << 5 << 4 << 3 << 7 << 8  << 0 << 2 << 1 << 6;
+    orders.append(o);
+    o.clear();
+    o << 0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 ;
+    orders.append(o);
+    o.clear();
+    o << 3 << 8 << 1 << 0 << 2 << 7 << 5 << 4 << 6;
+    orders.append(o);
+    o.clear();
+    o << 2 << 7 << 3 << 8 << 4 << 0 << 1 << 5 << 6;
+    orders.append(o);
+    o.clear();
+    o << 2 << 7 << 3 << 8 << 4 << 0 << 1 << 5 << 6;
+    orders.append(o);
+    o.clear();
+    o << 6 << 0 << 2 << 1 << 7 << 3 << 5 << 4 << 8;
+    orders.append(o);
 }
-
-ApplicationModel::Private::Private()
+void ApplicationModel::changeOrder(const int &hash)
 {
-    QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
-    QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
-    for (auto const &app : japps.array()) {
-        QJsonObject const &jso = app.toObject();
-        auto const name = jso["name"].toString();
-        auto const id = jso["id"].toString();
-        auto const icon = get_icon_name(jso);
-        this->data.append(AppInfo(icon, name, id));
-        qDebug() << "name:" << name << "icon:" << icon << "id:" << id;
+    if(hash < 0) {
+        d->data = d->originalData;
+        return;
+    }
+    int order = qAbs(hash) % 7;
+    QList<int> o = d->orders.at(order);
+    QList<AppInfo> newData;
+    for(int i = 0; i < o.size(); ++i) {
+        newData.append(d->originalData.at(o.at(i)));
+    }
+    d->data = newData;
+}
+
+void ApplicationModel::changeLanguage(const QString &lang)
+{ //todo: use QT translator instead of hardcoded strings.
+    if(lang == "fr") {
+        d->originalData[0].setName("CLIMATISATION");
+        d->originalData[1].setName("NAVIGATION");
+        d->originalData[2].setName("TÉLÉPHONE");
+        d->originalData[3].setName("RADIO");
+        d->originalData[4].setName("MULTIMÉDIA");
+        d->originalData[5].setName("MIXER");
+        d->originalData[6].setName("TABLEAU DE\nBORD");
+        d->originalData[7].setName("PARAMÈTRES");
+        d->originalData[8].setName("POINT D'INTÉRÊT");
+    } else {
+        d->originalData[0].setName("HVAC");
+        d->originalData[1].setName("NAVIGATION");
+        d->originalData[2].setName("PHONE");
+        d->originalData[3].setName("RADIO");
+        d->originalData[4].setName("MULTIMEDIA");
+        d->originalData[5].setName("MIXER");
+        d->originalData[6].setName("DASHBOARD");
+        d->originalData[7].setName("SETTINGS");
+        d->originalData[8].setName("POINT OF\nINTEREST");
     }
 }
 
 ApplicationModel::ApplicationModel(QObject *parent)
     : QAbstractListModel(parent)
-    , d(new Private())
+    , d(new Private(this))
 {
+    setObjectName("ApplicationModel");
 }
 
 ApplicationModel::~ApplicationModel()
 {
-    delete this->d;
+    delete d;
 }
 
 int ApplicationModel::rowCount(const QModelIndex &parent) const
@@ -80,7 +129,7 @@ int ApplicationModel::rowCount(const QModelIndex &parent) const
     if (parent.isValid())
         return 0;
 
-    return this->d->data.count();
+    return d->data.count();
 }
 
 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
@@ -91,13 +140,13 @@ QVariant ApplicationModel::data(const QModelIndex &index, int role) const
 
     switch (role) {
     case Qt::DecorationRole:
-        ret = this->d->data[index.row()].iconPath();
+        ret = d->data[index.row()].iconPath();
         break;
     case Qt::DisplayRole:
-        ret = this->d->data[index.row()].name();
+        ret = d->data[index.row()].name();
         break;
     case Qt::UserRole:
-        ret = this->d->data[index.row()].id();
+        ret = d->data[index.row()].id();
         break;
     default:
         break;
@@ -114,31 +163,3 @@ QHash<int, QByteArray> ApplicationModel::roleNames() const
     roles[Qt::UserRole] = "id";
     return roles;
 }
-
-QString ApplicationModel::id(int i) const
-{
-    return data(index(i), Qt::UserRole).toString();
-}
-
-void ApplicationModel::move(int from, int to)
-{
-    QModelIndex parent;
-    if (to < 0 || to > rowCount()) return;
-    if (from < to) {
-        if (!beginMoveRows(parent, from, from, parent, to + 1)) {
-            qDebug() << from << to << false;
-            return;
-        }
-        d->data.move(from, to);
-        endMoveRows();
-    } else if (from > to) {
-        if (!beginMoveRows(parent, from, from, parent, to)) {
-            qDebug() << from << to << false;
-            return;
-        }
-        d->data.move(from, to);
-        endMoveRows();
-    } else {
-        qDebug() << from << to << false;
-    }
-}