00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include "intl.h"
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <gdk/gdk.h>
00035 #include <gdk/gdkkeysyms.h>
00036 #include <assert.h>
00037
00038 #include "globals.h"
00039 #include "gtk_eb_html.h"
00040 #include "log_window.h"
00041 #include "util.h"
00042
00043 #include "pixmaps/cancel.xpm"
00044
00045 int current_row=-1, last_pos=-1;
00046
00047 static void free_string_list(gpointer data, gpointer user_data)
00048 {
00049 g_free(data);
00050 }
00051
00052 static void free_gs_list(gpointer data, gpointer user_data)
00053 {
00054 g_slist_foreach((GSList*)data, (GFunc)free_string_list, NULL);
00055 g_slist_free((GSList*)data);
00056 }
00057
00058 static void create_html_widget(log_window* lw)
00059 {
00060
00061 lw->html_display = ext_gtk_text_new(NULL, NULL);
00062 gtk_eb_html_init(EXT_GTK_TEXT(lw->html_display));
00063
00064
00065 lw->html_scroller = gtk_scrolled_window_new(NULL, NULL);
00066 gtk_widget_set_usize(lw->html_scroller, 375, 150);
00067 gtk_container_add(GTK_CONTAINER(lw->html_scroller), lw->html_display);
00068 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lw->html_scroller), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
00069
00070 gtk_box_pack_end(GTK_BOX(lw->date_html_hbox), lw->html_scroller, TRUE, TRUE, 5);
00071
00072 gtk_widget_show(GTK_WIDGET(lw->html_scroller));
00073 gtk_widget_show(GTK_WIDGET(lw->html_display));
00074 }
00075
00076
00077 static void set_html_text(GSList* gl, log_window* lw)
00078 {
00079 GSList *h;
00080
00081
00082 if (lw->html_display != NULL)
00083 gtk_widget_destroy(GTK_WIDGET(lw->html_display));
00084 if (lw->html_scroller != NULL)
00085 gtk_widget_destroy(GTK_WIDGET(lw->html_scroller));
00086
00087
00088
00089 create_html_widget(lw);
00090
00091 ext_gtk_text_freeze(EXT_GTK_TEXT(lw->html_display));
00092
00093
00094 h = gl;
00095 while (h != NULL) {
00096 if (h->data != NULL) {
00097 gtk_eb_html_add(EXT_GTK_TEXT(lw->html_display), (gchar*)h->data,0,0,0);
00098 }
00099 h = g_slist_next(h);
00100 }
00101
00102 ext_gtk_text_thaw(EXT_GTK_TEXT(lw->html_display));
00103
00104 }
00105
00106 static void load_log_information(log_window* lw)
00107 {
00108 gchar name_buffer[NAME_MAX];
00109 gchar read_buffer[4096];
00110 FILE* fp;
00111 gchar* p1, *p2;
00112 gchar date_buffer[128];
00113 gchar* p3[1];
00114 GSList* gl = NULL, *gl1 = NULL;
00115 gint idx;
00116
00117 gtk_clist_freeze(GTK_CLIST(lw->date_list));
00118
00119 make_safe_filename(name_buffer, lw->remote->nick);
00120
00121 if ( (fp = fopen(name_buffer, "r")) != NULL) {
00122 gl = g_slist_alloc();
00123
00124 while (fgets(read_buffer, 4096, fp) != NULL) {
00125 if ((p1 = strstr(read_buffer, _("Conversation started on "))) != NULL)
00126 {
00127
00128
00129
00130 p1 += strlen(_("Conversation started on "));
00131
00132
00133 if ((p2 = strstr(read_buffer, "</B></P>")) != NULL) {
00134 strncpy(date_buffer, p1, p2 - p1);
00135 date_buffer[p2-p1] = '\0';
00136 } else {
00137
00138 strncpy(date_buffer, p1, (read_buffer + strlen(read_buffer)) - p1);
00139 date_buffer[(read_buffer + strlen(read_buffer)) - p1 - 1] = '\0';
00140 }
00141
00142 p3[0] = date_buffer;
00143 idx = gtk_clist_append(GTK_CLIST(lw->date_list), p3);
00144
00145
00146 gl1 = g_slist_alloc();
00147 gl = g_slist_append(gl, gl1);
00148
00149
00150 gtk_clist_set_row_data(GTK_CLIST(lw->date_list), idx, gl1);
00151 } else if ((p1 = strstr(read_buffer, _("Conversation ended on "))) == NULL) {
00152 if (gl1 != NULL) {
00153 gl1 = g_slist_append(gl1, strdup(read_buffer));
00154 }
00155 else
00156 {
00157 char buff1[4096];
00158 p3[0] = _("Undated Log");
00159
00160 strcpy(buff1, read_buffer);
00161 idx = gtk_clist_append(GTK_CLIST(lw->date_list), p3);
00162
00163
00164 gl1 = g_slist_alloc();
00165 gl = g_slist_append(gl, gl1);
00166
00167
00168 gtk_clist_set_row_data(GTK_CLIST(lw->date_list), idx, gl1);
00169 gl1 = g_slist_append(gl1, strdup(buff1));
00170 }
00171 }
00172 }
00173 fclose(fp);
00174 } else {
00175 perror(name_buffer);
00176 }
00177
00178 lw->entries = gl;
00179
00180 gtk_clist_thaw(GTK_CLIST(lw->date_list));
00181 }
00182
00183
00184
00185 static void destroy_event(GtkWidget* widget, gpointer data)
00186 {
00187 log_window* lw = (log_window*)data;
00188
00189 g_slist_foreach(lw->entries, (GFunc)free_gs_list, NULL);
00190
00191 lw->remote->logwindow = NULL;
00192 lw->window = NULL;
00193 g_free(lw);
00194 }
00195
00196 static void close_log_window_callback(GtkWidget* button, gpointer data)
00197 {
00198 log_window* lw = (log_window*)data;
00199 gtk_widget_destroy(lw->window);
00200 }
00201
00202 static gboolean search_callback(GtkWidget *widget, GdkEventKey *event, gpointer data)
00203 {
00204 int cur_pos=0,cur_log=current_row;
00205 log_window * lw = (log_window *)data;
00206 gboolean looped=FALSE;
00207
00208 if (event->keyval == GDK_Return) {
00209 gchar * text = gtk_entry_get_text(GTK_ENTRY(widget));
00210 strip_html(text);
00211 if(strlen(text) == 0)
00212 return TRUE;
00213
00214 while(!looped) {
00215 int total_length=0;
00216 GSList *c_slist, *s_list;
00217
00218 s_list = (GSList*)gtk_clist_get_row_data(GTK_CLIST(lw->date_list), cur_log);
00219 if (!s_list) {
00220
00221 s_list = (GSList*)gtk_clist_get_row_data(GTK_CLIST(lw->date_list), 0);
00222
00223 cur_log=cur_pos=current_row=last_pos=total_length=0;
00224 looped=TRUE;
00225 }
00226 if(!s_list) {
00227 eb_debug(DBG_CORE, "search: breaking\n");
00228
00229 break;
00230 }
00231
00232
00233
00234 c_slist = s_list;
00235 while(s_list && cur_pos <= last_pos) {
00236 s_list=s_list->next;
00237 if(s_list && s_list->data && cur_pos<last_pos) {
00238 total_length+=strlen((char*)s_list->data);
00239 }
00240 if(s_list) cur_pos++;
00241 }
00242 while(s_list) {
00243 gchar *list_text = (gchar *)s_list->data;
00244 gchar *no_html_text = NULL;
00245 if (list_text)
00246 no_html_text = strdup(list_text);
00247 if (no_html_text)
00248 strip_html(no_html_text);
00249
00250 if(no_html_text && strstr(no_html_text,text) != NULL) {
00251 GtkEditable *editable;
00252
00253 gtk_clist_select_row(GTK_CLIST(lw->date_list),cur_log,0);
00254 gtk_clist_moveto (GTK_CLIST(lw->date_list),
00255 cur_log,0,0,0);
00256
00257 set_html_text(c_slist,lw);
00258 editable = GTK_EDITABLE (lw->html_display);
00259
00260 last_pos=cur_pos+1;
00261 current_row=cur_log;
00262
00263 eb_debug(DBG_CORE, "search: total2=%d\n",total_length);
00264 gtk_editable_set_position ( editable,
00265 total_length+(int)(strstr(no_html_text,text)-no_html_text));
00266 gtk_editable_select_region( editable,
00267 total_length+(int)(strstr(no_html_text,text)-no_html_text),
00268 total_length+(int)(strstr(no_html_text,text)-no_html_text)+strlen(text));
00269 total_length+=strlen(no_html_text);
00270 return TRUE;
00271 } else if (no_html_text)
00272 total_length+=strlen(no_html_text);
00273
00274 if (no_html_text)
00275 free(no_html_text);
00276
00277 s_list=s_list->next;
00278 cur_pos++;
00279 last_pos=cur_pos;
00280 }
00281
00282 cur_log++;
00283 cur_pos=0;last_pos=0;
00284 current_row=cur_log;
00285 }
00286
00287 }
00288 return TRUE;
00289 }
00290
00291
00292 static void select_date_callback (GtkCList *clist, gint row, gint column,
00293 GdkEventButton *event, gpointer user_data)
00294 {
00295 GSList* string_list;
00296 log_window* lw = (log_window*)user_data;
00297
00298 string_list = (GSList*)gtk_clist_get_row_data(GTK_CLIST(clist), row);
00299 set_html_text(string_list, lw);
00300 current_row = row; last_pos=0;
00301 }
00302
00303
00304 log_window* eb_log_window_new(struct contact *rc)
00305 {
00306 GtkWidget *iconwid0;
00307 GdkPixmap *icon0;
00308 GdkBitmap *mask;
00309
00310 log_window* lw = g_new0(log_window, 1);
00311 char buffer[512];
00312 GtkWidget* vbox;
00313 GtkWidget* hbox;
00314 GtkWidget* button;
00315 GtkWidget* toolbar;
00316 GtkWidget* search_label;
00317 GtkWidget* search_entry;
00318 lw->remote = rc;
00319 rc->logwindow = lw;
00320 lw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00321 gtk_window_set_position(GTK_WINDOW(lw->window), GTK_WIN_POS_MOUSE);
00322
00323 sprintf(buffer, _("Past conversations with %s"), rc->nick);
00324 gtk_window_set_title(GTK_WINDOW(lw->window), buffer);
00325 gtk_container_set_border_width(GTK_CONTAINER(lw->window), 5);
00326
00327
00328
00329 lw->date_html_hbox = gtk_hbox_new(FALSE, 0);
00330
00331 lw->date_list = gtk_clist_new(1);
00332 gtk_clist_set_column_width(GTK_CLIST(lw->date_list), 0, 100);
00333 gtk_clist_set_selection_mode(GTK_CLIST(lw->date_list), GTK_SELECTION_BROWSE);
00334
00335
00336 lw->date_scroller = gtk_scrolled_window_new(NULL, NULL);
00337 gtk_widget_set_usize(lw->date_scroller, 180, 150);
00338 gtk_container_add(GTK_CONTAINER(lw->date_scroller), lw->date_list);
00339 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lw->date_scroller), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
00340
00341
00342 gtk_box_pack_start(GTK_BOX(lw->date_html_hbox), lw->date_scroller, TRUE, TRUE, 5);
00343 vbox = gtk_vbox_new(FALSE, 0);
00344 gtk_box_pack_start(GTK_BOX(vbox), lw->date_html_hbox, TRUE, TRUE, 5);
00345 gtk_container_add(GTK_CONTAINER(lw->window), vbox);
00346
00347 gtk_signal_connect(GTK_OBJECT(lw->window), "destroy",
00348 GTK_SIGNAL_FUNC(destroy_event), lw);
00349
00350
00351 hbox = gtk_hbox_new(FALSE, 0);
00352 toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_ICONS);
00353 gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
00354 gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
00355
00356
00357 gtk_widget_realize(GTK_WIDGET(lw->window));
00358
00359
00360
00361 search_label = gtk_label_new(_("Search: "));
00362 search_entry = gtk_entry_new();
00363 gtk_box_pack_start(GTK_BOX(hbox), search_label, FALSE, FALSE, 0);
00364 gtk_box_pack_start(GTK_BOX(hbox), search_entry, TRUE, TRUE, 0);
00365 gtk_widget_show (search_label);
00366 gtk_widget_show (search_entry);
00367 gtk_signal_connect(GTK_OBJECT(search_entry), "key_press_event",
00368 GTK_SIGNAL_FUNC(search_callback), lw);
00369
00370
00371
00372 icon0 = gdk_pixmap_create_from_xpm_d(lw->window->window, &mask, NULL, cancel_xpm);
00373 iconwid0 = gtk_pixmap_new(icon0, mask);
00374 gtk_widget_show(iconwid0);
00375 button = gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
00376 GTK_TOOLBAR_CHILD_BUTTON,
00377 NULL,
00378 "close",
00379 "Close",
00380 "close",
00381 iconwid0,
00382 GTK_SIGNAL_FUNC(close_log_window_callback),
00383 lw);
00384 gtk_widget_show(GTK_WIDGET(button));
00385 gtk_widget_show(GTK_WIDGET(toolbar));
00386
00387 gtk_box_pack_end(GTK_BOX(hbox), toolbar, FALSE, FALSE, 0);
00388 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
00389
00390
00391 gtk_signal_connect(GTK_OBJECT(lw->date_list), "select-row",
00392 GTK_SIGNAL_FUNC(select_date_callback), lw);
00393
00394 load_log_information(lw);
00395
00396
00397 gtk_widget_show(GTK_WIDGET(lw->date_list));
00398 gtk_widget_show(GTK_WIDGET(lw->html_display));
00399 gtk_widget_show(GTK_WIDGET(lw->date_scroller));
00400 gtk_widget_show(GTK_WIDGET(lw->date_html_hbox));
00401 gtk_widget_show(GTK_WIDGET(hbox));
00402 gtk_widget_show(GTK_WIDGET(vbox));
00403
00404 gtk_widget_show(GTK_WIDGET(lw->window));
00405
00406 gtk_clist_select_row(GTK_CLIST(lw->date_list), 0, 0);
00407
00408 return lw;
00409 }