on 3d2e4e5 help sign in
unity8/tests/mocks/Unity/Application/ApplicationManager.cpp
620 lines 19 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 "ApplicationDBusAdaptor.h"
19#include "ApplicationInfo.h"
20#include "MirSurfaceItem.h"
22#include <paths.h>
24#include <QDir>
25#include <QGuiApplication>
26#include <QQuickItem>
27#include <QQuickView>
28#include <QQmlComponent>
29#include <QTimer>
30#include <QDateTime>
31#include <QtDBus/QtDBus>
33ApplicationManager *ApplicationManager::the_application_manager = nullptr;
35ApplicationManager *ApplicationManager::singleton()
37 if (!the_application_manager) {
38 the_application_manager = new ApplicationManager();
39 new ApplicationDBusAdaptor(the_application_manager);
41 QDBusConnection connection = QDBusConnection::sessionBus();
42 connection.registerService("com.canonical.Unity8");
43 connection.registerObject("/com/canonical/Unity8/Mocks", the_application_manager);
44 }
45 return the_application_manager;
48ApplicationManager::ApplicationManager(QObject *parent)
49 : ApplicationManagerInterface(parent)
50 , m_suspended(false)
51 , m_mainStageComponent(0)
52 , m_mainStage(0)
53 , m_sideStageComponent(0)
54 , m_sideStage(0)
55 , m_rightMargin(0)
57 m_roleNames.insert(RoleSurface, "surface");
58 m_roleNames.insert(RoleFullscreen, "fullscreen");
60 buildListOfAvailableApplications();
63ApplicationManager::~ApplicationManager()
67int ApplicationManager::rowCount(const QModelIndex& parent) const {
68 return !parent.isValid() ? m_runningApplications.size() : 0;
71QVariant ApplicationManager::data(const QModelIndex& index, int role) const {
72 if (index.row() < 0 || index.row() >= m_runningApplications.size())
73 return QVariant();
75 auto app = m_runningApplications.at(index.row());
76 switch(role) {
77 case RoleAppId:
78 return app->appId();
79 case RoleName:
80 return app->name();
81 case RoleComment:
82 return app->comment();
83 case RoleIcon:
84 return app->icon();
85 case RoleStage:
86 return app->stage();
87 case RoleState:
88 return app->state();
89 case RoleFocused:
90 return app->focused();
91 case RoleScreenshot:
92 return app->screenshot();
93 case RoleSurface:
94 return QVariant::fromValue(app->surface());
95 case RoleFullscreen:
96 return app->fullscreen();
97 default:
98 return QVariant();
99 }
102ApplicationInfo *ApplicationManager::get(int row) const {
103 if (row < 0 || row >= m_runningApplications.size())
104 return nullptr;
105 return m_runningApplications.at(row);
108ApplicationInfo *ApplicationManager::findApplication(const QString &appId) const {
109 for (ApplicationInfo *app : m_runningApplications) {
110 if (app->appId() == appId) {
111 return app;
112 }
113 }
114 return nullptr;
117QModelIndex ApplicationManager::findIndex(ApplicationInfo* application)
119 for (int i = 0; i < m_runningApplications.size(); ++i) {
120 if (m_runningApplications.at(i) == application) {
121 return index(i);
122 }
123 }
125 return QModelIndex();
128void ApplicationManager::add(ApplicationInfo *application) {
129 if (!application) {
130 return;
131 }
133 beginInsertRows(QModelIndex(), m_runningApplications.size(), m_runningApplications.size());
134 m_runningApplications.append(application);
135 endInsertRows();
136 Q_EMIT applicationAdded(application->appId());
137 Q_EMIT countChanged();
138 Q_EMIT focusRequested(application->appId());
140 connect(application, &ApplicationInfo::surfaceChanged, this, [application, this]() {
141 QModelIndex appIndex = findIndex(application);
142 if (!appIndex.isValid()) return;
143 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << ApplicationManager::RoleSurface);
144 });
147void ApplicationManager::remove(ApplicationInfo *application) {
148 int i = m_runningApplications.indexOf(application);
149 if (i != -1) {
150 beginRemoveRows(QModelIndex(), i, i);
151 m_runningApplications.removeAt(i);
152 endRemoveRows();
153 Q_EMIT applicationRemoved(application->appId());
154 Q_EMIT countChanged();
155 }
156 disconnect(application, &ApplicationInfo::surfaceChanged, this, 0);
159void ApplicationManager::move(int from, int to) {
160 if (from == to) return;
162 if (from >= 0 && from < m_runningApplications.size() && to >= 0 && to < m_runningApplications.size()) {
163 QModelIndex parent;
164 /* When moving an item down, the destination index needs to be incremented
165 * by one, as explained in the documentation:
166 * http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
167 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
168 m_runningApplications.move(from, to);
169 endMoveRows();
170 }
173int ApplicationManager::keyboardHeight() const
175 return 0;
178bool ApplicationManager::keyboardVisible() const
180 return false;
183int ApplicationManager::sideStageWidth() const
185 return 0;
188ApplicationManager::StageHint ApplicationManager::stageHint() const
190 return MainStage;
193ApplicationManager::FormFactorHint ApplicationManager::formFactorHint() const
195 return PhoneFormFactor;
198ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
199 const QStringList &arguments)
201 return startApplication(appId, NoFlag, arguments);
204ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
205 ExecFlags flags,
206 const QStringList &arguments)
208 Q_UNUSED(arguments)
209 ApplicationInfo *application = 0;
211 for (ApplicationInfo *availableApp : m_availableApplications) {
212 if (availableApp->appId() == appId) {
213 application = availableApp;
214 break;
215 }
216 }
218 if (!application)
219 return 0;
221 if (flags.testFlag(ApplicationManager::ForceMainStage)
222 && application->stage() == ApplicationInfo::SideStage) {
223 application->setStage(ApplicationInfo::MainStage);
224 }
225 add(application);
226 application->setState(ApplicationInfo::Running);
228 return application;
231bool ApplicationManager::stopApplication(const QString &appId)
233 ApplicationInfo *application = findApplication(appId);
234 if (application == nullptr)
235 return false;
237 if (application->appId() == focusedApplicationId()) {
238 unfocusCurrentApplication();
239 }
240 application->setState(ApplicationInfo::Stopped);
241 remove(application);
242 return true;
245bool ApplicationManager::updateScreenshot(const QString &appId)
247 int idx = -1;
248 ApplicationInfo *application = nullptr;
249 for (int i = 0; i < m_availableApplications.count(); ++i) {
250 application = m_availableApplications.at(i);
251 if (application->appId() == appId) {
252 idx = i;
253 break;
254 }
255 }
257 if (idx == -1) {
258 return false;
259 }
261 QModelIndex appIndex = index(idx);
262 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << RoleScreenshot);
263 return true;
266QString ApplicationManager::focusedApplicationId() const {
267 for (ApplicationInfo *app : m_runningApplications) {
268 if (app->focused()) {
269 return app->appId();
270 }
271 }
272 return QString();
275bool ApplicationManager::suspended() const
277 return m_suspended;
280void ApplicationManager::setSuspended(bool suspended)
282 ApplicationInfo *focusedApp = findApplication(focusedApplicationId());
283 if (focusedApp) {
284 if (suspended) {
285 focusedApp->setState(ApplicationInfo::Suspended);
286 } else {
287 focusedApp->setState(ApplicationInfo::Running);
288 }
289 }
290 m_suspended = suspended;
291 Q_EMIT suspendedChanged();
294bool ApplicationManager::focusApplication(const QString &appId)
296 ApplicationInfo *application = findApplication(appId);
297 if (application == nullptr)
298 return false;
300 if (application->stage() == ApplicationInfo::MainStage) {
301 // unfocus currently focused mainstage app
302 for (ApplicationInfo *app : m_runningApplications) {
303 if (app->focused() && app->stage() == ApplicationInfo::MainStage) {
304 app->setFocused(false);
305 app->hideWindow();
306 }
307 }
309 // focus this app
310 application->setFocused(true);
311 if (!m_mainStage)
312 createMainStage();
313 application->showWindow(m_mainStage);
314 m_mainStage->setZ(-1000);
315 if (m_sideStage)
316 m_sideStage->setZ(-2000);
317 } else if (application->stage() == ApplicationInfo::SideStage) {
318 // unfocus currently focused sidestage app
319 for (ApplicationInfo *app : m_runningApplications) {
320 if (app->focused() && app->stage() == ApplicationInfo::SideStage) {
321 app->setFocused(false);
322 app->hideWindow();
323 }
324 }
326 // focus this app
327 application->setFocused(true);
328 if (!m_sideStage)
329 createSideStage();
330 application->showWindow(m_sideStage);
331 m_sideStage->setZ(-1000);
332 if (m_mainStage)
333 m_mainStage->setZ(-2000);
334 }
336 // move app to top of stack
337 move(m_runningApplications.indexOf(application), 0);
338 Q_EMIT focusedApplicationIdChanged();
339 return true;
342bool ApplicationManager::requestFocusApplication(const QString &appId)
344 if (appId != focusedApplicationId()) {
345 QMetaObject::invokeMethod(this, "focusRequested", Qt::QueuedConnection, Q_ARG(QString, appId));
346 return true;
347 }
348 return false;
351void ApplicationManager::unfocusCurrentApplication()
353 for (ApplicationInfo *app : m_runningApplications) {
354 if (app->focused()) {
355 app->hideWindow();
356 app->setFocused(false);
357 }
358 }
359 Q_EMIT focusedApplicationIdChanged();
362void ApplicationManager::generateQmlStrings(ApplicationInfo *application)
364 // TODO: Is there a better way of solving this fullscreen vs. regular
365 // application height?
366 QString topMargin;
367 if (application->fullscreen()) {
368 topMargin.append("0");
369 } else {
370 // Taken from Panel.panelHeight
371 topMargin.append("units.gu(3) + units.dp(2)");
372 }
374 QString windowQml = QString(
375 "import QtQuick 2.0\n"
376 "Image {\n"
377 " anchors.fill: parent\n"
378 " anchors.topMargin: %1\n"
379 " anchors.rightMargin: %2\n"
380 " source: \"file://%3/Dash/graphics/phone/screenshots/%4.png\"\n"
381 " smooth: true\n"
382 " fillMode: Image.PreserveAspectCrop\n"
383 "}").arg(topMargin)
384 .arg(m_rightMargin)
385 .arg(qmlDirectory())
386 .arg(application->icon().toString());
387 application->setWindowQml(windowQml);
389 QString imageQml = QString(
390 "import QtQuick 2.0\n"
391 "Image {\n"
392 " anchors.fill: parent\n"
393 " source: \"file://%1/Dash/graphics/phone/screenshots/%2.png\"\n"
394 " smooth: true\n"
395 " fillMode: Image.PreserveAspectCrop\n"
396 "}").arg(qmlDirectory())
397 .arg(application->icon().toString());
398 application->setImageQml(imageQml);
400 application->setScreenshot(QString("file://%1/Dash/graphics/phone/screenshots/%2@12.png").arg(qmlDirectory())
401 .arg(application->icon().toString()));
404void ApplicationManager::buildListOfAvailableApplications()
406 ApplicationInfo *application;
408 application = new ApplicationInfo(this);
409 application->setAppId("dialer-app");
410 application->setName("Dialer");
411 application->setIcon(QUrl("dialer"));
412 application->setStage(ApplicationInfo::SideStage);
413 generateQmlStrings(application);
414 m_availableApplications.append(application);
416 application = new ApplicationInfo(this);
417 application->setAppId("camera-app");
418 application->setName("Camera");
419 application->setIcon(QUrl("camera"));
420 application->setFullscreen(true);
421 generateQmlStrings(application);
422 m_availableApplications.append(application);
424 application = new ApplicationInfo(this);
425 application->setAppId("gallery-app");
426 application->setName("Gallery");
427 application->setIcon(QUrl("gallery"));
428 generateQmlStrings(application);
429 m_availableApplications.append(application);
431 application = new ApplicationInfo(this);
432 application->setAppId("facebook-webapp");
433 application->setName("Facebook");
434 application->setIcon(QUrl("facebook"));
435 application->setStage(ApplicationInfo::SideStage);
436 generateQmlStrings(application);
437 m_availableApplications.append(application);
439 application = new ApplicationInfo(this);
440 application->setAppId("webbrowser-app");
441 application->setFullscreen(true);
442 application->setName("Browser");
443 application->setIcon(QUrl("browser"));
444 generateQmlStrings(application);
445 m_availableApplications.append(application);
447 application = new ApplicationInfo(this);
448 application->setAppId("twitter-webapp");
449 application->setName("Twitter");
450 application->setIcon(QUrl("twitter"));
451 application->setStage(ApplicationInfo::SideStage);
452 generateQmlStrings(application);
453 m_availableApplications.append(application);
455 application = new ApplicationInfo(this);
456 application->setAppId("gmail-webapp");
457 application->setName("GMail");
458 application->setIcon(QUrl("gmail"));
459 m_availableApplications.append(application);
461 application = new ApplicationInfo(this);
462 application->setAppId("ubuntu-weather-app");
463 application->setName("Weather");
464 application->setIcon(QUrl("weather"));
465 application->setStage(ApplicationInfo::SideStage);
466 generateQmlStrings(application);
467 m_availableApplications.append(application);
469 application = new ApplicationInfo(this);
470 application->setAppId("notes-app");
471 application->setName("Notepad");
472 application->setIcon(QUrl("notepad"));
473 application->setStage(ApplicationInfo::SideStage);
474 m_availableApplications.append(application);
476 application = new ApplicationInfo(this);
477 application->setAppId("calendar-app");
478 application->setName("Calendar");
479 application->setIcon(QUrl("calendar"));
480 application->setStage(ApplicationInfo::SideStage);
481 m_availableApplications.append(application);
483 application = new ApplicationInfo(this);
484 application->setAppId("mediaplayer-app");
485 application->setName("Media Player");
486 application->setIcon(QUrl("mediaplayer-app"));
487 application->setFullscreen(true);
488 m_availableApplications.append(application);
490 application = new ApplicationInfo(this);
491 application->setAppId("evernote");
492 application->setName("Evernote");
493 application->setIcon(QUrl("evernote"));
494 m_availableApplications.append(application);
496 application = new ApplicationInfo(this);
497 application->setAppId("map");
498 application->setName("Map");
499 application->setIcon(QUrl("map"));
500 generateQmlStrings(application);
501 m_availableApplications.append(application);
503 application = new ApplicationInfo(this);
504 application->setAppId("pinterest");
505 application->setName("Pinterest");
506 application->setIcon(QUrl("pinterest"));
507 m_availableApplications.append(application);
509 application = new ApplicationInfo(this);
510 application->setAppId("soundcloud");
511 application->setName("SoundCloud");
512 application->setIcon(QUrl("soundcloud"));
513 m_availableApplications.append(application);
515 application = new ApplicationInfo(this);
516 application->setAppId("wikipedia");
517 application->setName("Wikipedia");
518 application->setIcon(QUrl("wikipedia"));
519 m_availableApplications.append(application);
521 application = new ApplicationInfo(this);
522 application->setAppId("youtube");
523 application->setName("YouTube");
524 application->setIcon(QUrl("youtube"));
525 m_availableApplications.append(application);
528void ApplicationManager::createMainStageComponent()
530 // The assumptions I make here really should hold.
531 QQuickView *quickView =
532 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
534 QQmlEngine *engine = quickView->engine();
536 m_mainStageComponent = new QQmlComponent(engine, this);
537 QString mainStageQml =
538 "import QtQuick 2.0\n"
539 "Rectangle {\n"
540 " anchors.fill: parent\n"
541 " color: 'black'\n"
542 " z: -2000\n"
543 "}\n";
544 m_mainStageComponent->setData(mainStageQml.toLatin1(), QUrl());
547void ApplicationManager::createMainStage()
549 if (!m_mainStageComponent)
550 createMainStageComponent();
552 // The assumptions I make here really should hold.
553 QQuickView *quickView =
554 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
556 QQuickItem *shell = quickView->rootObject();
558 m_mainStage = qobject_cast<QQuickItem *>(m_mainStageComponent->create());
559 m_mainStage->setParentItem(shell);
562void ApplicationManager::createSideStageComponent()
564 // The assumptions I make here really should hold.
565 QQuickView *quickView =
566 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
568 QQmlEngine *engine = quickView->engine();
570 m_sideStageComponent = new QQmlComponent(engine, this);
571 QString sideStageQml =
572 "import QtQuick 2.0\n"
573 "import Ubuntu.Components 0.1\n"
574 "Item {\n"
575 " width: units.gu(40)\n" // from SideStage in Shell.qml
576 " anchors.top: parent.top\n"
577 " anchors.bottom: parent.bottom\n"
578 " anchors.right: parent.right\n"
579 " z: -1000\n"
580 "}\n";
581 m_sideStageComponent->setData(sideStageQml.toLatin1(), QUrl());
584void ApplicationManager::createSideStage()
586 if (!m_sideStageComponent)
587 createSideStageComponent();
589 // The assumptions I make here really should hold.
590 QQuickView *quickView =
591 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
593 QQuickItem *shell = quickView->rootObject();
595 m_sideStage = qobject_cast<QQuickItem *>(m_sideStageComponent->create());
596 m_sideStage->setParentItem(shell);
597 m_sideStage->setFlag(QQuickItem::ItemHasContents, false);
600QStringList ApplicationManager::availableApplications()
602 QStringList appIds;
603 Q_FOREACH(ApplicationInfo *app, m_availableApplications) {
604 appIds << app->appId();
605 }
606 return appIds;
609int ApplicationManager::rightMargin() const
611 return m_rightMargin;
614void ApplicationManager::setRightMargin(int rightMargin)
616 m_rightMargin = rightMargin;
617 Q_FOREACH(ApplicationInfo *app, m_availableApplications) {
618 generateQmlStrings(app);
619 }