1 /*
2  * Contact Availability Prediction plugin for Purple
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., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02111-1301, USA.
20  */
21 
22 #ifndef _CAP_H_
23 #define _CAP_H_
24 
25 #include "internal.h"
26 #include "pidgin.h"
27 
28 #include "conversation.h"
29 
30 #include "gtkconv.h"
31 #include "gtkblist.h"
32 #include "gtkplugin.h"
33 #include "gtkutils.h"
34 
35 #include "blist.h"
36 #include "notify.h"
37 #include "version.h"
38 #include "debug.h"
39 
40 #include "util.h"
41 
42 #include <glib.h>
43 #include <time.h>
44 #include <sqlite3.h>
45 #include "cap_statistics.h"
46 
47 #define CAP_PLUGIN_ID "gtk-g-off_-cap"
48 
49 /* Variables used throughout lifetime of the plugin */
50 PurplePlugin *_plugin_pointer;
51 sqlite3 *_db; /**< The database */
52 
53 GHashTable *_buddy_stats = NULL;
54 GHashTable *_my_offline_times = NULL;
55 gboolean _signals_connected;
56 gboolean _sqlite_initialized;
57 
58 /* Prefs UI */
59 typedef struct _CapPrefsUI CapPrefsUI;
60 
61 struct _CapPrefsUI {
62 	GtkWidget *ret;
63 	GtkWidget *cap_vbox;
64 	GtkWidget *table_layout;
65 
66 	GtkWidget *threshold_label;
67 	GtkWidget *threshold_input;
68 	GtkWidget *threshold_minutes_label;
69 
70 	GtkWidget *msg_difference_label;
71 	GtkWidget *msg_difference_input;
72 	GtkWidget *msg_difference_minutes_label;
73 
74 	GtkWidget *last_seen_label;
75 	GtkWidget *last_seen_input;
76 	GtkWidget *last_seen_minutes_label;
77 };
78 
79 static void generate_prediction(CapStatistics *statistics);
80 static double generate_prediction_for(PurpleBuddy *buddy);
81 static CapStatistics * get_stats_for(PurpleBuddy *buddy);
82 static void destroy_stats(gpointer data);
83 static void insert_cap_msg_count_success(const char *buddy_name, const char *account, const char *protocol, int minute);
84 static void insert_cap_status_count_success(const char *buddy_name, const char *account, const char *protocol, const char *status_id);
85 static void insert_cap_msg_count_failed(const char *buddy_name, const char *account, const char *protocol, int minute);
86 static void insert_cap_status_count_failed(const char *buddy_name, const char *account, const char *protocol, const char *status_id);
87 static void insert_cap_success(CapStatistics *stats);
88 static void insert_cap_failure(CapStatistics *stats);
89 static gboolean max_message_difference_cb(gpointer data);
90 /* Pidgin Signal Handlers */
91 /* sent-im-msg */
92 static void sent_im_msg(PurpleAccount *account, const char *receiver, const char *message);
93 /* received-im-msg */
94 static void received_im_msg(PurpleAccount *account, char *sender, char *message, PurpleConversation *conv, PurpleMessageFlags flags);
95 /* buddy-status-changed */
96 static void buddy_status_changed(PurpleBuddy *buddy, PurpleStatus *old_status, PurpleStatus *status);
97 /* buddy-signed-on */
98 static void buddy_signed_on(PurpleBuddy *buddy);
99 /* buddy-signed-off */
100 static void buddy_signed_off(PurpleBuddy *buddy);
101 /* drawing-tooltip */
102 static void drawing_tooltip(PurpleBlistNode *node, GString *text, gboolean full);
103 /* signed-on */
104 static void signed_on(PurpleConnection *gc);
105 /* signed-off */
106 static void signed_off(PurpleConnection *gc);
107 static void reset_all_last_message_times(gpointer key, gpointer value, gpointer user_data);
108 static PurpleStatus * get_status_for(PurpleBuddy *buddy);
109 static void create_tables(void);
110 static gboolean create_database_connection(void);
111 static void destroy_database_connection(void);
112 static guint word_count(const gchar *string);
113 static void insert_status_change(CapStatistics *statistics);
114 static void insert_status_change_from_purple_status(CapStatistics *statistics, PurpleStatus *status);
115 static void insert_word_count(const char *sender, const char *receiver, guint count);
116 static gboolean plugin_load(PurplePlugin *plugin);
117 static void add_plugin_functionality(PurplePlugin *plugin);
118 static void cancel_conversation_timeouts(gpointer key, gpointer value, gpointer user_data);
119 static void remove_plugin_functionality(PurplePlugin *plugin);
120 static void write_stats_on_unload(gpointer key, gpointer value, gpointer user_data);
121 static gboolean plugin_unload(PurplePlugin *plugin);
122 static CapPrefsUI * create_cap_prefs_ui(void);
123 static void cap_prefs_ui_destroy_cb(GtkObject *object, gpointer user_data);
124 static void numeric_spinner_prefs_cb(GtkSpinButton *spinbutton, gpointer user_data);
125 static GtkWidget * get_config_frame(PurplePlugin *plugin);
126 static void init_plugin(PurplePlugin *plugin);
127 
128 #endif
129