File: database.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 (445 lines) | stat: -rw-r--r-- 11,262 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* database.vala - Easy-to-use interface to Listaller's software database(s)
 *
 * 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;

namespace Listaller {

public enum AppSource {
	ALL,
	EXTERN,
	NATIVEPKG,
	UNKNOWN;
}

private enum ForceDB {
	NONE,
	PRIVATE,
	SHARED;
}

private class SoftwareDB : MsgObject {
	private InternalDB? db_shared;
	private InternalDB? db_priv;
	private Settings conf;

	public ForceDB force_db { get; set; }

	public signal void application (AppItem appid);

	public SoftwareDB (Settings liconf, bool include_shared = true) {
		base ();

		db_shared = null;
		db_priv = null;
		force_db = ForceDB.NONE;

		// We just store the settings, so other objects can fetch them
		conf = liconf;

		if (include_shared) {
			db_shared = new InternalDB (true, conf.testmode);

			/* If we open a shared DB and have root-access, don't open the
			 * private database. It makes no sense someone is working as root
			 * and installing private stuff into root's home-dir */
			if (!is_root ()) {
				db_priv = new InternalDB (false, conf.testmode);
			}
		} else {
			// If we only want to open the personal DB, don't touch the shared one
			db_priv = new InternalDB (false, conf.testmode);
		}

		// Forward messages of the internal DBs to this meta-db
		if (db_priv != null)
			db_priv.message.connect ( (m) => { this.message (m); } );
		if (db_shared != null)
			db_shared.message.connect ( (m) => { this.message (m); } );
	}

	private void emit_dberror (string details) {
		// Emit a database error
		emit_error (ErrorEnum.DATABASE_FAILURE, details);
	}

	private bool shared_db_canbeused (bool error = false) {
		bool ret = true;
		if (db_shared == null) {
			ret = false;
		}

		if (force_db == ForceDB.PRIVATE)
			return false;

		/* If shared db does not exist AND we don't have root-access, opening the db will fail.
		 * (so we check for this case here, just to be sure) */
		if (ret)
			if ((!is_root ()) && (!FileUtils.test (db_shared.get_database_name (), FileTest.EXISTS))) {
				db_shared = null;
				ret = false;
			}

		if ((!ret) && (error))
			emit_dberror (_("Tried to perform action on shared software database, but the database is not opened! (maybe a permission problem?)"));

		return ret;
	}

	private bool private_db_canbeused (bool error = false) {
		if (force_db == ForceDB.SHARED)
			return false;

		if (db_priv == null) {
			if (error)
				emit_dberror (_("Tried to perform action on private software database, but the database is not opened! (this should never happen!)"));
			return false;
		}

		return true;
	}

	public Settings get_liconf () {
		return conf;
	}

	public bool open_read () {
		bool ret = false;
		try {
			if (private_db_canbeused ())
				ret = db_priv.open_r ();
			if (shared_db_canbeused ()) {
				if (!ret)
					ret = db_shared.open_r ();
				else
					db_shared.open_r ();
			}
		} catch (Error e) {
			emit_dberror (_("Unable to open database for read-only: %s").printf (e.message));
			ret = false;
		}
		return ret;
	}

	public bool open_write () {
		bool ret = false;
		try {
			if (private_db_canbeused ())
				ret = db_priv.open_rw ();
			if (shared_db_canbeused ()) {
				// We can only have write access to shared DB if we're root
				if (is_root ()) {
					if (!ret)
						ret = db_shared.open_rw ();
					else
						db_shared.open_rw ();
				} else {
					if (!ret)
						ret = db_shared.open_r ();
					else
						db_shared.open_r ();
				}
			}
		} catch (Error e) {
			emit_dberror (_("Unable to open database for writing: %s").printf (e.message));
			ret = false;
		}
		return ret;
	}

	public bool database_locked () {
		bool ret = false;
		if (shared_db_canbeused ())
			ret = db_shared.database_locked ();
		if (ret)
			return true;
		if (private_db_canbeused ())
			ret = db_priv.database_locked ();

		return ret;
	}

	public bool database_writeable () {
		bool ret = false;
		if (shared_db_canbeused ())
			ret = db_shared.database_writeable ();
		if (ret)
			return true;
		if (private_db_canbeused ())
			ret = db_priv.database_writeable ();

		return ret;
	}

	public bool add_application (AppItem app) {
		bool ret;
		try {
		if (app.shared) {
			if (shared_db_canbeused (true))
				ret = db_shared.add_application (app);
			else
				return false;
		} else {
			if (private_db_canbeused (true))
				ret = db_priv.add_application (app);
			else
				return false;
		}
		} catch (Error e) {
			emit_dberror (_("Unable to add application to database: %s").printf (e.message));
			ret = false;
		}
		return ret;
	}

	public bool add_application_filelist (AppItem app, Collection<IPK.FileEntry> flist) {
		bool ret;
		try {
			if (app.shared) {
				if (shared_db_canbeused (true))
					ret = db_shared.add_application_filelist (app, flist);
				else
					return false;
			} else {
				if (private_db_canbeused (true))
					ret = db_priv.add_application_filelist (app, flist);
				else
					return false;
			}
		} catch (Error e) {
			emit_dberror (_("Unable to add application file list to database: %s").printf (e.message));
			ret = false;
		}
		return ret;
	}

