File: manager.vala

package info (click to toggle)
listaller 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,812 kB
  • sloc: ansic: 2,152; sh: 1,735; perl: 1,452; xml: 874; cpp: 765; java: 157; makefile: 150; cs: 48; python: 24
file content (332 lines) | stat: -rw-r--r-- 9,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* manager.vala - Manage installed applications (remove them / maintain their dependencies)
 *
 * Copyright (C) 2010-2012 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

using GLib;
using Gee;
using Listaller;
using Listaller.Utils;

[CCode (cheader_filename = "listaller-glib/manager.h")]

namespace Listaller {

/**
 * Allows managing installed applications
 *
 * This class allows managing installed applications as
 * well as performing maintainance tasks to keep applications
 * running.
 */
public class Manager : MsgObject {
	private Settings conf;

	public signal void status_changed (StatusItem status);
	public signal void application (AppItem appid);

	public Settings settings {
		get { return conf; }
		set { conf = value; }
	}

	/*
	 * @param: settings A valid LiSettings instance, describing basic settings (or null)
	 */
	public Manager (Settings? settings) {
		base ();
		conf = settings;
		if (conf == null)
			conf = new Settings (false);
	}

	private void emit_status (StatusEnum status, string info) {
		StatusItem item = new StatusItem (status);
		item.info = info;
		status_changed (item);
	}

	private bool init_db (out SoftwareDB sdb, bool writeable = true) {
		SoftwareDB db = new SoftwareDB (conf, true);
		// Connect the database events with this application manager
		connect_with_object (db, ObjConnectFlags.NONE);
		db.application.connect ( (a) => { this.application (a); } );

		sdb = db;
		if (writeable) {
			if (!db.open_write ()) {
				emit_error (ErrorEnum.DB_OPEN_FAILED, _("Unable to open software database for reading & writing!"));
				return false;
			}
		} else {
			if (!db.open_read ()) {
				emit_error (ErrorEnum.DB_OPEN_FAILED, _("Unable to open software database for reading only!"));
				return false;
			}
		}
		return true;
	}

	public bool find_applications (AppSource filter, out ArrayList<AppItem> appList = null) {
		SoftwareDB db;
		if (!init_db (out db, false))
			return false;

		return db.find_all_applications (filter, out appList);
	}

	/** Find applications which match the strings in values
	 *
	 * @values:
	 */
	public bool find_applications_by_values (AppSource filter,
						 [CCode (array_null_terminated = true, array_length = false)] string[] values,
						 out ArrayList<AppItem> appList = null) {
		SoftwareDB db;
		if (!init_db (out db, false))
			return false;

		ArrayList<AppItem> res = db.find_applications (values);
		// Emit signals for found applications
		foreach (AppItem app in res)
			application (app);
		return true;
	}

	public ArrayList<AppItem>? get_applications_by_fullname (string full_name) {
		SoftwareDB db;
		if (!init_db (out db, false))
			return null;

		var appList = db.get_applications_by_fullname (full_name);
		return appList;
	}

	public AppItem? get_application_by_idname (string idname) {
		SoftwareDB db;
		if (!init_db (out db, false))
			return null;

		AppItem? app = db.get_application_by_idname (idname);
		return app;
	}

	private bool remove_application_internal (AppItem app) {
		// Emit that we're starting
		emit_status (StatusEnum.ACTION_STARTED,
			     _("Removal of %s started.").printf (app.full_name));

		SoftwareDB db;
		if (!init_db (out db))
			return false;

		// Check if this application exists, if not exit
		if (db.get_application_by_id (app) == null) {
			emit_error (ErrorEnum.REMOVAL_FAILED, _("Could not uninstall application %s. It is not installed.").printf (app.full_name));
			return false;
		}
		// Remove all files which belong to this application
		ArrayList<string>? files = db.get_application_filelist (app);
		if (files == null) {
			emit_error (ErrorEnum.REMOVAL_FAILED, _("'%s' has no file-list registered. The software database might be broken.").printf (app.full_name));
			return false;
		}

		foreach (string fname in files) {
			if (FileUtils.test (fname, FileTest.EXISTS)) {
				int ret = FileUtils.remove (fname);
				if (ret != 0) {
					emit_error (ErrorEnum.REMOVAL_FAILED, _("Could not remove file %s!").printf (fname));
					return false;
				}
				string dirn = Path.get_dirname (fname);
				// Remove directory if it is empty
				if (dir_is_empty (dirn)) {
					DirUtils.remove (dirn);
				}
			}
		}
		bool ret = db.remove_application (app);

		emit_status (StatusEnum.REMOVAL_FINISHED,
			     _("Removal of %s finished.").printf (app.full_name));

		return ret;
	}

