File: lipa-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 (134 lines) | stat: -rw-r--r-- 3,876 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
/* lipa-manager.vala -- Handle application management tasks in lipa
 *
 * Copyright (C) 2012 Matthias Klumpp
 *
 * Licensed under the GNU General Public License Version 3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using GLib;
using Config;
using Listaller;

public class LipaManager : LipaModule {
	private Manager mgr;
	private bool show_progress;

	public LipaManager () {
		base ();
		show_progress = true;

		mgr = new Manager (liconf);
		mgr.message.connect (manager_message);
		mgr.error_code.connect (manager_error_code);
		//! mgr.status_changed.connect (manager_status_changed);
		mgr.progress_changed.connect (manager_progress_changed);
		mgr.application.connect (manager_new_application);
	}

	private void manager_error_code (ErrorItem error) {
		// End progress-bar, if it is shown
		show_progress = false;
		progress_bar.end ();

		stderr.printf ("%s\n", error.details);
		error_code = (int) error.error;
	}

	private void manager_message (MessageItem message) {
		stdout.printf ("%s\n", message.details);
	}

	private void manager_progress_changed (int progress) {
		if (show_progress)
			progress_bar.set_percentage (progress);
	}

	private string app_ownership_str (AppItem app) {
		string str_ownership;
		if (!app.shared)
			str_ownership = _("personal");
		else
			str_ownership = _("shared");

		return str_ownership;
	}

	private void print_appitem (AppItem app) {
		stdout.printf ("[%s|%s] %s -- %s\n", _("INSTALLED"), app_ownership_str (app), app.full_name, app.summary);
	}

	private void manager_new_application (AppItem app) {
		print_appitem (app);
	}

	public void remove_application (string app_identifier) {
		bool ret;
		AppItem? app = null;

		// Try to find an application which matches the name the user throws at us
		app = mgr.get_application_by_idname (app_identifier);
		if (app == null) {
			var appList = mgr.get_applications_by_fullname (app_identifier);
			if ((appList == null) || (appList.size == 0)) {
				stderr.printf (_("Could not find application which matches '%s'!\n"), app_identifier);
				error_code = 8;
				return;
			}
			// TODO: This has to be handled better!
			stdout.printf ("Selected application:\n");
			app = appList[0];
			print_appitem (app);
		}

		ret = console_get_prompt (_("Do you want to remove %s (%s) now?").printf (app.full_name, app_ownership_str (app)), true);
		// If user doesn't want to remove the application, exit
		if (!ret)
			return;

		progress_bar.start (_("Removing"));
		// Go!
		ret = mgr.remove_application (app);
		// On success, set everything to done
		if (ret) {
			progress_bar.set_percentage (100);
			progress_bar.end ();
		}

		if (ret) {
			stdout.printf ("%s\n", _("Removal of %s completed successfully!").printf (app.full_name));
		} else {
			stdout.printf ("%s\n", _("Removal of %s failed!").printf (app.full_name));
			error_code = 3;
		}
	}

	public void list_applications (bool all = false) {
		AppSource filter = AppSource.EXTERN;
		if (all)
			filter = AppSource.ALL;

		show_progress = false;
		mgr.find_applications (filter);
		show_progress = true;
	}

	public override void terminate_action () {
		if (mgr != null) {
			critical ("Please don't kill the application, it could damage installed applications and produce unexpected behavior!");
		}
	}

}