~inspirated/pidgin-countdown/krkhan

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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#define PURPLE_PLUGINS

#include <glib.h>
#include <gtk/gtk.h>
#include <string.h>

#include "prefs.h"
#include "notify.h"
#include "plugin.h"
#include "version.h"
#include "savedstatuses.h"
#include "prefs.h"
#include "pluginpref.h"
#include "gtkplugin.h"

static gboolean set_status(gpointer data);
void reset_timeout();

PurplePlugin *thisplugin;
int timeout;

static gboolean
plugin_load(PurplePlugin *plugin)
{
    static int handle;
    thisplugin = plugin;
    timeout = 0;
    // Load or create preferences
    purple_prefs_add_none("/plugins/gtk/countdown");
    purple_prefs_add_int("/plugins/gtk/countdown/year", 2009);
    purple_prefs_add_int("/plugins/gtk/countdown/month", 11);
    purple_prefs_add_int("/plugins/gtk/countdown/day", 25);
    purple_prefs_add_int("/plugins/gtk/countdown/hour", 0);
    purple_prefs_add_int("/plugins/gtk/countdown/min", 0);
    purple_prefs_add_int("/plugins/gtk/countdown/sec", 0);
    purple_prefs_add_int("/plugins/gtk/countdown/timeout", 10);
    purple_prefs_add_bool("/plugins/gtk/countdown/utc", FALSE);
    purple_prefs_add_string("/plugins/gtk/countdown/append", ".");

    reset_timeout();

    purple_debug_info("countdown", "Countdown plugin initialised ...\n");
    return TRUE;
}

static gboolean
plugin_unload(PurplePlugin *plugin)
{
    if(timeout != 0)
      purple_timeout_remove(timeout);
    return TRUE;
}

void reset_timeout()
{
    if(timeout != 0)
        purple_timeout_remove(timeout);

    // Setup timeout for refreshing info
    timeout = purple_timeout_add_seconds(purple_prefs_get_int("/plugins/gtk/countdown/timeout"),
                                         set_status,
                                         thisplugin);
    set_status(thisplugin);
}

static gboolean
set_status(gpointer data)
{
    PurplePlugin* plugin = (PurplePlugin*) data;
    PurpleSavedStatus* status;

    struct tm target;
    time_t target_time;
    int difference;

    target.tm_year = purple_prefs_get_int("/plugins/gtk/countdown/year") - 1900;
    target.tm_mon = purple_prefs_get_int("/plugins/gtk/countdown/month");
    target.tm_mday = purple_prefs_get_int("/plugins/gtk/countdown/day");
    target.tm_hour = purple_prefs_get_int("/plugins/gtk/countdown/hour");
    target.tm_min = purple_prefs_get_int("/plugins/gtk/countdown/min");
    target.tm_sec = purple_prefs_get_int("/plugins/gtk/countdown/sec");

    gboolean utc = purple_prefs_get_bool("/plugins/gtk/countdown/utc");
    const gchar *append = purple_prefs_get_string("/plugins/gtk/countdown/append");

    if(utc == TRUE) {
        target_time = timegm(&target);
    } else {
        target_time = timelocal(&target);
    }

    difference = (int) difftime(target_time, time(NULL));

    int days = difference / (24*60*60);
    int hours = (difference - days*24*60*60) / (60*60);
    int minutes = (difference - days*24*60*60 - hours*60*60) / 60;
    int seconds = difference - days*24*60*60 - hours*60*60 - minutes*60;

    char *msg;
    asprintf(&msg,
            "%d days, %d hours %d minutes and %d seconds%s",
            days, hours, minutes, seconds, append);

    status = purple_savedstatus_get_current();
    purple_savedstatus_set_message(status, msg);
    purple_savedstatus_activate(status);

    free(msg);

    return TRUE;
}

static void calendar_changed(GtkWidget *calendar, gpointer data)
{
    guint year, month, day;
    gtk_calendar_get_date(GTK_CALENDAR(calendar), &year, &month, &day);
    purple_prefs_set_int("/plugins/gtk/countdown/year", year);
    purple_prefs_set_int("/plugins/gtk/countdown/month", month);
    purple_prefs_set_int("/plugins/gtk/countdown/day", day);
    purple_debug_info("countdown", "Target date changed\n");
    set_status(thisplugin);
}

