on fc480e9 help sign in
unity8/Components/ApplicationManagerWrapper.qml
187 lines 7.2 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: might be called with appId, but mostly with desktopFile
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: might be called with appId, or else with Application object
95 if (typeof application == "string") {
96 appId = application;
97 } else {
98 appId = desktopFileToAppId(application.desktopFile);
99 }
101 ApplicationManager.stopApplication(appId);
102 }
104 function focusApplication(application) {
105 if (application == null || application == undefined) {
106 return;
107 }
109 ApplicationManager.focusApplication(application.appId);
110 }
112 function unfocusCurrentApplication() {
113 ApplicationManager.unfocusCurrentApplication();
114 }
116 function moveRunningApplicationStackPosition(from, to, stage) {
117 if (from == to || from < 0 || to < 0) return;
119 if (stage == ApplicationInfo.SideStage) {
120 sideStageModel.move(from, to);
121 } else {
122 mainStageModel.move(from, to);
123 }
124 }
126 function getApplicationFromDesktopFile(desktopFile, stage) {
127 var appId;
129 // HACK: might be called with appId, but mostly with desktopFile
130 if (desktopFile.indexOf(".desktop") >= 0) {
131 appId = desktopFileToAppId(desktopFile);
132 } else {
133 appId = desktopFile;
134 }
136 for (var i = 0, len = ApplicationManager.count; i < len; i++ ) {
137 var app = ApplicationManager.get(i);
139 // if stage not specified, return whichever app running on either stage
140 if (app.appId == appId && (stage == undefined || app.stage == stage)) {
141 return app;
142 }
143 }
144 }
146 function desktopFileToAppId(desktopFile) {
147 var right = desktopFile.lastIndexOf(".desktop");
148 var left = desktopFile.lastIndexOf("/");
149 if (left == -1 || right == -1 || left == right) {
150 console.log("ApplicationManagerWrapper: unable to extract appId from '" + desktopFile + "'");
151 return "";
152 }
153 return desktopFile.substring(left+1, right);
154 }
156 function appIdToDesktopFile(desktopFile) {
157 return "/usr/share/applications/" + desktopFile + ".desktop"
158 }
160 Connections {
161 target: ApplicationManager
162 onFocusRequested: {
163 // FIXME: hardcoded mapping from ApplicationManager.FavoriteApplications
164 // enum to desktop files
165 var desktopFile
166 if (favoriteApplication == ApplicationManager.CameraApplication) {
167 desktopFile = "/usr/share/applications/camera-app.desktop"
168 } else if (favoriteApplication == ApplicationManager.GalleryApplication) {
169 desktopFile = "/usr/share/applications/gallery-app.desktop"
170 } else if (favoriteApplication == ApplicationManager.ShareApplication) {
171 desktopFile = "/usr/share/applications/share-app.desktop"
172 } else if (favoriteApplication == ApplicationManager.BrowserApplication) {
173 desktopFile = "/usr/share/applications/webbrowser-app.desktop"
174 } else if (favoriteApplication == ApplicationManager.PhoneApplication) {
175 desktopFile = "/usr/share/applications/phone-app.desktop"
176 } else if (favoriteApplication == ApplicationManager.DialerApplication) {
177 desktopFile = "/usr/share/applications/dialer-app.desktop"
178 } else if (favoriteApplication == ApplicationManager.MessagingApplication) {
179 desktopFile = "/usr/share/applications/messaging-app.desktop"
180 } else if (favoriteApplication == ApplicationManager.AddressbookApplication) {
181 desktopFile = "/usr/share/applications/address-book-app.desktop"
182 }
184 applicationManager.focusRequested(desktopFile);
185 }
186 }