summaryrefslogtreecommitdiff
path: root/src/caja-bookmarks-window.c
blob: 4abff8409ffcbe1da0eb0e58a3cc510986584289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
 * Caja
 *
 * Copyright (C) 1999, 2000 Eazel, Inc.
 *
 * Caja is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Caja is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors: John Sullivan <sullivan@eazel.com>
 */

/* caja-bookmarks-window.c - implementation of bookmark-editing window.
 */

#include <config.h>
#include <cairo-gobject.h>

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include <eel/eel-gtk-extensions.h>
#include <eel/eel-mate-extensions.h>

#include <libcaja-private/caja-global-preferences.h>

#include "caja-bookmarks-window.h"
#include "caja-window.h"
#include "caja-navigation-window.h"
#include "caja-spatial-window.h"

/* Static variables to keep track of window state. If there were
 * more than one bookmark-editing window, these would be struct or
 * class fields.
 */
static int		     bookmark_list_changed_signal_id;
static CajaBookmarkList *bookmarks = NULL;
static GtkTreeView	    *bookmark_list_widget = NULL; /* awkward name to distinguish from CajaBookmarkList */
static GtkListStore	    *bookmark_list_store = NULL;
static GtkListStore	    *bookmark_empty_list_store = NULL;
static GtkTreeSelection     *bookmark_selection = NULL;
static int                   selection_changed_id = 0;
static GtkWidget	    *name_field = NULL;
static int		     name_field_changed_signal_id;
static GtkWidget	    *remove_button = NULL;
static GtkWidget            *jump_button = NULL;
static gboolean		     text_changed = FALSE;
static gboolean		     name_text_changed = FALSE;
static GtkWidget	    *uri_field = NULL;
static int		     uri_field_changed_signal_id;
static int		     row_changed_signal_id;
static int		     row_deleted_signal_id;
static int                   row_activated_signal_id;
static int                   button_pressed_signal_id;
static int                   key_pressed_signal_id;
static int                   jump_button_signal_id;
static CajaApplication  *application;
static gboolean              parent_is_browser_window;

/* forward declarations */
static guint    get_selected_row                            (void);
static gboolean get_selection_exists                        (void);
static void     name_or_uri_field_activate                  (CajaEntry        *entry);
static void     caja_bookmarks_window_restore_geometry  (GtkWidget            *window);
static void     on_bookmark_list_changed                    (CajaBookmarkList *list,
        gpointer              user_data);
static void     on_name_field_changed                       (GtkEditable          *editable,
        gpointer              user_data);
static void     on_remove_button_clicked                    (GtkButton            *button,
        gpointer              user_data);
static void     on_jump_button_clicked                      (GtkButton            *button,
        gpointer              user_data);
static void	on_row_changed				    (GtkListStore	  *store,
        GtkTreePath	  *path,
        GtkTreeIter	  *iter,
        gpointer		   user_data);
static void	on_row_deleted				    (GtkListStore	  *store,
        GtkTreePath	  *path,
        gpointer		   user_data);
static void	on_row_activated			    (GtkTreeView	  *view,
        GtkTreePath	  *path,
        GtkTreeViewColumn    *column,
        gpointer		   user_data);
static gboolean	on_button_pressed                           (GtkTreeView	  *view,
        GdkEventButton       *event,
        gpointer		   user_data);
static gboolean	on_key_pressed                              (GtkTreeView	  *view,
        GdkEventKey          *event,
        gpointer		   user_data);
static void     on_selection_changed                        (GtkTreeSelection     *treeselection,
        gpointer              user_data);

static gboolean on_text_field_focus_out_event               (GtkWidget            *widget,
        GdkEventFocus        *event,
        gpointer              user_data);
static void     on_uri_field_changed                        (GtkEditable          *editable,
        gpointer              user_data);
static gboolean on_window_delete_event                      (GtkWidget            *widget,
        GdkEvent             *event,
        gpointer              user_data);
static void     on_window_hide_event                        (GtkWidget            *widget,
        gpointer              user_data);
static void     on_window_destroy_event                     (GtkWidget            *widget,
        gpointer              user_data);
static void     repopulate                                  (void);
static void     set_up_close_accelerator                    (GtkWidget            *window);
static void	open_selected_bookmark 			    (gpointer   user_data, GdkScreen *screen);
static void	update_bookmark_from_text		    (void);

/* We store a pointer to the bookmark in a column so when an item is moved
   with DnD we know which item it is. However we have to be careful to keep
   this in sync with the actual bookmark. Note that
   caja_bookmark_list_insert_item() makes a copy of the bookmark, so we
   have to fetch the new copy and update our pointer. */
