aboutsummaryrefslogtreecommitdiff
path: root/src/krb5-auth-applet.c
blob: 4f2215da7e11be9337ff0977c2d1cb2f32eab655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Krb5 Auth Applet -- Acquire and release kerberos tickets
 *
 * (C) 2008 Guido Guenther <agx@sigxcpu.org>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <glib/gi18n.h>

#include "krb5-auth-applet.h"
#include "krb5-auth-dialog.h"
#ifdef HAVE_LIBNOTIFY
#include "krb5-auth-notify.h"
#endif

#define NOTIFY_SECONDS 300

enum ka_icons {
	inv_icon = 0,
	exp_icon,
	val_icon,
};

/* update the tray icon's tooltip and icon */
int
ka_update_status(Krb5AuthApplet* applet, krb5_timestamp expiry)
{
	gchar* expiry_text;
	int now = time(0);
	int remaining = expiry - now;
	static int last_warn = 0;
	static gboolean expiry_notified = FALSE;

	if (remaining > 0) {
		int hours, minutes;
		if (remaining >= 3600) {
			hours = remaining / 3600;
			minutes = (remaining % 3600) / 60;
			expiry_text = g_strdup_printf (_("Your credentials expire in %.2d:%.2dh"), hours, minutes);
			gtk_status_icon_set_from_icon_name (applet->tray_icon, applet->icons[val_icon]);
		} else {
			minutes = remaining / 60;
			expiry_text = g_strdup_printf (ngettext(
							"Your credentials expire in %d minute",
							"Your credentials expire in %d minutes",
							minutes), minutes);
			gtk_status_icon_set_from_icon_name (applet->tray_icon, applet->icons[exp_icon]);
		}
#ifdef HAVE_LIBNOTIFY
		if (expiry_notified) {
			ka_send_event_notification (applet, NOTIFY_URGENCY_NORMAL,
						_("Network credentials valid"),
						_("Your Kerberos credentials have been refreshed."), NULL);
			expiry_notified = FALSE;
		} else if (remaining < applet->pw_prompt_secs && (now - last_warn) > NOTIFY_SECONDS &&
		           !applet->renewable) {
			ka_send_event_notification (applet, NOTIFY_URGENCY_NORMAL,
						_("Network credentials expiring"),
						expiry_text, NULL);
			last_warn = now;
		}
#endif
	} else {
		expiry_text = g_strdup (_("Your credentials have expired"));
		gtk_status_icon_set_from_icon_name (applet->tray_icon, applet->icons[inv_icon]);
#ifdef HAVE_LIBNOTIFY
		if (!expiry_notified) {
			ka_send_event_notification (applet, NOTIFY_URGENCY_NORMAL,
						_("Network credentials expired"),
						_("Your Kerberos credentails have expired."), NULL);
			expiry_notified = TRUE;
			last_warn = 0;
		}
#endif
	}

	gtk_status_icon_set_tooltip (applet->tray_icon, expiry_text);
	g_free (expiry_text);
	return 0;
}


static void
ka_menu_add_separator_item (GtkWidget* menu)
{
        GtkWidget* menu_item;

        menu_item = gtk_separator_menu_item_new ();
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
        gtk_widget_show (menu_item);
}


/* Free all resources and quit */
static void
ka_quit_applet (GtkMenuItem* menuitem, gpointer user_data)
{
	Krb5AuthApplet* applet = (Krb5AuthApplet*) user_data;

	g_free (applet->principal);
	g_free (applet);
	gtk_main_quit ();
}


static void
ka_about_dialog (GtkMenuItem* menuitem, gpointer user_data)
{
	gchar* authors[] = {  "Christopher Aillon <caillon@redhat.com>",
			      "Colin Walters <walters@verbum.org>",
		              "Guido G√ľnther <agx@sigxpcu.org>",
			      NULL };
	gtk_show_about_dialog (NULL,
                              "authors", authors,
                              "version", VERSION,
                              "copyright", "Copyright (C) 2004,2005,2006 Red Hat, Inc.,\n2008 Guido G√ľnther",
                               NULL);
}


