summaryrefslogtreecommitdiff
path: root/libcaja-private/caja-open-with-dialog.c
blob: 91810f8462b08a741d7fcc58bde875a8255e7bad (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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
   eel-open-with-dialog.c: an open-with dialog

   Copyright (C) 2004 Novell, Inc.
   Copyright (C) 2007 Red Hat, Inc.

   The Mate Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Mate Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Mate Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Authors: Dave Camp <dave@novell.com>
            Alexander Larsson <alexl@redhat.com>
*/

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

#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gio/gio.h>

#include <eel/eel-stock-dialogs.h>

#include "caja-open-with-dialog.h"
#include "caja-signaller.h"

#define sure_string(s)                    ((const char *)((s)!=NULL?(s):""))
#define DESKTOP_ENTRY_GROUP		  "Desktop Entry"

struct _CajaOpenWithDialogDetails
{
    GAppInfo *selected_app_info;

    char *content_type;
    char *extension;

    GtkWidget *label;
    GtkWidget *entry;
    GtkWidget *button;
    GtkWidget *checkbox;

    GtkWidget *desc_label;

    GtkWidget *open_label;

    GtkWidget     *program_list;
    GtkListStore  *program_list_store;
    GSList	      *add_icon_paths;
    gint	       add_items_idle_id;
    gint	       add_icons_idle_id;

    gboolean add_mode;
};

enum
{
    COLUMN_APP_INFO,
    COLUMN_ICON,
    COLUMN_GICON,
    COLUMN_NAME,
    COLUMN_COMMENT,
    COLUMN_EXEC,
    NUM_COLUMNS
};

enum
{
    RESPONSE_OPEN,
    RESPONSE_REMOVE
};

enum
{
    APPLICATION_SELECTED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (CajaOpenWithDialog, caja_open_with_dialog, GTK_TYPE_DIALOG);

static void
caja_open_with_dialog_finalize (GObject *object)
{
    CajaOpenWithDialog *dialog;

    dialog = CAJA_OPEN_WITH_DIALOG (object);

    if (dialog->details->add_icons_idle_id)
    {
        g_source_remove (dialog->details->add_icons_idle_id);
    }

    if (dialog->details->add_items_idle_id)
    {
        g_source_remove (dialog->details->add_items_idle_id);
    }

    if (dialog->details->selected_app_info)
    {
        g_object_unref (dialog->details->selected_app_info);
    }
    g_free (dialog->details->content_type);
    g_free (dialog->details->extension);

    g_free (dialog->details);

    G_OBJECT_CLASS (caja_open_with_dialog_parent_class)->finalize (object);
}

/* An application is valid if:
 *
 * 1) The file exists
 * 2) The user has permissions to run the file
 */
static gboolean
check_application (CajaOpenWithDialog *dialog)
{
    char *command;
    char *path = NULL;
    char **argv = NULL;
    int argc;
    GError *error = NULL;
    gint retval = TRUE;

    command = NULL;
    if (dialog->details->selected_app_info != NULL)
    {
        command = g_strdup (g_app_info_get_executable (dialog->details->selected_app_info));
    }

    if (command == NULL)
    {
        command = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->details->entry)));
    }

    g_shell_parse_argv (command, &argc, &argv, &error);
    if (error)
    {
        eel_show_error_dialog (_("Could not run application"),
                               error->message,
                               GTK_WINDOW (dialog));
        g_error_free (error);
        retval = FALSE;
        goto cleanup;
    }

    path = g_find_program_in_path (argv[0]);
    if (!path)
    {
        char *error_message;

        error_message = g_strdup_printf (_("Could not find '%s'"),
                                         argv[0]);

        eel_show_error_dialog (_("Could not find application"),
                               error_message,
                               GTK_WINDOW (dialog));
        g_free (error_message);
        retval = FALSE;
        goto cleanup;
    }

cleanup:
    g_strfreev (argv);
    g_free (path);
    g_free (command);

    return retval;
}

