tags.c

Go to the documentation of this file.
00001 /*
00002  * File Name: actions.c
00003  */
00004 
00005 /*
00006  * This file is part of ctb.
00007  *
00008  * ctb is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * ctb is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <stdio.h>
00028 #include <strings.h>
00029 #include <string.h>
00030 
00031 #include "tags.h"
00032 #include "filetypes.h"
00033 
00034 static const struct {
00035     char *tag;
00036     char *special_dir;
00037     char *exts[32];
00038 } extension_tag_map[] = {
00039     { "personal", "Personal Documents", { NULL } },
00040     { "help",     "Help",               { NULL } },
00041     {"book",      "Books",              {"pdf", "epub", "pdb",
00042 #if MACHINE_HAS_ACSM
00043         "acsm",
00044 #endif
00045         NULL}},
00046     { "image",    "Images",             { "jpg", "jpeg", "png", "bmp", "tiff", "gif", NULL } },
00047     { "news",     "News",               { "np", "inp", NULL } },
00048     { "note",     "Notes",              { "note" } },
00049     { NULL, NULL, {} }
00050 };
00051 
00052 
00053 static const char* get_tag_for_extension(const char* ext) 
00054 {
00055     int i, j;
00056     for (i = 0; extension_tag_map[i].tag; i++) 
00057     {
00058         for (j = 0; extension_tag_map[i].exts[j]; j++)
00059         {
00060             if (strcasecmp(extension_tag_map[i].exts[j], ext) == 0)
00061             {
00062                 return extension_tag_map[i].tag;
00063             }
00064         }
00065     }
00066     return NULL;
00067 }
00068 
00069 
00070 static const char* get_tag_for_dir(const char* dir, const char* basedir) 
00071 {
00072     char fulldir[256];
00073     int i;
00074     for (i=0; extension_tag_map[i].tag; i++) 
00075     {
00076         snprintf(fulldir, sizeof(fulldir), "%s/%s", basedir, extension_tag_map[i].special_dir);
00077         int len = strlen(fulldir);
00078         if (strncasecmp(fulldir, dir, len) == 0) {
00079             if (dir[len] == 0 || dir[len] == '/') {
00080                 return extension_tag_map[i].tag;
00081             }
00082         }
00083     }
00084     return NULL;
00085 }
00086 
00087 
00088 const gchar* get_tag_for_file(const gchar* filename,
00089                               const gchar* dir,
00090                               const char* basedir)
00091 {
00092     static const char* empty = "";
00093 
00094     const char *ctag = get_tag_for_dir(dir, basedir);
00095     if (ctag) return ctag;
00096 
00097     const char *ext = g_extension_pointer(filename);
00098     ctag = get_tag_for_extension(ext);
00099     if (ctag) return ctag;
00100 
00101     return empty;
00102 }
00103 
Generated by  doxygen 1.6.2-20100208