comparison plugins/timestamp.c @ 9191:06b28fb24300

[gaim-migrate @ 9986] " This patch was inspired by Robert Story's previous timestamp patch (#944943). That was rejected because of a timing inconsistency issue mentioned by Faceprint. This patch disables timestamps in a given conversation when no messages have been displayed since the last timestamp. When a new message is about to be displayed in a disabled timestamp conversation, a timestamp is inserted first to maintain timing consistency. Then the timestamp display is reenabled and the IM message is printed. This patch also handles a bug in the original timestamp plugin. Previously, when the timestamp interval was modified in the preferences, no current open conversations are affected. I have modified it so that all open conversations use the new interval. I would have sent this as a separate patch, but this is my first patch and didn't want to mess it up :)." --Eddie Sohn i liked the original patch and was somewhat disappointed that it didn't get fixed to address Nathan's concern, so i'm happy to merge this one in. committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Sun, 06 Jun 2004 02:08:57 +0000
parents 294ae6548d4e
children 0e3a84f18467
comparison
equal deleted inserted replaced
9190:9e3289499977 9191:06b28fb24300
36 #define TIMESTAMP_PLUGIN_ID "gtk-timestamp" 36 #define TIMESTAMP_PLUGIN_ID "gtk-timestamp"
37 37
38 /* Set the default to 5 minutes. */ 38 /* Set the default to 5 minutes. */
39 static int interval = 5 * 60 * 1000; 39 static int interval = 5 * 60 * 1000;
40 40
41 static GSList *timestamp_timeouts; 41 static GSList *timestamp_timeouts = NULL;
42 42
43 static gboolean do_timestamp (gpointer data) 43 static gboolean do_timestamp (gpointer data)
44 { 44 {
45 GaimConversation *c = (GaimConversation *)data; 45 GaimConversation *c = (GaimConversation *)data;
46 char *buf; 46 char *buf;
47 char mdate[6]; 47 char mdate[6];
48 int is_conversation_active;
48 time_t tim = time(NULL); 49 time_t tim = time(NULL);
49 50
50 if (!g_list_find(gaim_get_conversations(), c)) 51 if (!g_list_find(gaim_get_conversations(), c))
51 return FALSE; 52 return FALSE;
52 53
53 strftime(mdate, sizeof(mdate), "%H:%M", localtime(&tim)); 54 /* is_conversation_active is true if an im has been displayed since the last timestamp */
54 buf = g_strdup_printf(" %s", mdate); 55 is_conversation_active = GPOINTER_TO_INT(gaim_conversation_get_data(c, "timestamp-conv-active"));
55 gaim_conversation_write(c, NULL, buf, GAIM_MESSAGE_NO_LOG, tim); 56
56 g_free(buf); 57 if (is_conversation_active){
58 gaim_conversation_set_data(c, "timestamp-conv-active", GINT_TO_POINTER(FALSE));
59 strftime(mdate, sizeof(mdate), "%H:%M", localtime(&tim));
60 buf = g_strdup_printf(" %s", mdate);
61 gaim_conversation_write(c, NULL, buf, GAIM_MESSAGE_NO_LOG, tim);
62 g_free(buf);
63 }
64 else
65 gaim_conversation_set_data(c, "timestamp-enabled", GINT_TO_POINTER(FALSE));
66
57 return TRUE; 67 return TRUE;
58 } 68 }
59 69
70
71 static gboolean
72 timestamp_displaying_conv_msg(GaimAccount *account, GaimConversation *conv,
73 char **buffer, void *data)
74 {
75 int is_timestamp_enabled;
76
77 if (!g_list_find(gaim_get_conversations(), conv))
78 return FALSE;
79
80 /* set to true, since there has been an im since the last timestamp */
81 gaim_conversation_set_data(conv, "timestamp-conv-active", GINT_TO_POINTER(TRUE));
82
83 is_timestamp_enabled = GPOINTER_TO_INT(gaim_conversation_get_data(conv, "timestamp-enabled"));
84
85 if (!is_timestamp_enabled){
86 gaim_conversation_set_data(conv, "timestamp-enabled", GINT_TO_POINTER(TRUE));
87 do_timestamp((gpointer)conv);
88 }
89
90 return FALSE;
91 }
92
93 static gboolean
94 timestamp_receiving_msg(GaimAccount *account, char **sender, char **buffer,
95 int *flags, void *data)
96 {
97 GaimConversation* conv;
98
99 conv = gaim_find_conversation_with_account(*sender, account);
100 if (conv != NULL)
101 return timestamp_displaying_conv_msg(account, conv, buffer, data);
102 return FALSE;
103 }
104
105
60 static void timestamp_new_convo(GaimConversation *conv) 106 static void timestamp_new_convo(GaimConversation *conv)
61 { 107 {
62 do_timestamp(conv); 108 if (!g_list_find(gaim_get_conversations(), conv))
109 return;
110
111 /*
112 This if statement stops conversations that have already been initialized for timestamps
113 from being reinitialized. This prevents every active conversation from immediately being spammed
114 with a new timestamp when the user modifies the timer interval.
115 */
116 if (!gaim_conversation_get_data(conv, "timestamp-initialized")){
117 gaim_conversation_set_data(conv, "timestamp-initialized", GINT_TO_POINTER(TRUE));
118 gaim_conversation_set_data(conv, "timestamp-enabled", GINT_TO_POINTER(TRUE));
119 gaim_conversation_set_data(conv, "timestamp-conv-active", GINT_TO_POINTER(TRUE));
120 do_timestamp(conv);
121 }
63 122
64 timestamp_timeouts = g_slist_append(timestamp_timeouts, 123 timestamp_timeouts = g_slist_append(timestamp_timeouts,
65 GINT_TO_POINTER(g_timeout_add(interval, do_timestamp, conv))); 124 GINT_TO_POINTER(g_timeout_add(interval, do_timestamp, conv)));
66 125 }
67 } 126
127
128 static void destroy_timer_list()
129 {
130 GSList *to;
131
132 for (to = timestamp_timeouts; to != NULL; to = to->next)
133 g_source_remove(GPOINTER_TO_INT(to->data));
134
135 g_slist_free(timestamp_timeouts);
136
137 timestamp_timeouts = NULL;
138 }
139
140 static void init_timer_list()
141 {
142 GList *cnvs;
143 GaimConversation *c;
144
145 if (timestamp_timeouts != NULL)
146 destroy_timer_list();
147
148 for (cnvs = gaim_get_conversations(); cnvs != NULL; cnvs = cnvs->next) {
149 c = cnvs->data;
150 timestamp_new_convo(c);
151 }
152 }
153
154
68 155
69 static void set_timestamp(GtkWidget *button, GtkWidget *spinner) { 156 static void set_timestamp(GtkWidget *button, GtkWidget *spinner) {
70 int tm; 157 int tm;
71 158
72 tm = 0; 159 tm = 0;
76 163
77 tm = tm * 60 * 1000; 164 tm = tm * 60 * 1000;
78 165
79 interval = tm; 166 interval = tm;
80 gaim_prefs_set_int("/plugins/gtk/timestamp/interval", interval); 167 gaim_prefs_set_int("/plugins/gtk/timestamp/interval", interval);
168
169 destroy_timer_list();
170 init_timer_list();
81 } 171 }
82 172
83 static GtkWidget * 173 static GtkWidget *
84 get_config_frame(GaimPlugin *plugin) 174 get_config_frame(GaimPlugin *plugin)
85 { 175 {
122 } 212 }
123 213
124 static gboolean 214 static gboolean
125 plugin_load(GaimPlugin *plugin) 215 plugin_load(GaimPlugin *plugin)
126 { 216 {
127 GList *cnvs; 217 void *conv_handle = gaim_conversations_get_handle();
128 GaimConversation *c; 218
129 219 init_timer_list();
130 timestamp_timeouts = NULL; 220
131 for (cnvs = gaim_get_conversations(); cnvs != NULL; cnvs = cnvs->next) { 221 gaim_signal_connect(conv_handle, "conversation-created",
132 c = cnvs->data; 222 plugin, GAIM_CALLBACK(timestamp_new_convo), NULL);
133 timestamp_new_convo(c); 223
134 } 224 //record IM display events for each conversation
135 225 gaim_signal_connect(conv_handle, "receiving-im-msg",
136 gaim_signal_connect(gaim_conversations_get_handle(), 226 plugin, GAIM_CALLBACK(timestamp_receiving_msg), NULL);
137 "conversation-created", 227 gaim_signal_connect(conv_handle, "displaying-im-msg",
138 plugin, GAIM_CALLBACK(timestamp_new_convo), NULL); 228 plugin, GAIM_CALLBACK(timestamp_displaying_conv_msg), NULL);
139 229
140 interval = gaim_prefs_get_int("/plugins/gtk/timestamp/interval"); 230 interval = gaim_prefs_get_int("/plugins/gtk/timestamp/interval");
141 231
142 return TRUE; 232 return TRUE;
143 } 233 }
144 234
235
236
145 static gboolean 237 static gboolean
146 plugin_unload(GaimPlugin *plugin) 238 plugin_unload(GaimPlugin *plugin)
147 { 239 {
148 GSList *to; 240 void *conv_handle = gaim_conversations_get_handle();
149 241
150 for (to = timestamp_timeouts; to != NULL; to = to->next) 242 gaim_signal_disconnect(conv_handle, "conversation-created",
151 g_source_remove(GPOINTER_TO_INT(to->data)); 243 plugin, GAIM_CALLBACK(timestamp_new_convo));
152 244 gaim_signal_disconnect(conv_handle, "receiving-im-msg",
153 g_slist_free(timestamp_timeouts); 245 plugin, GAIM_CALLBACK(timestamp_receiving_msg));
154 246 gaim_signal_disconnect(conv_handle, "displaying-im-msg",
247 plugin, GAIM_CALLBACK(timestamp_displaying_conv_msg));
248
249 destroy_timer_list();
155 return TRUE; 250 return TRUE;
156 } 251 }
157 252
158 static GaimGtkPluginUiInfo ui_info = 253 static GaimGtkPluginUiInfo ui_info =
159 { 254 {