/* Only called for non-desktop files */
static char *
get_app_name (const char *commandline, GError **error)
{
    char *basename;
    char *unquoted;
    char **argv;
    int argc;

    if (!g_shell_parse_argv (commandline,
                             &argc, &argv, error))
    {
        return NULL;
    }

    unquoted = g_shell_unquote (argv[0], NULL);
    if (unquoted)
    {
        basename = g_path_get_basename (unquoted);
    }
    else
    {
        basename = g_strdup (argv[0]);
    }

    g_free (unquoted);
    g_strfreev (argv);

    return basename;
}

/* This will check if the application the user wanted exists will return that
 * application.  If it doesn't exist, it will create one and return that.
 * It also sets the app info as the default for this type.
 */
static GAppInfo *
add_or_find_application (CajaOpenWithDialog *dialog)
{
    GAppInfo *app;
    GError *error;
    gboolean success, should_set_default;
    char *message;

    error = NULL;
    app = NULL;
    if (dialog->details->selected_app_info)
    {
        app = g_object_ref (dialog->details->selected_app_info);
    }
    else
    {
        char *app_name;
        const char *commandline;

        commandline = gtk_entry_get_text (GTK_ENTRY (dialog->details->entry));
        app_name = get_app_name (commandline, &error);
        if (app_name != NULL)
        {
            app = g_app_info_create_from_commandline (commandline,
                    app_name,
                    G_APP_INFO_CREATE_NONE,
                    &error);
            g_free (app_name);
        }
    }

    if (app == NULL)
    {
        message = g_strdup_printf (_("Could not add application to the application database: %s"), error ? error->message : _("Unknown error"));
        eel_show_error_dialog (_("Could not add application"),
                               message,
                               GTK_WINDOW (dialog));
        g_free (message);

        if (error)
            g_error_free (error);

        return NULL;
    }

    should_set_default = (dialog->details->add_mode) ||
                          gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->details->checkbox));
    success = TRUE;

    if (should_set_default)
    {
        if (dialog->details->content_type)
        {
            success = g_app_info_set_as_default_for_type (app,
                      dialog->details->content_type,
                      &error);
        }
        else
        {
            success = g_app_info_set_as_default_for_extension (app,
                      dialog->details->extension,
                      &error);
        }
    }
    else
    {
        GList *applications;

        applications = g_app_info_get_all_for_type (dialog->details->content_type);
        if (dialog->details->content_type && applications != NULL)
        {
            /* we don't care about reporting errors here */
            g_app_info_add_supports_type (app,
                                          dialog->details->content_type,
                                          NULL);
        }

        if (applications != NULL)
        {
            g_list_free_full (applications, g_object_unref);
        }
    }

    if (!success && should_set_default)
    {
        message = g_strdup_printf (_("Could not set application as the default: %s"), error->message);
        eel_show_error_dialog (_("Could not set as default application"),
                               message,
                               GTK_WINDOW (dialog));
        g_free (message);
        g_error_free (error);
    }

    g_signal_emit_by_name (caja_signaller_get_current (),
                           "mime_data_changed");
    return app;
}

static void
emit_application_selected (CajaOpenWithDialog *dialog,
                           GAppInfo *application)
{
    g_signal_emit (G_OBJECT (dialog), signals[APPLICATION_SELECTED], 0,
                   application);
}

static void
response_cb (CajaOpenWithDialog *dialog,
             int response_id,
             gpointer data)
{
    switch (response_id)
    {
    case RESPONSE_OPEN:
        if (check_application (dialog))
        {
            GAppInfo *application;

            application = add_or_find_application (dialog);

            if (application)
            {
                emit_application_selected (dialog, application);
                g_object_unref (application);

                gtk_widget_destroy (GTK_WIDGET (dialog));
            }
        }

        break;
    case RESPONSE_REMOVE:
        if (dialog->details->selected_app_info != NULL)
        {
            if (g_app_info_delete (dialog->details->selected_app_info))
            {
                GtkTreeModel *model;
                GtkTreeIter iter;
                GAppInfo *info, *selected;

                selected = dialog->details->selected_app_info;
                dialog->details->selected_app_info = NULL;

                model = GTK_TREE_MODEL (dialog->details->program_list_store);
                if (gtk_tree_model_get_iter_first (model, &iter))
                {
                    do
                    {
                        gtk_tree_model_get (model, &iter,
                                            COLUMN_APP_INFO, &info,
                                            -1);
                        if (g_app_info_equal (selected, info))
                        {
                            gtk_list_store_remove (dialog->details->program_list_store, &iter);
                            break;
                        }
                    }
                    while (gtk_tree_model_iter_next (model, &iter));
                }

                g_object_unref (selected);
            }
        }
        break;
    case GTK_RESPONSE_NONE:
    case GTK_RESPONSE_DELETE_EVENT:
    case GTK_RESPONSE_CANCEL:
        gtk_widget_destroy (GTK_WIDGET (dialog));
        break;
    default :
        g_assert_not_reached ();
    }

}


