* all:
[modest] / src / gtk / modest-main-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtktreeviewcolumn.h>
32 #include <modest-runtime.h>
33
34 #include <widgets/modest-main-window.h>
35 #include <widgets/modest-window-priv.h>
36 #include <widgets/modest-msg-edit-window.h>
37 #include "modest-account-view-window.h"
38
39
40 #include "modest-widget-memory.h"
41 #include "modest-ui-actions.h"
42 #include "modest-main-window-ui.h"
43 #include "modest-account-mgr.h"
44 #include "modest-conf.h"
45 #include <modest-tny-msg.h>
46 #include "modest-mail-operation.h"
47 #include "modest-icon-names.h"
48
49 /* 'private'/'protected' functions */
50 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
51 static void modest_main_window_init          (ModestMainWindow *obj);
52 static void modest_main_window_finalize      (GObject *obj);
53
54 static void restore_sizes (ModestMainWindow *self);
55 static void save_sizes (ModestMainWindow *self);
56
57 static gboolean     on_header_view_button_press_event   (ModestHeaderView *header_view,
58                                                          GdkEventButton   *event,
59                                                          ModestMainWindow *self);
60
61 static gboolean     on_folder_view_button_press_event   (ModestFolderView *folder_view,
62                                                          GdkEventButton   *event,
63                                                          ModestMainWindow *self);
64
65 static gboolean     show_context_popup_menu             (ModestMainWindow *window,
66                                                          GtkTreeView      *tree_view,
67                                                          GdkEventButton   *event,
68                                                          GtkWidget        *menu);
69
70 static void         connect_signals                      (ModestMainWindow *self);
71
72
73 /* list my signals */
74 enum {
75         /* MY_SIGNAL_1, */
76         /* MY_SIGNAL_2, */
77         LAST_SIGNAL
78 };
79
80 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
81 struct _ModestMainWindowPrivate {
82
83         GtkWidget *folder_paned;
84         GtkWidget *msg_paned;
85         GtkWidget *main_paned;
86         
87         GtkWidget *online_toggle;
88         GtkWidget *folder_info_label;
89 };
90
91
92 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
93                                                 MODEST_TYPE_MAIN_WINDOW, \
94                                                 ModestMainWindowPrivate))
95
96 typedef struct _GetMsgAsyncHelper {
97         ModestMainWindowPrivate *main_window_private;
98         guint action;
99         ModestMailOperationReplyType reply_type;
100         ModestMailOperationForwardType forward_type;
101         gchar *from;
102         TnyIterator *iter;
103 } GetMsgAsyncHelper;
104
105 /* globals */
106 static GtkWindowClass *parent_class = NULL;
107
108 /* uncomment the following if you have defined any signals */
109 /* static guint signals[LAST_SIGNAL] = {0}; */
110
111 GType
112 modest_main_window_get_type (void)
113 {
114         static GType my_type = 0;
115         if (!my_type) {
116                 static const GTypeInfo my_info = {
117                         sizeof(ModestMainWindowClass),
118                         NULL,           /* base init */
119                         NULL,           /* base finalize */
120                         (GClassInitFunc) modest_main_window_class_init,
121                         NULL,           /* class finalize */
122                         NULL,           /* class data */
123                         sizeof(ModestMainWindow),
124                         1,              /* n_preallocs */
125                         (GInstanceInitFunc) modest_main_window_init,
126                         NULL
127                 };
128                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
129                                                   "ModestMainWindow",
130                                                   &my_info, 0);
131         }
132         return my_type;
133 }
134
135 static void
136 modest_main_window_class_init (ModestMainWindowClass *klass)
137 {
138         GObjectClass *gobject_class;
139         gobject_class = (GObjectClass*) klass;
140
141         parent_class            = g_type_class_peek_parent (klass);
142         gobject_class->finalize = modest_main_window_finalize;
143
144         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
145 }
146
147 static void
148 modest_main_window_init (ModestMainWindow *obj)
149 {
150         ModestMainWindowPrivate *priv;
151         TnyFolderStoreQuery     *query;
152         TnyDevice               *device;
153         GtkWidget               *icon;
154         gboolean                online;
155         
156         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
157         
158         priv->folder_paned = NULL;
159         priv->msg_paned    = NULL;
160         priv->main_paned   = NULL;      
161
162         /* folder view */
163         query = tny_folder_store_query_new ();
164         tny_folder_store_query_add_item (query, NULL,
165                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
166
167         obj->folder_view =
168                 MODEST_FOLDER_VIEW(modest_folder_view_new (modest_runtime_get_account_store(),
169                                                            query));
170         if (!obj->folder_view)
171                 g_printerr ("modest: cannot instantiate folder view\n");        
172         g_object_unref (G_OBJECT (query));
173
174         /* header view */
175         obj->header_view  =
176                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
177         if (!obj->header_view)
178                 g_printerr ("modest: cannot instantiate header view\n");
179
180         /* msg preview */
181         obj->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL));
182         if (!obj->msg_preview)
183                 g_printerr ("modest: cannot instantiate msgpreiew\n");
184
185         /* online/offline combo */
186         priv->online_toggle = gtk_toggle_button_new ();
187         device  = tny_account_store_get_device(TNY_ACCOUNT_STORE(modest_runtime_get_account_store()));;
188         online  = tny_device_is_online (device);
189         icon    = gtk_image_new_from_icon_name (online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT,
190                                                 GTK_ICON_SIZE_BUTTON);
191         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->online_toggle), online);
192         gtk_button_set_image (GTK_BUTTON(priv->online_toggle),icon);
193
194         /* label with number of items, unread items for 
195            the current folder */
196         priv->folder_info_label = gtk_label_new (NULL);
197
198         /* status bar */
199         obj->status_bar   = gtk_statusbar_new ();
200         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(obj->status_bar),
201                                            FALSE);
202
203         /* progress bar */
204         obj->progress_bar = gtk_progress_bar_new ();
205         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(obj->progress_bar), 1.0);
206         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(obj->progress_bar),
207                                         PANGO_ELLIPSIZE_END);
208 }
209
210 static void
211 modest_main_window_finalize (GObject *obj)
212 {
213         G_OBJECT_CLASS(parent_class)->finalize (obj);
214 }
215
216
217 static void
218 restore_sizes (ModestMainWindow *self)
219 {
220         ModestConf *conf;
221         ModestMainWindowPrivate *priv;
222         ModestWindowPrivate *parent_priv;
223         
224         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
225         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
226
227         conf = modest_runtime_get_conf ();
228         
229         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_paned),
230                                       "modest-folder-paned");
231         modest_widget_memory_restore (conf, G_OBJECT(priv->msg_paned),
232                                       "modest-msg-paned");
233         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
234                                       "modest-main-paned");
235         modest_widget_memory_restore (conf, G_OBJECT(self->header_view),"header-view");
236         modest_widget_memory_restore (conf,G_OBJECT(self), "modest-main-window");
237 }
238
239
240 static void
241 save_sizes (ModestMainWindow *self)
242 {
243         ModestWindowPrivate *parent_priv;
244         ModestMainWindowPrivate *priv;
245         ModestConf *conf;
246         
247         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
248         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
249
250         conf = modest_runtime_get_conf ();
251         
252         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
253         modest_widget_memory_save (conf, G_OBJECT(priv->folder_paned),
254                                    "modest-folder-paned");
255         modest_widget_memory_save (conf, G_OBJECT(priv->msg_paned),
256                                    "modest-msg-paned");
257         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
258                                    "modest-main-paned");
259         modest_widget_memory_save (conf, G_OBJECT(self->header_view), "header-view");
260 }
261
262
263 static void
264 on_connection_changed (TnyDevice *device, gboolean online, ModestMainWindow *self)
265 {
266         GtkWidget *icon;
267         const gchar *icon_name;
268         ModestMainWindowPrivate *priv;
269         
270         g_return_if_fail (device);
271         g_return_if_fail (self);
272
273         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
274
275         icon_name = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
276         icon      = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
277
278         /* Block handlers in order to avoid unnecessary calls */
279         //g_signal_handler_block (G_OBJECT (priv->online_toggle), priv->toggle_button_signal);
280         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->online_toggle), online);
281         //g_signal_handler_unblock (G_OBJECT (online_toggle), priv->toggle_button_signal);
282
283         gtk_button_set_image (GTK_BUTTON(priv->online_toggle), icon);
284         //statusbar_push (widget_factory, 0, online ? _("Modest went online") : _("Modest went offline"));
285         
286         /* If Modest has became online and the header view has a
287            header selected then show it */
288         /* FIXME: there is a race condition if some account needs to
289            ask the user for a password */
290
291 /*      if (online) { */
292 /*              GtkTreeSelection *selected; */
293
294 /*              selected = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view)); */
295 /*              _modest_header_view_change_selection (selected, header_view); */
296 /*      } */
297 }
298
299 void
300 on_online_toggle_toggled (GtkToggleButton *toggle, ModestMainWindow *self)
301 {
302         gboolean online;
303         TnyDevice *device;
304         ModestMainWindowPrivate *priv;
305
306         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
307
308         device = tny_account_store_get_device
309                 (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()));
310
311         online  = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->online_toggle));
312
313         if (online)
314                 tny_device_force_online (device);
315         else
316                 tny_device_force_offline (device);
317
318         g_object_unref (G_OBJECT (device));
319 }
320
321 static gboolean
322 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
323 {
324         save_sizes (self);
325         return FALSE;
326 }
327
328 static void
329 on_destroy (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
330 {
331         gtk_main_quit();
332 }
333
334
335
336 static void
337 connect_signals (ModestMainWindow *self)
338 {       
339         ModestWindowPrivate *parent_priv;
340         ModestMainWindowPrivate *priv;
341         TnyDevice *device;
342         ModestTnyAccountStore *account_store;
343         
344         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
345         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
346
347         account_store = modest_runtime_get_account_store ();
348         device        = tny_account_store_get_device(TNY_ACCOUNT_STORE(account_store));
349         
350         /* folder view */
351         g_signal_connect (G_OBJECT(self->folder_view), "folder_selection_changed",
352                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
353         g_signal_connect (G_OBJECT(self->folder_view), "folder_moved",
354                           G_CALLBACK(modest_ui_actions_on_folder_moved), NULL);
355         g_signal_connect (G_OBJECT(self->folder_view), "button-press-event",
356                           G_CALLBACK (on_folder_view_button_press_event),self);
357         g_signal_connect (self->folder_view,"popup-menu",
358                           G_CALLBACK (on_folder_view_button_press_event),self);
359
360         /* header view */
361         g_signal_connect (G_OBJECT(self->header_view), "status_update",
362                           G_CALLBACK(modest_ui_actions_on_header_status_update), self);
363         g_signal_connect (G_OBJECT(self->header_view), "header_selected",
364                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
365         g_signal_connect (G_OBJECT(self->header_view), "header_activated",
366                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
367         g_signal_connect (G_OBJECT(self->header_view), "item_not_found",
368                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
369         g_signal_connect (G_OBJECT(self->header_view), "button-press-event",
370                           G_CALLBACK (on_header_view_button_press_event), self);
371         g_signal_connect (G_OBJECT(self->header_view),"popup-menu",
372                           G_CALLBACK (on_header_view_button_press_event), self);
373                 
374         /* msg preview */
375         g_signal_connect (G_OBJECT(self->msg_preview), "link_clicked",
376                           G_CALLBACK(modest_ui_actions_on_msg_link_clicked), self);
377         g_signal_connect (G_OBJECT(self->msg_preview), "link_hover",
378                           G_CALLBACK(modest_ui_actions_on_msg_link_hover), self);
379         g_signal_connect (G_OBJECT(self->msg_preview), "attachment_clicked",
380                           G_CALLBACK(modest_ui_actions_on_msg_attachment_clicked), self);
381
382         /* Account store */
383         g_signal_connect (G_OBJECT (modest_runtime_get_account_store()), "accounts_reloaded",
384                           G_CALLBACK (modest_ui_actions_on_accounts_reloaded), self);
385         
386         /* Device */
387         g_signal_connect (G_OBJECT(device), "connection_changed",
388                           G_CALLBACK(on_connection_changed), self);
389         g_signal_connect (G_OBJECT(priv->online_toggle), "toggled",
390                           G_CALLBACK(on_online_toggle_toggled), self);
391         
392         /* window */
393         g_signal_connect (G_OBJECT(self), "destroy", G_CALLBACK(on_destroy), NULL);
394         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
395 }
396
397
398 static GtkWidget*
399 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
400 {
401         GtkWidget *win;
402
403         win = gtk_scrolled_window_new (NULL, NULL);
404         gtk_scrolled_window_set_policy
405                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
406                  GTK_POLICY_AUTOMATIC);
407         
408         if (needs_viewport)
409                 gtk_scrolled_window_add_with_viewport
410                         (GTK_SCROLLED_WINDOW(win), widget);
411         else
412                 gtk_container_add (GTK_CONTAINER(win),
413                                    widget);
414
415         return win;
416 }
417
418
419
420
421 ModestWindow *
422 modest_main_window_new (void)
423 {
424         GObject *obj;
425         ModestMainWindow *self;
426         ModestMainWindowPrivate *priv;
427         ModestWindowPrivate *parent_priv;
428         GtkWidget *main_vbox;
429         GtkWidget *status_hbox;
430         GtkWidget *header_win, *folder_win;
431         GtkActionGroup *action_group;
432         GError *error = NULL;
433                 
434         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
435         self = MODEST_MAIN_WINDOW(obj);
436         
437         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
438         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
439         
440         /* ***************** */
441         parent_priv->ui_manager = gtk_ui_manager_new();
442         action_group = gtk_action_group_new ("ModestMainWindowActions");
443         
444         /* Add common actions */
445         gtk_action_group_add_actions (action_group,
446                                       modest_action_entries,
447                                       G_N_ELEMENTS (modest_action_entries),
448                                       obj);
449
450         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
451         g_object_unref (action_group);
452
453         /* Load the UI definition */
454         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
455                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
456         if (error != NULL) {
457                 g_printerr ("modest: could not merge modest-main-window-ui.xml: %s", error->message);
458                 g_error_free (error);
459                 error = NULL;
460         }
461         /* *************** */
462
463         /* Add accelerators */
464         gtk_window_add_accel_group (GTK_WINDOW (obj), 
465                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
466
467         /* Toolbar / Menubar */
468         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
469         parent_priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
470
471         gtk_toolbar_set_tooltips (GTK_TOOLBAR (parent_priv->toolbar), TRUE);    
472         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(self->folder_view), FALSE);
473         header_win = wrapped_in_scrolled_window (GTK_WIDGET(self->header_view), FALSE);                    
474
475         /* paned */
476         priv->folder_paned = gtk_vpaned_new ();
477         priv->msg_paned = gtk_vpaned_new ();
478         priv->main_paned = gtk_hpaned_new ();
479         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
480         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->msg_paned);
481         gtk_paned_add1 (GTK_PANED(priv->msg_paned), header_win);
482         gtk_paned_add2 (GTK_PANED(priv->msg_paned), GTK_WIDGET(self->msg_preview));
483
484         gtk_widget_show (GTK_WIDGET(self->header_view));
485
486         /* status bar / progress */
487         status_hbox = gtk_hbox_new (FALSE, 0);
488         gtk_box_pack_start (GTK_BOX(status_hbox), priv->folder_info_label, FALSE,FALSE, 6);
489         gtk_box_pack_start (GTK_BOX(status_hbox), self->status_bar, TRUE, TRUE, 0);
490         gtk_box_pack_start (GTK_BOX(status_hbox), self->progress_bar,FALSE, FALSE, 0);
491         gtk_box_pack_start (GTK_BOX(status_hbox), priv->online_toggle,FALSE, FALSE, 0);
492
493         /* putting it all together... */
494         main_vbox = gtk_vbox_new (FALSE, 6);
495         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->menubar, FALSE, FALSE, 0);
496         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->toolbar, FALSE, FALSE, 0);
497         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
498         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
499         
500         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
501         restore_sizes (MODEST_MAIN_WINDOW(obj));        
502
503         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
504         gtk_window_set_icon_from_file  (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);        
505         gtk_widget_show_all (main_vbox);
506         
507         /* Init toggle in correct state */
508         //modest_ui_actions_on_connection_changed (device, tny_device_is_online (device), self);
509
510         connect_signals (MODEST_MAIN_WINDOW(obj));
511         return (ModestWindow *) obj;
512 }
513
514 static gboolean 
515 on_header_view_button_press_event (ModestHeaderView *header_view,
516                                    GdkEventButton   *event,
517                                    ModestMainWindow *self)
518 {
519         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
520                 GtkWidget *menu;
521                 ModestWindowPrivate *parent_priv;
522         
523                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
524                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/HeaderViewContextMenu");
525
526                 return show_context_popup_menu (self,
527                                                 GTK_TREE_VIEW (header_view), 
528                                                 event, 
529                                                 menu);
530         }
531
532         return FALSE;
533 }
534
535 static gboolean 
536 on_folder_view_button_press_event (ModestFolderView *folder_view,
537                                    GdkEventButton   *event,
538                                    ModestMainWindow *self)
539 {
540         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
541                 GtkWidget *menu;
542                 ModestWindowPrivate *parent_priv;
543         
544                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
545                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewContextMenu");
546
547                 return show_context_popup_menu (self,
548                                                 GTK_TREE_VIEW (folder_view), 
549                                                 event, 
550                                                 menu);
551         }
552
553         return FALSE;
554 }
555
556 static gboolean 
557 show_context_popup_menu (ModestMainWindow *window,
558                          GtkTreeView *tree_view,
559                          GdkEventButton   *event,                        
560                          GtkWidget *menu)
561 {
562         g_return_val_if_fail (menu, FALSE);
563
564         if (event != NULL) {
565                 /* Ensure that the header is selected */
566                 GtkTreeSelection *selection;
567
568                 selection = gtk_tree_view_get_selection (tree_view);
569         
570                 if (gtk_tree_selection_count_selected_rows (selection) <= 1) {
571                         GtkTreePath *path;
572                 
573                         /* Get tree path for row that was clicked */
574                         if (gtk_tree_view_get_path_at_pos (tree_view,
575                                                            (gint) event->x, 
576                                                            (gint) event->y,
577                                                            &path, 
578                                                            NULL, NULL, NULL)) {
579                                 gtk_tree_selection_unselect_all (selection);
580                                 gtk_tree_selection_select_path (selection, path);
581                                 gtk_tree_path_free (path);
582                         }
583                 }
584
585                 /* Show popup */
586                 if (gtk_tree_selection_count_selected_rows(selection) == 1)
587                         gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
588                                         NULL, NULL,
589                                         event->button, event->time);
590         }
591         return TRUE;
592 }