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