on 00a39ec help sign in
unity8/Components/ApplicationManagerWrapper.qml
186 lines 7.3 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 signal focusRequested(string desktopFile)
29 property bool keyboardVisible: ApplicationManager.keyboardVisible
30 property int keyboardHeight: ApplicationManager.keyboardHeight
32 property bool fake: ApplicationManager.fake ? ApplicationManager.fake : false
34 ApplicationsModelStageFiltered {
35 id: mainStageModel
36 stage: ApplicationInfo.MainStage
37 }
39 ApplicationsModelStageFiltered {
40 id: sideStageModel
41 stage: ApplicationInfo.SideStage
42 }
44 Connections {
45 target: ApplicationManager
46 onFocusedApplicationIdChanged: {
47 var app = ApplicationManager.findApplication(ApplicationManager.focusedApplicationId)
48 if (!app) { //nothing at all focused, so clear all
49 mainStageFocusedApplication = null;
50 sideStageFocusedApplication = null;
51 } else {
52 if (app.stage == ApplicationInfo.MainStage) {
53 mainStageFocusedApplication = app;
54 // possible the side stage app being unfocused fired this signal, so check for it
55 if (sideStageFocusedApplication && !sideStageFocusedApplication.focused)
56 sideStageFocusedApplication = null;
57 } else {
58 sideStageFocusedApplication = app;
59 // possible the main stage app being unfocused fired this signal, so check for it
60 if (mainStageFocusedApplication && !mainStageFocusedApplication.focused)
61 mainStageFocusedApplication = null;
62 }
63 }
64 }
65 }
67 function activateApplication(desktopFile, argument) {
68 var appId;
70 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
71 // TODO: convert entire shell to use appId only.
72 if (desktopFile.indexOf(".desktop") >= 0) {
73 appId = desktopFileToAppId(desktopFile);
74 } else {
75 appId = desktopFile;
76 }
78 var application = ApplicationManager.findApplication(appId);
79 if (application !== null) {
80 return application;
81 }
83 var execFlags = sideStageEnabled ? ApplicationManager.NoFlag : ApplicationManager.ForceMainStage;
85 if (argument) {
86 return ApplicationManager.startApplication(appId, execFlags, [argument]);
87 } else {
88 return ApplicationManager.startApplication(appId, execFlags);
89 }
90 }
92 function stopApplication(application) {
93 var appId;
95 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
96 // TODO: convert entire shell to use appId only.
97 if (typeof application == "string") {
98 appId = application;
99 } else {
100 appId = desktopFileToAppId(application.desktopFile);
101 }
103 ApplicationManager.stopApplication(appId);
104 }
106 function focusApplication(application) {
107 if (application == null || application == undefined) {
108 return;
109 }
111 ApplicationManager.focusApplication(application.appId);
112 }
114 function unfocusCurrentApplication() {
115 ApplicationManager.unfocusCurrentApplication();
116 }
118 function moveRunningApplicationStackPosition(from, to, stage) {
119 if (from == to || from < 0 || to < 0) return;
121 if (stage == ApplicationInfo.SideStage) {
122 sideStageModel.move(from, to);
123 } else {
124 mainStageModel.move(from, to);
125 }
126 }
128 function getApplicationFromDesktopFile(desktopFile, stage) {
129 var appId;
131 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
132 // TODO: convert entire shell to use appId only.
133 if (desktopFile.indexOf(".desktop") >= 0) {
134 appId = desktopFileToAppId(desktopFile);
135 } else {
136 appId = desktopFile;
137 }
139 for (var i = 0, len = ApplicationManager.count; i < len; i++ ) {
140 var app = ApplicationManager.get(i);
142 // if stage not specified, return whichever app running on either stage
143 if (app.appId == appId && (stage == undefined || app.stage == stage)) {
144 return app;
145 }
146 }
147 }
149 function desktopFileToAppId(desktopFile) {
150 var right = desktopFile.lastIndexOf(".desktop");
151 var left = desktopFile.lastIndexOf("/");
152 if (left == -1 || right == -1 || left == right) {
153 console.log("ApplicationManagerWrapper: unable to extract appId from '" + desktopFile + "'");
154 return "";
155 }
156 return desktopFile.substring(left+1, right);
157 }
159 Connections {
160 target: ApplicationManager
161 onFocusRequested: {
162 // FIXME: hardcoded mapping from ApplicationManager.FavoriteApplications
163 // enum to desktop files
164 var desktopFile
165 if (favoriteApplication == ApplicationManager.CameraApplication) {
166 desktopFile = "/usr/share/applications/camera-app.desktop"
167 } else if (favoriteApplication == ApplicationManager.GalleryApplication) {
168 desktopFile = "/usr/share/applications/gallery-app.desktop"
169 } else if (favoriteApplication == ApplicationManager.ShareApplication) {
170 desktopFile = "/usr/share/applications/share-app.desktop"
171 } else if (favoriteApplication == ApplicationManager.BrowserApplication) {
172 desktopFile = "/usr/share/applications/webbrowser-app.desktop"
173 } else if (favoriteApplication == ApplicationManager.PhoneApplication) {
174 desktopFile = "/usr/share/applications/phone-app.desktop"
175 } else if (favoriteApplication == ApplicationManager.DialerApplication) {
176 desktopFile = "/usr/share/applications/dialer-app.desktop"
177 } else if (favoriteApplication == ApplicationManager.MessagingApplication) {
178 desktopFile = "/usr/share/applications/messaging-app.desktop"
179 } else if (favoriteApplication == ApplicationManager.AddressbookApplication) {
180 desktopFile = "/usr/share/applications/address-book-app.desktop"
181 }
183 applicationManager.focusRequested(desktopFile);
184 }
185 }