comparison console/gntconv.c @ 14388:f4af666fafe3

[gaim-migrate @ 17094] Contactize the conversations. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Thu, 31 Aug 2006 04:39:21 +0000
parents 573b36b628f6
children a57d7f0cfc0c
comparison
equal deleted inserted replaced
14387:39b0124a455d 14388:f4af666fafe3
20 20
21 #define PREF_ROOT "/gaim/gnt/conversations" 21 #define PREF_ROOT "/gaim/gnt/conversations"
22 22
23 #include "config.h" 23 #include "config.h"
24 24
25 GHashTable *ggconvs;
26
27 typedef struct _GGConv GGConv; 25 typedef struct _GGConv GGConv;
28 typedef struct _GGConvChat GGConvChat; 26 typedef struct _GGConvChat GGConvChat;
29 typedef struct _GGConvIm GGConvIm; 27 typedef struct _GGConvIm GGConvIm;
30 28
31 struct _GGConv 29 struct _GGConv
32 { 30 {
33 GaimConversation *conv; 31 GList *list;
32 GaimConversation *active_conv;
33 /*GaimConversation *conv;*/
34 34
35 GntWidget *window; /* the container */ 35 GntWidget *window; /* the container */
36 GntWidget *entry; /* entry */ 36 GntWidget *entry; /* entry */
37 GntWidget *tv; /* text-view */ 37 GntWidget *tv; /* text-view */
38 38
59 if (key[0] == '\r' && key[1] == 0) 59 if (key[0] == '\r' && key[1] == 0)
60 { 60 {
61 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry)); 61 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
62 if (*text == '/') 62 if (*text == '/')
63 { 63 {
64 GaimConversation *conv = ggconv->conv; 64 GaimConversation *conv = ggconv->active_conv;
65 GaimCmdStatus status; 65 GaimCmdStatus status;
66 const char *cmdline = text + 1; 66 const char *cmdline = text + 1;
67 char *error = NULL, *escape; 67 char *error = NULL, *escape;
68 68
69 escape = g_markup_escape_text(cmdline, -1); 69 escape = g_markup_escape_text(cmdline, -1);
110 #endif 110 #endif
111 } 111 }
112 else 112 else
113 { 113 {
114 char *escape = g_markup_escape_text(text, -1); 114 char *escape = g_markup_escape_text(text, -1);
115 switch (gaim_conversation_get_type(ggconv->conv)) 115 switch (gaim_conversation_get_type(ggconv->active_conv))
116 { 116 {
117 case GAIM_CONV_TYPE_IM: 117 case GAIM_CONV_TYPE_IM:
118 gaim_conv_im_send_with_flags(GAIM_CONV_IM(ggconv->conv), escape, GAIM_MESSAGE_SEND); 118 gaim_conv_im_send_with_flags(GAIM_CONV_IM(ggconv->active_conv), escape, GAIM_MESSAGE_SEND);
119 break; 119 break;
120 case GAIM_CONV_TYPE_CHAT: 120 case GAIM_CONV_TYPE_CHAT:
121 gaim_conv_chat_send(GAIM_CONV_CHAT(ggconv->conv), escape); 121 gaim_conv_chat_send(GAIM_CONV_CHAT(ggconv->active_conv), escape);
122 break; 122 break;
123 default: 123 default:
124 g_free(escape); 124 g_free(escape);
125 g_return_val_if_reached(FALSE); 125 g_return_val_if_reached(FALSE);
126 } 126 }
149 } 149 }
150 150
151 static void 151 static void
152 closing_window(GntWidget *window, GGConv *ggconv) 152 closing_window(GntWidget *window, GGConv *ggconv)
153 { 153 {
154 GList *list = ggconv->list;
154 ggconv->window = NULL; 155 ggconv->window = NULL;
155 gaim_conversation_destroy(ggconv->conv); 156 while (list) {
157 GaimConversation *conv = list->data;
158 list = list->next;
159 gaim_conversation_destroy(conv);
160 }
156 } 161 }
157 162
158 static void 163 static void
159 size_changed_cb(GntWidget *w, int width, int height) 164 size_changed_cb(GntWidget *w, int width, int height)
160 { 165 {
167 { 172 {
168 gaim_prefs_set_int(PREF_ROOT "/position/x", x); 173 gaim_prefs_set_int(PREF_ROOT "/position/x", x);
169 gaim_prefs_set_int(PREF_ROOT "/position/y", y); 174 gaim_prefs_set_int(PREF_ROOT "/position/y", y);
170 } 175 }
171 176
177 static GaimConversation *
178 find_conv_with_contact(GaimConversation *conv)
179 {
180 GaimBlistNode *node;
181 GaimBuddy *buddy = gaim_find_buddy(conv->account, conv->name);
182 GaimConversation *ret = NULL;
183
184 if (!buddy)
185 return NULL;
186
187 for (node = ((GaimBlistNode*)buddy)->parent->child; node; node = node->next) {
188 if (node == (GaimBlistNode*)buddy)
189 continue;
190 if ((ret = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM,
191 ((GaimBuddy*)node)->name, ((GaimBuddy*)node)->account)) != NULL)
192 break;
193 }
194 return ret;
195 }
196
172 static void 197 static void
173 gg_create_conversation(GaimConversation *conv) 198 gg_create_conversation(GaimConversation *conv)
174 { 199 {
175 GGConv *ggc = g_hash_table_lookup(ggconvs, conv); 200 GGConv *ggc = conv->ui_data;
176 char *title; 201 char *title;
177 GaimConversationType type; 202 GaimConversationType type;
203 GaimConversation *cc;
178 204
179 if (ggc) 205 if (ggc)
180 return; 206 return;
181 207
182 ggc = g_new0(GGConv, 1); 208 cc = find_conv_with_contact(conv);
183 g_hash_table_insert(ggconvs, conv, ggc); 209 if (cc && cc->ui_data)
184 210 ggc = cc->ui_data;
185 ggc->conv = conv; 211 else
212 ggc = g_new0(GGConv, 1);
213
214 ggc->list = g_list_prepend(ggc->list, conv);
215 ggc->active_conv = conv;
186 conv->ui_data = ggc; 216 conv->ui_data = ggc;
217
218 if (cc && cc->ui_data) {
219 gg_conversation_set_active(conv);
220 return;
221 }
187 222
188 type = gaim_conversation_get_type(conv); 223 type = gaim_conversation_get_type(conv);
189 title = g_strdup_printf(_("%s"), gaim_conversation_get_title(conv)); 224 title = g_strdup_printf(_("%s"), gaim_conversation_get_title(conv));
225
190 ggc->window = gnt_box_new(FALSE, TRUE); 226 ggc->window = gnt_box_new(FALSE, TRUE);
191 gnt_box_set_title(GNT_BOX(ggc->window), title); 227 gnt_box_set_title(GNT_BOX(ggc->window), title);
192 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE); 228 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE);
193 gnt_box_set_pad(GNT_BOX(ggc->window), 0); 229 gnt_box_set_pad(GNT_BOX(ggc->window), 0);
194 gnt_widget_set_name(ggc->window, "conversation-window"); 230 gnt_widget_set_name(ggc->window, "conversation-window");
220 } 256 }
221 257
222 static void 258 static void
223 gg_destroy_conversation(GaimConversation *conv) 259 gg_destroy_conversation(GaimConversation *conv)
224 { 260 {
225 g_hash_table_remove(ggconvs, conv); 261 /* do stuff here */
262 GGConv *ggc = conv->ui_data;
263 ggc->list = g_list_remove(ggc->list, conv);
264 if (ggc->list && conv == ggc->active_conv)
265 ggc->active_conv = ggc->list->data;
266
267 if (ggc->list == NULL) {
268 gnt_widget_destroy(ggc->window);
269 g_free(ggc);
270 }
226 } 271 }
227 272
228 static void 273 static void
229 gg_write_common(GaimConversation *conv, const char *who, const char *message, 274 gg_write_common(GaimConversation *conv, const char *who, const char *message,
230 GaimMessageFlags flags, time_t mtime) 275 GaimMessageFlags flags, time_t mtime)
231 { 276 {
232 GGConv *ggconv = g_hash_table_lookup(ggconvs, conv); /* XXX: ggconv = conv->ui_data; should do */ 277 GGConv *ggconv = conv->ui_data;
233 char *strip, *newline; 278 char *strip, *newline;
234 GntTextFormatFlags fl = 0; 279 GntTextFormatFlags fl = 0;
235 int pos; 280 int pos;
236 281
237 g_return_if_fail(ggconv != NULL); 282 g_return_if_fail(ggconv != NULL);
283
284 if (ggconv->active_conv != conv) {
285 if (flags & (GAIM_MESSAGE_SEND | GAIM_MESSAGE_RECV))
286 gg_conversation_set_active(conv);
287 else
288 return;
289 }
238 290
239 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv)); 291 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv));
240 292
241 gnt_text_view_next_line(GNT_TEXT_VIEW(ggconv->tv)); 293 gnt_text_view_next_line(GNT_TEXT_VIEW(ggconv->tv));
242 294
406 .custom_smiley_add = NULL, 458 .custom_smiley_add = NULL,
407 .custom_smiley_write = NULL, 459 .custom_smiley_write = NULL,
408 .custom_smiley_close = NULL 460 .custom_smiley_close = NULL
409 }; 461 };
410 462
411 static void
412 destroy_ggconv(gpointer data)
413 {
414 GGConv *ggconv = data;
415 gnt_widget_destroy(ggconv->window);
416 g_free(ggconv);
417 }
418
419 GaimConversationUiOps *gg_conv_get_ui_ops() 463 GaimConversationUiOps *gg_conv_get_ui_ops()
420 { 464 {
421 return &conv_ui_ops; 465 return &conv_ui_ops;
422 } 466 }
423 467
534 return GAIM_CMD_STATUS_OK; 578 return GAIM_CMD_STATUS_OK;
535 } 579 }
536 580
537 void gg_conversation_init() 581 void gg_conversation_init()
538 { 582 {
539 ggconvs = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, destroy_ggconv);
540
541 gaim_prefs_add_none(PREF_ROOT); 583 gaim_prefs_add_none(PREF_ROOT);
542 gaim_prefs_add_none(PREF_ROOT "/size"); 584 gaim_prefs_add_none(PREF_ROOT "/size");
543 gaim_prefs_add_int(PREF_ROOT "/size/width", 70); 585 gaim_prefs_add_int(PREF_ROOT "/size/width", 70);
544 gaim_prefs_add_int(PREF_ROOT "/size/height", 20); 586 gaim_prefs_add_int(PREF_ROOT "/size/height", 20);
545 gaim_prefs_add_none(PREF_ROOT "/position"); 587 gaim_prefs_add_none(PREF_ROOT "/position");
584 cmd_show_window, _("statuses: Show the savedstatuses window."), gg_savedstatus_show_all); 626 cmd_show_window, _("statuses: Show the savedstatuses window."), gg_savedstatus_show_all);
585 } 627 }
586 628
587 void gg_conversation_uninit() 629 void gg_conversation_uninit()
588 { 630 {
589 g_hash_table_destroy(ggconvs); 631 }
590 ggconvs = NULL; 632
591 } 633 void gg_conversation_set_active(GaimConversation *conv)
592 634 {
635 GGConv *ggconv = conv->ui_data;
636
637 g_return_if_fail(ggconv);
638 g_return_if_fail(g_list_find(ggconv->list, conv));
639
640 ggconv->active_conv = conv;
641 gnt_screen_rename_widget(ggconv->window, gaim_conversation_get_title(conv));
642 }
643