view main.c @ 0:f5e03fc667f8

initial commit
author Pavol Rusnak <stick@gk2.sk>
date Mon, 26 Sep 2011 18:52:04 +0200
parents
children 483f5fe9d2b2
line wrap: on
line source

#include "notify.h"
#include "packagekit.h"
#include <gtk/gtk.h>

struct updates_info {
	int normal;
	int critical;
} UpdatesInfo;

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)
{
	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()
{
	GtkStatusIcon *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");
	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);
	return FALSE;
}

int main(int argc, char **argv)
{
	GtkStatusIcon *tray_icon;

	gtk_init(&argc, &argv);
	tray_icon = create_tray_icon();
	init_notify();

	g_timeout_add(3000, periodic_check, NULL);

	gtk_main();

	return 0;
}