#define BOOKMARK_LIST_COLUMN_ICON		0
#define BOOKMARK_LIST_COLUMN_NAME		1
#define BOOKMARK_LIST_COLUMN_BOOKMARK		2
#define BOOKMARK_LIST_COLUMN_STYLE		3
#define BOOKMARK_LIST_COLUMN_COUNT		4

/* layout constants */

/* Keep window from shrinking down ridiculously small; numbers are somewhat arbitrary */
#define BOOKMARKS_WINDOW_MIN_WIDTH	300
#define BOOKMARKS_WINDOW_MIN_HEIGHT	100

/* Larger size initially; user can stretch or shrink (but not shrink below min) */
#define BOOKMARKS_WINDOW_INITIAL_WIDTH	500
#define BOOKMARKS_WINDOW_INITIAL_HEIGHT	200

static void
caja_bookmarks_window_response_callback (GtkDialog *dialog,
        int response_id,
        gpointer callback_data)
{
    if (response_id == GTK_RESPONSE_HELP)
    {
        GError *error = NULL;

        gtk_show_uri_on_window (GTK_WINDOW (dialog),
                                "help:mate-user-guide/goscaja-36",
                                gtk_get_current_event_time (), &error);

        if (error)
        {
            GtkWidget *err_dialog;
            err_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
                                                 GTK_MESSAGE_ERROR,
                                                 GTK_BUTTONS_OK,
                                                 _("There was an error displaying help: \n%s"),
                                                 error->message);

            g_signal_connect (G_OBJECT (err_dialog),
                              "response", G_CALLBACK (gtk_widget_destroy),
                              NULL);
            gtk_window_set_resizable (GTK_WINDOW (err_dialog), FALSE);
            gtk_widget_show (err_dialog);
            g_error_free (error);
        }
    }
    else if (response_id == GTK_RESPONSE_CLOSE)
    {
        gtk_widget_hide (GTK_WIDGET (dialog));
    }
}

static GtkListStore *
create_bookmark_store (void)
{
    return gtk_list_store_new (BOOKMARK_LIST_COLUMN_COUNT,
                               CAIRO_GOBJECT_TYPE_SURFACE,
                               G_TYPE_STRING,
                               G_TYPE_OBJECT,
                               PANGO_TYPE_STYLE);
}

static void
setup_empty_list (void)
{
    GtkTreeIter iter;

    bookmark_empty_list_store = create_bookmark_store ();
    gtk_list_store_append (bookmark_empty_list_store, &iter);

    gtk_list_store_set (bookmark_empty_list_store, &iter,
                        BOOKMARK_LIST_COLUMN_NAME, _("No bookmarks defined"),
                        BOOKMARK_LIST_COLUMN_STYLE, PANGO_STYLE_ITALIC,
                        -1);
}

static void
bookmarks_set_empty (gboolean empty)
{
    GtkTreeIter iter;

    if (empty)
    {
        gtk_tree_view_set_model (bookmark_list_widget,
                                 GTK_TREE_MODEL (bookmark_empty_list_store));
        gtk_widget_set_sensitive (GTK_WIDGET (bookmark_list_widget), FALSE);
    }
    else
    {
        gtk_tree_view_set_model (bookmark_list_widget,
                                 GTK_TREE_MODEL (bookmark_list_store));
        gtk_widget_set_sensitive (GTK_WIDGET (bookmark_list_widget), TRUE);

        if (caja_bookmark_list_length (bookmarks) > 0 &&
                !get_selection_exists ())
        {
            gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (bookmark_list_store),
                                           &iter, NULL, 0);
            gtk_tree_selection_select_iter (bookmark_selection, &iter);
        }
    }

    on_selection_changed (bookmark_selection, NULL);
}

static void
edit_bookmarks_dialog_reset_signals (gpointer data,
                                     GObject *obj)
{
    g_signal_handler_disconnect (jump_button,
                                 jump_button_signal_id);
    g_signal_handler_disconnect (bookmark_list_widget,
                                 row_activated_signal_id);
    jump_button_signal_id =
        g_signal_connect (jump_button, "clicked",
                          G_CALLBACK (on_jump_button_clicked), NULL);
    row_activated_signal_id =
        g_signal_connect (bookmark_list_widget, "row_activated",
                          G_CALLBACK (on_row_activated), NULL);
}