static void
caja_open_with_dialog_class_init (CajaOpenWithDialogClass *class)
{
    GObjectClass *gobject_class;

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = caja_open_with_dialog_finalize;

    signals[APPLICATION_SELECTED] =
        g_signal_new ("application_selected",
                      G_TYPE_FROM_CLASS (class),
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET (CajaOpenWithDialogClass,
                                       application_selected),
                      NULL, NULL,
                      g_cclosure_marshal_VOID__POINTER,
                      G_TYPE_NONE,
                      1, G_TYPE_POINTER);
}

static void
chooser_response_cb (GtkFileChooser *chooser,
                     int response,
                     gpointer user_data)
{
    CajaOpenWithDialog *dialog;

    dialog = CAJA_OPEN_WITH_DIALOG (user_data);

    if (response == GTK_RESPONSE_OK)
    {
        char *filename;

        filename = gtk_file_chooser_get_filename (chooser);

        if (filename)
        {
            char *quoted_text;

            quoted_text = g_shell_quote (filename);

            gtk_entry_set_text (GTK_ENTRY (dialog->details->entry),
                                quoted_text);
            gtk_editable_set_position (GTK_EDITABLE (dialog->details->entry), -1);
            g_free (quoted_text);
            g_free (filename);
        }
    }

    gtk_widget_destroy (GTK_WIDGET (chooser));
}

static void
browse_clicked_cb (GtkWidget *button,
                   gpointer user_data)
{
    CajaOpenWithDialog *dialog;
    GtkWidget *chooser;

    dialog = CAJA_OPEN_WITH_DIALOG (user_data);

    chooser = eel_file_chooser_dialog_new (_("Select an Application"),
                                           GTK_WINDOW (dialog),
                                           GTK_FILE_CHOOSER_ACTION_OPEN,
                                           "process-stop",
                                           GTK_RESPONSE_CANCEL,
                                           "document-open",
                                           GTK_RESPONSE_OK,
                                           NULL);
    gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser), TRUE);
    g_signal_connect (chooser, "response",
                      G_CALLBACK (chooser_response_cb), dialog);
    gtk_dialog_set_default_response (GTK_DIALOG (chooser),
                                     GTK_RESPONSE_OK);
    gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), TRUE);
    gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser),
                                          FALSE);
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
                                         "/usr/bin");

    gtk_widget_show (chooser);
}

static void
entry_changed_cb (GtkWidget *entry,
                  CajaOpenWithDialog *dialog)
{
    /* We are writing in the entry, so we are not using a known appinfo anymore */
    if (dialog->details->selected_app_info != NULL)
    {
        g_object_unref (dialog->details->selected_app_info);
        dialog->details->selected_app_info = NULL;
    }

    if (gtk_entry_get_text (GTK_ENTRY (dialog->details->entry))[0] == '\000')
    {
        gtk_widget_set_sensitive (dialog->details->button, FALSE);
    }
    else
    {
        gtk_widget_set_sensitive (dialog->details->button, TRUE);
    }
}

