~ubuntu-branches/ubuntu/quantal/xarchiver/quantal

« back to all changes in this revision

Viewing changes to src/mime.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-11-07 14:54:00 UTC
  • mfrom: (1.1.7 upstream) (2.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081107145400-z0j3jmgads8coae2
Tags: 0.5.1-1
MergingĀ upstreamĀ versionĀ 0.5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2008 Giuseppe Torelli <colossus73@gmail.com>
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU Library General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "mime.h"
 
21
 
 
22
GSList *icon_cache = NULL;
 
23
extern GtkIconTheme *icon_theme;
 
24
const char *xa_get_stock_mime_icon(char *filename)
 
25
{
 
26
        const char *mime;
 
27
        const char *icon_name = "binary";
 
28
 
 
29
        mime = xdg_mime_get_mime_type_from_file_name(filename);
 
30
        if (strstr(filename,".ogg") || strstr(filename,".flac") )
 
31
                icon_name = "sound";
 
32
        else if (strncmp(mime,"image/",6) == 0)
 
33
                icon_name = "image";
 
34
        else if (strcmp(mime,"text/html") == 0)
 
35
                icon_name = "html";
 
36
        else if (strncmp(mime,"text/",5) == 0)
 
37
                icon_name = "txt";
 
38
        else if (strcmp(mime,"application/rtf") == 0 || strcmp(mime,"application/pdf") == 0 || strcmp(mime,"application/msword") == 0
 
39
                || strcmp (mime,"application/vnd.oasis.opendocument.text") == 0)
 
40
                icon_name = "document";
 
41
        else if (strcmp(mime,"audio/mpeg") == 0 || strcmp(mime,"audio/midi") == 0 || strcmp (mime,"audio/mp2") == 0)
 
42
                icon_name = "sound";
 
43
        else if (strcmp(mime,"application/vnd.ms-excel") == 0 || strcmp(mime,"application/vnd.oasis.opendocument.spreadsheet") == 0)
 
44
                icon_name = "gnome-mime-application-vnd.ms-excel";
 
45
        else if (strcmp(mime,"application/vnd.ms-powerpoint") == 0 || strcmp (mime,"application/vnd.oasis.opendocument.presentation") == 0)
 
46
                icon_name = "gnome-mime-application-vnd.ms-powerpoint";
 
47
        else if (strcmp(mime,"application/zip") == 0 || strcmp(mime,"application/x-rar") == 0 || strcmp(mime,"application/x-tar") == 0
 
48
                || strcmp(mime,"application/x-7z-compressed") == 0 || strcmp(mime,"application/x-bzip-compressed-tar") == 0
 
49
                || strcmp (mime,"application/x-compressed-tar") == 0 || strcmp (mime,"application/x-lha") == 0
 
50
                || strcmp (mime,"application/x-rpm") == 0 || strcmp (mime,"application/x-deb") == 0
 
51
                || strcmp (mime,"application/x-bzip") == 0  || strcmp (mime,"application/x-gzip") == 0)
 
52
                icon_name = "package";
 
53
        else if (strcmp(mime,"application/x-shockwave-flash") == 0 || strcmp(mime,"video/mpeg") == 0 || strcmp(mime,"video/quicktime") == 0
 
54
                || strcmp(mime,"video/x-msvideo") == 0 || strcmp (mime,"video/mp4") == 0 || strcmp(mime,"application/x-flash-video") == 0
 
55
                || strcmp(mime,"video/dv") == 0)
 
56
                icon_name = "video";
 
57
        else if (strcmp(mime,"application/x-cd-image") == 0)
 
58
                icon_name = "application-x-cd-image";
 
59
        else if (strcmp(mime,"application/x-php") == 0)
 
60
                icon_name = "gnome-mime-application-x-php";
 
61
        else if (strcmp(mime,"application/x-perl") == 0 || strcmp (mime,"application/x-csh") == 0 || strcmp (mime,"application/x-shellscript") == 0)
 
62
                icon_name = "gnome-mime-application-x-perl";
 
63
        else if (strcmp(mime,"application/x-font-ttf") == 0)
 
64
                icon_name = "gnome-mime-application-x-font-ttf";
 
65
        return icon_name;               
 
66
}
 
67
 
 
68
GdkPixbuf *xa_get_pixbuf_icon_from_cache(gchar *filename,gint size)
 
69
{
 
70
        pixbuf_cache *tie = NULL;
 
71
        const gchar *icon_name;
 
72
        GSList *found = NULL;
 
73
        GdkPixbuf *pixbuf = NULL;
 
74
 
 
75
        if (strcmp(filename,"folder") == 0)
 
76
                icon_name = filename;
 
77
        else if (strcmp(filename,"lock") == 0)
 
78
                icon_name = "gtk-dialog-authentication";
 
79
        else
 
80
                icon_name = xa_get_stock_mime_icon(filename);
 
81
 
 
82
        tie = g_new0(pixbuf_cache,1);
 
83
        if (tie)
 
84
        {
 
85
                tie->icon_name = g_strdup(icon_name);
 
86
                found = g_slist_find_custom(icon_cache,tie,(GCompareFunc)xa_icon_name_compare_func);
 
87
                if (found)
 
88
                {
 
89
                        g_free (tie->icon_name);
 
90
                        g_free (tie);
 
91
                        return ((pixbuf_cache *)found->data)->pixbuf;
 
92
                }
 
93
                else
 
94
                {
 
95
                        pixbuf = gtk_icon_theme_load_icon(icon_theme,icon_name,size,GTK_ICON_LOOKUP_FORCE_SVG,NULL);
 
96
                        if (pixbuf)
 
97
                        {
 
98
                                tie->pixbuf = pixbuf;
 
99
                                icon_cache = g_slist_prepend(icon_cache,tie);
 
100
                        }
 
101
                }
 
102
        }
 
103
        return pixbuf;
 
104
}
 
105
 
 
106
gint xa_icon_name_compare_func(pixbuf_cache *a, pixbuf_cache *b)
 
107
{
 
108
        return strcmp(a->icon_name, b->icon_name);
 
109
}
 
110
 
 
111
void xa_free_icon_cache()
 
112
{
 
113
        GSList *x = icon_cache;
 
114
 
 
115
        while (x)
 
116
        {
 
117
                pixbuf_cache *tie = x->data;
 
118
                g_free (tie->icon_name);
 
119
                g_object_unref (tie->pixbuf);
 
120
                g_free(tie);
 
121
                x = x->next;
 
122
        }
 
123
        g_slist_free(icon_cache);
 
124
}