Mercurial > projects > pk-update-icon
annotate main.c @ 9:0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
author | Guido Berhoerster <guido@berhoerster.name> |
---|---|
date | Tue, 11 Oct 2011 17:07:49 +0200 |
parents | 58a3312a1c59 |
children | 64f05992d8ec |
rev | line source |
---|---|
2 | 1 /* |
2 * Copyright (C) 2011 Pavol Rusnak <stick@gk2.sk> | |
8
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
3 * Copyright (C) 2011 Guido Berhoerster <gber@opensuse.org> |
2 | 4 * |
5 * Licensed under the GNU General Public License Version 2 | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 */ | |
21 | |
3
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
22 #include "main.h" |
0 | 23 #include "notify.h" |
24 #include "packagekit.h" | |
8
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
25 #include <locale.h> |
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
26 #include <glib/gi18n.h> |
0 | 27 #include <gtk/gtk.h> |
9
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
28 #include <unique/unique.h> |
0 | 29 |
3
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
30 struct UpdatesInfo info; |
0 | 31 |
1
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
32 GtkStatusIcon *tray_icon; |
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
33 |
0 | 34 void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data) |
35 { | |
36 char *argv[2] = { "gpk-update-viewer", NULL }; | |
37 g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); | |
38 } | |
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(); | |
8
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
50 item = gtk_menu_item_new_with_mnemonic(_("_Quit")); |
0 | 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"); | |
8
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
68 gtk_status_icon_set_title(tray_icon, _("Software Update(s)")); |
0 | 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); | |
1
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
76 if (info.critical > 0) { |
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
77 gtk_status_icon_set_from_icon_name(tray_icon, "software-update-urgent"); |
3
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
78 gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info)); |
1
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
79 } else |
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
80 if (info.normal > 0) { |
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
81 gtk_status_icon_set_from_icon_name(tray_icon, "software-update-available"); |
3
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
82 gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info)); |
1
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
83 } |
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
84 return TRUE; |
0 | 85 } |
86 | |
87 int main(int argc, char **argv) | |
88 { | |
89 GtkStatusIcon *tray_icon; | |
9
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
90 UniqueApp *app; |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
91 int exitval = 0; |
0 | 92 |
8
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
93 setlocale(LC_ALL, ""); |
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
94 bindtextdomain(PACKAGE, LOCALEDIR); |
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
95 bind_textdomain_codeset(PACKAGE, "UTF-8"); |
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
96 textdomain(PACKAGE); |
58a3312a1c59
add internationalization support
Guido Berhoerster <guido@berhoerster.name>
parents:
3
diff
changeset
|
97 |
0 | 98 gtk_init(&argc, &argv); |
9
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
99 app = unique_app_new(APP_NAME, NULL); |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
100 if (unique_app_is_running(app)) { |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
101 g_printerr("Another instance of pk-update-icon is already running. Exiting.\n"); |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
102 exitval = 1; |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
103 goto out; |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
104 } |
0 | 105 tray_icon = create_tray_icon(); |
106 init_notify(); | |
107 | |
1
483f5fe9d2b2
change icon and tooltip of the tray icon
Pavol Rusnak <stick@gk2.sk>
parents:
0
diff
changeset
|
108 periodic_check(NULL); |
3
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
109 send_notify(&info); |
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
110 // update tray icon and tooltip every 2 hours |
f8ad23e60000
code rework, show notify window just once
Pavol Rusnak <stick@gk2.sk>
parents:
2
diff
changeset
|
111 g_timeout_add_seconds(2*3600, periodic_check, NULL); |
0 | 112 |
113 gtk_main(); | |
114 | |
9
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
115 g_object_unref(tray_icon); |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
116 |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
117 out: |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
118 g_object_unref(app); |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
119 |
0e50d3652326
use libunique to ensure that only a single instance of pk-update-icon can be run
Guido Berhoerster <guido@berhoerster.name>
parents:
8
diff
changeset
|
120 return exitval; |
0 | 121 } |