/**
 * create_bookmarks_window:
 *
 * Create a new bookmark-editing window.
 * @list: The CajaBookmarkList that this window will edit.
 *
 * Return value: A pointer to the new window.
 **/
GtkWindow *
create_bookmarks_window (CajaBookmarkList *list, CajaWindow *window_source)
{
    GtkWidget         *window;
    GtkTreeViewColumn *col;
    GtkCellRenderer   *rend;
    GtkBuilder        *builder;

    bookmarks = list;

    builder = gtk_builder_new ();
    if (!gtk_builder_add_from_file (builder,
                                    UIDIR  "/caja-bookmarks-window.ui",
                                    NULL))
    {
        return NULL;
    }

    window = (GtkWidget *)gtk_builder_get_object (builder, "bookmarks_dialog");
    bookmark_list_widget = (GtkTreeView *)gtk_builder_get_object (builder, "bookmark_tree_view");
    remove_button = (GtkWidget *)gtk_builder_get_object (builder, "bookmark_delete_button");
    jump_button = (GtkWidget *)gtk_builder_get_object (builder, "bookmark_jump_button");

    application = window_source->application;

    if (CAJA_IS_NAVIGATION_WINDOW (window_source))
    {
        parent_is_browser_window = TRUE;
    }
    else
    {
        parent_is_browser_window = FALSE;
    }

    set_up_close_accelerator (window);

    caja_bookmarks_window_restore_geometry (window);

    g_object_weak_ref (G_OBJECT (window_source), edit_bookmarks_dialog_reset_signals,
                       window_source);

    bookmark_list_widget = GTK_TREE_VIEW (gtk_builder_get_object (builder, "bookmark_tree_view"));

    rend = gtk_cell_renderer_pixbuf_new ();
    col = gtk_tree_view_column_new_with_attributes ("Icon",
            rend,
            "surface",
            BOOKMARK_LIST_COLUMN_ICON,
            NULL);
    gtk_tree_view_append_column (bookmark_list_widget,
                                 GTK_TREE_VIEW_COLUMN (col));
    gtk_tree_view_column_set_fixed_width (GTK_TREE_VIEW_COLUMN (col),
                                          CAJA_ICON_SIZE_SMALLER);

    rend = gtk_cell_renderer_text_new ();
    g_object_set (rend,
                  "ellipsize", PANGO_ELLIPSIZE_END,
                  "ellipsize-set", TRUE,
                  NULL);

    col = gtk_tree_view_column_new_with_attributes ("Icon",
            rend,
            "text",
            BOOKMARK_LIST_COLUMN_NAME,
            "style",
            BOOKMARK_LIST_COLUMN_STYLE,
            NULL);
    gtk_tree_view_append_column (bookmark_list_widget,
                                 GTK_TREE_VIEW_COLUMN (col));

    bookmark_list_store = create_bookmark_store ();
    setup_empty_list ();
    gtk_tree_view_set_model (bookmark_list_widget,
                             GTK_TREE_MODEL (bookmark_empty_list_store));

    bookmark_selection =
        GTK_TREE_SELECTION (gtk_tree_view_get_selection (bookmark_list_widget));

    name_field = caja_entry_new ();

    gtk_widget_show (name_field);
    gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_name_placeholder")),
                        name_field, TRUE, TRUE, 0);

    gtk_label_set_mnemonic_widget (
        GTK_LABEL (gtk_builder_get_object (builder, "bookmark_name_label")),
        name_field);

    uri_field = caja_entry_new ();
    gtk_widget_show (uri_field);
    gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "bookmark_location_placeholder")),
                        uri_field, TRUE, TRUE, 0);

    gtk_label_set_mnemonic_widget (
        GTK_LABEL (gtk_builder_get_object (builder, "bookmark_location_label")),
        uri_field);

    bookmark_list_changed_signal_id =
        g_signal_connect (bookmarks, "contents_changed",
                          G_CALLBACK (on_bookmark_list_changed), NULL);
    row_changed_signal_id =
        g_signal_connect (bookmark_list_store, "row_changed",
                          G_CALLBACK (on_row_changed), NULL);
    row_deleted_signal_id =
        g_signal_connect (bookmark_list_store, "row_deleted",
                          G_CALLBACK (on_row_deleted), NULL);
    row_activated_signal_id =
        g_signal_connect (bookmark_list_widget, "row_activated",
                          G_CALLBACK (on_row_activated), window_source);
    button_pressed_signal_id =
        g_signal_connect (bookmark_list_widget, "button_press_event",
                          G_CALLBACK (on_button_pressed), NULL);
    key_pressed_signal_id =
        g_signal_connect (bookmark_list_widget, "key_press_event",
                          G_CALLBACK (on_key_pressed), NULL);
    selection_changed_id =
        g_signal_connect (bookmark_selection, "changed",
                          G_CALLBACK (on_selection_changed), NULL);

    g_signal_connect (window, "delete_event",
                      G_CALLBACK (on_window_delete_event), NULL);
    g_signal_connect (window, "hide",
                      G_CALLBACK (on_window_hide_event), NULL);
    g_signal_connect (window, "destroy",
                      G_CALLBACK (on_window_destroy_event), NULL);
    g_signal_connect (window, "response",
                      G_CALLBACK (caja_bookmarks_window_response_callback), NULL);

    name_field_changed_signal_id =
        g_signal_connect (name_field, "changed",
                          G_CALLBACK (on_name_field_changed), NULL);

    g_signal_connect (name_field, "focus_out_event",
                      G_CALLBACK (on_text_field_focus_out_event), NULL);
    g_signal_connect (name_field, "activate",
                      G_CALLBACK (name_or_uri_field_activate), NULL);

    uri_field_changed_signal_id =
        g_signal_connect (uri_field, "changed",
                          G_CALLBACK (on_uri_field_changed), NULL);

    g_signal_connect (uri_field, "focus_out_event",
                      G_CALLBACK (on_text_field_focus_out_event), NULL);
    g_signal_connect (uri_field, "activate",
                      G_CALLBACK (name_or_uri_field_activate), NULL);
    g_signal_connect (remove_button, "clicked",
                      G_CALLBACK (on_remove_button_clicked), NULL);
    jump_button_signal_id =
        g_signal_connect (jump_button, "clicked",
                          G_CALLBACK (on_jump_button_clicked), window_source);

    gtk_tree_selection_set_mode (bookmark_selection, GTK_SELECTION_BROWSE);

    /* Fill in list widget with bookmarks, must be after signals are wired up. */
    repopulate();

    g_object_unref (builder);

    return GTK_WINDOW (window);
}

