on 527a6ca help sign in
unity8/Components/ApplicationManagerWrapper.qml
171 lines 6.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
19import Utils 0.1
21Item {
22 id: applicationManager
24 property var mainStageApplications: mainStageModel
25 property var sideStageApplications: sideStageModel
26 property var mainStageFocusedApplication: null
27 property var sideStageFocusedApplication: null
28 property bool sideStageEnabled: true
29 signal focusRequested(string desktopFile)
30 property bool keyboardVisible: ApplicationManager.keyboardVisible
31 property int keyboardHeight: ApplicationManager.keyboardHeight
33 property bool fake: ApplicationManager.fake ? ApplicationManager.fake : false
35 SortFilterProxyModel {
36 id: mainStageModel
37 model: ApplicationManager
38 dynamicSortFilter: true
39 filterRole: ApplicationManager.RoleStage
40 filterRegExp: RegExp(ApplicationInfo.MainStage)
42 onCountChanged: { print("MODEL Main:", count, ApplicationManager.RoleStage, ApplicationManager.RoleFocused)
43 mainStageFocusedApplication = ApplicationManager.get(mainStageModel.findFirst(ApplicationManager.RoleFocused, true));
44 print("MS", mainStageFocusedApplication)
45 }
46 onLayoutChanged: print("LAYOUT")
47 onDataChanged: print("DATA")
48 }
50 SortFilterProxyModel {
51 id: sideStageModel
52 model: ApplicationManager
53 dynamicSortFilter: true
54 filterRole: ApplicationManager.RoleStage
55 filterRegExp: RegExp(ApplicationInfo.SideStage)
57 onDataChanged: { print("MODEL Side", count)
58 sideStageFocusedApplication = ApplicationManager.get(sideStageModel.findFirst(ApplicationManager.RoleFocused, true));
59 print("SS", sideStageFocusedApplication)
60 }
61 }
63 function activateApplication(desktopFile, argument) {
64 var appId;
66 // HACK: might be called with appId, but mostly with desktopFile
67 if (desktopFile.indexOf(".desktop") = -1) {
68 appId = desktopFileToAppId(desktopFile);
69 } else {
70 appId = desktopFile;
71 }
73 var application = ApplicationManager.findApplication(appId);
74 if (application !== null) {
75 return application;
76 }
78 var execFlags = sideStageEnabled ? ApplicationManager.NoFlag : ApplicationManager.ForceMainStage;
80 if (argument) {
81 return ApplicationManager.startApplication(appId, execFlags, [argument]);
82 } else {
83 return ApplicationManager.startApplication(appId, execFlags);
84 }
85 }
87 function stopProcess(application) {
88 var appId;
90 // HACK: might be called with appId, or else with Application object
91 if (typeof application == "string") {
92 appId = desktopFileToAppId(application.desktopFile);
93 } else {
94 appId = application;
95 }
97 ApplicationManager.stopProcess(appId);
98 }
100 function focusApplication(application) {
101 if (application == null || application == undefined) {
102 return;
103 }
105 ApplicationManager.focusApplication(application.appId);
106 }
108 function unfocusCurrentApplication() {
109 ApplicationManager.unfocusCurrentApplication();
110 }
112 function moveRunningApplicationStackPosition(from, to, stage) { //FIXME: stage unused!
113 if (from !== to && from >= 0 && to >= 0) ApplicationManager.move(from, to);
114 }
116 function getApplicationFromDesktopFile(desktopFile, stage) {
117 var foundSideStageApp, foundMainStageApp;
118 var appId = desktopFileToAppId(desktopFile);
120 for (var i = 0, len = ApplicationManager.count; i < len; i++ ) {
121 var app = ApplicationManager.get(i);
123 // if stage not specified, return whichever app running on either stage
124 if (app.appId == appId && (stage == undefined || app.stage == stage)) {
125 return app;
126 }
127 }
128 }
130 function desktopFileToAppId(desktopFile) {
131 var right = desktopFile.lastIndexOf(".desktop");
132 var left = desktopFile.lastIndexOf("/");
133 if (left == -1 || right == -1 || left == right) {
134 console.log("ApplicationManagerWrapper: unable to extract appId from '" + desktopFile + "'");
135 return "";
136 }
137 return desktopFile.substring(left+1, right);
138 }
140 function appIdToDesktopFile(desktopFile) {
141 return "/usr/share/applications/" + desktopFile + ".desktop"
142 }
144 Connections {
145 target: ApplicationManager
146 onFocusRequested: {
147 // FIXME: hardcoded mapping from ApplicationManager.FavoriteApplications
148 // enum to desktop files
149 var desktopFile
150 if (favoriteApplication == ApplicationManager.CameraApplication) {
151 desktopFile = "/usr/share/applications/camera-app.desktop"
152 } else if (favoriteApplication == ApplicationManager.GalleryApplication) {
153 desktopFile = "/usr/share/applications/gallery-app.desktop"
154 } else if (favoriteApplication == ApplicationManager.ShareApplication) {
155 desktopFile = "/usr/share/applications/share-app.desktop"
156 } else if (favoriteApplication == ApplicationManager.BrowserApplication) {
157 desktopFile = "/usr/share/applications/webbrowser-app.desktop"
158 } else if (favoriteApplication == ApplicationManager.PhoneApplication) {
159 desktopFile = "/usr/share/applications/phone-app.desktop"
160 } else if (favoriteApplication == ApplicationManager.DialerApplication) {
161 desktopFile = "/usr/share/applications/dialer-app.desktop"
162 } else if (favoriteApplication == ApplicationManager.MessagingApplication) {
163 desktopFile = "/usr/share/applications/messaging-app.desktop"
164 } else if (favoriteApplication == ApplicationManager.AddressbookApplication) {
165 desktopFile = "/usr/share/applications/address-book-app.desktop"
166 }
168 applicationManager.focusRequested(desktopFile);
169 }
170 }