#define CAJA_OPEN_WITH_DIALOG_ICON_SIZE 24
static cairo_surface_t *
get_surface_for_icon (GIcon *icon)
{
    cairo_surface_t  *surface;
    gint icon_scale;

    surface = NULL;
    icon_scale = gdk_window_get_scale_factor (gdk_get_default_root_window ());

    if (G_IS_FILE_ICON (icon))
    {
        char *filename;

        filename = g_file_get_path (g_file_icon_get_file (G_FILE_ICON (icon)));
        if (filename)
        {
            GdkPixbuf *pixbuf;
            pixbuf = gdk_pixbuf_new_from_file_at_size (filename,
                                                       CAJA_OPEN_WITH_DIALOG_ICON_SIZE * icon_scale,
                                                       CAJA_OPEN_WITH_DIALOG_ICON_SIZE * icon_scale,
                                                       NULL);
            surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, icon_scale, NULL);
            g_object_unref (pixbuf);
        }
        g_free (filename);
    }
    else if (G_IS_THEMED_ICON (icon))
    {
        const char * const *names;

        names = g_themed_icon_get_names (G_THEMED_ICON (icon));

        if (names != NULL && names[0] != NULL)
        {
            char *icon_no_extension;
            char *p;

            icon_no_extension = g_strdup (names[0]);
            p = strrchr (icon_no_extension, '.');
            if (p &&
                    (strcmp (p, ".png") == 0 ||
                     strcmp (p, ".xpm") == 0 ||
                     strcmp (p, ".svg") == 0))
            {
                *p = 0;
            }
            surface = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (),
                                                   icon_no_extension,
                                                   CAJA_OPEN_WITH_DIALOG_ICON_SIZE,
                                                   icon_scale,
                                                   NULL,
                                                   GTK_ICON_LOOKUP_FORCE_SIZE,
                                                   NULL);
            g_free (icon_no_extension);
        }
    }
    return surface;
}

static gboolean
caja_open_with_dialog_add_icon_idle (CajaOpenWithDialog *dialog)
{
    cairo_surface_t *surface;
    GIcon           *icon;
    gboolean         long_operation;
    GtkTreeIter      iter;
    GtkTreePath     *path = NULL;

    long_operation = FALSE;
    do
    {
        if (!dialog->details->add_icon_paths)
        {
            dialog->details->add_icons_idle_id = 0;
            return FALSE;
        }

        path = dialog->details->add_icon_paths->data;
        dialog->details->add_icon_paths->data = NULL;
        dialog->details->add_icon_paths = g_slist_delete_link (dialog->details->add_icon_paths,
                                          dialog->details->add_icon_paths);

        if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (dialog->details->program_list_store),
                                      &iter, path))
        {
            gtk_tree_path_free (path);
            continue;
        }

        gtk_tree_path_free (path);

        gtk_tree_model_get (GTK_TREE_MODEL (dialog->details->program_list_store), &iter,
                            COLUMN_GICON, &icon, -1);

        if (icon == NULL)
        {
            continue;
        }

        surface = get_surface_for_icon (icon);
        if (surface)
        {
            long_operation = TRUE;
            gtk_list_store_set (dialog->details->program_list_store, &iter, COLUMN_ICON, surface, -1);
            cairo_surface_destroy (surface);
        }

        /* don't go back into the main loop if this wasn't very hard to do */
    }
    while (!long_operation);

    return TRUE;
}


static gboolean
caja_open_with_search_equal_func (GtkTreeModel *model,
                                  int column,
                                  const char *key,
                                  GtkTreeIter *iter,
                                  gpointer user_data)
{
    char *name;
    char *path;
    gboolean ret;

    if (key != NULL)
    {
        char *normalized_key;

        normalized_key = g_utf8_casefold (key, -1);
        g_assert (normalized_key != NULL);

        ret = TRUE;

        gtk_tree_model_get (model, iter,
                            COLUMN_NAME, &name,
                            COLUMN_EXEC, &path,
                            -1);

        if (name != NULL)
        {
            char *normalized_name;

            normalized_name = g_utf8_casefold (name, -1);
            g_assert (normalized_name != NULL);

            if (strncmp (normalized_name, normalized_key, strlen (normalized_key)) == 0)
            {
                ret = FALSE;
            }

            g_free (normalized_name);
        }

        if (ret && path != NULL)
        {
            char *normalized_path;
            char *basename, *normalized_basename;

            normalized_path = g_utf8_casefold (path, -1);
            g_assert (normalized_path != NULL);

            basename = g_path_get_basename (path);
            g_assert (basename != NULL);

            normalized_basename = g_utf8_casefold (basename, -1);
            g_assert (normalized_basename != NULL);

            if (strncmp (normalized_path, normalized_key, strlen (normalized_key)) == 0 ||
                    strncmp (normalized_basename, normalized_key, strlen (normalized_key)) == 0)
            {
                ret = FALSE;
            }

            g_free (basename);
            g_free (normalized_basename);
            g_free (normalized_path);
        }

        g_free (name);
        g_free (path);
        g_free (normalized_key);

        return ret;
    }
    else
    {
        return TRUE;
    }
}



