00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 # include <config.h>
00024 #endif
00025
00026 #include "intl.h"
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <gtk/gtk.h>
00031 #include <glib.h>
00032 #include <fcntl.h>
00033 #include <sys/stat.h>
00034 #include <sys/types.h>
00035 #include "away_window.h"
00036 #include "dialog.h"
00037 #include "gtk_globals.h"
00038 #include "service.h"
00039 #include "pixmaps/ok.xpm"
00040 #include "pixmaps/cancel.xpm"
00041
00042 #ifndef MIN
00043 #define MIN(x, y) ((x)<(y)?(x):(y))
00044 #endif
00045
00046 static GtkWidget *awaybox = NULL;
00047 GtkWidget * away_window;
00048 GtkWidget * away_message;
00049 GtkWidget * title;
00050 GtkWidget * save_later;
00051 GList *away_messages;
00052 gint is_away = 0;
00053 gint away_open = 0;
00054
00055 static void destroy_away()
00056 {
00057 GList * list;
00058 eb_local_account * ela = NULL;
00059
00060 is_away = 0;
00061
00062 for (list = accounts; list; list = g_list_next(list)) {
00063 ela = list->data;
00064
00065 if(ela->connected)
00066 eb_services[ela->service_id].sc->set_away(ela, NULL);
00067 }
00068 }
00069
00070 static void imback()
00071 {
00072 if (awaybox)
00073 gtk_widget_destroy(awaybox);
00074 awaybox = NULL;
00075 }
00076
00077 static void destroy( GtkWidget *widget, gpointer data)
00078 {
00079 away_open = 0;
00080 }
00081
00082 void load_away_messages()
00083 {
00084 FILE * fp;
00085 char buff[2048];
00086 away * my_away = NULL;
00087 gboolean reading_title=FALSE;
00088 gboolean reading_message=FALSE;
00089
00090 g_snprintf (buff, 1024, "%saway_messages", config_dir);
00091
00092 if(!(fp = fopen(buff, "r")))
00093 return;
00094
00095 away_messages = NULL;
00096
00097 while ( fgets(buff, sizeof(buff), fp))
00098 {
00099 g_strchomp(buff);
00100 if (!g_strncasecmp(buff, "<AWAY_MESSAGE>", strlen("<AWAY_MESSAGE>"))) {
00101 my_away = g_new0(away, 1);
00102 } else if (!g_strncasecmp( buff, "</AWAY_MESSAGE>", strlen("</AWAY_MESSAGE>"))) {
00103 away_messages = g_list_append(away_messages, my_away);
00104 } else if (!g_strncasecmp(buff, "<TITLE>", strlen("<TITLE>"))) {
00105 reading_title=TRUE;
00106 } else if (!g_strncasecmp(buff, "</TITLE>", strlen("</TITLE>"))) {
00107 reading_title=FALSE;
00108 } else if (!g_strncasecmp(buff, "<MESSAGE>", strlen("<MESSAGE>"))) {
00109 reading_message=TRUE;
00110 my_away->message = g_string_new(NULL);
00111 } else if (!g_strncasecmp(buff, "</MESSAGE>", strlen("</MESSAGE>"))) {
00112 reading_message=FALSE;
00113 } else if(reading_title) {
00114 strncpy(my_away->title, buff, MIN(strlen(buff), sizeof(my_away->title)));
00115 } else if(reading_message) {
00116 if(my_away->message && my_away->message->len)
00117 g_string_append_c(my_away->message, '\n');
00118 g_string_append(my_away->message, buff);
00119 }
00120 }
00121
00122 fclose(fp);
00123 }
00124
00125
00126 void build_away_menu()
00127 {
00128 GList * l;
00129 GtkWidget * label;
00130 GList * away_list = away_messages;
00131 away * my_away = NULL;
00132
00133 l = gtk_container_children(GTK_CONTAINER(away_menu));
00134
00135 while (l) {
00136 gtk_widget_destroy(GTK_WIDGET(l->data));
00137 l = l->next;
00138 }
00139
00140 label = gtk_tearoff_menu_item_new();
00141 gtk_menu_append(GTK_MENU(away_menu), label);
00142 gtk_widget_show(label);
00143
00144 label = gtk_menu_item_new_with_label(_("New Away Message"));
00145 gtk_menu_append(GTK_MENU(away_menu), label);
00146 gtk_signal_connect(GTK_OBJECT(label), "activate",
00147 show_away2, NULL );
00148 gtk_widget_show(label);
00149
00150 if (away_list) {
00151 label = gtk_menu_item_new();
00152 gtk_menu_append(GTK_MENU(away_menu), label );
00153 gtk_widget_show(label);
00154 }
00155
00156 while (away_list) {
00157 my_away = (away *)away_list->data;
00158
00159 label = gtk_menu_item_new_with_label(my_away->title);
00160 gtk_menu_append(GTK_MENU(away_menu), label);
00161 gtk_signal_connect(GTK_OBJECT(label), "activate",
00162 show_away, my_away->message->str);
00163
00164 gtk_widget_show(label);
00165
00166 away_list = away_list->next;
00167 }
00168 gtk_widget_show(away_menu);
00169 }
00170
00171 void write_away_messages()
00172 {
00173 GList * away_list = away_messages;
00174 FILE *fp;
00175 char buff2[2048];
00176 away * my_away;
00177
00178 g_snprintf(buff2, 1024, "%saway_messages",config_dir);
00179
00180 if(!(fp = fdopen(creat(buff2,0700),"w")))
00181 return;
00182
00183 while (away_list) {
00184 my_away = (away *)away_list->data;
00185
00186 fprintf(fp,"<AWAY_MESSAGE>\n");
00187 fprintf(fp,"<TITLE>\n");
00188 strncpy(buff2, my_away->title, strlen(my_away->title) + 1);
00189 g_strchomp(buff2);
00190 fprintf(fp,"%s\n",buff2);
00191 fprintf(fp,"</TITLE>\n");
00192 fprintf(fp,"<MESSAGE>\n");
00193 strncpy(buff2, my_away->message->str, strlen(my_away->message->str) + 1);
00194 g_strchomp(buff2);
00195 fprintf(fp,"%s\n",buff2);
00196 fprintf(fp,"</MESSAGE>\n");
00197 fprintf(fp,"</AWAY_MESSAGE>\n");
00198
00199 away_list = away_list->next;
00200 }
00201 fclose(fp);
00202 }
00203
00204 static void ok_callback( GtkWidget * widget, gpointer data)
00205 {
00206 GString * a_title = g_string_sized_new(1024);
00207 GString * a_message = g_string_sized_new(1024);
00208 gint save = GTK_TOGGLE_BUTTON(save_later)->active;
00209 away * my_away;
00210
00211 gchar buff[2048];
00212
00213 strcpy(buff, gtk_entry_get_text(GTK_ENTRY(title)));
00214 g_string_append(a_title, buff);
00215
00216 strcpy(buff, gtk_entry_get_text(GTK_ENTRY(away_message)));
00217 g_string_append(a_message, buff);
00218
00219 if (save == 1) {
00220 my_away = g_new0(away, 1);
00221 strncpy(my_away->title, a_title->str, strlen(a_title->str));
00222 my_away->message = g_string_new(NULL);
00223 g_string_append(my_away->message, a_message->str);
00224 away_messages = g_list_append(away_messages, my_away);
00225 write_away_messages();
00226 build_away_menu();
00227 }
00228
00229 show_away(NULL, a_message->str);
00230
00231 g_string_free(a_title, TRUE);
00232 g_string_free(a_message, TRUE);
00233
00234 gtk_widget_destroy(away_window);
00235 }
00236
00237 static void cancel_callback( GtkWidget *widget, gpointer data)
00238 {
00239 gtk_widget_destroy(away_window);
00240 }
00241
00242 void show_away2(GtkWidget *w, void *null)
00243 {
00244 if ( !away_open ) {
00245 GtkWidget * vbox;
00246 GtkWidget * hbox;
00247 GtkWidget * hbox2;
00248 GtkWidget * label;
00249 GtkWidget * frame;
00250 GtkWidget * table;
00251 GtkWidget * separator;
00252 GtkWidget * button;
00253 GtkWidget * iconwid;
00254 GdkPixmap * icon;
00255 GdkBitmap * mask;
00256
00257 away_window = gtk_window_new(GTK_WINDOW_DIALOG);
00258 gtk_window_set_position(GTK_WINDOW(away_window), GTK_WIN_POS_MOUSE);
00259 gtk_widget_realize(away_window);
00260
00261 vbox = gtk_vbox_new(FALSE, 5);
00262
00263
00264
00265 table = gtk_table_new(2, 3, FALSE);
00266
00267 label = gtk_label_new(_("Title:"));
00268 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
00269 gtk_widget_show(label);
00270
00271 title = gtk_entry_new();
00272 gtk_table_attach_defaults(GTK_TABLE(table), title, 1, 2, 0, 1);
00273 gtk_widget_show(title);
00274
00275 label = gtk_label_new(_("Away Message:"));
00276 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
00277 gtk_widget_show(label);
00278
00279 away_message = gtk_entry_new();
00280 gtk_table_attach_defaults(GTK_TABLE(table), away_message, 1, 2, 1, 2);
00281 gtk_widget_show(away_message);
00282
00283 save_later = gtk_check_button_new_with_label(_("Save for later use"));
00284 gtk_table_attach_defaults(GTK_TABLE(table), save_later, 1, 2, 2, 3);
00285 gtk_widget_show(save_later);
00286
00287 frame = gtk_frame_new(_("New Away Message"));
00288 gtk_container_add(GTK_CONTAINER(frame), table);
00289 gtk_widget_show(table);
00290
00291 gtk_table_set_row_spacings (GTK_TABLE(table), 5);
00292 gtk_table_set_col_spacings (GTK_TABLE(table), 5);
00293
00294 gtk_container_set_border_width(GTK_CONTAINER(away_window), 5);
00295 gtk_container_set_border_width(GTK_CONTAINER(table), 5);
00296
00297 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
00298 gtk_widget_show(frame);
00299
00300
00301
00302 separator = gtk_hseparator_new();
00303 gtk_box_pack_start(GTK_BOX(vbox), separator, FALSE, FALSE, 5);
00304 gtk_widget_show(separator);
00305
00306
00307
00308 hbox = gtk_hbox_new(FALSE, 5);
00309 hbox2 = gtk_hbox_new(TRUE, 5);
00310
00311 gtk_widget_set_usize(hbox2, 200, 25);
00312
00313
00314
00315 icon = gdk_pixmap_create_from_xpm_d(away_window->window, &mask, NULL, ok_xpm);
00316 iconwid = gtk_pixmap_new(icon, mask);
00317 label = gtk_label_new(_("Set Away"));
00318
00319 gtk_box_pack_start(GTK_BOX(hbox), iconwid, FALSE, FALSE, 2);
00320 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
00321
00322 gtk_widget_show(iconwid);
00323 gtk_widget_show(label);
00324
00325 button = gtk_button_new();
00326
00327 gtk_signal_connect(GTK_OBJECT(button), "clicked",
00328 GTK_SIGNAL_FUNC(ok_callback), NULL);
00329
00330 gtk_widget_show(hbox);
00331
00332 gtk_container_add(GTK_CONTAINER(button), hbox);
00333 gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 0);
00334 gtk_widget_show(button);
00335
00336
00337
00338 hbox = gtk_hbox_new(FALSE, 5);
00339
00340 icon = gdk_pixmap_create_from_xpm_d(away_window->window, &mask, NULL, cancel_xpm);
00341 iconwid = gtk_pixmap_new(icon, mask);
00342 label = gtk_label_new(_("Cancel"));
00343
00344 gtk_box_pack_start(GTK_BOX(hbox), iconwid, FALSE, FALSE, 2);
00345 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
00346
00347 gtk_widget_show(iconwid);
00348 gtk_widget_show(label);
00349
00350 button = gtk_button_new();
00351
00352 gtk_signal_connect(GTK_OBJECT(button), "clicked",
00353 GTK_SIGNAL_FUNC(cancel_callback), NULL);
00354
00355 gtk_widget_show(hbox);
00356
00357 gtk_container_add(GTK_CONTAINER(button), hbox);
00358
00359 gtk_box_pack_start(GTK_BOX(hbox2), button, TRUE, TRUE, 0);
00360 gtk_widget_show(button);
00361
00362 hbox = gtk_hbox_new(FALSE, 0);
00363
00364 gtk_box_pack_end(GTK_BOX(hbox), hbox2, FALSE, FALSE, 0);
00365 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
00366
00367 gtk_widget_show(hbox);
00368 gtk_widget_show(hbox2);
00369 gtk_widget_show(vbox);
00370
00371 gtk_container_add(GTK_CONTAINER(away_window), vbox);
00372
00373 gtk_window_set_policy(GTK_WINDOW(away_window), FALSE, FALSE, TRUE);
00374
00375 gtk_widget_show(away_window);
00376 }
00377
00378 gtk_window_set_title(GTK_WINDOW(away_window), _("New Away Message"));
00379 eb_icon(away_window->window);
00380 gtk_signal_connect(GTK_OBJECT(away_window), "destroy",
00381 GTK_SIGNAL_FUNC(destroy), NULL);
00382 away_open = 1;
00383 }
00384
00385 void show_away(GtkWidget *w, gchar *a_message)
00386 {
00387 GList * list;
00388 eb_local_account * ela = NULL;
00389
00390 if (!is_away) {
00391 GtkWidget *label;
00392 GtkWidget *vbox;
00393
00394 awaybox = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00395 gtk_widget_realize(awaybox);
00396
00397 vbox = gtk_vbox_new(FALSE, 0);
00398
00399 label = gtk_label_new(_("You are currently away, click \"I'm back\" to return"));
00400
00401 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5);
00402 gtk_widget_show(label);
00403
00404 label = gtk_label_new(_("This is the away message that will \nbe sent to people messaging you.\nYou may edit this message if you wish."));
00405
00406 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5);
00407 gtk_widget_show(label);
00408
00409
00410 away_message = gtk_entry_new();
00411 gtk_entry_set_text(GTK_ENTRY(away_message), (strlen(a_message)>0 ? a_message : _("User is currently away")) );
00412 gtk_box_pack_start(GTK_BOX(vbox), away_message, TRUE, TRUE, 5);
00413 gtk_widget_show(away_message);
00414
00415 label = gtk_button_new_with_label(_("I'm Back"));
00416 gtk_signal_connect_object(GTK_OBJECT(label), "clicked",
00417 GTK_SIGNAL_FUNC(imback), GTK_OBJECT(awaybox));
00418
00419 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 0);
00420 GTK_WIDGET_SET_FLAGS(label, GTK_CAN_DEFAULT);
00421 gtk_widget_grab_default(label);
00422 gtk_widget_show(label);
00423
00424
00425 gtk_signal_connect_object(GTK_OBJECT(awaybox), "destroy",
00426 GTK_SIGNAL_FUNC(destroy_away), GTK_OBJECT(awaybox));
00427
00428 gtk_container_add(GTK_CONTAINER(awaybox), vbox);
00429 gtk_widget_show(vbox);
00430 }
00431
00432 gtk_window_set_title(GTK_WINDOW(awaybox), _("Away"));
00433 eb_icon(awaybox->window);
00434 gtk_container_border_width(GTK_CONTAINER(awaybox), 2);
00435 gtk_widget_show(awaybox);
00436 is_away = 1;
00437
00438 for (list = accounts; list; list = g_list_next(list)) {
00439 ela = list->data;
00440
00441 if(ela->connected)
00442 eb_services[ela->service_id].sc->set_away(ela, a_message);
00443 }
00444 }
00445