* cleanup icon handling a bit
[modest] / src / maemo / modest-platform.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31 #include <modest-platform.h>
32 #include <libosso.h>
33
34 #ifdef MODEST_HILDON_VERSION_0
35 #include <osso-mime.h>
36 #else
37 #include <hildon-mime.h>
38 #endif /*MODEST_HILDON_VERSION_0*/
39
40 #include <tny-maemo-device.h>
41 #include <gtk/gtkicontheme.h>
42
43 gboolean
44 modest_platform_init (void)
45 {       
46         osso_context_t *osso_context =
47                 osso_initialize(PACKAGE, PACKAGE_VERSION,
48                                 TRUE, NULL);    
49         if (!osso_context) {
50                 g_printerr ("modest: failed to acquire osso context\n");
51                 return FALSE;
52         }
53         return TRUE;
54 }
55
56 TnyDevice*
57 modest_platform_get_new_device (void)
58 {
59         return TNY_DEVICE (tny_maemo_device_new ());
60 }
61
62
63 const gchar*
64 guess_mime_type_from_name (const gchar* name)
65 {
66         int i;
67         const gchar* ext;
68         const static gchar* octet_stream= "application/octet-stream";
69         const static gchar* mime_map[][2] = {
70                 { "pdf",  "application/pdf"},
71                 { "doc",  "application/msword"},
72                 { "xls",  "application/excel"},
73                 { "png",  "image/png" },
74                 { "gif",  "image/gif" },
75                 { "jpg",  "image/jpeg"},
76                 { "jpeg", "image/jpeg"},
77                 { "mp3",  "audio/mp3" }
78         };
79
80         if (!name)
81                 return octet_stream;
82         
83         ext = g_strrstr (name, ".");
84         if (!ext)
85                 return octet_stream;
86         
87         for (i = 0; i != G_N_ELEMENTS(mime_map); ++i) {
88                 if (g_ascii_strcasecmp (mime_map[i][0], ext + 1)) /* +1: ignore '.'*/
89                         return mime_map[i][1];
90         }
91         return octet_stream;
92 }
93
94
95 gchar*
96 modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
97                                           gchar **effective_mime_type)
98 {
99         GString *mime_str = NULL;
100         gchar *icon_name  = NULL;
101         gchar **icons, **cursor;
102         
103         
104         g_return_val_if_fail (name || mime_type, NULL);
105
106         if (!mime_type || g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
107                 mime_str = g_string_new (guess_mime_type_from_name(name));
108         else {
109                 mime_str = g_string_new (mime_type);
110                 g_string_ascii_down (mime_str);
111         }
112 #ifdef MODEST_HILDON_VERSION_0
113         icons = osso_mime_get_icon_names (mime_str->str, NULL);
114 #else
115         icons = hildon_mime_get_icon_names (mime_str->str, NULL);
116 #endif /*MODEST_HILDON_VERSION_0*/
117         for (cursor = icons; cursor; ++cursor) {
118                 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default(), *cursor)) {
119                         icon_name = g_strdup (*cursor);
120                         break;
121                 }
122         }
123         g_strfreev (icons);
124
125         if (effective_mime_type)
126                 *effective_mime_type = g_string_free (mime_str, FALSE);
127         else
128                 g_string_free (mime_str, TRUE);
129
130         return icon_name;
131 }
132
133
134 GdkPixbuf*
135 modest_platform_get_icon (const gchar *name)
136 {
137         GError *err = NULL;
138         GdkPixbuf* pixbuf;
139         GtkIconTheme *current_theme;
140
141         g_return_val_if_fail (name, NULL);
142
143         current_theme = gtk_icon_theme_get_default ();
144         pixbuf = gtk_icon_theme_load_icon (current_theme, name, 26,
145                                            GTK_ICON_LOOKUP_NO_SVG,
146                                            &err);
147         if (!pixbuf) {
148                 g_printerr ("modest: error while loading icon '%s': %s\n",
149                             name, err->message);
150                 g_error_free (err);
151         }
152         
153         return pixbuf;
154 }
155