static gboolean
caja_open_with_dialog_add_items_idle (CajaOpenWithDialog *dialog)
{
    GtkCellRenderer   *renderer;
    GtkTreeViewColumn *column;
    GtkTreeModel      *sort;
    GList             *all_applications;
    GList             *l;

    /* create list store */
    dialog->details->program_list_store = gtk_list_store_new (NUM_COLUMNS,
                                          G_TYPE_APP_INFO,
                                          CAIRO_GOBJECT_TYPE_SURFACE,
                                          G_TYPE_ICON,
                                          G_TYPE_STRING,
                                          G_TYPE_STRING,
                                          G_TYPE_STRING);
    sort = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dialog->details->program_list_store));
    all_applications = g_app_info_get_all ();

    for (l = all_applications; l; l = l->next)
    {
        GAppInfo *app = l->data;
        GtkTreeIter     iter;
        GtkTreePath    *path;

        if (!g_app_info_supports_uris (app) &&
                !g_app_info_supports_files (app))
            continue;

        gtk_list_store_append (dialog->details->program_list_store, &iter);
        gtk_list_store_set (dialog->details->program_list_store, &iter,
                            COLUMN_APP_INFO,  app,
                            COLUMN_ICON,      NULL,
                            COLUMN_GICON,     g_app_info_get_icon (app),
                            COLUMN_NAME,      g_app_info_get_display_name (app),
                            COLUMN_COMMENT,   g_app_info_get_description (app),
                            COLUMN_EXEC,      g_app_info_get_executable,
                            -1);

        path = gtk_tree_model_get_path (GTK_TREE_MODEL (dialog->details->program_list_store), &iter);
        if (path != NULL)
        {
            dialog->details->add_icon_paths = g_slist_prepend (dialog->details->add_icon_paths, path);
        }
    }
    g_list_free (all_applications);

    gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->details->program_list),
                             GTK_TREE_MODEL (sort));
    gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort),
                                          COLUMN_NAME, GTK_SORT_ASCENDING);
    gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (dialog->details->program_list),
                                         caja_open_with_search_equal_func,
                                         NULL, NULL);

    renderer = gtk_cell_renderer_pixbuf_new ();
    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_pack_start (column, renderer, FALSE);
    gtk_tree_view_column_set_attributes (column, renderer,
                                         "surface", COLUMN_ICON,
                                         NULL);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_attributes (column, renderer,
                                         "text", COLUMN_NAME,
                                         NULL);
    gtk_tree_view_column_set_sort_column_id (column, COLUMN_NAME);
    gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->details->program_list), column);

    dialog->details->add_icon_paths = g_slist_reverse (dialog->details->add_icon_paths);

    if (!dialog->details->add_icons_idle_id)
    {
        dialog->details->add_icons_idle_id =
            g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, (GSourceFunc) caja_open_with_dialog_add_icon_idle,
                             dialog, NULL);
    }

    dialog->details->add_items_idle_id = 0;
    return FALSE;
}

