view main.c @ 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 58a3312a1c59
line wrap: on
line source

/*
 * Copyright (C) 2011 Pavol Rusnak <stick@gk2.sk>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * 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 UpdatesInfo info;

GtkStatusIcon *tray_icon;

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()
{
	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)
{
	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;

	gtk_init(&argc, &argv);
	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);

	gtk_main();

	return 0;
}