2008-02-04 [colin] 3.2.0cvs75
[claws.git] / src / mainwindow.c
1 /*
2    Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3    Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "defs.h"
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtkwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtksignal.h>
28 #include <gtk/gtkvbox.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtkstatusbar.h>
31 #include <gtk/gtkprogressbar.h>
32 #include <gtk/gtkhpaned.h>
33 #include <gtk/gtkvpaned.h>
34 #include <gtk/gtkcheckmenuitem.h>
35 #include <gtk/gtkitemfactory.h>
36 #include <gtk/gtkeditable.h>
37 #include <gtk/gtkmenu.h>
38 #include <gtk/gtkmenuitem.h>
39 #include <gtk/gtkhandlebox.h>
40 #include <gtk/gtktoolbar.h>
41 #include <gtk/gtkbutton.h>
42 #include <gtk/gtktooltips.h>
43 #include <string.h>
44
45 #include "main.h"
46 #include "mainwindow.h"
47 #include "folderview.h"
48 #include "foldersel.h"
49 #include "summaryview.h"
50 #include "summary_search.h"
51 #include "messageview.h"
52 #include "message_search.h"
53 #include "headerview.h"
54 #include "menu.h"
55 #include "stock_pixmap.h"
56 #include "folder.h"
57 #include "inc.h"
58 #include "log.h"
59 #include "compose.h"
60 #include "procmsg.h"
61 #include "import.h"
62 #include "export.h"
63 #include "edittags.h"
64 #include "prefs_common.h"
65 #include "prefs_actions.h"
66 #include "prefs_filtering.h"
67 #include "prefs_account.h"
68 #include "prefs_summary_column.h"
69 #include "prefs_folder_column.h"
70 #include "prefs_template.h"
71 #include "action.h"
72 #include "account.h"
73 #include "addressbook.h"
74 #include "logwindow.h"
75 #include "manage_window.h"
76 #include "alertpanel.h"
77 #include "statusbar.h"
78 #include "inputdialog.h"
79 #include "utils.h"
80 #include "gtkutils.h"
81 #include "codeconv.h"
82 #include "about.h"
83 #include "manual.h"
84 #include "version.h"
85 #include "ssl_manager.h"
86 #include "sslcertwindow.h"
87 #include "prefs_gtk.h"
88 #include "pluginwindow.h"
89 #include "hooks.h"
90 #include "progressindicator.h"
91 #include "localfolder.h"
92 #include "filtering.h"
93 #include "folderutils.h"
94 #include "foldersort.h"
95 #include "icon_legend.h"
96 #include "colorlabel.h"
97 #include "tags.h"
98 #include "textview.h"
99 #include "imap.h"
100 #include "socket.h"
101 #include "printing.h"
102
103 #define AC_LABEL_WIDTH  240
104
105 /* list of all instantiated MainWindow */
106 static GList *mainwin_list = NULL;
107
108 static GdkCursor *watch_cursor = NULL;
109 static GdkCursor *hand_cursor = NULL;
110
111 static gint iconified_count = 0;
112
113 static void main_window_menu_callback_block     (MainWindow     *mainwin);
114 static void main_window_menu_callback_unblock   (MainWindow     *mainwin);
115
116 static void main_window_show_cur_account        (MainWindow     *mainwin);
117 #ifndef MAEMO
118 static void main_window_separation_change       (MainWindow     *mainwin,
119                                                  LayoutType      layout_mode);
120 #endif
121 static void main_window_set_widgets             (MainWindow     *mainwin,
122                                                  LayoutType      layout_mode);
123
124 static void toolbar_child_attached              (GtkWidget      *widget,
125                                                  GtkWidget      *child,
126                                                  gpointer        data);
127 static void toolbar_child_detached              (GtkWidget      *widget,
128                                                  GtkWidget      *child,
129                                                  gpointer        data);
130 #ifndef MAEMO
131 static gboolean ac_label_button_pressed         (GtkWidget      *widget,
132                                                  GdkEventButton *event,
133                                                  gpointer        data);
134 #endif
135 static gint main_window_close_cb                (GtkWidget      *widget,
136                                                  GdkEventAny    *event,
137                                                  gpointer        data);
138
139 static void main_window_size_allocate_cb        (GtkWidget      *widget,
140                                                  GtkAllocation  *allocation,
141                                                  gpointer        data);
142 static void folder_window_size_allocate_cb      (GtkWidget      *widget,
143                                                  GtkAllocation  *allocation,
144                                                  gpointer        data);
145 static void message_window_size_allocate_cb     (GtkWidget      *widget,
146                                                  GtkAllocation  *allocation,
147                                                  gpointer        data);
148
149 static void update_folderview_cb (MainWindow    *mainwin,
150                                   guint          action,
151                                   GtkWidget     *widget);
152 static void add_mailbox_cb       (MainWindow    *mainwin,
153                                   guint          action,
154                                   GtkWidget     *widget);
155 static void foldersort_cb        (MainWindow    *mainwin,
156                                   guint          action,
157                                   GtkWidget     *widget);
158 static void import_mbox_cb       (MainWindow    *mainwin,
159                                   guint          action,
160                                   GtkWidget     *widget);
161 static void export_mbox_cb       (MainWindow    *mainwin,
162                                   guint          action,
163                                   GtkWidget     *widget);
164 static void export_list_mbox_cb  (MainWindow    *mainwin, 
165                                   guint          action,
166                                   GtkWidget     *widget);
167 static void empty_trash_cb       (MainWindow    *mainwin,
168                                   guint          action,
169                                   GtkWidget     *widget);
170
171 static void save_as_cb           (MainWindow    *mainwin,
172                                   guint          action,
173                                   GtkWidget     *widget);
174 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
175 static void page_setup_cb        (MainWindow    *mainwin,
176                                   guint          action,
177                                   GtkWidget     *widget);
178 #endif
179
180 static void print_cb             (MainWindow    *mainwin,
181                                   guint          action,
182                                   GtkWidget     *widget);
183 static void app_exit_cb          (MainWindow    *mainwin,
184                                   guint          action,
185                                   GtkWidget     *widget);
186
187 static void search_cb            (MainWindow    *mainwin,
188                                   guint          action,
189                                   GtkWidget     *widget);
190
191 static void toggle_message_cb    (MainWindow    *mainwin,
192                                   guint          action,
193                                   GtkWidget     *widget);
194 static void toggle_toolbar_cb    (MainWindow    *mainwin,
195                                   guint          action,
196                                   GtkWidget     *widget);
197 static void toggle_col_headers_cb(MainWindow    *mainwin,
198                                   guint          action,
199                                   GtkWidget     *widget);
200 #ifndef MAEMO
201 static void toggle_statusbar_cb  (MainWindow    *mainwin,
202                                   guint          action,
203                                   GtkWidget     *widget);
204 static void set_layout_cb        (MainWindow    *mainwin,
205                                   guint          action,
206                                   GtkWidget     *widget);
207 #endif
208 static void addressbook_open_cb (MainWindow     *mainwin,
209                                  guint           action,
210                                  GtkWidget      *widget);
211 static void log_window_show_cb  (MainWindow     *mainwin,
212                                  guint           action,
213                                  GtkWidget      *widget);
214 static void filtering_debug_window_show_cb      (MainWindow     *mainwin,
215                                  guint           action,
216                                  GtkWidget      *widget);
217
218 static void inc_cancel_cb               (MainWindow     *mainwin,
219                                          guint           action,
220                                          GtkWidget      *widget);
221
222 static void open_msg_cb                 (MainWindow     *mainwin,
223                                          guint           action,
224                                          GtkWidget      *widget);
225
226 static void view_source_cb              (MainWindow     *mainwin,
227                                          guint           action,
228                                          GtkWidget      *widget);
229
230 static void show_all_header_cb          (MainWindow     *mainwin,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void hide_quotes_cb(MainWindow   *mainwin,
235                                          guint           action,
236                                          GtkWidget      *widget);
237
238 static void move_to_cb                  (MainWindow     *mainwin,
239                                          guint           action,
240                                          GtkWidget      *widget);
241 static void copy_to_cb                  (MainWindow     *mainwin,
242                                          guint           action,
243                                          GtkWidget      *widget);
244 static void delete_cb                   (MainWindow     *mainwin,
245                                          guint           action,
246                                          GtkWidget      *widget);
247 static void delete_trash_cb                     (MainWindow     *mainwin,
248                                          guint           action,
249                                          GtkWidget      *widget);
250
251 static void cancel_cb                   (MainWindow     *mainwin,
252                                          guint           action,
253                                          GtkWidget      *widget);
254
255 static void mark_cb                     (MainWindow     *mainwin,
256                                          guint           action,
257                                          GtkWidget      *widget);
258 static void unmark_cb                   (MainWindow     *mainwin,
259                                          guint           action,
260                                          GtkWidget      *widget);
261
262 static void mark_as_unread_cb           (MainWindow     *mainwin,
263                                          guint           action,
264                                          GtkWidget      *widget);
265 static void mark_as_read_cb             (MainWindow     *mainwin,
266                                          guint           action,
267                                          GtkWidget      *widget);
268 static void mark_all_read_cb            (MainWindow     *mainwin,
269                                          guint           action,
270                                          GtkWidget      *widget);
271 static void mark_as_spam_cb             (MainWindow     *mainwin, 
272                                          guint           action,
273                                          GtkWidget      *widget);
274
275 static void ignore_thread_cb            (MainWindow     *mainwin, 
276                                          guint           action,
277                                          GtkWidget      *widget);
278 static void unignore_thread_cb          (MainWindow     *mainwin, 
279                                          guint           action,
280                                          GtkWidget      *widget);
281 static void watch_thread_cb             (MainWindow     *mainwin, 
282                                          guint           action,
283                                          GtkWidget      *widget);
284 static void unwatch_thread_cb           (MainWindow     *mainwin, 
285                                          guint           action,
286                                          GtkWidget      *widget);
287 static void lock_msgs_cb                (MainWindow     *mainwin, 
288                                          guint           action,
289                                          GtkWidget      *widget);
290 static void unlock_msgs_cb              (MainWindow     *mainwin, 
291                                          guint           action,
292                                          GtkWidget      *widget);
293
294 static void reedit_cb                   (MainWindow     *mainwin,
295                                          guint           action,
296                                          GtkWidget      *widget);
297
298 static void add_address_cb              (MainWindow     *mainwin,
299                                          guint           action,
300                                          GtkWidget      *widget);
301
302 static void set_charset_cb              (MainWindow     *mainwin,
303                                          guint           action,
304                                          GtkWidget      *widget);
305
306 static void set_decode_cb               (MainWindow     *mainwin,
307                                          guint           action,
308                                          GtkWidget      *widget);
309
310 static void hide_read_messages   (MainWindow    *mainwin,
311                                   guint          action,
312                                   GtkWidget     *widget);
313
314 static void thread_cb            (MainWindow    *mainwin,
315                                   guint          action,
316                                   GtkWidget     *widget);
317 static void expand_threads_cb    (MainWindow    *mainwin,
318                                   guint          action,
319                                   GtkWidget     *widget);
320 static void collapse_threads_cb  (MainWindow    *mainwin,
321                                   guint          action,
322                                   GtkWidget     *widget);
323
324 static void set_summary_display_item_cb  (MainWindow    *mainwin,
325                                   guint          action,
326                                   GtkWidget     *widget);
327 static void set_folder_display_item_cb   (MainWindow    *mainwin,
328                                   guint          action,
329                                   GtkWidget     *widget);
330 static void sort_summary_cb      (MainWindow    *mainwin,
331                                   guint          action,
332                                   GtkWidget     *widget);
333 static void sort_summary_type_cb (MainWindow    *mainwin,
334                                   guint          action,
335                                   GtkWidget     *widget);
336 static void attract_by_subject_cb(MainWindow    *mainwin,
337                                   guint          action,
338                                   GtkWidget     *widget);
339
340 static void delete_duplicated_cb (MainWindow    *mainwin,
341                                   guint          action,
342                                   GtkWidget     *widget);
343 static void delete_duplicated_all_cb (MainWindow        *mainwin,
344                                   guint          action,
345                                   GtkWidget     *widget);
346 static void filter_cb            (MainWindow    *mainwin,
347                                   guint          action,
348                                   GtkWidget     *widget);
349 static void process_cb           (MainWindow    *mainwin,
350                                   guint          action,
351                                   GtkWidget     *widget);
352 static void execute_summary_cb   (MainWindow    *mainwin,
353                                   guint          action,
354                                   GtkWidget     *widget);
355 static void update_summary_cb    (MainWindow    *mainwin,
356                                   guint          action,
357                                   GtkWidget     *widget);
358
359 static void prev_cb              (MainWindow    *mainwin,
360                                   guint          action,
361                                   GtkWidget     *widget);
362 static void next_cb              (MainWindow    *mainwin,
363                                   guint          action,
364                                   GtkWidget     *widget);
365 static void next_unread_cb       (MainWindow    *mainwin,
366                                   guint          action,
367                                   GtkWidget     *widget);
368 static void prev_unread_cb       (MainWindow    *mainwin,
369                                   guint          action,
370                                   GtkWidget     *widget);
371
372 static void prev_new_cb          (MainWindow    *mainwin,
373                                   guint          action,
374                                   GtkWidget     *widget);
375 static void next_new_cb          (MainWindow    *mainwin,
376                                   guint          action,
377                                   GtkWidget     *widget);
378 static void prev_marked_cb       (MainWindow    *mainwin,
379                                   guint          action,
380                                   GtkWidget     *widget);
381 static void next_marked_cb       (MainWindow    *mainwin,
382                                   guint          action,
383                                   GtkWidget     *widget);
384 static void prev_labeled_cb      (MainWindow    *mainwin,
385                                   guint          action,
386                                   GtkWidget     *widget);
387 static void next_labeled_cb      (MainWindow    *mainwin,
388                                   guint          action,
389                                   GtkWidget     *widget);
390 static void last_read_cb         (MainWindow    *mainwin,
391                                   guint          action,
392                                   GtkWidget     *widget);
393 static void parent_cb            (MainWindow    *mainwin,
394                                   guint          action,
395                                   GtkWidget     *widget);
396
397 static void goto_folder_cb       (MainWindow    *mainwin,
398                                   guint          action,
399                                   GtkWidget     *widget);
400 static void goto_unread_folder_cb(MainWindow    *mainwin,
401                                   guint          action,
402                                   GtkWidget     *widget);
403
404 static void copy_cb              (MainWindow    *mainwin,
405                                   guint          action,
406                                   GtkWidget     *widget);
407 static void allsel_cb            (MainWindow    *mainwin,
408                                   guint          action,
409                                   GtkWidget     *widget);
410 static void select_thread_cb     (MainWindow    *mainwin,
411                                   guint          action,
412                                   GtkWidget     *widget);
413 static void delete_thread_cb     (MainWindow    *mainwin,
414                                   guint          action,
415                                   GtkWidget     *widget);
416
417 static void create_filter_cb     (MainWindow    *mainwin,
418                                   guint          action,
419                                   GtkWidget     *widget);
420 static void create_processing_cb (MainWindow    *mainwin,
421                                   guint          action,
422                                   GtkWidget     *widget);
423 static void open_urls_cb         (MainWindow    *mainwin,
424                                   guint          action,
425                                   GtkWidget     *widget);
426
427 static void prefs_template_open_cb      (MainWindow     *mainwin,
428                                          guint           action,
429                                          GtkWidget      *widget);
430 static void prefs_actions_open_cb       (MainWindow     *mainwin,
431                                          guint           action,
432                                          GtkWidget      *widget);
433 static void prefs_tags_open_cb          (MainWindow     *mainwin,
434                                          guint           action,
435                                          GtkWidget      *widget);
436 static void prefs_account_open_cb       (MainWindow     *mainwin,
437                                          guint           action,
438                                          GtkWidget      *widget);
439
440 static void prefs_pre_processing_open_cb  (MainWindow   *mainwin,
441                                            guint         action,
442                                            GtkWidget    *widget);
443
444 static void prefs_post_processing_open_cb (MainWindow   *mainwin,
445                                            guint         action,
446                                            GtkWidget    *widget);
447
448 static void prefs_filtering_open_cb     (MainWindow     *mainwin,
449                                          guint           action,
450                                          GtkWidget      *widget);
451 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
452 static void ssl_manager_open_cb         (MainWindow     *mainwin,
453                                          guint           action,
454                                          GtkWidget      *widget);
455 #endif
456 static void new_account_cb       (MainWindow    *mainwin,
457                                   guint          action,
458                                   GtkWidget     *widget);
459
460 static void account_selector_menu_cb     (GtkMenuItem   *menuitem,
461                                           gpointer       data);
462 static void account_receive_menu_cb      (GtkMenuItem   *menuitem,
463                                           gpointer       data);
464 #ifndef MAEMO
465 static void account_compose_menu_cb      (GtkMenuItem   *menuitem,
466                                           gpointer       data);
467 #endif
468 static void prefs_open_cb       (GtkMenuItem    *menuitem,
469                                  gpointer        data);
470 static void plugins_open_cb     (GtkMenuItem    *menuitem,
471                                  gpointer        data);
472
473 static void online_switch_clicked(GtkButton     *btn, 
474                                   gpointer data);
475
476 static void manual_open_cb       (MainWindow    *mainwin,
477                                   guint          action,
478                                   GtkWidget     *widget);
479
480 static void legend_open_cb       (GtkMenuItem   *menuitem,
481                                   gpointer       data);
482
483 static void scan_tree_func       (Folder        *folder,
484                                   FolderItem    *item,
485                                   gpointer       data);
486                                   
487 static void toggle_work_offline_cb(MainWindow *mainwin, guint action, GtkWidget *widget);
488
489 static void addr_harvest_cb      ( MainWindow  *mainwin,
490                                    guint       action,
491                                    GtkWidget   *widget );
492
493 static void addr_harvest_msg_cb  ( MainWindow  *mainwin,
494                                    guint       action,
495                                    GtkWidget   *widget );
496 static void sync_cb              ( MainWindow *mainwin, 
497                                    guint action, 
498                                    GtkWidget *widget );
499
500 static gboolean mainwindow_focus_in_event       (GtkWidget      *widget, 
501                                                  GdkEventFocus  *focus,
502                                                  gpointer        data);
503 static gboolean mainwindow_visibility_event_cb  (GtkWidget      *widget, 
504                                                  GdkEventVisibility     *state,
505                                                  gpointer        data);
506 static gboolean mainwindow_state_event_cb       (GtkWidget      *widget, 
507                                                  GdkEventWindowState    *state,
508                                                  gpointer        data);
509 static void main_window_reply_cb                        (MainWindow     *mainwin, 
510                                                  guint           action,
511                                                  GtkWidget      *widget);
512 static gboolean mainwindow_progressindicator_hook       (gpointer        source,
513                                                  gpointer        userdata);
514
515 static gint mailing_list_create_submenu(GtkItemFactory *ifactory,
516                                        MsgInfo *msginfo);
517
518 static gint mailing_list_populate_submenu(GtkWidget *menu, const gchar * list_header);
519         
520 static void get_url_part(const gchar **buf, gchar *url_decoded, gint maxlen);
521
522 static void mailing_list_compose(GtkWidget *w, gpointer *data);
523  
524 static void mailing_list_open_uri(GtkWidget *w, gpointer *data);
525 #define  SEPARATE_ACTION 500 
526 static void mainwindow_quicksearch              (MainWindow     *mainwin, 
527                                                  guint           action, 
528                                                  GtkWidget      *widget);
529 static gboolean any_folder_want_synchronise(void);
530
531 static GtkItemFactoryEntry mainwin_entries[] =
532 {
533         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
534         {N_("/_File/_Add mailbox"),             NULL, NULL, 0, "<Branch>"},
535         {N_("/_File/_Add mailbox/MH..."),       NULL, add_mailbox_cb, 0, NULL},
536         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
537         {N_("/_File/Change folder order..."),   NULL, foldersort_cb,  0, NULL},
538         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
539         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
540         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
541         {N_("/_File/Exp_ort selected to mbox file..."), 
542                                                 NULL, export_list_mbox_cb, 0, NULL},
543         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
544         {N_("/_File/Empty all _Trash folders"), "<shift>D", empty_trash_cb, 0, NULL},
545         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
546         {N_("/_File/_Save as..."),              "<control>S", save_as_cb, 0, NULL},
547 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
548         {N_("/_File/Page setup..."),            NULL, page_setup_cb, 0, NULL},
549 #endif
550         {N_("/_File/_Print..."),                "<control>P", print_cb, 0, NULL},
551         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
552         {N_("/_File/_Work offline"),            "<control>W", toggle_work_offline_cb, 0, "<ToggleItem>"},
553         {N_("/_File/Synchronise folders"),      "<control><shift>S", sync_cb, 0, NULL},
554         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
555         /* {N_("/_File/_Close"),                "<alt>W", app_exit_cb, 0, NULL}, */
556         {N_("/_File/E_xit"),                    "<control>Q", app_exit_cb, 0, NULL},
557
558         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
559         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
560         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
561         {N_("/_Edit/Select _thread"),           NULL, select_thread_cb, 0, NULL},
562         {N_("/_Edit/_Delete thread"),           NULL, delete_thread_cb, 0, NULL},
563         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
564         {N_("/_Edit/_Find in current message..."),
565                                                 "<control>F", search_cb, 0, NULL},
566         {N_("/_Edit/_Search folder..."),        "<shift><control>F", search_cb, 1, NULL},
567         {N_("/_Edit/_Quick search"),            "slash", mainwindow_quicksearch, 0, NULL},
568         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
569         {N_("/_View/Show or hi_de"),            NULL, NULL, 0, "<Branch>"},
570         {N_("/_View/Show or hi_de/_Toolbar"),
571                                                 NULL, NULL, 0, "<Branch>"},
572         {N_("/_View/Show or hi_de/_Toolbar/Text _below icons"),
573                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
574         {N_("/_View/Show or hi_de/_Toolbar/Text be_side icons"),
575                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH_HORIZ, "/View/Show or hide/Toolbar/Text below icons"},
576         {N_("/_View/Show or hi_de/_Toolbar/_Icons only"),
577                                                 NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Show or hide/Toolbar/Text below icons"},
578         {N_("/_View/Show or hi_de/_Toolbar/_Text only"),
579                                                 NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Show or hide/Toolbar/Text below icons"},
580 #ifndef MAEMO
581         {N_("/_View/Show or hi_de/_Toolbar/_Hide"),
582                                                 NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Show or hide/Toolbar/Text below icons"},
583 #endif
584         {N_("/_View/Show or hi_de/_Message view"),
585                                                 "V", toggle_message_cb, 0, "<ToggleItem>"},
586 #ifndef MAEMO
587         {N_("/_View/Show or hi_de/Status _bar"),
588                                                 NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
589 #endif
590         {N_("/_View/Show or hi_de/Column headers"),
591                                                 NULL, toggle_col_headers_cb, 0, "<ToggleItem>"},
592         {N_("/_View/Set displayed _columns"),   NULL, NULL, 0, "<Branch>"},
593         {N_("/_View/Set displayed _columns/in _Folder list..."),        NULL, set_folder_display_item_cb, 0, NULL},
594         {N_("/_View/Set displayed _columns/in _Message list..."),NULL, set_summary_display_item_cb, 0, NULL},
595
596         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
597 #ifndef MAEMO
598         {N_("/_View/La_yout"),                  NULL, NULL, 0, "<Branch>"},
599         {N_("/_View/Layout/_Standard"),         NULL, set_layout_cb, NORMAL_LAYOUT, "<RadioItem>"},
600         {N_("/_View/Layout/_Three columns"),    NULL, set_layout_cb, VERTICAL_LAYOUT, "/View/Layout/Standard"},
601         {N_("/_View/Layout/_Wide message"),     NULL, set_layout_cb, WIDE_LAYOUT, "/View/Layout/Standard"},
602         {N_("/_View/Layout/W_ide message list"),NULL, set_layout_cb, WIDE_MSGLIST_LAYOUT, "/View/Layout/Standard"},
603         {N_("/_View/Layout/S_mall screen"),     NULL, set_layout_cb, SMALL_LAYOUT, "/View/Layout/Standard"},
604         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
605 #endif
606         {N_("/_View/_Sort"),                    NULL, NULL, 0, "<Branch>"},
607         {N_("/_View/_Sort/by _number"),         NULL, sort_summary_cb, SORT_BY_NUMBER, "<RadioItem>"},
608         {N_("/_View/_Sort/by S_ize"),           NULL, sort_summary_cb, SORT_BY_SIZE, "/View/Sort/by number"},
609         {N_("/_View/_Sort/by _Date"),           NULL, sort_summary_cb, SORT_BY_DATE, "/View/Sort/by number"},
610         {N_("/_View/_Sort/by Thread date"),     NULL, sort_summary_cb, SORT_BY_THREAD_DATE, "/View/Sort/by number"},
611         {N_("/_View/_Sort/by _From"),           NULL, sort_summary_cb, SORT_BY_FROM, "/View/Sort/by number"},
612         {N_("/_View/_Sort/by _To"),             NULL, sort_summary_cb, SORT_BY_TO, "/View/Sort/by number"},
613         {N_("/_View/_Sort/by S_ubject"),        NULL, sort_summary_cb, SORT_BY_SUBJECT, "/View/Sort/by number"},
614         {N_("/_View/_Sort/by _color label"),    NULL, sort_summary_cb, SORT_BY_LABEL, "/View/Sort/by number"},
615         {N_("/_View/_Sort/by tag"),             NULL, sort_summary_cb, SORT_BY_TAGS, "/View/Sort/by number"},
616         {N_("/_View/_Sort/by _mark"),           NULL, sort_summary_cb, SORT_BY_MARK, "/View/Sort/by number"},
617         {N_("/_View/_Sort/by _status"),         NULL, sort_summary_cb, SORT_BY_STATUS, "/View/Sort/by number"},
618         {N_("/_View/_Sort/by a_ttachment"),
619                                                 NULL, sort_summary_cb, SORT_BY_MIME, "/View/Sort/by number"},
620         {N_("/_View/_Sort/by score"),           NULL, sort_summary_cb, SORT_BY_SCORE, "/View/Sort/by number"},
621         {N_("/_View/_Sort/by locked"),          NULL, sort_summary_cb, SORT_BY_LOCKED, "/View/Sort/by number"},
622         {N_("/_View/_Sort/D_on't sort"),        NULL, sort_summary_cb, SORT_BY_NONE, "/View/Sort/by number"},
623         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
624         {N_("/_View/_Sort/Ascending"),          NULL, sort_summary_type_cb, SORT_ASCENDING, "<RadioItem>"},
625         {N_("/_View/_Sort/Descending"),         NULL, sort_summary_type_cb, SORT_DESCENDING, "/View/Sort/Ascending"},
626         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
627         {N_("/_View/_Sort/_Attract by subject"),
628                                                 NULL, attract_by_subject_cb, 0, NULL},
629         {N_("/_View/Th_read view"),             "<control>T", thread_cb, 0, "<ToggleItem>"},
630         {N_("/_View/E_xpand all threads"),      NULL, expand_threads_cb, 0, NULL},
631         {N_("/_View/Co_llapse all threads"),    NULL, collapse_threads_cb, 0, NULL},
632         {N_("/_View/_Hide read messages"),      NULL, hide_read_messages, 0, "<ToggleItem>"},
633
634         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
635         {N_("/_View/_Go to"),                   NULL, NULL, 0, "<Branch>"},
636         {N_("/_View/_Go to/_Previous message"), "P", prev_cb, 0, NULL},
637         {N_("/_View/_Go to/_Next message"),     "N", next_cb, 0, NULL},
638         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
639         {N_("/_View/_Go to/P_revious unread message"),
640                                                 "<shift>P", prev_unread_cb, 0, NULL},
641         {N_("/_View/_Go to/N_ext unread message"),
642                                                 "<shift>N", next_unread_cb, 0, NULL},
643         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
644         {N_("/_View/_Go to/Previous ne_w message"),     NULL, prev_new_cb, 0, NULL},
645         {N_("/_View/_Go to/Ne_xt new message"), NULL, next_new_cb, 0, NULL},
646         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
647         {N_("/_View/_Go to/Previous _marked message"),
648                                                 NULL, prev_marked_cb, 0, NULL},
649         {N_("/_View/_Go to/Next m_arked message"),
650                                                 NULL, next_marked_cb, 0, NULL},
651         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
652         {N_("/_View/_Go to/Previous _labeled message"),
653                                                 NULL, prev_labeled_cb, 0, NULL},
654         {N_("/_View/_Go to/Next la_beled message"),
655                                                 NULL, next_labeled_cb, 0, NULL},
656         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
657         {N_("/_View/_Go to/Last read message"),
658                                                 NULL, last_read_cb, 0, NULL},
659         {N_("/_View/_Go to/Parent message"),
660                                                 "<control>Up", parent_cb, 0, NULL},
661         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
662         {N_("/_View/_Go to/Next unread _folder"),       "<shift>G", goto_unread_folder_cb, 0, NULL},
663         {N_("/_View/_Go to/_Other folder..."),  "G", goto_folder_cb, 0, NULL},
664         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
665
666 #define ENC_SEPARATOR \
667         {N_("/_View/Character _encoding/---"),          NULL, NULL, 0, "<Separator>"}
668 #define ENC_ACTION(action) \
669          NULL, set_charset_cb, action, "/View/Character encoding/Auto detect"
670
671         {N_("/_View/Character _encoding"),              NULL, NULL, 0, "<Branch>"},
672         {N_("/_View/Character _encoding/_Auto detect"),
673          NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
674         ENC_SEPARATOR,
675
676         {N_("/_View/Character _encoding/7bit ASCII (US-ASC_II)"),
677          ENC_ACTION(C_US_ASCII)},
678         {N_("/_View/Character _encoding/Unicode (_UTF-8)"),
679          ENC_ACTION(C_UTF_8)},
680         ENC_SEPARATOR,
681
682         {N_("/_View/Character _encoding/Western European"),NULL, NULL, 0, "<Branch>"},
683         {N_("/_View/Character _encoding/Western European/ISO-8859-_1"),
684          ENC_ACTION(C_ISO_8859_1)},
685         {N_("/_View/Character _encoding/Western European/ISO-8859-15"),
686          ENC_ACTION(C_ISO_8859_15)},
687         {N_("/_View/Character _encoding/Western European/Windows-1252"),
688          ENC_ACTION(C_WINDOWS_1252)},
689
690         {N_("/_View/Character _encoding/Central European (ISO-8859-_2)"),
691          ENC_ACTION(C_ISO_8859_2)},
692
693         {N_("/_View/Character _encoding/Baltic"),       NULL, NULL, 0, "<Branch>"},
694         {N_("/_View/Character _encoding/Baltic/ISO-8859-13"),
695          ENC_ACTION(C_ISO_8859_13)},
696         {N_("/_View/Character _encoding/Baltic/ISO-8859-_4"),
697          ENC_ACTION(C_ISO_8859_4)},
698
699         {N_("/_View/Character _encoding/Greek (ISO-8859-_7)"),
700          ENC_ACTION(C_ISO_8859_7)},
701
702         {N_("/_View/Character _encoding/Hebrew"),       NULL, NULL, 0, "<Branch>"},
703         {N_("/_View/Character _encoding/Hebrew/ISO-8859-_8"),
704          ENC_ACTION(C_ISO_8859_8)},
705         {N_("/_View/Character _encoding/Hebrew/Windows-1255"),
706          ENC_ACTION(C_CP1255)},
707
708         {N_("/_View/Character _encoding/Arabic"),       NULL, NULL, 0, "<Branch>"},
709         {N_("/_View/Character _encoding/Arabic/ISO-8859-_6"),
710          ENC_ACTION(C_ISO_8859_6)},
711         {N_("/_View/Character _encoding/Arabic/Windows-1256"),
712          ENC_ACTION(C_CP1256)},
713
714         {N_("/_View/Character _encoding/Turkish (ISO-8859-_9)"),
715          ENC_ACTION(C_ISO_8859_9)},
716
717         {N_("/_View/Character _encoding/Cyrillic"),     NULL, NULL, 0, "<Branch>"},
718         {N_("/_View/Character _encoding/Cyrillic/ISO-8859-_5"),
719          ENC_ACTION(C_ISO_8859_5)},
720         {N_("/_View/Character _encoding/Cyrillic/KOI8-_R"),
721          ENC_ACTION(C_KOI8_R)},
722         {N_("/_View/Character _encoding/Cyrillic/KOI8-U"),
723          ENC_ACTION(C_KOI8_U)},
724         {N_("/_View/Character _encoding/Cyrillic/Windows-1251"),
725          ENC_ACTION(C_CP1251)},
726
727         {N_("/_View/Character _encoding/Japanese"),     NULL, NULL, 0, "<Branch>"},
728         {N_("/_View/Character _encoding/Japanese/ISO-2022-_JP"),
729          ENC_ACTION(C_ISO_2022_JP)},
730         {N_("/_View/Character _encoding/Japanese/ISO-2022-JP-2"),
731          ENC_ACTION(C_ISO_2022_JP_2)},
732         {N_("/_View/Character _encoding/Japanese/_EUC-JP"),
733          ENC_ACTION(C_EUC_JP)},
734         {N_("/_View/Character _encoding/Japanese/_Shift__JIS"),
735          ENC_ACTION(C_SHIFT_JIS)},
736
737         {N_("/_View/Character _encoding/Chinese"),      NULL, NULL, 0, "<Branch>"},
738         {N_("/_View/Character _encoding/Chinese/Simplified (_GB2312)"),
739          ENC_ACTION(C_GB2312)},
740         {N_("/_View/Character _encoding/Chinese/Simplified (GBK)"),
741          ENC_ACTION(C_GBK)},
742         {N_("/_View/Character _encoding/Chinese/Traditional (_Big5)"),
743          ENC_ACTION(C_BIG5)},
744         {N_("/_View/Character _encoding/Chinese/Traditional (EUC-_TW)"),
745          ENC_ACTION(C_EUC_TW)},
746         {N_("/_View/Character _encoding/Chinese/ISO-2022-_CN"),
747          ENC_ACTION(C_ISO_2022_CN)},
748
749         {N_("/_View/Character _encoding/Korean"),       NULL, NULL, 0, "<Branch>"},
750         {N_("/_View/Character _encoding/Korean/EUC-_KR"),
751          ENC_ACTION(C_EUC_KR)},
752         {N_("/_View/Character _encoding/Korean/ISO-2022-KR"),
753          ENC_ACTION(C_ISO_2022_KR)},
754
755         {N_("/_View/Character _encoding/Thai"),         NULL, NULL, 0, "<Branch>"},
756         {N_("/_View/Character _encoding/Thai/TIS-620"),
757          ENC_ACTION(C_TIS_620)},
758         {N_("/_View/Character _encoding/Thai/Windows-874"),
759          ENC_ACTION(C_WINDOWS_874)},
760
761 #undef ENC_SEPARATOR
762 #undef ENC_ACTION
763
764 #define DEC_SEPARATOR \
765         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"}
766 #define DEC_ACTION(action) \
767          NULL, set_decode_cb, action, "/View/Decode/Auto detect"
768         {N_("/_View/Decode"),           NULL, NULL, 0, "<Branch>"},
769         {N_("/_View/Decode/_Auto detect"),
770          NULL, set_decode_cb, 0, "<RadioItem>"},
771         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"},
772         {N_("/_View/Decode/_8bit"),             DEC_ACTION(ENC_8BIT)},
773         {N_("/_View/Decode/_Quoted printable"), DEC_ACTION(ENC_QUOTED_PRINTABLE)},
774         {N_("/_View/Decode/_Base64"),           DEC_ACTION(ENC_BASE64)},
775         {N_("/_View/Decode/_Uuencode"),         DEC_ACTION(ENC_X_UUENCODE)},
776
777 #undef DEC_SEPARATOR
778 #undef DEC_ACTION
779
780         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
781         {N_("/_View/Open in new _window"),      "<control><alt>N", open_msg_cb, 0, NULL},
782         {N_("/_View/Mess_age source"),          "<control>U", view_source_cb, 0, NULL},
783         {N_("/_View/All headers"),              "<control>H", show_all_header_cb, 0, "<ToggleItem>"},
784         {N_("/_View/Quotes"),                   NULL, NULL, 0, "<Branch>"},
785         {N_("/_View/Quotes/_Fold all"),         "<control><shift>Q", hide_quotes_cb, 1, "<ToggleItem>"},
786         {N_("/_View/Quotes/Fold from level _2"),NULL, hide_quotes_cb, 2, "<ToggleItem>"},
787         {N_("/_View/Quotes/Fold from level _3"),NULL, hide_quotes_cb, 3, "<ToggleItem>"},
788         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
789         {N_("/_View/_Update summary"),          "<control><alt>U", update_summary_cb,  0, NULL},
790
791         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
792         {N_("/_Message/Recei_ve"),              NULL, NULL, 0, "<Branch>"},
793         {N_("/_Message/Recei_ve/Get from _current account"),
794                                                 "<control>I",   inc_mail_cb, 0, NULL},
795         {N_("/_Message/Recei_ve/Get from _all accounts"),
796                                                 "<shift><control>I", inc_all_account_mail_cb, 0, NULL},
797         {N_("/_Message/Recei_ve/Cancel receivin_g"),
798                                                 NULL, inc_cancel_cb, 0, NULL},
799         {N_("/_Message/Recei_ve/---"),          NULL, NULL, 0, "<Separator>"},
800         {N_("/_Message/_Send queued messages"), NULL, send_queue_cb, 0, NULL},
801         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
802         {N_("/_Message/Compose a_n email message"),     "<control>M", compose_mail_cb, 0, NULL},
803         {N_("/_Message/Compose a news message"),        NULL,   compose_news_cb, 0, NULL},
804         {N_("/_Message/_Reply"),                "<control>R",   main_window_reply_cb, COMPOSE_REPLY, NULL},
805         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
806         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", main_window_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
807         {N_("/_Message/Repl_y to/_sender"),     NULL, main_window_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
808         {N_("/_Message/Repl_y to/mailing _list"),
809                                                 "<control>L", main_window_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
810         {N_("/_Message/Follow-up and reply to"),NULL, main_window_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
811         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
812         {N_("/_Message/_Forward"),              "<control><alt>F", main_window_reply_cb, COMPOSE_FORWARD_INLINE, NULL},
813         {N_("/_Message/For_ward as attachment"),        NULL, main_window_reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
814         {N_("/_Message/Redirect"),              NULL, main_window_reply_cb, COMPOSE_REDIRECT, NULL},
815
816         {N_("/_Message/Mailing-_List"),                 NULL, NULL, 0, "<Branch>"},
817         {N_("/_Message/Mailing-_List/Post"),            NULL, NULL, 0, "<Branch>"},
818         {N_("/_Message/Mailing-_List/Help"),            NULL, NULL, 0, "<Branch>"},
819         {N_("/_Message/Mailing-_List/Subscribe"),       NULL, NULL, 0, "<Branch>"},
820         {N_("/_Message/Mailing-_List/Unsubscribe"),     NULL, NULL, 0, "<Branch>"},
821         {N_("/_Message/Mailing-_List/View archive"),    NULL, NULL, 0, "<Branch>"},
822         {N_("/_Message/Mailing-_List/Contact owner"),   NULL, NULL, 0, "<Branch>"},
823         
824         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
825         {N_("/_Message/M_ove..."),              "<control>O", move_to_cb, 0, NULL},
826         {N_("/_Message/_Copy..."),              "<shift><control>O", copy_to_cb, 0, NULL},
827         {N_("/_Message/Move to _trash"),        "<control>D", delete_trash_cb,  0, NULL},
828         {N_("/_Message/_Delete..."),            NULL, delete_cb,  0, NULL},
829         {N_("/_Message/Cancel a news message"), "", cancel_cb,  0, NULL},
830         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
831         {N_("/_Message/_Mark"),                 NULL, NULL, 0, "<Branch>"},
832         {N_("/_Message/_Mark/_Mark"),           "<shift>asterisk", mark_cb, 0, NULL},
833         {N_("/_Message/_Mark/_Unmark"),         "U", unmark_cb, 0, NULL},
834         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
835         {N_("/_Message/_Mark/Mark as unr_ead"), "<shift>exclam", mark_as_unread_cb, 0, NULL},
836         {N_("/_Message/_Mark/Mark as rea_d"),   NULL, mark_as_read_cb, 0, NULL},
837         {N_("/_Message/_Mark/Mark all _read"),  NULL, mark_all_read_cb, 0, NULL},
838         {N_("/_Message/_Mark/Ignore thread"),   NULL, ignore_thread_cb, 0, NULL},
839         {N_("/_Message/_Mark/Unignore thread"), NULL, unignore_thread_cb, 0, NULL},
840         {N_("/_Message/_Mark/Watch thread"),    NULL, watch_thread_cb, 0, NULL},
841         {N_("/_Message/_Mark/Unwatch thread"),  NULL, unwatch_thread_cb, 0, NULL},
842         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
843         {N_("/_Message/_Mark/Mark as _spam"),   NULL, mark_as_spam_cb, 1, NULL},
844         {N_("/_Message/_Mark/Mark as _ham"),    NULL, mark_as_spam_cb, 0, NULL},
845         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
846         {N_("/_Message/_Mark/Lock"),            NULL, lock_msgs_cb, 0, NULL},
847         {N_("/_Message/_Mark/Unlock"),          NULL, unlock_msgs_cb, 0, NULL},
848         {N_("/_Message/Color la_bel"),          NULL, NULL,            0, NULL},
849         {N_("/_Message/T_ags"),                 NULL, NULL,            0, NULL},
850         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
851         {N_("/_Message/Re-_edit"),              NULL, reedit_cb, 0, NULL},
852
853         {N_("/_Tools"),                         NULL, NULL, 0, "<Branch>"},
854         {N_("/_Tools/_Address book..."),        "<shift><control>A", addressbook_open_cb, 0, NULL},
855         {N_("/_Tools/Add sender to address boo_k"),
856                                                 NULL, add_address_cb, 0, NULL},
857         {N_("/_Tools/_Harvest addresses"),      NULL, NULL, 0, "<Branch>"},
858         {N_("/_Tools/_Harvest addresses/from _Folder..."),
859                                                 NULL, addr_harvest_cb, 0, NULL},
860         {N_("/_Tools/_Harvest addresses/from _Messages..."),
861                                                 NULL, addr_harvest_msg_cb, 0, NULL},
862         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
863         {N_("/_Tools/_Filter all messages in folder"),
864                                                 NULL, filter_cb, 0, NULL},
865         {N_("/_Tools/Filter _selected messages"),
866                                                 NULL, filter_cb, 1, NULL},
867         {N_("/_Tools/Run folder processing rules"),
868                                                 NULL, process_cb, 0, NULL},
869         {N_("/_Tools/_Create filter rule"),     NULL, NULL, 0, "<Branch>"},
870         {N_("/_Tools/_Create filter rule/_Automatically"),
871                                                 NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
872         {N_("/_Tools/_Create filter rule/by _From"),
873                                                 NULL, create_filter_cb, FILTER_BY_FROM, NULL},
874         {N_("/_Tools/_Create filter rule/by _To"),
875                                                 NULL, create_filter_cb, FILTER_BY_TO, NULL},
876         {N_("/_Tools/_Create filter rule/by _Subject"),
877                                                 NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
878         {N_("/_Tools/C_reate processing rule"), NULL, NULL, 0, "<Branch>"},
879         {N_("/_Tools/C_reate processing rule/_Automatically"),
880                                                 NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
881         {N_("/_Tools/C_reate processing rule/by _From"),
882                                                 NULL, create_processing_cb, FILTER_BY_FROM, NULL},
883         {N_("/_Tools/C_reate processing rule/by _To"),
884                                                 NULL, create_processing_cb, FILTER_BY_TO, NULL},
885         {N_("/_Tools/C_reate processing rule/by _Subject"),
886                                                 NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
887         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
888         {N_("/_Tools/List _URLs..."),           "<shift><control>U", open_urls_cb, 0, NULL},
889         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
890         {N_("/_Tools/Actio_ns"),                NULL, NULL, 0, "<Branch>"},
891         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
892         {N_("/_Tools/Ch_eck for new messages in all folders"),
893                                                 NULL, update_folderview_cb, 0, NULL},
894         {N_("/_Tools/Delete du_plicated messages"),
895                                                 NULL, NULL, 0, "<Branch>"},
896         {N_("/_Tools/Delete du_plicated messages/In selected folder"),
897                                                 NULL, delete_duplicated_cb,   0, NULL},
898         {N_("/_Tools/Delete du_plicated messages/In all folders"),
899                                                 NULL, delete_duplicated_all_cb,   0, NULL},
900         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
901         {N_("/_Tools/E_xecute"),                "X", execute_summary_cb, 0, NULL},
902 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
903         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
904         {N_("/_Tools/SSL cer_tificates..."),    
905                                                 NULL, ssl_manager_open_cb, 0, NULL},
906 #endif
907         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
908         {N_("/_Tools/Filtering Log"),           NULL, filtering_debug_window_show_cb, 0, NULL},
909         {N_("/_Tools/Network _Log"),            "<shift><control>L", log_window_show_cb, 0, NULL},
910
911         {N_("/_Configuration"),                 NULL, NULL, 0, "<Branch>"},
912         {N_("/_Configuration/C_hange current account"),
913                                                 NULL, NULL, 0, "<Branch>"},
914         {N_("/_Configuration/_Preferences for current account..."),
915                                                 NULL, prefs_account_open_cb, 0, NULL},
916         {N_("/_Configuration/Create _new account..."),
917                                                 NULL, new_account_cb, 0, NULL},
918         {N_("/_Configuration/_Edit accounts..."),
919                                                 NULL, account_edit_open, 0, NULL},
920         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
921         {N_("/_Configuration/P_references..."),
922                                                 NULL, prefs_open_cb, 0, NULL},
923         {N_("/_Configuration/Pre-pr_ocessing..."),
924                                                 NULL, prefs_pre_processing_open_cb, 0, NULL},
925         {N_("/_Configuration/Post-pro_cessing..."),
926                                                 NULL, prefs_post_processing_open_cb, 0, NULL},
927         {N_("/_Configuration/_Filtering..."),
928                                                 NULL, prefs_filtering_open_cb, 0, NULL},
929         {N_("/_Configuration/_Templates..."),   NULL, prefs_template_open_cb, 0, NULL},
930         {N_("/_Configuration/_Actions..."),     NULL, prefs_actions_open_cb, 0, NULL},
931         {N_("/_Configuration/Tag_s..."),        NULL, prefs_tags_open_cb, 0, NULL},
932         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
933         {N_("/_Configuration/Plu_gins..."),     NULL, plugins_open_cb, 0, NULL},
934
935         {N_("/_Help"),                          NULL, NULL, 0, "<Branch>"},
936         {N_("/_Help/_Manual"),                  NULL, manual_open_cb, MANUAL_MANUAL_CLAWS, NULL},
937         {N_("/_Help/_Online User-contributed FAQ"),     
938                                                 NULL, manual_open_cb, MANUAL_FAQ_CLAWS, NULL},
939         {N_("/_Help/Icon _Legend"),             NULL, legend_open_cb, 0, NULL},
940         {N_("/_Help/---"),                      NULL, NULL, 0, "<Separator>"},
941         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
942 };
943
944 static gboolean offline_ask_sync = TRUE;
945 static guint lastkey;
946 static gboolean is_obscured = FALSE;
947
948 static gboolean main_window_accel_activate (GtkAccelGroup *accelgroup,
949                                             GObject *arg1,
950                                             guint value,
951                                             GdkModifierType mod,
952                                             gpointer user_data) 
953 {
954         MainWindow *mainwin = (MainWindow *)user_data;
955
956         if (mainwin->summaryview &&
957             mainwin->summaryview->quicksearch &&
958             quicksearch_has_focus(mainwin->summaryview->quicksearch) &&
959             (mod == 0 || mod == GDK_SHIFT_MASK)) {
960                 quicksearch_pass_key(mainwin->summaryview->quicksearch, lastkey, mod);
961                 return TRUE;
962         }
963         return FALSE;
964 }
965
966 #define N_COLOR_LABELS colorlabel_get_color_count()
967
968 static void mainwindow_colorlabel_menu_item_activate_item_cb(GtkMenuItem *menu_item,
969                                                           gpointer data)
970 {
971         MainWindow *mainwin;
972         GtkMenuShell *menu;
973         GtkCheckMenuItem **items;
974         gint n;
975         GList *cur;
976         GSList *sel;
977
978         mainwin = (MainWindow *)data;
979         g_return_if_fail(mainwin != NULL);
980
981         sel = summary_get_selection(mainwin->summaryview);
982         if (!sel) return;
983
984         menu = GTK_MENU_SHELL(mainwin->colorlabel_menu);
985         g_return_if_fail(menu != NULL);
986
987         Xalloca(items, (N_COLOR_LABELS + 1) * sizeof(GtkWidget *), return);
988
989         /* NOTE: don't return prematurely because we set the "dont_toggle"
990          * state for check menu items */
991         g_object_set_data(G_OBJECT(menu), "dont_toggle",
992                           GINT_TO_POINTER(1));
993
994         /* clear items. get item pointers. */
995         for (n = 0, cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
996                 if (GTK_IS_CHECK_MENU_ITEM(cur->data)) {
997                         gtk_check_menu_item_set_active
998                                 (GTK_CHECK_MENU_ITEM(cur->data), FALSE);
999                         items[n] = GTK_CHECK_MENU_ITEM(cur->data);
1000                         n++;
1001                 }
1002         }
1003
1004         if (n == (N_COLOR_LABELS + 1)) {
1005                 /* iterate all messages and set the state of the appropriate
1006                  * items */
1007                 for (; sel != NULL; sel = sel->next) {
1008                         MsgInfo *msginfo;
1009                         gint clabel;
1010
1011                         msginfo = (MsgInfo *)sel->data;
1012                         if (msginfo) {
1013                                 clabel = MSG_GET_COLORLABEL_VALUE(msginfo->flags);
1014                                 if (!items[clabel]->active)
1015                                         gtk_check_menu_item_set_active
1016                                                 (items[clabel], TRUE);
1017                         }
1018                 }
1019         } else
1020                 g_warning("invalid number of color elements (%d)\n", n);
1021
1022         g_slist_free(sel);
1023         /* reset "dont_toggle" state */
1024         g_object_set_data(G_OBJECT(menu), "dont_toggle",
1025                           GINT_TO_POINTER(0));
1026 }
1027
1028 static void mainwindow_colorlabel_menu_item_activate_cb(GtkWidget *widget,
1029                                                      gpointer data)
1030 {
1031         guint color = GPOINTER_TO_UINT(data);
1032         MainWindow *mainwin;
1033
1034         mainwin = g_object_get_data(G_OBJECT(widget), "mainwin");
1035         g_return_if_fail(mainwin != NULL);
1036
1037         /* "dont_toggle" state set? */
1038         if (g_object_get_data(G_OBJECT(mainwin->colorlabel_menu),
1039                                 "dont_toggle"))
1040                 return;
1041
1042         summary_set_colorlabel(mainwin->summaryview, color, NULL);
1043 }
1044
1045 static void mainwindow_tags_menu_item_activate_item_cb(GtkMenuItem *menu_item,
1046                                                           gpointer data)
1047 {
1048         MainWindow *mainwin;
1049         GtkMenuShell *menu;
1050         GList *cur;
1051         GSList *sel;
1052         GHashTable *menu_table = g_hash_table_new_full(
1053                                         g_direct_hash,
1054                                         g_direct_equal,
1055                                         NULL, NULL);
1056         GHashTable *menu_allsel_table = g_hash_table_new_full(
1057                                         g_direct_hash,
1058                                         g_direct_equal,
1059                                         NULL, NULL);
1060         gint sel_len;
1061         mainwin = (MainWindow *)data;
1062         g_return_if_fail(mainwin != NULL);
1063
1064         sel = summary_get_selection(mainwin->summaryview);
1065         if (!sel) return;
1066
1067         menu = GTK_MENU_SHELL(mainwin->tags_menu);
1068         g_return_if_fail(menu != NULL);
1069
1070         /* NOTE: don't return prematurely because we set the "dont_toggle"
1071          * state for check menu items */
1072         g_object_set_data(G_OBJECT(menu), "dont_toggle",
1073                           GINT_TO_POINTER(1));
1074
1075         /* clear items. get item pointers. */
1076         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
1077                 if (GTK_IS_CHECK_MENU_ITEM(cur->data)) {
1078                         gint id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cur->data),
1079                                 "tag_id"));
1080                         gtk_check_menu_item_set_active
1081                                 (GTK_CHECK_MENU_ITEM(cur->data), FALSE);
1082                                 
1083                         g_hash_table_insert(menu_table, GINT_TO_POINTER(id), GTK_CHECK_MENU_ITEM(cur->data));
1084                         g_hash_table_insert(menu_allsel_table, GINT_TO_POINTER(id), GINT_TO_POINTER(0));
1085                 }
1086         }
1087
1088         /* iterate all messages and set the state of the appropriate
1089          * items */
1090         sel_len = 0;
1091         for (; sel != NULL; sel = sel->next) {
1092                 MsgInfo *msginfo;
1093                 GSList *tags = NULL;
1094                 gint id;
1095                 GtkCheckMenuItem *item;
1096                 msginfo = (MsgInfo *)sel->data;
1097                 sel_len++;
1098                 if (msginfo) {
1099                         tags =  msginfo->tags;
1100                         if (!tags)
1101                                 continue;
1102
1103                         for (; tags; tags = tags->next) {
1104                                 gint num_checked = GPOINTER_TO_INT(g_hash_table_lookup(menu_allsel_table, tags->data));
1105                                 id = GPOINTER_TO_INT(tags->data);
1106                                 item = g_hash_table_lookup(menu_table, GINT_TO_POINTER(tags->data));
1107                                 if (item && !item->active) {
1108                                         gtk_check_menu_item_set_active
1109                                                 (item, TRUE);
1110                                 }
1111                                 num_checked++;
1112                                 g_hash_table_replace(menu_allsel_table, tags->data, GINT_TO_POINTER(num_checked));
1113                         }
1114                 }
1115         }
1116
1117         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
1118                 if (GTK_IS_CHECK_MENU_ITEM(cur->data)) {
1119                         gint id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cur->data),
1120                                 "tag_id"));
1121                         gint num_checked = GPOINTER_TO_INT(g_hash_table_lookup(menu_allsel_table, GINT_TO_POINTER(id)));
1122                         if (num_checked < sel_len && num_checked > 0)
1123                                 gtk_check_menu_item_set_inconsistent(GTK_CHECK_MENU_ITEM(cur->data), TRUE);
1124                         else
1125                                 gtk_check_menu_item_set_inconsistent(GTK_CHECK_MENU_ITEM(cur->data), FALSE);
1126                 }
1127         }
1128         g_slist_free(sel);
1129         g_hash_table_destroy(menu_table);
1130         g_hash_table_destroy(menu_allsel_table);
1131         /* reset "dont_toggle" state */
1132         g_object_set_data(G_OBJECT(menu), "dont_toggle",
1133                           GINT_TO_POINTER(0));
1134 }
1135
1136 static void mainwindow_tags_menu_item_activate_cb(GtkWidget *widget,
1137                                                      gpointer data)
1138 {
1139         gint id = GPOINTER_TO_INT(data);
1140         gboolean set = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1141         MainWindow *mainwin;
1142
1143         mainwin = g_object_get_data(G_OBJECT(widget), "mainwin");
1144         g_return_if_fail(mainwin != NULL);
1145
1146         /* "dont_toggle" state set? */
1147         if (g_object_get_data(G_OBJECT(mainwin->tags_menu),
1148                                 "dont_toggle"))
1149                 return;
1150
1151         if (!set)
1152                 id = -id;
1153         summary_set_tag(mainwin->summaryview, id, NULL);
1154 }
1155
1156 static void mainwindow_colorlabel_menu_create(MainWindow *mainwin, gboolean refresh)
1157 {
1158         GtkWidget *label_menuitem;
1159         GtkWidget *menu;
1160         GtkWidget *item;
1161         gint i;
1162
1163         label_menuitem = gtk_item_factory_get_item(mainwin->menu_factory,
1164                                                    "/Message/Color label");
1165         g_signal_connect(G_OBJECT(label_menuitem), "activate",
1166                          G_CALLBACK(mainwindow_colorlabel_menu_item_activate_item_cb),
1167                            mainwin);
1168         gtk_widget_show(label_menuitem);
1169
1170         menu = gtk_menu_new();
1171
1172         /* create sub items. for the menu item activation callback we pass the
1173          * index of label_colors[] as data parameter. for the None color we
1174          * pass an invalid (high) value. also we attach a data pointer so we
1175          * can always get back the Mainwindow pointer. */
1176
1177         item = gtk_check_menu_item_new_with_label(_("None"));
1178         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1179         g_signal_connect(G_OBJECT(item), "activate",
1180                          G_CALLBACK(mainwindow_colorlabel_menu_item_activate_cb),
1181                            GUINT_TO_POINTER(0));
1182         g_object_set_data(G_OBJECT(item), "mainwin", mainwin);
1183         gtk_widget_show(item);
1184
1185         gtk_widget_add_accelerator(item, "activate", 
1186                                    mainwin->menu_factory->accel_group, 
1187                                    GDK_0, GDK_CONTROL_MASK,
1188                                    GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1189
1190         item = gtk_menu_item_new();
1191         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1192         gtk_widget_show(item);
1193
1194         /* create pixmap/label menu items */
1195         for (i = 0; i < N_COLOR_LABELS; i++) {
1196                 item = colorlabel_create_check_color_menu_item(
1197                         i, refresh, MAINWIN_COLORMENU);
1198                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1199                 g_signal_connect(G_OBJECT(item), "activate",
1200                                  G_CALLBACK(mainwindow_colorlabel_menu_item_activate_cb),
1201                                  GUINT_TO_POINTER(i + 1));
1202                 g_object_set_data(G_OBJECT(item), "mainwin",
1203                                   mainwin);
1204                 gtk_widget_show(item);
1205                 if (i < 9)
1206                         gtk_widget_add_accelerator(item, "activate", 
1207                                    mainwin->menu_factory->accel_group, 
1208                                    GDK_1+i, GDK_CONTROL_MASK,
1209                                    GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1210         }
1211         gtk_widget_show(menu);
1212         gtk_menu_item_set_submenu(GTK_MENU_ITEM(label_menuitem), menu);
1213         mainwin->colorlabel_menu = menu;
1214 }
1215
1216 static void mainwindow_tags_menu_item_apply_tags_activate_cb(GtkWidget *widget,
1217                                                      gpointer data)
1218 {
1219         MainWindow *mainwin;
1220
1221         mainwin = g_object_get_data(G_OBJECT(widget), "mainwin");
1222         g_return_if_fail(mainwin != NULL);
1223
1224         /* "dont_toggle" state set? */
1225         if (g_object_get_data(G_OBJECT(mainwin->tags_menu),
1226                                 "dont_toggle"))
1227                 return;
1228         
1229         tag_apply_open(summary_get_selection(mainwin->summaryview));    
1230 }
1231
1232 static gint mainwin_tag_cmp_list(gconstpointer a, gconstpointer b)
1233 {
1234         gint id_a = GPOINTER_TO_INT(a);
1235         gint id_b = GPOINTER_TO_INT(b);
1236         const gchar *tag_a = tags_get_tag(id_a);
1237         const gchar *tag_b = tags_get_tag(id_b);
1238         
1239                 
1240         if (tag_a == NULL)
1241                 return tag_b == NULL ? 0:1;
1242         
1243         if (tag_b == NULL)
1244                 return tag_a == NULL ? 0:1;
1245  
1246         return g_utf8_collate(tag_a, tag_b);
1247 }
1248
1249 static void mainwindow_tags_menu_create(MainWindow *mainwin, gboolean refresh)
1250 {
1251         GtkWidget *label_menuitem;
1252         GtkWidget *menu;
1253         GtkWidget *item;
1254         GSList *cur = tags_get_list();
1255         GSList *orig = NULL;
1256         gboolean existing_tags = FALSE;
1257
1258         cur = orig = g_slist_sort(cur, mainwin_tag_cmp_list);
1259
1260         label_menuitem = gtk_item_factory_get_item(mainwin->menu_factory,
1261                                                    "/Message/Tags");
1262         g_signal_connect(G_OBJECT(label_menuitem), "activate",
1263                          G_CALLBACK(mainwindow_tags_menu_item_activate_item_cb),
1264                            mainwin);
1265
1266         gtk_widget_show(label_menuitem);
1267
1268         menu = gtk_menu_new();
1269
1270         /* create tags menu items */
1271         for (; cur; cur = cur->next) {
1272                 gint id = GPOINTER_TO_INT(cur->data);
1273                 const gchar *tag = tags_get_tag(id);
1274
1275                 item = gtk_check_menu_item_new_with_label(tag);
1276                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1277                 g_signal_connect(G_OBJECT(item), "activate",
1278                                  G_CALLBACK(mainwindow_tags_menu_item_activate_cb),
1279                                  GINT_TO_POINTER(id));
1280                 g_object_set_data(G_OBJECT(item), "mainwin",
1281                                   mainwin);
1282                 g_object_set_data(G_OBJECT(item), "tag_id",
1283                                   GINT_TO_POINTER(id));
1284                 gtk_widget_show(item);
1285                 existing_tags = TRUE;
1286         }
1287         if (existing_tags) {
1288                 /* separator */
1289                 item = gtk_menu_item_new();
1290                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1291                 gtk_widget_show(item);
1292         }
1293
1294         item = gtk_menu_item_new_with_label(_("Apply tags..."));
1295         gtk_widget_add_accelerator(item, "activate", 
1296                    mainwin->menu_factory->accel_group, 
1297                    GDK_T, GDK_CONTROL_MASK|GDK_SHIFT_MASK,
1298                    GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1299         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1300         g_signal_connect(G_OBJECT(item), "activate",
1301                          G_CALLBACK(mainwindow_tags_menu_item_apply_tags_activate_cb),
1302                          NULL);
1303         g_object_set_data(G_OBJECT(item), "mainwin",
1304                           mainwin);
1305         gtk_widget_show(item);
1306
1307         g_slist_free(orig);
1308         gtk_widget_show(menu);
1309         gtk_menu_item_set_submenu(GTK_MENU_ITEM(label_menuitem), menu);
1310         mainwin->tags_menu = menu;
1311 }
1312 #ifndef MAEMO
1313 static gboolean warning_icon_pressed(GtkWidget *widget, GdkEventButton *evt,
1314                                     MainWindow *mainwindow)
1315 {
1316         if (evt && evt->button == 1) {
1317                 log_window_show_error(mainwindow->logwin);
1318                 gtk_widget_hide(mainwindow->warning_btn);
1319         }
1320         return FALSE;
1321 }
1322
1323 static gboolean warning_visi_notify(GtkWidget *widget,
1324                                        GdkEventVisibility *event,
1325                                        MainWindow *mainwindow)
1326 {
1327         gdk_window_set_cursor(mainwindow->warning_btn->window, hand_cursor);
1328         return FALSE;
1329 }
1330
1331 static gboolean warning_leave_notify(GtkWidget *widget,
1332                                       GdkEventCrossing *event,
1333                                       MainWindow *mainwindow)
1334 {
1335         gdk_window_set_cursor(mainwindow->warning_btn->window, NULL);
1336         return FALSE;
1337 }
1338
1339 static gboolean warning_enter_notify(GtkWidget *widget,
1340                                       GdkEventCrossing *event,
1341                                       MainWindow *mainwindow)
1342 {
1343         gdk_window_set_cursor(mainwindow->warning_btn->window, hand_cursor);
1344         return FALSE;
1345 }
1346 #endif
1347 void mainwindow_show_error(void)
1348 {
1349         MainWindow *mainwin = mainwindow_get_mainwindow();
1350         gtk_widget_show(mainwin->warning_btn);
1351 }
1352
1353 void mainwindow_clear_error(MainWindow *mainwin)
1354 {
1355         gtk_widget_hide(mainwin->warning_btn);
1356 }
1357
1358 #define BREAK_ON_MODIFIER_KEY() \
1359         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1360
1361 static gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
1362                                     gpointer data)
1363 {
1364         MainWindow *mainwin = (MainWindow*) data;
1365         
1366         if (!mainwin || !event) 
1367                 return FALSE;
1368
1369         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
1370         {
1371                 lastkey = event->keyval;
1372                 return FALSE;
1373         }
1374
1375         switch (event->keyval) {
1376         case GDK_Q:             /* Quit */
1377                 BREAK_ON_MODIFIER_KEY();
1378
1379                 app_exit_cb(mainwin, 0, NULL);
1380                 return FALSE;
1381         case GDK_space:
1382                 if (gtk_window_is_active(GTK_WINDOW(mainwin->window))) {
1383                         if (mainwin->folderview && mainwin->summaryview
1384                             && ((!mainwin->summaryview->displayed
1385                                 && !mainwin->summaryview->selected) 
1386                                 || (mainwin->summaryview->folder_item
1387                                     && mainwin->summaryview->folder_item->total_msgs == 0))) {
1388                                 g_signal_stop_emission_by_name(G_OBJECT(widget), 
1389                                                "key_press_event");
1390                                 folderview_select_next_unread(mainwin->folderview, TRUE);
1391                         }
1392                 }
1393                 break;
1394 #ifdef MAEMO
1395         case GDK_F6:
1396                 if (maemo_mainwindow_is_fullscreen(widget)) {
1397                         gtk_window_unfullscreen(GTK_WINDOW(widget));
1398                 } else {
1399                         gtk_window_fullscreen(GTK_WINDOW(widget));
1400                 }
1401                 break;
1402         case GDK_Escape:
1403                 if (mainwin->summaryview && 
1404                     mainwin->summaryview->ext_messageview && 
1405                     mainwin->summaryview->ext_messageview->window && 
1406                     widget == mainwin->summaryview->ext_messageview->window) {
1407                         messageview_destroy(mainwin->summaryview->ext_messageview);
1408                 }
1409                 break;
1410 #endif
1411         default:
1412                 break;
1413         }
1414         return FALSE;
1415 }
1416
1417 #undef BREAK_ON_MODIFIER_KEY
1418
1419 #ifdef MAEMO
1420 void mainwindow_maemo_led_set(gboolean state) {
1421         static gint last_state = -1;
1422         if (last_state == state)
1423                 return;
1424         last_state = (gint)state;
1425         if (prefs_common.maemo_show_led) {
1426                 if(state) {
1427                   execute_command_line("/usr/bin/dbus-send --system --type=method_call "
1428                         "--dest=com.nokia.mce "
1429                         "/com/nokia/mce/request com.nokia.mce.request.req_led_pattern_activate "
1430                         "string:PatternCommunicationEvent", TRUE);
1431                   execute_command_line("/usr/bin/dbus-send --system --type=method_call "
1432                         "--dest=com.nokia.mce "
1433                         "/com/nokia/mce/request com.nokia.mce.request.req_led_pattern_activate "
1434                         "string:PatternCommunicationEmail", TRUE);
1435                 } else {
1436                   execute_command_line("/usr/bin/dbus-send --system --type=method_call "
1437                         "--dest=com.nokia.mce "
1438                         "/com/nokia/mce/request com.nokia.mce.request.req_led_pattern_deactivate "
1439                         "string:PatternCommunicationEvent", TRUE);
1440                   execute_command_line("/usr/bin/dbus-send --system --type=method_call "
1441                         "--dest=com.nokia.mce "
1442                         "/com/nokia/mce/request com.nokia.mce.request.req_led_pattern_deactivate "
1443                         "string:PatternCommunicationEmail", TRUE);
1444                 }
1445         } 
1446 }
1447
1448 static void led_update(FolderItem *removed_item)
1449 {
1450         guint new, unread, unreadmarked, marked, total;
1451
1452         folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
1453         if (removed_item) {
1454                 total -= removed_item->total_msgs;
1455                 new -= removed_item->new_msgs;
1456                 unread -= removed_item->unread_msgs;
1457         }
1458
1459         if (new > 0)
1460                 mainwindow_maemo_led_set(TRUE);
1461         else
1462                 mainwindow_maemo_led_set(FALSE);
1463 }
1464
1465 static gboolean maemo_folder_item_update_hook(gpointer source, gpointer data)
1466 {
1467         led_update(NULL);
1468
1469         return FALSE;
1470 }
1471
1472 static gboolean maemo_folder_update_hook(gpointer source, gpointer data)
1473 {
1474         FolderUpdateData *hookdata;
1475         hookdata = source;
1476         if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
1477                 led_update(hookdata->item);
1478         else
1479                 led_update(NULL);
1480
1481         return FALSE;
1482 }
1483
1484 static void main_window_install_maemo_hooks(MainWindow *mainwin)
1485 {
1486         gint maemo_item_hook_id, maemo_folder_hook_id;
1487         
1488         maemo_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, maemo_folder_item_update_hook, NULL);
1489         if (maemo_item_hook_id == -1) {
1490                 goto err_out_item;
1491         }
1492
1493         maemo_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, maemo_folder_update_hook, NULL);
1494         if (maemo_folder_hook_id == -1) {
1495                 goto err_out_folder;
1496         }
1497         
1498         return;
1499
1500 err_out_folder:
1501         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, maemo_item_hook_id);
1502 err_out_item:
1503         return;
1504 }
1505 #endif
1506
1507 MainWindow *main_window_create()
1508 {
1509         MainWindow *mainwin;
1510         GtkWidget *window;
1511         GtkWidget *vbox;
1512         GtkWidget *menubar;
1513         GtkWidget *handlebox;
1514         GtkWidget *vbox_body;
1515 #ifndef MAEMO
1516         GtkWidget *hbox_stat;
1517         GtkWidget *statusbar;
1518         GtkWidget *progressbar;
1519         GtkWidget *statuslabel;
1520         GtkWidget *ac_button;
1521         GtkWidget *ac_label;
1522         GtkWidget *online_pixmap;
1523         GtkWidget *offline_pixmap;
1524         GtkTooltips *tips;
1525         GtkWidget *warning_icon;
1526         GtkWidget *warning_btn;
1527 #endif
1528         GtkWidget *online_switch;
1529         GtkWidget *offline_switch;
1530         FolderView *folderview;
1531         SummaryView *summaryview;
1532         MessageView *messageview;
1533         GdkColormap *colormap;
1534         GdkColor color[4];
1535         gboolean success[4];
1536         GtkItemFactory *ifactory;
1537         GtkWidget *ac_menu;
1538         GtkWidget *menuitem;
1539         gint i;
1540         guint n_menu_entries;
1541
1542         static GdkGeometry geometry;
1543
1544         debug_print("Creating main window...\n");
1545         mainwin = g_new0(MainWindow, 1);
1546
1547         /* main window */
1548         window = GTK_WIDGET(gtkut_window_new(GTK_WINDOW_TOPLEVEL, "mainwindow"));
1549         gtk_window_set_title(GTK_WINDOW(window), PROG_VERSION);
1550         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
1551 #ifdef MAEMO
1552         prefs_common.layout_mode = SMALL_LAYOUT;
1553 #endif
1554         if (!geometry.min_height) {
1555                 geometry.min_width = 320;
1556                 geometry.min_height = 200;
1557         }
1558         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
1559                                       GDK_HINT_MIN_SIZE);
1560
1561         g_signal_connect(G_OBJECT(window), "delete_event",
1562                          G_CALLBACK(main_window_close_cb), mainwin);
1563         MANAGE_WINDOW_SIGNALS_CONNECT(window);
1564         g_signal_connect(G_OBJECT(window), "focus_in_event",
1565                          G_CALLBACK(mainwindow_focus_in_event),
1566                          mainwin);
1567         g_signal_connect(G_OBJECT(window), "key_press_event",
1568                          G_CALLBACK(mainwindow_key_pressed), mainwin);
1569
1570         gtk_widget_realize(window);
1571         gtk_widget_add_events(window, GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK);
1572         
1573
1574         gtkut_widget_set_app_icon(window);
1575
1576         vbox = gtk_vbox_new(FALSE, 0);
1577         gtk_widget_show(vbox);
1578         gtk_container_add(GTK_CONTAINER(window), vbox);
1579
1580         /* menu bar */
1581         n_menu_entries = sizeof(mainwin_entries) / sizeof(mainwin_entries[0]);
1582         menubar = menubar_create(window, mainwin_entries, 
1583                                  n_menu_entries, "<Main>", mainwin);
1584         gtk_widget_show(menubar);
1585
1586 #ifndef MAEMO
1587         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
1588 #endif
1589
1590         ifactory = gtk_item_factory_from_widget(menubar);
1591
1592 /*      gtk_widget_show(gtk_item_factory_get_item(ifactory,"/Message/Mailing-List"));
1593         main_create_mailing_list_menu (mainwin, NULL); */
1594
1595         if (prefs_common.toolbar_detachable) {
1596                 handlebox = gtk_handle_box_new();
1597                 gtk_widget_show(handlebox);
1598                 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
1599                 g_signal_connect(G_OBJECT(handlebox), "child_attached",
1600                                  G_CALLBACK(toolbar_child_attached), mainwin);
1601                 g_signal_connect(G_OBJECT(handlebox), "child_detached",
1602                                  G_CALLBACK(toolbar_child_detached), mainwin);
1603         } else {
1604                 handlebox = gtk_hbox_new(FALSE, 0);
1605                 gtk_widget_show(handlebox);
1606                 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
1607         }
1608         /* link window to mainwin->window to avoid gdk warnings */
1609         mainwin->window       = window;
1610         mainwin_list = g_list_append(mainwin_list, mainwin);
1611         
1612 #ifdef MAEMO
1613         mainwin->toolbar = toolbar_create(TOOLBAR_MAIN, 
1614                                           window, 
1615                                           (gpointer)mainwin);
1616 #else
1617         mainwin->toolbar = toolbar_create(TOOLBAR_MAIN, 
1618                                           handlebox, 
1619                                           (gpointer)mainwin);
1620 #endif
1621         toolbar_set_learn_button
1622                 (mainwin->toolbar,
1623                  LEARN_SPAM);
1624
1625         /* vbox that contains body */
1626         vbox_body = gtk_vbox_new(FALSE, BORDER_WIDTH);
1627         gtk_widget_show(vbox_body);
1628         gtk_container_set_border_width(GTK_CONTAINER(vbox_body), BORDER_WIDTH);
1629         gtk_box_pack_start(GTK_BOX(vbox), vbox_body, TRUE, TRUE, 0);
1630
1631 #ifndef MAEMO
1632         hbox_stat = gtk_hbox_new(FALSE, 2);
1633         gtk_box_pack_end(GTK_BOX(vbox_body), hbox_stat, FALSE, FALSE, 0);
1634
1635         tips = gtk_tooltips_new();
1636
1637         warning_icon = gtk_image_new_from_stock
1638                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1639         warning_btn = gtk_event_box_new();
1640         gtk_event_box_set_visible_window(GTK_EVENT_BOX(warning_btn), FALSE);
1641         
1642         mainwin->warning_btn      = warning_btn;
1643         
1644         g_signal_connect(G_OBJECT(warning_btn), "button-press-event", 
1645                          G_CALLBACK(warning_icon_pressed),
1646                          (gpointer) mainwin);
1647         g_signal_connect(G_OBJECT(warning_btn), "visibility-notify-event",
1648                          G_CALLBACK(warning_visi_notify), mainwin);
1649         g_signal_connect(G_OBJECT(warning_btn), "motion-notify-event",
1650                          G_CALLBACK(warning_visi_notify), mainwin);
1651         g_signal_connect(G_OBJECT(warning_btn), "leave-notify-event",
1652                          G_CALLBACK(warning_leave_notify), mainwin);
1653         g_signal_connect(G_OBJECT(warning_btn), "enter-notify-event",
1654                          G_CALLBACK(warning_enter_notify), mainwin);
1655
1656         gtk_container_add (GTK_CONTAINER(warning_btn), warning_icon);
1657
1658         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),warning_btn, 
1659                              _("Some error(s) happened. Click here to view log."), NULL);
1660         gtk_box_pack_start(GTK_BOX(hbox_stat), warning_btn, FALSE, FALSE, 0);
1661
1662         statusbar = statusbar_create();
1663         gtk_box_pack_start(GTK_BOX(hbox_stat), statusbar, TRUE, TRUE, 0);
1664
1665         progressbar = gtk_progress_bar_new();
1666         gtk_widget_set_size_request(progressbar, 120, 1);
1667         gtk_box_pack_start(GTK_BOX(hbox_stat), progressbar, FALSE, FALSE, 0);
1668
1669         online_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_ONLINE);
1670         offline_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_OFFLINE);
1671         online_switch = gtk_button_new ();
1672         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),online_switch, 
1673                              _("You are online. Click the icon to go offline"), NULL);
1674         offline_switch = gtk_button_new ();
1675         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),offline_switch, 
1676                              _("You are offline. Click the icon to go online"),
1677                              NULL);
1678         gtk_container_add (GTK_CONTAINER(online_switch), online_pixmap);
1679         gtk_button_set_relief (GTK_BUTTON(online_switch), GTK_RELIEF_NONE);
1680         g_signal_connect (G_OBJECT(online_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
1681         gtk_box_pack_start (GTK_BOX(hbox_stat), online_switch, FALSE, FALSE, 0);
1682         gtk_container_add (GTK_CONTAINER(offline_switch), offline_pixmap);
1683         gtk_button_set_relief (GTK_BUTTON(offline_switch), GTK_RELIEF_NONE);
1684         g_signal_connect (G_OBJECT(offline_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
1685         gtk_box_pack_start (GTK_BOX(hbox_stat), offline_switch, FALSE, FALSE, 0);
1686         
1687         statuslabel = gtk_label_new("");
1688         gtk_box_pack_start(GTK_BOX(hbox_stat), statuslabel, FALSE, FALSE, 0);
1689
1690         ac_button = gtk_button_new();
1691         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
1692                              ac_button, _("Select account"), NULL);
1693         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
1694         gtk_widget_set_size_request(ac_button, -1, 0);
1695         gtk_box_pack_end(GTK_BOX(hbox_stat), ac_button, FALSE, FALSE, 0);
1696         g_signal_connect(G_OBJECT(ac_button), "button_press_event",
1697                          G_CALLBACK(ac_label_button_pressed), mainwin);
1698
1699         ac_label = gtk_label_new("");
1700         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
1701
1702         gtk_widget_show_all(hbox_stat);
1703
1704         gtk_widget_hide(offline_switch);
1705         gtk_widget_hide(warning_btn);
1706 #else
1707         online_switch = gtk_button_new ();
1708         offline_switch = gtk_button_new ();
1709         g_signal_connect (G_OBJECT(online_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
1710         g_signal_connect (G_OBJECT(offline_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
1711 #endif
1712         /* create views */
1713         mainwin->folderview  = folderview  = folderview_create();
1714         mainwin->summaryview = summaryview = summary_create();
1715         mainwin->messageview = messageview = messageview_create(mainwin);
1716
1717         /* init log instances data before creating log views */
1718         set_log_title(LOG_PROTOCOL, _("Network log"));
1719         set_log_prefs(LOG_PROTOCOL,
1720                         &prefs_common.logwin_width,
1721                         &prefs_common.logwin_height);
1722         set_log_title(LOG_DEBUG_FILTERING, _("Filtering/processing debug log"));
1723         set_log_prefs(LOG_DEBUG_FILTERING,
1724                         &prefs_common.filtering_debugwin_width,
1725                         &prefs_common.filtering_debugwin_height);
1726
1727         /* setup log windows */
1728         mainwin->logwin = log_window_create(LOG_PROTOCOL);
1729         log_window_init(mainwin->logwin);
1730
1731         mainwin->filtering_debugwin = log_window_create(LOG_DEBUG_FILTERING);
1732         log_window_set_clipping(mainwin->logwin,
1733                                 prefs_common.cliplog,
1734                                 prefs_common.loglength);
1735
1736         log_window_init(mainwin->filtering_debugwin);
1737         log_window_set_clipping(mainwin->filtering_debugwin,
1738                                 prefs_common.filtering_debug_cliplog,
1739                                 prefs_common.filtering_debug_loglength);
1740         if (prefs_common.enable_filtering_debug)
1741                 log_message(LOG_DEBUG_FILTERING, _("filtering log enabled\n"));
1742         else
1743                 log_message(LOG_DEBUG_FILTERING, _("filtering log disabled\n"));
1744
1745         folderview->mainwin      = mainwin;
1746         folderview->summaryview  = summaryview;
1747
1748         summaryview->mainwin     = mainwin;
1749         summaryview->folderview  = folderview;
1750         summaryview->messageview = messageview;
1751         summaryview->window      = window;
1752
1753         mainwin->vbox           = vbox;
1754         mainwin->menubar        = menubar;
1755         mainwin->menu_factory   = ifactory;
1756         mainwin->handlebox      = handlebox;
1757         mainwin->vbox_body      = vbox_body;
1758         mainwin->online_switch  = online_switch;
1759         mainwin->offline_switch    = offline_switch;
1760 #ifndef MAEMO
1761         messageview->statusbar  = statusbar;
1762         mainwin->statusbar      = statusbar;
1763         mainwin->hbox_stat      = hbox_stat;
1764         mainwin->progressbar    = progressbar;
1765         mainwin->statuslabel    = statuslabel;
1766         mainwin->online_pixmap  = online_pixmap;
1767         mainwin->offline_pixmap = offline_pixmap;
1768         mainwin->ac_button      = ac_button;
1769         mainwin->ac_label       = ac_label;
1770         /* set context IDs for status bar */
1771         mainwin->mainwin_cid = gtk_statusbar_get_context_id
1772                 (GTK_STATUSBAR(statusbar), "Main Window");
1773         mainwin->folderview_cid = gtk_statusbar_get_context_id
1774                 (GTK_STATUSBAR(statusbar), "Folder View");
1775         mainwin->summaryview_cid = gtk_statusbar_get_context_id
1776                 (GTK_STATUSBAR(statusbar), "Summary View");
1777         mainwin->messageview_cid = gtk_statusbar_get_context_id
1778                 (GTK_STATUSBAR(statusbar), "Message View");
1779         messageview->statusbar_cid = mainwin->messageview_cid;
1780
1781 #else
1782         messageview->statusbar  = NULL;
1783         mainwin->statusbar      = NULL;
1784         mainwin->hbox_stat      = NULL;
1785         /* mainwin->progressbar is set in toolbar.c */
1786         mainwin->statuslabel    = NULL;
1787         mainwin->online_pixmap  = NULL;
1788         mainwin->offline_pixmap = NULL;
1789         mainwin->ac_button      = NULL;
1790         mainwin->ac_label       = NULL;
1791 #endif
1792         
1793         /* allocate colors for summary view and folder view */
1794         summaryview->color_marked.red = summaryview->color_marked.green = 0;
1795         summaryview->color_marked.blue = (guint16)65535;
1796
1797         summaryview->color_dim.red = summaryview->color_dim.green =
1798                 summaryview->color_dim.blue = COLOR_DIM;
1799
1800         gtkut_convert_int_to_gdk_color(prefs_common.color_new,
1801                                        &folderview->color_new);
1802
1803         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
1804                                        &folderview->color_op);
1805
1806         summaryview->color_important.red = 0;
1807         summaryview->color_important.green = 0;
1808         summaryview->color_important.blue = (guint16)65535;
1809
1810         color[0] = summaryview->color_marked;
1811         color[1] = summaryview->color_dim;
1812         color[2] = folderview->color_new;
1813         color[3] = folderview->color_op;
1814
1815         colormap = gdk_drawable_get_colormap(window->window);
1816         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
1817         for (i = 0; i < 4; i++) {
1818                 if (success[i] == FALSE)
1819                         g_warning("MainWindow: color allocation %d failed\n", i);
1820         }
1821
1822         debug_print("done.\n");
1823
1824         messageview->visible = prefs_common.msgview_visible;
1825
1826         main_window_set_widgets(mainwin, prefs_common.layout_mode);
1827
1828         g_signal_connect(G_OBJECT(window), "size_allocate",
1829                          G_CALLBACK(main_window_size_allocate_cb),
1830                          mainwin);
1831
1832         /* set menu items */
1833         menuitem = gtk_item_factory_get_item
1834                 (ifactory, "/View/Character encoding/Auto detect");
1835         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1836
1837         switch (prefs_common.toolbar_style) {
1838         case TOOLBAR_NONE:
1839                 menuitem = gtk_item_factory_get_item
1840                         (ifactory, "/View/Show or hide/Toolbar/Hide");
1841                 break;
1842         case TOOLBAR_ICON:
1843                 menuitem = gtk_item_factory_get_item
1844                         (ifactory, "/View/Show or hide/Toolbar/Icons only");
1845                 break;
1846         case TOOLBAR_TEXT:
1847                 menuitem = gtk_item_factory_get_item
1848                         (ifactory, "/View/Show or hide/Toolbar/Text only");
1849                 break;
1850         case TOOLBAR_BOTH:
1851                 menuitem = gtk_item_factory_get_item
1852                         (ifactory, "/View/Show or hide/Toolbar/Text below icons");
1853                 break;
1854         case TOOLBAR_BOTH_HORIZ:
1855                 menuitem = gtk_item_factory_get_item
1856                         (ifactory,
1857                          "/View/Show or hide/Toolbar/Text beside icons");
1858         }
1859         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1860
1861 #ifndef MAEMO
1862         gtk_widget_hide(mainwin->hbox_stat);
1863         menuitem = gtk_item_factory_get_item
1864                 (ifactory, "/View/Show or hide/Status bar");
1865         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1866                                        prefs_common.show_statusbar);
1867 #endif  
1868         menuitem = gtk_item_factory_get_item
1869                 (ifactory, "/View/Show or hide/Column headers");
1870         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1871                                        prefs_common.show_col_headers);
1872         /* set account selection menu */
1873         ac_menu = gtk_item_factory_get_widget
1874                 (ifactory, "/Configuration/Change current account");
1875         mainwin->ac_menu = ac_menu;
1876
1877         toolbar_main_set_sensitive(mainwin);
1878
1879         /* create actions menu */
1880         main_window_update_actions_menu(mainwin);
1881
1882         main_create_mailing_list_menu (mainwin, NULL);
1883
1884         /* attach accel groups to main window */
1885 #define ADD_MENU_ACCEL_GROUP_TO_WINDOW(menu,win)                        \
1886         gtk_window_add_accel_group                                      \
1887                 (GTK_WINDOW(win),                                       \
1888                  gtk_item_factory_from_widget(menu)->accel_group);      \
1889         g_signal_connect(G_OBJECT(gtk_item_factory_from_widget(menu)->accel_group), \
1890                         "accel_activate",                               \
1891                         G_CALLBACK(main_window_accel_activate), mainwin);
1892                          
1893         
1894         ADD_MENU_ACCEL_GROUP_TO_WINDOW(summaryview->popupmenu, mainwin->window);
1895         
1896         /* connect the accelerators for equivalent 
1897            menu items in different menus             */
1898         menu_connect_identical_items();
1899
1900 #ifndef MAEMO
1901         gtk_window_iconify(GTK_WINDOW(mainwin->window));
1902 #endif
1903
1904         g_signal_connect(G_OBJECT(window), "window_state_event",
1905                          G_CALLBACK(mainwindow_state_event_cb), mainwin);
1906         g_signal_connect(G_OBJECT(window), "visibility_notify_event",
1907                          G_CALLBACK(mainwindow_visibility_event_cb), mainwin);
1908         gtk_widget_add_events(GTK_WIDGET(window), GDK_VISIBILITY_NOTIFY_MASK);
1909
1910         if (prefs_common.layout_mode == VERTICAL_LAYOUT ||
1911             prefs_common.layout_mode == SMALL_LAYOUT) {
1912                 summary_relayout(mainwin->summaryview); 
1913         }
1914         summary_update_unread(mainwin->summaryview, NULL);
1915         
1916         gtk_widget_show(mainwin->window);
1917
1918         /* initialize views */
1919         folderview_init(folderview);
1920         summary_init(summaryview);
1921         messageview_init(messageview);
1922 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
1923         sslcertwindow_register_hook();
1924 #endif
1925         mainwin->lock_count = 0;
1926         mainwin->menu_lock_count = 0;
1927         mainwin->cursor_count = 0;
1928
1929         mainwin->progressindicator_hook =
1930                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1931
1932         if (!watch_cursor)
1933                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1934         if (!hand_cursor)
1935                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1936
1937         /* init work_offline */
1938         if (prefs_common.work_offline)
1939                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1940
1941         mainwindow_colorlabel_menu_create(mainwin, FALSE);
1942         mainwindow_tags_menu_create(mainwin, FALSE);
1943
1944 #ifdef MAEMO
1945         main_window_install_maemo_hooks(mainwin);
1946 #endif
1947         return mainwin;
1948 }
1949
1950 void main_window_update_actions_menu(MainWindow *mainwin)
1951 {
1952         GtkItemFactory *ifactory;
1953
1954         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1955         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1956 }
1957
1958 void main_window_cursor_wait(MainWindow *mainwin)
1959 {
1960
1961         if (mainwin->cursor_count == 0) {
1962                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1963                 textview_cursor_wait(mainwin->messageview->mimeview->textview);
1964         }
1965         
1966         mainwin->cursor_count++;
1967
1968         gdk_flush();
1969 }
1970
1971 void main_window_cursor_normal(MainWindow *mainwin)
1972 {
1973         if (mainwin->cursor_count)
1974                 mainwin->cursor_count--;
1975
1976         if (mainwin->cursor_count == 0) {
1977                 gdk_window_set_cursor(mainwin->window->window, NULL);
1978                 textview_cursor_normal(mainwin->messageview->mimeview->textview);
1979         }
1980         gdk_flush();
1981 }
1982
1983 /* lock / unlock the user-interface */
1984 void main_window_lock(MainWindow *mainwin)
1985 {
1986         if (mainwin->lock_count == 0 && mainwin->ac_button)
1987                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1988
1989         mainwin->lock_count++;
1990
1991         main_window_set_menu_sensitive(mainwin);
1992         toolbar_main_set_sensitive(mainwin);
1993 }
1994
1995 void main_window_unlock(MainWindow *mainwin)
1996 {
1997         if (mainwin->lock_count)
1998                 mainwin->lock_count--;
1999
2000         main_window_set_menu_sensitive(mainwin);
2001         toolbar_main_set_sensitive(mainwin);
2002
2003         if (mainwin->lock_count == 0 && mainwin->ac_button)
2004                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
2005 }
2006
2007 static void main_window_menu_callback_block(MainWindow *mainwin)
2008 {
2009         mainwin->menu_lock_count++;
2010 }
2011
2012 static void main_window_menu_callback_unblock(MainWindow *mainwin)
2013 {
2014         if (mainwin->menu_lock_count)
2015                 mainwin->menu_lock_count--;
2016 }
2017
2018 static guint prefs_tag = 0;
2019
2020 void main_window_reflect_prefs_all(void)
2021 {
2022         main_window_reflect_prefs_all_real(FALSE);
2023 }
2024
2025 static gboolean reflect_prefs_timeout_cb(gpointer data) 
2026 {
2027         gboolean pixmap_theme_changed = GPOINTER_TO_INT(data);
2028         GList *cur;
2029         MainWindow *mainwin;
2030 #ifndef MAEMO
2031         GtkWidget *pixmap;
2032 #endif
2033         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
2034                 mainwin = (MainWindow *)cur->data;
2035
2036                 main_window_show_cur_account(mainwin);
2037                 main_window_set_menu_sensitive(mainwin);
2038                 toolbar_main_set_sensitive(mainwin);
2039
2040                 /* pixmap themes */
2041                 if (pixmap_theme_changed) {
2042                         toolbar_update(TOOLBAR_MAIN, mainwin);
2043                         messageview_reflect_prefs_pixmap_theme();
2044                         compose_reflect_prefs_pixmap_theme();
2045                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
2046                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
2047 #ifndef MAEMO
2048                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
2049                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
2050                                              mainwin->online_pixmap);
2051                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
2052                         gtk_widget_show(pixmap);
2053                         mainwin->online_pixmap = pixmap;
2054                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
2055                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
2056                                              mainwin->offline_pixmap);
2057                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
2058                         gtk_widget_show(pixmap);
2059                         mainwin->offline_pixmap = pixmap;
2060 #endif
2061                 }
2062                 
2063                 headerview_set_font(mainwin->messageview->headerview);
2064                 headerview_set_visibility(mainwin->messageview->headerview,
2065                                           prefs_common.display_header_pane);
2066                 textview_reflect_prefs(mainwin->messageview->mimeview->textview);
2067                 folderview_reflect_prefs();
2068                 summary_reflect_prefs();
2069 #ifndef MAEMO
2070                 summary_redisplay_msg(mainwin->summaryview);
2071 #endif
2072                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
2073                         if (mainwin->in_folder) {
2074                                 mainwindow_enter_folder(mainwin);
2075                         } else {
2076                                 mainwindow_exit_folder(mainwin);
2077                         }
2078                 }
2079         }
2080         prefs_tag = 0;
2081         return FALSE;
2082 }
2083
2084 void main_window_reflect_prefs_all_now(void)
2085 {
2086         reflect_prefs_timeout_cb(GINT_TO_POINTER(FALSE));
2087 }
2088
2089 void main_window_reflect_prefs_custom_colors(MainWindow *mainwin)
2090 {
2091         GtkMenuShell *menu;
2092         GList *cur;
2093
2094         /* re-create colorlabel submenu */
2095         menu = GTK_MENU_SHELL(mainwin->colorlabel_menu);
2096         g_return_if_fail(menu != NULL);
2097
2098         /* clear items. get item pointers. */
2099         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
2100                 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(cur->data));
2101         }
2102         mainwindow_colorlabel_menu_create(mainwin, TRUE);
2103         summary_reflect_prefs_custom_colors(mainwin->summaryview);
2104
2105 }
2106
2107 static gint tags_tag = 0;
2108 static gboolean main_window_reflect_tags_changes_real(gpointer data)
2109 {
2110         GtkMenuShell *menu;
2111         GList *cur;
2112         MainWindow *mainwin = (MainWindow *)data;
2113
2114         if (summary_is_locked(mainwin->summaryview))
2115                 return TRUE;
2116
2117         /* re-create tags submenu */
2118         menu = GTK_MENU_SHELL(mainwin->tags_menu);
2119         g_return_val_if_fail(menu != NULL, FALSE);
2120
2121         /* clear items. get item pointers. */
2122         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
2123                 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(cur->data));
2124         }
2125         mainwindow_tags_menu_create(mainwin, TRUE);
2126         summary_reflect_tags_changes(mainwin->summaryview);
2127         
2128         return FALSE;
2129 }
2130
2131 void main_window_reflect_tags_changes(MainWindow *mainwin)
2132 {
2133         if (tags_tag == 0) {
2134                 tags_tag = g_timeout_add(100, main_window_reflect_tags_changes_real, 
2135                                                 mainwin);
2136         }
2137 }
2138
2139 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
2140 {
2141         if (prefs_tag == 0 || pixmap_theme_changed) {
2142                 prefs_tag = g_timeout_add(500, reflect_prefs_timeout_cb, 
2143                                                 GINT_TO_POINTER(pixmap_theme_changed));
2144         }
2145 }
2146
2147 void main_window_set_summary_column(void)
2148 {
2149         GList *cur;
2150         MainWindow *mainwin;
2151
2152         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
2153                 mainwin = (MainWindow *)cur->data;
2154                 summary_set_column_order(mainwin->summaryview);
2155         }
2156 }
2157
2158 void main_window_set_folder_column(void)
2159 {
2160         GList *cur;
2161         MainWindow *mainwin;
2162
2163         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
2164                 mainwin = (MainWindow *)cur->data;
2165                 folderview_set_column_order(mainwin->folderview);
2166         }
2167 }
2168
2169 static void main_window_set_account_selector_menu(MainWindow *mainwin,
2170                                                   GList *account_list)
2171 {
2172         GList *cur_ac, *cur_item;
2173         GtkWidget *menuitem;
2174         PrefsAccount *ac_prefs;
2175
2176         /* destroy all previous menu item */
2177         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
2178         while (cur_item != NULL) {
2179                 GList *next = cur_item->next;
2180                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
2181                 cur_item = next;
2182         }
2183
2184         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
2185                 ac_prefs = (PrefsAccount *)cur_ac->data;
2186
2187                 menuitem = gtk_menu_item_new_with_label
2188                         (ac_prefs->account_name
2189                          ? ac_prefs->account_name : _("Untitled"));
2190                 gtk_widget_show(menuitem);
2191                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
2192                 g_signal_connect(G_OBJECT(menuitem), "activate",
2193                                  G_CALLBACK(account_selector_menu_cb),
2194                                  ac_prefs);
2195         }
2196 }
2197
2198 static void main_window_set_account_receive_menu(MainWindow *mainwin,
2199                                                  GList *account_list)
2200 {
2201         GList *cur_ac, *cur_item;
2202         GtkWidget *menu;
2203         GtkWidget *menuitem;
2204         PrefsAccount *ac_prefs;
2205
2206         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
2207                                            "/Message/Receive");
2208
2209         /* search for separator */
2210         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
2211              cur_item = cur_item->next) {
2212                 if (GTK_BIN(cur_item->data)->child == NULL) {
2213                         cur_item = cur_item->next;
2214                         break;
2215                 }
2216         }
2217
2218         /* destroy all previous menu item */
2219         while (cur_item != NULL) {
2220                 GList *next = cur_item->next;
2221                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
2222                 cur_item = next;
2223         }
2224
2225         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
2226                 ac_prefs = (PrefsAccount *)cur_ac->data;
2227
2228                 menuitem = gtk_menu_item_new_with_label
2229                         (ac_prefs->account_name ? ac_prefs->account_name
2230                          : _("Untitled"));
2231                 gtk_widget_show(menuitem);
2232                 gtk_menu_append(GTK_MENU(menu), menuitem);
2233                 g_signal_connect(G_OBJECT(menuitem), "activate",
2234                                  G_CALLBACK(account_receive_menu_cb),
2235                                  ac_prefs);
2236         }
2237 }
2238
2239 static void main_window_set_toolbar_combo_receive_menu(MainWindow *mainwin,
2240                                                        GList *account_list)
2241 {
2242         GList *cur_ac;
2243         GtkWidget *menuitem;
2244         PrefsAccount *ac_prefs;
2245         GtkWidget *menu = NULL;
2246
2247         if (mainwin->toolbar->getall_btn == NULL) /* button doesn't exist */
2248                 return;
2249
2250         menu = gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(mainwin->toolbar->getall_btn));
2251         if (menu)
2252                 gtk_widget_destroy(menu);
2253         menu = gtk_menu_new();
2254
2255         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
2256                 ac_prefs = (PrefsAccount *)cur_ac->data;
2257
2258                 menuitem = gtk_menu_item_new_with_label
2259                         (ac_prefs->account_name
2260                          ? ac_prefs->account_name : _("Untitled"));
2261                 gtk_widget_show(menuitem);
2262                 gtk_menu_append(GTK_MENU(menu), menuitem);
2263                 g_signal_connect(G_OBJECT(menuitem), "activate",
2264                                  G_CALLBACK(account_receive_menu_cb),
2265                                  ac_prefs);
2266         }
2267         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(mainwin->toolbar->getall_btn), menu);
2268 }
2269
2270 static void main_window_set_toolbar_combo_compose_menu(MainWindow *mainwin,
2271                                                        GList *account_list)
2272 {
2273 #ifndef MAEMO
2274         GList *cur_ac;
2275         GtkWidget *menuitem;
2276         PrefsAccount *ac_prefs;
2277         GtkWidget *menu = NULL;
2278
2279         if (mainwin->toolbar->compose_mail_btn == NULL) /* button doesn't exist */
2280                 return;
2281
2282         menu = gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(mainwin->toolbar->compose_mail_btn));
2283         if (menu)
2284                 gtk_widget_destroy(menu);
2285         menu = gtk_menu_new();
2286
2287         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
2288                 ac_prefs = (PrefsAccount *)cur_ac->data;
2289
2290                 menuitem = gtk_menu_item_new_with_label
2291                         (ac_prefs->account_name
2292                          ? ac_prefs->account_name : _("Untitled"));
2293                 gtk_widget_show(menuitem);
2294                 gtk_menu_append(GTK_MENU(menu), menuitem);
2295                 g_signal_connect(G_OBJECT(menuitem), "activate",
2296                                  G_CALLBACK(account_compose_menu_cb),
2297                                  ac_prefs);
2298         }
2299         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(mainwin->toolbar->compose_mail_btn), menu);
2300 #endif
2301 }
2302
2303 void main_window_set_account_menu(GList *account_list)
2304 {
2305         GList *cur;
2306         MainWindow *mainwin;
2307
2308         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
2309                 mainwin = (MainWindow *)cur->data;
2310                 main_window_set_account_selector_menu(mainwin, account_list);
2311                 main_window_set_account_receive_menu(mainwin, account_list);
2312                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
2313                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
2314         }
2315         hooks_invoke(ACCOUNT_LIST_CHANGED_HOOKLIST, NULL);
2316 }
2317
2318 void main_window_set_account_menu_only_toolbar(GList *account_list)
2319 {
2320         GList *cur;
2321         MainWindow *mainwin;
2322
2323         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
2324                 mainwin = (MainWindow *)cur->data;
2325                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
2326                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
2327         }
2328 }
2329
2330 static void main_window_show_cur_account(MainWindow *mainwin)
2331 {
2332         gchar *buf;
2333         gchar *ac_name;
2334
2335         ac_name = g_strdup(cur_account
2336                            ? (cur_account->account_name
2337                               ? cur_account->account_name : _("Untitled"))
2338                            : _("none"));
2339
2340         if (cur_account)
2341                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
2342         else
2343                 buf = g_strdup(PROG_VERSION);
2344         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
2345         g_free(buf);
2346
2347         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
2348         if (mainwin->ac_button)
2349                 gtk_widget_queue_resize(mainwin->ac_button);
2350
2351         g_free(ac_name);
2352 }
2353 #ifndef MAEMO
2354 static void main_window_separation_change(MainWindow *mainwin, LayoutType layout_mode)
2355 {
2356         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
2357         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
2358         GtkWidget *message_wid = mainwin->messageview->vbox;
2359
2360         if (layout_mode == prefs_common.layout_mode) 
2361                 return;
2362
2363         debug_print("Changing window separation type from %d to %d\n",
2364                     prefs_common.layout_mode, layout_mode);
2365
2366         /* remove widgets from those containers */
2367         gtk_widget_ref(folder_wid);
2368         gtk_widget_ref(summary_wid);
2369         gtk_widget_ref(message_wid);
2370         gtkut_container_remove
2371                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
2372         gtkut_container_remove
2373                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
2374         gtkut_container_remove
2375                 (GTK_CONTAINER(message_wid->parent), message_wid);
2376
2377         gtk_widget_hide(mainwin->window);
2378         main_window_set_widgets(mainwin, layout_mode);
2379         gtk_widget_show(mainwin->window);
2380
2381         gtk_widget_unref(folder_wid);
2382         gtk_widget_unref(summary_wid);
2383         gtk_widget_unref(message_wid);
2384 }
2385 #endif
2386 void mainwindow_reset_paned(GtkPaned *paned)
2387 {
2388                 gint min, max, mid;
2389
2390                 if (gtk_paned_get_child1(GTK_PANED(paned)))
2391                         gtk_widget_show(gtk_paned_get_child1(GTK_PANED(paned)));
2392                 if (gtk_paned_get_child2(GTK_PANED(paned)))
2393                         gtk_widget_show(gtk_paned_get_child2(GTK_PANED(paned)));
2394
2395 GTK_EVENTS_FLUSH();
2396                 g_object_get (G_OBJECT(paned),
2397                                 "min-position",
2398                                 &min, NULL);
2399                 g_object_get (G_OBJECT(paned),
2400                                 "max-position",
2401                                 &max, NULL);
2402                 mid = (min+max)/2;
2403                 gtk_paned_set_position(GTK_PANED(paned), mid);
2404 }
2405
2406 static void mainwin_paned_show_first(GtkPaned *paned)
2407 {
2408                 gint max;
2409                 g_object_get (G_OBJECT(paned),
2410                                 "max-position",
2411                                 &max, NULL);
2412
2413                 if (gtk_paned_get_child1(GTK_PANED(paned)))
2414                         gtk_widget_show(gtk_paned_get_child1(GTK_PANED(paned)));
2415                 if (gtk_paned_get_child2(GTK_PANED(paned)))
2416                         gtk_widget_hide(gtk_paned_get_child2(GTK_PANED(paned)));
2417                 gtk_paned_set_position(GTK_PANED(paned), max);
2418 }
2419
2420 static void mainwin_paned_show_last(GtkPaned *paned)
2421 {
2422                 gint min;
2423                 g_object_get (G_OBJECT(paned),
2424                                 "min-position",
2425                                 &min, NULL);
2426
2427                 if (gtk_paned_get_child1(GTK_PANED(paned)))
2428                         gtk_widget_hide(gtk_paned_get_child1(GTK_PANED(paned)));
2429                 if (gtk_paned_get_child2(GTK_PANED(paned)))
2430                         gtk_widget_show(gtk_paned_get_child2(GTK_PANED(paned)));
2431                 gtk_paned_set_position(GTK_PANED(paned), min);
2432 }
2433
2434 void main_window_toggle_message_view(MainWindow *mainwin)
2435 {
2436         SummaryView *summaryview = mainwin->summaryview;
2437         GtkWidget *ppaned = NULL;
2438         GtkWidget *container = NULL;
2439         
2440         switch (prefs_common.layout_mode) {
2441         case NORMAL_LAYOUT:
2442         case VERTICAL_LAYOUT:
2443         case SMALL_LAYOUT:
2444                 ppaned = mainwin->vpaned;
2445                 container = mainwin->hpaned;
2446                 if (ppaned->parent != NULL) {
2447                         mainwin->messageview->visible = FALSE;
2448                         summaryview->displayed = NULL;
2449                         gtk_widget_ref(ppaned);
2450                         gtkut_container_remove(GTK_CONTAINER(container), ppaned);
2451                         gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
2452                 } else {
2453                         mainwin->messageview->visible = TRUE;
2454                         gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), ppaned);
2455                         gtk_container_add(GTK_CONTAINER(container), ppaned);
2456                         gtk_widget_unref(ppaned);
2457                 }
2458                 break;
2459         case WIDE_LAYOUT:
2460                 ppaned = mainwin->hpaned;
2461                 container = mainwin->vpaned;
2462                 if (mainwin->messageview->vbox->parent != NULL) {
2463                         mainwin->messageview->visible = FALSE;
2464                         summaryview->displayed = NULL;
2465                         gtk_widget_ref(mainwin->messageview->vbox);
2466                         gtkut_container_remove(GTK_CONTAINER(container), mainwin->messageview->vbox);
2467                 } else {
2468                         mainwin->messageview->visible = TRUE;
2469                         gtk_container_add(GTK_CONTAINER(container), mainwin->messageview->vbox);
2470                         gtk_widget_unref(mainwin->messageview->vbox);
2471                 }
2472                 break;
2473         case WIDE_MSGLIST_LAYOUT:
2474                 g_warning("can't hide messageview in this wide msglist layout");
2475                 break;
2476         }
2477
2478         if (messageview_is_visible(mainwin->messageview))
2479                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2480                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2481         else
2482                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2483                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2484
2485         if (mainwin->messageview->visible == FALSE)
2486                 messageview_clear(mainwin->messageview);
2487
2488         main_window_set_menu_sensitive(mainwin);
2489
2490         prefs_common.msgview_visible = mainwin->messageview->visible;
2491
2492         if (messageview_is_visible(mainwin->messageview)) {
2493                 gtk_widget_queue_resize(mainwin->hpaned);
2494                 gtk_widget_queue_resize(mainwin->vpaned);
2495         }
2496         summary_grab_focus(summaryview);
2497 }
2498
2499 void main_window_get_size(MainWindow *mainwin)
2500 {
2501         GtkAllocation *allocation;
2502
2503         if (mainwin_list == NULL || mainwin->messageview == NULL) {
2504                 debug_print("called after messageview "
2505                             "has been deallocated!\n");
2506                 return;
2507         }
2508
2509         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
2510
2511         if (allocation->width > 1 && allocation->height > 1) {
2512                 prefs_common.summaryview_width = allocation->width;
2513
2514                 if (messageview_is_visible(mainwin->messageview))
2515                         prefs_common.summaryview_height = allocation->height;
2516
2517                 prefs_common.mainview_width = allocation->width;
2518         }
2519
2520         allocation = &mainwin->window->allocation;
2521         if (allocation->width > 1 && allocation->height > 1 &&
2522             !prefs_common.mainwin_maximised) {
2523                 prefs_common.mainview_height = allocation->height;
2524                 prefs_common.mainwin_width   = allocation->width;
2525                 prefs_common.mainwin_height  = allocation->height;
2526         }
2527
2528         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
2529         if (allocation->width > 1 && allocation->height > 1) {
2530                 prefs_common.folderview_width  = allocation->width;
2531                 prefs_common.folderview_height = allocation->height;
2532         }
2533
2534         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
2535         if (allocation->width > 1 && allocation->height > 1) {
2536                 prefs_common.msgview_width = allocation->width;
2537                 prefs_common.msgview_height = allocation->height;
2538         }
2539
2540 /*      debug_print("summaryview size: %d x %d\n",
2541                     prefs_common.summaryview_width,
2542                     prefs_common.summaryview_height);
2543         debug_print("folderview size: %d x %d\n",
2544                     prefs_common.folderview_width,
2545                     prefs_common.folderview_height);
2546         debug_print("messageview size: %d x %d\n",
2547                     prefs_common.msgview_width,
2548                     prefs_common.msgview_height); */
2549 }
2550
2551 void main_window_get_position(MainWindow *mainwin)
2552 {
2553         gint x, y;
2554
2555         if (prefs_common.mainwin_maximised)
2556                 return;
2557
2558         gtkut_widget_get_uposition(mainwin->window, &x, &y);
2559
2560         prefs_common.mainview_x = x;
2561         prefs_common.mainview_y = y;
2562         prefs_common.mainwin_x = x;
2563         prefs_common.mainwin_y = y;
2564
2565         debug_print("main window position: %d, %d\n", x, y);
2566 }
2567
2568 void main_window_progress_on(MainWindow *mainwin)
2569 {
2570         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2571 }
2572
2573 void main_window_progress_off(MainWindow *mainwin)
2574 {
2575         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
2576         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2577 }
2578
2579 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
2580 {
2581         if (confirm && procmsg_have_trashed_mails_fast()) {
2582                 if (alertpanel(_("Empty trash"),
2583                                _("Delete all messages in trash folders?"),
2584                                GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL)
2585                     != G_ALERTALTERNATE)
2586                         return;
2587                 manage_window_focus_in(mainwin->window, NULL, NULL);
2588         }
2589
2590         procmsg_empty_all_trash();
2591
2592         if (mainwin->summaryview->folder_item &&
2593             mainwin->summaryview->folder_item->stype == F_TRASH)
2594                 gtk_widget_grab_focus(mainwin->folderview->ctree);
2595 }
2596
2597 static void main_window_add_mailbox(MainWindow *mainwin)
2598 {
2599         gchar *path;
2600         Folder *folder;
2601
2602         path = input_dialog(_("Add mailbox"),
2603                             _("Input the location of mailbox.\n"
2604                               "If an existing mailbox is specified, it will be\n"
2605                               "scanned automatically."),
2606                             "Mail");
2607         if (!path) return;
2608         if (folder_find_from_path(path)) {
2609                 alertpanel_error(_("The mailbox '%s' already exists."), path);
2610                 g_free(path);
2611                 return;
2612         }
2613         folder = folder_new(folder_get_class_from_string("mh"), 
2614                             !strcmp(path, "Mail") ? _("Mailbox") : 
2615                             g_path_get_basename(path), path);
2616         g_free(path);
2617
2618         if (folder->klass->create_tree(folder) < 0) {
2619                 alertpanel_error(_("Creation of the mailbox failed.\n"
2620                                    "Maybe some files already exist, or you don't have the permission to write there."));
2621                 folder_destroy(folder);
2622                 return;
2623         }
2624
2625         folder_add(folder);
2626         folder_set_ui_func(folder, scan_tree_func, mainwin);
2627         folder_scan_tree(folder, TRUE);
2628         folder_set_ui_func(folder, NULL, NULL);
2629 }
2630
2631 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
2632 {
2633         SensitiveCond state = 0;
2634         SummarySelection selection;
2635         FolderItem *item = mainwin->summaryview->folder_item;
2636         GList *account_list = account_get_list();
2637         GSList *tmp;
2638         
2639         selection = summary_get_selection_type(mainwin->summaryview);
2640
2641         if (mainwin->lock_count == 0 && !claws_is_starting())
2642                 state |= M_UNLOCKED;
2643         if (selection != SUMMARY_NONE)
2644                 state |= M_MSG_EXIST;
2645         if (item && item->path && folder_item_parent(item) && !item->no_select) {
2646                 state |= M_EXEC;
2647                 /*              if (item->folder->type != F_NEWS) */
2648                 state |= M_ALLOW_DELETE;
2649
2650                 if (prefs_common.immediate_exec == 0
2651                     && mainwin->lock_count == 0)
2652                         state |= M_DELAY_EXEC;
2653
2654                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
2655                     || selection != SUMMARY_NONE)
2656                         state |= M_HIDE_READ_MSG;
2657         }               
2658         if (mainwin->summaryview->threaded)
2659                 state |= M_THREADED;
2660         else
2661                 state |= M_UNTHREADED;  
2662         if (selection == SUMMARY_SELECTED_SINGLE ||
2663             selection == SUMMARY_SELECTED_MULTIPLE)
2664                 state |= M_TARGET_EXIST;
2665         if (selection == SUMMARY_SELECTED_SINGLE)
2666                 state |= M_SINGLE_TARGET_EXIST;
2667         if (mainwin->summaryview->folder_item &&
2668             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
2669                 state |= M_NEWS;
2670         else
2671                 state |= M_NOT_NEWS;
2672         if (prefs_common.actions_list && g_slist_length(prefs_common.actions_list))
2673                 state |= M_ACTIONS_EXIST;
2674
2675         tmp = tags_get_list();
2676         if (tmp && g_slist_length(tmp))
2677                 state |= M_TAGS_EXIST;
2678         g_slist_free(tmp);
2679
2680         if (procmsg_have_queued_mails_fast() && !procmsg_is_sending())
2681                 state |= M_HAVE_QUEUED_MAILS;
2682
2683         if (selection == SUMMARY_SELECTED_SINGLE &&
2684             (item &&
2685              (folder_has_parent_of_type(item, F_DRAFT) ||
2686               folder_has_parent_of_type(item, F_OUTBOX) ||
2687               folder_has_parent_of_type(item, F_QUEUE))))
2688                 state |= M_ALLOW_REEDIT;
2689         if (cur_account)
2690                 state |= M_HAVE_ACCOUNT;
2691         
2692         if (any_folder_want_synchronise())
2693                 state |= M_WANT_SYNC;
2694
2695         if (item && item->prefs->processing && selection != SUMMARY_NONE)
2696                 state |= M_HAVE_PROCESSING;
2697
2698         for ( ; account_list != NULL; account_list = account_list->next) {
2699                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
2700                         state |= M_HAVE_NEWS_ACCOUNT;
2701                         break;
2702                 }
2703         }
2704         
2705         if (procmsg_spam_can_learn() &&
2706             (mainwin->summaryview->folder_item &&
2707              mainwin->summaryview->folder_item->folder->klass->type != F_UNKNOWN &&
2708              mainwin->summaryview->folder_item->folder->klass->type != F_NEWS)) {
2709                 state |= M_CAN_LEARN_SPAM;
2710         }
2711
2712         if (inc_is_active())
2713                 state |= M_INC_ACTIVE;
2714         if (imap_cancel_all_enabled())
2715                 state |= M_INC_ACTIVE;
2716
2717         if (mainwin->summaryview->deleted > 0 ||
2718             mainwin->summaryview->moved > 0 ||
2719             mainwin->summaryview->copied > 0)
2720                 state |= M_DELAY_EXEC;
2721
2722         return state;
2723 }
2724
2725
2726
2727 void main_window_set_menu_sensitive(MainWindow *mainwin)
2728 {
2729         GtkItemFactory *ifactory = mainwin->menu_factory;
2730         SensitiveCond state;
2731         gboolean sensitive;
2732         GtkWidget *menu;
2733         GtkWidget *menuitem;
2734         SummaryView *summaryview;
2735         gchar *menu_path;
2736         gint i;
2737         GList *cur_item;
2738
2739         static const struct {
2740                 gchar *const entry;
2741                 SensitiveCond cond;
2742         } entry[] = {
2743                 {"/File/Save as...", M_TARGET_EXIST},
2744                 {"/File/Print..."  , M_TARGET_EXIST},
2745                 {"/File/Synchronise folders", M_WANT_SYNC},
2746                 {"/File/Exit"      , M_UNLOCKED},
2747
2748                 {"/Edit/Select thread"             , M_TARGET_EXIST},
2749                 {"/Edit/Delete thread"             , M_TARGET_EXIST},
2750                 {"/Edit/Find in current message...", M_SINGLE_TARGET_EXIST},
2751
2752                 {"/View/Set displayed columns/in Folder list..."
2753                                                    , M_UNLOCKED}, 
2754                 {"/View/Sort"                      , M_EXEC},
2755                 {"/View/Thread view"               , M_EXEC},
2756                 {"/View/Expand all threads"        , M_MSG_EXIST},
2757                 {"/View/Collapse all threads"      , M_MSG_EXIST},
2758                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
2759                 {"/View/Go to/Previous message"        , M_MSG_EXIST},
2760                 {"/View/Go to/Next message"        , M_MSG_EXIST},
2761                 {"/View/Go to/Previous unread message" , M_MSG_EXIST},
2762                 {"/View/Go to/Previous new message"    , M_MSG_EXIST},
2763                 {"/View/Go to/Previous marked message" , M_MSG_EXIST},
2764                 {"/View/Go to/Previous labeled message", M_MSG_EXIST},
2765                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
2766                 {"/View/Go to/Last read message"   , M_SINGLE_TARGET_EXIST},
2767                 {"/View/Go to/Parent message"      , M_SINGLE_TARGET_EXIST},
2768                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
2769                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
2770                 {"/View/All headers"               , M_SINGLE_TARGET_EXIST},
2771                 {"/View/Quotes"                    , M_SINGLE_TARGET_EXIST},
2772
2773                 {"/Message/Receive/Get from current account"
2774                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2775                 {"/Message/Receive/Get from all accounts"
2776                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2777                 {"/Message/Receive/Cancel receiving"
2778                                                  , M_INC_ACTIVE},
2779                 {"/Message/Send queued messages"  , M_HAVE_ACCOUNT|M_HAVE_QUEUED_MAILS},
2780                 {"/Message/Compose an email message", M_HAVE_ACCOUNT},
2781                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
2782                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2783                 {"/Message/Reply to"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2784                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_TARGET_EXIST|M_NEWS},
2785                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2786                 {"/Message/Forward as attachment" , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2787                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2788                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE},
2789                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC},
2790                 {"/Message/Move to trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
2791                 {"/Message/Delete..."             , M_TARGET_EXIST|M_ALLOW_DELETE},
2792                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_NEWS},
2793                 {"/Message/Mark"                  , M_TARGET_EXIST},
2794                 {"/Message/Mark/Mark as spam"     , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2795                 {"/Message/Mark/Mark as ham"      , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2796                 {"/Message/Mark/Ignore thread"    , M_TARGET_EXIST},
2797                 {"/Message/Mark/Unignore thread"  , M_TARGET_EXIST},
2798                 {"/Message/Mark/Lock"             , M_TARGET_EXIST},
2799                 {"/Message/Mark/Unlock"           , M_TARGET_EXIST},
2800                 {"/Message/Color label"           , M_TARGET_EXIST},
2801                 {"/Message/Tags"                  , M_TARGET_EXIST},
2802                 {"/Message/Re-edit"               , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
2803
2804                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
2805                 {"/Tools/Harvest addresses"            , M_MSG_EXIST},
2806                 {"/Tools/Harvest addresses/from Messages..."
2807                                                        , M_TARGET_EXIST},
2808                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC},
2809                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC},
2810                 {"/Tools/Run folder processing rules"  , M_HAVE_PROCESSING},
2811                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2812                 {"/Tools/Create processing rule"       , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2813                 {"/Tools/List URLs..."                 , M_TARGET_EXIST},
2814                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_ACTIONS_EXIST},
2815                 {"/Tools/Execute"                      , M_DELAY_EXEC},
2816                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE},
2817
2818                 {"/Configuration", M_UNLOCKED},
2819                 {"/Configuration/Preferences for current account...", M_UNLOCKED},
2820                 {"/Configuration/Create new account...", M_UNLOCKED},
2821                 {"/Configuration/Edit accounts...", M_UNLOCKED},
2822
2823                 {NULL, 0}
2824         };
2825
2826         state = main_window_get_current_state(mainwin);
2827
2828         for (i = 0; entry[i].entry != NULL; i++) {
2829                 sensitive = ((entry[i].cond & state) == entry[i].cond);
2830                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
2831         }
2832
2833         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
2834
2835         /* search for separator */
2836         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
2837              cur_item = cur_item->next) {
2838                 if (GTK_BIN(cur_item->data)->child == NULL) {
2839                         cur_item = cur_item->next;
2840                         break;
2841                 }
2842         }
2843
2844         for (; cur_item != NULL; cur_item = cur_item->next) {
2845                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
2846                                          (M_UNLOCKED & state) != 0);
2847         }
2848
2849         main_window_menu_callback_block(mainwin);
2850
2851 #define SET_CHECK_MENU_ACTIVE(path, active) \
2852 { \
2853         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2854         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2855 }
2856
2857         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
2858                               messageview_is_visible(mainwin->messageview));
2859
2860         summaryview = mainwin->summaryview;
2861         menu_path = "/View/Sort/Don't sort";
2862
2863         switch (summaryview->sort_key) {
2864         case SORT_BY_NUMBER:
2865                 menu_path = "/View/Sort/by number"; break;
2866         case SORT_BY_SIZE:
2867                 menu_path = "/View/Sort/by Size"; break;
2868         case SORT_BY_DATE:
2869                 menu_path = "/View/Sort/by Date"; break;
2870         case SORT_BY_THREAD_DATE:
2871                 menu_path = "/View/Sort/by Thread date"; break;
2872         case SORT_BY_FROM:
2873                 menu_path = "/View/Sort/by From"; break;
2874         case SORT_BY_TO:
2875                 menu_path = "/View/Sort/by To"; break;
2876         case SORT_BY_SUBJECT:
2877                 menu_path = "/View/Sort/by Subject"; break;
2878         case SORT_BY_LABEL:
2879                 menu_path = "/View/Sort/by color label"; break;
2880         case SORT_BY_MARK:
2881                 menu_path = "/View/Sort/by mark"; break;
2882         case SORT_BY_STATUS:
2883                 menu_path = "/View/Sort/by status"; break;
2884         case SORT_BY_MIME:
2885                 menu_path = "/View/Sort/by attachment"; break;
2886         case SORT_BY_SCORE:
2887                 menu_path = "/View/Sort/by score"; break;
2888         case SORT_BY_LOCKED:
2889                 menu_path = "/View/Sort/by locked"; break;
2890         case SORT_BY_TAGS:
2891                 menu_path = "/View/Sort/by tag"; break;
2892         case SORT_BY_NONE:
2893         default:
2894                 menu_path = "/View/Sort/Don't sort"; break;
2895         }
2896         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
2897
2898         if (summaryview->sort_type == SORT_ASCENDING) {
2899                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
2900         } else {
2901                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
2902         }
2903
2904         if (summaryview->sort_key != SORT_BY_NONE) {
2905                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
2906                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
2907         } else {
2908                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
2909                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
2910         }
2911
2912         if (mainwin->messageview 
2913         &&  mainwin->messageview->mimeview
2914         &&  mainwin->messageview->mimeview->textview)
2915                 SET_CHECK_MENU_ACTIVE("/View/All headers",
2916                               mainwin->messageview->mimeview->textview->show_all_headers);
2917         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
2918         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
2919         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
2920         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
2921         if (prefs_common.hide_quotes == 1)
2922                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", TRUE);
2923         if (prefs_common.hide_quotes == 2)
2924                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", TRUE);
2925         if (prefs_common.hide_quotes == 3)
2926                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", TRUE);
2927
2928 #undef SET_CHECK_MENU_ACTIVE
2929
2930         main_window_menu_callback_unblock(mainwin);
2931 }
2932
2933 void main_create_mailing_list_menu (MainWindow *mainwin, MsgInfo *msginfo)
2934 {
2935         GtkItemFactory *ifactory;
2936         gint is_menu = 0;
2937         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2938         
2939         if (msginfo) 
2940                 is_menu = mailing_list_create_submenu (ifactory, msginfo);
2941         if (is_menu)
2942                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2943                                 (ifactory,"/Message/Mailing-List"), TRUE);
2944         else
2945                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2946                                 (ifactory,"/Message/Mailing-List"), FALSE);
2947 }
2948
2949 static gint mailing_list_create_submenu (GtkItemFactory *ifactory, MsgInfo *msginfo)
2950 {
2951         gint menu_nb = 0;
2952         GtkWidget *menuitem;
2953         
2954         if (!msginfo || !msginfo->extradata) {
2955                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Post", FALSE);
2956                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Help", FALSE);
2957                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Subscribe", FALSE);
2958                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Unsubscribe", FALSE);
2959                 menu_set_sensitive(ifactory, "/Message/Mailing-List/View archive", FALSE);
2960                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Contact owner", FALSE);
2961                 return 0;
2962         }
2963                 
2964         /* Mailing list post */
2965         if (!strcmp2 (msginfo->extradata->list_post, "NO")) {
2966                 g_free(msginfo->extradata->list_post);
2967                 msginfo->extradata->list_post = g_strdup (_("No posting allowed"));
2968         }
2969         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Post");
2970                 
2971         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_post);
2972  
2973         /* Mailing list help */
2974         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Help");
2975         
2976         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_help);
2977
2978         /* Mailing list subscribe */
2979         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Subscribe");
2980         
2981         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_subscribe);
2982                 
2983         /* Mailing list unsubscribe */
2984         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Unsubscribe");
2985         
2986         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_unsubscribe);
2987         
2988         /* Mailing list view archive */
2989         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/View archive");
2990         
2991         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_archive);
2992         
2993         /* Mailing list contact owner */
2994         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Contact owner");
2995         
2996         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_owner);
2997         
2998         return menu_nb;
2999 }
3000
3001 static gint mailing_list_populate_submenu (GtkWidget *menuitem, const gchar * list_header)
3002 {
3003         GtkWidget *item, *menu;
3004         const gchar *url_pt ;
3005         gchar url_decoded[BUFFSIZE];
3006         GList *amenu, *alist;
3007         gint menu_nb = 0;
3008         
3009         menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
3010         
3011         /* First delete old submenu */
3012         /* FIXME: we can optimize this, and only change/add/delete necessary items */
3013         for (amenu = (GTK_MENU_SHELL(menu)->children) ; amenu; ) {
3014                 alist = amenu->next;
3015                 item = GTK_WIDGET (amenu->data);
3016                 gtk_widget_destroy (item);
3017                 amenu = alist;
3018         }
3019         if (list_header) {
3020                 for (url_pt = list_header; url_pt && *url_pt;) {
3021                         get_url_part (&url_pt, url_decoded, BUFFSIZE);
3022                         item = NULL;
3023                         if (!g_ascii_strncasecmp(url_decoded, "mailto:", 7)) {
3024                                 item = gtk_menu_item_new_with_label ((url_decoded));
3025                                 g_signal_connect(G_OBJECT(item), "activate",
3026                                                  G_CALLBACK(mailing_list_compose),
3027                                                  NULL);
3028                         }
3029                         else if (!g_ascii_strncasecmp(url_decoded, "http:", 5) ||
3030                                  !g_ascii_strncasecmp(url_decoded, "https:",6)) {
3031
3032                                 item = gtk_menu_item_new_with_label ((url_decoded));
3033                                 g_signal_connect(G_OBJECT(item), "activate",
3034                                                  G_CALLBACK(mailing_list_open_uri),
3035                                                  NULL);
3036                         } 
3037                         if (item) {
3038                                 gtk_menu_append (GTK_MENU(menu), item);
3039                                 gtk_widget_show (item);
3040                                 menu_nb++;
3041                         }
3042                 }
3043         }
3044         if (menu_nb)
3045                 gtk_widget_set_sensitive (menuitem, TRUE);
3046         else
3047                 gtk_widget_set_sensitive (menuitem, FALSE);
3048                 
3049
3050         return menu_nb;
3051 }
3052
3053 static void get_url_part (const gchar **buffer, gchar *url_decoded, gint maxlen)
3054 {
3055         gchar tmp[BUFFSIZE];
3056         const gchar *buf;
3057         gint i = 0;
3058         buf = *buffer;
3059         gboolean with_plus = TRUE;
3060
3061         if (buf == 0x00) {
3062                 *url_decoded = '\0';
3063                 *buffer = NULL;
3064                 return;
3065         }
3066         /* Ignore spaces, comments  and tabs () */
3067         for (;*buf == ' ' || *buf == '(' || *buf == '\t'; buf++)
3068                 if (*buf == '(')
3069                         for (;*buf != ')' && *buf != 0x00; buf++);
3070         
3071         /* First non space and non comment must be a < */
3072         if (*buf =='<' ) {
3073                 buf++;
3074                 if (!strncmp(buf, "mailto:", strlen("mailto:")))
3075                         with_plus = FALSE;
3076                 for (i = 0; *buf != '>' && *buf != 0x00 && i<maxlen; tmp[i++] = *(buf++));
3077                 buf++;
3078         }
3079         else  {
3080                 *buffer = NULL;
3081                 *url_decoded = '\0';
3082                 return;
3083         }
3084         
3085         tmp[i]       = 0x00;
3086         *url_decoded = '\0';
3087         *buffer = NULL;
3088         
3089         if (i == maxlen) {
3090                 return;
3091         }
3092         decode_uri_with_plus (url_decoded, (const gchar *)tmp, with_plus);
3093
3094         /* Prepare the work for the next url in the list */
3095         /* after the closing bracket >, ignore space, comments and tabs */
3096         for (;buf && *buf && (*buf == ' ' || *buf == '(' || *buf == '\t'); buf++)
3097                 if (*buf == '(')
3098                         for (;*buf != ')' && *buf != 0x00; buf++);
3099                         
3100         if (!buf || !*buf) {
3101                 *buffer = NULL;
3102                 return;
3103         }
3104
3105         /* now first non space, non comment must be a comma */
3106         if (*buf != ',')
3107                 for (;*buf != 0x00; buf++);
3108         else
3109                 buf++;
3110         *buffer = buf;
3111 }
3112         
3113 static void mailing_list_compose (GtkWidget *w, gpointer *data)
3114 {
3115         gchar *mailto;
3116
3117         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
3118         compose_new(NULL, mailto+7, NULL);
3119 }
3120  
3121  static void mailing_list_open_uri (GtkWidget *w, gpointer *data)
3122 {
3123  
3124         gchar *mailto;
3125  
3126         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
3127         open_uri (mailto, prefs_common_get_uri_cmd());
3128
3129         
3130 void main_window_popup(MainWindow *mainwin)
3131 {
3132         if (!GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
3133                 main_window_show(mainwin);
3134
3135         if (prefs_common.mainwin_maximised)
3136                 gtk_window_maximize(GTK_WINDOW(mainwin->window));
3137
3138         gtkut_window_popup(mainwin->window);
3139
3140         if (prefs_common.layout_mode == SMALL_LAYOUT) {
3141                 if (mainwin->in_folder) {
3142                         mainwindow_enter_folder(mainwin);
3143                 } else {
3144                         mainwindow_exit_folder(mainwin);
3145                 }
3146         }
3147 }
3148
3149 void main_window_show(MainWindow *mainwin)
3150 {
3151         gtk_widget_show(mainwin->window);
3152         gtk_widget_show(mainwin->vbox_body);
3153
3154         gtk_widget_set_uposition(mainwin->window,
3155                                  prefs_common.mainwin_x,
3156                                  prefs_common.mainwin_y);
3157
3158         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
3159                              prefs_common.folderview_width,
3160                              prefs_common.folderview_height);
3161         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
3162                              prefs_common.summaryview_width,
3163                              prefs_common.summaryview_height);
3164         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
3165                              prefs_common.msgview_width,
3166                              prefs_common.msgview_height);
3167 }
3168
3169 void main_window_hide(MainWindow *mainwin)
3170 {
3171         main_window_get_size(mainwin);
3172         main_window_get_position(mainwin);
3173
3174         gtk_widget_hide(mainwin->window);
3175         gtk_widget_hide(mainwin->vbox_body);
3176 }
3177
3178 static void main_window_set_widgets(MainWindow *mainwin, LayoutType layout_mode)
3179 {
3180         GtkWidget *folderwin = NULL;
3181         GtkWidget *messagewin = NULL;
3182         GtkWidget *hpaned;
3183         GtkWidget *vpaned;
3184         GtkWidget *vbox_body = mainwin->vbox_body;
3185         GtkItemFactory *ifactory = mainwin->menu_factory;
3186         GtkWidget *menuitem;
3187         gboolean first_set = (mainwin->hpaned == NULL);
3188         debug_print("Setting widgets... ");
3189
3190         if (layout_mode == SMALL_LAYOUT && first_set) {
3191                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
3192                                     prefs_common.folderview_width,
3193                                     prefs_common.folderview_height);
3194                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
3195                                     0,0);
3196                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
3197                                     0,0);
3198         } else {
3199                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
3200                                     prefs_common.folderview_width,
3201                                     prefs_common.folderview_height);
3202                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
3203                                     prefs_common.summaryview_width,
3204                                     prefs_common.summaryview_height);
3205                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
3206                                     prefs_common.msgview_width,
3207                                     prefs_common.msgview_height);
3208         }
3209
3210 #ifndef MAEMO
3211         mainwin->messageview->statusbar = mainwin->statusbar;
3212         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
3213 #endif
3214         /* clean top-most container */
3215         if (mainwin->hpaned) {
3216                 if (mainwin->hpaned->parent == mainwin->vpaned)
3217                         gtk_widget_destroy(mainwin->vpaned);
3218                 else
3219                         gtk_widget_destroy(mainwin->hpaned);
3220         }
3221
3222         menu_set_sensitive(ifactory, "/View/Show or hide/Message view", 
3223                 (layout_mode != WIDE_MSGLIST_LAYOUT && layout_mode != SMALL_LAYOUT));
3224         switch (layout_mode) {
3225         case VERTICAL_LAYOUT:
3226         case NORMAL_LAYOUT:
3227         case SMALL_LAYOUT:
3228                 hpaned = gtk_hpaned_new();
3229                 if (layout_mode == VERTICAL_LAYOUT)
3230                         vpaned = gtk_hpaned_new();
3231                 else
3232                         vpaned = gtk_vpaned_new();
3233                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
3234                 gtk_paned_add1(GTK_PANED(hpaned),
3235                                GTK_WIDGET_PTR(mainwin->folderview));
3236                 gtk_widget_show(hpaned);
3237                 gtk_widget_queue_resize(hpaned);
3238
3239                 if (messageview_is_visible(mainwin->messageview)) {
3240                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
3241                         gtk_paned_add1(GTK_PANED(vpaned),
3242                                        GTK_WIDGET_PTR(mainwin->summaryview));
3243                 } else {
3244                         gtk_paned_add2(GTK_PANED(hpaned),
3245                                        GTK_WIDGET_PTR(mainwin->summaryview));
3246                         gtk_widget_ref(vpaned);
3247                 }
3248                 gtk_paned_add2(GTK_PANED(vpaned),
3249                                GTK_WIDGET_PTR(mainwin->messageview));
3250                 gtk_widget_show(vpaned);
3251                 if (layout_mode == SMALL_LAYOUT && first_set) {
3252                         mainwin_paned_show_first(GTK_PANED(hpaned));
3253                 }
3254                 gtk_widget_queue_resize(vpaned);
3255                 break;
3256         case WIDE_LAYOUT:
3257                 vpaned = gtk_vpaned_new();
3258                 hpaned = gtk_hpaned_new();
3259                 gtk_box_pack_start(GTK_BOX(vbox_body), vpaned, TRUE, TRUE, 0);
3260                 gtk_paned_add1(GTK_PANED(vpaned), hpaned);
3261
3262                 gtk_paned_add1(GTK_PANED(hpaned),
3263                                GTK_WIDGET_PTR(mainwin->folderview));
3264                 gtk_paned_add2(GTK_PANED(hpaned),
3265                                GTK_WIDGET_PTR(mainwin->summaryview));
3266
3267                 gtk_widget_show(hpaned);
3268                 gtk_widget_queue_resize(hpaned);
3269
3270                 if (messageview_is_visible(mainwin->messageview)) {
3271                         gtk_paned_add2(GTK_PANED(vpaned),
3272                                GTK_WIDGET_PTR(mainwin->messageview));   
3273                 } else {
3274                         gtk_widget_ref(GTK_WIDGET_PTR(mainwin->messageview));
3275                 }
3276                 gtk_widget_show(vpaned);
3277                 gtk_widget_queue_resize(vpaned);
3278                 break;
3279         case WIDE_MSGLIST_LAYOUT:
3280                 vpaned = gtk_vpaned_new();
3281                 hpaned = gtk_hpaned_new();
3282                 gtk_box_pack_start(GTK_BOX(vbox_body), vpaned, TRUE, TRUE, 0);
3283
3284                 gtk_paned_add1(GTK_PANED(vpaned),
3285                                GTK_WIDGET_PTR(mainwin->summaryview));
3286                 gtk_paned_add1(GTK_PANED(hpaned),
3287                                GTK_WIDGET_PTR(mainwin->folderview));
3288
3289                 gtk_widget_show(hpaned);
3290                 gtk_widget_queue_resize(hpaned);
3291
3292                 if (messageview_is_visible(mainwin->messageview)) {
3293                         gtk_paned_add2(GTK_PANED(hpaned),
3294                                GTK_WIDGET_PTR(mainwin->messageview));   
3295                 } else {
3296                         gtk_widget_ref(GTK_WIDGET_PTR(mainwin->messageview));
3297                 }
3298                 gtk_paned_add2(GTK_PANED(vpaned), hpaned);
3299
3300                 gtk_widget_show(vpaned);
3301                 gtk_widget_queue_resize(vpaned);
3302                 break;
3303         default:
3304                 g_warning("Unknown layout");
3305                 return;
3306         }
3307
3308         mainwin->hpaned = hpaned;
3309         mainwin->vpaned = vpaned;
3310
3311         if (layout_mode == SMALL_LAYOUT) {
3312                 if (mainwin->messageview->visible)
3313                         main_window_toggle_message_view(mainwin);
3314         } 
3315
3316         if (layout_mode == SMALL_LAYOUT && first_set) {
3317                 gtk_widget_realize(mainwin->window);
3318                 gtk_widget_realize(mainwin->folderview->ctree);
3319                 gtk_widget_realize(mainwin->summaryview->hbox);
3320                 gtk_widget_realize(mainwin->summaryview->hbox_l);
3321                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
3322                                     prefs_common.folderview_width,
3323                                     prefs_common.folderview_height);
3324                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
3325                                     0,0);
3326                 gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
3327                                     0,0);
3328                 gtk_widget_set_size_request(GTK_WIDGET(mainwin->window),
3329                                 prefs_common.mainwin_width,
3330                                 prefs_common.mainwin_height);
3331                 gtk_paned_set_position(GTK_PANED(mainwin->hpaned), 800);
3332         } 
3333         /* remove headerview if not in prefs */
3334         headerview_set_visibility(mainwin->messageview->headerview,
3335                                   prefs_common.display_header_pane);
3336
3337         if (messageview_is_visible(mainwin->messageview))
3338                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
3339                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
3340         else
3341                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
3342                               GTK_ARROW_UP, GTK_SHADOW_OUT);
3343
3344         gtk_window_move(GTK_WINDOW(mainwin->window),
3345                         prefs_common.mainwin_x,
3346                         prefs_common.mainwin_y);
3347
3348         gtk_widget_queue_resize(vbox_body);
3349         gtk_widget_queue_resize(mainwin->vbox);
3350         gtk_widget_queue_resize(mainwin->window);
3351         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
3352          * and mimeview icon list/ctree lose track of their visibility states */
3353         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
3354                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
3355         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
3356                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
3357         if (mainwin->messageview->mimeview->ctree_mode)
3358                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
3359         else 
3360                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
3361
3362         prefs_common.layout_mode = layout_mode;
3363
3364         menuitem = gtk_item_factory_get_item
3365                 (ifactory, "/View/Show or hide/Message view");
3366         gtk_check_menu_item_set_active
3367                 (GTK_CHECK_MENU_ITEM(menuitem),
3368                  messageview_is_visible(mainwin->messageview));
3369
3370 #define SET_CHECK_MENU_ACTIVE(path, active) \
3371 { \
3372         menuitem = gtk_item_factory_get_widget(ifactory, path); \
3373         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
3374 }
3375
3376 #ifndef MAEMO
3377         switch (prefs_common.layout_mode) {
3378         case NORMAL_LAYOUT:
3379                 SET_CHECK_MENU_ACTIVE("/View/Layout/Standard", TRUE);
3380                 break;
3381         case VERTICAL_LAYOUT:
3382                 SET_CHECK_MENU_ACTIVE("/View/Layout/Three columns", TRUE);
3383                 break;
3384         case WIDE_LAYOUT:
3385                 SET_CHECK_MENU_ACTIVE("/View/Layout/Wide message", TRUE);
3386                 break;
3387         case WIDE_MSGLIST_LAYOUT:
3388                 SET_CHECK_MENU_ACTIVE("/View/Layout/Wide message list", TRUE);
3389                 break;
3390         case SMALL_LAYOUT:
3391                 SET_CHECK_MENU_ACTIVE("/View/Layout/Small screen", TRUE);
3392                 break;
3393         }
3394 #endif
3395 #undef SET_CHECK_MENU_ACTIVE
3396
3397         if (folderwin) {
3398                 g_signal_connect
3399                         (G_OBJECT(folderwin), "size_allocate",
3400                          G_CALLBACK(folder_window_size_allocate_cb),
3401                          mainwin);
3402         }
3403         if (messagewin) {
3404                 g_signal_connect
3405                         (G_OBJECT(messagewin), "size_allocate",
3406                          G_CALLBACK(message_window_size_allocate_cb),
3407                          mainwin);
3408         }
3409
3410         debug_print("done.\n");
3411 }
3412
3413 void main_window_destroy_all(void)
3414 {
3415         while (mainwin_list != NULL) {
3416                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
3417                 
3418                 /* free toolbar stuff */
3419                 toolbar_clear_list(TOOLBAR_MAIN);
3420                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
3421                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
3422
3423                 summaryview_destroy(mainwin->summaryview);
3424                 mainwin->folderview->mainwin = NULL;
3425                 mainwin->summaryview->mainwin = NULL;
3426                 mainwin->messageview->mainwin = NULL;
3427
3428                 g_free(mainwin->toolbar);
3429                 g_free(mainwin);
3430                 
3431                 mainwin_list = g_list_remove(mainwin_list, mainwin);
3432         }
3433         g_list_free(mainwin_list);
3434         mainwin_list = NULL;
3435 }
3436
3437 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
3438                                    gpointer data)
3439 {
3440         gtk_widget_set_size_request(child, 1, -1);
3441 }
3442
3443 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
3444                                    gpointer data)
3445 {
3446         gtk_widget_set_size_request(child, -1, -1);
3447 }
3448 #ifndef MAEMO
3449 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
3450                                     gpointer data)
3451 {
3452         MainWindow *mainwin = (MainWindow *)data;
3453
3454         if (!event) return FALSE;
3455
3456         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
3457         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
3458                           widget);
3459
3460         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
3461                        menu_button_position, widget,
3462                        event->button, event->time);
3463
3464         return TRUE;
3465 }
3466 #endif
3467 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
3468                                  gpointer data)
3469 {
3470         MainWindow *mainwin = (MainWindow *)data;
3471         gboolean close_allowed = TRUE;
3472
3473         hooks_invoke(MAIN_WINDOW_CLOSE, &close_allowed);
3474
3475         if (close_allowed && mainwin->lock_count == 0)
3476                 app_exit_cb(data, 0, widget);
3477
3478         return TRUE;
3479 }
3480
3481 static void main_window_size_allocate_cb(GtkWidget *widget,
3482                                          GtkAllocation *allocation,
3483                                          gpointer data)
3484 {
3485         MainWindow *mainwin = (MainWindow *)data;
3486         main_window_get_size(mainwin);
3487 }
3488
3489 static void folder_window_size_allocate_cb(GtkWidget *widget,
3490                                            GtkAllocation *allocation,
3491                                            gpointer data)
3492 {
3493         MainWindow *mainwin = (MainWindow *)data;
3494
3495         main_window_get_size(mainwin);
3496 }
3497
3498 static void message_window_size_allocate_cb(GtkWidget *widget,
3499                                             GtkAllocation *allocation,
3500                                             gpointer data)
3501 {
3502         MainWindow *mainwin = (MainWindow *)data;
3503
3504         main_window_get_size(mainwin);
3505 }
3506
3507 static void add_mailbox_cb(MainWindow *mainwin, guint action,
3508                            GtkWidget *widget)
3509 {
3510         main_window_add_mailbox(mainwin);
3511 }
3512
3513 static void update_folderview_cb(MainWindow *mainwin, guint action,
3514                                  GtkWidget *widget)
3515 {
3516         summary_show(mainwin->summaryview, NULL);
3517         folderview_check_new_all();
3518 }
3519
3520 static void foldersort_cb(MainWindow *mainwin, guint action,
3521                            GtkWidget *widget)
3522 {
3523         foldersort_open();
3524 }
3525
3526 static void import_mbox_cb(MainWindow *mainwin, guint action,
3527                            GtkWidget *widget)
3528 {
3529         /* only notify if import has failed */
3530         if (import_mbox(mainwin->summaryview->folder_item) == -1) {
3531                 alertpanel_error(_("Mbox import has failed."));
3532         }
3533 }
3534
3535 static void export_mbox_cb(MainWindow *mainwin, guint action,
3536                            GtkWidget *widget)
3537 {
3538         /* only notify if export has failed */
3539         if (export_mbox(mainwin->summaryview->folder_item) == -1) {
3540                 alertpanel_error(_("Export to mbox has failed."));
3541         }
3542 }
3543
3544 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
3545                                 GtkWidget *widget)
3546 {
3547         /* only notify if export has failed */
3548         if (summaryview_export_mbox_list(mainwin->summaryview) == -1) {
3549                 alertpanel_error(_("Export to mbox has failed."));
3550         }
3551 }
3552
3553 static void empty_trash_cb(MainWindow *mainwin, guint action,
3554                            GtkWidget *widget)
3555 {
3556         main_window_empty_trash(mainwin, TRUE);
3557 }
3558
3559 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3560 {
3561         summary_save_as(mainwin->summaryview);
3562 }
3563
3564 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3565 {
3566         summary_print(mainwin->summaryview);
3567 }
3568
3569 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
3570 static void page_setup_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3571 {
3572         MainWindow *mainwindow;
3573         GtkWindow *win;
3574
3575         mainwindow = mainwindow_get_mainwindow();
3576         win = (mainwindow ? GTK_WINDOW(mainwindow->window) : NULL);
3577
3578         printing_page_setup(win);
3579 }
3580 #endif
3581
3582 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3583 {
3584         if (prefs_common.confirm_on_exit) {
3585                 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
3586                                GTK_STOCK_CANCEL, GTK_STOCK_QUIT,  NULL)
3587                     != G_ALERTALTERNATE)
3588                         return;
3589                 manage_window_focus_in(mainwin->window, NULL, NULL);
3590         }
3591
3592         app_will_exit(widget, mainwin);
3593 }
3594
3595 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3596 {
3597         if (action == 1)
3598                 summary_search(mainwin->summaryview);
3599         else
3600                 message_search(mainwin->messageview);
3601 }
3602
3603 static void mainwindow_quicksearch(MainWindow *mainwin, guint action, GtkWidget *widget)
3604 {
3605         summaryview_activate_quicksearch(mainwin->summaryview, TRUE);
3606 }
3607
3608 static void toggle_message_cb(MainWindow *mainwin, guint action,
3609                               GtkWidget *widget)
3610 {
3611         gboolean active;
3612
3613         active = GTK_CHECK_MENU_ITEM(widget)->active;
3614
3615         if (active != messageview_is_visible(mainwin->messageview))
3616                 summary_toggle_view(mainwin->summaryview);
3617 }
3618
3619 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
3620                               GtkWidget *widget)
3621 {
3622         toolbar_toggle(action, mainwin);
3623 }
3624
3625 static void main_window_reply_cb(MainWindow *mainwin, guint action,
3626                           GtkWidget *widget)
3627 {
3628         MessageView *msgview = (MessageView*)mainwin->messageview;
3629         GSList *msginfo_list = NULL;
3630
3631         g_return_if_fail(msgview != NULL);
3632
3633         msginfo_list = summary_get_selection(mainwin->summaryview);
3634         g_return_if_fail(msginfo_list != NULL);
3635         compose_reply_from_messageview(msgview, msginfo_list, action);
3636         g_slist_free(msginfo_list);
3637 }
3638
3639 static void toggle_col_headers_cb(MainWindow *mainwin, guint action,
3640                                 GtkWidget *widget)
3641 {
3642         FolderView *folderview = mainwin->folderview;
3643         SummaryView *summaryview = mainwin->summaryview;
3644
3645         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3646                 gtk_clist_column_titles_show(GTK_CLIST(folderview->ctree));
3647                 gtk_clist_column_titles_show(GTK_CLIST(summaryview->ctree));
3648                 prefs_common.show_col_headers = TRUE;
3649         } else {
3650                 gtk_clist_column_titles_hide(GTK_CLIST(folderview->ctree));
3651                 gtk_clist_column_titles_hide(GTK_CLIST(summaryview->ctree));
3652                 prefs_common.show_col_headers = FALSE;
3653         }
3654 }
3655
3656 #ifndef MAEMO
3657 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
3658                                 GtkWidget *widget)
3659 {
3660         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3661                 gtk_widget_show(mainwin->hbox_stat);
3662                 prefs_common.show_statusbar = TRUE;
3663         } else {
3664                 gtk_widget_hide(mainwin->hbox_stat);
3665                 prefs_common.show_statusbar = FALSE;
3666         }
3667 }
3668
3669 static void set_layout_cb(MainWindow *mainwin, guint action,
3670                                GtkWidget *widget)
3671 {
3672         LayoutType layout_mode = action;
3673         LayoutType old_layout_mode = prefs_common.layout_mode;
3674         if (mainwin->menu_lock_count) {
3675                 return;
3676         }
3677         if (!GTK_CHECK_MENU_ITEM(widget)->active) {
3678                 return;
3679         }
3680         
3681         if (layout_mode == prefs_common.layout_mode) {
3682                 return;
3683         }
3684         
3685         if (!mainwin->messageview->visible && layout_mode != SMALL_LAYOUT)
3686                 main_window_toggle_message_view(mainwin);
3687         else if (mainwin->messageview->visible && layout_mode == SMALL_LAYOUT)
3688                 main_window_toggle_message_view(mainwin);
3689
3690         main_window_separation_change(mainwin, layout_mode);
3691         mainwindow_reset_paned(GTK_PANED(mainwin->vpaned));
3692         if (old_layout_mode == SMALL_LAYOUT && layout_mode != SMALL_LAYOUT) {
3693                 mainwindow_reset_paned(GTK_PANED(mainwin->hpaned));
3694         }
3695         if (old_layout_mode != SMALL_LAYOUT && layout_mode == SMALL_LAYOUT) {
3696                 mainwin_paned_show_first(GTK_PANED(mainwin->hpaned));
3697                 mainwindow_exit_folder(mainwin);
3698         }
3699         summary_relayout(mainwin->summaryview); 
3700         summary_update_unread(mainwin->summaryview, NULL);
3701 }
3702 #endif
3703
3704 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline,
3705                                         gboolean ask_sync)
3706 {
3707         offline_ask_sync = ask_sync;
3708         if (offline)
3709                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
3710         else
3711                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
3712         offline_ask_sync = TRUE;
3713 }
3714
3715 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
3716 {
3717         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active, TRUE);
3718 }
3719
3720 static gboolean any_folder_want_synchronise(void)
3721 {
3722         GList *folderlist = folder_get_list();
3723
3724         /* see if there are synchronised folders */
3725         for (; folderlist; folderlist = folderlist->next) {
3726                 Folder *folder = (Folder *)folderlist->data;
3727                 if (folder_want_synchronise(folder)) {
3728                         return TRUE;
3729                 }
3730         }
3731         
3732         return FALSE;
3733 }
3734
3735 static void mainwindow_check_synchronise(MainWindow *mainwin, gboolean ask)
3736 {
3737         
3738         if (!any_folder_want_synchronise())
3739                 return;
3740
3741         if (offline_ask_sync && ask && alertpanel(_("Folder synchronisation"),
3742                         _("Do you want to synchronise your folders now?"),
3743                         GTK_STOCK_CANCEL, _("+_Synchronise"), NULL) != G_ALERTALTERNATE)
3744                 return;
3745         
3746         if (offline_ask_sync)
3747                 folder_synchronise(NULL);
3748 }
3749
3750 static void online_switch_clicked (GtkButton *btn, gpointer data) 
3751 {
3752         MainWindow *mainwin;
3753         GtkItemFactory *ifactory;
3754         GtkCheckMenuItem *menuitem;
3755         gboolean have_connectivity;
3756
3757 #ifdef HAVE_NETWORKMANAGER
3758         have_connectivity = networkmanager_is_online(NULL); 
3759 #else
3760         have_connectivity = TRUE;
3761 #endif
3762
3763         mainwin = (MainWindow *) data;
3764         
3765         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
3766         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
3767         
3768         g_return_if_fail(mainwin != NULL);
3769         g_return_if_fail(menuitem != NULL);
3770         
3771         if (btn == GTK_BUTTON(mainwin->online_switch)) {
3772 #ifndef MAEMO
3773                 gtk_widget_hide (mainwin->online_switch);
3774                 gtk_widget_show (mainwin->offline_switch);
3775 #endif
3776                 menuitem->active = TRUE;
3777                 inc_autocheck_timer_remove();
3778                         
3779                 /* go offline */
3780                 if (prefs_common.work_offline)
3781                         return;
3782
3783                 if(have_connectivity)
3784                         mainwindow_check_synchronise(mainwin, TRUE);
3785                 prefs_common.work_offline = TRUE;
3786                 imap_disconnect_all();
3787                 hooks_invoke(OFFLINE_SWITCH_HOOKLIST, NULL);
3788         } else {
3789                 /*go online */
3790                 if (!prefs_common.work_offline)
3791                         return;
3792 #ifndef MAEMO
3793                 gtk_widget_hide (mainwin->offline_switch);
3794                 gtk_widget_show (mainwin->online_switch);
3795 #endif
3796                 menuitem->active = FALSE;
3797                 prefs_common.work_offline = FALSE;
3798                 inc_autocheck_timer_set();
3799                 refresh_resolvers();
3800                 hooks_invoke(OFFLINE_SWITCH_HOOKLIST, NULL);
3801         }
3802 }
3803
3804 static void addressbook_open_cb(MainWindow *mainwin, guint action,
3805                                 GtkWidget *widget)
3806 {
3807         addressbook_open(NULL);
3808 }
3809
3810 static void log_window_show_cb(MainWindow *mainwin, guint action,
3811                                GtkWidget *widget)
3812 {
3813         log_window_show(mainwin->logwin);
3814 }
3815
3816 static void filtering_debug_window_show_cb(MainWindow *mainwin, guint action,
3817                                GtkWidget *widget)
3818 {
3819         log_window_show(mainwin->filtering_debugwin);
3820 }
3821
3822 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3823 {
3824         inc_cancel_all();
3825         imap_cancel_all();
3826 }
3827
3828 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3829 {
3830         summary_move_to(mainwin->summaryview);
3831 }
3832
3833 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3834 {
3835         summary_copy_to(mainwin->summaryview);
3836 }
3837
3838 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3839 {
3840         summary_delete(mainwin->summaryview);
3841 }
3842
3843 static void delete_trash_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3844 {
3845         summary_delete_trash(mainwin->summaryview);
3846 }
3847
3848 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3849 {
3850         summary_cancel(mainwin->summaryview);
3851 }
3852
3853 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3854 {
3855         summary_open_msg(mainwin->summaryview);
3856 }
3857
3858 static void view_source_cb(MainWindow *mainwin, guint action,
3859                            GtkWidget *widget)
3860 {
3861         summary_view_source(mainwin->summaryview);
3862 }
3863
3864 static void show_all_header_cb(MainWindow *mainwin, guint action,
3865                                GtkWidget *widget)
3866 {
3867         if (mainwin->menu_lock_count) return;
3868         mainwin->summaryview->messageview->all_headers = 
3869                         GTK_CHECK_MENU_ITEM(widget)->active;
3870         summary_display_msg_selected(mainwin->summaryview,
3871                                      GTK_CHECK_MENU_ITEM(widget)->active);
3872 }
3873
3874 #define SET_CHECK_MENU_ACTIVE(path, active) \
3875 { \
3876         menuitem = gtk_item_factory_get_widget(ifactory, path); \
3877         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
3878 }
3879
3880 static void hide_quotes_cb(MainWindow *mainwin, guint action,
3881                                GtkWidget *widget)
3882 {
3883         GtkWidget *menuitem;
3884         GtkItemFactory *ifactory = mainwin->menu_factory;
3885
3886         if (mainwin->menu_lock_count) return;
3887
3888         prefs_common.hide_quotes = 
3889                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
3890
3891         mainwin->menu_lock_count++;
3892         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
3893         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
3894         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
3895         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);
3896         mainwin->menu_lock_count--;
3897
3898         summary_redisplay_msg(mainwin->summaryview);
3899 }
3900
3901 #undef SET_CHECK_MENU_ACTIVE
3902 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3903 {
3904         summary_mark(mainwin->summaryview);
3905 }
3906
3907 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3908 {
3909         summary_unmark(mainwin->summaryview);
3910 }
3911
3912 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
3913                               GtkWidget *widget)
3914 {
3915         summary_mark_as_unread(mainwin->summaryview);
3916 }
3917
3918 static void mark_as_read_cb(MainWindow *mainwin, guint action,
3919                             GtkWidget *widget)
3920 {
3921         summary_mark_as_read(mainwin->summaryview);
3922 }
3923
3924 static void mark_all_read_cb(MainWindow *mainwin, guint action,
3925                              GtkWidget *widget)
3926 {
3927         summary_mark_all_read(mainwin->summaryview);
3928 }
3929
3930 static void mark_as_spam_cb(MainWindow *mainwin, guint action,
3931                              GtkWidget *widget)
3932 {
3933         summary_mark_as_spam(mainwin->summaryview, action, NULL);
3934 }
3935
3936 static void ignore_thread_cb(MainWindow *mainwin, guint action,
3937                             GtkWidget *widget)
3938 {
3939         summary_ignore_thread(mainwin->summaryview);
3940 }
3941
3942 static void unignore_thread_cb(MainWindow *mainwin, guint action,
3943                             GtkWidget *widget)
3944 {
3945         summary_unignore_thread(mainwin->summaryview);
3946 }
3947
3948 static void watch_thread_cb(MainWindow *mainwin, guint action,
3949                             GtkWidget *widget)
3950 {
3951         summary_watch_thread(mainwin->summaryview);
3952 }
3953
3954 static void unwatch_thread_cb(MainWindow *mainwin, guint action,
3955                             GtkWidget *widget)
3956 {
3957         summary_unwatch_thread(mainwin->summaryview);
3958 }
3959
3960 static void lock_msgs_cb(MainWindow *mainwin, guint action,
3961                             GtkWidget *widget)
3962 {
3963         summary_msgs_lock(mainwin->summaryview);
3964 }
3965
3966 static void unlock_msgs_cb(MainWindow *mainwin, guint action,
3967                             GtkWidget *widget)
3968 {
3969         summary_msgs_unlock(mainwin->summaryview);
3970 }
3971
3972
3973 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3974 {
3975         summary_reedit(mainwin->summaryview);
3976 }
3977
3978 static void open_urls_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3979 {
3980         if (!mainwin->summaryview->displayed && mainwin->summaryview->selected) {
3981                 summary_display_msg_selected(mainwin->summaryview, 
3982                         mainwin->messageview->mimeview->textview->show_all_headers);
3983         }
3984         messageview_list_urls(mainwin->messageview);
3985 }
3986
3987 static void add_address_cb(MainWindow *mainwin, guint action,
3988                            GtkWidget *widget)
3989 {
3990         summary_add_address(mainwin->summaryview);
3991 }
3992
3993 static void set_charset_cb(MainWindow *mainwin, guint action,
3994                            GtkWidget *widget)
3995 {
3996         const gchar *str;
3997
3998         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3999                 str = conv_get_charset_str((CharSet)action);
4000                 
4001                 g_free(mainwin->messageview->forced_charset);
4002                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
4003                 procmime_force_charset(str);
4004                 
4005                 summary_redisplay_msg(mainwin->summaryview);
4006                 
4007                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
4008         }
4009 }
4010
4011 static void set_decode_cb(MainWindow *mainwin, guint action,
4012                            GtkWidget *widget)
4013 {
4014         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4015                 mainwin->messageview->forced_encoding = (EncodingType)action;
4016                 
4017                 summary_redisplay_msg(mainwin->summaryview);
4018                 
4019                 debug_print("forced encoding: %d\n", action);
4020         }
4021 }
4022
4023 static void hide_read_messages (MainWindow *mainwin, guint action,
4024                                 GtkWidget *widget)
4025 {
4026         if (!mainwin->summaryview->folder_item
4027             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
4028                 return;
4029         summary_toggle_show_read_messages(mainwin->summaryview);
4030 }
4031
4032 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4033 {
4034         gboolean threaded = FALSE;
4035         if (mainwin->menu_lock_count) return;
4036         if (!mainwin->summaryview->folder_item) return;
4037
4038         threaded = GTK_CHECK_MENU_ITEM(widget)->active;
4039
4040         mainwin->summaryview->folder_item->threaded = threaded; 
4041
4042         mainwin->summaryview->threaded = threaded;
4043
4044         summary_show(mainwin->summaryview, 
4045                         mainwin->summaryview->folder_item);
4046 }
4047
4048 static void expand_threads_cb(MainWindow *mainwin, guint action,
4049                               GtkWidget *widget)
4050 {
4051         summary_expand_threads(mainwin->summaryview);
4052 }
4053
4054 static void collapse_threads_cb(MainWindow *mainwin, guint action,
4055                                 GtkWidget *widget)
4056 {
4057         summary_collapse_threads(mainwin->summaryview);
4058 }
4059
4060 static void set_summary_display_item_cb(MainWindow *mainwin, guint action,
4061                                 GtkWidget *widget)
4062 {
4063         prefs_summary_column_open();
4064 }
4065
4066 static void set_folder_display_item_cb(MainWindow *mainwin, guint action,
4067                                 GtkWidget *widget)
4068 {
4069         prefs_folder_column_open();
4070 }
4071
4072 static void sort_summary_cb(MainWindow *mainwin, guint action,
4073                             GtkWidget *widget)
4074 {
4075         FolderItem *item = mainwin->summaryview->folder_item;
4076         GtkWidget *menuitem;
4077
4078         if (mainwin->menu_lock_count) return;
4079
4080         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
4081                 menuitem = gtk_item_factory_get_item
4082                         (mainwin->menu_factory, "/View/Sort/Ascending");
4083                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
4084                              GTK_CHECK_MENU_ITEM(menuitem)->active
4085                              ? SORT_ASCENDING : SORT_DESCENDING);
4086                 item->sort_key = action;
4087         }
4088 }
4089
4090 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
4091                                  GtkWidget *widget)
4092 {
4093         FolderItem *item = mainwin->summaryview->folder_item;
4094
4095         if (mainwin->menu_lock_count) return;
4096
4097         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
4098                 summary_sort(mainwin->summaryview,
4099                              item->sort_key, (FolderSortType)action);
4100 }
4101
4102 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
4103                                   GtkWidget *widget)
4104 {
4105         summary_attract_by_subject(mainwin->summaryview);
4106 }
4107
4108 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
4109                                  GtkWidget *widget)
4110 {
4111         FolderItem *item;
4112
4113         item = folderview_get_selected_item(mainwin->folderview);
4114         if (item) {
4115                 main_window_cursor_wait(mainwin);
4116                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
4117
4118                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
4119                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
4120
4121                 STATUSBAR_POP(mainwin);
4122                 main_window_cursor_normal(mainwin);
4123         }
4124 }
4125
4126 struct DelDupsData
4127 {
4128         guint   dups;
4129         guint   folders;
4130 };
4131
4132 static void deldup_all(FolderItem *item, gpointer _data)
4133 {
4134         struct DelDupsData *data = _data;
4135         gint result;
4136         
4137         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
4138         if (result >= 0) {
4139                 data->dups += result;
4140                 data->folders += 1;
4141         }
4142 }
4143
4144 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
4145                                  GtkWidget *widget)
4146 {
4147         struct DelDupsData data = {0, 0};
4148
4149         main_window_cursor_wait(mainwin);
4150         folder_func_to_all_folders(deldup_all, &data);
4151         main_window_cursor_normal(mainwin);
4152         
4153         alertpanel_notice(ngettext("Deleted %d duplicate message in %d folders.\n",
4154                                    "Deleted %d duplicate messages in %d folders.\n",
4155                                    data.dups),
4156                           data.dups, data.folders);
4157 }
4158
4159 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4160 {
4161         summary_filter(mainwin->summaryview, (gboolean)action);
4162 }
4163
4164 static void process_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4165 {
4166         FolderItem *item = mainwin->summaryview->folder_item;   
4167         g_return_if_fail(item != NULL);
4168
4169         item->processing_pending = TRUE;
4170         folder_item_apply_processing(item);     
4171         item->processing_pending = FALSE;
4172 }
4173
4174 static void execute_summary_cb(MainWindow *mainwin, guint action,
4175                                GtkWidget *widget)
4176 {
4177         summary_execute(mainwin->summaryview);
4178 }
4179
4180 static void update_summary_cb(MainWindow *mainwin, guint action,
4181                               GtkWidget *widget)
4182 {
4183         FolderItem *fitem;
4184         FolderView *folderview = mainwin->folderview;
4185
4186         if (!mainwin->summaryview->folder_item) return;
4187         if (!folderview->opened) return;
4188
4189         folder_update_op_count();
4190
4191         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
4192                                             folderview->opened);
4193         if (!fitem) return;
4194
4195         folder_item_scan(fitem);
4196         summary_show(mainwin->summaryview, fitem);
4197 }
4198
4199 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4200 {
4201         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
4202 }
4203
4204 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4205 {
4206         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
4207 }
4208
4209 static void prev_unread_cb(MainWindow *mainwin, guint action,
4210                            GtkWidget *widget)
4211 {
4212         summary_select_prev_unread(mainwin->summaryview);
4213 }
4214
4215 static void next_unread_cb(MainWindow *mainwin, guint action,
4216                            GtkWidget *widget)
4217 {
4218         summary_select_next_unread(mainwin->summaryview);
4219 }
4220
4221 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4222 {
4223         summary_select_prev_new(mainwin->summaryview);
4224 }
4225
4226 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4227 {
4228         summary_select_next_new(mainwin->summaryview);
4229 }
4230
4231 static void prev_marked_cb(MainWindow *mainwin, guint action,
4232                            GtkWidget *widget)
4233 {
4234         summary_select_prev_marked(mainwin->summaryview);
4235 }
4236
4237 static void next_marked_cb(MainWindow *mainwin, guint action,
4238                            GtkWidget *widget)
4239 {
4240         summary_select_next_marked(mainwin->summaryview);
4241 }
4242
4243 static void prev_labeled_cb(MainWindow *mainwin, guint action,
4244                             GtkWidget *widget)
4245 {
4246         summary_select_prev_labeled(mainwin->summaryview);
4247 }
4248
4249 static void next_labeled_cb(MainWindow *mainwin, guint action,
4250                             GtkWidget *widget)
4251 {
4252         summary_select_next_labeled(mainwin->summaryview);
4253 }
4254
4255 static void last_read_cb(MainWindow *mainwin, guint action,
4256                             GtkWidget *widget)
4257 {
4258         summary_select_last_read(mainwin->summaryview);
4259 }
4260
4261 static void parent_cb(MainWindow *mainwin, guint action,
4262                             GtkWidget *widget)
4263 {
4264         summary_select_parent(mainwin->summaryview);
4265 }
4266
4267 static void goto_folder_cb(MainWindow *mainwin, guint action,
4268                            GtkWidget *widget)
4269 {
4270         FolderItem *to_folder;
4271
4272         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
4273
4274         if (to_folder)
4275                 folderview_select(mainwin->folderview, to_folder);
4276 }
4277
4278 static void goto_unread_folder_cb(MainWindow *mainwin, guint action,
4279                            GtkWidget *widget)
4280 {
4281         folderview_select_next_unread(mainwin->folderview, FALSE);
4282 }
4283
4284 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4285 {
4286         messageview_copy_clipboard(mainwin->messageview);
4287 }
4288
4289 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4290 {
4291         MessageView *msgview = mainwin->messageview;
4292
4293         if (messageview_is_visible(msgview) &&
4294                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
4295                 messageview_select_all(mainwin->messageview);
4296         else
4297                 summary_select_all(mainwin->summaryview);
4298 }
4299
4300 static void select_thread_cb(MainWindow *mainwin, guint action,
4301                              GtkWidget *widget)
4302 {
4303         summary_select_thread(mainwin->summaryview, FALSE);
4304 }
4305
4306 static void delete_thread_cb(MainWindow *mainwin, guint action,
4307                              GtkWidget *widget)
4308 {
4309         summary_select_thread(mainwin->summaryview, TRUE);
4310 }
4311
4312 static void create_filter_cb(MainWindow *mainwin, guint action,
4313                              GtkWidget *widget)
4314 {
4315         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
4316 }
4317
4318 static void create_processing_cb(MainWindow *mainwin, guint action,
4319                              GtkWidget *widget)
4320 {
4321         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
4322 }
4323
4324 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
4325                                          GtkWidget *widget)
4326 {
4327         prefs_filtering_open(&pre_global_processing,
4328                              _("Processing rules to apply before folder rules"),
4329                              MANUAL_ANCHOR_PROCESSING,
4330                              NULL, NULL, FALSE);
4331 }
4332
4333 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
4334                                           GtkWidget *widget)
4335 {
4336         prefs_filtering_open(&post_global_processing,
4337                              _("Processing rules to apply after folder rules"),
4338                              MANUAL_ANCHOR_PROCESSING,
4339                              NULL, NULL, FALSE);
4340 }
4341
4342 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
4343                                     GtkWidget *widget)
4344 {
4345         prefs_filtering_open(&filtering_rules,
4346                              _("Filtering configuration"),
4347                              MANUAL_ANCHOR_FILTERING,
4348                              NULL, NULL, TRUE);
4349 }
4350
4351 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
4352                                    GtkWidget *widget)
4353 {
4354         prefs_template_open();
4355 }
4356
4357 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
4358                                   GtkWidget *widget)
4359 {
4360         prefs_actions_open(mainwin);
4361 }
4362
4363 static void prefs_tags_open_cb(MainWindow *mainwin, guint action,
4364                                   GtkWidget *widget)
4365 {
4366         GSList * list = summary_get_selected_msg_list(mainwin->summaryview);
4367         tag_apply_open(list);
4368 }
4369 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
4370 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
4371                                   GtkWidget *widget)
4372 {
4373         ssl_manager_open(mainwin);
4374 }
4375 #endif
4376 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
4377                                   GtkWidget *widget)
4378 {
4379         if (!cur_account) {
4380                 new_account_cb(mainwin, 0, widget);
4381         } else {
4382                 account_open(cur_account);
4383         }
4384 }
4385
4386 static void new_account_cb(MainWindow *mainwin, guint action,
4387                            GtkWidget *widget)
4388 {
4389         account_edit_open();
4390         if (!compose_get_compose_list()) account_add();
4391 }
4392
4393 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
4394 {
4395         FolderItem *item = NULL;
4396         cur_account = (PrefsAccount *)data;
4397         
4398         if (!mainwindow_get_mainwindow())
4399                 return;
4400         main_window_show_cur_account(mainwindow_get_mainwindow());
4401         toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
4402         main_window_set_menu_sensitive(mainwindow_get_mainwindow());
4403         toolbar_main_set_sensitive(mainwindow_get_mainwindow());
4404         item = folderview_get_selected_item(
4405                         mainwindow_get_mainwindow()->folderview);
4406         if (item) {
4407                 toolbar_set_compose_button
4408                         (mainwindow_get_mainwindow()->toolbar,
4409                          FOLDER_TYPE(item->folder) == F_NEWS ? 
4410                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
4411         }
4412 }
4413
4414 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
4415 {
4416         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
4417         PrefsAccount *account = (PrefsAccount *)data;
4418
4419         inc_account_mail(mainwin, account);
4420 }
4421 #ifndef MAEMO
4422 static void account_compose_menu_cb(GtkMenuItem *menuitem, gpointer data)
4423 {
4424         PrefsAccount *account = (PrefsAccount *)data;
4425
4426         compose_new_with_folderitem(account, NULL, NULL);
4427 }
4428 #endif
4429 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
4430 {
4431         prefs_gtk_open();
4432 }
4433
4434 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
4435 {
4436         pluginwindow_create();
4437 }
4438
4439 static void manual_open_cb(MainWindow *mainwin, guint action,
4440                            GtkWidget *widget)
4441 {
4442         manual_open((ManualType)action, NULL);
4443 }
4444
4445 static void legend_open_cb(GtkMenuItem *menuitem, gpointer data)
4446 {
4447         legend_show();
4448 }
4449
4450 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
4451 {
4452         MainWindow *mainwin = (MainWindow *)data;
4453         gchar *str;
4454
4455         if (item->path)
4456                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
4457                                       LOCAL_FOLDER(folder)->rootpath,
4458                                       G_DIR_SEPARATOR,
4459                                       item->path);
4460         else
4461                 str = g_strdup_printf(_("Scanning folder %s ..."),
4462                                       LOCAL_FOLDER(folder)->rootpath);
4463
4464         STATUSBAR_PUSH(mainwin, str);
4465         STATUSBAR_POP(mainwin);
4466         g_free(str);
4467 }
4468
4469 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
4470                                           gpointer data)
4471 {
4472         SummaryView *summary;
4473
4474         g_return_val_if_fail(data, FALSE);
4475         if (!g_list_find(mainwin_list, data))
4476                 return TRUE;
4477         summary = ((MainWindow *)data)->summaryview;
4478         g_return_val_if_fail(summary, FALSE);
4479
4480         if (GTK_CLIST(summary->ctree)->selection && 
4481             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
4482                 return FALSE;
4483
4484         return FALSE;
4485 }
4486
4487 static gboolean mainwindow_visibility_event_cb(GtkWidget *widget, GdkEventVisibility *event,
4488                                           gpointer data)
4489 {
4490         is_obscured = (event->state == GDK_VISIBILITY_FULLY_OBSCURED);
4491         return FALSE;
4492 }
4493
4494 static gboolean mainwindow_state_event_cb(GtkWidget *widget, GdkEventWindowState *state,
4495                                           gpointer data)
4496 {
4497         if (!claws_is_starting()
4498                 && state->changed_mask&GDK_WINDOW_STATE_ICONIFIED
4499                 && state->new_window_state&GDK_WINDOW_STATE_ICONIFIED) {
4500
4501                 if (iconified_count > 0)
4502                         hooks_invoke(MAIN_WINDOW_GOT_ICONIFIED, NULL);
4503                 iconified_count++;
4504         } else if (!claws_is_starting()) {
4505                 prefs_common.mainwin_maximised = 
4506                         ((state->new_window_state&GDK_WINDOW_STATE_MAXIMIZED) != 0);
4507         }
4508         if (state->new_window_state == 0)
4509                 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(widget), FALSE);
4510         return FALSE;
4511 }
4512
4513 gboolean mainwindow_is_obscured(void)
4514 {
4515         return is_obscured;
4516 }
4517
4518 /*
4519  * Harvest addresses for selected folder.
4520  */
4521 static void addr_harvest_cb( MainWindow *mainwin,
4522                             guint action,
4523                             GtkWidget *widget )
4524 {
4525         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
4526 }
4527
4528 /*
4529  * Harvest addresses for selected messages in summary view.
4530  */
4531 static void addr_harvest_msg_cb( MainWindow *mainwin,
4532                             guint action,
4533                             GtkWidget *widget )
4534 {
4535         summary_harvest_address( mainwin->summaryview );
4536 }
4537
4538 /*!
4539  *\brief        get a MainWindow
4540  *
4541  *\return       MainWindow * The first mainwindow in the mainwin_list
4542  */
4543 MainWindow *mainwindow_get_mainwindow(void)
4544 {
4545         if (mainwin_list && mainwin_list->data)
4546                 return (MainWindow *)(mainwin_list->data);
4547         else
4548                 return NULL;
4549 }
4550
4551 static gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
4552 {
4553         ProgressData *data = (ProgressData *) source;
4554         MainWindow *mainwin = (MainWindow *) userdata;
4555
4556         switch (data->cmd) {
4557         case PROGRESS_COMMAND_START:
4558         case PROGRESS_COMMAND_STOP:
4559                 gtk_progress_bar_set_fraction
4560                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
4561                 break;
4562         case PROGRESS_COMMAND_SET_PERCENTAGE:
4563                 gtk_progress_bar_set_fraction
4564                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
4565                 break;          
4566         }
4567         while (gtk_events_pending()) gtk_main_iteration ();
4568
4569         return FALSE;
4570 }
4571
4572 static void sync_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
4573 {
4574         mainwindow_check_synchronise(mainwin, FALSE);
4575 }
4576
4577 void mainwindow_learn (MainWindow *mainwin, gboolean is_spam)
4578 {
4579         summary_mark_as_spam(mainwin->summaryview, is_spam, NULL);
4580 }
4581
4582 void mainwindow_jump_to(const gchar *target, gboolean popup)
4583 {
4584         gchar *tmp = NULL;
4585         gchar *p = NULL;
4586         FolderItem *item = NULL;
4587         gchar *msg = NULL;
4588         MainWindow *mainwin = mainwindow_get_mainwindow();
4589         gchar *from_uri = NULL;
4590         if (!target)
4591                 return;
4592                 
4593         if (!mainwin) {
4594                 g_print("not initialized\n");
4595                 return;
4596         }
4597
4598         if ((from_uri = g_filename_from_uri(target, NULL, NULL)) != NULL)
4599                 tmp = from_uri;
4600         else
4601                 tmp = g_strdup(target);
4602         
4603         if ((p = strstr(tmp, "\r")) != NULL)
4604                 *p = '\0';
4605         if ((p = strstr(tmp, "\n")) != NULL)
4606                 *p = '\0';
4607
4608         if ((item = folder_find_item_from_identifier(tmp))) {
4609                 g_print("selecting folder '%s'\n", tmp);
4610                 folderview_select(mainwin->folderview, item);
4611                 if (popup)
4612                         main_window_popup(mainwin);
4613                 g_free(tmp);
4614                 return;
4615         }
4616         
4617         msg = strrchr(tmp, G_DIR_SEPARATOR);
4618         if (msg) {
4619                 *msg++ = '\0';
4620                 if ((item = folder_find_item_from_identifier(tmp))) {
4621                         g_print("selecting folder '%s'\n", tmp);
4622                         folderview_select(mainwin->folderview, item);
4623                 } else if ((item = folder_find_item_from_real_path(tmp))) {
4624                         g_print("selecting folder '%s'\n", tmp);
4625                         folderview_select(mainwin->folderview, item);
4626                 } else {
4627                         g_print("'%s' not found\n", tmp);
4628                 }
4629                 if (item && msg && atoi(msg)) {
4630                         g_print("selecting message %d\n", atoi(msg));
4631                         summary_select_by_msgnum(mainwin->summaryview, atoi(msg));
4632                         summary_display_msg_selected(mainwin->summaryview, FALSE);
4633                         if (popup)
4634                                 main_window_popup(mainwin);
4635                         g_free(tmp);
4636                         return;
4637                 } else if (item && msg[0] == '<' && msg[strlen(msg)-1] == '>') {
4638                         MsgInfo *msginfo = NULL;
4639                         msg++;
4640                         msg[strlen(msg)-1] = '\0';
4641                         msginfo = folder_item_get_msginfo_by_msgid(item, msg);
4642                         if (msginfo) {
4643                                 g_print("selecting message %s\n", msg);
4644                                 summary_select_by_msgnum(mainwin->summaryview, msginfo->msgnum);
4645                                 summary_display_msg_selected(mainwin->summaryview, FALSE);
4646                                 if (popup)
4647                                         main_window_popup(mainwin);
4648                                 g_free(tmp);
4649                                 procmsg_msginfo_free(msginfo);
4650                                 return;
4651                         } else {
4652                                 g_print("'%s' not found\n", msg);
4653                         }
4654                 } else {
4655                         g_print("'%s' not found\n", msg);
4656                 }
4657         } else {
4658                 g_print("'%s' not found\n", tmp);
4659         }
4660         
4661         g_free(tmp);
4662 }
4663
4664 void mainwindow_exit_folder(MainWindow *mainwin) {
4665         if (prefs_common.layout_mode == SMALL_LAYOUT) {
4666                 folderview_close_opened(mainwin->folderview);
4667                 mainwin_paned_show_first(GTK_PANED(mainwin->hpaned));
4668                 gtk_widget_grab_focus(mainwin->folderview->ctree);
4669         }
4670         mainwin->in_folder = FALSE;
4671 }
4672
4673 void mainwindow_enter_folder(MainWindow *mainwin) {
4674         if (prefs_common.layout_mode == SMALL_LAYOUT) {
4675                 mainwin_paned_show_last(GTK_PANED(mainwin->hpaned));
4676         }
4677         mainwin->in_folder = TRUE;
4678 }
4679
4680 #ifdef MAEMO
4681 gboolean maemo_mainwindow_is_fullscreen(GtkWidget *widget)
4682 {
4683         gint w, h;
4684         gtk_window_get_size(GTK_WINDOW(widget), &w, &h); 
4685         return (w == 800);
4686 }
4687
4688 void maemo_window_full_screen_if_needed (GtkWindow *window)
4689 {
4690         if (maemo_mainwindow_is_fullscreen(mainwindow_get_mainwindow()->window)) {
4691                 gtk_window_fullscreen(GTK_WINDOW(window));
4692         }
4693 }
4694
4695 void maemo_connect_key_press_to_mainwindow (GtkWindow *window)
4696 {
4697         g_signal_connect(G_OBJECT(window), "key_press_event",
4698                          G_CALLBACK(mainwindow_key_pressed), mainwindow_get_mainwindow());
4699 }
4700 #endif