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: gimv_plugin.h,v 1.4 2004/09/21 08:44:32 makeinu Exp $
22  */
23 
24 #ifndef __GIMV_PLUGIN_H__
25 #define __GIMV_PLUGIN_H__
26 
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif /* HAVE_CONFIG_H */
30 
31 #include <gmodule.h>
32 #include <gtk/gtkwidget.h>
33 
34 #include "gimv_mime_types.h"
35 #include "gimv_prefs_win.h"
36 
37 #ifdef USE_GTK2
38 #  define GIMV_PLUGIN_IF_VERSION    20004
39 #else
40 #  define GIMV_PLUGIN_IF_VERSION    4
41 #endif
42 
43 #define GIMV_PLUGIN_IO_STREAMER       "IOStreamer"
44 #define GIMV_PLUGIN_IMAGE_LOADER      "ImageLoader"
45 #define GIMV_PLUGIN_IMAGE_SAVER       "ImageSaver"
46 #define GIMV_PLUGIN_MOVIE_LOADER      "MovieLoader"
47 #define GIMV_PLUGIN_MOVIE_SAVER       "MovieSaver"
48 #define GIMV_PLUGIN_IMAGE_EFFECTER    "ImageEfecter"
49 #define GIMV_PLUGIN_ARCHIVER          "Archiver"
50 #define GIMV_PLUGIN_EXT_ARCHIVER      "ExternalArchiver"
51 #define GIMV_PLUGIN_IMAGEVIEW_EMBEDER "ImageViewEmbeder"
52 #define GIMV_PLUGIN_THUMBVIEW_EMBEDER "ThumbnailViewEmbeder"
53 #define GIMV_PLUGIN_THUMB_CACHE       "ThumbnailCache"
54 #define GIMV_PLUGIN_DATABASE          "DataBase"
55 #define GIMV_PLUGIN_MISC              "Misc"
56 
57 typedef enum {
58    GIMV_PLUGIN_PREFS_STRING,
59    GIMV_PLUGIN_PREFS_INT,
60    GIMV_PLUGIN_PREFS_FLOAT,
61    GIMV_PLUGIN_PREFS_BOOL,
62 } GimvPluginPrefsType;
63 
64 
65 typedef gboolean (*GimvPluginRegistFn) (const gchar *plugin_name,
66                                         const gchar *module_name,
67                                         gpointer     impl,
68                                         gint         size);
69 
70 typedef struct GimvPluginInfo_Tag
71 {
72    const guint32 if_version; /* plugin interface version */
73 
74    const gchar * const name;
75    const gchar * const version;
76    const gchar * const author;
77    const gchar * const description;
78 
79    /*
80     * idx:    Sequencial index number which start from 0.
81     * impl:   Pointer to implement struct.
82     * size:   Size of implement struct.
83     * Return: Plugin type if succeeded. If idx is invalid, return NULL.
84     */
85    const gchar *(*get_implement) (guint     idx,
86                                   gpointer *impl,
87                                   guint    *size);
88 
89    /*
90     * idx:    Sequencial index number which start from 0.
91     * entry:
92     * size:
93     * Return: TRUE if succeeded.
94     */
95    gboolean (*get_mime_type) (guint               idx,
96                               GimvMimeTypeEntry **entry,
97                               guint              *size);
98 
99    /*
100     * idx:    Sequencial index number which start from 0.
101     * page:   Entry.of preference interface page.
102     * size:   Size of page struct.
103     * Return: TRUE if succeeded.
104     */
105    gboolean (*get_prefs_ui)  (guint              idx,
106                               GimvPrefsWinPage **page,
107                               guint             *size);
108 } GimvPluginInfo;
109 
110 
111 /* plugin type */
112 const gchar *gimv_plugin_type_get           (guint idx);
113 
114 /* load/unload */
115 gboolean gimv_plugin_load_module            (const gchar *filename,
116                                              gint        *error);
117 gboolean gimv_plugin_unload_module          (const gchar *filename,
118                                              gint        *error);
119 void     gimv_plugin_search_directory       (const gchar *dirname);
120 
121 /* plugin list */
122 void   gimv_plugin_create_search_dir_list   (void);
123 GList *gimv_plugin_get_search_dir_list      (void);
124 
125 /* get each infomation from plugin */
126 const gchar *gimv_plugin_get_name           (GModule *module);
127 const gchar *gimv_plugin_get_version_string (GModule *module);
128 const gchar *gimv_plugin_get_author         (GModule *module);
129 const gchar *gimv_plugin_get_module_name    (GModule *module);
130 GList       *gimv_plugin_get_list           (const gchar *type);
131 
132 /* plugin preference */
133 gboolean gimv_plugin_prefs_save_value       (const gchar     *pname,
134                                              const gchar     *ptype,
135                                              const gchar     *key,
136                                              const gchar     *value);
137 gboolean gimv_plugin_prefs_load_value       (const gchar     *name,
138                                              const gchar     *ptype,
139                                              const gchar     *key,
140                                              GimvPluginPrefsType type,
141                                              gpointer         data);
142 void     gimv_plugin_prefs_read_files       (void);
143 void     gimv_plugin_prefs_write_files      (void);
144 
145 void     gimv_plugin_init                   (void);
146 
147 
148 
149 
150 /* utility for plugin */
151 #if 1 /* FIXME!!! uma shika dana ore ha... */
152 #define GIMV_PLUGIN_GET_IMPL(impl_array, type)                                  \
153 static const gchar *                                                            \
154 gimv_plugin_get_impl (guint idx, gpointer *impl, guint *size)                   \
155 {                                                                               \
156    g_return_val_if_fail(impl, NULL);                                            \
157    *impl = NULL;                                                                \
158    g_return_val_if_fail(size, NULL);                                            \
159    *size = 0;                                                                   \
160    if (idx < sizeof (impl_array) / sizeof (impl_array[0])) {                    \
161       *size = sizeof (impl_array[0]);                                           \
162       *impl = &impl_array[idx];                                                 \
163    } else {                                                                     \
164       return NULL;                                                              \
165    }                                                                            \
166    return type;                                                                 \
167 }
168 
169 
170 #define GIMV_PLUGIN_GET_MIME_TYPE(mime_types_array)                             \
171 static gboolean                                                                 \
172 gimv_plugin_get_mime_type (guint idx, GimvMimeTypeEntry **entry, guint *size)   \
173 {                                                                               \
174    g_return_val_if_fail(entry, FALSE);                                          \
175    *entry = NULL;                                                               \
176    g_return_val_if_fail(size, FALSE);                                           \
177    *size = 0;                                                                   \
178    if (idx < sizeof (mime_types_array) / sizeof (GimvMimeTypeEntry)) {          \
179       *size  = sizeof (mime_types_array[0]);                                    \
180       *entry = &mime_types_array[idx];                                          \
181    } else {                                                                     \
182       return FALSE;                                                             \
183    }                                                                            \
184    return TRUE;                                                                 \
185 }
186 
187 
188 #include <string.h>
189 
190 typedef struct PluginPrefsEntry_Tag
191 {
192    const gchar         *key;
193    GimvPluginPrefsType  type;
194    const gchar         *defval;
195    gpointer             value;
196 } GimvPluginPrefsEntry;
197 
198 
199 #define GIMV_PLUGIN_PREFS_GET_VALUE(prefix, pname, ptype, table)                \
200 gboolean                                                                        \
201 prefix##_prefs_get_value (const gchar *key, gpointer *value)                    \
202 {                                                                               \
203    GimvPluginPrefsEntry *entry = NULL;                                          \
204    gboolean success;                                                            \
205    guint i;                                                                     \
206                                                                                 \
207    g_return_val_if_fail(key && value, FALSE);                                   \
208                                                                                 \
209    *value = NULL;                                                               \
210                                                                                 \
211    for (i = 0; i < sizeof (table) / sizeof (GimvPluginPrefsEntry); i++) {       \
212       if (table[i].key && !strcmp(key, table[i].key)) {                         \
213          entry = &table[i];                                                     \
214          break;                                                                 \
215       }                                                                         \
216    }                                                                            \
217    if (!entry) {                                                                \
218       g_warning ("GimvPluginPrefs: key \"%s\" not found!\n", key);              \
219       return FALSE;                                                             \
220    }                                                                            \
221                                                                                 \
222    success = gimv_plugin_prefs_load_value (pname,                               \
223                                            ptype,                               \
224                                            entry->key,                          \
225                                            entry->type,                         \
226                                            value);                              \
227    if (!success) {                                                              \
228       gimv_plugin_prefs_save_value (pname,                                      \
229                                     ptype,                                      \
230                                     entry->key,                                 \
231                                     entry->defval);                             \
232       success = gimv_plugin_prefs_load_value (pname,                            \
233                                               ptype,                            \
234                                               key,                              \
235                                               entry->type,                      \
236                                               value);                           \
237       g_return_val_if_fail(success, FALSE);                                     \
238    }                                                                            \
239                                                                                 \
240    return TRUE;                                                                 \
241 }
242 
243 
244 #define GIMV_PLUGIN_PREFS_GET_ALL(prefix, table, src_struct_p, dest_struct)     \
245 {                                                                               \
246    guint i;                                                                     \
247    src_struct_p = g_malloc0(sizeof(dest_struct));                               \
248    for (i = 0; i < sizeof (table) / sizeof (GimvPluginPrefsEntry); i++) {       \
249       prefix##_prefs_get_value(table[i].key, table[i].value);                   \
250    }                                                                            \
251    *src_struct_p = dest_struct;                                                 \
252    for (i = 0; i < sizeof (table) / sizeof (GimvPluginPrefsEntry); i++) {       \
253       if (table[i].type == GIMV_PLUGIN_PREFS_STRING) {                          \
254          gpointer valp_src, valp_dest;                                          \
255          gulong offset;                                                         \
256          gchar *str;                                                            \
257                                                                                 \
258          valp_dest = table[i].value;                                            \
259          offset = (char *) table[i].value - (char *) &dest_struct;              \
260          valp_src = (G_STRUCT_MEMBER_P (src_struct_p, offset));                 \
261          str = *(gchar **)valp_dest;                                            \
262          if (str) {                                                             \
263             *(gchar **)valp_dest = g_strdup (str);                              \
264             *(gchar **)valp_src  = g_strdup (str);                              \
265          }                                                                      \
266       }                                                                         \
267    }                                                                            \
268 }
269 
270 
271 #define GIMV_PLUGIN_PREFS_APPLY_ALL(table, pname, ptype, action, src_struct_p, dest_struct)\
272 {                                                                               \
273    guint i;                                                                     \
274                                                                                 \
275    for (i = 0;                                                                  \
276         i < sizeof (table) / sizeof (GimvPluginPrefsEntry);                     \
277         i++)                                                                    \
278    {                                                                            \
279       GimvPluginPrefsEntry *entry = &table[i];                                  \
280       gpointer valp, valp_src, valp_dest;                                       \
281       gchar *valstr, buf[256];                                                  \
282       gulong offset;                                                            \
283                                                                                 \
284       valp_dest = entry->value;                                                 \
285       offset = (gchar *) entry->value - (gchar *) &dest_struct;                 \
286       valp_src = (G_STRUCT_MEMBER_P (src_struct_p, offset));                    \
287                                                                                 \
288       switch (action) {                                                         \
289       case GIMV_PREFS_WIN_ACTION_OK:                                            \
290       case GIMV_PREFS_WIN_ACTION_APPLY:                                         \
291          valp = valp_dest;                                                      \
292          break;                                                                 \
293       default:                                                                  \
294          valp = valp_src;                                                       \
295          break;                                                                 \
296       }                                                                         \
297                                                                                 \
298       switch (entry->type) {                                                    \
299       case GIMV_PLUGIN_PREFS_STRING:                                            \
300          valstr = *((gchar **) valp);                                           \
301          break;                                                                 \
302       case GIMV_PLUGIN_PREFS_INT:                                               \
303          g_snprintf (buf, sizeof(buf) / sizeof(gchar),                          \
304                      "%d", *((gint *) valp));                                   \
305          valstr = buf;                                                          \
306          break;                                                                 \
307       case GIMV_PLUGIN_PREFS_FLOAT:                                             \
308          g_snprintf (buf, sizeof(buf) / sizeof(gchar),                          \
309                      "%f", *((gfloat *) valp));                                 \
310          valstr = buf;                                                          \
311          break;                                                                 \
312       case GIMV_PLUGIN_PREFS_BOOL:                                              \
313          valstr = *((gboolean *) valp) ? "TRUE" : "FALSE";                      \
314          break;                                                                 \
315       default:                                                                  \
316          valstr = NULL;                                                         \
317          break;                                                                 \
318       }                                                                         \
319                                                                                 \
320       if (valstr)                                                               \
321          gimv_plugin_prefs_save_value (pname, ptype, entry->key, valstr);       \
322    }                                                                            \
323 }
324 
325 #define GIMV_PLUGIN_PREFS_FREE_ALL(table, src_struct_p, dest_struct)            \
326 {                                                                               \
327    guint i;                                                                     \
328                                                                                 \
329    for (i = 0;                                                                  \
330         i < sizeof (table) / sizeof (GimvPluginPrefsEntry);                     \
331         i++)                                                                    \
332    {                                                                            \
333       GimvPluginPrefsEntry *entry = &table[i];                                  \
334       gpointer valp_src, valp_dest;                                             \
335       gulong offset;                                                            \
336                                                                                 \
337       valp_dest = entry->value;                                                 \
338       offset = (char *) entry->value - (char *) &dest_struct;                   \
339       valp_src = (G_STRUCT_MEMBER_P (src_struct_p, offset));                    \
340                                                                                 \
341       if (entry->type == GIMV_PLUGIN_PREFS_STRING) {                            \
342          g_free (*((gchar **) valp_src));                                       \
343          g_free (*((gchar **) valp_dest));                                      \
344          *((gchar **) valp_src)  = NULL;                                        \
345          *((gchar **) valp_dest) = NULL;                                        \
346       }                                                                         \
347    }                                                                            \
348    g_free(src_struct_p);                                                        \
349    src_struct_p = NULL;                                                         \
350 }
351 #endif /* FIXME!!! uma shika dana ore ha... */
352 
353 #endif /* __GIMV_PLUGIN_H__ */
354