Mercurial > pidgin.yaz
comparison pidgin/plugins/timestamp.c @ 15374:5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 20 Jan 2007 02:32:10 +0000 |
parents | |
children | d75099d2567e |
comparison
equal
deleted
inserted
replaced
15373:f79e0f4df793 | 15374:5fe8042783c1 |
---|---|
1 /* | |
2 * Gaim - iChat-style timestamps | |
3 * | |
4 * Copyright (C) 2002-2003, Sean Egan | |
5 * Copyright (C) 2003, Chris J. Friesen <Darth_Sebulba04@yahoo.com> | |
6 * Copyright (C) 2007, Andrew Gaul <andrew@gaul.org> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
24 #include "internal.h" | |
25 | |
26 #include "conversation.h" | |
27 #include "debug.h" | |
28 #include "prefs.h" | |
29 #include "signals.h" | |
30 #include "version.h" | |
31 | |
32 #include "gtkimhtml.h" | |
33 #include "gtkplugin.h" | |
34 #include "gtkutils.h" | |
35 | |
36 #define TIMESTAMP_PLUGIN_ID "gtk-timestamp" | |
37 | |
38 /* minutes externally, seconds internally, and milliseconds in preferences */ | |
39 static int interval = 5 * 60; | |
40 | |
41 static void | |
42 timestamp_display(GaimConversation *conv, time_t then, time_t now) | |
43 { | |
44 GaimGtkConversation *gtk_conv = GAIM_GTK_CONVERSATION(conv); | |
45 GtkWidget *imhtml = gtk_conv->imhtml; | |
46 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml)); | |
47 GtkTextIter iter; | |
48 const char *mdate; | |
49 int y, height; | |
50 GdkRectangle rect; | |
51 | |
52 /* display timestamp */ | |
53 mdate = gaim_utf8_strftime(then == 0 ? "%H:%M" : "\n%H:%M", | |
54 localtime(&now)); | |
55 gtk_text_buffer_get_end_iter(buffer, &iter); | |
56 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, mdate, | |
57 strlen(mdate), "TIMESTAMP", NULL); | |
58 | |
59 /* scroll view if necessary */ | |
60 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
61 gtk_text_view_get_line_yrange( | |
62 GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
63 if (((y + height) - (rect.y + rect.height)) > height && | |
64 gtk_text_buffer_get_char_count(buffer)) { | |
65 gboolean smooth = gaim_prefs_get_bool( | |
66 "/gaim/gtk/conversations/use_smooth_scrolling"); | |
67 gtk_imhtml_scroll_to_end(GTK_IMHTML(imhtml), smooth); | |
68 } | |
69 } | |
70 | |
71 static gboolean | |
72 timestamp_displaying_conv_msg(GaimAccount *account, const char *who, | |
73 char **buffer, GaimConversation *conv, | |
74 GaimMessageFlags flags, void *data) | |
75 { | |
76 time_t now = time(NULL) / interval * interval; | |
77 time_t then; | |
78 | |
79 if (!g_list_find(gaim_get_conversations(), conv)) | |
80 return FALSE; | |
81 | |
82 then = GPOINTER_TO_INT(gaim_conversation_get_data( | |
83 conv, "timestamp-last")); | |
84 | |
85 if (now - then >= interval) { | |
86 timestamp_display(conv, then, now); | |
87 gaim_conversation_set_data( | |
88 conv, "timestamp-last", GINT_TO_POINTER(now)); | |
89 } | |
90 | |
91 return FALSE; | |
92 } | |
93 | |
94 static void | |
95 timestamp_new_convo(GaimConversation *conv) | |
96 { | |
97 GaimGtkConversation *gtk_conv = GAIM_GTK_CONVERSATION(conv); | |
98 GtkTextBuffer *buffer; | |
99 | |
100 if (!g_list_find(gaim_get_conversations(), conv)) | |
101 return; | |
102 | |
103 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_conv->imhtml)); | |
104 gtk_text_buffer_create_tag(buffer, "TIMESTAMP", | |
105 "foreground", "#888888", "justification", GTK_JUSTIFY_CENTER, | |
106 "weight", PANGO_WEIGHT_BOLD, NULL); | |
107 | |
108 gaim_conversation_set_data(conv, "timestamp-last", GINT_TO_POINTER(0)); | |
109 } | |
110 | |
111 static void | |
112 set_timestamp(GtkWidget *spinner, void *null) | |
113 { | |
114 int tm; | |
115 | |
116 tm = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinner)); | |
117 gaim_debug(GAIM_DEBUG_MISC, "timestamp", | |
118 "setting interval to %d minutes\n", tm); | |
119 | |
120 interval = tm * 60; | |
121 gaim_prefs_set_int("/plugins/gtk/timestamp/interval", interval * 1000); | |
122 } | |
123 | |
124 static GtkWidget * | |
125 get_config_frame(GaimPlugin *plugin) | |
126 { | |
127 GtkWidget *ret; | |
128 GtkWidget *frame, *label; | |
129 GtkWidget *vbox, *hbox; | |
130 GtkObject *adj; | |
131 GtkWidget *spinner; | |
132 | |
133 ret = gtk_vbox_new(FALSE, 18); | |
134 gtk_container_set_border_width (GTK_CONTAINER (ret), 12); | |
135 | |
136 frame = gaim_gtk_make_frame(ret, _("Display Timestamps Every")); | |
137 vbox = gtk_vbox_new(FALSE, 5); | |
138 gtk_container_add(GTK_CONTAINER(frame), vbox); | |
139 | |
140 hbox = gtk_hbox_new(FALSE, 5); | |
141 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
142 | |
143 /* XXX limit to divisors of 60? */ | |
144 adj = gtk_adjustment_new(interval / 60, 1, 60, 1, 0, 0); | |
145 spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0, 0); | |
146 gtk_box_pack_start(GTK_BOX(hbox), spinner, TRUE, TRUE, 0); | |
147 g_signal_connect(G_OBJECT(spinner), "value-changed", | |
148 G_CALLBACK(set_timestamp), NULL); | |
149 label = gtk_label_new(_("minutes")); | |
150 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5); | |
151 | |
152 gtk_widget_show_all(ret); | |
153 return ret; | |
154 } | |
155 | |
156 static gboolean | |
157 plugin_load(GaimPlugin *plugin) | |
158 { | |
159 void *conv_handle = gaim_conversations_get_handle(); | |
160 void *gtkconv_handle = gaim_gtk_conversations_get_handle(); | |
161 | |
162 /* lower priority to display initial timestamp after logged messages */ | |
163 gaim_signal_connect_priority(conv_handle, "conversation-created", | |
164 plugin, GAIM_CALLBACK(timestamp_new_convo), NULL, | |
165 GAIM_SIGNAL_PRIORITY_DEFAULT + 1); | |
166 | |
167 gaim_signal_connect(gtkconv_handle, "displaying-chat-msg", | |
168 plugin, GAIM_CALLBACK(timestamp_displaying_conv_msg), NULL); | |
169 gaim_signal_connect(gtkconv_handle, "displaying-im-msg", | |
170 plugin, GAIM_CALLBACK(timestamp_displaying_conv_msg), NULL); | |
171 | |
172 interval = gaim_prefs_get_int("/plugins/gtk/timestamp/interval") / 1000; | |
173 | |
174 return TRUE; | |
175 } | |
176 | |
177 static GaimGtkPluginUiInfo ui_info = | |
178 { | |
179 get_config_frame, | |
180 0 /* page_num (Reserved) */ | |
181 }; | |
182 | |
183 static GaimPluginInfo info = | |
184 { | |
185 GAIM_PLUGIN_MAGIC, | |
186 GAIM_MAJOR_VERSION, | |
187 GAIM_MINOR_VERSION, | |
188 GAIM_PLUGIN_STANDARD, /**< type */ | |
189 GAIM_GTK_PLUGIN_TYPE, /**< ui_requirement */ | |
190 0, /**< flags */ | |
191 NULL, /**< dependencies */ | |
192 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
193 | |
194 TIMESTAMP_PLUGIN_ID, /**< id */ | |
195 N_("Timestamp"), /**< name */ | |
196 VERSION, /**< version */ | |
197 /** summary */ | |
198 N_("Display iChat-style timestamps"), | |
199 /** description */ | |
200 N_("Display iChat-style timestamps every N minutes."), | |
201 "Sean Egan <seanegan@gmail.com>", /**< author */ | |
202 GAIM_WEBSITE, /**< homepage */ | |
203 | |
204 plugin_load, /**< load */ | |
205 NULL, /**< unload */ | |
206 NULL, /**< destroy */ | |
207 | |
208 &ui_info, /**< ui_info */ | |
209 NULL, /**< extra_info */ | |
210 NULL, | |
211 NULL | |
212 }; | |
213 | |
214 static void | |
215 init_plugin(GaimPlugin *plugin) | |
216 { | |
217 gaim_prefs_add_none("/plugins/gtk/timestamp"); | |
218 gaim_prefs_add_int("/plugins/gtk/timestamp/interval", interval * 1000); | |
219 } | |
220 | |
221 GAIM_INIT_PLUGIN(interval, init_plugin, info) |