Mercurial > projects > pk-update-icon
diff main.c @ 14:64f05992d8ec
GObject-based rewrite
use asynchronous packagekit-glib API
use persistent menu widget and notification object
update existing notification when new updates become available, close it when no updates are available
show status icon when updates are available, hide it when no updates are available
hide icon when gpk-update-viewer is executed, check for updates when gpk-update-viewer exits
introduce a startup delay before the first check for updates is made
add context menu item to manually trigger a check for updates
remove context menu item for quitting pk-update-icon
author | Guido Berhoerster <guido@berhoerster.name> |
---|---|
date | Thu, 20 Oct 2011 08:19:22 +0200 |
parents | 0e50d3652326 |
children | 7af115023d5a |
line wrap: on
line diff
--- a/main.c Tue Oct 11 17:36:32 2011 +0200 +++ b/main.c Thu Oct 20 08:19:22 2011 +0200 @@ -1,5 +1,4 @@ /* - * Copyright (C) 2011 Pavol Rusnak <stick@gk2.sk> * Copyright (C) 2011 Guido Berhoerster <gber@opensuse.org> * * Licensed under the GNU General Public License Version 2 @@ -19,103 +18,47 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "main.h" -#include "notify.h" -#include "packagekit.h" #include <locale.h> #include <glib/gi18n.h> #include <gtk/gtk.h> #include <unique/unique.h> - -struct UpdatesInfo info; - -GtkStatusIcon *tray_icon; +#include <libnotify/notify.h> +#include "pkui-icon.h" -void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data) -{ - char *argv[2] = { "gpk-update-viewer", NULL }; - g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); -} - -static void tray_menu_destroy(GtkWidget *menu, gpointer userdata) -{ - gtk_widget_destroy(menu); - g_object_unref(menu); -} - -void tray_icon_on_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data) +int +main(int argc, char **argv) { - GtkWidget *item; - GtkWidget *menu = gtk_menu_new(); - item = gtk_menu_item_new_with_mnemonic(_("_Quit")); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_main_quit), user_data); - gtk_widget_show(item); - g_object_ref(menu); - g_object_ref_sink(menu); - g_object_unref(menu); - g_signal_connect(G_OBJECT(menu), "selection-done", G_CALLBACK(tray_menu_destroy), NULL); - gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, user_data, button, activate_time); - gtk_widget_show_all(GTK_WIDGET(menu)); -} - -static GtkStatusIcon *create_tray_icon() -{ - tray_icon = gtk_status_icon_new(); - g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(tray_icon_on_click), NULL); - g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_icon_on_menu), NULL); - gtk_status_icon_set_from_icon_name(tray_icon, "system-software-update"); - gtk_status_icon_set_title(tray_icon, _("Software Update(s)")); - gtk_status_icon_set_visible(tray_icon, TRUE); - return tray_icon; -} - -gboolean periodic_check(gpointer user_data) -{ - query_packagekit(&info); - if (info.critical > 0) { - gtk_status_icon_set_from_icon_name(tray_icon, "software-update-urgent"); - 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, notify_text(&info)); - } - return TRUE; -} - -int main(int argc, char **argv) -{ - GtkStatusIcon *tray_icon; - UniqueApp *app; - int exitval = 0; + PkuiIcon *icon; + UniqueApp *app; + int exitval = 0; setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); bind_textdomain_codeset(PACKAGE, "UTF-8"); textdomain(PACKAGE); gtk_init(&argc, &argv); + app = unique_app_new(APP_NAME, NULL); if (unique_app_is_running(app)) { - g_printerr("Another instance of pk-update-icon is already running. Exiting.\n"); + g_printerr("Another instance of pk-update-icon is already " + "running. Exiting.\n"); exitval = 1; goto out; } - tray_icon = create_tray_icon(); - init_notify(); - periodic_check(NULL); - send_notify(&info); - // update tray icon and tooltip every 2 hours - g_timeout_add_seconds(2*3600, periodic_check, NULL); + notify_init(PACKAGE); + + icon = pkui_icon_new(10, 2 * 3600); gtk_main(); - g_object_unref(tray_icon); - + g_object_unref(icon); out: g_object_unref(app); + if (notify_is_initted()) + notify_uninit (); - return exitval; + return (exitval); }