0fe82131cf079b68059202e56e19ec4616bb5326
[claws.git] / src / prefs_ext_prog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 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
52         GtkWidget *uri_label;
53         GtkWidget *uri_combo;
54         GtkWidget *uri_entry;
55         
56         GtkWidget *printcmd_label;
57         GtkWidget *printcmd_entry;
58         
59         GtkWidget *exteditor_label;
60         GtkWidget *exteditor_combo;
61         GtkWidget *exteditor_entry;
62
63         GtkWidget *astextviewer_label;
64         GtkWidget *astextviewer_entry;
65 } ExtProgPage;
66
67 static void prefs_ext_prog_create_widget(PrefsPage *_page, GtkWindow *window, 
68                                   gpointer data)
69 {
70         ExtProgPage *prefs_ext_prog = (ExtProgPage *) _page;
71
72         GtkWidget *table;
73         GtkWidget *vbox;
74         GtkWidget *hint_label;
75         GtkWidget *table2;
76         GtkWidget *cmds_use_system_default_checkbtn;
77         GtkWidget *uri_label;
78         GtkWidget *uri_combo;
79         GtkWidget *uri_entry;
80         GtkWidget *exteditor_label;
81         GtkWidget *exteditor_combo;
82         GtkWidget *exteditor_entry;
83         GtkWidget *astextviewer_label;
84         GtkWidget *astextviewer_entry;
85         GtkWidget *printcmd_label;
86         GtkWidget *printcmd_entry;
87         CLAWS_TIP_DECL();
88         int i = 0;
89         gchar *tmp;
90
91         table = gtk_table_new(2, 1, FALSE);
92         gtk_widget_show(table);
93         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
94         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
95         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
96
97         vbox = gtk_vbox_new(TRUE, 0);
98         gtk_widget_show(vbox);
99
100         gtk_table_attach(GTK_TABLE (table), vbox, 0, 1, 0, 1,
101                          (GtkAttachOptions) (GTK_SHRINK),
102                          (GtkAttachOptions) (0), 0, 0);
103
104         hint_label = gtk_label_new(_("%s will be replaced with file name / URI"));
105         gtk_label_set_justify (GTK_LABEL (hint_label), GTK_JUSTIFY_LEFT);
106         gtkut_widget_set_small_font_size (hint_label);
107         gtk_widget_show(hint_label);
108         gtk_box_pack_start(GTK_BOX (vbox),
109                            hint_label, FALSE, FALSE, 4);
110
111         table2 = gtk_table_new(7, 2, FALSE);
112         gtk_widget_show(table2);
113         gtk_container_set_border_width(GTK_CONTAINER(table2), 8);
114         gtk_table_set_row_spacings(GTK_TABLE(table2), 4);
115         gtk_table_set_col_spacings(GTK_TABLE(table2), 8);
116         
117         gtk_table_attach(GTK_TABLE (table), table2, 0, 1, 1, 2,
118                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
119                          (GtkAttachOptions) (0), 0, 0);
120         
121         cmds_use_system_default_checkbtn = gtk_check_button_new_with_label(
122                 _("Use system defaults when possible")); 
123         
124 #ifndef MAEMO
125         gtk_widget_show(cmds_use_system_default_checkbtn);
126         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cmds_use_system_default_checkbtn),
127                                         prefs_common.cmds_use_system_default);
128 #else
129         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cmds_use_system_default_checkbtn),
130                                         TRUE);
131 #endif
132         tmp = g_find_program_in_path("xdg-open");
133         if (!tmp) {
134                 g_print("xdg-open not found\n");
135                 gtk_widget_set_sensitive(cmds_use_system_default_checkbtn, FALSE);
136                 gtk_widget_set_sensitive(cmds_use_system_default_checkbtn, FALSE);
137                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cmds_use_system_default_checkbtn),
138                                         FALSE);
139         } else
140                 g_free(tmp);
141
142         
143         gtk_table_attach(GTK_TABLE (table2), cmds_use_system_default_checkbtn, 0, 2, i, i+1,
144                          (GtkAttachOptions) (GTK_FILL),
145                          (GtkAttachOptions) (0), 0, 2);
146         
147         uri_label = gtk_label_new (_("Web browser"));
148         gtk_widget_show(uri_label);
149 #ifdef MAEMO
150         gtk_widget_set_sensitive(uri_label, FALSE);
151 #endif
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 #ifdef MAEMO
173         gtk_widget_set_sensitive(uri_combo, FALSE);
174 #endif
175         gtk_table_attach (GTK_TABLE (table2), uri_combo, 1, 2, i, i+1,
176                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
177
178         uri_entry = gtk_bin_get_child(GTK_BIN((uri_combo)));
179         gtk_entry_set_text(GTK_ENTRY(uri_entry), prefs_common.uri_cmd ? prefs_common.uri_cmd : "");
180         
181         exteditor_label = gtk_label_new (_("Text editor"));
182         gtk_widget_show(exteditor_label);
183
184         i++;
185         gtk_table_attach(GTK_TABLE (table2), exteditor_label, 0, 1, i, i+1,
186                          (GtkAttachOptions) (GTK_FILL),
187                          (GtkAttachOptions) (0), 0, 2);
188         gtk_label_set_justify(GTK_LABEL (exteditor_label), GTK_JUSTIFY_RIGHT);
189         gtk_misc_set_alignment(GTK_MISC (exteditor_label), 1, 0.5);
190
191         exteditor_combo = combobox_text_new(TRUE,
192                                         "gedit %s",
193                                         "kedit %s",
194                                         "mousepad %s",
195                                         "nedit %s",
196                                         "mgedit --no-fork %s",
197                                         "emacs %s",
198                                         "xemacs %s",
199                                         "kterm -e jed %s",
200                                         "kterm -e vi %s",
201                                         NULL);
202         gtk_table_attach (GTK_TABLE (table2), exteditor_combo, 1, 2, i, i+1,
203                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
204
205         exteditor_entry = gtk_bin_get_child(GTK_BIN((exteditor_combo)));
206         gtk_entry_set_text(GTK_ENTRY(exteditor_entry), 
207                            prefs_common.ext_editor_cmd ? prefs_common.ext_editor_cmd : "");
208
209         astextviewer_label = gtk_label_new(_("Command for 'Display as text'"));
210         gtk_widget_show(astextviewer_label);
211
212         i++;
213         gtk_table_attach(GTK_TABLE (table2), astextviewer_label, 0, 1, i, i+1,
214                          (GtkAttachOptions) (GTK_FILL),
215                          (GtkAttachOptions) (0), 0, 2);
216         gtk_label_set_justify(GTK_LABEL (astextviewer_label), GTK_JUSTIFY_RIGHT);
217         gtk_misc_set_alignment(GTK_MISC (astextviewer_label), 1, 0.5);
218
219         astextviewer_entry = gtk_entry_new ();
220         gtk_widget_show(astextviewer_entry);
221         CLAWS_SET_TIP(astextviewer_entry,
222                              _("This option enables MIME parts to be displayed in the "
223                                "message view via a script when using the 'Display as text' "
224                                "contextual menu item"));
225         
226         gtk_table_attach(GTK_TABLE (table2), astextviewer_entry, 1, 2, i, i+1,
227                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
228                          (GtkAttachOptions) (0), 0, 0);
229         gtk_entry_set_text(GTK_ENTRY(astextviewer_entry), 
230                            prefs_common.mime_textviewer ? prefs_common.mime_textviewer : "");
231
232         printcmd_label = gtk_label_new (_("Print command"));
233 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
234         gtk_widget_show(printcmd_label);
235 #endif
236         i++;
237         gtk_table_attach(GTK_TABLE (table2), printcmd_label, 0, 1, i, i+1,
238                          (GtkAttachOptions) (GTK_FILL),
239                          (GtkAttachOptions) (0), 0, 2);
240         gtk_label_set_justify(GTK_LABEL (printcmd_label), GTK_JUSTIFY_RIGHT);
241         gtk_misc_set_alignment(GTK_MISC (printcmd_label), 1, 0.5);
242
243         printcmd_entry = gtk_entry_new ();
244 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
245         gtk_widget_show(printcmd_entry);
246 #endif
247         gtk_table_attach(GTK_TABLE (table2), printcmd_entry, 1, 2, i, i+1,
248                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
249                          (GtkAttachOptions) (0), 0, 0);
250         gtk_entry_set_text(GTK_ENTRY(printcmd_entry), prefs_common.print_cmd ? prefs_common.print_cmd : "");
251
252         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, uri_label);
253         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, uri_combo);
254 #if 0 /* we should do that, but it detaches the editor and breaks
255          compose.c's external composition. */
256         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, exteditor_label);
257         SET_TOGGLE_SENSITIVITY_REVERSE (cmds_use_system_default_checkbtn, exteditor_combo);
258 #endif
259
260         prefs_ext_prog->window                  = GTK_WIDGET(window);
261         prefs_ext_prog->uri_entry               = uri_entry;
262         prefs_ext_prog->exteditor_entry         = exteditor_entry;
263         prefs_ext_prog->astextviewer_entry      = astextviewer_entry;
264         prefs_ext_prog->printcmd_entry          = printcmd_entry;
265         prefs_ext_prog->cmds_use_system_default_checkbtn = cmds_use_system_default_checkbtn;
266         prefs_ext_prog->page.widget = table;
267 }
268
269 static void prefs_ext_prog_save(PrefsPage *_page)
270 {
271         ExtProgPage *ext_prog = (ExtProgPage *) _page;
272
273         prefs_common.uri_cmd = gtk_editable_get_chars
274                 (GTK_EDITABLE(ext_prog->uri_entry), 0, -1);
275         prefs_common.print_cmd = gtk_editable_get_chars
276                 (GTK_EDITABLE(ext_prog->printcmd_entry), 0, -1);
277         prefs_common.ext_editor_cmd = gtk_editable_get_chars
278                 (GTK_EDITABLE(ext_prog->exteditor_entry), 0, -1);
279         prefs_common.mime_textviewer = gtk_editable_get_chars
280                 (GTK_EDITABLE(ext_prog->astextviewer_entry), 0, -1);
281         prefs_common.cmds_use_system_default = gtk_toggle_button_get_active
282                 (GTK_TOGGLE_BUTTON(ext_prog->cmds_use_system_default_checkbtn));
283 }
284
285 static void prefs_ext_prog_destroy_widget(PrefsPage *_page)
286 {
287         /* ExtProgPage *ext_prog = (ExtProgPage *) _page; */
288
289 }
290
291 ExtProgPage *prefs_ext_prog;
292
293 void prefs_ext_prog_init(void)
294 {
295         ExtProgPage *page;
296         static gchar *path[3];
297         
298         path[0] = _("Message View");
299         path[1] = _("External Programs");
300         path[2] = NULL;
301
302         page = g_new0(ExtProgPage, 1);
303         page->page.path = path;
304         page->page.create_widget = prefs_ext_prog_create_widget;
305         page->page.destroy_widget = prefs_ext_prog_destroy_widget;
306         page->page.save_page = prefs_ext_prog_save;
307         page->page.weight = 155.0;
308         prefs_gtk_register_page((PrefsPage *) page);
309         prefs_ext_prog = page;
310 }
311
312 void prefs_ext_prog_done(void)
313 {
314         prefs_gtk_unregister_page((PrefsPage *) prefs_ext_prog);
315         g_free(prefs_ext_prog);
316 }