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
|
/*
* Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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; version 2 of the License.
*
* NetSurf 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, see <http://www.gnu.org/licenses/>.
*/
/**
* \file generic data viewer implementation.
*
* This viewer can be used for utf-8 encoded chunk of data. Thie data
* might be page source or the debugging of dom or box trees. It will
* show the data in a tab, window or editor as per user configuration.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "utils/log.h"
#include "utils/nsoption.h"
#include "utils/utf8.h"
#include "utils/messages.h"
#include "utils/url.h"
#include "utils/utils.h"
#include "utils/file.h"
#include "utils/filepath.h"
#include "desktop/netsurf.h"
#include "desktop/browser.h"
#include "render/html.h"
#include "content/hlcache.h"
#include "content/content.h"
#include "gtk/dialogs/about.h"
#include "gtk/fetch.h"
#include "gtk/compat.h"
#include "gtk/gui.h"
#include "gtk/viewdata.h"
struct nsgtk_viewdata_ctx {
char *data;
size_t data_len;
char *filename;
GtkBuilder *builder; /**< The gtk builder that built the widgets. */
GtkWindow *window; /**< handle to gtk window (builder holds reference) */
GtkTextView *gv; /**< handle to gtk text view (builder holds reference) */
struct nsgtk_viewdata_ctx *next;
struct nsgtk_viewdata_ctx *prev;
};
struct menu_events {
const char *widget;
GCallback handler;
};
static struct nsgtk_viewdata_ctx *nsgtk_viewdata_list = NULL;
static char viewdata_zoomlevel = 10;
#define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
#define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate( \
GtkMenuItem *widget, gpointer g)
MENUPROTO(viewdata_save_as);
MENUPROTO(viewdata_print);
MENUPROTO(viewdata_close);
MENUPROTO(viewdata_select_all);
MENUPROTO(viewdata_cut);
MENUPROTO(viewdata_copy);
MENUPROTO(viewdata_paste);
MENUPROTO(viewdata_delete);
MENUPROTO(viewdata_zoom_in);
MENUPROTO(viewdata_zoom_out);
MENUPROTO(viewdata_zoom_normal);
MENUPROTO(viewdata_about);
static struct menu_events viewdata_menu_events[] = {
MENUEVENT(viewdata_save_as),
MENUEVENT(viewdata_print),
MENUEVENT(viewdata_close),
MENUEVENT(viewdata_select_all),
MENUEVENT(viewdata_cut),
MENUEVENT(viewdata_copy),
MENUEVENT(viewdata_paste),
MENUEVENT(viewdata_delete),
MENUEVENT(viewdata_zoom_in),
MENUEVENT(viewdata_zoom_out),
MENUEVENT(viewdata_zoom_normal),
MENUEVENT(viewdata_about),
{NULL, NULL}
};
static void nsgtk_attach_viewdata_menu_handlers(GtkBuilder *xml, gpointer g)
{
struct menu_events *event = viewdata_menu_events;
while (event->widget != NULL)
{
GtkWidget *w = GTK_WIDGET(gtk_builder_get_object(xml, event->widget));
g_signal_connect(G_OBJECT(w), "activate", event->handler, g);
event++;
}
}
static gboolean nsgtk_viewdata_destroy_event(GtkBuilder *window, gpointer g)
{
struct nsgtk_viewdata_ctx *vdctx = (struct nsgtk_viewdata_ctx *)g;
if (vdctx->next != NULL) {
vdctx->next->prev = vdctx->prev;
}
if (vdctx->prev != NULL) {
vdctx->prev->next = vdctx->next;
} else {
nsgtk_viewdata_list = vdctx->next;
}
/* release the data */
free(vdctx->data);
/* free the builder */
g_object_unref(G_OBJECT(vdctx->builder));
/* free the context structure */
free(vdctx);
return FALSE;
}
static gboolean nsgtk_viewdata_delete_event(GtkWindow * window, gpointer g)
{
return FALSE;
}
static void nsgtk_viewdata_file_save(GtkWindow *parent, const char *filename,
const char *data, size_t data_size)
{
FILE *f;
GtkWidget *notif;
GtkWidget *label;
f = fopen(filename, "w+");
if (f != NULL) {
fwrite(data, data_size, 1, f);
fclose(f);
return;
}
/* inform user of faliure */
notif = gtk_dialog_new_with_buttons(messages_get("gtkSaveFailedTitle"),
parent,
GTK_DIALOG_MODAL, GTK_STOCK_OK,
GTK_RESPONSE_NONE, NULL);
g_signal_connect_swapped(notif, "response",
G_CALLBACK(gtk_widget_destroy), notif);
label = gtk_label_new(messages_get("gtkSaveFailed"));
gtk_container_add(GTK_CONTAINER(nsgtk_dialog_get_content_area(GTK_DIALOG(notif))), label);
gtk_widget_show_all(notif);
}
gboolean nsgtk_on_viewdata_save_as_activate(GtkMenuItem *widget, gpointer g)
{
struct nsgtk_viewdata_ctx *nsg = (struct nsgtk_viewdata_ctx *) g;
GtkWidget *fc;
fc = gtk_file_chooser_dialog_new(messages_get("gtkSaveFile"),
nsg->window,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE,
GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), nsg->filename);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(fc),
TRUE);
if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT) {
char *filename;
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
nsgtk_viewdata_file_save(nsg->window, filename, nsg->data, nsg->data_len);
g_free(filename);
}
gtk_widget_destroy(fc);
return TRUE;
}
gboolean nsgtk_on_viewdata_print_activate( GtkMenuItem *widget, gpointer g)
{
/* correct printing */
return TRUE;
}
gboolean nsgtk_on_viewdata_close_activate( GtkMenuItem *widget, gpointer g)
{
struct nsgtk_viewdata_ctx *nsg = (struct nsgtk_viewdata_ctx *) g;
gtk_widget_destroy(GTK_WIDGET(nsg->window));
return TRUE;
}
gboolean nsgtk_on_viewdata_select_all_activate (GtkMenuItem *widget, gpointer g)
{
struct nsgtk_viewdata_ctx *nsg = (struct nsgtk_viewdata_ctx *) g;
GtkTextBuffer *buf = gtk_text_view_get_buffer(nsg->gv);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(buf, &start, &end);
gtk_text_buffer_select_range(buf, &start, &end);
return TRUE;
}
gboolean nsgtk_on_viewdata_cut_activate(GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
gboolean nsgtk_on_viewdata_copy_activate(GtkMenuItem *widget, gpointer g)
{
struct nsgtk_viewdata_ctx *nsg = (struct nsgtk_viewdata_ctx *) g;
GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv));
gtk_text_buffer_copy_clipboard(buf,
gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
return TRUE;
}
gboolean nsgtk_on_viewdata_paste_activate(GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
gboolean nsgtk_on_viewdata_delete_activate(GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
static void nsgtk_viewdata_update_zoomlevel(gpointer g)
{
struct nsgtk_viewdata_ctx *nsg;
GtkTextBuffer *buf;
GtkTextTagTable *tab;
GtkTextTag *tag;
nsg = nsgtk_viewdata_list;
while (nsg) {
if (nsg->gv) {
buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv));
tab = gtk_text_buffer_get_tag_table(
GTK_TEXT_BUFFER(buf));
tag = gtk_text_tag_table_lookup(tab, "zoomlevel");
if (!tag) {
tag = gtk_text_tag_new("zoomlevel");
gtk_text_tag_table_add(tab, GTK_TEXT_TAG(tag));
}
gdouble fscale = ((gdouble) viewdata_zoomlevel) / 10;
g_object_set(GTK_TEXT_TAG(tag), "scale", fscale, NULL);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf),
&start, &end);
gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf),
&start, &end);
gtk_text_buffer_apply_tag(GTK_TEXT_BUFFER(buf),
GTK_TEXT_TAG(tag), &start, &end);
}
nsg = nsg->next;
}
}
gboolean nsgtk_on_viewdata_zoom_in_activate(GtkMenuItem *widget, gpointer g)
{
viewdata_zoomlevel++;
nsgtk_viewdata_update_zoomlevel(g);
return TRUE;
}
gboolean nsgtk_on_viewdata_zoom_out_activate(GtkMenuItem *widget, gpointer g)
{
if (viewdata_zoomlevel > 1) {
viewdata_zoomlevel--;
nsgtk_viewdata_update_zoomlevel(g);
}
return TRUE;
}
gboolean nsgtk_on_viewdata_zoom_normal_activate(GtkMenuItem *widget, gpointer g)
{
viewdata_zoomlevel = 10;
nsgtk_viewdata_update_zoomlevel(g);
return TRUE;
}
gboolean nsgtk_on_viewdata_about_activate(GtkMenuItem *widget, gpointer g)
{
struct nsgtk_viewdata_ctx *nsg = (struct nsgtk_viewdata_ctx *) g;
nsgtk_about_dialog_init(nsg->window, netsurf_version);
return TRUE;
}
/**
* View the data in a gtk text window.
*/
static nserror
window_init(const char *title,
const char *filename,
char *ndata,
size_t ndata_len)
{
GError* error = NULL;
GtkWindow *window;
GtkWidget *cutbutton;
GtkWidget *pastebutton;
GtkWidget *deletebutton;
GtkWidget *printbutton;
GtkTextView *dataview;
PangoFontDescription *fontdesc;
GtkTextBuffer *tb;
struct nsgtk_viewdata_ctx *newctx;
newctx = malloc(sizeof(struct nsgtk_viewdata_ctx));
if (newctx == NULL) {
return NSERROR_NOMEM;
}
newctx->builder = gtk_builder_new();
if (newctx->builder == NULL) {
free(newctx);
return NSERROR_INIT_FAILED;
}
if (!gtk_builder_add_from_file(newctx->builder,
glade_file_location->viewdata,
&error)) {
LOG(("Couldn't load builder file: %s", error->message));
g_error_free(error);
free(newctx);
return NSERROR_INIT_FAILED;
}
window = GTK_WINDOW(gtk_builder_get_object(newctx->builder, "ViewDataWindow"));
if (window == NULL) {
LOG(("Unable to find window in builder "));
/* free the builder */
g_object_unref(G_OBJECT(newctx->builder));
/* free the context structure */
free(newctx);
return NSERROR_INIT_FAILED;
}
cutbutton = GTK_WIDGET(gtk_builder_get_object(newctx->builder, "viewdata_cut"));
pastebutton = GTK_WIDGET(gtk_builder_get_object(newctx->builder, "viewdata_paste"));
deletebutton = GTK_WIDGET(gtk_builder_get_object(newctx->builder, "viewdata_delete"));
printbutton = GTK_WIDGET(gtk_builder_get_object(newctx->builder, "viewdata_print"));
gtk_widget_set_sensitive(cutbutton, FALSE);
gtk_widget_set_sensitive(pastebutton, FALSE);
gtk_widget_set_sensitive(deletebutton, FALSE);
/* for now */
gtk_widget_set_sensitive(printbutton, FALSE);
newctx->filename = strdup(filename);
newctx->data = ndata;
newctx->data_len = ndata_len;
newctx->window = window;
newctx->next = nsgtk_viewdata_list;
newctx->prev = NULL;
if (nsgtk_viewdata_list != NULL) {
nsgtk_viewdata_list->prev = newctx;
}
nsgtk_viewdata_list = newctx;
nsgtk_attach_viewdata_menu_handlers(newctx->builder, newctx);
gtk_window_set_title(window, title);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(nsgtk_viewdata_destroy_event),
newctx);
g_signal_connect(G_OBJECT(window), "delete-event",
G_CALLBACK(nsgtk_viewdata_delete_event),
newctx);
dataview = GTK_TEXT_VIEW(gtk_builder_get_object(newctx->builder,
"viewdata_view"));
fontdesc = pango_font_description_from_string("Monospace 8");
newctx->gv = dataview;
nsgtk_widget_modify_font(GTK_WIDGET(dataview), fontdesc);
tb = gtk_text_view_get_buffer(dataview);
gtk_text_buffer_set_text(tb, newctx->data, -1);
gtk_widget_show(GTK_WIDGET(window));
return NSERROR_OK;
}
/**
* open a window to dispaly an existing file.
*/
static nserror
window_init_fname(const char *title,
const char *leafname,
const char *filename)
{
nserror ret;
FILE *f;
char *ndata;
long tell_len;
size_t ndata_len;
f = fopen(filename, "r");
if (f == NULL) {
return NSERROR_NOT_FOUND;
}
if (fseek(f, 0, SEEK_END) != 0) {
fclose(f);
return NSERROR_BAD_SIZE;
}
tell_len = ftell(f);
if (tell_len == -1) {
fclose(f);
return NSERROR_BAD_SIZE;
}
if (fseek(f, 0, SEEK_SET) != 0) {
fclose(f);
return NSERROR_BAD_SIZE;
}
ndata = malloc(tell_len);
ndata_len = fread(ndata, 1, tell_len, f);
fclose(f);
ret = window_init(title, leafname, ndata, ndata_len);
return ret;
}
/**
* open a new tab from an existing file.
*/
static nserror
tab_init_fname(const char *title,
const char *leafname,
const char *fname)
{
nsurl *url;
nserror ret;
/* Open tab on temporary file */
ret = netsurf_path_to_nsurl(fname, &url);
if (ret != NSERROR_OK) {
return ret;
}
/* open tab on temportary file */
ret = browser_window_create(BW_CREATE_TAB | BW_CREATE_HISTORY, url, NULL, NULL, NULL);
nsurl_unref(url);
if (ret != NSERROR_OK) {
return ret;
}
return NSERROR_OK;
}
/**
* create a new tab from data.
*/
static nserror
tab_init(const char *title,
const char *leafname,
char *ndata,
size_t ndata_len)
{
nserror ret;
gchar *fname;
gint handle;
FILE *f;
handle = g_file_open_tmp("nsgtkdataXXXXXX", &fname, NULL);
if ((handle == -1) || (fname == NULL)) {
return NSERROR_SAVE_FAILED;
}
close(handle); /* in case it was binary mode */
/* save data to temporary file */
f = fopen(fname, "w");
if (f == NULL) {
warn_user(messages_get("gtkSourceTabError"), 0);
g_free(fname);
return NSERROR_SAVE_FAILED;
}
fprintf(f, "%s", ndata);
fclose(f);
ret = tab_init_fname(title, leafname, fname);
if (ret == NSERROR_OK) {
free(ndata);
}
g_free(fname);
return ret;
}
/**
* Build string vector of search path.
*
* ${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
*
* $XDG_DATA_HOME if empty use $HOME/.local/share
*
* XDG_DATA_DIRS if empty use /usr/local/share/:/usr/share/
*
* \return string vector of search pathnames or NULL on error.
*/
static char** xdg_data_strvec(void)
{
const char *xdg_data_dirs;
const char *xdg_data_home;
const char *home_dir;
char *xdg_data_path;
int xdg_data_size;
char **svec;
xdg_data_dirs = getenv("XDG_DATA_DIRS");
if ((xdg_data_dirs == NULL) ||
(*xdg_data_dirs == 0) ||
(strlen(xdg_data_dirs) > 4096)) {
xdg_data_dirs = "/usr/local/share/:/usr/share/";
}
xdg_data_home = getenv("XDG_DATA_HOME");
if ((xdg_data_home == NULL) ||
(*xdg_data_home == 0) ||
(strlen(xdg_data_home) > 4096)) {
/* $XDG_DATA_HOME is empty use $HOME/.local/share */
home_dir = getenv("HOME");
if ((home_dir == NULL) ||
(*home_dir == 0) ||
(strlen(home_dir) > 4096)) {
xdg_data_path = strdup(xdg_data_dirs);
} else {
xdg_data_size = strlen(home_dir) +
SLEN("/.local/share:") +
strlen(xdg_data_dirs) + 1;
xdg_data_path = malloc(xdg_data_size);
snprintf(xdg_data_path, xdg_data_size ,
"%s/.local/share/:%s",
home_dir, xdg_data_dirs);
}
} else {
xdg_data_size = strlen(xdg_data_home) +
strlen(xdg_data_dirs) + 2;
xdg_data_path = malloc(xdg_data_size);
snprintf(xdg_data_path, xdg_data_size , "%s:%s",
xdg_data_home, xdg_data_dirs);
}
LOG(("%s", xdg_data_path));
svec = filepath_path_to_strvec(xdg_data_path);
free(xdg_data_path);
return svec;
}
/**
* Search application defaults file for matching mime type.
*
* create filename form path and applications/defaults.list
*
* look for [Default Applications]
* search lines looking like mime/type=Desktop
*
* \param path The base path.
* \param mimetype The mimetype to search for.
* \return The desktop file associated with the mime type or NULL if not found.
*/
static char *xdg_get_default_app(const char *path, const char *mimetype)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t rd;
int fname_len;
char *fname;
int mimetype_len;
char *ret = NULL;
fname_len = strlen(path) + SLEN("/applications/defaults.list") + 1;
fname = malloc(fname_len);
snprintf(fname, fname_len, "%s/applications/defaults.list", path);
LOG(("Checking %s", fname));
fp = fopen(fname, "r");
free(fname);
if (fp == NULL) {
return NULL;
}
mimetype_len = strlen(mimetype);
while ((rd = getline(&line, &len, fp)) != -1) {
/* line includes line endings if present, remove them */
while ((line[rd - 1] == '\n') || (line[rd - 1] == '\r')) {
rd--;
}
line[rd] = 0;
/* look for mimetype */
if ((rd > mimetype_len) &&
(line[mimetype_len] == '=') &&
(strncmp(line, mimetype, mimetype_len) == 0)) {
ret = strdup(line + mimetype_len + 1);
LOG(("Found line match for %s length %zu\n", mimetype, rd));
LOG(("Result %s", ret));
break;
}
}
free(line);
fclose(fp);
return ret;
}
/**
* Search desktop file for an Exec line.
*
* search path is combined with applications/application.desktop to
* create a filename.
*
* Desktop file format http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
*
* \todo The parsing of the desktop file is badly incomplete and needs
* improving. For example the handling of the = delimiter is wrong and
* selection from the "Desktop Entry" group is completely absent.
*
*/
static char *xdg_get_exec_cmd(const char *path, const char *desktop)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t rd;
int fname_len;
char *fname;
char *ret = NULL;
fname_len = strlen(path) + SLEN("/applications/") + strlen(desktop) + 1;
fname = malloc(fname_len);
snprintf(fname, fname_len, "%s/applications/%s", path, desktop);
LOG(("Checking %s", fname));
fp = fopen(fname, "r");
free(fname);
if (fp == NULL) {
return NULL;
}
while ((rd = getline(&line, &len, fp)) != -1) {
/* line includes line endings if present, remove them */
while ((line[rd - 1] == '\n') || (line[rd - 1] == '\r')) {
rd--;
}
line[rd] = 0;
/* look for mimetype */
if ((rd > (ssize_t)SLEN("Exec=")) &&
(strncmp(line, "Exec=", SLEN("Exec=")) == 0)) {
ret = strdup(line + SLEN("Exec="));
LOG(("Found Exec length %zu", rd));
LOG(("Result %s", ret));
break;
}
}
free(line);
fclose(fp);
return ret;
}
static char *exec_arg(const char *arg, int len, const char *fname)
{
char *res = NULL;
if (*arg == '%') {
arg++;
if ((*arg == 'f') || (*arg == 'F') ||
(*arg == 'u') || (*arg == 'U')) {
res = strdup(fname);
}
} else {
res = calloc(1, len + 1);
if (res != NULL) {
memcpy(res, arg, len);
}
}
return res;
}
/**
* Build vector for executing app.
*/
static char **build_exec_argv(const char *fname, const char *exec_cmd)
{
char **argv;
const char *start; /* current arguments start */
const char *cur; /* current ptr within exec cmd */
int aidx = 0; /* argv index */
argv = calloc(10, sizeof(char *));
if (argv == NULL) {
return NULL;
}
cur = exec_cmd;
while (*cur != 0) {
/* skip whitespace */
while ((*cur != 0) && (*cur == ' ')) {
cur++;
}
if (*cur == 0) {
break;
}
start = cur;
/* find end of element */
while ((*cur != 0) && (*cur != ' ')) {
cur++;
}
argv[aidx] = exec_arg(start, cur - start, fname);
if (argv[aidx] != NULL) {
LOG(("adding \"%s\"", argv[aidx]));
aidx++;
}
}
/* if no arguments were found there was nothing to execute */
if (aidx == 0) {
free(argv);
return NULL;
}
return argv;
}
/**
* open an editor from an existing file.
*/
static nserror
editor_init_fname(const char *title,
const char *leafname,
const char *fname)
{
char **xdg_data_vec;
int veci;
char *def_app_desktop; /* desktop file of default app for mimetype */
char *exec_cmd;
char **argv;
/* build string vector of search path */
xdg_data_vec = xdg_data_strvec();
/* find user configured app for opening text/plain */
veci = 0;
while (xdg_data_vec[veci] != NULL) {
def_app_desktop = xdg_get_default_app(xdg_data_vec[veci],
"text/plain");
if (def_app_desktop != NULL) {
break;
}
veci++;
}
if (def_app_desktop == NULL) {
/* no default app */
filepath_free_strvec(xdg_data_vec);
return NSERROR_NOT_FOUND;
}
/* find app to execute */
veci = 0;
while (xdg_data_vec[veci] != NULL) {
exec_cmd = xdg_get_exec_cmd(xdg_data_vec[veci], def_app_desktop);
if (exec_cmd != NULL) {
break;
}
veci++;
}
free(def_app_desktop);
filepath_free_strvec(xdg_data_vec);
if (exec_cmd == NULL) {
/* no exec entry */
return NSERROR_NOT_FOUND;
}
/* build exec vector */
argv = build_exec_argv(fname, exec_cmd);
free(exec_cmd);
/* execute target app on saved data */
if (g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
NULL, NULL) != TRUE) {
return NSERROR_NOT_FOUND;
}
filepath_free_strvec(argv);
return NSERROR_OK;
}
/**
* open an editor with data.
*/
static nserror
editor_init(const char *title,
const char *leafname,
char *ndata,
size_t ndata_len)
{
nserror ret;
gchar *fname;
gint handle;
FILE *f;
handle = g_file_open_tmp("nsgtkdataXXXXXX", &fname, NULL);
if ((handle == -1) || (fname == NULL)) {
return NSERROR_SAVE_FAILED;
}
close(handle); /* in case it was binary mode */
/* save data to temporary file */
f = fopen(fname, "w");
if (f == NULL) {
warn_user(messages_get("gtkSourceTabError"), 0);
g_free(fname);
return NSERROR_SAVE_FAILED;
}
fprintf(f, "%s", ndata);
fclose(f);
ret = editor_init_fname(title, leafname, fname);
if (ret == NSERROR_OK) {
free(ndata);
}
g_free(fname);
return ret;
}
/* exported interface documented in gtk/viewdata.h */
nserror
nsgtk_viewdata(const char *title,
const char *filename,
char *ndata,
size_t ndata_len)
{
nserror ret;
switch (nsoption_int(developer_view)) {
case 0:
ret = window_init(title, filename, ndata, ndata_len);
break;
case 1:
ret = tab_init(title, filename, ndata, ndata_len);
break;
case 2:
ret = editor_init(title, filename, ndata, ndata_len);
break;
default:
ret = NSERROR_BAD_PARAMETER;
break;
}
if (ret != NSERROR_OK) {
/* release the data */
free(ndata);
}
return ret;
}
/* exported interface documented in gtk/viewdata.h */
nserror
nsgtk_viewfile(const char *title,
const char *leafname,
const char *filename)
{
nserror ret;
switch (nsoption_int(developer_view)) {
case 0:
ret = window_init_fname(title, leafname, filename);
break;
case 1:
ret = tab_init_fname(title, leafname, filename);
break;
case 2:
ret = editor_init_fname(title, leafname, filename);
break;
default:
ret = NSERROR_BAD_PARAMETER;
break;
}
return ret;
}
|