on bfec158 help sign in
unity8/Components/ApplicationManagerWrapper.qml
157 lines 5.7 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 */
17import QtQuick 2.0
18import Unity.Application 0.1
20Item {
21 id: applicationManager
23 property alias mainStageApplications: mainStageModel
24 property alias sideStageApplications: sideStageModel
25 property variant mainStageFocusedApplication: null
26 property variant sideStageFocusedApplication: null
27 property bool sideStageEnabled: true
28 property bool keyboardVisible: ApplicationManager.keyboardVisible
29 property int keyboardHeight: ApplicationManager.keyboardHeight
31 property bool fake: ApplicationManager.fake ? ApplicationManager.fake : false
33 ApplicationsModelStageFiltered {
34 id: mainStageModel
35 stage: ApplicationInfo.MainStage
36 }
38 ApplicationsModelStageFiltered {
39 id: sideStageModel
40 stage: ApplicationInfo.SideStage
41 }
43 Connections {
44 target: ApplicationManager
45 onFocusedApplicationIdChanged: {
46 var app = ApplicationManager.findApplication(ApplicationManager.focusedApplicationId)
47 if (!app) { //nothing at all focused, so clear all
48 mainStageFocusedApplication = null;
49 sideStageFocusedApplication = null;
50 } else {
51 if (app.stage == ApplicationInfo.MainStage) {
52 mainStageFocusedApplication = app;
53 // possible the side stage app being unfocused fired this signal, so check for it
54 if (sideStageFocusedApplication && !sideStageFocusedApplication.focused)
55 sideStageFocusedApplication = null;
56 } else {
57 sideStageFocusedApplication = app;
58 // possible the main stage app being unfocused fired this signal, so check for it
59 if (mainStageFocusedApplication && !mainStageFocusedApplication.focused)
60 mainStageFocusedApplication = null;
61 }
62 }
63 }
64 }
66 function activateApplication(desktopFile, argument) {
67 var appId;
69 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
70 // TODO: convert entire shell to use appId only.
71 if (desktopFile.indexOf(".desktop") >= 0) {
72 appId = desktopFileToAppId(desktopFile);
73 } else {
74 appId = desktopFile;
75 }
77 var application = ApplicationManager.findApplication(appId);
78 if (application !== null) {
79 return application;
80 }
82 var execFlags = sideStageEnabled ? ApplicationManager.NoFlag : ApplicationManager.ForceMainStage;
84 if (argument) {
85 return ApplicationManager.startApplication(appId, execFlags, [argument]);
86 } else {
87 return ApplicationManager.startApplication(appId, execFlags);
88 }
89 }
91 function stopApplication(application) {
92 var appId;
94 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
95 // TODO: convert entire shell to use appId only.
96 if (typeof application == "string") {
97 appId = application;
98 } else {
99 appId = desktopFileToAppId(application.desktopFile);
100 }
102 ApplicationManager.stopApplication(appId);
103 }
105 function focusApplication(application) {
106 if (application == null || application == undefined) {
107 return;
108 }
110 ApplicationManager.focusApplication(application.appId);
111 }
113 function unfocusCurrentApplication() {
114 ApplicationManager.unfocusCurrentApplication();
115 }
117 function moveRunningApplicationStackPosition(from, to, stage) {
118 if (from == to || from < 0 || to < 0) return;
120 if (stage == ApplicationInfo.SideStage) {
121 sideStageModel.move(from, to);
122 } else {
123 mainStageModel.move(from, to);
124 }
125 }
127 function getApplicationFromDesktopFile(desktopFile, stage) {
128 var appId;
130 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
131 // TODO: convert entire shell to use appId only.
132 if (desktopFile.indexOf(".desktop") >= 0) {
133 appId = desktopFileToAppId(desktopFile);
134 } else {
135 appId = desktopFile;
136 }
138 for (var i = 0, len = ApplicationManager.count; i < len; i++ ) {
139 var app = ApplicationManager.get(i);
141 // if stage not specified, return whichever app running on either stage
142 if (app.appId == appId && (stage == undefined || app.stage == stage)) {
143 return app;
144 }
145 }
146 }
148 function desktopFileToAppId(desktopFile) {
149 var right = desktopFile.lastIndexOf(".desktop");
150 var left = desktopFile.lastIndexOf("/");
151 if (left == -1 || right == -1 || left == right) {
152 console.log("ApplicationManagerWrapper: unable to extract appId from '" + desktopFile + "'");
153 return "";
154 }
155 return desktopFile.substring(left+1, right);
156 }