Using the Tizen application manager to receive information about installed apps and...
[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
22 #define APP_LIST_COLUMN_COUNT 5
23
24 AppLauncherWidget::AppLauncherWidget(QWidget *parent) :
25     QWidget(parent),
26     mp_ui(new Ui::AppLauncherWidget),
27     mp_appList(new QList<AppInfo>()),
28     mp_appTable(0),
29     mp_dBusAppFrameworkProxy()
30 {
31     mp_ui->setupUi(this);
32
33     AppInfo ai;
34     for (int i = 0; i < 100; ++i)
35     {
36         ai.setName("test" + QString::number(i));
37         mp_appList->append(ai);
38     }
39
40     qDebug("D-Bus: connect to org.agl.homescreenappframeworkbindertizen /AppFramework");
41     mp_dBusAppFrameworkProxy = new org::agl::appframework("org.agl.homescreenappframeworkbindertizen",
42                                               "/AppFramework",
43                                               QDBusConnection::sessionBus(),
44                                               0);
45
46     populateAppList();
47 }
48
49 AppLauncherWidget::~AppLauncherWidget()
50 {
51     delete mp_dBusAppFrameworkProxy;
52     if (0 != mp_appTable)
53     {
54         delete mp_appTable;
55     }
56     delete mp_appList;
57     delete mp_ui;
58 }
59
60 void AppLauncherWidget::updateColorScheme()
61 {
62     QSettings settings;
63     QSettings settings_cs(QApplication::applicationDirPath() +
64                           "/colorschemes/" +
65                           settings.value("systemsettings/colorscheme", "default").toString() +
66                           "/" +
67                           QString::number(settings.value("systemsettings/daynightmode", SystemDayNight::DAYNIGHTMODE_DAY).toInt()) +
68                           ".ini",
69                           QSettings::IniFormat);
70
71     mp_ui->widget_Background->setStyleSheet(settings_cs.value("AppLauncherWidget/widget_Background").toString());
72     mp_ui->widget_Home_Icon->setStyleSheet(settings_cs.value("AppLauncherWidget/widget_Home_Icon").toString());
73 }
74
75 void AppLauncherWidget::populateAppList()
76 {
77     setStyleSheet("QTableWidget {background-color: transparent;}"
78                   "QTableCornerButton::section {background-color: transparent;}");
79
80     if (0 == mp_appTable)
81     {
82         mp_appTable = new QTableWidget(this);
83         QObject::connect(mp_appTable, SIGNAL(cellClicked(int, int)), this, SLOT(on_tableView_clicked(int, int)));
84     }
85     else
86     {
87         mp_appTable->clear();
88     }
89
90     mp_appTable->setShowGrid(false);
91     mp_appTable->setFrameShape(QFrame::NoFrame);
92     mp_appTable->move(40, 40);
93     mp_appTable->resize(1000, 1920 - 40 - 40 - 60 - 60);
94     mp_appTable->horizontalHeader()->setVisible(false);
95     mp_appTable->verticalHeader()->setVisible(false);
96     mp_appTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
97     mp_appTable->setRowCount(100);
98     mp_appTable->setColumnCount(APP_LIST_COLUMN_COUNT);
99
100     int i;
101
102     QStringList apps = mp_dBusAppFrameworkProxy->getAvailableAppNames();
103     mp_appList->clear();
104
105     mp_appTable->setRowCount((apps.size() + (APP_LIST_COLUMN_COUNT - 1)) / APP_LIST_COLUMN_COUNT);
106
107     if (apps.size() >= (9 * APP_LIST_COLUMN_COUNT))
108     {
109         mp_appTable->resize(1000, 1920 - 40 - 40 - 60 - 60);
110     }
111     else
112     {
113         mp_appTable->resize(1000, mp_appTable->rowCount() * 190);
114     }
115
116
117     for (i = 0; i < (mp_appTable->rowCount() * APP_LIST_COLUMN_COUNT); i++)
118     {
119         mp_appTable->verticalHeader()->resizeSection(i, 190);
120         mp_appTable->horizontalHeader()->resizeSection(i, 190);
121     }
122
123     AppInfo ai;
124     for (i = 0; i < apps.size(); ++i)
125     {
126         qDebug("new app: %s", apps.at(i).toStdString().c_str());
127         ai.setName(apps.at(i));
128         mp_appList->append(ai);
129     }
130
131     for (i = 0; i < mp_appList->size(); i++)
132     {
133        mp_appTable->setItem(i / APP_LIST_COLUMN_COUNT,
134                             i % APP_LIST_COLUMN_COUNT,
135                             new QTableWidgetItem(mp_appList->at(i).getName()));
136        mp_appTable->item(i / APP_LIST_COLUMN_COUNT,
137                          i % APP_LIST_COLUMN_COUNT)->setFlags(Qt::ItemIsEnabled);
138        mp_appTable->item(i / APP_LIST_COLUMN_COUNT,
139                          i % APP_LIST_COLUMN_COUNT)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
140     }
141
142
143 }
144
145 void AppLauncherWidget::on_tableView_clicked(int row, int col)
146 {
147     if (mp_appList->size() > row * APP_LIST_COLUMN_COUNT + col)
148     {
149         int pid = mp_dBusAppFrameworkProxy->launchApp(mp_appList->at(row * APP_LIST_COLUMN_COUNT + col).getName());
150         qDebug("%d, %d: start app %s", row, col, mp_appList->at(row * APP_LIST_COLUMN_COUNT + col).getName().toStdString().c_str());
151         qDebug("pid: %d", pid);
152     }
153 }