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

« back to all changes in this revision

Viewing changes to src/tray.c

  • Committer: Balint Reczey
  • Date: 2019-09-20 20:02:13 UTC
  • mfrom: (975.1.4 master)
  • Revision ID: balint.reczey@canonical.com-20190920200213-8mx0d8aitaf1xo9c
MergeĀ lp:~rbalint/update-notifier/updates-available-world-readable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* tray.c
2
 
 * Copyright (C) 2004 Lukas Lipka <lukas@pmad.net>
3
 
 *           (C) 2004 Michael Vogt <mvo@debian.org>
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Lesser General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Lesser General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
19
 
 */
20
 
 
21
 
#include <signal.h>
22
 
#include <gnome.h>
23
 
#include <gtk/gtk.h>
24
 
#include <glade/glade.h>
25
 
#include "eggtrayicon.h"
26
 
 
27
 
#define ICON_FILE PACKAGE_DATA_DIR"/pixmaps/upgrade-notifier.png"
28
 
#define UPGRADE_CHECKER PACKAGE_LIB_DIR"/upgrade-notifier/apt-check"
29
 
#define PACKAGE_MANAGER "/usr/sbin/synaptic"
30
 
 
31
 
const char * gksu_message = _("Please enter the root password to start the package manager");
32
 
 
33
 
// tick is each (1/1000)sec 
34
 
#define TIMEOUT 1000*60*60
35
 
 
36
 
EggTrayIcon *tray_icon = NULL;
37
 
GtkWidget *icon = NULL;
38
 
GtkWidget *box = NULL;
39
 
GtkTooltips *tt = NULL;
40
 
GtkWidget *menu = NULL;
41
 
GladeXML *xml;
42
 
guint timer_id;
43
 
 
44
 
GString *all_upgrades = NULL;
45
 
int num_upgrades = 0;
46
 
 
47
 
static void start_package_manager()
48
 
{
49
 
   gchar *cmd;
50
 
   cmd = g_strdup_printf("gksu -u root -m \"%s\" "
51
 
                         " \"%s ; killall -HUP upgrade-notifier \" &", 
52
 
                         gksu_message, PACKAGE_MANAGER);
53
 
   system(cmd);
54
 
   g_free(cmd);
55
 
}
56
 
 
57
 
static void create_menu ()
58
 
{
59
 
        GtkWidget *menuitem;
60
 
        
61
 
        menu = gtk_menu_new ();
62
 
 
63
 
        menuitem = gtk_menu_item_new_with_label (_("Start package manager"));
64
 
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
65
 
        g_signal_connect (G_OBJECT (menuitem), "activate", 
66
 
                          G_CALLBACK (start_package_manager), 0);
67
 
 
68
 
        menuitem = gtk_separator_menu_item_new ();
69
 
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
70
 
 
71
 
        menuitem = gtk_menu_item_new_with_label (_("Exit"));
72
 
        g_signal_connect (G_OBJECT (menuitem), "activate", 
73
 
                          G_CALLBACK (gtk_main_quit), 0);
74
 
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
75
 
        
76
 
        gtk_widget_show_all (menu);
77
 
}
78
 
 
79
 
static void tray_icon_load ()
80
 
{
81
 
        GdkPixbuf *pixbuf;
82
 
 
83
 
        pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));
84
 
 
85
 
        if (pixbuf)
86
 
                g_object_unref (G_OBJECT (pixbuf));
87
 
        
88
 
        pixbuf = gdk_pixbuf_new_from_file (ICON_FILE, NULL);
89
 
 
90
 
        gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
91
 
 
92
 
#if 0
93
 
        gtk_image_set_from_stock(GTK_IMAGE(icon), 
94
 
                                 "gtk-dialog-warning",
95
 
                                 GTK_ICON_SIZE_SMALL_TOOLBAR);
96
 
#endif
97
 
 
98
 
        // hide by default
99
 
        gtk_widget_hide(icon);
100
 
}
101
 
 
102
 
static void clicked (GtkWidget *widget, GdkEventButton *event, gpointer data)
103
 
{
104
 
   GtkWidget *view, *dia;
105
 
   GtkTextBuffer *buffer;
106
 
 
107
 
 
108
 
   if(event->button == 1 && event->type==GDK_2BUTTON_PRESS) {
109
 
 
110
 
      //xml = glade_xml_new("upgrade-dialog.glade",NULL, NULL);
111
 
      dia = glade_xml_get_widget(xml, "dialog_upgrades");
112
 
      
113
 
      view = glade_xml_get_widget(xml, "textview_upgrades");
114
 
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
115
 
      gtk_text_buffer_set_text (buffer, all_upgrades->str, -1);
116
 
      
117
 
      if(gtk_dialog_run(dia) == GTK_RESPONSE_OK) 
118
 
         start_package_manager();
119
 
      gtk_widget_hide (dia);
120
 
   } 
121
 
 
122
 
   if(event->button == 3) 
123
 
     gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,  
124
 
                   event->button, event->time); 