static void
program_list_selection_changed (GtkTreeSelection  *selection,
                                CajaOpenWithDialog *dialog)
{
    GtkTreeModel     *model;
    GtkTreeIter       iter;
    GAppInfo *info;

    if (!gtk_tree_selection_get_selected (selection, &model, &iter))
    {
        gtk_widget_set_sensitive (dialog->details->button, FALSE);
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                           RESPONSE_REMOVE,
                                           FALSE);
        return;
    }

    info = NULL;
    gtk_tree_model_get (model, &iter,
                        COLUMN_APP_INFO, &info,
                        -1);

    if (info == NULL)
    {
        return;
    }

    gtk_entry_set_text (GTK_ENTRY (dialog->details->entry),
                        sure_string (g_app_info_get_executable (info)));
    gtk_label_set_text (GTK_LABEL (dialog->details->desc_label),
                        sure_string (g_app_info_get_description (info)));
    gtk_widget_set_sensitive (dialog->details->button, TRUE);
    gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                       RESPONSE_REMOVE,
                                       g_app_info_can_delete (info));

    if (dialog->details->selected_app_info)
    {
        g_object_unref (dialog->details->selected_app_info);
    }

    dialog->details->selected_app_info = info;
}

static void
program_list_selection_activated (GtkTreeView       *view,
                                  GtkTreePath       *path,
                                  GtkTreeViewColumn *column,
                                  CajaOpenWithDialog *dialog)
{
    GtkTreeSelection *selection;

    /* update the entry with the info from the selection */
    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));
    program_list_selection_changed (selection, dialog);

    gtk_dialog_response (GTK_DIALOG (&dialog->parent), RESPONSE_OPEN);
}

static void
expander_toggled (GtkWidget *expander, CajaOpenWithDialog *dialog)
{
    if (gtk_expander_get_expanded (GTK_EXPANDER (expander)) == TRUE)
    {
        gtk_widget_grab_focus (dialog->details->entry);
        gtk_window_resize (GTK_WINDOW (dialog), 400, 1);
    }
    else
    {
        GtkTreeSelection *selection;

        gtk_widget_grab_focus (dialog->details->program_list);
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));
        program_list_selection_changed (selection, dialog);
    }
}