	public ArrayList<string>? get_application_filelist (AppItem app) {
		ArrayList<string>? res;
		try {
			if (app.shared) {
				if (shared_db_canbeused (true))
					res = db_shared.get_application_filelist (app);
				else
					return null;
			} else {
				if (private_db_canbeused (true))
					res = db_priv.get_application_filelist (app);
				else
					return null;
			}
		} catch (Error e) {
			emit_dberror (_("Unable to fetch application file list: %s").printf (e.message));
			res = null;
		}
		return res;
	}

	public int get_applications_count () {
		int cnt = 0;
		try {
			if (shared_db_canbeused ())
				cnt += db_shared.get_applications_count ();
			if (private_db_canbeused ())
				cnt += db_priv.get_applications_count ();
		} catch (Error e) {
			emit_dberror (_("Unable to count applications: %s").printf (e.message));
		}
		return cnt;
	}

	public AppItem? get_application_by_idname (string appIdName) {
		AppItem? app = null;

		try {
			// It is important that we prefer the personal over the shared database!
			// (so command-line tools allow removing the personal app first)
			if (private_db_canbeused ())
					app = db_priv.get_application_by_idname (appIdName);

			if (app == null)
				if (shared_db_canbeused ())
					app = db_shared.get_application_by_idname (appIdName);

		} catch (Error e) {
			emit_dberror (_("Unable to fetch application by id: %s").printf (e.message));
		}

		return app;
	}

	public AppItem? get_application_by_id (AppItem app) {
		AppItem? resApp;
		resApp = get_application_by_idname (app.idname);
		return resApp;
	}

	public ArrayList<AppItem>? get_applications_by_fullname (string appFullName) {
		ArrayList<AppItem>? appList = null;
		try {
			if (shared_db_canbeused ())
				appList = db_shared.get_applications_by_fullname (appFullName);

			if (appList == null)
				if (private_db_canbeused ())
					appList = db_priv.get_applications_by_fullname (appFullName);
		} catch (Error e) {
			emit_dberror (_("Unable to fetch application by name: %s").printf (e.message));
		}

		return appList;
	}

	public bool remove_application (AppItem app) {
		bool ret;
		try {
			if (app.shared) {
				if (shared_db_canbeused (true))
					ret = db_shared.remove_application (app);
				else
					return false;
			} else {
				if (private_db_canbeused (true))
					ret = db_priv.remove_application (app);
				else
					return false;
			}
		} catch (Error e) {
			emit_dberror (_("Unable to remove application from database: %s").printf (e.message));
			ret = false;
		}
		return ret;
	}

	public ArrayList<AppItem> find_applications ([CCode (array_null_terminated = true, array_length = false)] string[] values) {
		var list = new ArrayList<AppItem> ();

		try {
			if (shared_db_canbeused ()) {
				var tmp = db_shared.find_applications (values);
				list.add_all (tmp);
			}
			if (private_db_canbeused ()) {
				var tmp = db_priv.find_applications (values);
				list.add_all (tmp);
			}
		} catch (Error e) {
			emit_dberror (_("Unable to search application database: %s").printf (e.message));
		}

		return list;
	}

	public void _internal_emit_dbapps (double one, ref ArrayList<AppItem> appList) {
		uint i = 1;
		foreach (AppItem app in appList) {
			application (app);
			change_main_progress ((int) Math.round (one * i));
			i++;
		}
	}

	public bool find_all_applications (AppSource filter, out ArrayList<AppItem> appList = null) {
		ArrayList<AppItem> alist = new ArrayList<AppItem> ();

		double one = 100d / get_applications_count ();

		if (private_db_canbeused ()) {
			alist = db_priv.get_applications_all ();
		}
		if (shared_db_canbeused ()) {
			if (alist == null)
				alist = db_shared.get_applications_all ();
			else
				alist.add_all (db_shared.get_applications_all ());
		}
		if (alist == null)
			return false;
		_internal_emit_dbapps (one, ref alist);
		appList = alist;

		return true;
	}

	public bool set_application_dependencies (string appName, ArrayList<IPK.Dependency> deps) {
		if (is_root ()) {
			if (shared_db_canbeused (true))
				return db_shared.set_application_dependencies (appName, deps);
			else
				return false;
		} else {
			if (private_db_canbeused (true))
				return db_priv.set_application_dependencies (appName, deps);
			else
				return false;
		}
	}

	/* Dependency stuff */

	public bool add_dependency (IPK.Dependency dep) {
		bool ret = false;
		try {
			if (is_root ()) {
				if (shared_db_canbeused (true))
					ret = db_shared.add_dependency (dep);
				else
					return false;
			} else {
				if (private_db_canbeused (true))
					ret = db_priv.add_dependency (dep);
				else
					return false;
			}
		} catch (Error e) {
			emit_dberror (_("Unable to add a dependency to database: %s").printf (e.message));
			return false;
		}
		return ret;
	}

	public IPK.Dependency? get_dependency_by_id (string depIdName) {
		IPK.Dependency? dep = null;
		if (shared_db_canbeused ())
			dep = db_shared.get_dependency_by_id (depIdName);

		if (dep == null)
			if (private_db_canbeused ())
				dep = db_priv.get_dependency_by_id (depIdName);
		return dep;
	}

	public bool set_dependency_environment (string depIdName, string env) {
		bool ret = false;
		if (shared_db_canbeused ())
			ret = db_shared.set_dependency_environment (depIdName, env);

		if (!ret)
			if (private_db_canbeused ())
				ret = db_priv.set_dependency_environment (depIdName, env);

		return ret;
	}
}

} // End of namespace