on 451cbdb help sign in
unity8/tests/mocks/Unity/Application/ApplicationManager.cpp
621 lines 20 kB history raw
1/*
2 * Copyright (C) 2013-2014 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 if (count() == 1) Q_EMIT emptyChanged(isEmpty()); // was empty but not anymore
139 Q_EMIT focusRequested(application->appId());
141 connect(application, &ApplicationInfo::surfaceChanged, this, [application, this]() {
142 QModelIndex appIndex = findIndex(application);
143 if (!appIndex.isValid()) return;
144 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << ApplicationManager::RoleSurface);
145 });
148void ApplicationManager::remove(ApplicationInfo *application) {
149 int i = m_runningApplications.indexOf(application);
150 if (i != -1) {
151 beginRemoveRows(QModelIndex(), i, i);
152 m_runningApplications.removeAt(i);
153 endRemoveRows();
154 Q_EMIT applicationRemoved(application->appId());
155 Q_EMIT countChanged();
156 if (isEmpty()) Q_EMIT emptyChanged(isEmpty());
157 }
158 disconnect(application, &ApplicationInfo::surfaceChanged, this, 0);
161void ApplicationManager::move(int from, int to) {
162 if (from == to) return;
164 if (from >= 0 && from < m_runningApplications.size() && to >= 0 && to < m_runningApplications.size()) {
165 QModelIndex parent;
166 /* When moving an item down, the destination index needs to be incremented
167 * by one, as explained in the documentation:
168 * http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
169 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
170 m_runningApplications.move(from, to);
171 endMoveRows();
172 }
175int ApplicationManager::sideStageWidth() const
177 return 0;
180ApplicationManager::StageHint ApplicationManager::stageHint() const
182 return MainStage;
185ApplicationManager::FormFactorHint ApplicationManager::formFactorHint() const
187 return PhoneFormFactor;
190ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
191 const QStringList &arguments)
193 return startApplication(appId, NoFlag, arguments);
196ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
197 ExecFlags flags,
198 const QStringList &arguments)
200 Q_UNUSED(arguments)
201 ApplicationInfo *application = 0;
203 for (ApplicationInfo *availableApp : m_availableApplications) {
204 if (availableApp->appId() == appId) {
205 application = availableApp;
206 break;
207 }
208 }
210 if (!application)
211 return 0;
213 if (flags.testFlag(ApplicationManager::ForceMainStage)
214 && application->stage() == ApplicationInfo::SideStage) {
215 application->setStage(ApplicationInfo::MainStage);
216 }
217 add(application);
218 application->setState(ApplicationInfo::Running);
220 return application;
223bool ApplicationManager::stopApplication(const QString &appId)
225 ApplicationInfo *application = findApplication(appId);
226 if (application == nullptr)
227 return false;
229 if (application->appId() == focusedApplicationId()) {
230 unfocusCurrentApplication();
231 }
232 application->setState(ApplicationInfo::Stopped);
233 remove(application);
234 return true;
237bool ApplicationManager::updateScreenshot(const QString &appId)
239 int idx = -1;
240 ApplicationInfo *application = nullptr;
241 for (int i = 0; i < m_availableApplications.count(); ++i) {
242 application = m_availableApplications.at(i);
243 if (application->appId() == appId) {
244 idx = i;
245 break;
246 }
247 }
249 if (idx == -1) {
250 return false;
251 }
253 QModelIndex appIndex = index(idx);
254 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << RoleScreenshot);
255 return true;
258QString ApplicationManager::focusedApplicationId() const {
259 for (ApplicationInfo *app : m_runningApplications) {
260 if (app->focused()) {
261 return app->appId();
262 }
263 }
264 return QString();
267bool ApplicationManager::suspended() const
269 return m_suspended;
272void ApplicationManager::setSuspended(bool suspended)
274 ApplicationInfo *focusedApp = findApplication(focusedApplicationId());
275 if (focusedApp) {
276 if (suspended) {
277 focusedApp->setState(ApplicationInfo::Suspended);
278 } else {
279 focusedApp->setState(ApplicationInfo::Running);
280 }
281 }
282 m_suspended = suspended;
283 Q_EMIT suspendedChanged();
286bool ApplicationManager::focusApplication(const QString &appId)
288 ApplicationInfo *application = findApplication(appId);
289 if (application == nullptr)
290 return false;
292 if (application->stage() == ApplicationInfo::MainStage) {
293 // unfocus currently focused mainstage app
294 for (ApplicationInfo *app : m_runningApplications) {
295 if (app->focused() && app->stage() == ApplicationInfo::MainStage) {
296 app->setFocused(false);
297 app->hideWindow();
298 app->setState(ApplicationInfo::Suspended);
299 }
300 }
302 // focus this app
303 application->setFocused(true);
304 if (!m_mainStage)
305 createMainStage();
306 application->setState(ApplicationInfo::Running);
307 application->showWindow(m_mainStage);
308 m_mainStage->setZ(-1000);
309 if (m_sideStage)
310 m_sideStage->setZ(-2000);
311 } else if (application->stage() == ApplicationInfo::SideStage) {
312 // unfocus currently focused sidestage app
313 for (ApplicationInfo *app : m_runningApplications) {
314 if (app->focused() && app->stage() == ApplicationInfo::SideStage) {
315 app->setFocused(false);
316 app->hideWindow();
317 app->setState(ApplicationInfo::Suspended);
318 }
319 }
321 // focus this app
322 application->setFocused(true);
323 if (!m_sideStage)
324 createSideStage();
325 application->setState(ApplicationInfo::Running);
326 application->showWindow(m_sideStage);
327 m_sideStage->setZ(-1000);
328 if (m_mainStage)
329 m_mainStage->setZ(-2000);
330 }
332 // move app to top of stack
333 move(m_runningApplications.indexOf(application), 0);
334 Q_EMIT focusedApplicationIdChanged();
335 return true;
338bool ApplicationManager::requestFocusApplication(const QString &appId)
340 if (appId != focusedApplicationId()) {
341 QMetaObject::invokeMethod(this, "focusRequested", Qt::QueuedConnection, Q_ARG(QString, appId));
342 return true;
343 }
344 return false;
347void ApplicationManager::unfocusCurrentApplication()
349 for (ApplicationInfo *app : m_runningApplications) {
350 if (app->focused()) {
351 app->hideWindow();
352 app->setFocused(false);
353 }
354 }
355 Q_EMIT focusedApplicationIdChanged();
358void ApplicationManager::generateQmlStrings(ApplicationInfo *application)
360 // TODO: Is there a better way of solving this fullscreen vs. regular
361 // application height?
362 QString topMargin;
363 if (application->fullscreen()) {
364 topMargin.append("0");
365 } else {
366 // Taken from Panel.panelHeight
367 topMargin.append("units.gu(3) + units.dp(2)");
368 }
370 QString windowQml = QString(
371 "import QtQuick 2.0\n"
372 "Image {\n"
373 " anchors.fill: parent\n"
374 " anchors.topMargin: %1\n"
375 " anchors.rightMargin: %2\n"
376 " source: \"file://%3/Dash/graphics/phone/screenshots/%4.png\"\n"
377 " smooth: true\n"
378 " fillMode: Image.PreserveAspectCrop\n"
379 "}").arg(topMargin)
380 .arg(m_rightMargin)
381 .arg(qmlDirectory())
382 .arg(application->icon().toString());
383 application->setWindowQml(windowQml);
385 QString imageQml = QString(
386 "import QtQuick 2.0\n"
387 "Image {\n"
388 " anchors.fill: parent\n"
389 " source: \"file://%1/Dash/graphics/phone/screenshots/%2.png\"\n"
390 " smooth: true\n"
391 " fillMode: Image.PreserveAspectCrop\n"
392 "}").arg(qmlDirectory())
393 .arg(application->icon().toString());
394 application->setImageQml(imageQml);
396 application->setScreenshot(QString("file://%1/Dash/graphics/phone/screenshots/%2@12.png").arg(qmlDirectory())
397 .arg(application->icon().toString()));
400void ApplicationManager::buildListOfAvailableApplications()
402 ApplicationInfo *application;
404 application = new ApplicationInfo(this);
405 application->setAppId("dialer-app");
406 application->setName("Dialer");
407 application->setIcon(QUrl("dialer"));
408 application->setStage(ApplicationInfo::SideStage);
409 generateQmlStrings(application);
410 m_availableApplications.append(application);
412 application = new ApplicationInfo(this);
413 application->setAppId("camera-app");
414 application->setName("Camera");
415 application->setIcon(QUrl("camera"));
416 application->setFullscreen(true);
417 generateQmlStrings(application);
418 m_availableApplications.append(application);
420 application = new ApplicationInfo(this);
421 application->setAppId("gallery-app");
422 application->setName("Gallery");
423 application->setIcon(QUrl("gallery"));
424 generateQmlStrings(application);
425 m_availableApplications.append(application);
427 application = new ApplicationInfo(this);
428 application->setAppId("facebook-webapp");
429 application->setName("Facebook");
430 application->setIcon(QUrl("facebook"));
431 application->setStage(ApplicationInfo::SideStage);
432 generateQmlStrings(application);
433 m_availableApplications.append(application);
435 application = new ApplicationInfo(this);
436 application->setAppId("webbrowser-app");
437 application->setFullscreen(true);
438 application->setName("Browser");
439 application->setIcon(QUrl("browser"));
440 generateQmlStrings(application);
441 m_availableApplications.append(application);
443 application = new ApplicationInfo(this);
444 application->setAppId("twitter-webapp");
445 application->setName("Twitter");
446 application->setIcon(QUrl("twitter"));
447 application->setStage(ApplicationInfo::SideStage);
448 generateQmlStrings(application);
449 m_availableApplications.append(application);
451 application = new ApplicationInfo(this);
452 application->setAppId("gmail-webapp");
453 application->setName("GMail");
454 application->setIcon(QUrl("gmail"));
455 m_availableApplications.append(application);
457 application = new ApplicationInfo(this);
458 application->setAppId("ubuntu-weather-app");
459 application->setName("Weather");
460 application->setIcon(QUrl("weather"));
461 application->setStage(ApplicationInfo::SideStage);
462 generateQmlStrings(application);
463 m_availableApplications.append(application);
465 application = new ApplicationInfo(this);
466 application->setAppId("notes-app");
467 application->setName("Notepad");
468 application->setIcon(QUrl("notepad"));
469 application->setStage(ApplicationInfo::SideStage);
470 m_availableApplications.append(application);
472 application = new ApplicationInfo(this);
473 application->setAppId("calendar-app");
474 application->setName("Calendar");
475 application->setIcon(QUrl("calendar"));
476 application->setStage(ApplicationInfo::SideStage);
477 m_availableApplications.append(application);
479 application = new ApplicationInfo(this);
480 application->setAppId("mediaplayer-app");
481 application->setName("Media Player");
482 application->setIcon(QUrl("mediaplayer-app"));
483 application->setFullscreen(true);
484 m_availableApplications.append(application);
486 application = new ApplicationInfo(this);
487 application->setAppId("evernote");
488 application->setName("Evernote");
489 application->setIcon(QUrl("evernote"));
490 m_availableApplications.append(application);
492 application = new ApplicationInfo(this);
493 application->setAppId("map");
494 application->setName("Map");
495 application->setIcon(QUrl("map"));
496 generateQmlStrings(application);
497 m_availableApplications.append(application);
499 application = new ApplicationInfo(this);
500 application->setAppId("pinterest");
501 application->setName("Pinterest");
502 application->setIcon(QUrl("pinterest"));
503 m_availableApplications.append(application);
505 application = new ApplicationInfo(this);
506 application->setAppId("soundcloud");
507 application->setName("SoundCloud");
508 application->setIcon(QUrl("soundcloud"));
509 m_availableApplications.append(application);
511 application = new ApplicationInfo(this);
512 application->setAppId("wikipedia");
513 application->setName("Wikipedia");
514 application->setIcon(QUrl("wikipedia"));
515 m_availableApplications.append(application);
517 application = new ApplicationInfo(this);
518 application->setAppId("youtube");
519 application->setName("YouTube");
520 application->setIcon(QUrl("youtube"));
521 m_availableApplications.append(application);
524void ApplicationManager::createMainStageComponent()
526 // The assumptions I make here really should hold.
527 QQuickView *quickView =
528 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
530 QQmlEngine *engine = quickView->engine();
532 m_mainStageComponent = new QQmlComponent(engine, this);
533 QString mainStageQml =
534 "import QtQuick 2.0\n"
535 "Rectangle {\n"
536 " anchors.fill: parent\n"
537 " color: 'black'\n"
538 " z: -2000\n"
539 "}\n";
540 m_mainStageComponent->setData(mainStageQml.toLatin1(), QUrl());
543void ApplicationManager::createMainStage()
545 if (!m_mainStageComponent)
546 createMainStageComponent();
548 // The assumptions I make here really should hold.
549 QQuickView *quickView =
550 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
552 QQuickItem *shell = quickView->rootObject();
554 m_mainStage = qobject_cast<QQuickItem *>(m_mainStageComponent->create());
555 m_mainStage->setParentItem(shell);
558void ApplicationManager::createSideStageComponent()
560 // The assumptions I make here really should hold.
561 QQuickView *quickView =
562 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
564 QQmlEngine *engine = quickView->engine();
566 m_sideStageComponent = new QQmlComponent(engine, this);
567 QString sideStageQml =
568 "import QtQuick 2.0\n"
569 "import Ubuntu.Components 0.1\n"
570 "Item {\n"
571 " width: units.gu(40)\n" // from SideStage in Shell.qml
572 " anchors.top: parent.top\n"
573 " anchors.bottom: parent.bottom\n"
574 " anchors.right: parent.right\n"
575 " z: -1000\n"
576 "}\n";
577 m_sideStageComponent->setData(sideStageQml.toLatin1(), QUrl());
580void ApplicationManager::createSideStage()
582 if (!m_sideStageComponent)
583 createSideStageComponent();
585 // The assumptions I make here really should hold.
586 QQuickView *quickView =
587 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
589 QQuickItem *shell = quickView->rootObject();
591 m_sideStage = qobject_cast<QQuickItem *>(m_sideStageComponent->create());
592 m_sideStage->setParentItem(shell);
593 m_sideStage->setFlag(QQuickItem::ItemHasContents, false);
596QStringList ApplicationManager::availableApplications()
598 QStringList appIds;
599 Q_FOREACH(ApplicationInfo *app, m_availableApplications) {
600 appIds << app->appId();
601 }
602 return appIds;
605int ApplicationManager::rightMargin() const
607 return m_rightMargin;
610void ApplicationManager::setRightMargin(int rightMargin)
612 m_rightMargin = rightMargin;
613 Q_FOREACH(ApplicationInfo *app, m_availableApplications) {
614 generateQmlStrings(app);
615 }
618bool ApplicationManager::isEmpty() const
620 return m_runningApplications.isEmpty();