static void
caja_open_with_dialog_init (CajaOpenWithDialog *dialog)
{
    GtkWidget *hbox;
    GtkWidget *vbox;
    GtkWidget *vbox2;
    GtkWidget *label;
    GtkWidget *scrolled_window;
    GtkWidget *expander;
    GtkTreeSelection *selection;

    dialog->details = g_new0 (CajaOpenWithDialogDetails, 1);

    gtk_window_set_title (GTK_WINDOW (dialog), _("Open With"));
    gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
    gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);

    gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), 2);

    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

    vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
    gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);

    dialog->details->label = gtk_label_new ("");
    gtk_label_set_xalign (GTK_LABEL (dialog->details->label), 0.0);
    gtk_label_set_line_wrap (GTK_LABEL (dialog->details->label), TRUE);
    gtk_box_pack_start (GTK_BOX (vbox2), dialog->details->label,
                        FALSE, FALSE, 0);
    gtk_widget_show (dialog->details->label);


    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_set_size_request (scrolled_window, 400, 300);

    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
                                         GTK_SHADOW_IN);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_overlay_scrolling (GTK_SCROLLED_WINDOW (scrolled_window), FALSE);

    dialog->details->program_list = gtk_tree_view_new ();
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->details->program_list),
                                       FALSE);
    gtk_container_add (GTK_CONTAINER (scrolled_window), dialog->details->program_list);

    gtk_box_pack_start (GTK_BOX (vbox2), scrolled_window, TRUE, TRUE, 0);

    dialog->details->desc_label = gtk_label_new (_("Select an application to view its description."));
    gtk_label_set_xalign (GTK_LABEL (dialog->details->desc_label), 0.0);
    gtk_label_set_justify (GTK_LABEL (dialog->details->desc_label), GTK_JUSTIFY_LEFT);
    gtk_label_set_max_width_chars (GTK_LABEL (dialog->details->desc_label), 54);
    gtk_label_set_line_wrap (GTK_LABEL (dialog->details->desc_label), TRUE);
    gtk_label_set_single_line_mode (GTK_LABEL (dialog->details->desc_label), FALSE);
    gtk_box_pack_start (GTK_BOX (vbox2), dialog->details->desc_label, FALSE, FALSE, 0);

    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
    g_signal_connect (selection, "changed",
                      G_CALLBACK (program_list_selection_changed),
                      dialog);
    g_signal_connect (dialog->details->program_list, "row-activated",
                      G_CALLBACK (program_list_selection_activated),
                      dialog);

    dialog->details->add_items_idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
                                         (GSourceFunc) caja_open_with_dialog_add_items_idle,
                                         dialog, NULL);


    gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), vbox, TRUE, TRUE, 0);
    gtk_widget_show_all (vbox);


    expander = gtk_expander_new_with_mnemonic (_("_Use a custom command"));
    gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 0);
    g_signal_connect_after (expander, "activate", G_CALLBACK (expander_toggled), dialog);

    gtk_widget_show (expander);

    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    gtk_container_add (GTK_CONTAINER (expander), hbox);
    gtk_widget_show (hbox);

    dialog->details->entry = gtk_entry_new ();
    gtk_entry_set_activates_default (GTK_ENTRY (dialog->details->entry), TRUE);

    gtk_box_pack_start (GTK_BOX (hbox), dialog->details->entry,
                        TRUE, TRUE, 0);
    gtk_widget_show (dialog->details->entry);

    dialog->details->button = gtk_button_new_with_mnemonic (_("_Browse..."));
    g_signal_connect (dialog->details->button, "clicked",
                      G_CALLBACK (browse_clicked_cb), dialog);
    gtk_box_pack_start (GTK_BOX (hbox), dialog->details->button, FALSE, FALSE, 0);
    gtk_widget_show (dialog->details->button);

    /* Add remember this application checkbox - only visible in open mode */
    dialog->details->checkbox = gtk_check_button_new ();
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->details->checkbox), TRUE);
    gtk_button_set_use_underline (GTK_BUTTON (dialog->details->checkbox), TRUE);
    gtk_widget_show (GTK_WIDGET (dialog->details->checkbox));
    gtk_box_pack_start (GTK_BOX (vbox), dialog->details->checkbox, FALSE, FALSE, 0);

    eel_dialog_add_button (GTK_DIALOG (dialog),
                           _("_Remove"),
                           "list-remove",
                           RESPONSE_REMOVE);

    gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                       RESPONSE_REMOVE,
                                       FALSE);

    eel_dialog_add_button (GTK_DIALOG (dialog),
                           _("_Cancel"),
                           "process-stop",
                           GTK_RESPONSE_CANCEL);


    /* Create a custom stock icon */
    dialog->details->button = gtk_button_new ();

    /* Hook up the entry to the button */
    gtk_widget_set_sensitive (dialog->details->button, FALSE);
    g_signal_connect (G_OBJECT (dialog->details->entry), "changed",
                      G_CALLBACK (entry_changed_cb), dialog);

    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
    gtk_widget_show (hbox);

    label = gtk_label_new_with_mnemonic (_("_Open"));
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (dialog->details->button));
    gtk_widget_show (label);
    dialog->details->open_label = label;

    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, FALSE, 0);

    gtk_widget_show (dialog->details->button);
    gtk_widget_set_can_default (dialog->details->button, TRUE);

    gtk_container_add (GTK_CONTAINER (dialog->details->button), hbox);

    gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
                                  dialog->details->button, RESPONSE_OPEN);


    gtk_dialog_set_default_response (GTK_DIALOG (dialog),
                                     RESPONSE_OPEN);

    g_signal_connect (dialog, "response",
                      G_CALLBACK (response_cb),
                      dialog);
}

static char *
get_extension (const char *basename)
{
    char *p;

    p = strrchr (basename, '.');

    if (p && *(p + 1) != '\0')
    {
        return g_strdup (p + 1);
    }
    else
    {
        return NULL;
    }
}