static void hour_changed(GtkWidget *spin, gpointer data)
{
    guint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
    purple_prefs_set_int("/plugins/gtk/countdown/hour", value);
    purple_debug_info("countdown", "Target hour changed\n");
    set_status(thisplugin);
}

static void min_changed(GtkWidget *spin, gpointer data)
{
    guint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
    purple_prefs_set_int("/plugins/gtk/countdown/min", value);
    purple_debug_info("countdown", "Target minute changed\n");
    set_status(thisplugin);
}

static void sec_changed(GtkWidget *spin, gpointer data)
{
    guint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
    purple_prefs_set_int("/plugins/gtk/countdown/sec", value);
    purple_debug_info("countdown", "Target seconds changed\n");
    set_status(thisplugin);
}

static void timeout_changed(GtkWidget *spin, gpointer data)
{
    guint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
    purple_prefs_set_int("/plugins/gtk/countdown/timeout", value);
    purple_debug_info("countdown", "Timeout changed\n");
    reset_timeout();
}

static void utc_changed(GtkWidget *check, gpointer data)
{
   gboolean value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
   purple_prefs_set_bool("/plugins/gtk/countdown/utc", value);
   purple_debug_info("countdown", "UTC preference changed\n");
   set_status(thisplugin);
}

static gboolean append_changed(GtkWidget *entry, GdkEventFocus *event, gpointer data)
{
   const gchar *value = gtk_entry_get_text(GTK_ENTRY(entry));
   purple_prefs_set_string("/plugins/gtk/countdown/append", value);
   purple_debug_info("countdown", "Append string changed\n");
   set_status(thisplugin);
   return FALSE;
}