void
edit_bookmarks_dialog_set_signals (CajaWindow *window)
{

    g_signal_handler_disconnect (jump_button,
                                 jump_button_signal_id);
    g_signal_handler_disconnect (bookmark_list_widget,
                                 row_activated_signal_id);

    jump_button_signal_id =
        g_signal_connect (jump_button, "clicked",
                          G_CALLBACK (on_jump_button_clicked), window);
    row_activated_signal_id =
        g_signal_connect (bookmark_list_widget, "row_activated",
                          G_CALLBACK (on_row_activated), window);

    g_object_weak_ref (G_OBJECT (window), edit_bookmarks_dialog_reset_signals,
                       window);
}

static CajaBookmark *
get_selected_bookmark (void)
{
    g_return_val_if_fail(CAJA_IS_BOOKMARK_LIST(bookmarks), NULL);

    if (!get_selection_exists())
        return NULL;

    if (caja_bookmark_list_length (bookmarks) < 1)
        return NULL;

    return caja_bookmark_list_item_at(bookmarks, get_selected_row ());
}

static guint
get_selected_row (void)
{
    GtkTreeIter       iter;
    GtkTreePath      *path;
    GtkTreeModel     *model;
    gint		 *indices, row;

    g_assert (get_selection_exists());

    model = GTK_TREE_MODEL (bookmark_list_store);
    gtk_tree_selection_get_selected (bookmark_selection,
                                     &model,
                                     &iter);

    path = gtk_tree_model_get_path (model, &iter);
    indices = gtk_tree_path_get_indices (path);
    row = indices[0];
    gtk_tree_path_free (path);
    return row;
}

static gboolean
get_selection_exists (void)
{
    return gtk_tree_selection_get_selected (bookmark_selection, NULL, NULL);
}

static void
caja_bookmarks_window_restore_geometry (GtkWidget *window)
{
    const char *window_geometry;

    g_return_if_fail (GTK_IS_WINDOW (window));
    g_return_if_fail (CAJA_IS_BOOKMARK_LIST (bookmarks));

    window_geometry = caja_bookmark_list_get_window_geometry (bookmarks);

    if (window_geometry != NULL)
    {
        eel_gtk_window_set_initial_geometry_from_string
        (GTK_WINDOW (window), window_geometry,
         BOOKMARKS_WINDOW_MIN_WIDTH, BOOKMARKS_WINDOW_MIN_HEIGHT, FALSE);

    }
    else
    {
        /* use default since there was no stored geometry */
        gtk_window_set_default_size (GTK_WINDOW (window),
                                     BOOKMARKS_WINDOW_INITIAL_WIDTH,
                                     BOOKMARKS_WINDOW_INITIAL_HEIGHT);

        /* Let window manager handle default position if no position stored */
    }
}

