on 2e2f62f help sign in
unity8/tests/mocks/Unity/Application/ApplicationManager.cpp
520 lines 16 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "ApplicationManager.h"
18#include "ApplicationInfo.h"
20#include <paths.h>
22#include <QDir>
23#include <QGuiApplication>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QQmlComponent>
27#include <QTimer>
28#include <QDateTime>
29#include <QDebug>
31ApplicationManager::ApplicationManager(QObject *parent)
32 : ApplicationManagerInterface(parent)
33 , m_mainStageComponent(0)
34 , m_mainStage(0)
35 , m_sideStageComponent(0)
36 , m_sideStage(0)
38 buildListOfAvailableApplications();
41ApplicationManager::~ApplicationManager()
45int ApplicationManager::rowCount(const QModelIndex& parent) const {
46 return !parent.isValid() ? m_runningApplications.size() : 0;
49QVariant ApplicationManager::data(const QModelIndex& index, int role) const {
50 if (index.row() < 0 || index.row() >= m_runningApplications.size())
51 return QVariant();
53 auto app = m_runningApplications.at(index.row());
54 switch(role) {
55 case RoleAppId:
56 return app->appId();
57 case RoleName:
58 return app->name();
59 case RoleComment:
60 return app->comment();
61 case RoleIcon:
62 return app->icon();
63 case RoleStage:
64 return app->stage();
65 case RoleState:
66 return app->state();
67 case RoleFocused:
68 return app->focused();
69 default:
70 return QVariant();
71 }
74ApplicationInfo *ApplicationManager::get(int row) const {
75 if (row < 0 || row >= m_runningApplications.size())
76 return nullptr;
77 return m_runningApplications.at(row);
80ApplicationInfo *ApplicationManager::findApplication(const QString &appId) const {
81 for (ApplicationInfo *app : m_runningApplications) {
82 if (app->appId() == appId) {
83 return app;
84 }
85 }
86 return nullptr;
89void ApplicationManager::add(ApplicationInfo *application) {
90 if (!application)
91 return;
93 beginInsertRows(QModelIndex(), m_runningApplications.size(), m_runningApplications.size());
94 m_runningApplications.append(application);
95 endInsertRows();
96 Q_EMIT countChanged();
99void ApplicationManager::remove(ApplicationInfo *application) {
100 int i = m_runningApplications.indexOf(application);
101 if (i != -1) {
102 beginRemoveRows(QModelIndex(), i, i);
103 m_runningApplications.removeAt(i);
104 endRemoveRows();
105 Q_EMIT countChanged();
106 }
109void ApplicationManager::move(int from, int to) {
110 if (from == to) return;
112 if (from >= 0 && from < m_runningApplications.size() && to >= 0 && to < m_runningApplications.size()) {
113 QModelIndex parent;
114 /* When moving an item down, the destination index needs to be incremented
115 * by one, as explained in the documentation:
116 * http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
117 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
118 m_runningApplications.move(from, to);
119 endMoveRows();
120 }
123int ApplicationManager::keyboardHeight() const
125 return 0;
128bool ApplicationManager::keyboardVisible() const
130 return false;
133int ApplicationManager::sideStageWidth() const
135 return 0;
138ApplicationManager::StageHint ApplicationManager::stageHint() const
140 return MainStage;
143ApplicationManager::FormFactorHint ApplicationManager::formFactorHint() const
145 return PhoneFormFactor;
148ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
149 const QStringList &arguments)
151 return startApplication(appId, NoFlag, arguments);
154ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
155 ExecFlags flags,
156 const QStringList &arguments)
158 Q_UNUSED(arguments)
159 ApplicationInfo *application = 0;
161 for (ApplicationInfo *availableApp : m_availableApplications) {
162 if (availableApp->appId() == appId) {
163 application = availableApp;
164 break;
165 }
166 }
168 if (!application)
169 return 0;
171 if (flags.testFlag(ApplicationManager::ForceMainStage)
172 && application->stage() == ApplicationInfo::SideStage) {
173 application->setStage(ApplicationInfo::MainStage);
174 }
175 add(application);
177 QMetaObject::invokeMethod(this, "focusApplication", Qt::QueuedConnection, Q_ARG(QString, appId));
179 return application;
182bool ApplicationManager::stopApplication(const QString &appId)
184 ApplicationInfo *application = findApplication(appId);
185 if (application == nullptr)
186 return false;
188 remove(application);
189 Q_EMIT focusedApplicationIdChanged();
190 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
191 return true;
194void ApplicationManager::updateScreenshot(const QString &appId)
196 int idx = -1;
197 ApplicationInfo *application = nullptr;
198 for (int i = 0; i < m_availableApplications.count(); ++i) {
199 application = m_availableApplications.at(i);
200 if (application->appId() == appId) {
201 idx = i;
202 break;
203 }
204 }
206 if (idx == -1) {
207 return;
208 }
210 application->setScreenshot(QString("image://application/%1/%2").arg(appId).arg(QDateTime::currentMSecsSinceEpoch()));
211 QModelIndex appIndex = index(idx);
212 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << RoleScreenshot);
215QString ApplicationManager::focusedApplicationId() const {
216 for (ApplicationInfo *app : m_runningApplications) {
217 if (app->focused()) {
218 return app->appId();
219 }
220 }
221 return QString();
224bool ApplicationManager::focusApplication(const QString &appId)
226 ApplicationInfo *application = findApplication(appId);
227 if (application == nullptr)
228 return false;
230 if (application->stage() == ApplicationInfo::MainStage) {
231 // unfocus currently focused mainstage app
232 for (ApplicationInfo *app : m_runningApplications) {
233 if (app->focused() && app->stage() == ApplicationInfo::MainStage) {
234 app->setFocused(false);
235 app->hideWindow();
236 }
237 }
239 // focus this app
240 application->setFocused(true);
241 if (!m_mainStage)
242 createMainStage();
243 application->showWindow(m_mainStage);
244 m_mainStage->setZ(-1000);
245 if (m_sideStage)
246 m_sideStage->setZ(-2000);
247 } else if (application->stage() == ApplicationInfo::SideStage) {
248 // unfocus currently focused sidestage app
249 for (ApplicationInfo *app : m_runningApplications) {
250 if (app->focused() && app->stage() == ApplicationInfo::SideStage) {
251 app->setFocused(false);
252 app->hideWindow();
253 }
254 }
256 // focus this app
257 application->setFocused(true);
258 if (!m_sideStage)
259 createSideStage();
260 application->showWindow(m_sideStage);
261 m_sideStage->setZ(-1000);
262 if (m_mainStage)
263 m_mainStage->setZ(-2000);
264 }
266 // move app to top of stack
267 move(m_runningApplications.indexOf(application), 0);
268 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
269 Q_EMIT focusedApplicationIdChanged();
270 return true;
273void ApplicationManager::activateApplication(const QString &appId)
275 QMetaObject::invokeMethod(this, "requestFocus", Qt::QueuedConnection, Q_ARG(QString, appId));
278void ApplicationManager::unfocusCurrentApplication()
280 for (ApplicationInfo *app : m_runningApplications) {
281 if (app->focused()) {
282 app->hideWindow();
283 app->setFocused(false);
284 }
285 }
286 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
287 Q_EMIT focusedApplicationIdChanged();
290void ApplicationManager::generateQmlStrings(ApplicationInfo *application)
292 // TODO: Is there a better way of solving this fullscreen vs. regular
293 // application height?
294 QString topMargin;
295 if (application->fullscreen()) {
296 topMargin.append("0");
297 } else {
298 // Taken from Panel.panelHeight
299 topMargin.append("units.gu(3) + units.dp(2)");
300 }
302 QString windowQml = QString(
303 "import QtQuick 2.0\n"
304 "Image {\n"
305 " anchors.fill: parent\n"
306 " anchors.topMargin: %1\n"
307 " source: \"file://%2/Dash/graphics/phone/screenshots/%3.png\"\n"
308 " smooth: true\n"
309 " fillMode: Image.PreserveAspectCrop\n"
310 "}").arg(topMargin)
311 .arg(qmlDirectory())
312 .arg(application->icon().toString());
313 application->setWindowQml(windowQml);
315 QString imageQml = QString(
316 "import QtQuick 2.0\n"
317 "Image {\n"
318 " anchors.fill: parent\n"
319 " source: \"file://%1/Dash/graphics/phone/screenshots/%2.png\"\n"
320 " smooth: true\n"
321 " fillMode: Image.PreserveAspectCrop\n"
322 "}").arg(qmlDirectory())
323 .arg(application->icon().toString());
324 application->setImageQml(imageQml);
327void ApplicationManager::buildListOfAvailableApplications()
329 ApplicationInfo *application;
331 application = new ApplicationInfo(this);
332 application->setAppId("phone-app");
333 application->setName("Phone");
334 application->setIcon(QUrl("phone"));
335 application->setStage(ApplicationInfo::SideStage);
336 generateQmlStrings(application);
337 m_availableApplications.append(application);
339 application = new ApplicationInfo(this);
340 application->setAppId("camera-app");
341 application->setName("Camera");
342 application->setIcon(QUrl("camera"));
343 application->setFullscreen(true);
344 generateQmlStrings(application);
345 m_availableApplications.append(application);
347 application = new ApplicationInfo(this);
348 application->setAppId("gallery-app");
349 application->setName("Gallery");
350 application->setIcon(QUrl("gallery"));
351 generateQmlStrings(application);
352 m_availableApplications.append(application);
354 application = new ApplicationInfo(this);
355 application->setAppId("facebook-webapp");
356 application->setName("Facebook");
357 application->setIcon(QUrl("facebook"));
358 application->setStage(ApplicationInfo::SideStage);
359 generateQmlStrings(application);
360 m_availableApplications.append(application);
362 application = new ApplicationInfo(this);
363 application->setAppId("webbrowser-app");
364 application->setName("Browser");
365 application->setIcon(QUrl("browser"));
366 generateQmlStrings(application);
367 m_availableApplications.append(application);
369 application = new ApplicationInfo(this);
370 application->setAppId("twitter-webapp");
371 application->setName("Twitter");
372 application->setIcon(QUrl("twitter"));
373 application->setStage(ApplicationInfo::SideStage);
374 generateQmlStrings(application);
375 m_availableApplications.append(application);
377 application = new ApplicationInfo(this);
378 application->setAppId("gmail-webapp");
379 application->setName("GMail");
380 application->setIcon(QUrl("gmail"));
381 m_availableApplications.append(application);
383 application = new ApplicationInfo(this);
384 application->setAppId("ubuntu-weather-app");
385 application->setName("Weather");
386 application->setIcon(QUrl("weather"));
387 application->setStage(ApplicationInfo::SideStage);
388 generateQmlStrings(application);
389 m_availableApplications.append(application);
391 application = new ApplicationInfo(this);
392 application->setAppId("notes-app");
393 application->setName("Notepad");
394 application->setIcon(QUrl("notepad"));
395 application->setStage(ApplicationInfo::SideStage);
396 m_availableApplications.append(application);
398 application = new ApplicationInfo(this);
399 application->setAppId("calendar-app");
400 application->setName("Calendar");
401 application->setIcon(QUrl("calendar"));
402 application->setStage(ApplicationInfo::SideStage);
403 m_availableApplications.append(application);
405 application = new ApplicationInfo(this);
406 application->setAppId("mediaplayer-app");
407 application->setName("Media Player");
408 application->setIcon(QUrl("mediaplayer-app"));
409 application->setFullscreen(true);
410 m_availableApplications.append(application);
412 application = new ApplicationInfo(this);
413 application->setAppId("evernote");
414 application->setName("Evernote");
415 application->setIcon(QUrl("evernote"));
416 m_availableApplications.append(application);
418 application = new ApplicationInfo(this);
419 application->setAppId("map");
420 application->setName("Map");
421 application->setIcon(QUrl("map"));
422 generateQmlStrings(application);
423 m_availableApplications.append(application);
425 application = new ApplicationInfo(this);
426 application->setAppId("pinterest");
427 application->setName("Pinterest");
428 application->setIcon(QUrl("pinterest"));
429 m_availableApplications.append(application);
431 application = new ApplicationInfo(this);
432 application->setAppId("soundcloud");
433 application->setName("SoundCloud");
434 application->setIcon(QUrl("soundcloud"));
435 m_availableApplications.append(application);
437 application = new ApplicationInfo(this);
438 application->setAppId("wikipedia");
439 application->setName("Wikipedia");
440 application->setIcon(QUrl("wikipedia"));
441 m_availableApplications.append(application);
443 application = new ApplicationInfo(this);
444 application->setAppId("youtube");
445 application->setName("YouTube");
446 application->setIcon(QUrl("youtube"));
447 m_availableApplications.append(application);
450void ApplicationManager::createMainStageComponent()
452 // The assumptions I make here really should hold.
453 QQuickView *quickView =
454 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
456 QQmlEngine *engine = quickView->engine();
458 m_mainStageComponent = new QQmlComponent(engine, this);
459 QString mainStageQml =
460 "import QtQuick 2.0\n"
461 "Rectangle {\n"
462 " anchors.fill: parent\n"
463 " color: 'black'\n"
464 " z: -2000\n"
465 "}\n";
466 m_mainStageComponent->setData(mainStageQml.toLatin1(), QUrl());
469void ApplicationManager::createMainStage()
471 if (!m_mainStageComponent)
472 createMainStageComponent();
474 // The assumptions I make here really should hold.
475 QQuickView *quickView =
476 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
478 QQuickItem *shell = quickView->rootObject();
480 m_mainStage = qobject_cast<QQuickItem *>(m_mainStageComponent->create());
481 m_mainStage->setParentItem(shell);
484void ApplicationManager::createSideStageComponent()
486 // The assumptions I make here really should hold.
487 QQuickView *quickView =
488 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
490 QQmlEngine *engine = quickView->engine();
492 m_sideStageComponent = new QQmlComponent(engine, this);
493 QString sideStageQml =
494 "import QtQuick 2.0\n"
495 "import Ubuntu.Components 0.1\n"
496 "Item {\n"
497 " width: units.gu(40)\n" // from SideStage in Shell.qml
498 " anchors.top: parent.top\n"
499 " anchors.bottom: parent.bottom\n"
500 " anchors.right: parent.right\n"
501 " z: -1000\n"
502 "}\n";
503 m_sideStageComponent->setData(sideStageQml.toLatin1(), QUrl());
506void ApplicationManager::createSideStage()
508 if (!m_sideStageComponent)
509 createSideStageComponent();
511 // The assumptions I make here really should hold.
512 QQuickView *quickView =
513 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
515 QQuickItem *shell = quickView->rootObject();
517 m_sideStage = qobject_cast<QQuickItem *>(m_sideStageComponent->create());
518 m_sideStage->setParentItem(shell);
519 m_sideStage->setFlag(QQuickItem::ItemHasContents, false);