static GtkWidget *get_config_frame(PurplePlugin *plugin)
{
    GtkWidget *table = gtk_table_new(5, 2, FALSE);
    GtkWidget *labeldate = gtk_label_new("Date");
    GtkWidget *labeltime = gtk_label_new("Time");
    GtkWidget *labelcolon1 = gtk_label_new(":");
    GtkWidget *labelcolon2 = gtk_label_new(":");
    GtkWidget *labeltimeout = gtk_label_new("Timeout");
    GtkWidget *labelseconds = gtk_label_new("seconds");
    GtkWidget *labelutc = gtk_label_new("UTC");
    GtkWidget *labelappend = gtk_label_new("Append");
    GtkWidget *calendar = gtk_calendar_new();
    GtkWidget *timebox = gtk_hbox_new(FALSE, 5);
    GtkWidget *timeoutbox = gtk_hbox_new(FALSE, 5);
    GtkWidget *spinhour = gtk_spin_button_new_with_range(0, 23, 1);
    GtkWidget *spinmin = gtk_spin_button_new_with_range(0, 59, 1);
    GtkWidget *spinsec = gtk_spin_button_new_with_range(0, 59, 1);
    GtkWidget *spintimeout = gtk_spin_button_new_with_range(10, 1000, 1);
    GtkWidget *checkutc = gtk_check_button_new();
    GtkWidget *entryappend = gtk_entry_new();

    guint year = purple_prefs_get_int("/plugins/gtk/countdown/year");
    guint month = purple_prefs_get_int("/plugins/gtk/countdown/month");
    guint day = purple_prefs_get_int("/plugins/gtk/countdown/day");
    guint hour = purple_prefs_get_int("/plugins/gtk/countdown/hour");
    guint min = purple_prefs_get_int("/plugins/gtk/countdown/min");
    guint sec = purple_prefs_get_int("/plugins/gtk/countdown/sec");
    guint timeoutval = purple_prefs_get_int("/plugins/gtk/countdown/timeout");
    // named "timeoutval" instead of "timeout" in order to not conflict with
    // the global timeout handle
    gboolean utc = purple_prefs_get_bool("/plugins/gtk/countdown/utc");
    const gchar *append = purple_prefs_get_string("/plugins/gtk/countdown/append");

    gtk_calendar_select_month(GTK_CALENDAR(calendar), month, year);
    gtk_calendar_select_day(GTK_CALENDAR(calendar), day);

    gtk_container_set_border_width(GTK_CONTAINER(table), 5);
    gtk_table_set_row_spacings(GTK_TABLE(table), 5);
    gtk_table_set_col_spacings(GTK_TABLE(table), 5);
    gtk_table_attach_defaults(GTK_TABLE(table), labeldate, 0, 1, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(table), calendar, 1, 2, 0, 1);

    gtk_entry_set_text(GTK_ENTRY(entryappend), append);

    gtk_table_attach_defaults(GTK_TABLE(table), labelappend, 0, 1, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(table), entryappend, 1, 2, 1, 2);

    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinhour), hour);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinmin), min);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinsec), sec);

    gtk_box_pack_start(GTK_BOX(timebox), spinhour, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(timebox), labelcolon1, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(timebox), spinmin, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(timebox), labelcolon2, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(timebox), spinsec, FALSE, FALSE, 0);

    gtk_table_attach_defaults(GTK_TABLE(table), labeltime, 0, 1, 2, 3);
    gtk_table_attach_defaults(GTK_TABLE(table), timebox, 1, 2, 2, 3);

    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spintimeout), timeoutval);

    gtk_box_pack_start(GTK_BOX(timeoutbox), spintimeout, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(timeoutbox), labelseconds, FALSE, FALSE, 0);

    gtk_table_attach_defaults(GTK_TABLE(table), labeltimeout, 0, 1, 3, 4);
    gtk_table_attach_defaults(GTK_TABLE(table), timeoutbox, 1, 2, 3, 4);

    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkutc), utc);

    gtk_table_attach_defaults(GTK_TABLE(table), labelutc, 0, 1, 4, 5);
    gtk_table_attach_defaults(GTK_TABLE(table), checkutc, 1, 2, 4, 5);

    g_signal_connect(G_OBJECT(calendar), "day-selected", G_CALLBACK(calendar_changed), NULL);
    g_signal_connect(G_OBJECT(entryappend), "focus-out-event", G_CALLBACK(append_changed), NULL);
    g_signal_connect(G_OBJECT(spinhour), "value-changed", G_CALLBACK(hour_changed), NULL);
    g_signal_connect(G_OBJECT(spinmin), "value-changed", G_CALLBACK(min_changed), NULL);
    g_signal_connect(G_OBJECT(spinsec), "value-changed", G_CALLBACK(sec_changed), NULL);
    g_signal_connect(G_OBJECT(spintimeout), "value-changed", G_CALLBACK(timeout_changed), NULL);
    g_signal_connect(G_OBJECT(checkutc), "toggled", G_CALLBACK(utc_changed), NULL);

    return table;
}

static PidginPluginUiInfo ui_info = {
	get_config_frame
};