/**
 * caja_bookmarks_window_save_geometry:
 *
 * Save window size & position to disk.
 * @window: The bookmarks window whose geometry should be saved.
 **/
void
caja_bookmarks_window_save_geometry (GtkWindow *window)
{
    g_return_if_fail (GTK_IS_WINDOW (window));
    g_return_if_fail (CAJA_IS_BOOKMARK_LIST (bookmarks));

    /* Don't bother if window is already closed */
    if (gtk_widget_get_visible (GTK_WIDGET (window)))
    {
        char *geometry_string;

        geometry_string = eel_gtk_window_get_geometry_string (window);

        caja_bookmark_list_set_window_geometry (bookmarks, geometry_string);
        g_free (geometry_string);
    }
}

static void
on_bookmark_list_changed (CajaBookmarkList *bookmarks, gpointer data)
{
    g_return_if_fail (CAJA_IS_BOOKMARK_LIST (bookmarks));

    /* maybe add logic here or in repopulate to save/restore selection */
    repopulate ();
}

static void
on_name_field_changed (GtkEditable *editable,
                       gpointer     user_data)
{
    GtkTreeIter   iter;
    g_return_if_fail(GTK_IS_TREE_VIEW(bookmark_list_widget));
    g_return_if_fail(GTK_IS_ENTRY(name_field));

    if (!get_selection_exists())
        return;

    /* Update text displayed in list instantly. Also remember that
     * user has changed text so we update real bookmark later.
     */
    gtk_tree_selection_get_selected (bookmark_selection,
                                     NULL,
                                     &iter);

    gtk_list_store_set (bookmark_list_store,
                        &iter, BOOKMARK_LIST_COLUMN_NAME,
                        gtk_entry_get_text (GTK_ENTRY (name_field)),
                        -1);
    text_changed = TRUE;
    name_text_changed = TRUE;
}

static void
open_selected_bookmark (gpointer user_data, GdkScreen *screen)
{
    CajaBookmark *selected;
    CajaWindow *window;
    GFile *location;

    selected = get_selected_bookmark ();

    if (!selected)
    {
        return;
    }

    location = caja_bookmark_get_location (selected);
    if (location == NULL)
    {
        return;
    }

    if (CAJA_IS_NAVIGATION_WINDOW (user_data))
    {
        window = user_data;
    }
    else if (CAJA_IS_SPATIAL_WINDOW (user_data))
    {
        window = caja_application_get_spatial_window (application,
                                                      NULL,
                                                      NULL,
                                                      location,
                                                      screen,
                                                      NULL);
    } else { /* window that opened bookmarks window has been closed */
        if (parent_is_browser_window || g_settings_get_boolean (caja_preferences, CAJA_PREFERENCES_ALWAYS_USE_BROWSER)) {
            window = caja_application_create_navigation_window (application,
                     screen);
        }
        else
        {
            window = caja_application_get_spatial_window (application,
                                                          NULL,
                                                          NULL,
                                                          location,
                                                          screen,
                                                          NULL);
        }
    }

    caja_window_go_to (window, location);

    g_object_unref (location);
}

static void
on_jump_button_clicked (GtkButton *button,
                        gpointer   user_data)
{
    GdkScreen *screen;

    screen = gtk_widget_get_screen (GTK_WIDGET (button));
    open_selected_bookmark (user_data, screen);
}

static void
bookmarks_delete_bookmark (void)
{
    GtkTreeIter iter;
    GtkTreePath *path;
    gint *indices, row, rows;

    g_assert (GTK_IS_TREE_VIEW (bookmark_list_widget));

    if (!gtk_tree_selection_get_selected (bookmark_selection, NULL, &iter))
        return;

    /* Remove the selected item from the list store. on_row_deleted() will
       remove it from the bookmark list. */
    path = gtk_tree_model_get_path (GTK_TREE_MODEL (bookmark_list_store),
                                    &iter);
    indices = gtk_tree_path_get_indices (path);
    row = indices[0];
    gtk_tree_path_free (path);

    gtk_list_store_remove (bookmark_list_store, &iter);

    /* Try to select the same row, or the last one in the list. */
    rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (bookmark_list_store), NULL);
    if (row >= rows)
        row = rows - 1;

    if (row < 0)
    {
        bookmarks_set_empty (TRUE);
    }
    else
    {
        gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (bookmark_list_store),
                                       &iter, NULL, row);
        gtk_tree_selection_select_iter (bookmark_selection, &iter);
    }
}

