2 * Contact Availability Prediction plugin for Purple
4 * Copyright (C) 2006 Geoffrey Foster.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 static void generate_prediction(CapStatistics
*statistics
) {
25 if(statistics
->buddy
) {
26 if(statistics
->prediction
== NULL
)
27 statistics
->prediction
= g_malloc(sizeof(CapPrediction
));
28 statistics
->prediction
->probability
= generate_prediction_for(statistics
->buddy
);
29 statistics
->prediction
->generated_at
= time(NULL
);
33 static double generate_prediction_for(PurpleBuddy
*buddy
) {
34 double prediction
= 1.0f
;
35 gboolean generated
= FALSE
;
36 PurpleAccount
*account
= purple_buddy_get_account(buddy
);
37 const gchar
*buddy_name
= purple_buddy_get_name(buddy
);
38 const gchar
*protocol_id
= purple_account_get_protocol_id(account
);
39 const gchar
*account_id
= purple_account_get_username(account
);
40 const gchar
*status_id
= purple_status_get_id(get_status_for(buddy
));
41 time_t t
= time(NULL
);
42 struct tm
*current_time
= localtime(&t
);
43 int current_minute
= current_time
->tm_min
+ current_time
->tm_hour
* 60;
44 int threshold
= purple_prefs_get_int("/plugins/gtk/cap/threshold");
45 int min_minute
= (current_minute
- threshold
) % 1440;
46 int max_minute
= (current_minute
+ threshold
) % 1440;
48 sqlite3_stmt
*stmt
= NULL
;
49 const char *tail
= NULL
;
53 sql
= sqlite3_mprintf("select sum(success_count) as successes, sum(failed_count) as failures "
54 "from cap_msg_count where "
55 "buddy=%Q and account=%Q and protocol=%Q and minute_val>=%d and minute_val<=%d;",
56 buddy_name
, account_id
, protocol_id
, min_minute
, max_minute
);
57 rc
= sqlite3_prepare(_db
, sql
, -1, &stmt
, &tail
);
62 if(sqlite3_step(stmt
) == SQLITE_ROW
) {
63 successes
= sqlite3_column_int(stmt
, 0);
64 failures
= sqlite3_column_int(stmt
, 1);
65 if(failures
+ successes
> 0) {
66 prediction
*= ((double)successes
/((double)(successes
+failures
)));
70 sqlite3_finalize(stmt
);
75 sql
= sqlite3_mprintf("select sum(success_count) as successes, sum(failed_count) as failures "
76 "from cap_status_count where "
77 "buddy=%Q and account=%Q and protocol=%Q and status=%Q;",
78 buddy_name
, account_id
, protocol_id
, status_id
);
79 rc
= sqlite3_prepare(_db
, sql
, -1, &stmt
, &tail
);
84 if(sqlite3_step(stmt
) == SQLITE_ROW
) {
85 successes
= sqlite3_column_int(stmt
, 0);
86 failures
= sqlite3_column_int(stmt
, 1);
87 if(failures
+ successes
> 0) {
88 prediction
*= ((double)successes
/((double)(successes
+failures
)));
92 sqlite3_finalize(stmt
);
103 static CapStatistics
* get_stats_for(PurpleBuddy
*buddy
) {
104 CapStatistics
*stats
;
106 g_return_val_if_fail(buddy
!= NULL
, NULL
);
108 stats
= g_hash_table_lookup(_buddy_stats
, purple_buddy_get_name(buddy
));
110 stats
= g_malloc0(sizeof(CapStatistics
));
111 stats
->last_message
= -1;
112 stats
->buddy
= buddy
;
113 stats
->last_seen
= -1;
114 stats
->last_status_id
= "";
116 g_hash_table_insert(_buddy_stats
,
117 g_strdup(purple_buddy_get_name(buddy
)), stats
);
119 /* This may actually be a different PurpleBuddy than what is in stats.
120 * We replace stats->buddy to make sure we're looking at a valid pointer. */
121 stats
->buddy
= buddy
;
123 generate_prediction(stats
);
127 static void destroy_stats(gpointer data
) {
128 CapStatistics
*stats
= data
;
129 g_free(stats
->prediction
);
130 /* g_free(stats->hourly_usage); */
131 /* g_free(stats->daily_usage); */
132 if (stats
->timeout_source_id
!= 0)
133 purple_timeout_remove(stats
->timeout_source_id
);
138 insert_cap_msg_count_success(const char *buddy_name
, const char *account
, const char *protocol
, int minute
) {
142 char *sql_select
= sqlite3_mprintf("SELECT * FROM cap_msg_count WHERE "
143 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
144 buddy_name
, account
, protocol
, minute
);
145 char *sql_ins_up
= NULL
;
147 purple_debug_info("cap", "%s\n", sql_select
);
149 sqlite3_prepare(_db
, sql_select
, -1, &stmt
, &tail
);
151 rc
= sqlite3_step(stmt
);
153 if(rc
== SQLITE_DONE
) {
154 sql_ins_up
= sqlite3_mprintf("INSERT INTO cap_msg_count VALUES (%Q, %Q, %Q, %d, %d, %d);",
155 buddy_name
, account
, protocol
, minute
, 1, 0);
156 } else if(rc
== SQLITE_ROW
) {
157 sql_ins_up
= sqlite3_mprintf("UPDATE cap_msg_count SET success_count=success_count+1 WHERE "
158 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
159 buddy_name
, account
, protocol
, minute
);
161 purple_debug_info("cap", "%d\n", rc
);
162 sqlite3_finalize(stmt
);
163 sqlite3_free(sql_select
);
167 sqlite3_finalize(stmt
);
168 sqlite3_free(sql_select
);
170 sqlite3_exec(_db
, sql_ins_up
, NULL
, NULL
, NULL
);
171 sqlite3_free(sql_ins_up
);
175 insert_cap_status_count_success(const char *buddy_name
, const char *account
, const char *protocol
, const char *status_id
) {
179 char *sql_select
= sqlite3_mprintf("SELECT * FROM cap_status_count WHERE "
180 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
181 buddy_name
, account
, protocol
, status_id
);
182 char *sql_ins_up
= NULL
;
184 purple_debug_info("cap", "%s\n", sql_select
);
186 sqlite3_prepare(_db
, sql_select
, -1, &stmt
, &tail
);
188 rc
= sqlite3_step(stmt
);
190 if(rc
== SQLITE_DONE
) {
191 sql_ins_up
= sqlite3_mprintf("INSERT INTO cap_status_count VALUES (%Q, %Q, %Q, %Q, %d, %d);",
192 buddy_name
, account
, protocol
, status_id
, 1, 0);
193 } else if(rc
== SQLITE_ROW
) {
194 sql_ins_up
= sqlite3_mprintf("UPDATE cap_status_count SET success_count=success_count+1 WHERE "
195 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
196 buddy_name
, account
, protocol
, status_id
);
198 purple_debug_info("cap", "%d\n", rc
);
199 sqlite3_finalize(stmt
);
200 sqlite3_free(sql_select
);
204 sqlite3_finalize(stmt
);
205 sqlite3_free(sql_select
);
207 sqlite3_exec(_db
, sql_ins_up
, NULL
, NULL
, NULL
);
208 sqlite3_free(sql_ins_up
);
212 insert_cap_msg_count_failed(const char *buddy_name
, const char *account
, const char *protocol
, int minute
) {
216 char *sql_select
= sqlite3_mprintf("SELECT * FROM cap_msg_count WHERE "
217 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
218 buddy_name
, account
, protocol
, minute
);
219 char *sql_ins_up
= NULL
;
221 purple_debug_info("cap", "%s\n", sql_select
);
223 sqlite3_prepare(_db
, sql_select
, -1, &stmt
, &tail
);
225 rc
= sqlite3_step(stmt
);
227 if(rc
== SQLITE_DONE
) {
228 sql_ins_up
= sqlite3_mprintf("INSERT INTO cap_msg_count VALUES (%Q, %Q, %Q, %d, %d, %d);",
229 buddy_name
, account
, protocol
, minute
, 0, 1);
230 } else if(rc
== SQLITE_ROW
) {
231 sql_ins_up
= sqlite3_mprintf("UPDATE cap_msg_count SET failed_count=failed_count+1 WHERE "
232 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
233 buddy_name
, account
, protocol
, minute
);
235 purple_debug_info("cap", "%d\n", rc
);
236 sqlite3_finalize(stmt
);
237 sqlite3_free(sql_select
);
241 sqlite3_finalize(stmt
);
242 sqlite3_free(sql_select
);
244 sqlite3_exec(_db
, sql_ins_up
, NULL
, NULL
, NULL
);
245 sqlite3_free(sql_ins_up
);
249 insert_cap_status_count_failed(const char *buddy_name
, const char *account
, const char *protocol
, const char *status_id
) {
253 char *sql_select
= sqlite3_mprintf("SELECT * FROM cap_status_count WHERE "
254 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
255 buddy_name
, account
, protocol
, status_id
);
256 char *sql_ins_up
= NULL
;
258 purple_debug_info("cap", "%s\n", sql_select
);
260 sqlite3_prepare(_db
, sql_select
, -1, &stmt
, &tail
);
262 rc
= sqlite3_step(stmt
);
264 if(rc
== SQLITE_DONE
) {
265 sql_ins_up
= sqlite3_mprintf("INSERT INTO cap_status_count VALUES (%Q, %Q, %Q, %Q, %d, %d);",
266 buddy_name
, account
, protocol
, status_id
, 0, 1);
267 } else if(rc
== SQLITE_ROW
) {
268 sql_ins_up
= sqlite3_mprintf("UPDATE cap_status_count SET failed_count=failed_count+1 WHERE "
269 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
270 buddy_name
, account
, protocol
, status_id
);
272 purple_debug_info("cap", "%d\n", rc
);
273 sqlite3_finalize(stmt
);
274 sqlite3_free(sql_select
);
278 sqlite3_finalize(stmt
);
279 sqlite3_free(sql_select
);
281 sqlite3_exec(_db
, sql_ins_up
, NULL
, NULL
, NULL
);
282 sqlite3_free(sql_ins_up
);
285 static void insert_cap_success(CapStatistics
*stats
) {
286 PurpleAccount
*account
= purple_buddy_get_account(stats
->buddy
);
287 const gchar
*buddy_name
= purple_buddy_get_name(stats
->buddy
);
288 const gchar
*protocol_id
= purple_account_get_protocol_id(account
);
289 const gchar
*account_id
= purple_account_get_username(account
);
290 const gchar
*status_id
= (stats
->last_message_status_id
) ?
291 stats
->last_message_status_id
:
292 purple_status_get_id(get_status_for(stats
->buddy
));
293 struct tm
*current_time
;
296 if(stats
->last_message
== -1) {
297 time_t now
= time(NULL
);
298 current_time
= localtime(&now
);
300 current_time
= localtime(&stats
->last_message
);
302 minute
= current_time
->tm_min
+ current_time
->tm_hour
* 60;
304 insert_cap_msg_count_success(buddy_name
, account_id
, protocol_id
, minute
);
306 insert_cap_status_count_success(buddy_name
, account_id
, protocol_id
, status_id
);
308 stats
->last_message
= -1;
309 stats
->last_message_status_id
= NULL
;
312 static void insert_cap_failure(CapStatistics
*stats
) {
313 PurpleAccount
*account
= purple_buddy_get_account(stats
->buddy
);
314 const gchar
*buddy_name
= purple_buddy_get_name(stats
->buddy
);
315 const gchar
*protocol_id
= purple_account_get_protocol_id(account
);
316 const gchar
*account_id
= purple_account_get_username(account
);
317 const gchar
*status_id
= (stats
->last_message_status_id
) ?
318 stats
->last_message_status_id
:
319 purple_status_get_id(get_status_for(stats
->buddy
));
320 struct tm
*current_time
= localtime(&stats
->last_message
);
321 int minute
= current_time
->tm_min
+ current_time
->tm_hour
* 60;
323 insert_cap_msg_count_failed(buddy_name
, account_id
, protocol_id
, minute
);
325 insert_cap_status_count_failed(buddy_name
, account_id
, protocol_id
, status_id
);
327 stats
->last_message
= -1;
328 stats
->last_message_status_id
= NULL
;
331 static gboolean
max_message_difference_cb(gpointer data
) {
332 CapStatistics
*stats
= data
;
333 purple_debug_info("cap", "Max Message Difference timeout occurred\n");
334 insert_cap_failure(stats
);
335 stats
->timeout_source_id
= 0;
339 /* Purple Signal Handlers */
342 static void sent_im_msg(PurpleAccount
*account
, PurpleMessage
*msg
, gpointer _unused
)
345 guint interval
, words
;
346 CapStatistics
*stats
= NULL
;
348 buddy
= purple_blist_find_buddy(account
, purple_message_get_recipient(msg
));
353 interval
= purple_prefs_get_int("/plugins/gtk/cap/max_msg_difference") * 60;
354 words
= word_count(purple_message_get_contents(msg
));
356 stats
= get_stats_for(buddy
);
358 insert_word_count(purple_account_get_username(account
), purple_message_get_recipient(msg
), words
);
359 stats
->last_message
= time(NULL
);
360 stats
->last_message_status_id
= purple_status_get_id(get_status_for(buddy
));
361 if(stats
->timeout_source_id
!= 0)
362 purple_timeout_remove(stats
->timeout_source_id
);
364 stats
->timeout_source_id
= purple_timeout_add_seconds(interval
, max_message_difference_cb
, stats
);
367 /* received-im-msg */
369 received_im_msg(PurpleAccount
*account
, char *sender
, char *message
, PurpleConversation
*conv
, PurpleMessageFlags flags
) {
371 CapStatistics
*stats
;
372 /* guint words = word_count(message); */
374 if (flags
& PURPLE_MESSAGE_AUTO_RESP
)
377 buddy
= purple_blist_find_buddy(account
, sender
);
382 stats
= get_stats_for(buddy
);
384 /* insert_word_count(sender, buddy_name, words); */
386 /* If we are waiting for a response from a prior message
387 * then cancel the timeout callback. */
388 if(stats
->timeout_source_id
!= 0) {
389 purple_debug_info("cap", "Cancelling timeout callback\n");
390 purple_timeout_remove(stats
->timeout_source_id
);
391 stats
->timeout_source_id
= 0;
394 insert_cap_success(stats
);
396 /* Reset the last_message value */
397 stats
->last_message
= -1;
398 /* Reset the last status id value */
399 stats
->last_message_status_id
= NULL
;
402 /* buddy-status-changed */
403 static void buddy_status_changed(PurpleBuddy
*buddy
, PurpleStatus
*old_status
, PurpleStatus
*status
) {
404 CapStatistics
*stats
= get_stats_for(buddy
);
405 insert_status_change_from_purple_status(stats
, status
);
408 /* buddy-signed-on */
409 static void buddy_signed_on(PurpleBuddy
*buddy
) {
410 CapStatistics
*stats
= get_stats_for(buddy
);
412 /* If the statistic object existed but doesn't have a buddy pointer associated
413 * with it then reassociate one with it. The pointer being null is a result
414 * of a buddy with existing stats signing off and Purple sticking around. */
416 stats
->buddy
= buddy
;
419 insert_status_change(stats
);
422 /* buddy-signed-off */
423 static void buddy_signed_off(PurpleBuddy
*buddy
) {
424 CapStatistics
*stats
= get_stats_for(buddy
);
426 /* We don't necessarily want to delete a buddies generated statistics every time they go offline.
427 * Instead we just set the buddy pointer to null so that when they come back online we can look
428 * them up again and continue using their statistics.
430 insert_status_change(stats
);
431 /* stats->buddy = NULL; */
432 stats
->last_seen
= time(NULL
);
435 /* drawing-tooltip */
436 static void drawing_tooltip(PurpleBlistNode
*node
, GString
*text
, gboolean full
) {
437 if (PURPLE_IS_BUDDY(node
)) {
438 PurpleBuddy
*buddy
= PURPLE_BUDDY(node
);
439 CapStatistics
*stats
= get_stats_for(buddy
);
440 /* get the probability that this buddy will respond and add to the tooltip */
441 if(stats
->prediction
->probability
>= 0.0) {
442 g_string_append_printf(text
, "\n<b>%s</b> %3.0f %%", _("Response Probability:"),
443 100 * stats
->prediction
->probability
);
445 g_string_append_printf(text
, "\n<b>%s</b> ???", _("Response Probability:"));
451 static void signed_on(PurpleConnection
*gc
) {
452 PurpleAccount
*account
= purple_connection_get_account(gc
);
453 const char *my_purple_name
= purple_account_get_username(account
);
454 gchar
*my_name
= g_strdup(my_purple_name
);
455 time_t *last_offline
= g_hash_table_lookup(_my_offline_times
, my_name
);
457 const gchar
*account_id
= purple_account_get_username(account
);
458 const gchar
*protocol_id
= purple_account_get_protocol_id(account
);
461 sql
= sqlite3_mprintf("insert into cap_my_usage values(%Q, %Q, %d, now());", account_id
, protocol_id
, 1);
462 sqlite3_exec(_db
, sql
, NULL
, NULL
, NULL
);
466 if(difftime(*last_offline
, time(NULL
)) > purple_prefs_get_int("/plugins/gtk/cap/max_seen_difference") * 60) {
467 /* reset all of the last_message times to -1 */
468 g_hash_table_foreach(_my_offline_times
, reset_all_last_message_times
, NULL
);
470 g_hash_table_remove(_my_offline_times
, my_name
);
476 static void signed_off(PurpleConnection
*gc
) {
477 /* Here we record the time you (the user) sign off of an account.
478 * The account username is the key in the hashtable and the sign off time_t
479 * (equal to the sign off time) is the value. */
480 PurpleAccount
*account
= purple_connection_get_account(gc
);
481 const char *my_purple_name
= purple_account_get_username(account
);
482 gchar
*my_name
= g_strdup(my_purple_name
);
483 time_t *offline_time
= g_malloc(sizeof(time_t));
484 const gchar
*account_id
= purple_account_get_username(account
);
485 const gchar
*protocol_id
= purple_account_get_protocol_id(account
);
488 sql
= sqlite3_mprintf("insert into cap_my_usage values(%Q, %Q, %d, now());", account_id
, protocol_id
, 0);
489 sqlite3_exec(_db
, sql
, NULL
, NULL
, NULL
);
493 g_hash_table_insert(_my_offline_times
, my_name
, offline_time
);
496 static void reset_all_last_message_times(gpointer key
, gpointer value
, gpointer user_data
) {
497 CapStatistics
*stats
= value
;
498 stats
->last_message
= -1;
501 static PurpleStatus
* get_status_for(PurpleBuddy
*buddy
) {
502 PurplePresence
*presence
= purple_buddy_get_presence(buddy
);
503 PurpleStatus
*status
= purple_presence_get_active_status(presence
);
507 static void create_tables() {
509 "CREATE TABLE IF NOT EXISTS cap_status ("
510 " buddy varchar(60) not null,"
511 " account varchar(60) not null,"
512 " protocol varchar(60) not null,"
513 " status varchar(60) not null,"
514 " event_time datetime not null,"
515 " primary key (buddy, account, protocol, event_time)"
520 "create table if not exists cap_message ("
521 " sender varchar(60) not null,"
522 " receiver varchar(60) not null,"
523 " account varchar(60) not null,"
524 " protocol varchar(60) not null,"
525 " word_count integer not null,"
526 " event_time datetime not null,"
527 " primary key (sender, account, protocol, receiver, event_time)"
532 "create table if not exists cap_msg_count ("
533 " buddy varchar(60) not null,"
534 " account varchar(60) not null,"
535 " protocol varchar(60) not null,"
536 " minute_val int not null,"
537 " success_count int not null,"
538 " failed_count int not null,"
539 " primary key (buddy, account, protocol, minute_val)"
544 "create table if not exists cap_status_count ("
545 " buddy varchar(60) not null,"
546 " account varchar(60) not null,"
547 " protocol varchar(60) not null,"
548 " status varchar(60) not null,"
549 " success_count int not null,"
550 " failed_count int not null,"
551 " primary key (buddy, account, protocol, status)"
556 "create table if not exists cap_my_usage ("
557 " account varchar(60) not null,"
558 " protocol varchar(60) not null,"
559 " online tinyint not null,"
560 " event_time datetime not null,"
561 " primary key(account, protocol, online, event_time)"
566 static gboolean
create_database_connection() {
574 path
= g_build_filename(purple_user_dir(), "cap.db", (gchar
*)NULL
);
576 /* make database connection here */
577 rc
= sqlite3_open(path
, &_db
);
582 /* Add tables here */
584 purple_debug_info("cap", "Database connection successfully made.\n");
587 static void destroy_database_connection() {
594 static guint
word_count(const gchar
*string
) {
595 /*TODO: doesn't really work, should use regex instead (#include <regex.h>)*/
596 gchar
**result
= g_strsplit_set(string
, " ", -1);
597 guint count
= g_strv_length(result
);
604 static void insert_status_change(CapStatistics
*statistics
) {
605 insert_status_change_from_purple_status(statistics
, get_status_for(statistics
->buddy
));
608 static void insert_status_change_from_purple_status(CapStatistics
*statistics
, PurpleStatus
*status
) {
609 PurpleAccount
*account
= purple_buddy_get_account(statistics
->buddy
);
611 const gchar
*status_id
;
612 const gchar
*buddy_name
;
613 const gchar
*protocol_id
;
614 const gchar
*account_id
;
616 /* It would seem that some protocols receive periodic updates of the buddies status.
617 * Check to make sure the last status is not the same as current status to prevent
618 * to many duplicated useless database entries. */
619 if(strcmp(statistics
->last_status_id
, purple_status_get_id(status
)) == 0)
622 status_id
= purple_status_get_id(status
);
623 buddy_name
= purple_buddy_get_name(statistics
->buddy
);
624 protocol_id
= purple_account_get_protocol_id(account
);
625 account_id
= purple_account_get_username(account
);
627 statistics
->last_status_id
= purple_status_get_id(status
);
629 purple_debug_info("cap", "Executing: insert into cap_status (buddy, account, protocol, status, event_time) values(%s, %s, %s, %s, now());\n", buddy_name
, account_id
, protocol_id
, status_id
);
631 sql
= sqlite3_mprintf("insert into cap_status values (%Q, %Q, %Q, %Q, now());", buddy_name
, account_id
, protocol_id
, status_id
);
632 sqlite3_exec(_db
, sql
, NULL
, NULL
, NULL
);
636 static void insert_word_count(const char *sender
, const char *receiver
, guint count
) {
638 /* dbi_result result; */
639 /* result = dbi_conn_queryf(_conn, "insert into cap_message values(\'%s\', \'%s\', %d, now());", sender, receiver, count); */
642 /* Purple plugin specific code */
644 static void add_plugin_functionality(PurplePlugin
*plugin
) {
645 if(_signals_connected
)
648 purple_debug_info("cap", "Adding plugin functionality.\n");
650 /* Connect all the signals */
651 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", plugin
,
652 PURPLE_CALLBACK(sent_im_msg
), NULL
);
654 purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin
,
655 PURPLE_CALLBACK(received_im_msg
), NULL
);
657 purple_signal_connect(purple_blist_get_handle(), "buddy-status-changed", plugin
,
658 PURPLE_CALLBACK(buddy_status_changed
), NULL
);
660 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin
,
661 PURPLE_CALLBACK(buddy_signed_on
), NULL
);
663 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin
,
664 PURPLE_CALLBACK(buddy_signed_off
), NULL
);
666 purple_signal_connect(pidgin_blist_get_handle(), "drawing-tooltip", plugin
,
667 PURPLE_CALLBACK(drawing_tooltip
), NULL
);
669 purple_signal_connect(purple_connections_get_handle(), "signed-on", plugin
,
670 PURPLE_CALLBACK(signed_on
), NULL
);
672 purple_signal_connect(purple_connections_get_handle(), "signed-off", plugin
,
673 PURPLE_CALLBACK(signed_off
), NULL
);
675 _signals_connected
= TRUE
;
678 static void cancel_conversation_timeouts(gpointer key
, gpointer value
, gpointer user_data
) {
679 CapStatistics
*stats
= value
;
680 if(stats
->timeout_source_id
!= 0) {
681 purple_timeout_remove(stats
->timeout_source_id
);
682 stats
->timeout_source_id
= 0;
686 static void remove_plugin_functionality(PurplePlugin
*plugin
) {
687 if(!_signals_connected
)
690 purple_debug_info("cap", "Removing plugin functionality.\n");
692 /* If there are any timeouts waiting to be processed then cancel them */
693 g_hash_table_foreach(_buddy_stats
, cancel_conversation_timeouts
, NULL
);
695 /* Connect all the signals */
696 purple_signal_disconnect(purple_conversations_get_handle(), "sent-im-msg", plugin
,
697 PURPLE_CALLBACK(sent_im_msg
));
699 purple_signal_disconnect(purple_conversations_get_handle(), "received-im-msg", plugin
,
700 PURPLE_CALLBACK(received_im_msg
));
702 purple_signal_disconnect(purple_blist_get_handle(), "buddy-status-changed", plugin
,
703 PURPLE_CALLBACK(buddy_status_changed
));
705 purple_signal_disconnect(purple_blist_get_handle(), "buddy-signed-on", plugin
,
706 PURPLE_CALLBACK(buddy_signed_on
));
708 purple_signal_disconnect(purple_blist_get_handle(), "buddy-signed-off", plugin
,
709 PURPLE_CALLBACK(buddy_signed_off
));
711 purple_signal_disconnect(pidgin_blist_get_handle(), "drawing-tooltip", plugin
,
712 PURPLE_CALLBACK(drawing_tooltip
));
714 purple_signal_disconnect(purple_connections_get_handle(), "signed-on", plugin
,
715 PURPLE_CALLBACK(signed_on
));
717 purple_signal_disconnect(purple_connections_get_handle(), "signed-off", plugin
,
718 PURPLE_CALLBACK(signed_off
));
720 _signals_connected
= FALSE
;
723 static void write_stats_on_unload(gpointer key
, gpointer value
, gpointer user_data
) {
724 CapStatistics
*stats
= value
;
725 if(stats
->last_message
!= -1 && stats
->buddy
!= NULL
) {
726 insert_cap_failure(stats
);
730 static CapPrefsUI
* create_cap_prefs_ui() {
731 CapPrefsUI
*ui
= g_malloc(sizeof(CapPrefsUI
));
733 ui
->ret
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 18);
734 gtk_container_set_border_width(GTK_CONTAINER(ui
->ret
), 10);
735 ui
->cap_vbox
= pidgin_make_frame(ui
->ret
, _("Statistics Configuration"));
737 /* msg_difference spinner */
738 ui
->msg_difference_label
= gtk_label_new(_("Maximum response timeout:"));
739 gtk_label_set_xalign(GTK_LABEL(ui
->msg_difference_label
), 0);
740 ui
->msg_difference_input
= gtk_spin_button_new_with_range(1, 1440, 1);
741 ui
->msg_difference_minutes_label
= gtk_label_new(_("minutes"));
742 gtk_label_set_xalign(GTK_LABEL(ui
->msg_difference_minutes_label
), 0);
744 /* last_seen spinner */
745 ui
->last_seen_label
= gtk_label_new(_("Maximum last-seen difference:"));
746 gtk_label_set_xalign(GTK_LABEL(ui
->last_seen_label
), 0);
747 ui
->last_seen_input
= gtk_spin_button_new_with_range(1, 1440, 1);
748 ui
->last_seen_minutes_label
= gtk_label_new(_("minutes"));
749 gtk_label_set_xalign(GTK_LABEL(ui
->last_seen_minutes_label
), 0);
751 /* threshold spinner */
752 ui
->threshold_label
= gtk_label_new(_("Threshold:"));
753 gtk_label_set_xalign(GTK_LABEL(ui
->threshold_label
), 0);
754 ui
->threshold_input
= gtk_spin_button_new_with_range(1, 1440, 1);
755 ui
->threshold_minutes_label
= gtk_label_new(_("minutes"));
756 gtk_label_set_xalign(GTK_LABEL(ui
->threshold_minutes_label
), 0);
758 /* Layout threshold/last-seen/response-timeout input items */
759 ui
->table_layout
= gtk_grid_new();
760 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->threshold_label
, 0, 0, 1, 1);
761 gtk_widget_set_hexpand(ui
->threshold_label
, TRUE
);
763 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->threshold_input
, 1, 0, 1, 1);
764 gtk_widget_set_hexpand(ui
->threshold_input
, TRUE
);
766 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->threshold_minutes_label
, 2, 0, 1, 1);
767 gtk_widget_set_hexpand(ui
->threshold_minutes_label
, TRUE
);
769 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->msg_difference_label
, 0, 1, 1, 1);
770 gtk_widget_set_hexpand(ui
->msg_difference_label
, TRUE
);
772 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->msg_difference_input
, 1, 1, 1, 1);
773 gtk_widget_set_hexpand(ui
->msg_difference_input
, TRUE
);
775 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->msg_difference_minutes_label
, 2, 1, 1, 1);
776 gtk_widget_set_hexpand(ui
->msg_difference_minutes_label
, TRUE
);
778 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->last_seen_label
, 0, 2, 1, 1);
779 gtk_widget_set_hexpand(ui
->last_seen_label
, TRUE
);
781 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->last_seen_input
, 1, 2, 1, 1);
782 gtk_widget_set_hexpand(ui
->last_seen_input
, TRUE
);
784 gtk_grid_attach(GTK_GRID(ui
->table_layout
), ui
->last_seen_minutes_label
, 2, 2, 1, 1);
785 gtk_widget_set_hexpand(ui
->last_seen_minutes_label
, TRUE
);
788 /* Config window - lay it out */
789 gtk_box_pack_start(GTK_BOX(ui
->cap_vbox
), ui
->table_layout
, FALSE
, FALSE
, 0);
791 /* Set the input areas to contain the configuration values from
794 if(purple_prefs_exists("/plugins/gtk/cap/max_msg_difference")) {
795 int max_msg_diff
= purple_prefs_get_int("/plugins/gtk/cap/max_msg_difference");
796 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui
->msg_difference_input
), max_msg_diff
);
798 if(purple_prefs_exists("/plugins/gtk/cap/max_seen_difference")) {
799 int max_seen_diff
= purple_prefs_get_int("/plugins/gtk/cap/max_seen_difference");
800 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui
->last_seen_input
), max_seen_diff
);
802 if(purple_prefs_exists("/plugins/gtk/cap/threshold")) {
803 int threshold
= purple_prefs_get_int("/plugins/gtk/cap/threshold");
804 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui
->threshold_input
), threshold
);
807 /* Add the signals */
808 g_signal_connect(G_OBJECT(ui
->ret
), "destroy",
809 G_CALLBACK(cap_prefs_ui_destroy_cb
), ui
);
811 g_signal_connect(G_OBJECT(ui
->msg_difference_input
), "value-changed",
812 G_CALLBACK(numeric_spinner_prefs_cb
), "/plugins/gtk/cap/max_msg_difference");
814 g_signal_connect(G_OBJECT(ui
->last_seen_input
), "value-changed",
815 G_CALLBACK(numeric_spinner_prefs_cb
), "/plugins/gtk/cap/max_seen_difference");
817 g_signal_connect(G_OBJECT(ui
->threshold_input
), "value-changed",
818 G_CALLBACK(numeric_spinner_prefs_cb
), "/plugins/gtk/cap/threshold");
823 static void cap_prefs_ui_destroy_cb(GObject
*object
, gpointer user_data
) {
824 CapPrefsUI
*ui
= user_data
;
826 add_plugin_functionality(_plugin_pointer
);
831 static void numeric_spinner_prefs_cb(GtkSpinButton
*spinbutton
, gpointer user_data
) {
832 purple_prefs_set_int(user_data
, gtk_spin_button_get_value_as_int(spinbutton
));
835 static GtkWidget
* get_config_frame(PurplePlugin
*plugin
) {
836 CapPrefsUI
*ui
= create_cap_prefs_ui();
839 * Prevent database stuff from occuring since we are editing values
841 remove_plugin_functionality(_plugin_pointer
);
846 static PidginPluginInfo
*
847 plugin_query(GError
**error
)
849 const gchar
* const authors
[] = {
850 "Geoffrey Foster <geoffrey.foster@gmail.com>",
854 return pidgin_plugin_info_new(
856 "name", N_("Contact Availability Prediction"),
857 "version", DISPLAY_VERSION
,
858 "category", N_("Utility"),
859 "summary", N_("Contact Availability Prediction plugin."),
860 "description", N_("Displays statistical information about "
861 "your buddies' availability"),
863 "website", PURPLE_WEBSITE
,
864 "abi-version", PURPLE_ABI_VERSION
,
865 "gtk-config-frame-cb", get_config_frame
,
870 static gboolean
plugin_load(PurplePlugin
*plugin
, GError
**error
) {
871 purple_prefs_add_none("/plugins/gtk/cap");
872 purple_prefs_add_int("/plugins/gtk/cap/max_seen_difference", 1);
873 purple_prefs_add_int("/plugins/gtk/cap/max_msg_difference", 10);
874 purple_prefs_add_int("/plugins/gtk/cap/threshold", 5);
876 _plugin_pointer
= plugin
;
877 _signals_connected
= FALSE
;
879 /* buddy_stats is a hashtable where strings are keys
880 * and the keys are a buddies account id (PurpleBuddy.name).
881 * keys/values are automatically deleted */
882 _buddy_stats
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, destroy_stats
);
884 /* ? - Can't remember at the moment
886 _my_offline_times
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
888 if(create_database_connection()) {
889 add_plugin_functionality(plugin
);
894 static gboolean
plugin_unload(PurplePlugin
*plugin
, GError
**error
) {
895 purple_debug_info("cap", "CAP plugin unloading\n");
897 /* clean up memory allocations */
899 g_hash_table_foreach(_buddy_stats
, write_stats_on_unload
, NULL
);
900 g_hash_table_destroy(_buddy_stats
);
903 /* close database connection */
904 destroy_database_connection();
909 PURPLE_PLUGIN_INIT(cap
, plugin_query
, plugin_load
, plugin_unload
);