projects/pk-update-icon

changeset 3:f8ad23e60000

code rework, show notify window just once
author Pavol Rusnak <stick@gk2.sk>
date Tue Sep 27 12:15:39 2011 +0200 (2011-09-27)
parents 847ae02bc13b
children 9126094c5ef0
files main.c main.h notify.c notify.h packagekit.h
line diff
     1.1 --- a/main.c	Tue Sep 27 02:31:31 2011 +0200
     1.2 +++ b/main.c	Tue Sep 27 12:15:39 2011 +0200
     1.3 @@ -18,14 +18,12 @@
     1.4   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     1.5   */
     1.6  
     1.7 +#include "main.h"
     1.8  #include "notify.h"
     1.9  #include "packagekit.h"
    1.10  #include <gtk/gtk.h>
    1.11  
    1.12 -struct updates_info {
    1.13 -	int normal;
    1.14 -	int critical;
    1.15 -} UpdatesInfo;
    1.16 +struct UpdatesInfo info;
    1.17  
    1.18  GtkStatusIcon *tray_icon;
    1.19  
    1.20 @@ -65,22 +63,19 @@
    1.21  	gtk_status_icon_set_from_icon_name(tray_icon, "system-software-update");
    1.22  	gtk_status_icon_set_title(tray_icon, "Software Update");
    1.23  	gtk_status_icon_set_visible(tray_icon, TRUE);
    1.24 -
    1.25  	return tray_icon;
    1.26  }
    1.27  
    1.28  gboolean periodic_check(gpointer user_data)
    1.29  {
    1.30 -	struct UpdatesInfo info;
    1.31  	query_packagekit(&info);
    1.32 -	send_notify(info.normal, info.critical);
    1.33  	if (info.critical > 0) {
    1.34  		gtk_status_icon_set_from_icon_name(tray_icon, "software-update-urgent");
    1.35 -		gtk_status_icon_set_tooltip_text(tray_icon, "Critical updates available.");
    1.36 +		gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info));
    1.37  	} else
    1.38  	if (info.normal > 0) {
    1.39  		gtk_status_icon_set_from_icon_name(tray_icon, "software-update-available");
    1.40 -		gtk_status_icon_set_tooltip_text(tray_icon, "Updates available.");
    1.41 +		gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info));
    1.42  	}
    1.43  	return TRUE;
    1.44  }
    1.45 @@ -94,7 +89,9 @@
    1.46  	init_notify();
    1.47  
    1.48  	periodic_check(NULL);
    1.49 -	g_timeout_add(15 * 60 * 1000, periodic_check, NULL);
    1.50 +	send_notify(&info);
    1.51 +	// update tray icon and tooltip every 2 hours
    1.52 +	g_timeout_add_seconds(2*3600, periodic_check, NULL);
    1.53  
    1.54  	gtk_main();
    1.55  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/main.h	Tue Sep 27 12:15:39 2011 +0200
     2.3 @@ -0,0 +1,9 @@
     2.4 +#ifndef MAIN_H
     2.5 +#define MAIN_H 1
     2.6 +
     2.7 +struct UpdatesInfo {
     2.8 +	int normal;
     2.9 +	int critical;
    2.10 +};
    2.11 +
    2.12 +#endif
     3.1 --- a/notify.c	Tue Sep 27 02:31:31 2011 +0200
     3.2 +++ b/notify.c	Tue Sep 27 12:15:39 2011 +0200
     3.3 @@ -28,17 +28,22 @@
     3.4  	notify_init(NOTIFY_NAME);
     3.5  }
     3.6  
     3.7 -void send_notify(int normal, int critical)
     3.8 +void send_notify(struct UpdatesInfo *info)
     3.9  {
    3.10  	NotifyNotification *ntfy;
    3.11 -	char buf[128];
    3.12 -	if (critical > 0) {
    3.13 -		snprintf(buf, sizeof(buf), "There are %d software updates available, %d of them critical.", normal + critical, critical);
    3.14 -	} else {
    3.15 -		snprintf(buf, sizeof(buf), "There are %d software updates available.", normal);
    3.16 -	}
    3.17 -	ntfy = notify_notification_new(NOTIFY_NAME, buf, critical > 0 ? "software-update-urgent" : "software-update-available");
    3.18 +	ntfy = notify_notification_new(NOTIFY_NAME, notify_text(info), info->critical > 0 ? "software-update-urgent" : "software-update-available");
    3.19  	notify_notification_set_timeout(ntfy, 3000);
    3.20 -	notify_notification_set_urgency(ntfy, critical > 0 ? NOTIFY_URGENCY_CRITICAL : NOTIFY_URGENCY_NORMAL);
    3.21 +	notify_notification_set_urgency(ntfy, info->critical > 0 ? NOTIFY_URGENCY_CRITICAL : NOTIFY_URGENCY_NORMAL);
    3.22  	notify_notification_show(ntfy, NULL);
    3.23  }
    3.24 +
    3.25 +const char *notify_text(struct UpdatesInfo *info)
    3.26 +{
    3.27 +	static char buf[128];
    3.28 +	if (info->critical > 0) {
    3.29 +		snprintf(buf, sizeof(buf), "There are %d software updates available, %d of them critical.", info->normal + info->critical, info->critical);
    3.30 +	} else {
    3.31 +		snprintf(buf, sizeof(buf), "There are %d software updates available.", info->normal);
    3.32 +	}
    3.33 +	return buf;
    3.34 +}
     4.1 --- a/notify.h	Tue Sep 27 02:31:31 2011 +0200
     4.2 +++ b/notify.h	Tue Sep 27 12:15:39 2011 +0200
     4.3 @@ -21,7 +21,10 @@
     4.4  #ifndef NOTIFY_H
     4.5  #define NOTIFY_H
     4.6  
     4.7 +#include "main.h"
     4.8 +
     4.9  void init_notify();
    4.10 -void send_notify(int normal, int critical);
    4.11 +void send_notify(struct UpdatesInfo *info);
    4.12 +const char *notify_text(struct UpdatesInfo *info);
    4.13  
    4.14  #endif
     5.1 --- a/packagekit.h	Tue Sep 27 02:31:31 2011 +0200
     5.2 +++ b/packagekit.h	Tue Sep 27 12:15:39 2011 +0200
     5.3 @@ -21,10 +21,7 @@
     5.4  #ifndef PACKAGEKIT_H
     5.5  #define PACKAGEKIT_H 1
     5.6  
     5.7 -struct UpdatesInfo {
     5.8 -	int normal;
     5.9 -	int critical;
    5.10 -};
    5.11 +#include "main.h"
    5.12  
    5.13  void query_packagekit(struct UpdatesInfo *info);
    5.14