2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
17#include "ApplicationManager.h"
18#include "ApplicationInfo.h"
23#include <QGuiApplication>
26#include <QQmlComponent>
31ApplicationManager::ApplicationManager(QObject *parent)
32 : ApplicationManagerInterface(parent)
33 , m_mainStageComponent(0)
35 , m_sideStageComponent(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())
53 auto app = m_runningApplications.at(index.row());
60 return app->comment();
68 return app->focused();
70 return app->screenshot();
76ApplicationInfo *ApplicationManager::get(int row) const {
77 if (row < 0 || row >= m_runningApplications.size())
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) {
93void ApplicationManager::add(ApplicationInfo *application) {
94 qDebug() << "add() " << application->appId();
99 beginInsertRows(QModelIndex(), m_runningApplications.size(), m_runningApplications.size());
100 m_runningApplications.append(application);
102 Q_EMIT countChanged();
105void ApplicationManager::remove(ApplicationInfo *application) {
106 int i = m_runningApplications.indexOf(application);
108 beginRemoveRows(QModelIndex(), i, i);
109 m_runningApplications.removeAt(i);
111 Q_EMIT countChanged();
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()) {
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);
129int ApplicationManager::keyboardHeight() const
134bool ApplicationManager::keyboardVisible() const
139int ApplicationManager::sideStageWidth() const
144ApplicationManager::StageHint ApplicationManager::stageHint() const
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,
162 const QStringList &arguments)
165 ApplicationInfo *application = 0;
167 for (ApplicationInfo *availableApp : m_availableApplications) {
168 if (availableApp->appId() == appId) {
169 application = availableApp;
177 if (flags.testFlag(ApplicationManager::ForceMainStage)
178 && application->stage() == ApplicationInfo::SideStage) {
179 application->setStage(ApplicationInfo::MainStage);
183 QMetaObject::invokeMethod(this, "focusApplication", Qt::QueuedConnection, Q_ARG(QString, appId));
188bool ApplicationManager::stopApplication(const QString &appId)
190 ApplicationInfo *application = findApplication(appId);
191 if (application == nullptr)
195 Q_EMIT focusedApplicationIdChanged();
196 qDebug() << Q_FUNC_INFO << "emitting focusedAppChanged" << focusedApplicationId();
200void ApplicationManager::updateScreenshot(const QString &appId)
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) {
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()) {
232bool ApplicationManager::focusApplication(const QString &appId)
234 ApplicationInfo *application = findApplication(appId);
235 if (application == nullptr)
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);
248 application->setFocused(true);
251 application->showWindow(m_mainStage);
252 m_mainStage->setZ(-1000);
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);
265 application->setFocused(true);
268 application->showWindow(m_sideStage);
269 m_sideStage->setZ(-1000);
271 m_mainStage->setZ(-2000);
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();
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()) {
291 app->setFocused(false);
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?
303 if (application->fullscreen()) {
304 topMargin.append("0");
306 // Taken from Panel.panelHeight
307 topMargin.append("units.gu(3) + units.dp(2)");
310 QString windowQml = QString(
311 "import QtQuick 2.0\n"
313 " anchors.fill: parent\n"
314 " anchors.topMargin: %1\n"
315 " source: \"file://%2/Dash/graphics/phone/screenshots/%3.png\"\n"
317 " fillMode: Image.PreserveAspectCrop\n"
320 .arg(application->icon().toString());
321 application->setWindowQml(windowQml);
323 QString imageQml = QString(
324 "import QtQuick 2.0\n"
326 " anchors.fill: parent\n"
327 " source: \"file://%1/Dash/graphics/phone/screenshots/%2.png\"\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"
483 " anchors.fill: parent\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"
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"
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);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list