offset fixes, admit to textline, proper updating of height/font at attr set,
[dia.git] / lib / filter.c
blobaa4a88a12b0e4ee1022cddaa37ebf3832a9ca7f2
1 /* Dia -- a diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * filter.c: definitions for adding new loaders and export filters.
5 * Copyright (C) 1999 James Henstridge
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.
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.
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.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include "filter.h"
27 #include "intl.h"
28 #include <string.h>
30 static GList *export_filters = NULL;
31 static GList *import_filters = NULL;
32 static GList *callback_filters = NULL;
34 static gint
35 export_filter_compare(gconstpointer a, gconstpointer b)
37 const DiaExportFilter *fa = a, *fb = b;
39 return g_strcasecmp(_(fa->description), _(fb->description));
42 void
43 filter_register_export(DiaExportFilter *efilter)
45 if (efilter->description == NULL) {
46 return;
48 export_filters = g_list_insert_sorted(export_filters, efilter,
49 export_filter_compare);
52 void
53 filter_unregister_export(DiaExportFilter *efilter)
55 export_filters = g_list_remove(export_filters, efilter);
58 /* returns a sorted list of the export filters. */
59 GList *
60 filter_get_export_filters(void)
62 return export_filters;
65 /* creates a nice label for the export filter (must be g_free'd) */
66 gchar *
67 filter_get_export_filter_label(DiaExportFilter *efilter)
69 GString *str = g_string_new(_(efilter->description));
70 gint ext = 0;
71 gchar *ret;
73 for (ext = 0; efilter->extensions[ext] != NULL; ext++) {
74 if (ext == 0)
75 g_string_append(str, " (*.");
76 else
77 g_string_append(str, ", *.");
78 g_string_append(str, efilter->extensions[ext]);
80 if (ext > 0)
81 g_string_append(str, ")");
82 ret = str->str;
83 g_string_free(str, FALSE);
84 return ret;
87 /* Guess the filter for a given filename.
88 * Returns the first filter found that matches the extension on the filename,
89 * or NULL if none such are found. */
90 DiaExportFilter *
91 filter_guess_export_filter(const gchar *filename)
93 GList *tmp;
94 gchar *ext;
96 ext = strrchr(filename, '.');
97 if (ext)
98 ext++;
99 else
100 ext = "";
102 for (tmp = export_filters; tmp != NULL; tmp = tmp->next) {
103 DiaExportFilter *ef = tmp->data;
104 gint i;
106 for (i = 0; ef->extensions[i] != NULL; i++)
107 if (!g_strcasecmp(ef->extensions[i], ext))
108 return ef;
110 return NULL;
113 /** Get an export filter by unique name.
115 DiaExportFilter *
116 filter_get_by_name(const gchar *name)
118 GList *tmp;
119 DiaExportFilter *filter = NULL;
121 for (tmp = export_filters; tmp != NULL; tmp = tmp->next) {
122 DiaExportFilter *ef = tmp->data;
123 if (ef->unique_name != NULL) {
124 if (!g_strcasecmp(ef->unique_name, name)) {
125 if (filter)
126 g_warning(_("Multiple export filters with unique name %s"), name);
127 filter = ef;
131 return filter;
134 static gint
135 import_filter_compare(gconstpointer a, gconstpointer b)
137 const DiaImportFilter *fa = a, *fb = b;
139 return g_strcasecmp(_(fa->description), _(fb->description));
142 void
143 filter_register_import(DiaImportFilter *ifilter)
145 if (ifilter->description == NULL) {
146 return;
148 import_filters = g_list_insert_sorted(import_filters, ifilter,
149 import_filter_compare);
152 void
153 filter_unregister_import(DiaImportFilter *ifilter)
155 import_filters = g_list_remove(import_filters, ifilter);
158 /* returns a sorted list of the export filters. */
159 GList *
160 filter_get_import_filters(void)
162 return import_filters;
165 /* creates a nice label for the export filter (must be g_free'd) */
166 gchar *
167 filter_get_import_filter_label(DiaImportFilter *ifilter)
169 GString *str = g_string_new(_(ifilter->description));
170 gint ext = 0;
171 gchar *ret;
173 for (ext = 0; ifilter->extensions[ext] != NULL; ext++) {
174 if (ext == 0)
175 g_string_append(str, " (*.");
176 else
177 g_string_append(str, ", *.");
178 g_string_append(str, ifilter->extensions[ext]);
180 if (ext > 0)
181 g_string_append(str, ")");
182 ret = str->str;
183 g_string_free(str, FALSE);
184 return ret;
187 /* guess the filter for a given filename. */
188 DiaImportFilter *
189 filter_guess_import_filter(const gchar *filename)
191 GList *tmp;
192 gchar *ext;
194 ext = strrchr(filename, '.');
195 if (ext)
196 ext++;
197 else
198 ext = "";
200 for (tmp = import_filters; tmp != NULL; tmp = tmp->next) {
201 DiaImportFilter *efilter = tmp->data;
202 gint i;
204 for (i = 0; efilter->extensions[i] != NULL; i++)
205 if (!g_strcasecmp(efilter->extensions[i], ext))
206 return efilter;
208 return NULL;
211 /* register a new callback from a plug-in */
212 void
213 filter_register_callback(DiaCallbackFilter *cbfilter)
215 /* sanity check */
216 g_return_if_fail (cbfilter != NULL);
217 g_return_if_fail (cbfilter->callback != NULL);
218 g_return_if_fail (cbfilter->menupath != NULL);
219 g_return_if_fail (cbfilter->description != NULL);
220 g_return_if_fail (cbfilter->action != NULL);
222 /* callback_filters is always pointing to the last element */
223 callback_filters = g_list_append (callback_filters, (gpointer)cbfilter);
226 /* return the registered callbacks list, called once to build menus */
227 GList *
228 filter_get_callbacks(void)
230 return g_list_first (callback_filters);