~ubuntu-branches/ubuntu/oneiric/alarm-clock-applet/oneiric

« back to all changes in this revision

Viewing changes to src/list-entry.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-05-30 23:24:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090530232427-88on1j2ily4ajxdz
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * list-entry.c -- Simple data structure to hold name, data and icon.
 
3
 * 
 
4
 * Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com>
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 * 
 
20
 * Authors:
 
21
 *              Johannes H. Jensen <joh@pseudoberries.com>
 
22
 */
 
23
 
 
24
#include <string.h>
 
25
#include <glib.h>
 
26
#include <gtk/gtk.h>
 
27
#include <libgnomevfs/gnome-vfs.h>
 
28
#include <libgnomeui/libgnomeui.h>
 
29
 
 
30
#include "list-entry.h"
 
31
#include "util.h"
 
32
 
 
33
/*
 
34
 * Creates a new AlarmListEntry.
 
35
 */
 
36
AlarmListEntry *
 
37
alarm_list_entry_new (const gchar *name, const gchar *data, const gchar *icon)
 
38
{
 
39
        AlarmListEntry *entry;
 
40
        
 
41
        entry = g_new (AlarmListEntry, 1);
 
42
        
 
43
        entry->name = NULL;
 
44
        entry->data = NULL;
 
45
        entry->icon = NULL;
 
46
        
 
47
        if (name)
 
48
                entry->name = g_strdup (name);
 
49
        if (data)
 
50
                entry->data = g_strdup (data);
 
51
        if (icon)
 
52
                entry->icon = g_strdup (icon);
 
53
        
 
54
        return entry;
 
55
}
 
56
 
 
57
void
 
58
alarm_list_entry_free (AlarmListEntry *e)
 
59
{
 
60
        g_free (e->data);
 
61
        g_free (e->name);
 
62
        g_free (e->icon);
 
63
        g_free (e);
 
64
}
 
65
 
 
66
AlarmListEntry *
 
67
alarm_list_entry_new_file (const gchar *uri, GnomeVFSResult *ret, gchar **mime_ret)
 
68
{
 
69
        AlarmListEntry *entry;
 
70
        GnomeVFSResult result;
 
71
        GnomeVFSFileInfo *info;
 
72
        GtkIconTheme *theme = NULL;
 
73
        
 
74
        if (!gnome_vfs_initialized())
 
75
                gnome_vfs_init();
 
76
        
 
77
        if (gtk_init_check (NULL, NULL))
 
78
                theme = gtk_icon_theme_get_default ();
 
79
        else
 
80
                g_warning ("gtk_init_check failed, icon lookup disabled.");
 
81
                
 
82
        info = gnome_vfs_file_info_new ();
 
83
        
 
84
        result = gnome_vfs_get_file_info (uri, info, 
 
85
                                GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
 
86
        
 
87
        if (ret != NULL)
 
88
                *ret = result;
 
89
        
 
90
        if (result != GNOME_VFS_OK) {
 
91
                g_warning ("Could not open uri: %s", uri);
 
92
                return NULL;
 
93
        }
 
94
        
 
95
        entry = g_new (AlarmListEntry, 1);
 
96
        entry->data = g_strdup (uri);
 
97
        entry->name = to_basename (info->name);
 
98
        
 
99
        if (theme != NULL)
 
100
                entry->icon = gnome_icon_lookup(theme, NULL,
 
101
                                                                                entry->data, NULL, info,
 
102
                                                                                info->mime_type, 0, 0);
 
103
        
 
104
        if (mime_ret != NULL)
 
105
                *mime_ret = g_strdup (info->mime_type);
 
106
        
 
107
        gnome_vfs_file_info_unref (info);
 
108
        
 
109
        return entry;
 
110
}
 
111
 
 
112
GList *
 
113
alarm_list_entry_list_new (const gchar *dir_uri, const gchar *supported_types[])
 
114
{
 
115
        GnomeVFSResult result;
 
116
        GnomeVFSFileInfoOptions options;
 
117
        GnomeVFSFileInfo *info;
 
118
        
 
119
        GtkIconTheme *theme;
 
120
        
 
121
        GList *list, *l, *flist;
 
122
        AlarmListEntry *entry;
 
123
        const gchar *mime;
 
124
        gboolean valid;
 
125
        gint i;
 
126
        
 
127
        if (!gnome_vfs_initialized())
 
128
                gnome_vfs_init();
 
129
        
 
130
        if (gtk_init_check (NULL, NULL))
 
131
                theme = gtk_icon_theme_get_default ();
 
132
        else
 
133
                g_warning ("gtk_init_check failed, icon lookup disabled.");
 
134
        
 
135
        options = GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS;
 
136
        
 
137
        result = gnome_vfs_directory_list_load (&list, dir_uri, options);
 
138
        
 
139
        if (result != GNOME_VFS_OK) {
 
140
                g_critical ("Could not open directory: %s", dir_uri);
 
141
                return NULL;
 
142
        }
 
143
        
 
144
        g_debug ("Loading files in %s ...", dir_uri);
 
145
        
 
146
        flist = NULL;
 
147
        
 
148
        for (l = list; l; l = l->next) {
 
149
                info = l->data;
 
150
                //g_debug ("-- %s", info->name);
 
151
                if (info->type == GNOME_VFS_FILE_TYPE_REGULAR) {
 
152
                        mime = gnome_vfs_file_info_get_mime_type(info);
 
153
                        //g_debug (" [ regular file: MIME: %s ]", mime);
 
154
                        
 
155
                        valid = TRUE;
 
156
                        if (supported_types != NULL) {
 
157
                                valid = FALSE;
 
158
                                for (i = 0; supported_types[i] != NULL; i++) {
 
159
                                        if (strstr (mime, supported_types[i]) != NULL) {
 
160
                                                // MATCH
 
161
                                                //g_debug (" [ MATCH ]");
 
162
                                                valid = TRUE;
 
163
                                                break;
 
164
                                        }
 
165
                                }
 
166
                        }
 
167
                        
 
168
                        if (valid) {
 
169
                                entry = g_new (AlarmListEntry, 1);
 
170
                                entry->data = g_strdup_printf ("%s/%s", dir_uri, info->name);
 
171
                                entry->name = to_basename (info->name);
 
172
                                
 
173
                                entry->icon = NULL;
 
174
                                if (theme != NULL)
 
175
                                        entry->icon = gnome_icon_lookup(theme, NULL,
 
176
                                                                                                        entry->data, NULL, info,
 
177
                                                                                                        mime, 0, 0);
 
178
                                
 
179
                                //g_debug ("Icon found: %s", entry->icon);
 
180
                                flist = g_list_append (flist, entry);
 
181
                        }
 
182
                }
 
183
        }
 
184
        
 
185
        gnome_vfs_file_info_list_free (list);
 
186
        
 
187
        return flist;
 
188
}
 
189
 
 
190
void
 
191
alarm_list_entry_list_free (GList **list)
 
192
{
 
193
        GList *l;
 
194
        AlarmListEntry *e;
 
195
        
 
196
        g_debug ("Alarm_file_entry_list_free (%p => %p)", list, *list);
 
197
        
 
198
        // Free data
 
199
        for (l = *list; l; l = l->next) {
 
200
                e = (AlarmListEntry *)l->data;
 
201
                alarm_list_entry_free (e);
 
202
        }
 
203
        
 
204
        g_list_free (*list);
 
205
        
 
206
        *list = NULL;
 
207
}