static PurplePluginInfo info = {
    PURPLE_PLUGIN_MAGIC,    /* Plugin magic, this must always be
                               PURPLE_PLUGIN_MAGIC.*/

    PURPLE_MAJOR_VERSION,   /* This is also defined in libpurple.  It helps
                               libpurple's plugin system determine which version
                               of libpurple this plugin was compiled for, and
                               whether loading it will cause problems. */

    PURPLE_MINOR_VERSION,   /* See previous */

    PURPLE_PLUGIN_STANDARD, /* PurplePluginType: There are 4 different values for
                               this field.  The first is PURPLE_PLUGIN_UNKNOWN,
                               which should not be used.  The second is
                               PURPLE_PLUGIN_STANDARD; this is the value most
                               plugins will use. Next, we have PURPLE_PLUGIN_LOADER;
                               this is the type you want to use if your plugin will
                               make it possible to load non-native plugins.  For
                               example, the Perl and Tcl loader plugins are of this
                               type.  Last, we have PURPLE_PLUGIN_PROTOCOL.  If your
                               plugin is going to allow the user to connect to
                               another network, this is the type you'd want to use. */

    PIDGIN_PLUGIN_TYPE,     /* This field is the UI requirement.  If you're writing
                               a core plugin, this must be NULL and the plugin must
                               not contain any UI code.  If you're writing a Pidgin
                               plugin, you need to use PIDGIN_PLUGIN_TYPE.  If you
                               are writing a Finch plugin, you would use
                               FINCH_PLUGIN_TYPE.*/

    0,                      /* This field is for plugin flags.  Currently, the only
                               flag available to plugins is invisible
                               (PURPLE_PLUGIN_FLAG_INVISIBLE). It causes the plugin
                               NOT to appear in the list of plugins. */

    NULL,                   /* This is a GList of plugin dependencies.  In other words,
                               it's a GList of plugin id's that your plugin depends on.
                               Set this value to NULL no matter what.  If your plugin
                               has dependencies, set them at run-time in the
                               plugin_init function. */

    PURPLE_PRIORITY_DEFAULT,/* This is the priority libpurple will give your plugin.
                               There are three possible values for this field,
                               PURPLE_PRIORITY_DEFAULT, PURPLE_PRIORITY_HIGHEST, and
                               PURPLE_PRIORITY_LOWEST. */

    "gtk-countdown",        /* This is your plugin's id.  There is a whole page dedicated
                               to this in the Related Pages section of the API docs. */

    "Countdown",            /* This is your plugin's name.  This is what will be
                               displayed for your plugin in the UI. */

    "0.2",                  /* This is the version of your plugin. */

    "Set the status to countdown to a date & time.",
                            /* This is the summary of your plugin.  It should be a short
                               blurb.  The UI determines where, if at all, to display
                               this. */

    "Set the date & time and this plugin will set your status to countdown to it.",
                            /* This is the description of your plugin. It can be as long
                               and as descriptive as you like.  And like the summary,
                               it's up to the UI where, if at all, to display this (and
                               how much to display). */

    "Stephen English <steve@secomputing.co.uk>"
    "\n\thttp://www.secomputing.co.uk/"
    "\nKamran Riaz Khan <krkhan@inspirated.com>"
    "\n\thttp://www.inspirated.com/",
                            /* This is where you can put your name and e-mail address. */

    "https://code.launchpad.net/pidgin-countdown",
                            /* This is the website for the plugin.  This tells users
                               where to find new versions, report bugs, etc. */

    plugin_load,            /* This is a pointer to a function for libpurple to call when
                               it is loading the plugin.  It should be of the type:

                                  gboolean plugin_load(PurplePlugin *plugin)

                               Returning FALSE will stop the loading of the plugin.
                               Anything else would evaluate as TRUE and the plugin will
                               continue to load. */

    plugin_unload,          /* Same as above except it is called when libpurple tries to
                               unload your plugin.  It should be of the type:

                                  gboolean plugin_unload(PurplePlugin *plugin)

                               Returning TRUE will tell libpurple to continue unloading
                               while FALSE will stop the unloading of your plugin. */

    NULL,                   /* Similar to the two above members, except this is called
                               when libpurple tries to destory the plugin.  This is
                               generally only called when for some reason or another the
                               plugin fails to probe correctly.  It should be of the type:

                                  void plugin_destroy(PurplePlugin *plugin) */

    &ui_info,               /* This is a pointer to a UI-specific struct.  For a Pidgin
                               plugin it will be a pointer to a PidginPluginUiInfo
                               struct, for example. */

    NULL,                   /* This is a pointer to either a PurplePluginLoaderInfo
                               struct or a PurplePluginProtocolInfo struct, and is
                               beyond the scope of this document. */

    NULL,                   /* This is a pointer to a PurplePluginUiInfo struct.  It is
                               a core/ui split way for core plugins to have a UI
                               configuration frame.  You can find an example of this
                               code in libpurple/plugins/pluginpref_example.c */

    NULL,                   /* This is a function pointer where you can define "plugin
                               actions".  The UI controls how they're displayed.  It
                               should be of the type:

                                  GList *function_name(PurplePlugin *plugin,
                                                       gpointer context)

                               It must return a GList of PurplePluginActions. */

    NULL,                   /* This is a pointer reserved for future use.  We set it to
                               NULL to indicate we don't need it. */

    NULL,                   /* This is a pointer reserved for future use.  We set it to
                               NULL to indicate we don't need it. */

    NULL,                   /* This is a pointer reserved for future use.  We set it to
                               NULL to indicate we don't need it. */

    NULL                    /* This is a pointer reserved for future use.  We set it to
                               NULL to indicate we don't need it. */
};

static void                        
init_plugin(PurplePlugin *plugin)
{
}

PURPLE_INIT_PLUGIN(countdown, init_plugin, info)