static void
set_uri_and_type (CajaOpenWithDialog *dialog,
                  const char *uri,
                  const char *mime_type,
                  const char *passed_extension,
                  gboolean add_mode)
{
    char *label;
    char *emname;
    char *name, *extension;
    char *checkbox_text;

    name = NULL;
    extension = NULL;

    if (uri != NULL)
    {
        GFile *file;

        file = g_file_new_for_uri (uri);
        name = g_file_get_basename (file);
        g_object_unref (file);
    }
    if (passed_extension == NULL && name != NULL)
    {
        extension = get_extension (name);
    }
    else
    {
        extension = g_strdup (passed_extension);
    }

    if (extension != NULL &&
            g_content_type_is_unknown (mime_type))
    {
        dialog->details->extension = g_strdup (extension);

        if (name != NULL)
        {
            emname = g_strdup_printf ("<i>%s</i>", name);
            if (add_mode)
            {
                /* Translators: first %s is a filename and second %s is a file extension */
                label = g_strdup_printf (_("Open %s and other %s document with:"),
                                         emname, dialog->details->extension);
            }
            else
            {
                /* Translators: the %s here is a file name */
                label = g_strdup_printf (_("Open %s with:"), emname);
                checkbox_text = g_strdup_printf (_("_Remember this application for %s documents"),
                                                 dialog->details->extension);

                gtk_button_set_label (GTK_BUTTON (dialog->details->checkbox), checkbox_text);
                g_free (checkbox_text);
            }
            g_free (emname);
        }
        else
        {
            /* Only in add mode - the %s here is a file extension */
            label = g_strdup_printf (_("Open all %s documents with:"),
                                     dialog->details->extension);
        }
        g_free (extension);
    }
    else
    {
        char *description;

        dialog->details->content_type = g_strdup (mime_type);
        description = g_content_type_get_description (mime_type);

        if (description == NULL)
        {
            description = g_strdup (_("Unknown"));
        }

        if (name != NULL)
        {
            emname = g_strdup_printf ("<i>%s</i>", name);
            if (add_mode)
            {
                /* Translators: First %s is a filename, second is a description
                 * of the type, eg "plain text document" */
                label = g_strdup_printf (_("Open %s and other \"%s\" files with:"),
                                         emname, description);
            }
            else
            {
                /* Translators: %s is a filename */
                label = g_strdup_printf (_("Open %s with:"), emname);
                /* Translators: %s is a file type description */
                checkbox_text = g_strdup_printf (_("_Remember this application for \"%s\" files"),
                                                 description);

                gtk_button_set_label (GTK_BUTTON (dialog->details->checkbox), checkbox_text);
                g_free (checkbox_text);
            }
            g_free (emname);
        }
        else
        {
            /* Only in add mode */
            label = g_strdup_printf (_("Open all \"%s\" files with:"), description);
        }

        g_free (description);
    }

    dialog->details->add_mode = add_mode;
    if (add_mode)
    {
        gtk_widget_hide (dialog->details->checkbox);

        gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->details->open_label),
                                          _("_Add"));
        gtk_window_set_title (GTK_WINDOW (dialog), _("Add Application"));
    }

    gtk_label_set_markup (GTK_LABEL (dialog->details->label), label);

    g_free (label);
    g_free (name);
}


static GtkWidget *
real_caja_open_with_dialog_new (const char *uri,
                                const char *mime_type,
                                const char *extension,
                                gboolean add_mode)
{
    GtkWidget *dialog;

    dialog = gtk_widget_new (CAJA_TYPE_OPEN_WITH_DIALOG, NULL);

    set_uri_and_type (CAJA_OPEN_WITH_DIALOG (dialog), uri, mime_type, extension, add_mode);

    return dialog;
}

GtkWidget *
caja_open_with_dialog_new (const char *uri,
                           const char *mime_type,
                           const char *extension)
{
    return real_caja_open_with_dialog_new (uri, mime_type, extension, FALSE);
}

GtkWidget *
caja_add_application_dialog_new (const char *uri,
                                 const char *mime_type)
{
    CajaOpenWithDialog *dialog;

    dialog = CAJA_OPEN_WITH_DIALOG (real_caja_open_with_dialog_new (uri, mime_type, NULL, TRUE));

    return GTK_WIDGET (dialog);
}

GtkWidget *
caja_add_application_dialog_new_for_multiple_files (const char *extension,
        const char *mime_type)
{
    CajaOpenWithDialog *dialog;

    dialog = CAJA_OPEN_WITH_DIALOG (real_caja_open_with_dialog_new (NULL, mime_type, extension, TRUE));

    return GTK_WIDGET (dialog);
}