static void
on_remove_button_clicked (GtkButton *button,
                          gpointer   user_data)
{
    bookmarks_delete_bookmark ();
}

/* This is a bit of a kludge to get DnD to work. We check if the row in the
   GtkListStore matches the one in the bookmark list. If it doesn't, we assume
   the bookmark has just been dragged here and we insert it into the bookmark
   list. */
static void
on_row_changed (GtkListStore *store,
                GtkTreePath *path,
                GtkTreeIter *iter,
                gpointer user_data)
{
    CajaBookmark *bookmark = NULL;
    gint *indices, row;
    gboolean insert_bookmark = TRUE;

    store = bookmark_list_store;

    indices = gtk_tree_path_get_indices (path);
    row = indices[0];
    gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
                        BOOKMARK_LIST_COLUMN_BOOKMARK, &bookmark,
                        -1);

    /* If the bookmark in the list doesn't match the changed one, it must
       have been dragged here, so we insert it into the list. */
    if (row < (gint) caja_bookmark_list_length (bookmarks))
    {
        CajaBookmark *bookmark_in_list;

        bookmark_in_list = caja_bookmark_list_item_at (bookmarks,
                           row);

        if (bookmark_in_list == bookmark)
            insert_bookmark = FALSE;
    }

    if (insert_bookmark)
    {
        g_signal_handler_block (bookmarks,
                                bookmark_list_changed_signal_id);
        caja_bookmark_list_insert_item (bookmarks, bookmark, row);
        g_signal_handler_unblock (bookmarks,
                                  bookmark_list_changed_signal_id);

        /* The bookmark will be copied when inserted into the list, so
           we have to update the pointer in the list store. */
        bookmark = caja_bookmark_list_item_at (bookmarks, row);
        g_signal_handler_block (store, row_changed_signal_id);
        gtk_list_store_set (store, iter,
                            BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark,
                            -1);
        g_signal_handler_unblock (store, row_changed_signal_id);
    }
}

/* The update_bookmark_from_text() calls in the
 * on_button_pressed() and on_key_pressed() handlers
 * of the tree view are a hack.
 *
 * The purpose is to track selection changes to the view
 * and write the text fields back before the selection
 * actually changed.
 *
 * Note that the focus-out event of the text entries is emitted
 * after the selection changed, else this would not not be neccessary.
 */

static gboolean
on_button_pressed (GtkTreeView *view,
                   GdkEventButton *event,
                   gpointer user_data)
{
    update_bookmark_from_text ();

    return FALSE;
}

static gboolean
on_key_pressed (GtkTreeView *view,
                GdkEventKey *event,
                gpointer user_data)
{
    if (event->keyval == GDK_KEY_Delete || event->keyval == GDK_KEY_KP_Delete)
    {
        bookmarks_delete_bookmark ();
        return TRUE;
    }

    update_bookmark_from_text ();

    return FALSE;
}

static void
on_row_activated (GtkTreeView       *view,
                  GtkTreePath       *path,
                  GtkTreeViewColumn *column,
                  gpointer           user_data)
{
    GdkScreen *screen;

    screen = gtk_widget_get_screen (GTK_WIDGET (view));
    open_selected_bookmark (user_data, screen);
}

static void
on_row_deleted (GtkListStore *store,
                GtkTreePath *path,
                gpointer user_data)
{
    gint *indices, row;

    indices = gtk_tree_path_get_indices (path);
    row = indices[0];

    g_signal_handler_block (bookmarks, bookmark_list_changed_signal_id);
    caja_bookmark_list_delete_item_at (bookmarks, row);
    g_signal_handler_unblock (bookmarks, bookmark_list_changed_signal_id);
}

