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
|
/*
* Copyright (C) 2002 Jorn Baayen
* Copyright (C) 2003, 2004 Marco Pesenti Gritti
* Copyright (C) 2004 Christian Persch
*
* This program 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, or (at your option)
* any later version.
*
* This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
#include "config.h"
#include "ephy-file-helpers.h"
#include "ephy-prefs.h"
#include "eel-gconf-extensions.h"
#include "ephy-debug.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <libgnome/gnome-init.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-file-info.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libxml/xmlreader.h>
/* bug http://bugzilla.gnome.org/show_bug.cgi?id=156687 */
#undef GNOME_DISABLE_DEPRECATED
#include <libgnome/gnome-desktop-item.h>
#define SN_API_NOT_YET_FROZEN
#include <libsn/sn.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#define EPHY_UUID "0d82d98f-7079-401c-abff-203fcde1ece3"
#define EPHY_UUID_ENVVAR "EPHY_UNIQUE"
#define EPHY_UUID_ENVSTRING EPHY_UUID_ENVVAR "=" EPHY_UUID
#define DELAY_MAX_TICKS 64
#define INITIAL_TICKS 2
static GHashTable *files = NULL;
static GHashTable *mime_table = NULL;
static char *dot_dir = NULL;
static char *tmp_dir = NULL;
static GList *del_on_exit = NULL;
const char *
ephy_file_tmp_dir (void)
{
if (tmp_dir == NULL)
{
char *partial_name;
char *full_name;
partial_name = g_strconcat ("epiphany-", g_get_user_name (),
"-XXXXXX", NULL);
full_name = g_build_filename (g_get_tmp_dir (), partial_name,
NULL);
#ifdef HAVE_MKDTEMP
tmp_dir = mkdtemp (full_name);
#else
#error no mkdtemp implementation
#endif
g_free (partial_name);
if (tmp_dir == NULL)
{
g_free (full_name);
}
}
return tmp_dir;
}
char *
ephy_file_downloads_dir (void)
{
const char *translated_folder;
char *desktop_dir, *converted, *downloads_dir;
/* The name of the default downloads folder */
translated_folder = _("Downloads");
converted = g_filename_from_utf8 (translated_folder, -1, NULL,
NULL, NULL);
desktop_dir = ephy_file_desktop_dir ();
downloads_dir = g_build_filename (desktop_dir, converted, NULL);
g_free (desktop_dir);
g_free (converted);
return downloads_dir;
}
char *
ephy_file_get_downloads_dir (void)
{
char *download_dir, *expanded;
download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR);
if (download_dir && strcmp (download_dir, "Downloads") == 0)
{
g_free (download_dir);
download_dir = ephy_file_downloads_dir ();
}
else if (download_dir && strcmp (download_dir, "Desktop") == 0)
{
g_free (download_dir);
download_dir = ephy_file_desktop_dir ();
}
else if (download_dir)
{
char *converted_dp;
converted_dp = g_filename_from_utf8 (download_dir, -1, NULL, NULL, NULL);
g_free (download_dir);
download_dir = converted_dp;
}
/* Emergency download destination */
if (download_dir == NULL)
{
const char *home_dir;
home_dir = g_get_home_dir ();
download_dir = g_strdup (home_dir != NULL ? home_dir : "/");
}
g_return_val_if_fail (download_dir != NULL, NULL);
expanded = gnome_vfs_expand_initial_tilde (download_dir);
g_free (download_dir);
return expanded;
}
char *
ephy_file_desktop_dir (void)
{
char *downloads_dir;
gboolean desktop_is_home;
desktop_is_home = eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR);
if (desktop_is_home)
{
downloads_dir = g_strdup (g_get_home_dir ());
}
else
{
downloads_dir = g_build_filename
(g_get_home_dir (), "Desktop", NULL);
}
return downloads_dir;
}
char *
ephy_file_tmp_filename (const char *base,
const char *extension)
{
int fd;
char *name = g_strdup (base);
fd = mkstemp (name);
if (fd != -1)
{
unlink (name);
close (fd);
}
else
{
g_free (name);
return NULL;
}
if (extension)
{
char *tmp;
tmp = g_strconcat (name, ".",
extension, NULL);
g_free (name);
name = tmp;
}
return name;
}
const char *
ephy_file (const char *filename)
{
char *ret;
guint i;
static const char * const paths[] =
{
SHARE_DIR "/",
SHARE_DIR "/glade/",
SHARE_DIR "/art/",
SHARE_UNINSTALLED_DIR "/",
SHARE_UNINSTALLED_DIR "/glade/",
SHARE_UNINSTALLED_DIR "/art/"
};
g_assert (files != NULL);
ret = g_hash_table_lookup (files, filename);
if (ret != NULL)
return ret;
for (i = 0; i < G_N_ELEMENTS (paths); i++)
{
ret = g_strconcat (paths[i], filename, NULL);
if (g_file_test (ret, G_FILE_TEST_EXISTS) == TRUE)
{
g_hash_table_insert (files, g_strdup (filename), ret);
return (const char *) ret;
}
g_free (ret);
}
g_warning ("Failed to find %s\n", filename);
return NULL;
}
const char *
ephy_dot_dir (void)
{
if (dot_dir == NULL)
{
dot_dir = g_build_filename (g_get_home_dir (),
GNOME_DOT_GNOME,
"epiphany",
NULL);
}
return dot_dir;
}
void
ephy_file_helpers_init (void)
{
const char *uuid;
/* See if we've been calling ourself, and abort if we have */
uuid = g_getenv (EPHY_UUID_ENVVAR);
if (uuid && strcmp (uuid, EPHY_UUID) == 0)
{
g_warning ("Self call detected, exiting!\n");
exit (1);
}
/* Put marker in env */
g_setenv (EPHY_UUID_ENVVAR, EPHY_UUID, TRUE);
files = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_free);
}
static void
delete_files (GList *l)
{
for (; l != NULL; l = l->next)
{
unlink (l->data);
}
}
void
ephy_file_helpers_shutdown (void)
{
g_hash_table_destroy (files);
del_on_exit = g_list_reverse (del_on_exit);
delete_files (del_on_exit);
g_list_foreach (del_on_exit, (GFunc)g_free, NULL);
g_list_free (del_on_exit);
del_on_exit = NULL;
if (mime_table != NULL)
{
LOG ("Destroying mime type hashtable");
g_hash_table_destroy (mime_table);
mime_table = NULL;
}
if (tmp_dir != NULL)
{
rmdir (tmp_dir);
g_free (tmp_dir);
tmp_dir = NULL;
}
g_free (dot_dir);
dot_dir = NULL;
}
gboolean
ephy_ensure_dir_exists (const char *dir)
{
if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
{
if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
{
g_warning (_("\"%s\" exists, please move it out of the way."), dir);
return FALSE;
}
if (mkdir (dir, 488) != 0)
{
g_warning (_("Failed to create directory \"%s\"."), dir);
return FALSE;
}
}
return TRUE;
}
static void
ephy_find_file_recursive (const char *path,
const char *fname,
GSList **list,
gint depth,
gint maxdepth)
{
GDir *dir;
const gchar *file;
dir = g_dir_open (path, 0, NULL);
if (dir != NULL)
{
while ((file = g_dir_read_name (dir)))
{
if (depth < maxdepth)
{
char *new_path = g_build_filename (path, file, NULL);
ephy_find_file_recursive (new_path, fname, list,
depth + 1, maxdepth);
g_free (new_path);
}
if (strcmp (file, fname) == 0)
{
char *new_path = g_build_filename (path, file, NULL);
*list = g_slist_prepend (*list, new_path);
}
}
g_dir_close (dir);
}
}
GSList *
ephy_file_find (const char *path,
const char *fname,
gint maxdepth)
{
GSList *ret = NULL;
ephy_find_file_recursive (path, fname, &ret, 0, maxdepth);
return ret;
}
gboolean
ephy_file_switch_temp_file (const char *filename,
const char *filename_temp)
{
char *old_file;
gboolean old_exist;
gboolean retval = TRUE;
old_file = g_strconcat (filename, ".old", NULL);
old_exist = g_file_test (filename, G_FILE_TEST_EXISTS);
if (old_exist)
{
if (rename (filename, old_file) < 0)
{
g_warning ("Failed to rename %s to %s", filename, old_file);
retval = FALSE;
goto failed;
}
}
if (rename (filename_temp, filename) < 0)
{
g_warning ("Failed to rename %s to %s", filename_temp, filename);
if (rename (old_file, filename) < 0)
{
g_warning ("Failed to restore %s from %s",
filename, filename_temp);
}
retval = FALSE;
goto failed;
}
if (old_exist)
{
if (unlink (old_file) < 0)
{
g_warning ("Failed to delete old file %s", old_file);
}
}
failed:
g_free (old_file);
return retval;
}
void
ephy_file_delete_on_exit (const char *path)
{
del_on_exit = g_list_prepend (del_on_exit,
g_strdup (path));
}
static void
load_mime_from_xml (void)
{
xmlTextReaderPtr reader;
const char *xml_file;
int ret;
EphyMimePermission permission = EPHY_MIME_PERMISSION_UNKNOWN;
g_return_if_fail (mime_table == NULL);
mime_table = g_hash_table_new_full (g_str_hash, g_str_equal,
xmlFree, NULL);
xml_file = ephy_file ("mime-types-permissions.xml");
if (xml_file == NULL)
{
g_warning ("MIME types permissions file not found!\n");
return;
}
reader = xmlNewTextReaderFilename (xml_file);
if (reader == NULL)
{
g_warning ("Could not load MIME types permissions file!\n");
return;
}
ret = xmlTextReaderRead (reader);
while (ret == 1)
{
const xmlChar *tag;
xmlReaderTypes type;
tag = xmlTextReaderConstName (reader);
type = xmlTextReaderNodeType (reader);
if (xmlStrEqual (tag, (const xmlChar *)"safe") && type == XML_READER_TYPE_ELEMENT)
{
permission = EPHY_MIME_PERMISSION_SAFE;
}
else if (xmlStrEqual (tag, (const xmlChar *)"unsafe") && type == XML_READER_TYPE_ELEMENT)
{
permission = EPHY_MIME_PERMISSION_UNSAFE;
}
else if (xmlStrEqual (tag, (const xmlChar *)"mime-type"))
{
xmlChar *type;
type = xmlTextReaderGetAttribute (reader, (const xmlChar *)"type");
g_hash_table_insert (mime_table, type,
GINT_TO_POINTER (permission));
}
ret = xmlTextReaderRead (reader);
}
xmlFreeTextReader (reader);
}
EphyMimePermission
ephy_file_check_mime (const char *mime_type)
{
EphyMimePermission permission;
gpointer tmp;
g_return_val_if_fail (mime_type != NULL, EPHY_MIME_PERMISSION_UNKNOWN);
if (mime_table == NULL)
{
load_mime_from_xml ();
}
tmp = g_hash_table_lookup (mime_table, mime_type);
if (tmp == NULL)
{
permission = EPHY_MIME_PERMISSION_UNKNOWN;
}
else
{
permission = GPOINTER_TO_INT (tmp);
}
return permission;
}
/* Copied from nautilus-program-choosing.c */
extern char **environ;
/* Cut and paste from gdkspawn-x11.c */
static gchar **
my_gdk_spawn_make_environment_for_screen (GdkScreen *screen,
gchar **envp)
{
gchar **retval = NULL;
gchar *display_name;
gint i, j = 0, env_len;
g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
if (envp == NULL)
envp = environ;
env_len = g_strv_length (envp);
retval = g_new (char *, env_len + 3);
display_name = gdk_screen_make_display_name (screen);
for (i = 0; envp[i] != NULL; i++)
if (!g_str_has_prefix (envp[i], "DISPLAY=") &&
!g_str_has_prefix (envp[i], EPHY_UUID_ENVVAR "="))
retval[j++] = g_strdup (envp[i]);
retval[j++] = g_strconcat ("DISPLAY=", display_name, NULL);
retval[j++] = g_strdup (EPHY_UUID_ENVSTRING);
retval[j] = NULL;
g_free (display_name);
return retval;
}
static void
sn_error_trap_push (SnDisplay *display,
Display *xdisplay)
{
gdk_error_trap_push ();
}
static void
sn_error_trap_pop (SnDisplay *display,
Display *xdisplay)
{
gdk_error_trap_pop ();
}
static char **
make_spawn_environment_for_sn_context (SnLauncherContext *sn_context,
char **envp)
{
char **retval;
int i, j, len;
retval = NULL;
if (envp == NULL) {
envp = environ;
}
len = g_strv_length (envp);
retval = g_new (char *, len + 3);
for (i = 0, j = 0; envp[i] != NULL; i++) {
if (!g_str_has_prefix (envp[i], "DESKTOP_STARTUP_ID=") &&
!g_str_has_prefix (envp[i], EPHY_UUID_ENVVAR "=")) {
retval[j++] = g_strdup (envp[i]);
}
}
retval[j++] = g_strdup_printf ("DESKTOP_STARTUP_ID=%s",
sn_launcher_context_get_startup_id (sn_context));
retval[j++] = g_strdup (EPHY_UUID_ENVSTRING);
retval[j] = NULL;
return retval;
}
/* This should be fairly long, as it's confusing to users if a startup
* ends when it shouldn't (it appears that the startup failed, and
* they have to relaunch the app). Also the timeout only matters when
* there are bugs and apps don't end their own startup sequence.
*
* This timeout is a "last resort" timeout that ignores whether the
* startup sequence has shown activity or not. Metacity and the
* tasklist have smarter, and correspondingly able-to-be-shorter
* timeouts. The reason our timeout is dumb is that we don't monitor
* the sequence (don't use an SnMonitorContext)
*/
#define STARTUP_TIMEOUT_LENGTH (30 /* seconds */ * 1000)
typedef struct
{
GdkScreen *screen;
GSList *contexts;
guint timeout_id;
} StartupTimeoutData;
static void
free_startup_timeout (void *data)
{
StartupTimeoutData *std;
std = data;
g_slist_foreach (std->contexts,
(GFunc) sn_launcher_context_unref,
NULL);
g_slist_free (std->contexts);
if (std->timeout_id != 0) {
g_source_remove (std->timeout_id);
std->timeout_id = 0;
}
g_free (std);
}
static gboolean
startup_timeout (void *data)
{
StartupTimeoutData *std;
GSList *tmp;
GTimeVal now;
int min_timeout;
std = data;
min_timeout = STARTUP_TIMEOUT_LENGTH;
g_get_current_time (&now);
tmp = std->contexts;
while (tmp != NULL) {
SnLauncherContext *sn_context;
GSList *next;
long tv_sec, tv_usec;
double elapsed;
sn_context = tmp->data;
next = tmp->next;
sn_launcher_context_get_last_active_time (sn_context,
&tv_sec, &tv_usec);
elapsed =
((((double)now.tv_sec - tv_sec) * G_USEC_PER_SEC +
(now.tv_usec - tv_usec))) / 1000.0;
if (elapsed >= STARTUP_TIMEOUT_LENGTH) {
std->contexts = g_slist_remove (std->contexts,
sn_context);
sn_launcher_context_complete (sn_context);
sn_launcher_context_unref (sn_context);
} else {
min_timeout = MIN (min_timeout, (STARTUP_TIMEOUT_LENGTH - elapsed));
}
tmp = next;
}
if (std->contexts == NULL) {
std->timeout_id = 0;
} else {
std->timeout_id = g_timeout_add (min_timeout,
startup_timeout,
std);
}
/* always remove this one, but we may have reinstalled another one. */
return FALSE;
}
static void
add_startup_timeout (GdkScreen *screen,
SnLauncherContext *sn_context)
{
StartupTimeoutData *data;
data = g_object_get_data (G_OBJECT (screen), "nautilus-startup-data");
if (data == NULL) {
data = g_new (StartupTimeoutData, 1);
data->screen = screen;
data->contexts = NULL;
data->timeout_id = 0;
g_object_set_data_full (G_OBJECT (screen), "nautilus-startup-data",
data, free_startup_timeout);
}
sn_launcher_context_ref (sn_context);
data->contexts = g_slist_prepend (data->contexts, sn_context);
if (data->timeout_id == 0) {
data->timeout_id = g_timeout_add (STARTUP_TIMEOUT_LENGTH,
startup_timeout,
data);
}
}
gboolean
ephy_file_launch_application (GnomeVFSMimeApplication *application,
const char *parameter,
guint32 user_time)
{
GdkScreen *screen;
GList *uris = NULL;
char *uri;
char **envp;
GnomeVFSResult result;
SnLauncherContext *sn_context;
SnDisplay *sn_display;
g_return_val_if_fail (application != NULL, FALSE);
g_return_val_if_fail (parameter != NULL, FALSE);
uri = gnome_vfs_make_uri_canonical (parameter);
if (uri == NULL) return FALSE;
uris = g_list_prepend (NULL, uri);
screen = gdk_screen_get_default ();
envp = my_gdk_spawn_make_environment_for_screen (screen, NULL);
sn_display = sn_display_new (gdk_display,
sn_error_trap_push,
sn_error_trap_pop);
/* Only initiate notification if application supports it. */
if (gnome_vfs_mime_application_supports_startup_notification (application))
{
char *name;
sn_context = sn_launcher_context_new (sn_display,
screen ? gdk_screen_get_number (screen) :
DefaultScreen (gdk_display));
name = g_filename_display_basename (uri);
if (name != NULL) {
char *description;
sn_launcher_context_set_name (sn_context, name);
/* FIXME: i18n after string freeze! */
description = g_strdup_printf ("Opening %s", name);
sn_launcher_context_set_description (sn_context, description);
g_free (name);
g_free (description);
}
if (!sn_launcher_context_get_initiated (sn_context)) {
const char *binary_name;
char **old_envp;
binary_name = gnome_vfs_mime_application_get_binary_name (application);
sn_launcher_context_set_binary_name (sn_context,
binary_name);
sn_launcher_context_initiate (sn_context,
g_get_prgname () ? g_get_prgname () : "unknown",
binary_name,
(Time) user_time);
old_envp = envp;
envp = make_spawn_environment_for_sn_context (sn_context, envp);
g_strfreev (old_envp);
}
} else {
sn_context = NULL;
}
result = gnome_vfs_mime_application_launch_with_env (application, uris, envp);
if (sn_context != NULL) {
if (result != GNOME_VFS_OK) {
sn_launcher_context_complete (sn_context); /* end sequence */
} else {
add_startup_timeout (screen ? screen :
gdk_display_get_default_screen (gdk_display_get_default ()),
sn_context);
}
sn_launcher_context_unref (sn_context);
}
sn_display_unref (sn_display);
g_strfreev (envp);
g_list_foreach (uris, (GFunc) g_free,NULL);
g_list_free (uris);
if (result != GNOME_VFS_OK)
{
g_warning ("Cannot launch application '%s'\n",
gnome_vfs_mime_application_get_name (application));
}
return result == GNOME_VFS_OK;
}
/* End cut-paste-adapt from nautilus */
static int
launch_desktop_item (const char *desktop_file,
const char *parameter,
guint32 user_time,
GError **error)
{
GnomeDesktopItem *item = NULL;
GList *uris = NULL;
char *canonical;
int ret = -1;
char *envp[2] = { EPHY_UUID_ENVSTRING, NULL };
item = gnome_desktop_item_new_from_file (desktop_file, 0, NULL);
if (item == NULL) return FALSE;
if (parameter != NULL)
{
canonical = gnome_vfs_make_uri_canonical (parameter);
uris = g_list_append (uris, canonical);
}
gnome_desktop_item_set_launch_time (item, user_time);
ret = gnome_desktop_item_launch_with_env (item, uris, 0, envp, error);
g_list_foreach (uris, (GFunc) g_free, NULL);
g_list_free (uris);
gnome_desktop_item_unref (item);
return ret;
}
gboolean
ephy_file_launch_desktop_file (const char *filename,
guint32 user_time)
{
GError *error = NULL;
const char * const *dirs;
char *path = NULL;
int i, ret = -1;
dirs = g_get_system_data_dirs ();
if (dirs == NULL) return FALSE;
for (i = 0; dirs[i] != NULL; i++)
{
path = g_build_filename (dirs[i], "applications", filename, NULL);
if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) break;
g_free (path);
path = NULL;
}
if (path != NULL)
{
ret = launch_desktop_item (path, NULL, user_time, &error);
if (ret == -1 || error != NULL)
{
g_warning ("Cannot launch desktop item '%s': %s\n",
path, error ? error->message : "(unknown error)");
g_clear_error (&error);
}
g_free (path);
}
return ret >= 0;
}
gboolean
ephy_file_launch_handler (const char *mime_type,
const char *address,
guint32 user_time)
{
GnomeVFSMimeApplication *app = NULL;
GnomeVFSFileInfo *info = NULL;
char *canonical;
gboolean ret = FALSE;
g_return_val_if_fail (address != NULL, FALSE);
canonical = gnome_vfs_make_uri_canonical (address);
if (canonical == NULL) return FALSE;
if (mime_type != NULL)
{
app = gnome_vfs_mime_get_default_application (mime_type);
}
else
{
/* Sniff mime type and check if it's safe to open */
info = gnome_vfs_file_info_new ();
if (gnome_vfs_get_file_info (canonical, info,
GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE) == GNOME_VFS_OK &&
(info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) &&
info->mime_type != NULL &&
info->mime_type[0] != '\0' &&
ephy_file_check_mime (info->mime_type) == EPHY_MIME_PERMISSION_SAFE)
{
/* FIXME rename tmp file to right extension ? */
app = gnome_vfs_mime_get_default_application (info->mime_type);
}
gnome_vfs_file_info_unref (info);
}
if (app != NULL)
{
ret = ephy_file_launch_application (app, address, user_time);
gnome_vfs_mime_application_free (app);
}
else
{
/* FIXME: warn user? */
g_warning ("No handler for found or file type is unsafe!\n");
}
g_free (canonical);
return ret;
}
struct _EphyFileMonitor
{
GnomeVFSMonitorHandle *handle;
EphyFileMonitorFunc callback;
EphyFileMonitorDelayFunc delay_func;
gpointer user_data;
char *uri;
guint delay;
guint timeout_id;
guint ticks;
GnomeVFSMonitorEventType type;
};
static gboolean
ephy_file_monitor_timeout_cb (EphyFileMonitor *monitor)
{
if (monitor->ticks > 0)
{
monitor->ticks--;
/* Run again */
return TRUE;
}
if (monitor->delay_func &&
monitor->delay_func (monitor, monitor->user_data))
{
monitor->ticks = DELAY_MAX_TICKS / 2;
/* Run again */
return TRUE;
}
monitor->timeout_id = 0;
monitor->callback (monitor, monitor->uri, monitor->type, monitor->user_data);
/* don't run again */
return FALSE;
}
static void
ephy_file_monitor_cb (GnomeVFSMonitorHandle *handle,
const char *monitor_uri,
const char *info_uri,
GnomeVFSMonitorEventType event_type,
EphyFileMonitor *monitor)
{
LOG ("File '%s' has changed, scheduling reload", monitor_uri);
switch (event_type)
{
case GNOME_VFS_MONITOR_EVENT_CHANGED:
monitor->ticks = INITIAL_TICKS;
/* fall-through */
case GNOME_VFS_MONITOR_EVENT_CREATED:
/* We make a lot of assumptions here, but basically we know
* that we just have to reload, by construction.
* Delay the reload a little bit so we don't endlessly
* reload while a file is written.
*/
monitor->type = event_type;
if (monitor->ticks == 0)
{
monitor->ticks = 1;
}
else
{
/* Exponential backoff */
monitor->ticks = MIN (monitor->ticks * 2,
DELAY_MAX_TICKS);
}
if (monitor->timeout_id == 0)
{
monitor->timeout_id =
g_timeout_add (monitor->delay,
(GSourceFunc) ephy_file_monitor_timeout_cb,
monitor);
}
break;
case GNOME_VFS_MONITOR_EVENT_DELETED:
if (monitor->timeout_id != 0)
{
g_source_remove (monitor->timeout_id);
monitor->timeout_id = 0;
}
monitor->ticks = 0;
monitor->callback (monitor, monitor->uri, event_type, monitor->user_data);
break;
case GNOME_VFS_MONITOR_EVENT_STARTEXECUTING:
case GNOME_VFS_MONITOR_EVENT_STOPEXECUTING:
case GNOME_VFS_MONITOR_EVENT_METADATA_CHANGED:
default:
break;
}
}
EphyFileMonitor *
ephy_file_monitor_add (const char *uri,
GnomeVFSMonitorType monitor_type,
guint delay,
EphyFileMonitorFunc callback,
EphyFileMonitorDelayFunc delay_func,
gpointer user_data)
{
EphyFileMonitor *monitor;
g_return_val_if_fail (uri != NULL, NULL);
g_return_val_if_fail (callback, NULL);
monitor = g_new (EphyFileMonitor, 1);
monitor->callback = callback;
monitor->delay_func = delay_func;
monitor->user_data = user_data;
monitor->uri = g_strdup (uri);
monitor->delay = delay;
monitor->ticks = 0;
monitor->timeout_id = 0;
if (gnome_vfs_monitor_add (&monitor->handle, uri, monitor_type,
(GnomeVFSMonitorCallback) ephy_file_monitor_cb,
monitor) != GNOME_VFS_OK)
{
LOG ("Failed to add file monitor for '%s'", uri);
g_free (monitor->uri);
g_free (monitor);
return NULL;
}
LOG ("File monitor for '%s' added", uri);
return monitor;
}
void
ephy_file_monitor_cancel (EphyFileMonitor *monitor)
{
g_return_if_fail (monitor != NULL);
g_return_if_fail (monitor->handle != NULL);
g_return_if_fail (monitor->uri != NULL);
LOG ("Cancelling file monitor for '%s'", monitor->uri);
gnome_vfs_monitor_cancel (monitor->handle);
if (monitor->timeout_id != 0)
{
g_source_remove (monitor->timeout_id);
}
g_free (monitor->uri);
g_free (monitor);
}
|