-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddytime.c
314 lines (256 loc) · 11.3 KB
/
buddytime.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Buddy's Time plugin v3
*
* Copyright (C) 2012, Jon Manning <[email protected]>
* and Alexander Rapp <[email protected]>
*
* 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 <gdk/gdk.h>
#include <gtk/gtkplug.h>
#include <time.h>
#include <config.h>
#include <debug.h>
#include <gtkplugin.h>
#include <gtkconv.h>
#include <gtkdialogs.h>
#include <gtkprefs.h>
#include <blist.h>
#include <gtkblist.h>
#include <request.h>
#include <signals.h>
#include <util.h>
#include <version.h>
#include <internal.h>
#include <gtk/gtkstock.h>
#include <conversation.h>
#include "plugin.h"
guint timeout_handle; // the handle to the update-time timeout
static char* buddytime_get_localtime(const char* buddytimezone) {
// returns a string containing the adjusted time
time_t current_time = time(NULL);
char* env_tz = getenv("TZ");
struct tm *buddy_time;
char *printedtime = malloc(32);;
setenv("TZ",buddytimezone,1);
buddy_time = localtime(¤t_time);
if (env_tz) {
setenv("TZ",env_tz,1);
} else {
unsetenv("TZ");
}
strftime (printedtime, 32, "%H:%M", buddy_time);
return printedtime;
}
static void buddytime_add_time(PurpleConversation *conv)
{
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
PidginWindow *convwin = pidgin_conv_get_window(gtkconv);
GtkWidget* remote_time;
if (g_hash_table_lookup(conv->data, "buddytime-indicator")) {
// the widget's already in this conversation! don't add a new one
return;
}
remote_time = gtk_menu_item_new_with_label ("");
//gtk_widget_set_sensitive(remote_time, FALSE);
if(remote_time) {
purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Adding remote clock.\n");
gtk_menu_item_set_right_justified(
GTK_MENU_ITEM(remote_time), TRUE);
gtk_widget_show_all(remote_time);
gtk_menu_shell_append(GTK_MENU_SHELL(convwin->menu.menubar),
remote_time);
g_hash_table_insert(conv->data, g_strdup("buddytime-indicator"), remote_time);
}
}
static void buddytime_remove_time(PurpleConversation *conv)
{
GtkWidget* time_widget;
if ((time_widget = g_hash_table_lookup(conv->data, "buddytime-indicator"))) {
purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Removing remote clock.\n");
gtk_widget_destroy(time_widget);
//free(time_widget);
// throw this in in case the conversation window isn't actually closing
// and it's just the plugin being unloaded
g_hash_table_remove(conv->data, "buddytime-indicator");
}
}
static void buddytime_remove_time_from_gtkconv(PidginConversation* gtkconv) {
g_list_foreach(gtkconv->convs, (GFunc)buddytime_remove_time, NULL);
}
static void buddytime_remove_time_from_window(PidginWindow* window, gpointer data) {
GList* gtkconv_list = pidgin_conv_window_get_gtkconvs(window);
purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Removing clock from window %p\n", window);
g_list_foreach(gtkconv_list, (GFunc)buddytime_remove_time_from_gtkconv, NULL);
}
static void buddytime_newconv_cb(PurpleConversation *conv, gpointer data) {
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
PidginWindow *convwin = pidgin_conv_get_window(gtkconv);
purple_debug(PURPLE_DEBUG_MISC, "buddytime", "New conversation\n");
if ((conv != NULL) && (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM )) {
purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Adding remote clock.\n");
buddytime_remove_time_from_window(convwin, NULL);
buddytime_add_time(conv);
} else {
purple_debug(PURPLE_DEBUG_ERROR, "buddytime", "New conversation IS NULL\n");
}
}
static void buddytime_update_clock(PurpleConversation* conv) {
PurpleAccount* acct = purple_conversation_get_account(conv);
const char* name = purple_conversation_get_name(conv);
PurpleBuddy* buddy = purple_find_buddy(acct, name);
PurpleBlistNode* node = &(buddy->node);
const char* remote_timezone = purple_blist_node_get_string (node, "timezone");
GtkWidget* time_widget;
GtkWidget* time_label;
char* theirtime;
//purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Updating conversation, this one is with %s\n",
//name);
//purple_debug(PURPLE_DEBUG_MISC, "buddytime", "This buddy's timezone is %s\n",
//remote_timezone);
if (remote_timezone == NULL) {
// This buddy has no setting!
// just return, the label should remain blank
return;
}
time_widget = g_hash_table_lookup(conv->data, "buddytime-indicator");
if (time_widget == NULL) {
// whoa, no widget here, maybe the plugin got loaded after the window was created
// create the widget and re-get it
buddytime_newconv_cb(conv, NULL);
time_widget = g_hash_table_lookup(conv->data, "buddytime-indicator");
}
time_label = gtk_bin_get_child(GTK_BIN(time_widget));
theirtime = buddytime_get_localtime(remote_timezone);
if (theirtime == NULL) {
gtk_label_set_text(GTK_LABEL (time_label), "");
} else {
// Print it in nice grey
char *markup = g_markup_printf_escaped ("<span foreground=\"gray\"><b>%s</b></span>", theirtime);
gtk_label_set_markup (GTK_LABEL (time_label), markup);
g_free (markup);
free (theirtime);
}
}
static void buddytime_update_conversation_from_window(PidginWindow* window, gpointer data) {
//purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Updating window %p\n", window);
buddytime_update_clock(pidgin_conv_window_get_active_conversation(window));
}
static void buddytime_update_all_clocks(void) {
// update all the conversation windows
GList* window_list = pidgin_conv_windows_get_list();
//GTimeVal current_time;
//g_get_current_time(¤t_time);
//purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Updating all clocks (time is %i secs)\n", current_time.tv_sec);
g_list_foreach(window_list, (GFunc)buddytime_update_conversation_from_window, NULL);
}
static gboolean buddytime_update_time_cb(gpointer data1, gpointer data2) {
buddytime_update_all_clocks();
return TRUE;
}
static void buddytime_switch_conv_cb(PurpleConversation *conv, gpointer data) {
purple_debug_misc("buddytime", "Switching conversation\n");
// fix the time for the new clock
buddytime_update_clock(conv);
// hide the old widget and display the new
// buddytime_remove_time(conv);
// buddytime_add_time(conv);
}
static void buddytime_setzone_cb(PurpleBlistNode* node, const char* timezone) {
// set the timezone for the selected blist node
//purple_debug(PURPLE_DEBUG_MISC, "buddytime", "Setting timezone for %p to %s\n", node, timezone);
purple_blist_node_set_string (node, "timezone", timezone);
// Update the buddy's time immediately
buddytime_update_all_clocks();
}
static void buddytime_setzone_dialog(PurpleBlistNode* node, void* data) {
const char* current_timezone = purple_blist_node_get_string(node, "timezone");
purple_request_input(NULL, _("Set Buddy's Timezone"), NULL,
_("Please enter the timezone "
"of this buddy in the Unix Area/City "
"(e.g. America/New_York) format."), current_timezone, FALSE, FALSE, NULL,
_("Set Timezone"), G_CALLBACK(buddytime_setzone_cb),
_("Cancel"), NULL, NULL, NULL, NULL, node);
}
static void buddytime_menu_cb(PurpleBlistNode* node, GList **menu, void* data) {
PurpleMenuAction *action;
action = purple_menu_action_new(_("Set Timezone"), G_CALLBACK(buddytime_setzone_dialog), NULL, NULL);
*menu = g_list_append(*menu, action);
}
static gboolean plugin_load(PurplePlugin *plugin)
{
purple_debug(PURPLE_DEBUG_INFO, "buddytime", "buddytime plugin loaded.\n");
purple_signal_connect(purple_blist_get_handle(), "blist-node-extended-menu", plugin,
PURPLE_CALLBACK(buddytime_menu_cb), NULL);
purple_signal_connect(purple_conversations_get_handle(), "conversation-created", plugin,
PURPLE_CALLBACK(buddytime_newconv_cb), NULL);
purple_signal_connect(pidgin_conversations_get_handle(), "conversation-switched", plugin,
PURPLE_CALLBACK(buddytime_switch_conv_cb), NULL);
purple_signal_connect(purple_conversations_get_handle(), "deleting-conversation", plugin,
PURPLE_CALLBACK(buddytime_remove_time), NULL);
// update the time every second or so
timeout_handle = g_timeout_add(1000, (GSourceFunc)buddytime_update_time_cb, NULL);
// and update any open windows now
buddytime_update_all_clocks();
return TRUE;
}
static gboolean plugin_unload(PurplePlugin *plugin)
{
// remove the clocks from all windows
GList* window_list = pidgin_conv_windows_get_list();
g_list_foreach(window_list, (GFunc)buddytime_remove_time_from_window, NULL);
// disconnect, unhook and destroy everything
purple_signal_disconnect(purple_blist_get_handle(), "blist-node-extended-menu", plugin,
PURPLE_CALLBACK(buddytime_menu_cb));
purple_signal_disconnect(purple_conversations_get_handle(), "conversation-created", plugin,
PURPLE_CALLBACK(buddytime_newconv_cb));
purple_signal_disconnect(pidgin_conversations_get_handle(), "conversation-switched", plugin,
PURPLE_CALLBACK(buddytime_switch_conv_cb));
purple_signal_disconnect(purple_conversations_get_handle(), "deleting-conversation", plugin,
PURPLE_CALLBACK(buddytime_remove_time));
purple_debug(PURPLE_DEBUG_INFO, "buddytime", "buddytime plugin unloaded.\n");
g_source_remove(timeout_handle);
return TRUE;
}
static PurplePluginInfo info =
{
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD, /**< type */
NULL, /**< ui_requirement */
0, /**< flags */
NULL, /**< dependencies */
PURPLE_PRIORITY_DEFAULT, /**< priority */
"gtk-buddytime", /**< id */
"Buddy's Time", /**< name */
VERSION, /**< version */
"Keeps track of your contact's local time", /** summary */
"This plugin puts a small clock in your conversation window, showing you the local time of "
"whomever you're talking to.", /** description */
"Jon Manning <[email protected]> and Alexander Rapp <[email protected]>",
/**< author */
PURPLE_WEBSITE, /**< homepage */
plugin_load, /**< load */
plugin_unload, /**< unload */
NULL, /**< destroy */
NULL, /**< ui_info */
NULL, /**< extra_info */
NULL, NULL, NULL, NULL, NULL, NULL
};
static void init_plugin(PurplePlugin *plugin) {
}
PURPLE_INIT_PLUGIN(buddytime, init_plugin, info)