125
 
   
126
 
}
127
 
 
128
 
void tray_create ()
129
 
{
130
 
   tt = gtk_tooltips_new();
131
 
   
132
 
   create_menu ();
133
 
   
134
 
   xml = glade_xml_new(PACKAGE_LIB_DIR"/upgrade-notifier/upgrade-dialog.glade",NULL, NULL);     
135
 
 
136
 
   tray_icon = egg_tray_icon_new ("apt upgrade-notifier");
137
 
   box = gtk_event_box_new ();
138
 
   icon = gtk_image_new ();
139
 
 
140
 
   gtk_container_add (GTK_CONTAINER (box), icon);
141
 
   gtk_container_add (GTK_CONTAINER (tray_icon), box);
142
 
   gtk_widget_show_all (GTK_WIDGET (tray_icon));
143
 
   
144
 
   g_signal_connect (G_OBJECT (box), "button-press-event", 
145
 
                     G_CALLBACK (clicked), NULL);
146
 
   
147
 
   tray_icon_load ();
148
 
}
149
 
 
150
 
void tray_destroy ()
151
 
{
152
 
        GdkPixbuf *pixbuf;
153
 
        pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));
154
 
        if (pixbuf)
155
 
                g_object_unref (G_OBJECT (pixbuf));
156
 
        gtk_widget_destroy (icon);
157
 
        gtk_widget_destroy (GTK_WIDGET (tray_icon));
158
 
}
159
 
 
160
 
 
161
 
void timer(gpointer data)
162
 
{
163
 
   gchar **strarray;
164
 
   num_upgrades = 0;
165
 
 
166
 
   if(all_upgrades != NULL)
167
 
      g_string_free(all_upgrades, TRUE);
168
 
   all_upgrades = g_string_new("");
169
 
 
170
 
   FILE *f = popen(UPGRADE_CHECKER,"r");
171
 
   char s[401];
172
 
   gboolean upgrade_available = FALSE;
173
 
   while( fgets(s, 400, f) != NULL) {
174
 
      strarray = g_strsplit(s," ", 0);
175
 
      g_string_append(all_upgrades, strarray[1]);
176
 
      g_string_append(all_upgrades, "\n");
177
 
      g_strfreev(strarray);
178
 
 
179
 
      num_upgrades++;
180
 
   }
181
 
   fclose(f);
182
 
 
183
 
   if(num_upgrades > 0) {
184
 
      gchar *s;
185
 
      s = g_strdup_printf(_("%i system upgrades available"), num_upgrades);
186
 
      gtk_tooltips_set_tip(tt, GTK_WIDGET(box), s, NULL);
187
 
      g_free(s);
188
 
      gtk_widget_show(icon);
189
 
   } else {
190
 
      gtk_widget_hide(icon);
191
 
   }
192
 
}
193
 
 
194
 
void sighup_handler(int j, siginfo_t *i, void *d)
195
 
{
196
 
   //g_print("sighub_handler\n");
197
 
   // reread timer
198
 
   timer(NULL);
199
 
}
200
 
 
201
 
 
202
 
int main (int argc, char *argv[])
203
 
{
204
 
   GnomeClient *client;
205
 
   gnome_program_init ("upgrade-notifier", "0.3", LIBGNOMEUI_MODULE,
206
 
                       argc, argv, GNOME_PARAM_NONE);
207
 
 
208
 
   client = gnome_master_client ();
209
 
   gnome_client_set_restart_style (client, GNOME_RESTART_ANYWAY);
210
 
   // make sure we die if the session dies
211
 
   gtk_signal_connect (GTK_OBJECT (client), "die",
212
 
                       GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
213
 
 
214
 
   struct sigaction new_act;
215
 
   memset( &new_act, 0, sizeof( new_act ) );
216
 
   new_act.sa_handler = sighup_handler;
217
 
   sigaction( SIGHUP, &new_act, NULL);
218
 
 
219
 
   // create icons and menu
220
 
   tray_create ();
221
 
 
222
 
   // fire timer up once, then set timeout
223
 
   timer(NULL);
224
 
   timer_id = gtk_timeout_add(TIMEOUT, (GtkFunction)timer, NULL);
225
 
 
226
 
   // run, the eventloop now takes care
227
 
   gtk_main ();
228
 
   tray_destroy ();
229
 
 
230
 
   return 0;
231
 
}