comparison main.c @ 14:64f05992d8ec

GObject-based rewrite use asynchronous packagekit-glib API use persistent menu widget and notification object update existing notification when new updates become available, close it when no updates are available show status icon when updates are available, hide it when no updates are available hide icon when gpk-update-viewer is executed, check for updates when gpk-update-viewer exits introduce a startup delay before the first check for updates is made add context menu item to manually trigger a check for updates remove context menu item for quitting pk-update-icon
author Guido Berhoerster <guido@berhoerster.name>
date Thu, 20 Oct 2011 08:19:22 +0200
parents 0e50d3652326
children 7af115023d5a
comparison
equal deleted inserted replaced
13:dca97330d81e 14:64f05992d8ec
1 /* 1 /*
2 * Copyright (C) 2011 Pavol Rusnak <stick@gk2.sk>
3 * Copyright (C) 2011 Guido Berhoerster <gber@opensuse.org> 2 * Copyright (C) 2011 Guido Berhoerster <gber@opensuse.org>
4 * 3 *
5 * Licensed under the GNU General Public License Version 2 4 * Licensed under the GNU General Public License Version 2
6 * 5 *
7 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
17 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */ 19 */
21 20
22 #include "main.h"
23 #include "notify.h"
24 #include "packagekit.h"
25 #include <locale.h> 21 #include <locale.h>
26 #include <glib/gi18n.h> 22 #include <glib/gi18n.h>
27 #include <gtk/gtk.h> 23 #include <gtk/gtk.h>
28 #include <unique/unique.h> 24 #include <unique/unique.h>
25 #include <libnotify/notify.h>
26 #include "pkui-icon.h"
29 27
30 struct UpdatesInfo info; 28 int
31 29 main(int argc, char **argv)
32 GtkStatusIcon *tray_icon;
33
34 void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data)
35 { 30 {
36 char *argv[2] = { "gpk-update-viewer", NULL }; 31 PkuiIcon *icon;
37 g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); 32 UniqueApp *app;
38 } 33 int exitval = 0;
39
40 static void tray_menu_destroy(GtkWidget *menu, gpointer userdata)
41 {
42 gtk_widget_destroy(menu);
43 g_object_unref(menu);
44 }
45
46 void tray_icon_on_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
47 {
48 GtkWidget *item;
49 GtkWidget *menu = gtk_menu_new();
50 item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
51 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
52 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_main_quit), user_data);
53 gtk_widget_show(item);
54 g_object_ref(menu);
55 g_object_ref_sink(menu);
56 g_object_unref(menu);
57 g_signal_connect(G_OBJECT(menu), "selection-done", G_CALLBACK(tray_menu_destroy), NULL);
58 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, user_data, button, activate_time);
59 gtk_widget_show_all(GTK_WIDGET(menu));
60 }
61
62 static GtkStatusIcon *create_tray_icon()
63 {
64 tray_icon = gtk_status_icon_new();
65 g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(tray_icon_on_click), NULL);
66 g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_icon_on_menu), NULL);
67 gtk_status_icon_set_from_icon_name(tray_icon, "system-software-update");
68 gtk_status_icon_set_title(tray_icon, _("Software Update(s)"));
69 gtk_status_icon_set_visible(tray_icon, TRUE);
70 return tray_icon;
71 }
72
73 gboolean periodic_check(gpointer user_data)
74 {
75 query_packagekit(&info);
76 if (info.critical > 0) {
77 gtk_status_icon_set_from_icon_name(tray_icon, "software-update-urgent");
78 gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info));
79 } else
80 if (info.normal > 0) {
81 gtk_status_icon_set_from_icon_name(tray_icon, "software-update-available");
82 gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info));
83 }
84 return TRUE;
85 }
86
87 int main(int argc, char **argv)
88 {
89 GtkStatusIcon *tray_icon;
90 UniqueApp *app;
91 int exitval = 0;
92 34
93 setlocale(LC_ALL, ""); 35 setlocale(LC_ALL, "");
36
94 bindtextdomain(PACKAGE, LOCALEDIR); 37 bindtextdomain(PACKAGE, LOCALEDIR);
95 bind_textdomain_codeset(PACKAGE, "UTF-8"); 38 bind_textdomain_codeset(PACKAGE, "UTF-8");
96 textdomain(PACKAGE); 39 textdomain(PACKAGE);
97 40
98 gtk_init(&argc, &argv); 41 gtk_init(&argc, &argv);
42
99 app = unique_app_new(APP_NAME, NULL); 43 app = unique_app_new(APP_NAME, NULL);
100 if (unique_app_is_running(app)) { 44 if (unique_app_is_running(app)) {
101 g_printerr("Another instance of pk-update-icon is already running. Exiting.\n"); 45 g_printerr("Another instance of pk-update-icon is already "
46 "running. Exiting.\n");
102 exitval = 1; 47 exitval = 1;
103 goto out; 48 goto out;
104 } 49 }
105 tray_icon = create_tray_icon();
106 init_notify();
107 50
108 periodic_check(NULL); 51 notify_init(PACKAGE);
109 send_notify(&info); 52
110 // update tray icon and tooltip every 2 hours 53 icon = pkui_icon_new(10, 2 * 3600);
111 g_timeout_add_seconds(2*3600, periodic_check, NULL);
112 54
113 gtk_main(); 55 gtk_main();
114 56
115 g_object_unref(tray_icon); 57 g_object_unref(icon);
116
117 out: 58 out:
118 g_object_unref(app); 59 g_object_unref(app);
60 if (notify_is_initted())
61 notify_uninit ();
119 62
120 return exitval; 63 return (exitval);
121 } 64 }