on c969157 help sign in
unity8/tests/mocks/Unity/Application/ApplicationManager.cpp
541 lines 18 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 case RoleScreenshot:
70 return app->screenshot();
71 default:
72 return QVariant();
73 }
76ApplicationInfo *ApplicationManager::get(int row) const {
77 if (row < 0 || row >= m_runningApplications.size())
78 return nullptr;
79 return m_runningApplications.at(row);
82ApplicationInfo *ApplicationManager::findApplication(const QString &appId) const {
83 qDebug() << "findapp called";
84 for (ApplicationInfo *app : m_runningApplications) {
85 qDebug() << "searching app"<< app->appId();
86 if (app->appId() == appId) {
87 return app;
88 }
89 }
90 return nullptr;
93void ApplicationManager::add(ApplicationInfo *application) {
94 qDebug() << "add() " << application->appId();
95 if (!application) {
96 return;
97 }
99 beginInsertRows(QModelIndex(), m_runningApplications.size(), m_runningApplications.size());
100 m_runningApplications.append(application);
101 endInsertRows();
102 Q_EMIT countChanged();
105void ApplicationManager::remove(ApplicationInfo *application) {
106 int i = m_runningApplications.indexOf(application);
107 if (i != -1) {
108 beginRemoveRows(QModelIndex(), i, i);
109 m_runningApplications.removeAt(i);
110 endRemoveRows();
111 Q_EMIT countChanged();
112 }
115void ApplicationManager::move(int from, int to) {
116 if (from == to) return;
118 if (from >= 0 && from < m_runningApplications.size() && to >= 0 && to < m_runningApplications.size()) {
119 QModelIndex parent;
120 /* When moving an item down, the destination index needs to be incremented
121 * by one, as explained in the documentation:
122 * http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
123 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
124 m_runningApplications.move(from, to);
125 endMoveRows();
126 }
129int ApplicationManager::keyboardHeight() const
131 return 0;
134bool ApplicationManager::keyboardVisible() const
136 return false;
139int ApplicationManager::sideStageWidth() const
141 return 0;
144ApplicationManager::StageHint ApplicationManager::stageHint() const
146 return MainStage;
149ApplicationManager::FormFactorHint ApplicationManager::formFactorHint() const
151 return PhoneFormFactor;
154ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
155 const QStringList &arguments)
157 return startApplication(appId, NoFlag, arguments);
160ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
161 ExecFlags flags,
162 const QStringList &arguments)
164 Q_UNUSED(arguments)
165 ApplicationInfo *application = 0;
167 for (ApplicationInfo *availableApp : m_availableApplications) {
168 if (availableApp->appId() == appId) {
169 application = availableApp;
170 break;
171 }
172 }
174 if (!application)
175 return 0;
177 if (flags.testFlag(ApplicationManager::ForceMainStage)
178 && application->stage() == ApplicationInfo::SideStage) {
179 application->setStage(ApplicationInfo::MainStage);
180 }
181 add(application);
183 QMetaObject::invokeMethod(this, "focusApplication", Qt::QueuedConnection, Q_ARG(QString, appId));
185 return application;
188bool ApplicationManager::stopApplication(const QString &appId)
190 ApplicationInfo *application = findApplication(appId);
191 if (application == nullptr)
192 return false;
194 remove(application);
195 Q_EMIT focusedApplicationIdChanged();
196 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
197 return true;
200void ApplicationManager::updateScreenshot(const QString &appId)
202 int idx = -1;
203 ApplicationInfo *application = nullptr;
204 for (int i = 0; i < m_availableApplications.count(); ++i) {
205 application = m_availableApplications.at(i);
206 if (application->appId() == appId) {
207 idx = i;
208 break;
209 }
210 }
212 if (idx == -1) {
213 return;
214 }
216 qDebug() << "setting new screenshot" << application->screenshot() << application;
217 application->setScreenshot(QString("image://application/%1/%2").arg(appId).arg(QDateTime::currentMSecsSinceEpoch()));
218 qDebug() << "set new screenshot" << application->screenshot();
219 QModelIndex appIndex = index(idx);
220 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << RoleScreenshot);
223QString ApplicationManager::focusedApplicationId() const {
224 for (ApplicationInfo *app : m_runningApplications) {
225 if (app->focused()) {
226 return app->appId();
227 }
228 }
229 return QString();
232bool ApplicationManager::focusApplication(const QString &appId)
234 ApplicationInfo *application = findApplication(appId);
235 if (application == nullptr)
236 return false;
238 if (application->stage() == ApplicationInfo::MainStage) {
239 // unfocus currently focused mainstage app
240 for (ApplicationInfo *app : m_runningApplications) {
241 if (app->focused() && app->stage() == ApplicationInfo::MainStage) {
242 app->setFocused(false);
243 app->hideWindow();
244 }
245 }
247 // focus this app
248 application->setFocused(true);
249 if (!m_mainStage)
250 createMainStage();
251 application->showWindow(m_mainStage);
252 m_mainStage->setZ(-1000);
253 if (m_sideStage)
254 m_sideStage->setZ(-2000);
255 } else if (application->stage() == ApplicationInfo::SideStage) {
256 // unfocus currently focused sidestage app
257 for (ApplicationInfo *app : m_runningApplications) {
258 if (app->focused() && app->stage() == ApplicationInfo::SideStage) {
259 app->setFocused(false);
260 app->hideWindow();
261 }
262 }
264 // focus this app
265 application->setFocused(true);
266 if (!m_sideStage)
267 createSideStage();
268 application->showWindow(m_sideStage);
269 m_sideStage->setZ(-1000);
270 if (m_mainStage)
271 m_mainStage->setZ(-2000);
272 }
274 // move app to top of stack
275 move(m_runningApplications.indexOf(application), 0);
276 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
277 Q_EMIT focusedApplicationIdChanged();
278 return true;
281void ApplicationManager::activateApplication(const QString &appId)
283 QMetaObject::invokeMethod(this, "focusRequested", Qt::QueuedConnection, Q_ARG(QString, appId));
286void ApplicationManager::unfocusCurrentApplication()
288 for (ApplicationInfo *app : m_runningApplications) {
289 if (app->focused()) {
290 app->hideWindow();
291 app->setFocused(false);
292 }
293 }
294 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
295 Q_EMIT focusedApplicationIdChanged();
298void ApplicationManager::generateQmlStrings(ApplicationInfo *application)
300 // TODO: Is there a better way of solving this fullscreen vs. regular
301 // application height?
302 QString topMargin;
303 if (application->fullscreen()) {
304 topMargin.append("0");
305 } else {
306 // Taken from Panel.panelHeight
307 topMargin.append("units.gu(3) + units.dp(2)");
308 }
310 QString windowQml = QString(
311 "import QtQuick 2.0\n"
312 "Image {\n"
313 " anchors.fill: parent\n"
314 " anchors.topMargin: %1\n"
315 " source: \"file://%2/Dash/graphics/phone/screenshots/%3.png\"\n"
316 " smooth: true\n"
317 " fillMode: Image.PreserveAspectCrop\n"
318 "}").arg(topMargin)
319 .arg(qmlDirectory())
320 .arg(application->icon().toString());
321 application->setWindowQml(windowQml);
323 QString imageQml = QString(
324 "import QtQuick 2.0\n"
325 "Image {\n"
326 " anchors.fill: parent\n"
327 " source: \"file://%1/Dash/graphics/phone/screenshots/%2.png\"\n"
328 " smooth: true\n"
329 " fillMode: Image.PreserveAspectCrop\n"
330 "}").arg(qmlDirectory())
331 .arg(application->icon().toString());
332 application->setImageQml(imageQml);
335void ApplicationManager::buildListOfAvailableApplications()
337 ApplicationInfo *application;
339 application = new ApplicationInfo(this);
340 application->setAppId("phone-app");
341 application->setName("Phone");
342 application->setIcon(QUrl("phone"));
343 application->setStage(ApplicationInfo::SideStage);
344 application->setScreenshot(QString("image://application/%1/123456789").arg(application->appId()));
345 generateQmlStrings(application);
346 m_availableApplications.append(application);
348 application = new ApplicationInfo(this);
349 application->setAppId("camera-app");
350 application->setName("Camera");
351 application->setIcon(QUrl("camera"));
352 application->setFullscreen(true);
353 application->setScreenshot(QUrl("image://application/phone/123456789"));
354 generateQmlStrings(application);
355 m_availableApplications.append(application);
357 application = new ApplicationInfo(this);
358 application->setAppId("gallery-app");
359 application->setName("Gallery");
360 application->setIcon(QUrl("gallery"));
361 application->setScreenshot(QUrl("image://application/phone/123456789"));
362 generateQmlStrings(application);
363 m_availableApplications.append(application);
365 application = new ApplicationInfo(this);
366 application->setAppId("facebook-webapp");
367 application->setName("Facebook");
368 application->setIcon(QUrl("facebook"));
369 application->setStage(ApplicationInfo::SideStage);
370 application->setScreenshot(QUrl("image://application/phone/123456789"));
371 generateQmlStrings(application);
372 m_availableApplications.append(application);
374 application = new ApplicationInfo(this);
375 application->setAppId("webbrowser-app");
376 application->setName("Browser");
377 application->setIcon(QUrl("browser"));
378 application->setScreenshot(QUrl("image://application/phone/123456789"));
379 generateQmlStrings(application);
380 m_availableApplications.append(application);
382 application = new ApplicationInfo(this);
383 application->setAppId("twitter-webapp");
384 application->setName("Twitter");
385 application->setIcon(QUrl("twitter"));
386 application->setStage(ApplicationInfo::SideStage);
387 application->setScreenshot(QUrl("image://application/phone/123456789"));
388 generateQmlStrings(application);
389 m_availableApplications.append(application);
391 application = new ApplicationInfo(this);
392 application->setAppId("gmail-webapp");
393 application->setName("GMail");
394 application->setIcon(QUrl("gmail"));
395 application->setScreenshot(QUrl("image://application/phone/123456789"));
396 m_availableApplications.append(application);
398 application = new ApplicationInfo(this);
399 application->setAppId("ubuntu-weather-app");
400 application->setName("Weather");
401 application->setIcon(QUrl("weather"));
402 application->setStage(ApplicationInfo::SideStage);
403 application->setScreenshot(QUrl("image://application/phone/123456789"));
404 generateQmlStrings(application);
405 m_availableApplications.append(application);
407 application = new ApplicationInfo(this);
408 application->setAppId("notes-app");
409 application->setName("Notepad");
410 application->setIcon(QUrl("notepad"));
411 application->setStage(ApplicationInfo::SideStage);
412 application->setScreenshot(QUrl("image://application/phone/123456789"));
413 m_availableApplications.append(application);
415 application = new ApplicationInfo(this);
416 application->setAppId("calendar-app");
417 application->setName("Calendar");
418 application->setIcon(QUrl("calendar"));
419 application->setStage(ApplicationInfo::SideStage);
420 application->setScreenshot(QUrl("image://application/phone/123456789"));
421 m_availableApplications.append(application);
423 application = new ApplicationInfo(this);
424 application->setAppId("mediaplayer-app");
425 application->setName("Media Player");
426 application->setIcon(QUrl("mediaplayer-app"));
427 application->setFullscreen(true);
428 application->setScreenshot(QUrl("image://application/phone/123456789"));
429 m_availableApplications.append(application);
431 application = new ApplicationInfo(this);
432 application->setAppId("evernote");
433 application->setName("Evernote");
434 application->setIcon(QUrl("evernote"));
435 application->setScreenshot(QUrl("image://application/phone/123456789"));
436 m_availableApplications.append(application);
438 application = new ApplicationInfo(this);
439 application->setAppId("map");
440 application->setName("Map");
441 application->setIcon(QUrl("map"));
442 application->setScreenshot(QUrl("image://application/phone/123456789"));
443 generateQmlStrings(application);
444 m_availableApplications.append(application);
446 application = new ApplicationInfo(this);
447 application->setAppId("pinterest");
448 application->setName("Pinterest");
449 application->setIcon(QUrl("pinterest"));
450 m_availableApplications.append(application);
452 application = new ApplicationInfo(this);
453 application->setAppId("soundcloud");
454 application->setName("SoundCloud");
455 application->setIcon(QUrl("soundcloud"));
456 m_availableApplications.append(application);
458 application = new ApplicationInfo(this);
459 application->setAppId("wikipedia");
460 application->setName("Wikipedia");
461 application->setIcon(QUrl("wikipedia"));
462 m_availableApplications.append(application);
464 application = new ApplicationInfo(this);
465 application->setAppId("youtube");
466 application->setName("YouTube");
467 application->setIcon(QUrl("youtube"));
468 m_availableApplications.append(application);
471void ApplicationManager::createMainStageComponent()
473 // The assumptions I make here really should hold.
474 QQuickView *quickView =
475 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
477 QQmlEngine *engine = quickView->engine();
479 m_mainStageComponent = new QQmlComponent(engine, this);
480 QString mainStageQml =
481 "import QtQuick 2.0\n"
482 "Rectangle {\n"
483 " anchors.fill: parent\n"
484 " color: 'black'\n"
485 " z: -2000\n"
486 "}\n";
487 m_mainStageComponent->setData(mainStageQml.toLatin1(), QUrl());
490void ApplicationManager::createMainStage()
492 if (!m_mainStageComponent)
493 createMainStageComponent();
495 // The assumptions I make here really should hold.
496 QQuickView *quickView =
497 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
499 QQuickItem *shell = quickView->rootObject();
501 m_mainStage = qobject_cast<QQuickItem *>(m_mainStageComponent->create());
502 m_mainStage->setParentItem(shell);
505void ApplicationManager::createSideStageComponent()
507 // The assumptions I make here really should hold.
508 QQuickView *quickView =
509 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
511 QQmlEngine *engine = quickView->engine();
513 m_sideStageComponent = new QQmlComponent(engine, this);
514 QString sideStageQml =
515 "import QtQuick 2.0\n"
516 "import Ubuntu.Components 0.1\n"
517 "Item {\n"
518 " width: units.gu(40)\n" // from SideStage in Shell.qml
519 " anchors.top: parent.top\n"
520 " anchors.bottom: parent.bottom\n"
521 " anchors.right: parent.right\n"
522 " z: -1000\n"
523 "}\n";
524 m_sideStageComponent->setData(sideStageQml.toLatin1(), QUrl());
527void ApplicationManager::createSideStage()
529 if (!m_sideStageComponent)
530 createSideStageComponent();
532 // The assumptions I make here really should hold.
533 QQuickView *quickView =
534 qobject_cast<QQuickView*>(QGuiApplication::topLevelWindows()[0]);
536 QQuickItem *shell = quickView->rootObject();
538 m_sideStage = qobject_cast<QQuickItem *>(m_sideStageComponent->create());
539 m_sideStage->setParentItem(shell);
540 m_sideStage->setFlag(QQuickItem::ItemHasContents, false);