Mercurial > projects > pk-update-icon
changeset 3:f8ad23e60000
code rework, show notify window just once
author | Pavol Rusnak <stick@gk2.sk> |
---|---|
date | Tue, 27 Sep 2011 12:15:39 +0200 |
parents | 847ae02bc13b |
children | 9126094c5ef0 |
files | main.c main.h notify.c notify.h packagekit.h |
diffstat | 5 files changed, 35 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/main.c Tue Sep 27 02:31:31 2011 +0200 +++ b/main.c Tue Sep 27 12:15:39 2011 +0200 @@ -18,14 +18,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "main.h" #include "notify.h" #include "packagekit.h" #include <gtk/gtk.h> -struct updates_info { - int normal; - int critical; -} UpdatesInfo; +struct UpdatesInfo info; GtkStatusIcon *tray_icon; @@ -65,22 +63,19 @@ gtk_status_icon_set_from_icon_name(tray_icon, "system-software-update"); gtk_status_icon_set_title(tray_icon, "Software Update"); gtk_status_icon_set_visible(tray_icon, TRUE); - return tray_icon; } gboolean periodic_check(gpointer user_data) { - struct UpdatesInfo info; query_packagekit(&info); - send_notify(info.normal, info.critical); if (info.critical > 0) { gtk_status_icon_set_from_icon_name(tray_icon, "software-update-urgent"); - gtk_status_icon_set_tooltip_text(tray_icon, "Critical updates available."); + gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info)); } else if (info.normal > 0) { gtk_status_icon_set_from_icon_name(tray_icon, "software-update-available"); - gtk_status_icon_set_tooltip_text(tray_icon, "Updates available."); + gtk_status_icon_set_tooltip_text(tray_icon, notify_text(&info)); } return TRUE; } @@ -94,7 +89,9 @@ init_notify(); periodic_check(NULL); - g_timeout_add(15 * 60 * 1000, periodic_check, NULL); + send_notify(&info); + // update tray icon and tooltip every 2 hours + g_timeout_add_seconds(2*3600, periodic_check, NULL); gtk_main();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Tue Sep 27 12:15:39 2011 +0200 @@ -0,0 +1,9 @@ +#ifndef MAIN_H +#define MAIN_H 1 + +struct UpdatesInfo { + int normal; + int critical; +}; + +#endif
--- a/notify.c Tue Sep 27 02:31:31 2011 +0200 +++ b/notify.c Tue Sep 27 12:15:39 2011 +0200 @@ -28,17 +28,22 @@ notify_init(NOTIFY_NAME); } -void send_notify(int normal, int critical) +void send_notify(struct UpdatesInfo *info) { NotifyNotification *ntfy; - char buf[128]; - if (critical > 0) { - snprintf(buf, sizeof(buf), "There are %d software updates available, %d of them critical.", normal + critical, critical); - } else { - snprintf(buf, sizeof(buf), "There are %d software updates available.", normal); - } - ntfy = notify_notification_new(NOTIFY_NAME, buf, critical > 0 ? "software-update-urgent" : "software-update-available"); + ntfy = notify_notification_new(NOTIFY_NAME, notify_text(info), info->critical > 0 ? "software-update-urgent" : "software-update-available"); notify_notification_set_timeout(ntfy, 3000); - notify_notification_set_urgency(ntfy, critical > 0 ? NOTIFY_URGENCY_CRITICAL : NOTIFY_URGENCY_NORMAL); + notify_notification_set_urgency(ntfy, info->critical > 0 ? NOTIFY_URGENCY_CRITICAL : NOTIFY_URGENCY_NORMAL); notify_notification_show(ntfy, NULL); } + +const char *notify_text(struct UpdatesInfo *info) +{ + static char buf[128]; + if (info->critical > 0) { + snprintf(buf, sizeof(buf), "There are %d software updates available, %d of them critical.", info->normal + info->critical, info->critical); + } else { + snprintf(buf, sizeof(buf), "There are %d software updates available.", info->normal); + } + return buf; +}
--- a/notify.h Tue Sep 27 02:31:31 2011 +0200 +++ b/notify.h Tue Sep 27 12:15:39 2011 +0200 @@ -21,7 +21,10 @@ #ifndef NOTIFY_H #define NOTIFY_H +#include "main.h" + void init_notify(); -void send_notify(int normal, int critical); +void send_notify(struct UpdatesInfo *info); +const char *notify_text(struct UpdatesInfo *info); #endif