static void
on_selection_changed (GtkTreeSelection *treeselection,
                      gpointer user_data)
{
    CajaBookmark *selected;
    char *name = NULL, *entry_text = NULL;

    g_assert (GTK_IS_ENTRY (name_field));
    g_assert (GTK_IS_ENTRY (uri_field));

    selected = get_selected_bookmark ();

    if (selected)
    {
        GFile *location;

        name = caja_bookmark_get_name (selected);
        location = caja_bookmark_get_location (selected);
        entry_text = g_file_get_parse_name (location);

        g_object_unref (location);
    }

    /* Set the sensitivity of widgets that require a selection */
    gtk_widget_set_sensitive (remove_button, selected != NULL);
    gtk_widget_set_sensitive (jump_button, selected != NULL);
    gtk_widget_set_sensitive (name_field, selected != NULL);
    gtk_widget_set_sensitive (uri_field, selected != NULL);

    g_signal_handler_block (name_field, name_field_changed_signal_id);
    caja_entry_set_text (CAJA_ENTRY (name_field),
                         name ? name : "");
    g_signal_handler_unblock (name_field, name_field_changed_signal_id);

    g_signal_handler_block (uri_field, uri_field_changed_signal_id);
    caja_entry_set_text (CAJA_ENTRY (uri_field),
                         entry_text ? entry_text : "");
    g_signal_handler_unblock (uri_field, uri_field_changed_signal_id);

    text_changed = FALSE;
    name_text_changed = FALSE;

    g_free (name);
    g_free (entry_text);
}

static void
update_bookmark_from_text (void)
{
    if (text_changed)
    {
        CajaBookmark *bookmark, *bookmark_in_list;
        char *name;
        cairo_surface_t *surface;
        guint selected_row;
        GtkTreeIter iter;
        GFile *location;

        g_assert (GTK_IS_ENTRY (name_field));
        g_assert (GTK_IS_ENTRY (uri_field));

        if (gtk_entry_get_text_length (GTK_ENTRY (uri_field)) == 0)
        {
            return;
        }

        location = g_file_parse_name
                   (gtk_entry_get_text (GTK_ENTRY (uri_field)));

        bookmark = caja_bookmark_new (location, gtk_entry_get_text (GTK_ENTRY (name_field)),
                                      name_text_changed, NULL);

        g_object_unref (location);

        selected_row = get_selected_row ();

        /* turn off list updating 'cuz otherwise the list-reordering code runs
         * after repopulate(), thus reordering the correctly-ordered list.
         */
        g_signal_handler_block (bookmarks,
                                bookmark_list_changed_signal_id);
        caja_bookmark_list_delete_item_at (bookmarks, selected_row);
        caja_bookmark_list_insert_item (bookmarks, bookmark, selected_row);
        g_signal_handler_unblock (bookmarks,
                                  bookmark_list_changed_signal_id);
        g_object_unref (bookmark);

        /* We also have to update the bookmark pointer in the list
           store. */
        gtk_tree_selection_get_selected (bookmark_selection,
                                         NULL, &iter);
        g_signal_handler_block (bookmark_list_store,
                                row_changed_signal_id);

        bookmark_in_list = caja_bookmark_list_item_at (bookmarks,
                           selected_row);

        name = caja_bookmark_get_name (bookmark_in_list);

        surface = caja_bookmark_get_surface (bookmark_in_list, GTK_ICON_SIZE_MENU);

        gtk_list_store_set (bookmark_list_store, &iter,
                            BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark_in_list,
                            BOOKMARK_LIST_COLUMN_NAME, name,
                            BOOKMARK_LIST_COLUMN_ICON, surface,
                            -1);
        g_signal_handler_unblock (bookmark_list_store,
                                  row_changed_signal_id);

        cairo_surface_destroy (surface);
        g_free (name);
    }
}

static gboolean
on_text_field_focus_out_event (GtkWidget *widget,
                               GdkEventFocus *event,
                               gpointer user_data)
{
    g_assert (CAJA_IS_ENTRY (widget));

    update_bookmark_from_text ();
    return FALSE;
}

static void
name_or_uri_field_activate (CajaEntry *entry)
{
    g_assert (CAJA_IS_ENTRY (entry));

    update_bookmark_from_text ();
    caja_entry_select_all_at_idle (entry);
}

static void
on_uri_field_changed (GtkEditable *editable,
                      gpointer user_data)
{
    /* Remember that user has changed text so we
     * update real bookmark later.
     */
    text_changed = TRUE;
}

static gboolean
on_window_delete_event (GtkWidget *widget,
                        GdkEvent *event,
                        gpointer user_data)
{
    gtk_widget_hide (widget);
    return TRUE;
}

static gboolean
restore_geometry (gpointer data)
{
    g_assert (GTK_IS_WINDOW (data));

    caja_bookmarks_window_restore_geometry (GTK_WIDGET (data));

    /* Don't call this again */
    return FALSE;
}

