on dc5895d help sign in
unity8/tests/mocks/Unity/Application/ApplicationManager.cpp
519 lines 18 kB history raw
1/*
2 * Copyright (C) 2013-2016 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"
19#include "MirSurface.h"
21#include <paths.h>
22#include <csignal>
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>
33#define APPLICATIONMANAGER_DEBUG 0
35#if APPLICATIONMANAGER_DEBUG
36#define DEBUG_MSG(params) qDebug().nospace() << "ApplicationManager::" << __func__ << " " << params
37#else
38#define DEBUG_MSG(params) ((void)0)
39#endif
41namespace unityapi = unity::shell::application;
43ApplicationManager::ApplicationManager(QObject *parent)
44 : ApplicationManagerInterface(parent)
46 DEBUG_MSG("");
47 buildListOfAvailableApplications();
49 // polling to find out when the toplevel window has been created as there's
50 // no signal telling us that
51 connect(&m_windowCreatedTimer, &QTimer::timeout,
52 this, &ApplicationManager::onWindowCreatedTimerTimeout);
53 m_windowCreatedTimer.setSingleShot(false);
54 m_windowCreatedTimer.start(200);
56 Q_ASSERT(MirFocusController::instance());
57 connect(MirFocusController::instance(), &MirFocusController::focusedSurfaceChanged,
58 this, &ApplicationManager::updateFocusedApplication, Qt::QueuedConnection);
61 // Emit signal to notify Upstart that Mir is ready to receive client connections
62 // see http://upstart.ubuntu.com/cookbook/#expect-stop
63 // We do this because some autopilot tests actually use this mock Unity.Application module,
64 // so we have to mimic what the real ApplicationManager does in that regard.
65 if (qgetenv("UNITY_MIR_EMITS_SIGSTOP") == "1") {
66 raise(SIGSTOP);
67 }
70ApplicationManager::~ApplicationManager()
74void ApplicationManager::onWindowCreatedTimerTimeout()
76 if (QGuiApplication::topLevelWindows().count() > 0) {
77 m_windowCreatedTimer.stop();
78 onWindowCreated();
79 }
82void ApplicationManager::onWindowCreated()
84 startApplication("unity8-dash");
87int ApplicationManager::rowCount(const QModelIndex& parent) const {
88 return !parent.isValid() ? m_runningApplications.size() : 0;
91QVariant ApplicationManager::data(const QModelIndex& index, int role) const {
92 if (index.row() < 0 || index.row() >= m_runningApplications.size())
93 return QVariant();
95 auto app = m_runningApplications.at(index.row());
96 switch(role) {
97 case RoleAppId:
98 return app->appId();
99 case RoleName:
100 return app->name();
101 case RoleComment:
102 return app->comment();
103 case RoleIcon:
104 return app->icon();
105 case RoleState:
106 return app->state();
107 case RoleFocused:
108 return app->focused();
109 case RoleIsTouchApp:
110 return app->isTouchApp();
111 case RoleExemptFromLifecycle:
112 return app->exemptFromLifecycle();
113 case RoleApplication:
114 return QVariant::fromValue(static_cast<unityapi::ApplicationInfoInterface*>(app));
115 default:
116 return QVariant();
117 }
120ApplicationInfo *ApplicationManager::get(int row) const {
121 if (row < 0 || row >= m_runningApplications.size())
122 return nullptr;
123 return m_runningApplications.at(row);
126ApplicationInfo *ApplicationManager::findApplication(const QString &appId) const {
127 for (ApplicationInfo *app : m_runningApplications) {
128 if (app->appId() == appId) {
129 return app;
130 }
131 }
132 return nullptr;
135QModelIndex ApplicationManager::findIndex(ApplicationInfo* application)
137 for (int i = 0; i < m_runningApplications.size(); ++i) {
138 if (m_runningApplications.at(i) == application) {
139 return index(i);
140 }
141 }
143 return QModelIndex();
146bool ApplicationManager::add(ApplicationInfo *application) {
147 if (!application || m_runningApplications.contains(application)) {
148 return false;
149 }
150 DEBUG_MSG(application->appId());
152 application->setState(ApplicationInfo::Starting);
154 beginInsertRows(QModelIndex(), m_runningApplications.size(), m_runningApplications.size());
155 m_runningApplications.append(application);
157 connect(application, &ApplicationInfo::focusedChanged, this, [application, this]() {
158 QModelIndex appIndex = findIndex(application);
159 if (!appIndex.isValid()) return;
160 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << ApplicationManager::RoleFocused);
161 });
162 connect(application, &ApplicationInfo::stateChanged, this, [application, this]() {
163 QModelIndex appIndex = findIndex(application);
164 if (!appIndex.isValid()) return;
165 Q_EMIT dataChanged(appIndex, appIndex, QVector<int>() << ApplicationManager::RoleState);
166 });
168 connect(application, &ApplicationInfo::closed, this, [application, this]() {
169 this->remove(application);
170 });
171 connect(application, &ApplicationInfo::focusRequested, this, [application, this]() {
172 Q_EMIT this->focusRequested(application->appId());
173 });
175 endInsertRows();
176 Q_EMIT countChanged();
177 if (count() == 1) Q_EMIT emptyChanged(isEmpty()); // was empty but not anymore
179 return true;
182void ApplicationManager::remove(ApplicationInfo *application) {
183 int i = m_runningApplications.indexOf(application);
184 if (i != -1) {
185 DEBUG_MSG(application->appId());
186 beginRemoveRows(QModelIndex(), i, i);
187 m_runningApplications.removeAt(i);
188 endRemoveRows();
189 Q_EMIT countChanged();
190 if (isEmpty()) Q_EMIT emptyChanged(isEmpty());
191 }
192 application->disconnect(this);
195void ApplicationManager::move(int from, int to) {
196 if (from == to) return;
198 if (from >= 0 && from < m_runningApplications.size() && to >= 0 && to < m_runningApplications.size()) {
199 QModelIndex parent;
200 /* When moving an item down, the destination index needs to be incremented
201 * by one, as explained in the documentation:
202 * http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
203 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
204 m_runningApplications.move(from, to);
205 endMoveRows();
206 }
209ApplicationInfo* ApplicationManager::startApplication(const QString &appId,
210 const QStringList &arguments)
212 DEBUG_MSG(appId);
213 Q_UNUSED(arguments)
215 ApplicationInfo *application = findApplication(appId);
216 if (application) {
217 // the requested app is already running
218 return application;
219 } else {
220 application = add(appId);
221 }
223 // most likely not among the available ones
224 if (!application)
225 return nullptr;
227 Q_EMIT application->focusRequested(); // we assume that an application that's starting up wants focus
229 return application;
232ApplicationInfo* ApplicationManager::add(QString appId)
234 ApplicationInfo *application = nullptr;
236 for (ApplicationInfo *availableApp : m_availableApplications) {
237 if (availableApp->appId() == appId) {
238 application = availableApp;
239 break;
240 }
241 }
243 if (application) {
244 if (!add(application)) {
245 application = nullptr;
246 }
247 }
249 return application;
252bool ApplicationManager::stopApplication(const QString &appId)
254 DEBUG_MSG(appId);
255 ApplicationInfo *application = findApplication(appId);
256 if (application == nullptr)
257 return false;
259 application->close();
260 return true;
263QString ApplicationManager::focusedApplicationId() const {
264 for (ApplicationInfo *app : m_runningApplications) {
265 if (app->focused()) {
266 return app->appId();
267 }
268 }
269 return QString();
272bool ApplicationManager::requestFocusApplication(const QString &appId)
274 ApplicationInfo *application = findApplication(appId);
275 if (application == nullptr)
276 return false;
278 application->requestFocus();
280 return true;
283void ApplicationManager::buildListOfAvailableApplications()
285 /*
286 ATTENTION!
287 Be careful when changing application properties here as some qmltests
288 rely on them being the way it's specified here (e.g. that camera-app
289 is fullscreen, that twitter-webapp can rotate in all directions, etc)
290 */
292 ApplicationInfo *application;
294 application = new ApplicationInfo(this);
295 application->setAppId("unity8-dash");
296 application->setName("Unity 8 Mock Dash");
297 application->setScreenshotId("unity8-dash");
298 application->setIconId("dash");
299 m_availableApplications.append(application);
301 application = new ApplicationInfo(this);
302 application->setAppId("dialer-app");
303 application->setName("Dialer");
304 application->setScreenshotId("dialer");
305 application->setIconId("dialer-app");
306 application->setSupportedOrientations(Qt::PortraitOrientation
307 | Qt::InvertedPortraitOrientation);
308 m_availableApplications.append(application);
310 application = new ApplicationInfo(this);
311 application->setAppId("camera-app");
312 application->setName("Camera");
313 application->setScreenshotId("camera");
314 application->setIconId("camera");
315 application->setFullscreen(true);
316 application->setSupportedOrientations(Qt::PortraitOrientation
317 | Qt::LandscapeOrientation
318 | Qt::InvertedPortraitOrientation
319 | Qt::InvertedLandscapeOrientation);
320 application->setRotatesWindowContents(true);
321 m_availableApplications.append(application);
323 application = new ApplicationInfo(this);
324 application->setAppId("camera-app2");
325 application->setName("Camera2");
326 application->setScreenshotId("camera");
327 application->setIconId("camera");
328 application->setSupportedOrientations(Qt::PortraitOrientation
329 | Qt::LandscapeOrientation
330 | Qt::InvertedPortraitOrientation
331 | Qt::InvertedLandscapeOrientation);
332 application->setRotatesWindowContents(true);
333 m_availableApplications.append(application);
335 application = new ApplicationInfo(this);
336 application->setAppId("gallery-app");
337 application->setName("Gallery");
338 application->setScreenshotId("gallery");
339 application->setIconId("gallery");
340 application->setShellChrome(Mir::LowChrome);
341 m_availableApplications.append(application);
343 application = new ApplicationInfo(this);
344 application->setAppId("facebook-webapp");
345 application->setName("Facebook");
346 application->setScreenshotId("facebook");
347 application->setIconId("facebook");
348 m_availableApplications.append(application);
350 application = new ApplicationInfo(this);
351 application->setAppId("webbrowser-app");
352 application->setShellChrome(Mir::LowChrome);
353 application->setName("Browser");
354 application->setScreenshotId("browser");
355 application->setIconId("browser");
356 m_availableApplications.append(application);
358 application = new ApplicationInfo(this);
359 application->setAppId("twitter-webapp");
360 application->setName("Twitter");
361 application->setScreenshotId("twitter");
362 application->setIconId("twitter");
363 m_availableApplications.append(application);
365 application = new ApplicationInfo(this);
366 application->setAppId("map");
367 application->setName("Map");
368 application->setIconId("map");
369 application->setScreenshotId("map");
370 m_availableApplications.append(application);
372 application = new ApplicationInfo(this);
373 application->setAppId("gmail-webapp");
374 application->setName("GMail");
375 application->setIconId("gmail");
376 application->setScreenshotId("gmail-webapp.svg");
377 application->setSupportedOrientations(Qt::PortraitOrientation
378 | Qt::LandscapeOrientation
379 | Qt::InvertedPortraitOrientation
380 | Qt::InvertedLandscapeOrientation);
381 m_availableApplications.append(application);
383 application = new ApplicationInfo(this);
384 application->setAppId("music-app");
385 application->setName("Music");
386 application->setIconId("soundcloud");
387 application->setScreenshotId("music");
388 application->setSupportedOrientations(Qt::PortraitOrientation
389 | Qt::LandscapeOrientation
390 | Qt::InvertedPortraitOrientation
391 | Qt::InvertedLandscapeOrientation);
392 m_availableApplications.append(application);
394 application = new ApplicationInfo(this);
395 application->setAppId("ubuntu-weather-app");
396 application->setName("Weather");
397 application->setIconId("weather");
398 application->setScreenshotId("ubuntu-weather-app.svg");
399 application->setSupportedOrientations(Qt::LandscapeOrientation
400 | Qt::InvertedLandscapeOrientation);
401 m_availableApplications.append(application);
403 application = new ApplicationInfo(this);
404 application->setAppId("notes-app");
405 application->setName("Notepad");
406 application->setIconId("notepad");
407// application->setStage(ApplicationInfoInterface::SideStage);
408 m_availableApplications.append(application);
410 application = new ApplicationInfo(this);
411 application->setAppId("calendar-app");
412 application->setName("Calendar");
413 application->setIconId("calendar");
414 m_availableApplications.append(application);
416 application = new ApplicationInfo(this);
417 application->setAppId("evernote");
418 application->setName("Evernote");
419 application->setIconId("evernote");
420 m_availableApplications.append(application);
422 application = new ApplicationInfo(this);
423 application->setAppId("pinterest");
424 application->setName("Pinterest");
425 application->setIconId("pinterest");
426 m_availableApplications.append(application);
428 application = new ApplicationInfo(this);
429 application->setAppId("soundcloud");
430 application->setName("SoundCloud");
431 application->setIconId("soundcloud");
432 m_availableApplications.append(application);
434 application = new ApplicationInfo(this);
435 application->setAppId("wikipedia");
436 application->setName("Wikipedia");
437 application->setIconId("wikipedia");
438 m_availableApplications.append(application);
440 application = new ApplicationInfo(this);
441 application->setAppId("youtube");
442 application->setName("YouTube");
443 application->setIconId("youtube");
444 m_availableApplications.append(application);
446 application = new ApplicationInfo(this);
447 application->setAppId("libreoffice");
448 application->setName("LibreOffice");
449 application->setIconId("libreoffice");
450 application->setScreenshotId("libreoffice");
451 application->setIsTouchApp(false);
452 m_availableApplications.append(application);
454 application = new ApplicationInfo(this);
455 application->setAppId("primary-oriented-app");
456 application->setName("Primary Oriented");
457 application->setSupportedOrientations(Qt::PrimaryOrientation);
458 m_availableApplications.append(application);
462QStringList ApplicationManager::availableApplications()
464 QStringList appIds;
465 Q_FOREACH(ApplicationInfo *app, m_availableApplications) {
466 appIds << app->appId();
467 }
468 return appIds;
471bool ApplicationManager::isEmpty() const
473 return m_runningApplications.isEmpty();
476void ApplicationManager::updateFocusedApplication()
478 ApplicationInfo *focusedApplication = nullptr;
479 ApplicationInfo *previouslyFocusedApplication = nullptr;
481 auto controller = MirFocusController::instance();
482 if (!controller) {
483 return;
484 }
486 MirSurface *surface = static_cast<MirSurface*>(controller->focusedSurface());
487 if (surface) {
488 focusedApplication = findApplication(surface);
489 }
491 surface = static_cast<MirSurface*>(controller->previouslyFocusedSurface());
492 if (surface) {
493 previouslyFocusedApplication = findApplication(surface);
494 }
496 if (focusedApplication != previouslyFocusedApplication) {
497 if (focusedApplication) {
498 DEBUG_MSG("focused " << focusedApplication->appId());
499 Q_EMIT focusedApplication->focusedChanged(true);
500 this->move(this->m_runningApplications.indexOf(focusedApplication), 0);
501 }
502 if (previouslyFocusedApplication) {
503 DEBUG_MSG("unfocused " << previouslyFocusedApplication->appId());
504 Q_EMIT previouslyFocusedApplication->focusedChanged(false);
505 }
506 Q_EMIT focusedApplicationIdChanged();
507 }
510ApplicationInfo *ApplicationManager::findApplication(MirSurface* surface)
512 for (ApplicationInfo *app : m_runningApplications) {
513 auto surfaceList = static_cast<MirSurfaceListModel*>(app->surfaceList());
514 if (surfaceList->contains(surface)) {
515 return app;
516 }
517 }
518 return nullptr;