Mercurial > pidgin
comparison gtk/plugins/cap/cap.c @ 14266:63ef6342db05
[gaim-migrate @ 16948]
* Initial commit of the Contact Availability Prediction plugin
* Located in trunk/gtk/plugins/cap
* Check README for further information and requirements for build
committer: Tailor Script <tailor@pidgin.im>
author | Geoff Foster <g-off_> |
---|---|
date | Mon, 21 Aug 2006 07:44:31 +0000 |
parents | |
children | eec3ce357b70 |
comparison
equal
deleted
inserted
replaced
14265:584cbd1628d0 | 14266:63ef6342db05 |
---|---|
1 /* | |
2 * Contact Availability Prediction plugin for Gaim | |
3 * | |
4 * Copyright (C) 2006 Geoffrey Foster. | |
5 * | |
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. | |
10 * | |
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. | |
15 * | |
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., 59 Temple Place - Suite 330, Boston, MA | |
19 * 02111-1307, USA. | |
20 */ | |
21 | |
22 #include "cap.h" | |
23 | |
24 static char * quote_string(const char *str) { | |
25 gchar *quoted_str = NULL; | |
26 quoted_str = g_strdup(str); | |
27 dbi_driver_quote_string(_driver, "ed_str); | |
28 return quoted_str; | |
29 } | |
30 | |
31 static void generate_minute_stats(CapStatistics *statistics) { | |
32 gchar *buddy_name = quote_string(statistics->buddy->name); | |
33 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(statistics->buddy->account)); | |
34 gchar *account_id = quote_string(gaim_account_get_username(statistics->buddy->account)); | |
35 dbi_result result = dbi_conn_queryf(_conn, "select minute_val, success_count, failed_count from cap_msg_count where buddy=%s account=%s and protocol=%s;", buddy_name, account_id, protocol_id); | |
36 | |
37 while(dbi_result_next_row(result)) { | |
38 int minute_val = dbi_result_get_int(result, "minute_val"); | |
39 int success_count = dbi_result_get_int(result, "success_count"); | |
40 int failed_count = dbi_result_get_int(result, "failed_count"); | |
41 double ratio = ((double)success_count/(double)(success_count + failed_count)); | |
42 statistics->minute_stats[minute_val] = ratio; | |
43 } | |
44 dbi_result_free(result); | |
45 } | |
46 | |
47 static void generate_prediction(CapStatistics *statistics) { | |
48 if(statistics->buddy) { | |
49 if(statistics->prediction == NULL) | |
50 statistics->prediction = g_malloc(sizeof(CapPrediction)); | |
51 statistics->prediction->probability = generate_prediction_for(statistics->buddy); | |
52 statistics->prediction->generated_at = time(NULL); | |
53 } | |
54 } | |
55 | |
56 static double generate_prediction_for(GaimBuddy *buddy) { | |
57 double prediction = 1.0f; | |
58 gboolean generated = FALSE; | |
59 gchar *buddy_name = quote_string(buddy->name); | |
60 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(buddy->account)); | |
61 gchar *account_id = quote_string(gaim_account_get_username(buddy->account)); | |
62 gchar *status_id = quote_string(gaim_status_get_id(get_status_for(buddy))); | |
63 time_t t = time(NULL); | |
64 struct tm *current_time = localtime(&t); | |
65 int current_minute = current_time->tm_min + current_time->tm_hour * 60; | |
66 int threshold = gaim_prefs_get_int("/plugins/gtk/cap/threshold"); | |
67 int min_minute = (current_minute - threshold) % 1440; | |
68 int max_minute = (current_minute + threshold) % 1440; | |
69 dbi_result result; | |
70 | |
71 result = dbi_conn_queryf(_conn, "select success_count, failed_count from cap_msg_count where buddy=%s and account=%s and protocol=%s and minute_val>=%d and minute_val<=%d;", buddy_name, account_id, protocol_id, min_minute, max_minute); | |
72 if(result) { | |
73 int successes = 0; | |
74 int failures = 0; | |
75 while(dbi_result_next_row(result)) { | |
76 if(!dbi_result_field_is_null(result, "success_count")) | |
77 successes += dbi_result_get_int(result, "success_count"); | |
78 | |
79 if(!dbi_result_field_is_null(result, "failed_count")) | |
80 failures += dbi_result_get_int(result, "failed_count"); | |
81 } | |
82 gaim_debug_info("cap", "Successes = %d; Failures = %d\n", successes, failures); | |
83 if(failures + successes > 0) { | |
84 prediction *= ((double)successes/((double)(successes + failures))); | |
85 generated = TRUE; | |
86 } | |
87 gaim_debug_info("cap", "After message value prediction is %0.4f.\n", prediction); | |
88 dbi_result_free(result); | |
89 } | |
90 | |
91 | |
92 /* | |
93 * Note to self: apparently when using a function like sum results in not being able to get | |
94 * the values for fields using libdbi...why? the way I'm doing it above sucks, find a fix. | |
95 result = dbi_conn_queryf(_conn, "select sum(success_count) as successes, sum(failed_count) as failures from cap_msg_count where buddy=%s and account=%s and protocol=%s and minute_val>=%d and minute_val<=%d;", buddy_name, account_id, protocol_id, min_minute, max_minute); | |
96 gaim_debug_info("cap", "select sum(success_count) as successes, sum(failed_count) as failures from cap_msg_count where buddy=%s and account=%s and protocol=%s and minute_val>=%d and minute_val<=%d;\n", buddy_name, account_id, protocol_id, min_minute, max_minute); | |
97 | |
98 if(result) { | |
99 int failures; | |
100 int successes; | |
101 int rc = dbi_result_get_numrows(result); | |
102 dbi_result_next_row(result); | |
103 gaim_debug_info("cap", "Result code: %d\n", rc); | |
104 failures = dbi_result_get_int_idx(result, 1);//"failures"); | |
105 successes = dbi_result_get_int_idx(result, 2); //"successes"); | |
106 gaim_debug_info("cap", "Successes = %d; Failures = %d\n", successes, failures); | |
107 dbi_result_get_fields(result, "successes.%i failures.%i", &successes, &failures); | |
108 gaim_debug_info("cap", "Successes = %d; Failures = %d\n", successes, failures); | |
109 if(failures + successes > 0.0) | |
110 prediction *= ((double)successes/((double)(successes + failures))); | |
111 gaim_debug_info("cap", "After message value prediction is %0.4f.\n", prediction); | |
112 dbi_result_free(result); | |
113 } | |
114 */ | |
115 | |
116 result = dbi_conn_queryf(_conn, "select success_count, failed_count from cap_status_count where buddy=%s and account=%s and protocol=%s and status=%s;", buddy_name, account_id, protocol_id, status_id); | |
117 if(result) { | |
118 int successes = 0; | |
119 int failures = 0; | |
120 | |
121 dbi_result_next_row(result); | |
122 | |
123 if(!dbi_result_field_is_null(result, "success_count")) | |
124 successes = dbi_result_get_int(result, "success_count"); | |
125 | |
126 if(!dbi_result_field_is_null(result, "failed_count")) | |
127 failures = dbi_result_get_int(result, "failed_count"); | |
128 | |
129 gaim_debug_info("cap", "Successes = %d; Failures = %d\n", successes, failures); | |
130 if(successes + failures > 0) { | |
131 prediction *= ((double)successes/(double)(successes + failures)); | |
132 generated = TRUE; | |
133 } | |
134 gaim_debug_info("cap", "After status value prediction is %0.4f.\n", prediction); | |
135 dbi_result_free(result); | |
136 } | |
137 | |
138 free(buddy_name); | |
139 free(account_id); | |
140 free(protocol_id); | |
141 free(status_id); | |
142 | |
143 if(strcmp(gaim_status_get_id(get_status_for(buddy)), "offline") == 0) { | |
144 //This is kind of stupid, change it. | |
145 gaim_debug_info("cap", "Buddy is offline.\n"); | |
146 if(prediction == 1.0f) | |
147 prediction = 0.0f; | |
148 } | |
149 | |
150 if(generated) | |
151 return prediction; | |
152 else | |
153 return -1; | |
154 } | |
155 | |
156 static CapStatistics * get_stats_for(GaimBuddy *buddy) { | |
157 gchar *buddy_name; | |
158 gchar *q_buddy_name = quote_string(buddy->name); | |
159 CapStatistics *stats; | |
160 buddy_name = g_strdup(buddy->name); | |
161 stats = g_hash_table_lookup(_buddy_stats, buddy_name); | |
162 if(!stats) { | |
163 dbi_result result; | |
164 gaim_debug_info("cap", "Creating stats for %s\n", buddy->name); | |
165 stats = g_malloc(sizeof(CapStatistics)); | |
166 stats->last_message = -1; | |
167 stats->last_message_status_id = NULL; | |
168 stats->last_status_id = NULL; | |
169 stats->prediction = NULL; | |
170 g_hash_table_insert(_buddy_stats, buddy_name, stats); | |
171 stats->buddy = buddy; | |
172 /* Setup the last seen online time from database or -1 if no time available. */ | |
173 result = dbi_conn_queryf(_conn, | |
174 //"select max(event_time) as last_event_time from cap_status where buddy=\'%s\' and status!=\'offline\';", | |
175 "select event_time from cap_status where buddy=\'%s\' and status!=\'offline\' order by event_time desc;", | |
176 q_buddy_name); | |
177 if(result && dbi_result_get_numrows(result) > 0) { | |
178 dbi_result_next_row(result); | |
179 stats->last_seen = dbi_result_get_datetime(result, "event_time"); | |
180 } else { | |
181 stats->last_seen = -1; | |
182 } | |
183 dbi_result_free(result); | |
184 /* Setup the last messaged time to be a 'useable' value for comparisons. */ | |
185 //result = dbi_conn_queryf(conn, "", ); | |
186 /* Setup the last status id to nothing */ | |
187 // --> Better approach would be to get the last status available in db and use it. | |
188 result = dbi_conn_queryf(_conn, | |
189 "select status, max(event_time) from cap_status where buddy=\'%s\' group by status;", | |
190 buddy->name); | |
191 if(result && dbi_result_get_numrows(result) > 0) { | |
192 dbi_result_next_row(result); | |
193 stats->last_status_id = dbi_result_get_string_copy(result, "status"); | |
194 } else { | |
195 stats->last_status_id = ""; | |
196 } | |
197 dbi_result_free(result); | |
198 //TODO: populate stats from database | |
199 } else { | |
200 g_free(buddy_name); | |
201 } | |
202 free(q_buddy_name); | |
203 generate_prediction(stats); | |
204 return stats; | |
205 } | |
206 | |
207 static void destroy_stats(gpointer data) { | |
208 CapStatistics *stats = data; | |
209 g_free(stats->prediction); | |
210 //g_free(stats->hourly_usage); | |
211 //g_free(stats->daily_usage); | |
212 g_free(stats); | |
213 } | |
214 | |
215 static gboolean remove_stats_for(GaimBuddy *buddy) { | |
216 gboolean success = TRUE; | |
217 //GString *buddy_name = g_string_new(buddy->name); | |
218 gchar *buddy_name = g_strdup(buddy->name); | |
219 success = g_hash_table_remove(_buddy_stats, buddy_name); | |
220 g_free(buddy_name); | |
221 return success; | |
222 } | |
223 | |
224 static dbi_result insert_cap_msg_count_success(const char *buddy_name, const char *account, const char *protocol, int minute) { | |
225 gaim_debug_info("cap", "Insert cap_msg_count success: %s %s %s %d\n", buddy_name, account, protocol, minute); | |
226 return dbi_conn_queryf(_conn, "insert into cap_msg_count (buddy, account, protocol, minute_val, success_count, failed_count) values (%s, %s, %s, %d, %d, %d) on duplicate key update success_count=success_count+1;", buddy_name, account, protocol, minute, 1, 0); | |
227 } | |
228 | |
229 static dbi_result insert_cap_status_count_success(const char *buddy_name, const char *account, const char *protocol, const char *status_id) { | |
230 gaim_debug_info("cap", "Insert cap_status_count success: %s %s %s %s\n", buddy_name, account, protocol, status_id); | |
231 return dbi_conn_queryf(_conn, "insert into cap_status_count (buddy, account, protocol, status, success_count, failed_count) values(%s, %s, %s, %s, %d, %d) on duplicate key update success_count=success_count+1;", buddy_name, account, protocol, status_id, 1, 0); | |
232 } | |
233 | |
234 static dbi_result insert_cap_msg_count_failed(const char *buddy_name, const char *account, const char *protocol, int minute) { | |
235 gaim_debug_info("cap", "Insert cap_msg_count failed: %s %s %s %d\n", buddy_name, account, protocol, minute); | |
236 return dbi_conn_queryf(_conn, "insert into cap_msg_count (buddy, account, protocol, minute_val, success_count, failed_count) values (%s, %s, %s, %d, %d, %d) on duplicate key update failed_count=failed_count+1;", buddy_name, account, protocol, minute, 0, 1); | |
237 } | |
238 | |
239 static dbi_result insert_cap_status_count_failed(const char *buddy_name, const char *account, const char *protocol, const char *status_id) { | |
240 gaim_debug_info("cap", "Insert cap_status_count failed: %s %s %s %s\n", buddy_name, account, protocol, status_id); | |
241 return dbi_conn_queryf(_conn, "insert into cap_status_count (buddy, account, protocol, status, success_count, failed_count) values(%s, %s, %s, %s, %d, %d) on duplicate key update failed_count=failed_count+1;", buddy_name, account, protocol, status_id, 0, 1); | |
242 } | |
243 | |
244 static void insert_cap_success(CapStatistics *stats) { | |
245 dbi_result result; | |
246 gchar *buddy_name = quote_string(stats->buddy->name); | |
247 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(stats->buddy->account)); | |
248 gchar *account_id = quote_string(gaim_account_get_username(stats->buddy->account)); | |
249 gchar *status_id = (stats->last_message_status_id) ? | |
250 quote_string(stats->last_message_status_id) : | |
251 quote_string(gaim_status_get_id(get_status_for(stats->buddy))); | |
252 struct tm *current_time; | |
253 int minute; | |
254 | |
255 if(stats->last_message == -1) { | |
256 time_t now = time(NULL); | |
257 current_time = localtime(&now); | |
258 } else { | |
259 current_time = localtime(&stats->last_message); | |
260 } | |
261 minute = current_time->tm_min + current_time->tm_hour * 60; | |
262 | |
263 result = insert_cap_msg_count_success(buddy_name, account_id, protocol_id, minute); | |
264 if(result) | |
265 dbi_result_free(result); | |
266 | |
267 result = insert_cap_status_count_success(buddy_name, account_id, protocol_id, status_id); | |
268 if(result) | |
269 dbi_result_free(result); | |
270 | |
271 stats->last_message = -1; | |
272 stats->last_message_status_id = NULL; | |
273 | |
274 free(status_id); | |
275 free(protocol_id); | |
276 free(account_id); | |
277 free(buddy_name); | |
278 } | |
279 | |
280 static void insert_cap_failure(CapStatistics *stats) { | |
281 dbi_result result; | |
282 gchar *buddy_name = quote_string(stats->buddy->name); | |
283 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(stats->buddy->account)); | |
284 gchar *account_id = quote_string(gaim_account_get_username(stats->buddy->account)); | |
285 gchar *status_id = (stats->last_message_status_id) ? | |
286 quote_string(stats->last_message_status_id) : | |
287 quote_string(gaim_status_get_id(get_status_for(stats->buddy))); | |
288 struct tm *current_time = localtime(&stats->last_message); | |
289 int minute = current_time->tm_min + current_time->tm_hour * 60; | |
290 | |
291 result = insert_cap_msg_count_failed(buddy_name, account_id, protocol_id, minute); | |
292 if(result) | |
293 dbi_result_free(result); | |
294 | |
295 result = insert_cap_status_count_failed(buddy_name, account_id, protocol_id, status_id); | |
296 if(result) | |
297 dbi_result_free(result); | |
298 | |
299 stats->last_message = -1; | |
300 stats->last_message_status_id = NULL; | |
301 | |
302 free(status_id); | |
303 free(protocol_id); | |
304 free(account_id); | |
305 free(buddy_name); | |
306 } | |
307 | |
308 static gboolean max_message_difference_cb(gpointer data) { | |
309 CapStatistics *stats = data; | |
310 gaim_debug_info("cap", "Max Message Difference timeout occured\n"); | |
311 insert_cap_failure(stats); | |
312 stats->timeout_source_id = 0; | |
313 return FALSE; | |
314 } | |
315 | |
316 /* Gaim Signal Handlers */ | |
317 | |
318 //sent-im-msg | |
319 static void sent_im_msg(GaimAccount *account, const char *receiver, const char *message) { | |
320 GaimBuddy *buddy = gaim_find_buddy(account, receiver); | |
321 guint interval = gaim_prefs_get_int("/plugins/gtk/cap/max_msg_difference") * 1000 * 60; | |
322 guint words = word_count(message); | |
323 CapStatistics *stats = get_stats_for(buddy); | |
324 | |
325 insert_word_count(gaim_account_get_username(account), receiver, words); | |
326 stats->last_message = time(NULL); | |
327 stats->last_message_status_id = gaim_status_get_id(get_status_for(buddy)); | |
328 if(stats->timeout_source_id != 0) | |
329 g_source_remove(stats->timeout_source_id); | |
330 | |
331 stats->timeout_source_id = g_timeout_add(interval, max_message_difference_cb, stats); | |
332 } | |
333 | |
334 //received-im-msg | |
335 static void received_im_msg(GaimAccount *account, char *sender, char *message, | |
336 GaimConversation *conv, GaimMessageFlags flags) { | |
337 GaimBuddy *buddy = gaim_find_buddy(account, sender); | |
338 guint words = word_count(message); | |
339 CapStatistics *stats = get_stats_for(buddy); | |
340 | |
341 //insert_word_count(sender, buddy_name, words); | |
342 | |
343 //If we are waiting for a response from a prior message | |
344 // then cancel the timeout callback. | |
345 if(stats->timeout_source_id != 0) { | |
346 gaim_debug_info("cap", "Cancelling timeout callback\n"); | |
347 g_source_remove(stats->timeout_source_id); | |
348 stats->timeout_source_id = 0; | |
349 } | |
350 | |
351 insert_cap_success(stats); | |
352 | |
353 stats->last_message = -1; //Reset the last_message value | |
354 stats->last_message_status_id = NULL; //Reset the last status id value | |
355 } | |
356 | |
357 //buddy-status-changed | |
358 static void buddy_status_changed(GaimBuddy *buddy, GaimStatus *old_status, GaimStatus *status) { | |
359 CapStatistics *stats = get_stats_for(buddy); | |
360 insert_status_change_from_gaim_status(stats, status); | |
361 } | |
362 | |
363 //buddy-signed-on | |
364 static void buddy_signed_on(GaimBuddy *buddy) { | |
365 CapStatistics *stats = get_stats_for(buddy); | |
366 | |
367 /* If the statistic object existed but doesn't have a buddy pointer associated | |
368 * with it then reassociate one with it. The pointer being null is a result | |
369 * of a buddy with existing stats signing off and Gaim sticking around. | |
370 */ | |
371 if(!stats->buddy) { | |
372 stats->buddy = buddy; | |
373 } | |
374 | |
375 insert_status_change(stats); | |
376 } | |
377 | |
378 //buddy-signed-off | |
379 static void buddy_signed_off(GaimBuddy *buddy) { | |
380 CapStatistics *stats = get_stats_for(buddy); | |
381 | |
382 /* We don't necessarily want to delete a buddies generated statistics every time they go offline. | |
383 * Instead we just set the buddy pointer to null so that when they come back online we can look | |
384 * them up again and continue using their statistics. | |
385 */ | |
386 insert_status_change(stats); | |
387 //stats->buddy = NULL; | |
388 stats->last_seen = time(NULL); | |
389 } | |
390 | |
391 static void buddy_idle(GaimBuddy *buddy, gboolean old_idle, gboolean idle) { | |
392 } | |
393 | |
394 static void blist_node_extended_menu(GaimBlistNode *node, GList **menu) { | |
395 GaimBuddy *buddy; | |
396 GaimMenuAction *menu_action; | |
397 gaim_debug_info("cap", "got extended blist menu\n"); | |
398 gaim_debug_info("cap", "is buddy: %d\n", GAIM_BLIST_NODE_IS_BUDDY(node)); | |
399 gaim_debug_info("cap", "is contact: %d\n", GAIM_BLIST_NODE_IS_CONTACT(node)); | |
400 gaim_debug_info("cap", "is group: %d\n", GAIM_BLIST_NODE_IS_GROUP(node)); | |
401 /* Probably only concerned with buddy/contact types. Contacts = meta-buddies (grouped msn/jabber/etc.) */ | |
402 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
403 buddy = (GaimBuddy *)node; | |
404 menu_action = gaim_menu_action_new(_("Display Statistics"), | |
405 GAIM_CALLBACK(display_statistics_action_cb), NULL, NULL); | |
406 *menu = g_list_append(*menu, menu_action); | |
407 } | |
408 | |
409 //drawing-tooltip | |
410 static void drawing_tooltip(GaimBlistNode *node, GString *text, gboolean full) { | |
411 if(node->type == GAIM_BLIST_BUDDY_NODE) { | |
412 GaimBuddy *buddy = (GaimBuddy *)node; | |
413 CapStatistics *stats = get_stats_for(buddy); | |
414 // get the probability that this buddy will respond and add to the tooltip | |
415 if(stats->prediction->probability >= 0.0) { | |
416 g_string_append_printf(text, "\n<b>%s</b> %0.4f", _("Response Probability:"), stats->prediction->probability); | |
417 } else { | |
418 g_string_append_printf(text, "\n<b>%s</b> ???", _("Response Probability:")); | |
419 } | |
420 } | |
421 } | |
422 | |
423 //signed-on | |
424 static void signed_on(GaimConnection *gc) { | |
425 GaimAccount *account = gaim_connection_get_account(gc); | |
426 const char *my_gaim_name = gaim_account_get_username(account); | |
427 gchar *my_name = g_strdup(my_gaim_name); | |
428 time_t *last_offline = g_hash_table_lookup(_my_offline_times, my_name); | |
429 | |
430 gchar *account_id = quote_string(gaim_account_get_username(account)); | |
431 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(account)); | |
432 dbi_result result; | |
433 | |
434 result = dbi_conn_queryf(_conn, "insert into cap_my_usage values(%s, %s, %d, now());", account_id, protocol_id, 1); | |
435 if(!result) | |
436 gaim_debug_error("cap", "Could not insert sign on into cap_my_usage\n"); | |
437 else | |
438 dbi_result_free(result); | |
439 | |
440 if(last_offline) { | |
441 if(difftime(*last_offline, time(NULL)) > gaim_prefs_get_int("/plugins/gtk/cap/max_seen_difference") * 60) { | |
442 //reset all of the last_message times to -1 | |
443 g_hash_table_foreach(_my_offline_times, reset_all_last_message_times, NULL); | |
444 } | |
445 g_hash_table_remove(_my_offline_times, my_name); | |
446 } | |
447 g_free(my_name); | |
448 } | |
449 | |
450 //signed-off | |
451 static void signed_off(GaimConnection *gc) { | |
452 /* Here we record the time you (the user) sign off of an account. | |
453 * The account username is the key in the hashtable and the sign off time_t | |
454 * (equal to the sign off time) is the value. */ | |
455 GaimAccount *account = gaim_connection_get_account(gc); | |
456 const char *my_gaim_name = gaim_account_get_username(account); | |
457 gchar *my_name = g_strdup(my_gaim_name); | |
458 time_t *offline_time = g_malloc(sizeof(time_t)); | |
459 gchar *account_id = quote_string(gaim_account_get_username(account)); | |
460 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(account)); | |
461 dbi_result result; | |
462 | |
463 result = dbi_conn_queryf(_conn, "insert into cap_my_usage values(%s, %s, %d, now());", account_id, protocol_id, 0); | |
464 if(!result) | |
465 gaim_debug_error("cap", "Could not insert sign off into cap_my_usage\n"); | |
466 else | |
467 dbi_result_free(result); | |
468 | |
469 time(offline_time); | |
470 g_hash_table_insert(_my_offline_times, my_name, offline_time); | |
471 } | |
472 | |
473 static const gchar * get_error_msg() { | |
474 if(error_msg) | |
475 return error_msg->str; | |
476 else | |
477 return NULL; | |
478 } | |
479 | |
480 static void set_error_msg(const gchar *msg) { | |
481 if(!error_msg) | |
482 error_msg = g_string_new(msg); | |
483 else | |
484 g_string_assign(error_msg, msg); | |
485 } | |
486 | |
487 static void append_error_msg(const gchar *msg) { | |
488 if(!error_msg) | |
489 set_error_msg(msg); | |
490 else | |
491 g_string_append(error_msg, msg); | |
492 } | |
493 | |
494 static void reset_all_last_message_times(gpointer key, gpointer value, gpointer user_data) { | |
495 CapStatistics *stats = value; | |
496 stats->last_message = -1; | |
497 } | |
498 | |
499 static GaimStatus * get_status_for(GaimBuddy *buddy) { | |
500 GaimPresence *presence = gaim_buddy_get_presence(buddy); | |
501 GaimStatus *status = gaim_presence_get_active_status(presence); | |
502 return status; | |
503 } | |
504 | |
505 static void create_tables() { | |
506 } | |
507 | |
508 static gboolean create_database_connection() { | |
509 int rc; | |
510 int driver_type; | |
511 //make database connection here | |
512 /* | |
513 * conn = dbi_conn_new("mysql"); | |
514 * dbi_conn_set_option(conn, "host", "localhost"); | |
515 * dbi_conn_set_option(conn, "username", "root"); | |
516 * dbi_conn_set_option(conn, "password", "b1qywm96"); | |
517 * dbi_conn_set_option(conn, "dbname", "mysql"); | |
518 * dbi_conn_set_option(conn, "encoding", "auto"); | |
519 */ | |
520 _conn = dbi_conn_new(gaim_prefs_get_string("/plugins/gtk/cap/db_driver")); | |
521 _driver = dbi_conn_get_driver(_conn); | |
522 gaim_debug_info("cap", "Using driver: %s\n", gaim_prefs_get_string("/plugins/gtk/cap/db_driver")); | |
523 if(strcmp(gaim_prefs_get_string("/plugins/gtk/cap/db_driver"), "mysql") == 0) { | |
524 driver_type = MYSQL; | |
525 dbi_conn_set_option(_conn, "host", gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_host")); | |
526 dbi_conn_set_option(_conn, "username", gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_user")); | |
527 dbi_conn_set_option(_conn, "password", gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_password")); | |
528 dbi_conn_set_option(_conn, "dbname", gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_name")); | |
529 dbi_conn_set_option(_conn, "encoding", "auto"); | |
530 dbi_conn_set_option_numeric(_conn, "port", gaim_prefs_get_int("/plugins/gtk/cap/mysql/db_port")); | |
531 } | |
532 if(dbi_conn_connect(_conn) < 0) { | |
533 const char *err_msg = ""; | |
534 //rc = dbi_conn_error(_conn, &err_msg); | |
535 rc = dbi_conn_error(_conn, NULL); | |
536 gaim_debug_error("cap", "CAP could not create database connection. %d\n", rc); | |
537 //set_error_msg(_("Could not create database connection. Reason: ")); | |
538 //append_error_msg(err_msg); | |
539 return FALSE; | |
540 } else { | |
541 //Add tables here | |
542 create_tables(); | |
543 } | |
544 gaim_debug_info("cap", "Database connection successfully made.\n"); | |
545 return TRUE; | |
546 } | |
547 | |
548 static guint word_count(const gchar *string) { | |
549 //TODO: doesn't really work, should use regex instead (#include <regex.h>) | |
550 gchar **result = g_strsplit_set(string, " ", -1); | |
551 guint count = g_strv_length(result); | |
552 | |
553 g_strfreev(result); | |
554 | |
555 return count; | |
556 } | |
557 | |
558 /* If the difference in time between the present time and the time that | |
559 * you last sent a message to a buddy is less than some value then return | |
560 * true, otherwise return false. | |
561 * The difference can either be + or - the max_difference. | |
562 * max_difference is in seconds | |
563 */ | |
564 static gboolean last_message_time_in_range(CapStatistics *statistics, gdouble max_difference) { | |
565 time_t now = time(NULL); | |
566 gdouble difference = 0.0; | |
567 //If there is no last_message time then it is considered in range | |
568 if(statistics->last_message == -1) { | |
569 return TRUE; | |
570 } | |
571 //Compute the difference between now and the last_message | |
572 difference = difftime(statistics->last_message, now); | |
573 //Absolute value | |
574 difference = (difference < 0.0) ? -difference : difference; | |
575 //If the difference is less than the maximum then we are good and its in range, otherwise not | |
576 if(difference <= max_difference) | |
577 return TRUE; | |
578 else | |
579 return FALSE; | |
580 } | |
581 | |
582 static gboolean last_seen_time_in_range(CapStatistics *statistics, gdouble max_difference) { | |
583 time_t now = time(NULL); | |
584 gdouble difference = 0.0; | |
585 if(statistics->last_seen == -1) | |
586 return FALSE; | |
587 difference = difftime(statistics->last_seen, now); | |
588 difference = (difference < 0.0) ? -difference : difference; | |
589 if(difference < max_difference) | |
590 return TRUE; | |
591 else | |
592 return FALSE; | |
593 } | |
594 | |
595 static void insert_status_change(CapStatistics *statistics) { | |
596 insert_status_change_from_gaim_status(statistics, get_status_for(statistics->buddy)); | |
597 } | |
598 | |
599 static void insert_status_change_from_gaim_status(CapStatistics *statistics, GaimStatus *status) { | |
600 dbi_result result; | |
601 gchar *status_id = quote_string(gaim_status_get_id(status)); | |
602 gchar *buddy_name = quote_string(statistics->buddy->name); | |
603 gchar *protocol_id = quote_string(gaim_account_get_protocol_id(statistics->buddy->account)); | |
604 gchar *account_id = quote_string(gaim_account_get_username(statistics->buddy->account)); | |
605 | |
606 statistics->last_status_id = gaim_status_get_id(status); | |
607 gaim_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); | |
608 | |
609 result = dbi_conn_queryf(_conn, "insert into cap_status (buddy, account, protocol, status, event_time) values(%s, %s, %s, %s, now());", buddy_name, account_id, protocol_id, status_id); | |
610 if(result) | |
611 dbi_result_free(result); | |
612 else { | |
613 const char *err = ""; | |
614 dbi_conn_error(_conn, &err); | |
615 gaim_debug_error("cap", "Could not insert status change event into database. %s\n", err); | |
616 } | |
617 | |
618 free(status_id); | |
619 free(buddy_name); | |
620 free(protocol_id); | |
621 free(account_id); | |
622 } | |
623 | |
624 static void insert_word_count(const char *sender, const char *receiver, guint count) { | |
625 //TODO! | |
626 //dbi_result result; | |
627 //result = dbi_conn_queryf(_conn, "insert into cap_message values(\'%s\', \'%s\', %d, now());", sender, receiver, count); | |
628 } | |
629 | |
630 /* Callbacks */ | |
631 void display_statistics_action_cb(GaimBlistNode *node, gpointer data) { | |
632 GaimBuddy *buddy; | |
633 | |
634 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
635 buddy = (GaimBuddy *)node; | |
636 gaim_debug_info("cap", "Statistics for %s requested.\n", buddy->name); | |
637 } | |
638 | |
639 /* Gaim plugin specific code */ | |
640 | |
641 static gboolean plugin_load(GaimPlugin *plugin) { | |
642 dbi_driver driver = dbi_driver_list(NULL); | |
643 gboolean drivers_available = FALSE; | |
644 int rc = dbi_initialize(gaim_prefs_get_string("/plugins/gtk/cap/libdbi_drivers")); | |
645 | |
646 if(rc == -1) { | |
647 gaim_debug_error("cap", "Error initializing dbi.\n"); | |
648 set_error_msg(_("Error initializing libdbi.")); | |
649 return FALSE; | |
650 } | |
651 | |
652 _plugin_pointer = plugin; | |
653 _signals_connected = FALSE; | |
654 | |
655 while(driver != NULL) { | |
656 gaim_debug_info("cap", "Located driver: %s\n", dbi_driver_get_name(driver)); | |
657 if(strcmp("mysql", dbi_driver_get_name(driver)) == 0) | |
658 drivers_available = TRUE; | |
659 | |
660 driver = dbi_driver_list(driver); | |
661 } | |
662 if(!drivers_available) | |
663 return FALSE; | |
664 | |
665 | |
666 if(gaim_prefs_get_bool("/plugins/gtk/cap/configured")) { | |
667 if(!add_plugin_functionality(plugin)) { | |
668 return FALSE; | |
669 } | |
670 } | |
671 return TRUE; | |
672 } | |
673 | |
674 static gboolean add_plugin_functionality(GaimPlugin *plugin) { | |
675 if(_signals_connected) | |
676 return TRUE; | |
677 | |
678 if(!create_database_connection()) { | |
679 /* | |
680 GtkWidget *dialog = gtk_message_dialog_new("title", | |
681 GTK_DIALOG_DESTROY_WITH_PARENT, | |
682 GTK_MESSAGE_ERROR, | |
683 GTK_BUTTONS_CLOSE, | |
684 get_error_msg()); | |
685 gtk_dialog_run(GTK_DIALOG(dialog)); | |
686 gtk_widget_destroy(dialog); | |
687 */ | |
688 return FALSE; | |
689 } | |
690 | |
691 /* buddy_stats is a hashtable where strings are keys | |
692 * and the keys are a buddies account id (GaimBuddy.name). | |
693 * keys/values are automatically deleted */ | |
694 _buddy_stats = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, destroy_stats); | |
695 | |
696 _my_offline_times = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
697 | |
698 /* Connect all the signals */ | |
699 gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg", plugin, | |
700 GAIM_CALLBACK(sent_im_msg), NULL); | |
701 | |
702 gaim_signal_connect(gaim_conversations_get_handle(), "received-im-msg", plugin, | |
703 GAIM_CALLBACK(received_im_msg), NULL); | |
704 | |
705 gaim_signal_connect(gaim_blist_get_handle(), "buddy-status-changed", plugin, | |
706 GAIM_CALLBACK(buddy_status_changed), NULL); | |
707 | |
708 gaim_signal_connect(gaim_blist_get_handle(), "buddy-signed-on", plugin, | |
709 GAIM_CALLBACK(buddy_signed_on), NULL); | |
710 | |
711 gaim_signal_connect(gaim_blist_get_handle(), "buddy-signed-off", plugin, | |
712 GAIM_CALLBACK(buddy_signed_off), NULL); | |
713 | |
714 //gaim_signal_connect(gaim_blist_get_handle(), "blist-node-extended-menu", plugin, | |
715 // GAIM_CALLBACK(blist_node_extended_menu), NULL); | |
716 | |
717 gaim_signal_connect(gaim_gtk_blist_get_handle(), "drawing-tooltip", plugin, | |
718 GAIM_CALLBACK(drawing_tooltip), NULL); | |
719 | |
720 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", plugin, | |
721 GAIM_CALLBACK(signed_on), NULL); | |
722 | |
723 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", plugin, | |
724 GAIM_CALLBACK(signed_off), NULL); | |
725 | |
726 gaim_signal_connect(gaim_blist_get_handle(), "buddy-idle-changed", plugin, | |
727 GAIM_CALLBACK(buddy_idle), NULL); | |
728 | |
729 _signals_connected = TRUE; | |
730 | |
731 return TRUE; | |
732 } | |
733 | |
734 static void write_stats_on_unload(gpointer key, gpointer value, gpointer user_data) { | |
735 CapStatistics *stats = value; | |
736 gaim_debug_info("cap", "Unloading, last message time %d\n", stats->last_message); | |
737 if(stats->last_message != -1 && stats->buddy != NULL) { | |
738 insert_cap_failure(stats); | |
739 } | |
740 } | |
741 | |
742 static gboolean plugin_unload(GaimPlugin *plugin) { | |
743 gaim_debug_info("cap", "CAP plugin unloading\n"); | |
744 //TODO: foreach stats object in hashtable: | |
745 // if stats->last_message != -1 then update for that time the failed counter...maybe? | |
746 g_hash_table_foreach(_buddy_stats, write_stats_on_unload, NULL); | |
747 //clean up memory allocations | |
748 if(_buddy_stats) | |
749 g_hash_table_destroy(_buddy_stats); | |
750 /*while(g_list_length(plugin_actions) > 0) { | |
751 GaimPluginAction *action = g_list_first(plugin_actions)->data; | |
752 gaim_plugin_action_free(action); | |
753 } | |
754 _plugin_pointer = NULL;*/ | |
755 | |
756 //close database connection | |
757 dbi_conn_close(_conn); | |
758 dbi_shutdown(); | |
759 | |
760 return TRUE; | |
761 } | |
762 | |
763 static GtkWidget * get_config_frame(GaimPlugin *plugin) { | |
764 GtkWidget *ret; | |
765 GtkWidget *vbox; | |
766 | |
767 GtkWidget *driver_vbox; | |
768 GtkWidget *driver_select_hbox; | |
769 GtkWidget *driver_choice; | |
770 GtkWidget *driver_label; | |
771 GtkWidget *driver_config_hbox; | |
772 GtkWidget *driver_config; | |
773 | |
774 GtkWidget *threshold_label; | |
775 GtkWidget *threshold_input; | |
776 GtkWidget *threshold_hbox; | |
777 GtkWidget *threshold_minutes_label; | |
778 | |
779 GtkWidget *msg_difference_label; | |
780 GtkWidget *msg_difference_input; | |
781 GtkWidget *msg_difference_hbox; | |
782 GtkWidget *msg_difference_minutes_label; | |
783 | |
784 GtkWidget *last_seen_label; | |
785 GtkWidget *last_seen_input; | |
786 GtkWidget *last_seen_hbox; | |
787 GtkWidget *last_seen_minutes_label; | |
788 | |
789 GtkWidget *dbd_label; | |
790 GtkWidget *dbd_input; | |
791 GtkWidget *dbd_hbox; | |
792 | |
793 ret = gtk_vbox_new(FALSE, 18); | |
794 g_signal_connect(G_OBJECT(ret), "destroy", | |
795 G_CALLBACK(prefs_closed_cb), "/plugins/gtk/cap/configured"); | |
796 gtk_container_set_border_width(GTK_CONTAINER(ret), 10); | |
797 vbox = gaim_gtk_make_frame(ret, _("Contact Availability Prediction Configuration")); | |
798 | |
799 driver_vbox = gtk_vbox_new(FALSE, 18); | |
800 //Driver selection and configuration | |
801 driver_choice = gtk_combo_box_new_text(); | |
802 g_signal_connect(G_OBJECT(driver_choice), "changed", | |
803 G_CALLBACK(combobox_prefs_cb), "/plugins/gtk/cap/db_driver"); | |
804 gtk_combo_box_append_text(GTK_COMBO_BOX(driver_choice), _("mysql")); | |
805 gtk_combo_box_set_active(GTK_COMBO_BOX(driver_choice), 0); | |
806 //gtk_combo_box_append_text(GTK_COMBO_BOX(driver_choice), _("pgsql")); | |
807 //gtk_combo_box_append_text(GTK_COMBO_BOX(driver_choice), _("sqlite")); | |
808 driver_label = gtk_label_new(_("Driver:")); | |
809 driver_select_hbox = gtk_hbox_new(FALSE, 18); | |
810 if(gaim_prefs_exists("/plugins/gtk/cap/db_driver")) { | |
811 const char *driver_string = gaim_prefs_get_string("/plugins/gtk/cap/db_driver"); | |
812 if(g_ascii_strcasecmp(driver_string, "mysql") == 0) { | |
813 gtk_combo_box_set_active(GTK_COMBO_BOX(driver_choice), 0); | |
814 } | |
815 } | |
816 gtk_box_pack_start(GTK_BOX(driver_select_hbox), driver_label, FALSE, FALSE, 0); | |
817 gtk_box_pack_start(GTK_BOX(driver_select_hbox), driver_choice, FALSE, FALSE, 0); | |
818 | |
819 driver_config = gtk_expander_new_with_mnemonic(_("_Configure")); | |
820 g_signal_connect(driver_config, "notify::expanded", | |
821 G_CALLBACK(driver_config_expanded), gtk_combo_box_get_active_text(GTK_COMBO_BOX(driver_choice))); | |
822 driver_config_hbox = gtk_hbox_new(FALSE, 18); | |
823 gtk_box_pack_start(GTK_BOX(driver_config_hbox), driver_config, FALSE, FALSE, 0); | |
824 | |
825 gtk_box_pack_start(GTK_BOX(driver_vbox), driver_select_hbox, FALSE, FALSE, 0); | |
826 gtk_box_pack_start(GTK_BOX(driver_vbox), driver_config_hbox, FALSE, FALSE, 0); | |
827 | |
828 //msg_difference | |
829 msg_difference_label = gtk_label_new(_("Maximum response timeout:")); | |
830 //FIXME: better maximum value | |
831 msg_difference_input = gtk_spin_button_new_with_range(1, 1440, 1); | |
832 msg_difference_hbox = gtk_hbox_new(FALSE, 18); | |
833 msg_difference_minutes_label = gtk_label_new(_("minutes")); | |
834 if(gaim_prefs_exists("/plugins/gtk/cap/max_msg_difference")) { | |
835 int max_msg_diff = gaim_prefs_get_int("/plugins/gtk/cap/max_msg_difference"); | |
836 gtk_spin_button_set_value(GTK_SPIN_BUTTON(msg_difference_input), max_msg_diff); | |
837 } | |
838 g_signal_connect(G_OBJECT(msg_difference_input), "value-changed", | |
839 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/max_msg_difference"); | |
840 gtk_box_pack_start(GTK_BOX(msg_difference_hbox), msg_difference_label, FALSE, FALSE, 0); | |
841 gtk_box_pack_start(GTK_BOX(msg_difference_hbox), msg_difference_input, FALSE, FALSE, 0); | |
842 gtk_box_pack_start(GTK_BOX(msg_difference_hbox), msg_difference_minutes_label, FALSE, FALSE, 0); | |
843 | |
844 //last_seen | |
845 last_seen_label = gtk_label_new(_("Maximum last-seen difference:")); | |
846 last_seen_input = gtk_spin_button_new_with_range(1, 1440, 1); | |
847 last_seen_hbox = gtk_hbox_new(FALSE, 18); | |
848 last_seen_minutes_label = gtk_label_new(_("minutes")); | |
849 if(gaim_prefs_exists("/plugins/gtk/cap/max_seen_difference")) { | |
850 int max_seen_diff = gaim_prefs_get_int("/plugins/gtk/cap/max_seen_difference"); | |
851 gtk_spin_button_set_value(GTK_SPIN_BUTTON(last_seen_input), max_seen_diff); | |
852 } | |
853 g_signal_connect(G_OBJECT(last_seen_input), "value-changed", | |
854 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/max_seen_difference"); | |
855 gtk_box_pack_start(GTK_BOX(last_seen_hbox), last_seen_label, FALSE, FALSE, 0); | |
856 gtk_box_pack_start(GTK_BOX(last_seen_hbox), last_seen_input, FALSE, FALSE, 0); | |
857 gtk_box_pack_start(GTK_BOX(last_seen_hbox), last_seen_minutes_label, FALSE, FALSE, 0); | |
858 | |
859 //threshold | |
860 threshold_label = gtk_label_new(_("Threshold:")); | |
861 threshold_input = gtk_spin_button_new_with_range(1, 1440, 1); | |
862 threshold_hbox = gtk_hbox_new(FALSE, 18); | |
863 threshold_minutes_label = gtk_label_new(_("minutes")); | |
864 if(gaim_prefs_exists("/plugins/gtk/cap/threshold")) { | |
865 int threshold = gaim_prefs_get_int("/plugins/gtk/cap/threshold"); | |
866 gtk_spin_button_set_value(GTK_SPIN_BUTTON(threshold_input), threshold); | |
867 } | |
868 g_signal_connect(G_OBJECT(threshold_input), "value-changed", | |
869 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/threshold"); | |
870 gtk_box_pack_start(GTK_BOX(threshold_hbox), threshold_label, FALSE, FALSE, 0); | |
871 gtk_box_pack_start(GTK_BOX(threshold_hbox), threshold_input, FALSE, FALSE, 0); | |
872 gtk_box_pack_start(GTK_BOX(threshold_hbox), threshold_minutes_label, FALSE, FALSE, 0); | |
873 | |
874 //dbd path input | |
875 dbd_label = gtk_label_new(_("libdbi driver path:")); | |
876 dbd_input = gtk_entry_new(); | |
877 if(gaim_prefs_exists("/plugins/gtk/cap/libdbi_drivers")) { | |
878 gtk_entry_set_text(GTK_ENTRY(dbd_input), gaim_prefs_get_string("/plugins/gtk/cap/libdbi_drivers")); | |
879 } | |
880 dbd_hbox = gtk_hbox_new(FALSE, 18); | |
881 g_signal_connect(G_OBJECT(dbd_input), "focus-out-event", | |
882 G_CALLBACK(text_entry_prefs_cb), "/plugins/gtk/cap/libdbi_drivers"); | |
883 gtk_box_pack_start(GTK_BOX(dbd_hbox), dbd_label, FALSE, FALSE, 0); | |
884 gtk_box_pack_start(GTK_BOX(dbd_hbox), dbd_input, FALSE, FALSE, 0); | |
885 | |
886 //Config window | |
887 gtk_box_pack_start(GTK_BOX(vbox), driver_vbox, FALSE, FALSE, 0); | |
888 gtk_box_pack_start(GTK_BOX(vbox), msg_difference_hbox, FALSE, FALSE, 0); | |
889 gtk_box_pack_start(GTK_BOX(vbox), last_seen_hbox, FALSE, FALSE, 0); | |
890 gtk_box_pack_start(GTK_BOX(vbox), threshold_hbox, FALSE, FALSE, 0); | |
891 gtk_box_pack_start(GTK_BOX(vbox), dbd_hbox, FALSE, FALSE, 0); | |
892 | |
893 return ret; | |
894 } | |
895 | |
896 static void numeric_spinner_prefs_cb(GtkSpinButton *spinbutton, gpointer user_data) { | |
897 gaim_prefs_set_int(user_data, gtk_spin_button_get_value_as_int(spinbutton)); | |
898 } | |
899 | |
900 static gboolean text_entry_prefs_cb(GtkWidget *widget, GdkEventFocus *event, gpointer user_data) { | |
901 gaim_prefs_set_string(user_data, gtk_entry_get_text(GTK_ENTRY(widget))); | |
902 return FALSE; | |
903 } | |
904 | |
905 static void combobox_prefs_cb(GtkComboBox *widget, gpointer user_data) { | |
906 gaim_prefs_set_string(user_data, gtk_combo_box_get_active_text(widget)); | |
907 } | |
908 | |
909 static void prefs_closed_cb(GtkObject *widget, gpointer user_data) { | |
910 gboolean successfully_configured = FALSE; | |
911 successfully_configured = add_plugin_functionality(_plugin_pointer); | |
912 gaim_debug_info("cap", "Configured? %s\n", ((successfully_configured) ? "yes" : "no")); | |
913 if(!successfully_configured) { | |
914 if(error_msg) { | |
915 GtkWidget *error_dialog = gtk_message_dialog_new( | |
916 NULL, | |
917 GTK_DIALOG_DESTROY_WITH_PARENT, | |
918 GTK_MESSAGE_ERROR, | |
919 GTK_BUTTONS_CLOSE, | |
920 "%s\n%s", | |
921 _("Contact Availability Prediction configuration error."), get_error_msg()); | |
922 gtk_dialog_run(GTK_DIALOG(error_dialog)); | |
923 gtk_widget_destroy(error_dialog); | |
924 } | |
925 } | |
926 | |
927 gaim_prefs_set_bool(user_data, successfully_configured); | |
928 } | |
929 | |
930 static GtkWidget * get_mysql_config() { | |
931 GtkWidget *config_area = gtk_table_new(5, 2, TRUE); | |
932 GtkWidget *username_label = gtk_label_new(_("Username:")); | |
933 GtkWidget *username_input = gtk_entry_new(); | |
934 GtkWidget *password_label = gtk_label_new(_("Password:")); | |
935 GtkWidget *password_input = gtk_entry_new(); | |
936 GtkWidget *host_label = gtk_label_new(_("Host:")); | |
937 GtkWidget *host_input = gtk_entry_new(); | |
938 GtkWidget *db_label = gtk_label_new(_("Database:")); | |
939 GtkWidget *db_input = gtk_entry_new(); | |
940 GtkWidget *port_label = gtk_label_new(_("Port:")); | |
941 GtkWidget *port_input = gtk_spin_button_new_with_range(0, 10000, 1); | |
942 | |
943 gtk_entry_set_visibility(GTK_ENTRY(password_input), FALSE); | |
944 | |
945 gtk_table_attach_defaults(GTK_TABLE(config_area), host_label, 0, 1, 0, 1); | |
946 gtk_table_attach_defaults(GTK_TABLE(config_area), host_input, 1, 2, 0, 1); | |
947 gtk_table_attach_defaults(GTK_TABLE(config_area), port_label, 0, 1, 1, 2); | |
948 gtk_table_attach_defaults(GTK_TABLE(config_area), port_input, 1, 2, 1, 2); | |
949 gtk_table_attach_defaults(GTK_TABLE(config_area), db_label, 0, 1, 2, 3); | |
950 gtk_table_attach_defaults(GTK_TABLE(config_area), db_input, 1, 2, 2, 3); | |
951 gtk_table_attach_defaults(GTK_TABLE(config_area), username_label, 0, 1, 3, 4); | |
952 gtk_table_attach_defaults(GTK_TABLE(config_area), username_input, 1, 2, 3, 4); | |
953 gtk_table_attach_defaults(GTK_TABLE(config_area), password_label, 0, 1, 4, 5); | |
954 gtk_table_attach_defaults(GTK_TABLE(config_area), password_input, 1, 2, 4, 5); | |
955 | |
956 //Initialize with data | |
957 if(gaim_prefs_exists("/plugins/gtk/cap/mysql/db_host")) { | |
958 gtk_entry_set_text(GTK_ENTRY(host_input), gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_host")); | |
959 } else { | |
960 gtk_entry_set_text(GTK_ENTRY(host_input), "localhost"); | |
961 } | |
962 if(gaim_prefs_exists("/plugins/gtk/cap/mysql/db_port")) { | |
963 gtk_spin_button_set_value(GTK_SPIN_BUTTON(port_input), gaim_prefs_get_int("/plugins/gtk/cap/mysql/db_port")); | |
964 } else { | |
965 gtk_spin_button_set_value(GTK_SPIN_BUTTON(port_input), 3306); | |
966 } | |
967 if(gaim_prefs_exists("/plugins/gtk/cap/mysql/db_user")) { | |
968 gtk_entry_set_text(GTK_ENTRY(username_input), gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_user")); | |
969 } else { | |
970 gtk_entry_set_text(GTK_ENTRY(username_input), "root"); | |
971 } | |
972 if(gaim_prefs_exists("/plugins/gtk/cap/mysql/db_password")) { | |
973 gtk_entry_set_text(GTK_ENTRY(password_input), gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_password")); | |
974 } else { | |
975 gtk_entry_set_text(GTK_ENTRY(password_input), ""); | |
976 } | |
977 if(gaim_prefs_exists("/plugins/gtk/cap/mysql/db_name")) { | |
978 gtk_entry_set_text(GTK_ENTRY(db_input), gaim_prefs_get_string("/plugins/gtk/cap/mysql/db_name")); | |
979 } else { | |
980 gtk_entry_set_text(GTK_ENTRY(db_input), "cap"); | |
981 } | |
982 | |
983 //Add callbacks | |
984 g_signal_connect(G_OBJECT(host_input), "focus-out-event", | |
985 G_CALLBACK(text_entry_prefs_cb), "/plugins/gtk/cap/mysql/db_host"); | |
986 g_signal_connect(G_OBJECT(port_input), "value-changed", | |
987 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/mysql/db_port"); | |
988 g_signal_connect(G_OBJECT(username_input), "focus-out-event", | |
989 G_CALLBACK(text_entry_prefs_cb), "/plugins/gtk/cap/mysql/db_user"); | |
990 g_signal_connect(G_OBJECT(password_input), "focus-out-event", | |
991 G_CALLBACK(text_entry_prefs_cb), "/plugins/gtk/cap/mysql/db_password"); | |
992 g_signal_connect(G_OBJECT(db_input), "focus-out-event", | |
993 G_CALLBACK(text_entry_prefs_cb), "/plugins/gtk/cap/mysql/db_name"); | |
994 | |
995 gtk_widget_show_all(config_area); | |
996 | |
997 return config_area; | |
998 } | |
999 | |
1000 static void driver_config_expanded(GObject *object, GParamSpec *param_spec, gpointer user_data) { | |
1001 GtkExpander *expander; | |
1002 gchar *driver = user_data; | |
1003 expander = GTK_EXPANDER(object); | |
1004 if(gtk_expander_get_expanded(expander)) { | |
1005 if(g_ascii_strcasecmp(driver, "mysql") == 0) { | |
1006 gtk_container_add(GTK_CONTAINER(expander), get_mysql_config()); | |
1007 } | |
1008 } else { | |
1009 gtk_container_remove(GTK_CONTAINER(expander), gtk_bin_get_child(GTK_BIN(expander))); | |
1010 } | |
1011 } | |
1012 | |
1013 static GaimGtkPluginUiInfo ui_info = { | |
1014 get_config_frame, | |
1015 0 /* page_num (reserved) */ | |
1016 }; | |
1017 | |
1018 static GaimPluginInfo info = { | |
1019 GAIM_PLUGIN_MAGIC, | |
1020 GAIM_MAJOR_VERSION, | |
1021 GAIM_MINOR_VERSION, | |
1022 GAIM_PLUGIN_STANDARD, /**< type */ | |
1023 GAIM_GTK_PLUGIN_TYPE, /**< ui_requirement */ | |
1024 0, /**< flags */ | |
1025 NULL, /**< dependencies */ | |
1026 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
1027 CAP_PLUGIN_ID, /**< id */ | |
1028 "Contact Availability Prediction", /**< name */ | |
1029 VERSION, /**< version */ | |
1030 N_("Contact Availability Prediction plugin."), /** summary */ | |
1031 N_("The contact availability plugin (cap) is used to display statistical information about buddies in a users contact list."), | |
1032 /** description */ | |
1033 "Geoffrey Foster <geoffrey.foster@gmail.com>", /**< author */ | |
1034 GAIM_WEBSITE, /**< homepage */ | |
1035 plugin_load, /**< load */ | |
1036 plugin_unload, /**< unload */ | |
1037 NULL, /**< destroy */ | |
1038 &ui_info, /**< ui_info */ | |
1039 NULL, /**< extra_info */ | |
1040 NULL, /**< prefs_info */ | |
1041 NULL | |
1042 }; | |
1043 | |
1044 static void init_plugin(GaimPlugin *plugin) { | |
1045 /* TODO: change this before distributing */ | |
1046 gaim_prefs_add_none("/plugins/gtk/cap"); | |
1047 gaim_prefs_add_int("/plugins/gtk/cap/max_seen_difference", 1); | |
1048 gaim_prefs_add_int("/plugins/gtk/cap/max_msg_difference", 10); | |
1049 gaim_prefs_add_int("/plugins/gtk/cap/threshold", 5); | |
1050 gaim_prefs_add_string("/plugins/gtk/cap/libdbi_drivers", "/usr/lib/dbd"); | |
1051 gaim_prefs_add_bool("/plugins/gtk/cap/configured", FALSE); | |
1052 gaim_prefs_add_string("/plugins/gtk/cap/db_driver", "mysql"); | |
1053 gaim_prefs_add_none("/plugins/gtk/cap/mysql"); | |
1054 gaim_prefs_add_string("/plugins/gtk/cap/mysql/db_host", "localhost"); | |
1055 gaim_prefs_add_int("/plugins/gtk/cap/mysql/db_port", 3306); | |
1056 gaim_prefs_add_string("/plugins/gtk/cap/mysql/db_user", "root"); | |
1057 gaim_prefs_add_string("/plugins/gtk/cap/mysql/db_password", ""); | |
1058 gaim_prefs_add_string("/plugins/gtk/cap/mysql/db_name", "cap"); | |
1059 } | |
1060 | |
1061 GAIM_INIT_PLUGIN(cap, init_plugin, info); |