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.
30 static GList
*export_filters
= NULL
;
31 static GList
*import_filters
= NULL
;
32 static GList
*callback_filters
= NULL
;
35 export_filter_compare(gconstpointer a
, gconstpointer b
)
37 const DiaExportFilter
*fa
= a
, *fb
= b
;
39 return g_strcasecmp(_(fa
->description
), _(fb
->description
));
43 filter_register_export(DiaExportFilter
*efilter
)
45 if (efilter
->description
== NULL
) {
48 export_filters
= g_list_insert_sorted(export_filters
, efilter
,
49 export_filter_compare
);
53 filter_unregister_export(DiaExportFilter
*efilter
)
55 export_filters
= g_list_remove(export_filters
, efilter
);
58 /* returns a sorted list of the export filters. */
60 filter_get_export_filters(void)
62 return export_filters
;
65 /* creates a nice label for the export filter (must be g_free'd) */
67 filter_get_export_filter_label(DiaExportFilter
*efilter
)
69 GString
*str
= g_string_new(_(efilter
->description
));
73 for (ext
= 0; efilter
->extensions
[ext
] != NULL
; ext
++) {
75 g_string_append(str
, " (*.");
77 g_string_append(str
, ", *.");
78 g_string_append(str
, efilter
->extensions
[ext
]);
81 g_string_append(str
, ")");
83 g_string_free(str
, FALSE
);
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. */
91 filter_guess_export_filter(const gchar
*filename
)
96 ext
= strrchr(filename
, '.');
102 for (tmp
= export_filters
; tmp
!= NULL
; tmp
= tmp
->next
) {
103 DiaExportFilter
*ef
= tmp
->data
;
106 for (i
= 0; ef
->extensions
[i
] != NULL
; i
++)
107 if (!g_strcasecmp(ef
->extensions
[i
], ext
))
113 /** Get an export filter by unique name.
116 filter_get_by_name(const gchar
*name
)
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
)) {
126 g_warning(_("Multiple export filters with unique name %s"), name
);
135 import_filter_compare(gconstpointer a
, gconstpointer b
)
137 const DiaImportFilter
*fa
= a
, *fb
= b
;
139 return g_strcasecmp(_(fa
->description
), _(fb
->description
));
143 filter_register_import(DiaImportFilter
*ifilter
)
145 if (ifilter
->description
== NULL
) {
148 import_filters
= g_list_insert_sorted(import_filters
, ifilter
,
149 import_filter_compare
);
153 filter_unregister_import(DiaImportFilter
*ifilter
)
155 import_filters
= g_list_remove(import_filters
, ifilter
);
158 /* returns a sorted list of the export filters. */
160 filter_get_import_filters(void)
162 return import_filters
;
165 /* creates a nice label for the export filter (must be g_free'd) */
167 filter_get_import_filter_label(DiaImportFilter
*ifilter
)
169 GString
*str
= g_string_new(_(ifilter
->description
));
173 for (ext
= 0; ifilter
->extensions
[ext
] != NULL
; ext
++) {
175 g_string_append(str
, " (*.");
177 g_string_append(str
, ", *.");
178 g_string_append(str
, ifilter
->extensions
[ext
]);
181 g_string_append(str
, ")");
183 g_string_free(str
, FALSE
);
187 /* guess the filter for a given filename. */
189 filter_guess_import_filter(const gchar
*filename
)
194 ext
= strrchr(filename
, '.');
200 for (tmp
= import_filters
; tmp
!= NULL
; tmp
= tmp
->next
) {
201 DiaImportFilter
*efilter
= tmp
->data
;
204 for (i
= 0; efilter
->extensions
[i
] != NULL
; i
++)
205 if (!g_strcasecmp(efilter
->extensions
[i
], ext
))
211 /* register a new callback from a plug-in */
213 filter_register_callback(DiaCallbackFilter
*cbfilter
)
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 */
228 filter_get_callbacks(void)
230 return g_list_first (callback_filters
);