static void
on_window_hide_event (GtkWidget *widget,
                      gpointer user_data)
{
    caja_bookmarks_window_save_geometry (GTK_WINDOW (widget));

    /* restore_geometry only works after window is hidden */
    g_idle_add (restore_geometry, widget);
}

static void
on_window_destroy_event (GtkWidget *widget,
                         gpointer user_data)
{
    g_object_unref (bookmark_list_store);
    g_object_unref (bookmark_empty_list_store);
    g_source_remove_by_user_data (widget);
}

static void
repopulate (void)
{
    CajaBookmark *selected;
    GtkListStore *store;
    GtkTreeRowReference *reference;
    guint index;
    GtkTreePath *path = NULL;

    g_assert (GTK_IS_TREE_VIEW (bookmark_list_widget));
    g_assert (CAJA_IS_BOOKMARK_LIST (bookmarks));

    store = GTK_LIST_STORE (bookmark_list_store);

    selected = get_selected_bookmark ();

    g_signal_handler_block (bookmark_selection,
                            selection_changed_id);
    g_signal_handler_block (bookmark_list_store,
                            row_deleted_signal_id);
    g_signal_handler_block (bookmark_list_widget,
                            row_activated_signal_id);
    g_signal_handler_block (bookmark_list_widget,
                            key_pressed_signal_id);
    g_signal_handler_block (bookmark_list_widget,
                            button_pressed_signal_id);

    gtk_list_store_clear (store);

    g_signal_handler_unblock (bookmark_list_widget,
                              row_activated_signal_id);
    g_signal_handler_unblock (bookmark_list_widget,
                              key_pressed_signal_id);
    g_signal_handler_unblock (bookmark_list_widget,
                              button_pressed_signal_id);
    g_signal_handler_unblock (bookmark_list_store,
                              row_deleted_signal_id);
    g_signal_handler_unblock (bookmark_selection,
                              selection_changed_id);

    /* Fill the list in with the bookmark names. */
    g_signal_handler_block (store, row_changed_signal_id);

    reference = NULL;

    for (index = 0; index < caja_bookmark_list_length (bookmarks); ++index)
    {
        CajaBookmark *bookmark;
        char             *bookmark_name;
        cairo_surface_t  *bookmark_surface;
        GtkTreeIter       iter;

        bookmark = caja_bookmark_list_item_at (bookmarks, index);
        bookmark_name = caja_bookmark_get_name (bookmark);
        bookmark_surface = caja_bookmark_get_surface (bookmark, GTK_ICON_SIZE_MENU);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            BOOKMARK_LIST_COLUMN_ICON, bookmark_surface,
                            BOOKMARK_LIST_COLUMN_NAME, bookmark_name,
                            BOOKMARK_LIST_COLUMN_BOOKMARK, bookmark,
                            BOOKMARK_LIST_COLUMN_STYLE, PANGO_STYLE_NORMAL,
                            -1);

        if (bookmark == selected)
        {
            /* save old selection */
            GtkTreePath *path;

            path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
            reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path);
            gtk_tree_path_free (path);
        }

        g_free (bookmark_name);
        cairo_surface_destroy (bookmark_surface);

    }
    g_signal_handler_unblock (store, row_changed_signal_id);

    if (reference != NULL)
    {
        /* restore old selection */

        /* bookmarks_set_empty() will call the selection change handler,
         * so we block it here in case of selection change.
         */
        g_signal_handler_block (bookmark_selection, selection_changed_id);

        g_assert (index != 0);
        g_assert (gtk_tree_row_reference_valid (reference));

        path = gtk_tree_row_reference_get_path (reference);
        gtk_tree_selection_select_path (bookmark_selection, path);
        gtk_tree_row_reference_free (reference);
        gtk_tree_path_free (path);

        g_signal_handler_unblock (bookmark_selection, selection_changed_id);
    }

    bookmarks_set_empty (index == 0);
}

static int
handle_close_accelerator (GtkWindow *window,
                          GdkEventKey *event,
                          gpointer user_data)
{
    g_assert (GTK_IS_WINDOW (window));
    g_assert (event != NULL);
    g_assert (user_data == NULL);

	if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w) {
        gtk_widget_hide (GTK_WIDGET (window));
        return TRUE;
    }

    return FALSE;
}

static void
set_up_close_accelerator (GtkWidget *window)
{
    /* Note that we don't call eel_gtk_window_set_up_close_accelerator
     * here because we have to handle saving geometry before hiding the
     * window.
     */
    g_signal_connect (window, "key_press_event",
                      G_CALLBACK (handle_close_accelerator), NULL);
}