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

« back to all changes in this revision

Viewing changes to src/reboot.c

  • Committer: mvo
  • Date: 2005-12-03 05:38:57 UTC
  • Revision ID: gustavo@niemeyer.net-20051203053857-d415a9ae9c2b33e4
* debian/99update-notifier:
  - only touch file if the dir exists
* src/update-notifier.c:
  - check if the user is in the admin group

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifdef HAVE_CONFIG_H
2
 
#include "config.h"
3
 
#endif
4
 
 
5
 
#include <sys/types.h>
6
 
#include <sys/stat.h>
7
 
#include <unistd.h>
8
 
 
9
 
#include <glade/glade.h>
10
 
#include <libnotify/notify.h>
11
 
 
12
 
#include "update-notifier.h"
13
 
#include "update.h"
14
 
 
15
 
 
16
 
#define GDM_SIGNAL "/usr/bin/gdm-signal"
17
 
 
18
 
static GladeXML *xml;
19
 
 
20
 
static gint
21
 
show_notification (TrayApplet *ta)
22
 
{
23
 
        NotifyNotification *n;
24
 
        int                 x, y;
25
 
 
26
 
        /* Get the location of the icon.  If there's no location, come back
27
 
         * in a few seconds. */
28
 
        gdk_window_get_origin (GTK_WIDGET(ta->tray_icon)->window, &x, &y);
29
 
        if ((x <= 0) || (y <= 0))
30
 
                return TRUE;
31
 
 
32
 
        /* Check whether the icon is still visible, may have gone away */
33
 
        if (!GTK_WIDGET_VISIBLE (ta->tray_icon))
34
 
                return FALSE;
35
 
 
36
 
        /* Create and show the notification */
37
 
        n = notify_notification_new (_("Restart Required"),
38
 
                                     _("In order to complete the update of "
39
 
                                       "your system, restarting is strongly "
40
 
                                       "recommended.\n\n"
41
 
                                       "Click this icon to restart your "
42
 
                                       "computer."),
43
 
                                     GTK_STOCK_DIALOG_WARNING,
44
 
                                     ta->tray_icon);
45
 
        notify_notification_set_timeout (n, 60000);
46
 
        notify_notification_show (n, NULL);
47
 
        g_object_set_data (G_OBJECT(ta->tray_icon), "notification", n);
48
 
 
49
 
        return FALSE;
50
 
}
51
 
 
52
 
gboolean
53
 
reboot_check (TrayApplet *ta)
54
 
{
55
 
        struct stat statbuf;
56
 
 
57
 
        /* If the file doesn't exist, we don't need to reboot */
58
 
        if (stat (REBOOT_FILE, &statbuf)) {
59
 
                NotifyNotification *n;
60
 
 
61
 
                gtk_widget_hide (GTK_WIDGET(ta->tray_icon));
62
 
 
63
 
                /* Hide any notification popup */
64
 
                n = g_object_get_data (G_OBJECT(ta->tray_icon), "notification");
65
 
                if (n)
66
 
                        notify_notification_close (n, NULL);
67
 
                g_object_set_data (G_OBJECT(ta->tray_icon), "notification", NULL);
68
 
 
69
 
                return TRUE;
70
 
        }
71
 
 
72
 
        /* Skip the rest if the icon is already visible */
73
 
        if (GTK_WIDGET_VISIBLE(GTK_WIDGET(ta->tray_icon)))
74
 
                return TRUE;
75
 
 
76
 
        gtk_tooltips_set_tip (GTK_TOOLTIPS(ta->tooltip),
77
 
                              GTK_WIDGET (ta->eventbox),
78
 
                              _("Restart Required"),
79
 
                              _("Click this icon to restart your computer."));
80
 
 
81
 
        gtk_widget_show (GTK_WIDGET(ta->tray_icon));
82
 
 
83
 
        /* Check whether the user doesn't like notifications */
84
 
        if (gconf_client_get_bool ((GConfClient*) ta->user_data,
85
 
                                   GCONF_KEY_NO_UPDATE_NOTIFICATIONS, NULL))
86
 
                return TRUE;
87
 
 
88
 
        /* Show the notification, after a delay so it doesn't look ugly
89
 
         * if we've just logged in */
90
 
        g_timeout_add(5000, show_notification, ta);
91
 
 
92
 
        return TRUE;
93
 
}
94
 
 
95
 
static void
96
 
request_reboot (void)
97
 
{
98
 
        GnomeClient *client;
99
 
        char *cmd[] = { GDM_SIGNAL, "--reboot", NULL };
100
 
 
101
 
        client = gnome_master_client ();
102
 
        g_return_if_fail (client != NULL);
103
 
 
104
 
        /* Tell gdm to reboot once we exit the session */
105
 
        g_spawn_sync (NULL, cmd, NULL, G_SPAWN_STDOUT_TO_DEV_NULL,
106
 
                      NULL, NULL, NULL, NULL, NULL, NULL);
107
 
 
108
 
        /* Tell gnome-session to save and exit the session without asking
109
 
         * the user. */
110
 
        gnome_client_request_save (client,
111
 
                                   GNOME_SAVE_GLOBAL,
112
 
                                   TRUE,
113
 
                                   GNOME_INTERACT_NONE,
114
 
                                   FALSE,
115
 
                                   TRUE);
116
 
}
117
 
 
118
 
static gboolean
119
 
button_release_cb (GtkWidget *widget,
120
 
                   GdkEventButton *event,
121
 
                   TrayApplet *ta)
122
 
{
123
 
        if ((event->button == 1) && (event->type == GDK_BUTTON_RELEASE)) {
124
 
                GtkWidget *dia;
125
 
 
126
 
                dia = glade_xml_get_widget (xml, "dialog_reboot");
127
 
                if (gtk_dialog_run (dia) == GTK_RESPONSE_OK)
128
 
                        request_reboot ();
129
 
 
130
 
                gtk_widget_hide (dia);
131
 
        }
132
 
 
133
 
        return TRUE;
134
 
}
135
 
 
136
 
 
137
 
void
138
 
reboot_tray_icon_init (TrayApplet *ta)
139
 
{
140
 
        GtkWidget *widget;
141
 
        GtkImage *image;
142
 
 
143
 
        xml = glade_xml_new (GLADEDIR"reboot-dialog.glade", NULL, NULL);
144
 
 
145
 
        widget = glade_xml_get_widget (xml, "image");
146
 
        gtk_image_set_from_file (GTK_IMAGE(widget), "/usr/share/pixmaps/reboot.png");
147
 
 
148
 
        ta->user_data = gconf_client_get_default();
149
 
 
150
 
        g_signal_connect (G_OBJECT(ta->tray_icon),
151
 
                          "button-release-event",
152
 
                          G_CALLBACK (button_release_cb),
153
 
                          ta);
154
 
 
155
 
        /* Check for updates for the first time */
156
 
        reboot_check (ta);
157
 
}