view xia-icon.c @ 15:3457a401e685

Add NEWS file
author Guido Berhoerster <guido+xinhibit-applet@berhoerster.name>
date Fri, 23 Jan 2015 10:58:04 +0100
parents 14f31e92372f
children
line wrap: on
line source

/*
 * Copyright (C) 2013 Guido Berhoerster <guido+xinhibit-applet@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <glib/gi18n.h>
#include "xia-debug.h"
#include "xia-inhibitor.h"
#include "xia-icon.h"

G_DEFINE_TYPE(XiaIcon, xia_icon, G_TYPE_OBJECT)

#define	XIA_ICON_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE((obj),\
    XIA_TYPE_ICON, XiaIconPrivate))

struct	_XiaIconPrivate {
	XiaInhibitor	*inhibitor;

	GtkStatusIcon	*status_icon;
	GtkWidget	*status_icon_popup_menu;
	GtkWidget	*check_menu_item_inhibited;
};

static void	icon_update(XiaIcon *self);
static GtkWidget * icon_popup_menu_create(XiaIcon *self);
static void	icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button,
    guint activate_time, gpointer user_data);
static void	icon_activate(GtkStatusIcon *status_icon, gpointer user_data);

static void
xia_icon_finalize(GObject *gobject)
{
	XiaIcon	*self = XIA_ICON(gobject);

	gtk_widget_destroy(self->priv->status_icon_popup_menu);
	g_object_unref(self->priv->status_icon_popup_menu);
	g_object_unref(self->priv->status_icon);
	g_object_unref(self->priv->inhibitor);

	G_OBJECT_CLASS(xia_icon_parent_class)->finalize(gobject);
}

static void
xia_icon_class_init(XiaIconClass *klass)
{
	GObjectClass	*gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->finalize = xia_icon_finalize;

	g_type_class_add_private(klass, sizeof (XiaIconPrivate));
}

static void
xia_icon_init(XiaIcon *self)
{
	self->priv = XIA_ICON_GET_PRIVATE(self);

	self->priv->inhibitor = xia_inhibitor_new();

	self->priv->status_icon = gtk_status_icon_new();
	gtk_status_icon_set_title(self->priv->status_icon,
	    _("Inhibit automatic power management"));

	self->priv->status_icon_popup_menu = icon_popup_menu_create(self);
	g_object_ref(self->priv->status_icon_popup_menu);
	g_object_ref_sink(GTK_OBJECT(self->priv->status_icon_popup_menu));

	g_signal_connect(G_OBJECT(self->priv->status_icon), "activate",
	    G_CALLBACK(icon_activate), self);
	g_signal_connect(G_OBJECT(self->priv->status_icon), "popup-menu",
	    G_CALLBACK(icon_popup_menu_popup), self);

	icon_update(self);
}

static void
about_dialog_show(GtkMenuItem *item, gpointer user_data)
{
	const gchar *copyright = "Copyright \302\251 2013 Guido Berhoerster\n";
	const gchar *comments = _("Allows users to inhibit automatic power "
	    "management\n\nTo Ai\n");
	const gchar *logo_icon_name = "xinhibit-applet";
	const gchar *authors[2] = {
	    "Guido Berhoerster <guido+xinhibit-applet@berhoerster.name>",
	    NULL
	};
	const gchar *license =
	    "Copyright \302\251 2013 Guido Berhoerster\n\n"
	    "Permission is hereby granted, free of charge, to any person "
	    "obtaining a copy of this software and associated documentation "
	    "files (the \"Software\"), to deal in the Software without "
	    "restriction, including without limitation the rights to use, "
	    "copy, modify, merge, publish, distribute, sublicense, and/or sell "
	    "copies of the Software, and to permit persons to whom the "
	    "Software is furnished to do so, subject to the following "
	    "conditions:\n\n"
	    "The above copyright notice and this permission notice shall be "
	    "included in all copies or substantial portions of the Software."
	    "\n\n"
	    "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
	    "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES "
	    "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
	    "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT "
	    "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, "
	    "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING "
	    "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR "
	    "OTHER DEALINGS IN THE SOFTWARE.";
	const gchar *translators = _("translators");

	if (strcmp(translators, "translators") == 0) {
	    translators = NULL;
	}

	gtk_show_about_dialog(NULL, "version", VERSION, "copyright", copyright,
	    "logo-icon-name", logo_icon_name, "comments", comments, "authors",
	    authors, "translator-credits", translators, "license", license,
	    "wrap-license", TRUE, NULL);
}

static void
icon_update(XiaIcon *self)
{
	gboolean	inhibited;

	inhibited = xia_inhibitor_get_inhibited(self->priv->inhibitor);

	g_debug("Updating icon, inhibit: %s", (inhibited) ? "on" : "off");

	gtk_status_icon_set_tooltip_text(self->priv->status_icon, (inhibited) ?
	    _("Enable Automatic Power Management") :
	    _("Inhibit Automatic Power Management"));
	gtk_status_icon_set_from_icon_name(self->priv->status_icon,
	    (inhibited) ? "xinhibit-applet-inhibited" :
	    "xinhibit-applet-uninhibited");
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
	    self->priv->check_menu_item_inhibited), inhibited);
}

static void
icon_activate(GtkStatusIcon *status_icon, gpointer user_data)
{
	XiaIcon	*self = XIA_ICON(user_data);
	gboolean inhibited = xia_inhibitor_get_inhibited(self->priv->inhibitor);

	g_debug("Icon activated, inhibit: %s", (!inhibited) ? "on" : "off");

	xia_inhibitor_set_inhibited(self->priv->inhibitor, !inhibited);
	icon_update(self);
}

static void
check_menu_item_inhibit_activate(GtkCheckMenuItem *menu_item,
    gpointer user_data)
{
	XiaIcon		*self = XIA_ICON(user_data);
	gboolean	inhibited = gtk_check_menu_item_get_active(menu_item);

	g_debug("Inhibit menu item activated, inhibit: %s", (inhibited) ? "on" :
	    "off");

	if (xia_inhibitor_get_inhibited(self->priv->inhibitor) != inhibited) {
		xia_inhibitor_set_inhibited(self->priv->inhibitor, inhibited);
		icon_update(self);
	}
}

static GtkWidget*
icon_popup_menu_create(XiaIcon *self)
{
	GtkWidget	*popup_menu = gtk_menu_new();
	GtkWidget	*item;
	GtkWidget	*image;

	self->priv->check_menu_item_inhibited = \
	    gtk_check_menu_item_new_with_mnemonic(_("_Inhibit Automatic Power "
	    "Management"));
	gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu),
	    self->priv->check_menu_item_inhibited);
	g_signal_connect(G_OBJECT(self->priv->check_menu_item_inhibited),
	    "activate", G_CALLBACK(check_menu_item_inhibit_activate), self);

	item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
	image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
	    GTK_ICON_SIZE_MENU);
	gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
	gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), item);
	g_signal_connect(G_OBJECT(item), "activate",
	    G_CALLBACK(about_dialog_show), self);

	gtk_widget_show_all(GTK_WIDGET(popup_menu));

	return (popup_menu);
}

static void
icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button,
    guint activate_time, gpointer user_data)
{
	XiaIcon *self = XIA_ICON(user_data);

	gtk_menu_popup(GTK_MENU(self->priv->status_icon_popup_menu), NULL, NULL,
	    NULL, NULL, button, activate_time);
}

XiaIcon *
xia_icon_new(void)
{
	return (g_object_new(XIA_TYPE_ICON, NULL));
}