* big commit, mainly cleanups:
[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 #include <osso-mime.h>
34 #include <tny-maemo-device.h>
35 #include <gtk/gtkicontheme.h>
36
37 gboolean
38 modest_platform_init (void)
39 {       
40         osso_context_t *osso_context =
41                 osso_initialize(PACKAGE, PACKAGE_VERSION,
42                                 TRUE, NULL);    
43         if (!osso_context) {
44                 g_printerr ("modest: failed to acquire osso context\n");
45                 return FALSE;
46         }
47         return TRUE;
48 }
49
50 TnyDevice*
51 modest_platform_get_new_device (void)
52 {
53         return TNY_DEVICE (tny_maemo_device_new ());
54 }
55
56
57 const gchar*
58 guess_mime_type_from_name (const gchar* name)
59 {
60         int i;
61         const gchar* ext;
62         const static gchar* octet_stream= "application/octet-stream";
63         const static gchar* mime_map[][2] = {
64                 { "pdf",  "application/pdf"},
65                 { "doc",  "application/msword"},
66                 { "xls",  "application/excel"},
67                 { "png",  "image/png" },
68                 { "gif",  "image/gif" },
69                 { "jpg",  "image/jpeg"},
70                 { "jpeg", "image/jpeg"},
71                 { "mp3",  "audio/mp3" }
72         };
73
74         if (!name)
75                 return octet_stream;
76         
77         ext = g_strrstr (name, ".");
78         if (!ext)
79                 return octet_stream;
80         
81         for (i = 0; i != G_N_ELEMENTS(mime_map); ++i) {
82                 if (g_ascii_strcasecmp (mime_map[i][0], ext + 1)) /* +1: ignore '.'*/
83                         return mime_map[i][1];
84         }
85         return octet_stream;
86 }
87
88
89 gchar*
90 modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
91                                           gchar **effective_mime_type)
92 {
93         GString *mime_str = NULL;
94         gchar *icon_name  = NULL;
95         gchar **icons, **cursor;
96         
97         
98         g_return_val_if_fail (name || mime_type, NULL);
99
100         if (!mime_type || g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
101                 mime_str = g_string_new (guess_mime_type_from_name(name));
102         else {
103                 mime_str = g_string_new (mime_type);
104                 g_string_ascii_down (mime_str);
105         }
106
107         icons = osso_mime_get_icon_names (mime_str->str, NULL);
108         for (cursor = icons; cursor; ++cursor) {
109                 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default(), *cursor)) {
110                         icon_name = g_strdup (*cursor);
111                         break;
112                 }
113         }
114         g_strfreev (icons);
115
116         if (effective_mime_type)
117                 *effective_mime_type = g_string_free (mime_str, FALSE);
118         else
119                 g_string_free (mime_str, TRUE);
120
121         return icon_name;
122 }