view main.c @ 9:0e50d3652326

use libunique to ensure that only a single instance of pk-update-icon can be run
author Guido Berhoerster <guido@berhoerster.name>
date Tue, 11 Oct 2011 17:07:49 +0200
parents 58a3312a1c59
children 64f05992d8ec
line wrap: on
line source

/*
 * 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
 *
 * 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 <locale.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <unique/unique.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(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;

	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");
		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);

	gtk_main();

	g_object_unref(tray_icon);

out:
	g_object_unref(app);

	return exitval;
}