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
|
/* upgrade-notifier.c
* Copyright (C) 2004 Lukas Lipka <lukas@pmad.net>
* (C) 2004 Michael Vogt <mvo@debian.org>
* (C) 2004 Michiel Sikkes <michiel@eyesopened.nl>
*
* 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 2 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>
#include <gnome.h>
#include <gtk/gtk.h>
#include "upgrade-notifier.h"
#include "eggtrayicon.h"
#define ICON_FILE PACKAGE_DATA_DIR"/pixmaps/upgrade-notifier.png"
#define UPGRADE_CHECKER PACKAGE_LIB_DIR"/upgrade-notifier/apt-check"
#define PACKAGE_MANAGER "/home/michiel/Devel/updatecenter/update-center"
#define TIMEOUT 1000*60
void
invoke_update_manager ()
{
gchar *argv[0];
gchar *cmd;
cmd = g_strdup_printf ("%s", PACKAGE_MANAGER);
argv[0] = cmd;
g_spawn_async (NULL, argv, NULL, G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, NULL, NULL);
g_free (cmd);
}
gboolean
button_release_cb (GtkWidget *widget,
GdkEventButton *event,
UpgradeNotifier *un)
{
if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
invoke_update_manager ();
}
}
void
trayicon_update_tooltip (UpgradeNotifier *un)
{
gchar *updates;
gchar *explanation;
updates = g_strdup_printf(_("There are %i updates available!"),
un->num_upgrades);
explanation = g_strdup(_("Press this icon to show the updates."));
gtk_tooltips_set_tip(un->tooltip, GTK_WIDGET (un->eventbox),
updates, explanation);
g_free (updates);
g_free (explanation);
}
void
trayicon_load (UpgradeNotifier *un)
{
GdkPixbuf *pixbuf;
pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (un->icon));
if (pixbuf)
g_object_unref (G_OBJECT (pixbuf));
pixbuf = gdk_pixbuf_new_from_file (ICON_FILE, NULL);
gtk_image_set_from_pixbuf (GTK_IMAGE (un->icon), pixbuf);
}
gboolean
trayicon_create (UpgradeNotifier *un)
{
un->tooltip = gtk_tooltips_new ();
un->tray_icon = egg_tray_icon_new ("apt upgrade-notifier");
un->eventbox = gtk_event_box_new ();
un->icon = gtk_image_new ();
trayicon_load (un);
gtk_container_add (GTK_CONTAINER (un->eventbox), un->icon);
gtk_container_add (GTK_CONTAINER (un->tray_icon), un->eventbox);
g_signal_connect (G_OBJECT(un->tray_icon),
"button-release-event",
G_CALLBACK (button_release_cb),
un);
gtk_widget_show_all (GTK_WIDGET (un->tray_icon));
/* Initial tooltip update */
trayicon_update_tooltip (un);
return TRUE;
}
void
update_check (UpgradeNotifier *un)
{
FILE *f = popen(UPGRADE_CHECKER, "r");
char s[401];
un->num_upgrades = 0;
/* Check the number of upgrades trough the helper script */
while (fgets (s, 400, f) != NULL) {
un->num_upgrades++;
}
fclose (f);
if (un->is_displayed) {
if (un->num_upgrades == 0) {
/* No updates anymore, lets shoot ourselves */
gtk_widget_destroy (GTK_WIDGET (un->tray_icon));
} else {
/* We still have updates, update the tooltip */
trayicon_update_tooltip (un);
}
} else {
/* We're not visible. Create the trayicon */
if (trayicon_create (un))
un->is_displayed = TRUE;
}
}
int
main (int argc, char *argv[])
{
GnomeClient *client;
UpgradeNotifier *un;
gnome_program_init (PACKAGE, PACKAGE_VERSION,
LIBGNOMEUI_MODULE,
argc, argv,
GNOME_PARAM_NONE);
client = gnome_master_client ();
/* Make sure we die when the session dies */
gnome_client_set_restart_style (client, GNOME_RESTART_ANYWAY);
/* Create the UpgradeNotifier object */
un = g_new0 (UpgradeNotifier, 1);
/* Check for updates for the first time */
update_check (un);
/* Set a time-out for the update check */
g_timeout_add (TIMEOUT, (GSourceFunc)update_check, un);
/* Start the main gtk loop */
gtk_main ();
return 0;
}
|