Few optimizations: avoid strlen when string length is known (read()>0 return);
[claws.git] / src / prefs_ext_prog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2013 Hiroyuki Yamamoto & the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include "utils.h"
36 #include "combobox.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39
40 #include "gtk/gtkutils.h"
41 #include "gtk/prefswindow.h"
42
43 #include "manage_window.h"
44
45 typedef struct _ExtProgPage
46 {
47         PrefsPage page;
48
49         GtkWidget *window;              /* do not modify */
50
51         GtkWidget *cmds_use_system_default_checkbtn;
52 #ifndef G_OS_WIN32
53         GtkWidget *uri_label;
54         GtkWidget *uri_combo;
55         GtkWidget *uri_entry;
56         
57 #endif
58         GtkWidget *exteditor_label;
59         GtkWidget *exteditor_combo;
60         GtkWidget *exteditor_entry;
61
62         GtkWidget *astextviewer_label;
63         GtkWidget *astextviewer_entry;
64 } ExtProgPage;
65
66 static void prefs_ext_prog_create_widget(PrefsPage *_page, GtkWindow *window, 
67                                   gpointer data)
68 {
69         ExtProgPage *prefs_ext_prog = (ExtProgPage *) _page;
70
71         GtkWidget *table;
72         GtkWidget *vbox;
73         GtkWidget *hint_label;
74         GtkWidget *table2;
75         GtkWidget *cmds_use_system_default_checkbtn;
76 #ifndef G_OS_WIN32
77         GtkWidget *uri_label;
78         GtkWidget *uri_combo;
79         GtkWidget *uri_entry;
80 #endif
81         GtkWidget *exteditor_label;
82         GtkWidget *exteditor_combo;
83         GtkWidget *exteditor_entry;
84         GtkWidget *astextviewer_label;
85         GtkWidget *astextviewer_entry;
86         int i = 0;
87         gchar *tmp;
88
89         table = gtk_table_new(2, 1, FALSE);
90         gtk_widget_show(table);
91         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
92         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
93         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
94
95         vbox = gtk_vbox_new(TRUE, 0);
96         gtk_widget_show(vbox);
97
98         gtk_table_attach(GTK_TABLE (table), vbox, 0, 1, 0, 1,
99                          (GtkAttachOptions) (GTK_SHRINK),
100                          (GtkAttachOptions) (0), 0, 0);
101
102         hint_label = gtk_label_new(_("%s will be replaced with file name / URI"));
103         gtk_label_set_justify (GTK_LABEL (hint_label), GTK_JUSTIFY_LEFT);
104         gtkut_widget_set_small_font_size (hint_label);
105         gtk_widget_show(hint_label);
106         gtk_box_pack_start(GTK_BOX (vbox),
107                            hint_label, FALSE, FALSE, 4);
108
109 #ifdef G_OS_UNIX
110         hint_label = gtk_label_new(_("For the text editor, %w will be replaced with GtkSocket ID"));
111         gtk_label_set_justify (GTK_LABEL (hint_label), GTK_JUSTIFY_LEFT);
112         gtkut_widget_set_small_font_size (hint_label);
113         gtk_widget_show(hint_label);
114         gtk_box_pack_start(GTK_BOX (vbox),
115                            hint_label, FALSE, FALSE, 4);
116 #endif
117
118         table2 = gtk_table_new(7, 2, FALSE);
119         gtk_widget_show(table2);
120         gtk_container_set_border_width(GTK_CONTAINER(table2), 8);
121         gtk_table_set_row_spacings(GTK_TABLE(table2), 4);
122         gtk_table_set_col_spacings(GTK_TABLE(table2), 8);
123         
124         gtk_table_attach(GTK_TABLE (table), table2, 0, 1, 1, 2,
125                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
126                          (GtkAttachOptions) (0), 0, 0);
127         
128         cmds_use_system_default_checkbtn = gtk_check_button_new_with_label(
129                 _("Use system defaults when possible")); 
130         
131         gtk_widget_show(cmds_use_system_default_checkbtn);
132         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cmds_use_system_default_checkbtn),
133                                         prefs_common.cmds_use_system_default);
134         tmp = g_find_program_in_path("xdg-open");
135         if (!tmp) {
136                 g_print("xdg-open not found\n");
137                 gtk_widget_set_sensitive(cmds_use_system_default_checkbtn, FALSE);
138                 gtk_widget_set_sensitive(cmds_use_system_default_checkbtn, FALSE);
139                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cmds_use_system_default_checkbtn),
140                                         FALSE);
141         } else
142                 g_free(tmp);
143
144         
145         gtk_table_attach(GTK_TABLE (table2), cmds_use_system_default_checkbtn, 0, 2, i, i+1,
146                          (GtkAttachOptions) (GTK_FILL),
147                          (GtkAttachOptions) (0), 0, 2);
148         
149 #ifndef G_OS_WIN32
150         uri_label = gtk_label_new (_("Web browser"));
151         gtk_widget_show(uri_label);
152         i++;
153         gtk_table_attach(GTK_TABLE (table2), uri_label, 0, 1, i, i+1,
154                          (GtkAttachOptions) (GTK_FILL),
155                          (GtkAttachOptions) (0), 0, 2);
156         gtk_label_set_justify(GTK_LABEL (uri_label), GTK_JUSTIFY_RIGHT);
157         gtk_misc_set_alignment(GTK_MISC (uri_label), 1, 0.5);
158
159         uri_combo = combobox_text_new(TRUE,
160                                DEFAULT_BROWSER_CMD,
161                                "galeon --new-tab '%s'",
162                                "galeon '%s'",
163                                "mozilla -remote 'openurl(%s,new-window)'",
164                                "netscape -remote 'openURL(%s, new-window)'",
165                                "netscape '%s'",
166                                "gnome-moz-remote --newwin '%s'",
167                                "kfmclient openURL '%s'",
168                                "opera -newwindow '%s'",
169                                "rxvt -e w3m '%s'",
170                                "rxvt -e lynx '%s'",
171                                NULL);
172         gtk_table_attach (GTK_TABLE (table2), uri_combo, 1, 2, i, i+1,
173                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
174
175         uri_entry = gtk_bin_get_child(GTK_BIN((uri_combo)));
176         gtk_entry_set_text(GTK_ENTRY(uri_entry), prefs_common.uri_cmd ? prefs_common.uri_cmd : "");
177 #endif  
178         exteditor_label = gtk_label_new (_("Text editor"));
179         gtk_widget_show(exteditor_label);
180
181         i++;
182         gtk_table_attach(GTK_TABLE (table2), exteditor_label, 0, 1, i, i+1,
183                          (GtkAttachOptions) (GTK_FILL),
184                          (GtkAttachOptions) (0), 0, 2);
185         gtk_label_set_justify(GTK_LABEL (exteditor_label), GTK_JUSTIFY_RIGHT);
186         gtk_misc_set_alignment(GTK_MISC (exteditor_label), 1, 0.5);
187
188         exteditor_combo = combobox_text_new(TRUE,
189                                         "gedit %s",
190                                         "kedit %s",
191                                         "mousepad %s",
192                                         "nedit %s",
193                                         "mgedit --no-fork %s",
194                                         "emacs %s",
195                                         "xemacs %s",
196 #ifdef G_OS_UNIX
197                                         "gvim -f --socketid %w %s",
198                                         "emacsclient --parent-id %w %s",
199 #endif
200                                         "kterm -e jed %s",
201                                         "kterm -e vi %s",
202                                         NULL);
203         gtk_table_attach (GTK_TABLE (table2), exteditor_combo, 1, 2, i, i+1,
204                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
205
206         exteditor_entry = gtk_bin_get_child(GTK_BIN((exteditor_combo)));
207         gtk_entry_set_text(GTK_ENTRY(exteditor_entry), 
208                            prefs_common.ext_editor_cmd ? prefs_common.ext_editor_cmd : "");
209
210         astextviewer_label = gtk_label_new(_("Command for 'Display as text'"));
211         gtk_widget_show(astextviewer_label);
212
213         i++;
214         gtk_table_attach(GTK_TABLE (table2), astextviewer_label, 0, 1, i, i+1,
215                          (GtkAttachOptions) (GTK_FILL),
216                          (GtkAttachOptions) (0), 0, 2);
217         gtk_label_set_justify(GTK_LABEL (astextviewer_label), GTK_JUSTIFY_RIGHT);
218         gtk_misc_set_alignment(GTK_MISC (astextviewer_label), 1, 0.5);
219
220         astextviewer_entry = gtk_entry_new ();
221         gtk_widget_show(astextviewer_entry);
222         CLAWS_SET_TIP(astextviewer_entry,
223                              _("This option enables MIME parts to be displayed in the "
224                                "message view via a script when using the 'Display as text' "
225                                "contextual menu item"));
226         
227         gtk_table_attach(GTK_TABLE (table2), astextviewer_entry, 1, 2, i, i+1,
228                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
229                          (GtkAttachOptions) (0), 0, 0);
230         gtk_entry_set_text(GTK_ENTRY(astextviewer_entry), 
231                            prefs_common.mime_textviewer ? prefs_common.mime_textviewer : "");
232
233 #ifndef G_OS_WIN32
234         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, uri_label);
235         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, uri_combo);
236 #endif
237 #if 0 /* we should do that, but it detaches the editor and breaks
238          compose.c's external composition. */
239         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, exteditor_label);
240         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, exteditor_combo);
241 #endif
242
243         prefs_ext_prog->window                  = GTK_WIDGET(window);
244 #ifndef G_OS_WIN32
245         prefs_ext_prog->uri_entry               = uri_entry;
246 #endif
247         prefs_ext_prog->exteditor_entry         = exteditor_entry;
248         prefs_ext_prog->astextviewer_entry      = astextviewer_entry;
249         prefs_ext_prog->cmds_use_system_default_checkbtn = cmds_use_system_default_checkbtn;
250         prefs_ext_prog->page.widget = table;
251 }
252
253 static void prefs_ext_prog_save(PrefsPage *_page)
254 {
255         ExtProgPage *ext_prog = (ExtProgPage *) _page;
256
257 #ifndef G_OS_WIN32
258         prefs_common.uri_cmd = gtk_editable_get_chars
259                 (GTK_EDITABLE(ext_prog->uri_entry), 0, -1);
260 #endif
261         prefs_common.ext_editor_cmd = gtk_editable_get_chars
262                 (GTK_EDITABLE(ext_prog->exteditor_entry), 0, -1);
263         prefs_common.mime_textviewer = gtk_editable_get_chars
264                 (GTK_EDITABLE(ext_prog->astextviewer_entry), 0, -1);
265         prefs_common.cmds_use_system_default = gtk_toggle_button_get_active
266                 (GTK_TOGGLE_BUTTON(ext_prog->cmds_use_system_default_checkbtn));
267 }
268
269 static void prefs_ext_prog_destroy_widget(PrefsPage *_page)
270 {
271         /* ExtProgPage *ext_prog = (ExtProgPage *) _page; */
272
273 }
274
275 ExtProgPage *prefs_ext_prog;
276
277 void prefs_ext_prog_init(void)
278 {
279         ExtProgPage *page;
280         static gchar *path[3];
281         
282         path[0] = _("Message View");
283         path[1] = _("External Programs");
284         path[2] = NULL;
285
286         page = g_new0(ExtProgPage, 1);
287         page->page.path = path;
288         page->page.create_widget = prefs_ext_prog_create_widget;
289         page->page.destroy_widget = prefs_ext_prog_destroy_widget;
290         page->page.save_page = prefs_ext_prog_save;
291         page->page.weight = 155.0;
292         prefs_gtk_register_page((PrefsPage *) page);
293         prefs_ext_prog = page;
294 }
295
296 void prefs_ext_prog_done(void)
297 {
298         prefs_gtk_unregister_page((PrefsPage *) prefs_ext_prog);
299         g_free(prefs_ext_prog);
300 }