~ubuntu-core-dev/update-notifier/ubuntu

4 by michiels
2004-10-22 Michiel Sikkes <michiel@eyesopened.nl>
1
/* upgrade-notifier.c
2
 * Copyright (C) 2004 Lukas Lipka <lukas@pmad.net>
3
 *           (C) 2004 Michael Vogt <mvo@debian.org>
4
 *           (C) 2004 Michiel Sikkes <michiel@eyesopened.nl>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 * Boston, MA 02111-1307, USA.
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
26
#include <signal.h>
27
#include <gnome.h>
28
#include <gtk/gtk.h>
29
30
#include "upgrade-notifier.h"
31
#include "eggtrayicon.h"
32
33
#define ICON_FILE PACKAGE_DATA_DIR"/pixmaps/upgrade-notifier.png"
34
#define UPGRADE_CHECKER PACKAGE_LIB_DIR"/upgrade-notifier/apt-check"
35
#define PACKAGE_MANAGER "/home/michiel/Devel/updatecenter/update-center"
36
37
#define TIMEOUT 1000*60
38
39
void
40
invoke_update_manager ()
41
{
42
	gchar *argv[0];
43
	gchar *cmd;
44
	cmd = g_strdup_printf ("%s", PACKAGE_MANAGER);
45
	argv[0] = cmd;
46
	g_spawn_async (NULL, argv, NULL, G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, NULL, NULL);
47
48
	g_free (cmd);
49
}
50
51
gboolean
52
button_release_cb (GtkWidget *widget, 
53
		   GdkEventButton *event, 
54
		   UpgradeNotifier *un)
55
{
56
	if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
57
		invoke_update_manager ();
58
	}
59
}
60
61
void
62
trayicon_update_tooltip (UpgradeNotifier *un)
63
{
64
	gchar *updates;
65
	gchar *explanation;
66
67
	updates = g_strdup_printf(_("There are %i updates available!"), 
68
			    un->num_upgrades);
69
70
	explanation = g_strdup(_("Press this icon to show the updates."));
71
72
	gtk_tooltips_set_tip(un->tooltip, GTK_WIDGET (un->eventbox), 
73
			    updates, explanation);
74
75
	g_free (updates);
76
	g_free (explanation);
77
}
78
79
void 
80
trayicon_load (UpgradeNotifier *un)
81
{
82
	GdkPixbuf *pixbuf;
83
84
	pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (un->icon));
85
	
86
	if (pixbuf)
87
		g_object_unref (G_OBJECT (pixbuf));
88
89
	pixbuf = gdk_pixbuf_new_from_file (ICON_FILE, NULL);
90
91
	gtk_image_set_from_pixbuf (GTK_IMAGE (un->icon), pixbuf);
92
}
93
94
gboolean
95
trayicon_create (UpgradeNotifier *un)
96
{
97
	un->tooltip = gtk_tooltips_new ();
98
	un->tray_icon = egg_tray_icon_new ("apt upgrade-notifier");
99
	un->eventbox = gtk_event_box_new ();
100
	un->icon = gtk_image_new ();
101
102
	trayicon_load (un);
103
104
	gtk_container_add (GTK_CONTAINER (un->eventbox), un->icon);
105
	gtk_container_add (GTK_CONTAINER (un->tray_icon), un->eventbox);
106
107
	g_signal_connect (G_OBJECT(un->tray_icon),
108
			  "button-release-event",
109
			  G_CALLBACK (button_release_cb),
110
			  un);
111
112
	gtk_widget_show_all (GTK_WIDGET (un->tray_icon));
113
114
	/* Initial tooltip update */
115
	trayicon_update_tooltip (un);
116
117
	return TRUE;
118
}
119
120
void
121
update_check (UpgradeNotifier *un)
122
{
123
	FILE *f = popen(UPGRADE_CHECKER, "r");
124
	char s[401];
125
126
	un->num_upgrades = 0;
127
128
	/* Check the number of upgrades trough the helper script */
129
	while (fgets (s, 400, f) != NULL) {
130
		un->num_upgrades++;
131
	}
132
133
	fclose (f);
134
135
	if (un->is_displayed) {
136
		if (un->num_upgrades == 0) {
137
			/* No updates anymore, lets shoot ourselves */
138
			gtk_widget_destroy (GTK_WIDGET (un->tray_icon));
139
		} else {
140
			/* We still have updates, update the tooltip */
141
			trayicon_update_tooltip (un);
142
		}
143
	} else { 
144
		/* We're not visible. Create the trayicon */
145
		if (trayicon_create (un))
146
			un->is_displayed = TRUE;
147
	}
148
}
149
150
int 
151
main (int argc, char *argv[])
152
{
153
	GnomeClient *client;
154
	UpgradeNotifier *un;
155
156
	gnome_program_init (PACKAGE, PACKAGE_VERSION, 
157
			    LIBGNOMEUI_MODULE,
158
			    argc, argv, 
159
			    GNOME_PARAM_NONE);
160
161
	client = gnome_master_client ();
162
163
	/* Make sure we die when the session dies */
164
	gnome_client_set_restart_style (client, GNOME_RESTART_ANYWAY);
165
166
	/* Create the UpgradeNotifier object */
167
	un = g_new0 (UpgradeNotifier, 1);
168
169
	/* Check for updates for the first time */
170
	update_check (un);
171
172
	/* Set a time-out for the update check */
173
	g_timeout_add (TIMEOUT, (GSourceFunc)update_check, un);
174
175
	/* Start the main gtk loop */
176
	gtk_main ();
177
178
	return 0;
179
}