pidgin/plugins/timestamp.c

changeset 16238
33bf2fd32108
parent 12604
893fbf89317c
parent 16123
8b98683319e7
equal deleted inserted replaced
13071:b98e72d4089a 16238:33bf2fd32108
1 /*
2 * Purple - 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 "gtkprefs.h"
35 #include "gtkutils.h"
36
37 #define TIMESTAMP_PLUGIN_ID "gtk-timestamp"
38
39 /* minutes externally, seconds internally, and milliseconds in preferences */
40 static int interval = 5 * 60;
41
42 static void
43 timestamp_display(PurpleConversation *conv, time_t then, time_t now)
44 {
45 PidginConversation *gtk_conv = PIDGIN_CONVERSATION(conv);
46 GtkWidget *imhtml = gtk_conv->imhtml;
47 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
48 GtkTextIter iter;
49 const char *mdate;
50 int y, height;
51 GdkRectangle rect;
52
53 /* display timestamp */
54 mdate = purple_utf8_strftime(then == 0 ? "%H:%M" : "\n%H:%M",
55 localtime(&now));
56 gtk_text_buffer_get_end_iter(buffer, &iter);
57 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, mdate,
58 strlen(mdate), "TIMESTAMP", NULL);
59
60 /* scroll view if necessary */
61 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect);
62 gtk_text_view_get_line_yrange(
63 GTK_TEXT_VIEW(imhtml), &iter, &y, &height);
64 if (((y + height) - (rect.y + rect.height)) > height &&
65 gtk_text_buffer_get_char_count(buffer)) {
66 gboolean smooth = purple_prefs_get_bool(
67 PIDGIN_PREFS_ROOT "/conversations/use_smooth_scrolling");
68 gtk_imhtml_scroll_to_end(GTK_IMHTML(imhtml), smooth);
69 }
70 }
71
72 static gboolean
73 timestamp_displaying_conv_msg(PurpleAccount *account, const char *who,
74 char **buffer, PurpleConversation *conv,
75 PurpleMessageFlags flags, void *data)
76 {
77 time_t now = time(NULL) / interval * interval;
78 time_t then;
79
80 if (!g_list_find(purple_get_conversations(), conv))
81 return FALSE;
82
83 then = GPOINTER_TO_INT(purple_conversation_get_data(
84 conv, "timestamp-last"));
85
86 if (now - then >= interval) {
87 timestamp_display(conv, then, now);
88 purple_conversation_set_data(
89 conv, "timestamp-last", GINT_TO_POINTER(now));
90 }
91
92 return FALSE;
93 }
94
95 static void
96 timestamp_new_convo(PurpleConversation *conv)
97 {
98 PidginConversation *gtk_conv = PIDGIN_CONVERSATION(conv);
99 GtkTextBuffer *buffer;
100
101 if (!g_list_find(purple_get_conversations(), conv))
102 return;
103
104 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtk_conv->imhtml));
105 gtk_text_buffer_create_tag(buffer, "TIMESTAMP",
106 "foreground", "#888888", "justification", GTK_JUSTIFY_CENTER,
107 "weight", PANGO_WEIGHT_BOLD, NULL);
108
109 purple_conversation_set_data(conv, "timestamp-last", GINT_TO_POINTER(0));
110 }
111
112 static void
113 set_timestamp(GtkWidget *spinner, void *null)
114 {
115 int tm;
116
117 tm = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinner));
118 purple_debug(PURPLE_DEBUG_MISC, "timestamp",
119 "setting interval to %d minutes\n", tm);
120
121 interval = tm * 60;
122 purple_prefs_set_int("/plugins/gtk/timestamp/interval", interval * 1000);
123 }
124
125 static GtkWidget *
126 get_config_frame(PurplePlugin *plugin)
127 {
128 GtkWidget *ret;
129 GtkWidget *frame, *label;
130 GtkWidget *vbox, *hbox;
131 GtkObject *adj;
132 GtkWidget *spinner;
133
134 ret = gtk_vbox_new(FALSE, 18);
135 gtk_container_set_border_width (GTK_CONTAINER (ret), 12);
136
137 frame = pidgin_make_frame(ret, _("Display Timestamps Every"));
138 vbox = gtk_vbox_new(FALSE, 5);
139 gtk_container_add(GTK_CONTAINER(frame), vbox);
140
141 hbox = gtk_hbox_new(FALSE, 5);
142 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
143
144 /* XXX limit to divisors of 60? */
145 adj = gtk_adjustment_new(interval / 60, 1, 60, 1, 0, 0);
146 spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0, 0);
147 gtk_box_pack_start(GTK_BOX(hbox), spinner, TRUE, TRUE, 0);
148 g_signal_connect(G_OBJECT(spinner), "value-changed",
149 G_CALLBACK(set_timestamp), NULL);
150 label = gtk_label_new(_("minutes"));
151 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
152
153 gtk_widget_show_all(ret);
154 return ret;
155 }
156
157 static gboolean
158 plugin_load(PurplePlugin *plugin)
159 {
160 void *conv_handle = purple_conversations_get_handle();
161 void *gtkconv_handle = pidgin_conversations_get_handle();
162
163 /* lower priority to display initial timestamp after logged messages */
164 purple_signal_connect_priority(conv_handle, "conversation-created",
165 plugin, PURPLE_CALLBACK(timestamp_new_convo), NULL,
166 PURPLE_SIGNAL_PRIORITY_DEFAULT + 1);
167
168 purple_signal_connect(gtkconv_handle, "displaying-chat-msg",
169 plugin, PURPLE_CALLBACK(timestamp_displaying_conv_msg), NULL);
170 purple_signal_connect(gtkconv_handle, "displaying-im-msg",
171 plugin, PURPLE_CALLBACK(timestamp_displaying_conv_msg), NULL);
172
173 interval = purple_prefs_get_int("/plugins/gtk/timestamp/interval") / 1000;
174
175 return TRUE;
176 }
177
178 static PidginPluginUiInfo ui_info =
179 {
180 get_config_frame,
181 0 /* page_num (Reserved) */
182 };
183
184 static PurplePluginInfo info =
185 {
186 PURPLE_PLUGIN_MAGIC,
187 PURPLE_MAJOR_VERSION,
188 PURPLE_MINOR_VERSION,
189 PURPLE_PLUGIN_STANDARD, /**< type */
190 PIDGIN_PLUGIN_TYPE, /**< ui_requirement */
191 0, /**< flags */
192 NULL, /**< dependencies */
193 PURPLE_PRIORITY_DEFAULT, /**< priority */
194
195 TIMESTAMP_PLUGIN_ID, /**< id */
196 N_("Timestamp"), /**< name */
197 VERSION, /**< version */
198 /** summary */
199 N_("Display iChat-style timestamps"),
200 /** description */
201 N_("Display iChat-style timestamps every N minutes."),
202 "Sean Egan <seanegan@gmail.com>", /**< author */
203 PURPLE_WEBSITE, /**< homepage */
204
205 plugin_load, /**< load */
206 NULL, /**< unload */
207 NULL, /**< destroy */
208
209 &ui_info, /**< ui_info */
210 NULL, /**< extra_info */
211 NULL,
212 NULL
213 };
214
215 static void
216 init_plugin(PurplePlugin *plugin)
217 {
218 purple_prefs_add_none("/plugins/gtk/timestamp");
219 purple_prefs_add_int("/plugins/gtk/timestamp/interval", interval * 1000);
220 }
221
222 PURPLE_INIT_PLUGIN(interval, init_plugin, info)

mercurial