/* The tray icon's context menu */
static GtkWidget*
ka_create_context_menu (Krb5AuthApplet* applet)
{
	GtkWidget* menu;
	GtkWidget* menu_item;
	GtkWidget* image;

	menu = gtk_menu_new ();

	/* kdestroy */
	menu_item = gtk_image_menu_item_new_with_mnemonic (_("Remove Credentials _Cache"));
	g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK (ka_destroy_cache), applet);
        image = gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_MENU);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	ka_menu_add_separator_item (menu);

	/* About item */
	menu_item = gtk_image_menu_item_new_with_mnemonic (_("_About"));
	g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK (ka_about_dialog), applet);
	image = gtk_image_new_from_stock (GTK_STOCK_ABOUT, GTK_ICON_SIZE_MENU);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	ka_menu_add_separator_item (menu);

	/* Quit */
	menu_item = gtk_image_menu_item_new_with_mnemonic (_("_Quit"));
	g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK (ka_quit_applet), applet);
        image = gtk_image_new_from_stock (GTK_STOCK_QUIT, GTK_ICON_SIZE_MENU);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	gtk_widget_show_all (menu);

	return menu;
}


static void
ka_tray_icon_on_menu (GtkStatusIcon* status_icon, guint button,
                       guint activate_time, gpointer user_data)
{
	Krb5AuthApplet* applet = (Krb5AuthApplet*) user_data;

	KA_DEBUG("Trayicon right clicked: %d", applet->pw_prompt_secs);
	gtk_menu_popup (GTK_MENU (applet->context_menu), NULL, NULL,
	                gtk_status_icon_position_menu, applet->tray_icon,
			button, activate_time);
}


static gboolean
ka_tray_icon_on_click (GtkStatusIcon* status_icon, gpointer data)
{
	Krb5AuthApplet* applet = (Krb5AuthApplet*) data;
	g_return_val_if_fail (applet != NULL, FALSE);

	KA_DEBUG("Trayicon clicked: %d", applet->pw_prompt_secs);
	ka_grab_credentials (applet);
	return TRUE;
}


gboolean
ka_show_tray_icon (Krb5AuthApplet* applet)
{
	g_return_val_if_fail (applet != NULL, FALSE);
	g_return_val_if_fail (applet->tray_icon != NULL, FALSE);

	gtk_status_icon_set_visible (applet->tray_icon, applet->show_trayicon);
	return TRUE;
}


static GtkStatusIcon*
ka_create_tray_icon (Krb5AuthApplet* applet)
{
	GtkStatusIcon* tray_icon;

	tray_icon = gtk_status_icon_new ();
	g_signal_connect (G_OBJECT(tray_icon), "activate",
                          G_CALLBACK(ka_tray_icon_on_click), applet);
	g_signal_connect (G_OBJECT(tray_icon),
			  "popup-menu",
			  G_CALLBACK(ka_tray_icon_on_menu), applet);
        gtk_status_icon_set_from_icon_name (tray_icon, applet->icons[exp_icon]);
        gtk_status_icon_set_tooltip (tray_icon, PACKAGE);
        return tray_icon;
}


int
ka_setup_icons (Krb5AuthApplet* applet)
{
	applet->icons[val_icon] = "krb-valid-ticket";
	applet->icons[exp_icon] = "krb-expiring-ticket";
	applet->icons[inv_icon] = "krb-no-valid-ticket";
	return TRUE;
}


/* create the tray icon applet */
Krb5AuthApplet*
ka_create_applet()
{
	Krb5AuthApplet* applet = g_malloc0 (sizeof(Krb5AuthApplet));

	if (!(ka_setup_icons (applet)))
		g_error ("Failure to setup icons");
	if (!(applet->tray_icon = ka_create_tray_icon (applet)))
		g_error ("Failure to create tray icon");
	if (!(applet->context_menu = ka_create_context_menu (applet)))
		g_error ("Failure to create context menu");
	gtk_window_set_default_icon_name (applet->icons[val_icon]);
	ka_show_tray_icon (applet);

	return applet;
}