1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/* Dia -- a diagram creation/manipulation program
* Copyright (C) 1998 Alexander Larsson
*
* filter.c: definitions for adding new loaders and export filters.
* Copyright (C) 1999 James Henstridge
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "filter.h"
#include "intl.h"
#include <string.h>
static GList *export_filters = NULL;
static GList *import_filters = NULL;
static GList *callback_filters = NULL;
static gint
export_filter_compare(gconstpointer a, gconstpointer b)
{
const DiaExportFilter *fa = a, *fb = b;
return g_ascii_strcasecmp(_(fa->description), _(fb->description));
}
void
filter_register_export(DiaExportFilter *efilter)
{
if (efilter->description == NULL) {
return;
}
export_filters = g_list_insert_sorted(export_filters, efilter,
export_filter_compare);
}
void
filter_unregister_export(DiaExportFilter *efilter)
{
export_filters = g_list_remove(export_filters, efilter);
}
/* returns a sorted list of the export filters. */
GList *
filter_get_export_filters(void)
{
return export_filters;
}
/* creates a nice label for the export filter (must be g_free'd) */
gchar *
filter_get_export_filter_label(DiaExportFilter *efilter)
{
GString *str = g_string_new(_(efilter->description));
gint ext = 0;
gchar *ret;
for (ext = 0; efilter->extensions[ext] != NULL; ext++) {
if (ext == 0)
g_string_append(str, " (*.");
else
g_string_append(str, ", *.");
g_string_append(str, efilter->extensions[ext]);
}
if (ext > 0)
g_string_append(str, ")");
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
/* Get the list of unique names for the given extension */
GList *
filter_get_unique_export_names(const char *ext)
{
GList *tmp, *res = NULL;
for (tmp = export_filters; tmp != NULL; tmp = tmp->next) {
DiaExportFilter *ef = tmp->data;
gint i;
for (i = 0; ef->extensions[i] != NULL; i++) {
if (!g_ascii_strcasecmp(ef->extensions[i], ext) && ef->unique_name)
res = g_list_append (res, (char *)ef->unique_name);
}
}
return res;
}
static GHashTable *_favored_hash = NULL;
/* Set the favorit 'guess' */
void
filter_set_favored_export(const char *ext, const char *name)
{
if (!_favored_hash)
_favored_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(_favored_hash, g_ascii_strdown(ext, -1), g_strdup(name));
}
/* Guess the filter for a given filename.
* Returns the first filter found that matches the extension on the filename,
* or NULL if none such are found.
* If there are multiple filters registered for the same extension some are
* excluded from being returned here by the hint FILTER_DONT_GUESS.
*/
DiaExportFilter *
filter_guess_export_filter(const gchar *filename)
{
GList *tmp;
gchar *ext;
gint no_guess = 0;
DiaExportFilter *dont_guess = NULL;
const gchar *unique_name;
ext = strrchr(filename, '.');
if (ext)
ext++;
else
ext = "";
/* maybe ther is no need to guess? */
unique_name = _favored_hash ? g_hash_table_lookup(_favored_hash, ext) : NULL;
if (unique_name) {
DiaExportFilter *ef = filter_export_get_by_name(unique_name);
if (ef)
return ef;
}
for (tmp = export_filters; tmp != NULL; tmp = tmp->next) {
DiaExportFilter *ef = tmp->data;
gint i;
for (i = 0; ef->extensions[i] != NULL; i++) {
if (!g_ascii_strcasecmp(ef->extensions[i], ext)) {
if (ef->hints & FILTER_DONT_GUESS) {
dont_guess = ef;
++no_guess;
continue;
}
return ef;
}
}
}
return (no_guess == 1) ? dont_guess : NULL;
}
/** Get an export filter by unique name.
*/
DiaExportFilter *
filter_export_get_by_name(const gchar *name)
{
GList *tmp;
DiaExportFilter *filter = NULL;
for (tmp = export_filters; tmp != NULL; tmp = tmp->next) {
DiaExportFilter *ef = tmp->data;
if (ef->unique_name != NULL) {
if (!g_ascii_strcasecmp(ef->unique_name, name)) {
if (filter)
g_warning(_("Multiple export filters with unique name %s"), name);
filter = ef;
}
}
}
return filter;
}
DiaImportFilter *
filter_import_get_by_name(const gchar *name)
{
GList *tmp;
DiaImportFilter *filter = NULL;
for (tmp = import_filters; tmp != NULL; tmp = tmp->next) {
DiaImportFilter *af = tmp->data;
if (af->unique_name != NULL) {
if (!g_ascii_strcasecmp(af->unique_name, name)) {
if (filter)
g_warning(_("Multiple import filters with unique name %s"), name);
filter = af;
}
}
}
return filter;
}
static gint
import_filter_compare(gconstpointer a, gconstpointer b)
{
const DiaImportFilter *fa = a, *fb = b;
return g_ascii_strcasecmp(_(fa->description), _(fb->description));
}
void
filter_register_import(DiaImportFilter *ifilter)
{
if (ifilter->description == NULL) {
return;
}
import_filters = g_list_insert_sorted(import_filters, ifilter,
import_filter_compare);
}
void
filter_unregister_import(DiaImportFilter *ifilter)
{
import_filters = g_list_remove(import_filters, ifilter);
}
/* returns a sorted list of the export filters. */
GList *
filter_get_import_filters(void)
{
return import_filters;
}
/* creates a nice label for the export filter (must be g_free'd) */
gchar *
filter_get_import_filter_label(DiaImportFilter *ifilter)
{
GString *str = g_string_new(_(ifilter->description));
gint ext = 0;
gchar *ret;
for (ext = 0; ifilter->extensions[ext] != NULL; ext++) {
if (ext == 0)
g_string_append(str, " (*.");
else
g_string_append(str, ", *.");
g_string_append(str, ifilter->extensions[ext]);
}
if (ext > 0)
g_string_append(str, ")");
ret = str->str;
g_string_free(str, FALSE);
return ret;
}
/* guess the filter for a given filename.
* If there are multiple filters registered for the same extension some are
* excluded from being returned here by the hint FILTER_DONT_GUESS.
*/
DiaImportFilter *
filter_guess_import_filter(const gchar *filename)
{
GList *tmp;
gchar *ext;
int no_guess = 0;
DiaImportFilter *dont_guess = NULL;
ext = strrchr(filename, '.');
if (ext)
ext++;
else
ext = "";
for (tmp = import_filters; tmp != NULL; tmp = tmp->next) {
DiaImportFilter *ifilter = tmp->data;
gint i;
for (i = 0; ifilter->extensions[i] != NULL; i++) {
if (!g_ascii_strcasecmp(ifilter->extensions[i], ext)) {
if (ifilter->hints & FILTER_DONT_GUESS) {
dont_guess = ifilter;
++no_guess;
continue;
}
return ifilter;
}
}
}
return (no_guess == 1) ? dont_guess : NULL;
}
/* register a new callback from a plug-in */
void
filter_register_callback(DiaCallbackFilter *cbfilter)
{
/* sanity check */
g_return_if_fail (cbfilter != NULL);
g_return_if_fail (cbfilter->callback != NULL);
g_return_if_fail (cbfilter->menupath != NULL);
g_return_if_fail (cbfilter->description != NULL);
g_return_if_fail (cbfilter->action != NULL);
/* callback_filters is always pointing to the last element */
callback_filters = g_list_append (callback_filters, (gpointer)cbfilter);
}
void
filter_unregister_callback(DiaCallbackFilter *cbfilter)
{
/* Unregistering a callback filter should remove it from the menu, too.
* But that would involve quite some additional code, maybe some
* notification to update menus ...
* We take the easy approach to let the caller check if the callback
* is still valid, i.e. the filter still exists.
*/
callback_filters = g_list_remove (callback_filters, cbfilter);
}
/* return the registered callbacks list, called once to build menus */
GList *
filter_get_callbacks(void)
{
return g_list_first (callback_filters);
}
|