1 /* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
2 
3 /*
4  * GImageView
5  * Copyright (C) 2001 Takuro Ashie
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * $Id: prefs_spi.c,v 1.4 2004/09/21 08:44:27 makeinu Exp $
22  */
23 
24 #include <string.h>
25 
26 #include "gimageview.h"
27 
28 #include "charset.h"
29 #include "gtkutils.h"
30 #include "gimv_plugin.h"
31 #include "gimv_prefs_ui_utils.h"
32 #include "gimv_prefs_win.h"
33 #include "prefs_spi.h"
34 #include "spi.h"
35 
36 #ifdef ENABLE_SPI
37 
38 #define CONF_DIRLIST_KEY          "search_dir_list"
39 #define CONF_DIRLIST              SPI_DEFAULT_SEARCH_DIR_LIST
40 #define CONF_USE_DEF_DIRLIST_KEY "use_default_search_dir_list"
41 #define CONF_USE_DEF_DIRLIST     "TRUE"
42 
43 extern GimvPluginInfo *gimv_spi_plugin_get_info  (void);
44 static GtkWidget *prefs_plugin_spi_page          (void);
45 static GtkWidget *prefs_plugin_spi_loader_page   (void);
46 static GtkWidget *prefs_plugin_spi_archiver_page (void);
47 static gboolean   prefs_spi_apply            (GimvPrefsWinAction action);
48 
49 
50 typedef struct SPIConf_Tag
51 {
52    gboolean  use_def_dirlist;
53    gchar    *dirlist;
54 } SPIConf;
55 SPIConf spiconf, spiconf_pre;
56 
57 
58 static GimvPrefsWinPage gimv_prefs_page_spi[] = {
59    {
60       path:           N_("/Plugin/Susie plugin"),
61       priority_hint:  0,
62       icon:           NULL,
63       icon_open:      NULL,
64       create_page_fn: prefs_plugin_spi_page,
65       apply_fn:       prefs_spi_apply,
66    },
67    {
68       path:           N_("/Plugin/Susie plugin/Import filter"),
69       priority_hint:  0,
70       icon:           NULL,
71       icon_open:      NULL,
72       create_page_fn: prefs_plugin_spi_loader_page,
73       apply_fn:       NULL,
74    },
75    {
76       path:           N_("/Plugin/Susie plugin/Archive extractor"),
77       priority_hint:  0,
78       icon:           NULL,
79       icon_open:      NULL,
80       create_page_fn: prefs_plugin_spi_archiver_page,
81       apply_fn:       NULL,
82    },
83 };
84 
85 
86 gboolean
gimv_prefs_ui_spi_get_page(guint idx,GimvPrefsWinPage ** page,guint * size)87 gimv_prefs_ui_spi_get_page (guint idx,
88                             GimvPrefsWinPage **page,
89                             guint *size)
90 {
91    g_return_val_if_fail(page, FALSE);
92    *page = NULL;
93    g_return_val_if_fail(size, FALSE);
94    *size = 0;
95    if (idx < sizeof (gimv_prefs_page_spi) / sizeof (GimvPrefsWinPage)) {
96       *size = sizeof (GimvPrefsWinPage);
97       *page = &gimv_prefs_page_spi[idx];
98    } else {
99       return FALSE;
100    }
101    return TRUE;
102 }
103 
104 
105 gboolean
gimv_prefs_spi_get_use_default_dirlist(void)106 gimv_prefs_spi_get_use_default_dirlist (void)
107 {
108    GimvPluginInfo *this = gimv_spi_plugin_get_info();
109    gboolean use = !strcasecmp("TRUE", CONF_USE_DEF_DIRLIST) ? TRUE : FALSE;
110    gboolean success;
111 
112    success = gimv_plugin_prefs_load_value (this->name,
113                                            GIMV_PLUGIN_IMAGE_LOADER,
114                                            CONF_USE_DEF_DIRLIST_KEY,
115                                            GIMV_PLUGIN_PREFS_BOOL,
116                                            (gpointer) &use);
117    if (!success) {
118       use = !strcasecmp("TRUE", CONF_USE_DEF_DIRLIST) ? TRUE : FALSE;
119       gimv_plugin_prefs_save_value (this->name,
120                                     GIMV_PLUGIN_IMAGE_LOADER,
121                                     CONF_USE_DEF_DIRLIST_KEY,
122                                     CONF_USE_DEF_DIRLIST);
123    }
124 
125    return use;
126 }
127 
128 
129 const gchar *
gimv_prefs_spi_get_dirconf(void)130 gimv_prefs_spi_get_dirconf (void)
131 {
132    GimvPluginInfo *this = gimv_spi_plugin_get_info();
133    const gchar *driver;
134    gboolean success;
135 
136    success = gimv_plugin_prefs_load_value (this->name,
137                                            GIMV_PLUGIN_IMAGE_LOADER,
138                                            CONF_DIRLIST_KEY,
139                                            GIMV_PLUGIN_PREFS_STRING,
140                                            (gpointer) &driver);
141    if (success)
142       return driver;
143 
144    driver = CONF_DIRLIST;
145    gimv_plugin_prefs_save_value (this->name,
146                                  GIMV_PLUGIN_IMAGE_LOADER,
147                                  CONF_DIRLIST_KEY,
148                                  driver);
149 
150    return driver;
151 }
152 
153 
154 static void
cb_spi_use_default_dir_list(GtkToggleButton * button,GtkWidget * widget)155 cb_spi_use_default_dir_list(GtkToggleButton *button, GtkWidget *widget)
156 {
157    g_return_if_fail (GTK_IS_WIDGET(widget));
158 
159    if (button->active) {
160       spiconf.use_def_dirlist = TRUE;
161       gtk_widget_hide (widget);
162    } else {
163       spiconf.use_def_dirlist = FALSE;
164       gtk_widget_show (widget);
165    }
166 }
167 
168 
169 /*******************************************************************************
170  *
171  *  for Susie plugins
172  *
173  *******************************************************************************/
174 #include "spi.h"
175 
176 static GtkWidget *
prefs_plugin_spi_page(void)177 prefs_plugin_spi_page (void)
178 {
179    GtkWidget *main_vbox, *frame, *toggle;
180 
181    spiconf.use_def_dirlist = spiconf_pre.use_def_dirlist
182       = gimv_prefs_spi_get_use_default_dirlist ();
183 
184    spiconf.dirlist = (gchar *) gimv_prefs_spi_get_dirconf ();
185    spiconf.dirlist = spiconf.dirlist ? g_strdup (spiconf.dirlist)
186                                      : g_strdup ("");
187    spiconf_pre.dirlist = g_strdup (spiconf.dirlist);
188 
189    main_vbox = gtk_vbox_new (FALSE, 0);
190    gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 5);
191 
192    frame = gimv_prefs_ui_dir_list_prefs (_("Directories list to search susie plugins"),
193                                          _("Select susie plugin directory"),
194                                          spiconf_pre.dirlist,
195                                          &spiconf.dirlist,
196                                     ',');
197    toggle = gtkutil_create_check_button (_("Use default directories list to search susie plugins"),
198                                          spiconf.use_def_dirlist,
199                                          cb_spi_use_default_dir_list,
200                                          frame);
201 
202    gtk_box_pack_start(GTK_BOX (main_vbox), toggle, FALSE, FALSE, 0);
203    gtk_box_pack_start(GTK_BOX (main_vbox), frame, TRUE, TRUE, 0);
204 
205    gtk_widget_show (main_vbox);
206    gtk_widget_show (toggle);
207    gtk_widget_show_all (frame);
208 
209    if (spiconf.use_def_dirlist)
210       gtk_widget_hide (frame);
211    else
212       gtk_widget_show (frame);
213 
214    return main_vbox;
215 }
216 
217 
218 static GtkWidget *
create_spi_admin_widget(GList * plugin_list)219 create_spi_admin_widget (GList *plugin_list)
220 {
221    GtkWidget *hbox, *scrollwin;
222    GtkWidget *clist;
223    GList *list;
224    gint i;
225    gchar *titles[] = {N_("Plugin Info"), N_("File Name")};
226    gint titles_num = sizeof (titles)/ sizeof (gchar *);
227    const gchar *text[32];
228 
229    hbox = gtk_hbox_new (FALSE, 0);
230    gtk_container_set_border_width(GTK_CONTAINER(hbox), 0);
231 
232    scrollwin = gtk_scrolled_window_new (NULL, NULL);
233    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
234                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
235    gtk_container_set_border_width(GTK_CONTAINER(scrollwin), 5);
236    gtk_box_pack_start (GTK_BOX (hbox), scrollwin, TRUE, TRUE, 0);
237    gtk_widget_set_usize (scrollwin, -1, 200);
238 
239 #ifdef ENABLE_TREEVIEW
240    {
241       GtkListStore *store;
242 
243       store = gtk_list_store_new (titles_num,
244                                   G_TYPE_STRING,
245                                   G_TYPE_STRING,
246                                   G_TYPE_STRING);
247       clist = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
248       gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (clist), TRUE);
249       gtk_container_add (GTK_CONTAINER (scrollwin), clist);
250 
251       /* set columns */
252       for (i = 0; i < titles_num; i++) {
253          GtkTreeViewColumn *col;
254          GtkCellRenderer *render;
255 
256          col = gtk_tree_view_column_new();
257          gtk_tree_view_column_set_title (col, _(titles[i]));
258          render = gtk_cell_renderer_text_new ();
259          gtk_tree_view_column_pack_start (col, render, TRUE);
260          gtk_tree_view_column_add_attribute (col, render, "text", i);
261          gtk_tree_view_append_column (GTK_TREE_VIEW (clist), col);
262       }
263 
264       for (list = plugin_list; list; list = g_list_next (list)) {
265          SusiePlugin *spi = list->data;
266          GtkTreeIter iter;
267 
268          text[0] = spi->description;
269 #ifdef G_PLATFORM_WIN32
270          text[1] = g_module_name (spi->pe);
271 #else
272          text[1] = spi->pe->filepath;
273 #endif
274 
275          gtk_list_store_append (store, &iter);
276          gtk_list_store_set (store, &iter,
277                              0, text[0],
278                              1, text[1],
279                              -1);
280       }
281    }
282 #else /* ENABLE_TREEVIEW */
283    clist = gtk_clist_new (titles_num);
284    for (i = 0; i < titles_num; i++) {
285       gtk_clist_set_column_title (GTK_CLIST (clist), i, _(titles[i]));
286       gtk_clist_set_column_auto_resize (GTK_CLIST (clist), i, TRUE);
287    }
288    gtk_clist_column_titles_show (GTK_CLIST (clist));
289    gtk_clist_set_selection_mode (GTK_CLIST (clist), GTK_SELECTION_SINGLE);
290    gtk_container_add (GTK_CONTAINER (scrollwin), clist);
291 
292    /* set default value */
293    for (list = plugin_list; list; list = g_list_next (list)) {
294       SusiePlugin *spi = list->data;
295 
296       text[0] = spi->description;
297       text[1] = spi->pe->filepath;
298 
299       gtk_clist_append (GTK_CLIST (clist), text);
300    }
301 #endif /* ENABLE_TREEVIEW */
302 
303    return hbox;
304 }
305 
306 
307 static GtkWidget *
prefs_plugin_spi_loader_page(void)308 prefs_plugin_spi_loader_page (void)
309 {
310    GtkWidget *main_vbox, *frame, *frame_vbox, *hbox;
311    GList *list;
312 
313    main_vbox = gtk_vbox_new (FALSE, 0);
314    gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 5);
315 
316    gimv_prefs_ui_create_frame (_("Import filters"),
317                                frame, frame_vbox, main_vbox, TRUE);
318 
319    /* clist */
320    list = spi_get_import_filter_list ();
321    hbox = create_spi_admin_widget (list);
322    gtk_box_pack_start (GTK_BOX (frame_vbox), hbox, TRUE, TRUE, 0);
323 
324    gtk_widget_show_all (main_vbox);
325 
326    return main_vbox;
327 }
328 
329 
330 static GtkWidget *
prefs_plugin_spi_archiver_page(void)331 prefs_plugin_spi_archiver_page (void)
332 {
333    GtkWidget *main_vbox, *frame, *frame_vbox, *hbox;
334    GList *list;
335 
336    main_vbox = gtk_vbox_new (FALSE, 0);
337    gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 5);
338 
339    gimv_prefs_ui_create_frame (_("Archive Extractor"),
340                                frame, frame_vbox, main_vbox, TRUE);
341 
342    /* clist */
343    list = spi_get_archive_extractor_list ();
344    hbox = create_spi_admin_widget (list);
345    gtk_box_pack_start (GTK_BOX (frame_vbox), hbox, TRUE, TRUE, 0);
346 
347    gtk_widget_show_all (main_vbox);
348 
349    return main_vbox;
350 }
351 
352 
353 static gboolean
prefs_spi_apply(GimvPrefsWinAction action)354 prefs_spi_apply (GimvPrefsWinAction action)
355 {
356    gchar *dirlist, *use;
357    GimvPluginInfo *this = gimv_spi_plugin_get_info();
358 
359    switch (action) {
360    case GIMV_PREFS_WIN_ACTION_OK:
361    case GIMV_PREFS_WIN_ACTION_APPLY:
362       dirlist = spiconf.dirlist;
363       use = spiconf.use_def_dirlist ? "TRUE" : "FALSE";
364       break;
365    default:
366       dirlist = spiconf_pre.dirlist;
367       use = spiconf_pre.use_def_dirlist ? "TRUE" : "FALSE";
368       break;
369    }
370 
371    gimv_plugin_prefs_save_value (this->name,
372                                  GIMV_PLUGIN_IMAGE_LOADER,
373                                  CONF_USE_DEF_DIRLIST_KEY,
374                                  use);
375    gimv_plugin_prefs_save_value (this->name,
376                                  GIMV_PLUGIN_IMAGE_LOADER,
377                                  CONF_DIRLIST_KEY,
378                                  dirlist);
379 
380    switch (action) {
381    case GIMV_PREFS_WIN_ACTION_OK:
382    case GIMV_PREFS_WIN_ACTION_CANCEL:
383       g_free(spiconf.dirlist);
384       g_free(spiconf_pre.dirlist);
385       spiconf.dirlist     = NULL;
386       spiconf_pre.dirlist = NULL;
387       break;
388    default:
389       break;
390    }
391 
392    return FALSE;
393 }
394 
395 #endif /* ENABLE_SPI */
396