	private void pk_progress_cb (PackageKit.Progress progress, PackageKit.ProgressType type) {
		if ((type == PackageKit.ProgressType.PERCENTAGE) ||
			(type == PackageKit.ProgressType.SUBPERCENTAGE)) {
				change_progress (progress.percentage, progress.subpercentage);
			}
	}

	private bool remove_shared_application_internal (AppItem app) {
		bool ret = true;
		PackageKit.Task pktask = new PackageKit.Task ();

		/* PackageKit will handle all Listaller superuser uninstallations.
		 * Therefore, PackageKit needs to be compiled with Listaller support enabled.
		 */

		PackageKit.Results? pkres;
		pktask.background = false;

		string pklipackage = app_item_build_pk_package_id (app);
		change_progress (0, -1);

		try {
			pkres = pktask.remove_packages ({ pklipackage, null }, false, true, null, pk_progress_cb);
		} catch (Error e) {
			emit_error (ErrorEnum.REMOVAL_FAILED, e.message);
			return false;
		}

		if (pkres.get_exit_code () != PackageKit.Exit.SUCCESS) {
			PackageKit.Error error = pkres.get_error_code ();
			emit_error (ErrorEnum.REMOVAL_FAILED, error.get_details ());
			return false;
		}

		change_progress (100, -1);

		// Emit status message (setup finished)
		emit_status (StatusEnum.REMOVAL_FINISHED,
			     _("Removal of %s finished.").printf (app.full_name));

		return ret;

	}

	public bool remove_application (AppItem app) {
		app.fast_check ();

		bool sumode_old = conf.sumode;
		if (app.shared != conf.sumode) {
			if (app.shared)
				debug (_("Trying to remove shared application, but AppManager is not in superuser mode!\nSetting AppManager to superuse mode now."));
			else
				debug (_("Trying to remove local application, but AppManager is in superuser mode!\nSetting AppManager to local mode now."));
			conf.sumode = app.shared;
		}

		bool ret;
		if ((app.shared) && (!is_root ())) {
			// Call PackageKit if we aren't root and want to remove a shared application
			ret = remove_shared_application_internal (app);
		} else {
			// Try normal app removal
			ret = remove_application_internal (app);
		}

		conf.sumode = sumode_old;

		return ret;
	}

	public bool refresh_appitem (ref AppItem item) {
		SoftwareDB db;
		if (!init_db (out db, false))
			return false;

		// We only want to fetch dependencies from the correct database, so limit DB usage
		if (item.shared)
			db.force_db = ForceDB.SHARED;
		else
			db.force_db = ForceDB.PRIVATE;

		var tmpItem = db.get_application_by_idname (item.idname);
		if (tmpItem == null)
			return false;
		item = tmpItem;

		return true;
	}

	public string? get_application_filelist_as_string (AppItem app) {
		app.fast_check ();

		SoftwareDB db;
		if (!init_db (out db))
			return null;

		// Check if this application exists, if not exit
		if (db.get_application_by_id (app) == null) {
			warning ("Unable to get file-list for application %s - app is not installed!", app.idname);
			return null;
		}

		// Remove all files which belong to this application
		ArrayList<string>? files = db.get_application_filelist (app);
		if (files == null) {
			critical ("Couldn't retrieve file-list for application %s! All apps should have a filelist, this should never happen!", app.idname);
			return null;
		}

		string res = "";
		foreach (string s in files)
			res += "%s\n".printf (s);

		return res;
	}

	public string get_app_ld_environment (AppItem app) {
		string[] depStr = app.dependencies.split ("\n");
		if (depStr.length <= 0) {
			debug ("No dependencies set! Application was maybe not retrieved from software DB and therefore lacks the required data.");
			debug ("Setting empty environment...");
			return "";
		}

		SoftwareDB db;
		if (!init_db (out db, false)) {
			debug ("Unable to open DB, app environment will be empty!");
			return "";
		}

		// We only want to fetch dependencies from the correct database, so limit DB usage
		if (app.shared)
			db.force_db = ForceDB.SHARED;
		else
			db.force_db = ForceDB.PRIVATE;

		// A new DepManager to resolve environment
		DepManager depman = new DepManager (db);

		string paths = "";

		HashSet<IPK.Dependency> depList = depman.dependencies_from_idlist (depStr);

		foreach (IPK.Dependency dep in depList) {
			// Now get paths for library, if possible (if dependency is a library)
			string p = depman.get_absolute_library_path (dep);
			if (p != "")
				paths = "%s;%s".printf (paths, p);
		}

		return paths;
	}

	public bool scan_applications () {
		//TODO: Scan for 3rd-party installed apps
		return true;
	}

}

} // End of namespace