1 | using Notify; |
---|
2 | using GLib; |
---|
3 | using Gtk; |
---|
4 | |
---|
5 | namespace VineLinux |
---|
6 | { |
---|
7 | class UpdateNotification : Notify.Notification |
---|
8 | { |
---|
9 | private TrayIcon tray_icon; |
---|
10 | |
---|
11 | public UpdateNotification(TrayIcon icon) |
---|
12 | { |
---|
13 | tray_icon = icon; |
---|
14 | } |
---|
15 | |
---|
16 | public void showNotification(int packages) |
---|
17 | { |
---|
18 | string title = _("Package Update"); |
---|
19 | string content; |
---|
20 | string icon = "vine-notify-update-red"; |
---|
21 | if (packages > 1) |
---|
22 | { |
---|
23 | content = _("%d packages are available to upgrade.").printf(packages); |
---|
24 | } |
---|
25 | else if (packages > 0) |
---|
26 | { |
---|
27 | content = _("An package is.available to upgrade."); |
---|
28 | } |
---|
29 | else |
---|
30 | { |
---|
31 | title = _("This system is up to date."); |
---|
32 | content = ""; |
---|
33 | icon = "vine-notify-update"; |
---|
34 | } |
---|
35 | |
---|
36 | try |
---|
37 | { |
---|
38 | update(title, content, icon); |
---|
39 | if (packages > 0) |
---|
40 | { |
---|
41 | show(); |
---|
42 | } |
---|
43 | } |
---|
44 | catch(GLib.Error e) |
---|
45 | { |
---|
46 | GLib.warning ("Notification.show() [%s]", e.message); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public override void closed() |
---|
51 | { |
---|
52 | if (this.closed_reason == 2) |
---|
53 | { |
---|
54 | tray_icon.showDialogs(); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | class RebootNotification : Notify.Notification |
---|
60 | { |
---|
61 | private TrayIcon tray_icon; |
---|
62 | |
---|
63 | public RebootNotification(TrayIcon icon) |
---|
64 | { |
---|
65 | tray_icon = icon; |
---|
66 | summary = _("Restarting computer is required"); |
---|
67 | body = _("You must restart this computer to use upgraded software."); |
---|
68 | icon_name = "vine-notify-update-orange"; |
---|
69 | } |
---|
70 | |
---|
71 | public void showNotification() |
---|
72 | { |
---|
73 | try |
---|
74 | { |
---|
75 | show(); |
---|
76 | } |
---|
77 | catch(GLib.Error e) |
---|
78 | { |
---|
79 | GLib.warning ("Notification.show() [%s]", e.message); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | public override void closed() |
---|
84 | { |
---|
85 | if (this.closed_reason == 2) |
---|
86 | { |
---|
87 | tray_icon.showDialogs(); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | [DBus (name = "org.vinelinux.NotifyUpdate")] |
---|
93 | class NotificationReceiver : Object |
---|
94 | { |
---|
95 | UpdateNotification upgrade_notification; |
---|
96 | RebootNotification reboot_notification; |
---|
97 | public int upgrade_packages; |
---|
98 | public int require_reboot; |
---|
99 | private TrayIcon tray_icon; |
---|
100 | |
---|
101 | public NotificationReceiver(TrayIcon icon) |
---|
102 | { |
---|
103 | tray_icon = icon; |
---|
104 | upgrade_notification = new UpdateNotification(icon); |
---|
105 | reboot_notification = new RebootNotification(icon); |
---|
106 | upgrade_packages = 0; |
---|
107 | require_reboot = 0; |
---|
108 | |
---|
109 | Bus.own_name (BusType.SYSTEM, "org.vinelinux.NotifyUpdate", |
---|
110 | BusNameOwnerFlags.NONE, |
---|
111 | onBusAquired, |
---|
112 | () => stderr.printf ("Bus name aquired\n"), |
---|
113 | () => stderr.printf ("Could not aquire name\n")); |
---|
114 | } |
---|
115 | |
---|
116 | private void onBusAquired(DBusConnection conn) |
---|
117 | { |
---|
118 | try |
---|
119 | { |
---|
120 | conn.register_object("/org/vinelinux/NotifyUpdate", this); |
---|
121 | } |
---|
122 | catch(IOError e) |
---|
123 | { |
---|
124 | GLib.critical("Could not register service"); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public void showMessage(string msg) |
---|
129 | { |
---|
130 | string[] params = msg.split(" "); |
---|
131 | string icon_name; |
---|
132 | |
---|
133 | if (params.length != 2) |
---|
134 | { |
---|
135 | GLib.warning("invalid parameter: %s", msg); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | upgrade_packages = int.parse(params[0]); |
---|
140 | require_reboot = int.parse(params[1]); |
---|
141 | |
---|
142 | if (upgrade_packages > 0) |
---|
143 | { |
---|
144 | icon_name = "vine-notify-update-red"; |
---|
145 | upgrade_notification.showNotification(upgrade_packages); |
---|
146 | } |
---|
147 | else if (require_reboot != 0) |
---|
148 | { |
---|
149 | icon_name = "vine-notify-update-orange"; |
---|
150 | reboot_notification.showNotification(); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | icon_name = "vine-notify-update"; |
---|
155 | upgrade_notification.showNotification(0); |
---|
156 | } |
---|
157 | tray_icon.set_from_icon_name(icon_name); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | class TrayIcon: Gtk.StatusIcon |
---|
162 | { |
---|
163 | Gtk.Menu menu; |
---|
164 | Gtk.ImageMenuItem menuitem_quit; |
---|
165 | NotificationReceiver receiver; |
---|
166 | |
---|
167 | public TrayIcon() |
---|
168 | { |
---|
169 | set_from_icon_name("vine-notify-update"); |
---|
170 | receiver = new NotificationReceiver(this); |
---|
171 | initActions(); |
---|
172 | } |
---|
173 | |
---|
174 | private void initActions() |
---|
175 | { |
---|
176 | activate.connect(() => |
---|
177 | { |
---|
178 | showDialogs(); |
---|
179 | }); |
---|
180 | |
---|
181 | menu = new Gtk.Menu(); |
---|
182 | menuitem_quit = |
---|
183 | new Gtk.ImageMenuItem.from_stock(Gtk.Stock.QUIT, null); |
---|
184 | menu.append(menuitem_quit); |
---|
185 | menu.show_all(); |
---|
186 | popup_menu.connect((source, button, activate_time) => |
---|
187 | { |
---|
188 | menu.popup(null, null, source.position_menu, |
---|
189 | button, activate_time); |
---|
190 | }); |
---|
191 | menuitem_quit.activate.connect(Gtk.main_quit); |
---|
192 | } |
---|
193 | |
---|
194 | public void showDialogs() |
---|
195 | { |
---|
196 | if (receiver.upgrade_packages > 0) |
---|
197 | { |
---|
198 | showUpgradeDialog(receiver.upgrade_packages); |
---|
199 | } |
---|
200 | else if (receiver.require_reboot != 0) |
---|
201 | { |
---|
202 | showRebootDialog(); |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | showUpToDateDialog(); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | private void showUpToDateDialog() |
---|
211 | { |
---|
212 | var dialog = new Gtk.MessageDialog(null, |
---|
213 | Gtk.DialogFlags.MODAL, |
---|
214 | Gtk.MessageType.INFO, |
---|
215 | Gtk.ButtonsType.OK, |
---|
216 | ""); |
---|
217 | dialog.title = _("Package Update"); |
---|
218 | dialog.text = _("This system is up to date."); |
---|
219 | |
---|
220 | dialog.run(); |
---|
221 | dialog.destroy(); |
---|
222 | } |
---|
223 | |
---|
224 | private void showUpgradeDialog(int upgrade_packages) |
---|
225 | { |
---|
226 | Builder builder = new Builder(); |
---|
227 | Dialog dialog; |
---|
228 | Label summary; |
---|
229 | Gtk.ListStore store; |
---|
230 | FileStream f; |
---|
231 | string pkg; |
---|
232 | |
---|
233 | builder.set_translation_domain("vine-notify-update"); |
---|
234 | builder.add_from_file("/usr/share/vine-notify-update/vine-notify-update.glade"); |
---|
235 | dialog = builder.get_object("upgrade_dialog") as Dialog; |
---|
236 | summary = builder.get_object("summary") as Label; |
---|
237 | store = builder.get_object("pkglist") as Gtk.ListStore; |
---|
238 | |
---|
239 | if (upgrade_packages > 1) |
---|
240 | { |
---|
241 | summary.set_text(_("%d packages will be upgraded. Are you sure?").printf(upgrade_packages)); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | summary.set_text(_("A package will be upgraded. Are you sure?")); |
---|
246 | } |
---|
247 | |
---|
248 | if ((f = FileStream.open("/var/run/upgrade-list.txt", "r")) == null) |
---|
249 | { |
---|
250 | //dialog.destroy(); |
---|
251 | return; |
---|
252 | } |
---|
253 | while (!f.eof()) |
---|
254 | { |
---|
255 | TreeIter iter; |
---|
256 | |
---|
257 | pkg = f.read_line(); |
---|
258 | if (pkg != null && pkg.strip() != "") |
---|
259 | { |
---|
260 | store.append(out iter); |
---|
261 | store.set(iter, 0, pkg); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | if (dialog.run() == 1) |
---|
266 | { |
---|
267 | GLib.warning ("dialog: ok"); |
---|
268 | try |
---|
269 | { |
---|
270 | GLib.Process.spawn_command_line_async("synaptic --upgrade-mode --non-interactive --hide-main-window -o Synaptic::AskRelated=false"); |
---|
271 | } |
---|
272 | catch(SpawnError e) |
---|
273 | { |
---|
274 | GLib.warning ("cannot execute synaptic: [%s]", e.message); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | dialog.destroy(); |
---|
279 | } |
---|
280 | |
---|
281 | private void showRebootDialog() |
---|
282 | { |
---|
283 | var dialog = new Gtk.MessageDialog(null, |
---|
284 | Gtk.DialogFlags.MODAL, |
---|
285 | Gtk.MessageType.QUESTION, |
---|
286 | Gtk.ButtonsType.YES_NO, |
---|
287 | ""); |
---|
288 | dialog.title = _("Package Update"); |
---|
289 | dialog.text = _("You must restart this computer to use upgraded software."); |
---|
290 | |
---|
291 | if (dialog.run() == Gtk.ResponseType.YES) |
---|
292 | { |
---|
293 | } |
---|
294 | |
---|
295 | dialog.destroy(); |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | class NotifyUpdateMain |
---|
300 | { |
---|
301 | public static int main(string[] args) |
---|
302 | { |
---|
303 | TrayIcon status_icon; |
---|
304 | |
---|
305 | Gtk.init(ref args); |
---|
306 | if (!Notify.init("vine-notify-update")) |
---|
307 | { |
---|
308 | GLib.critical("Couldn't initialize libnotify"); |
---|
309 | return 1; |
---|
310 | } |
---|
311 | |
---|
312 | status_icon = new TrayIcon(); |
---|
313 | |
---|
314 | Gtk.main(); |
---|
315 | return 0; |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|