18c373bc83cbded390d74cdddb8d2319f45259b7
[staging/HomeScreen.git] / HomeScreen / src / applauncherwidget.cpp
1 /*
2  * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "applauncherwidget.h"
18 #include "ui_applauncherwidget.h"
19 #include <include/daynightmode.hpp>
20 #include <QSettings>
21 #ifdef __i386__
22     #include <QProcess>
23 #endif
24
25 #define APP_LIST_COLUMN_COUNT 5
26
27 AppLauncherWidget::AppLauncherWidget(QWidget *parent) :
28     QWidget(parent),
29     mp_ui(new Ui::AppLauncherWidget),
30     mp_appList(new QList<AppInfo>()),
31     mp_appTable(0),
32     mp_dBusAppFrameworkProxy()
33 {
34     mp_ui->setupUi(this);
35
36     AppInfo ai;
37     for (int i = 0; i < 100; ++i)
38     {
39         ai.setName("test" + QString::number(i));
40         mp_appList->append(ai);
41     }
42
43     qDebug("D-Bus: connect to org.agl.homescreenappframeworkbindertizen /AppFramework");
44     mp_dBusAppFrameworkProxy = new org::agl::appframework("org.agl.homescreenappframeworkbindertizen",
45                                               "/AppFramework",
46                                               QDBusConnection::sessionBus(),
47                                               0);
48 }
49
50 AppLauncherWidget::~AppLauncherWidget()
51 {
52     delete mp_dBusAppFrameworkProxy;
53     if (0 != mp_appTable)
54     {
55         delete mp_appTable;
56     }
57     delete mp_appList;
58     delete mp_ui;
59 }
60
61 void AppLauncherWidget::updateColorScheme()
62 {
63     QSettings settings;
64     QSettings settings_cs(QApplication::applicationDirPath() +
65                           "/colorschemes/" +
66                           settings.value("systemsettings/colorscheme", "default").toString() +
67                           "/" +
68                           QString::number(settings.value("systemsettings/daynightmode", SystemDayNight::DAYNIGHTMODE_DAY).toInt()) +
69                           ".ini",
70                           QSettings::IniFormat);
71
72     mp_ui->widget_Background->setStyleSheet(settings_cs.value("AppLauncherWidget/widget_Background").toString());
73     mp_ui->widget_Home_Icon->setStyleSheet(settings_cs.value("AppLauncherWidget/widget_Home_Icon").toString());
74 }
75
76 void AppLauncherWidget::populateAppList()
77 {
78     setStyleSheet("QTableWidget {background-color: transparent;}"
79                   "QTableCornerButton::section {background-color: transparent;}");
80
81     if (0 == mp_appTable)
82     {
83         mp_appTable = new QTableWidget(this);
84         QObject::connect(mp_appTable, SIGNAL(cellClicked(int, int)), this, SLOT(on_tableView_clicked(int, int)));
85     }
86     else
87     {
88         mp_appTable->clear();
89     }
90
91     mp_appTable->setShowGrid(false);
92     mp_appTable->setFrameShape(QFrame::NoFrame);
93     mp_appTable->move(40, 40);
94     mp_appTable->resize(1000, 1920 - 40 - 40 - 60 - 60);
95     mp_appTable->horizontalHeader()->setVisible(false);
96     mp_appTable->verticalHeader()->setVisible(false);
97     mp_appTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
98     mp_appTable->setRowCount(100);
99     mp_appTable->setColumnCount(APP_LIST_COLUMN_COUNT);
100
101     int i;
102
103 #ifdef __arm__
104     QStringList apps = mp_dBusAppFrameworkProxy->getAvailableAppNames();
105 #endif
106 #ifdef __i386__
107     QStringList apps;
108     apps.append(QString("/usr/bin/gnome-terminal"));
109 #endif
110     mp_appList->clear();
111
112     mp_appTable->setRowCount((apps.size() + (APP_LIST_COLUMN_COUNT - 1)) / APP_LIST_COLUMN_COUNT);
113
114     if (apps.size() >= (9 * APP_LIST_COLUMN_COUNT))
115     {
116         mp_appTable->resize(1000, 1920 - 40 - 40 - 60 - 60);
117     }
118     else
119     {
120         mp_appTable->resize(1000, mp_appTable->rowCount() * 190);
121     }
122
123
124     for (i = 0; i < (mp_appTable->rowCount() * APP_LIST_COLUMN_COUNT); i++)
125     {
126         mp_appTable->verticalHeader()->resizeSection(i, 190);
127         mp_appTable->horizontalHeader()->resizeSection(i, 190);
128     }
129
130     AppInfo ai;
131     for (i = 0; i < apps.size(); ++i)
132     {
133         qDebug("new app: %s", apps.at(i).toStdString().c_str());
134         ai.setName(apps.at(i));
135         mp_appList->append(ai);
136     }
137
138     for (i = 0; i < mp_appList->size(); i++)
139     {
140        mp_appTable->setItem(i / APP_LIST_COLUMN_COUNT,
141                             i % APP_LIST_COLUMN_COUNT,
142                             new QTableWidgetItem(mp_appList->at(i).getName()));
143        mp_appTable->item(i / APP_LIST_COLUMN_COUNT,
144                          i % APP_LIST_COLUMN_COUNT)->setFlags(Qt::ItemIsEnabled);
145        mp_appTable->item(i / APP_LIST_COLUMN_COUNT,
146                          i % APP_LIST_COLUMN_COUNT)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
147     }
148 }
149
150 void AppLauncherWidget::on_tableView_clicked(int row, int col)
151 {
152     if (mp_appList->size() > row * APP_LIST_COLUMN_COUNT + col)
153     {
154 #ifdef __arm__
155         int pid = mp_dBusAppFrameworkProxy->launchApp(mp_appList->at(row * APP_LIST_COLUMN_COUNT + col).getName());
156 #endif
157 #ifdef __i386__
158         QProcess *myProcess = new QProcess();
159         myProcess->start(mp_appList->at(row * APP_LIST_COLUMN_COUNT + col).getName(), NULL);
160         int pid = myProcess->pid();
161 #endif
162         qDebug("%d, %d: start app %s", row, col, mp_appList->at(row * APP_LIST_COLUMN_COUNT + col).getName().toStdString().c_str());
163         qDebug("pid: %d", pid);
164
165         // the new app wants to be visible by default
166         newRequestsToBeVisibleApp(pid);
167     }
168 }