comparison audacious/skinwin.c @ 0:cb178e5ad177 trunk

[svn] Import audacious source.
author nenolod
date Mon, 24 Oct 2005 03:06:47 -0700
parents
children bf803441d49e
comparison
equal deleted inserted replaced
-1:000000000000 0:cb178e5ad177
1 /* BMP - Cross-platform multimedia player
2 * Copyright (C) 2003-2004 BMP development team.
3 *
4 * Based on XMMS:
5 * Copyright (C) 1998-2003 XMMS development team.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "skinwin.h"
27
28 #include <glib.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdk.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "main.h"
36 #include "skin.h"
37 #include "util.h"
38
39 #include <gdk/gdkx.h>
40
41
42 #define THUMBNAIL_WIDTH 90
43 #define THUMBNAIL_HEIGHT 40
44
45
46 enum SkinViewCols {
47 SKIN_VIEW_COL_PREVIEW,
48 SKIN_VIEW_COL_NAME,
49 SKIN_VIEW_N_COLS
50 };
51
52
53 GList *skinlist = NULL;
54
55
56
57 static gchar *
58 get_thumbnail_filename(const gchar * path)
59 {
60 gchar *basename, *pngname, *thumbname;
61
62 g_return_val_if_fail(path != NULL, NULL);
63
64 basename = g_path_get_basename(path);
65 pngname = g_strconcat(basename, ".png", NULL);
66
67 thumbname = g_build_filename(bmp_paths[BMP_PATH_SKIN_THUMB_DIR],
68 pngname, NULL);
69
70 g_free(basename);
71 g_free(pngname);
72
73 return thumbname;
74 }
75
76
77 static GdkPixbuf *
78 skin_get_preview(const gchar * path)
79 {
80 GdkPixbuf *preview = NULL;
81 gchar *dec_path, *preview_path;
82 gboolean is_archive = FALSE;
83
84 if (file_is_archive(path)) {
85 if (!(dec_path = archive_decompress(path)))
86 return NULL;
87
88 is_archive = TRUE;
89 }
90 else {
91 dec_path = g_strdup(path);
92 }
93
94 preview_path = find_file_recursively(dec_path, "main.bmp");
95
96 if (preview_path) {
97 preview = gdk_pixbuf_new_from_file(preview_path, NULL);
98 g_free(preview_path);
99 }
100
101 if (is_archive)
102 del_directory(dec_path);
103
104 g_free(dec_path);
105
106 return preview;
107 }
108
109
110 static GdkPixbuf *
111 skin_get_thumbnail(const gchar * path)
112 {
113 GdkPixbuf *scaled = NULL;
114 GdkPixbuf *preview;
115 gchar *thumbname;
116
117 g_return_val_if_fail(path != NULL, NULL);
118
119 if (g_str_has_suffix(path, BMP_SKIN_THUMB_DIR_BASENAME))
120 return NULL;
121
122 thumbname = get_thumbnail_filename(path);
123
124 if (g_file_test(thumbname, G_FILE_TEST_EXISTS)) {
125 scaled = gdk_pixbuf_new_from_file(thumbname, NULL);
126 g_free(thumbname);
127 return scaled;
128 }
129
130 if (!(preview = skin_get_preview(path))) {
131 g_free(thumbname);
132 return NULL;
133 }
134
135 scaled = gdk_pixbuf_scale_simple(preview,
136 THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
137 GDK_INTERP_BILINEAR);
138 g_object_unref(preview);
139
140 gdk_pixbuf_save(scaled, thumbname, "png", NULL, NULL);
141 g_free(thumbname);
142
143 return scaled;
144 }
145
146 static void
147 skinlist_add(const gchar * filename)
148 {
149 SkinNode *node;
150 gchar *basename;
151
152 g_return_if_fail(filename != NULL);
153
154 node = g_new0(SkinNode, 1);
155 node->path = g_strdup(filename);
156
157 basename = g_path_get_basename(filename);
158
159 if (file_is_archive(filename)) {
160 node->name = archive_basename(basename);
161 g_free(basename);
162 }
163 else {
164 node->name = basename;
165 }
166
167 skinlist = g_list_prepend(skinlist, node);
168 }
169
170 static gboolean
171 scan_skindir_func(const gchar * path, const gchar * basename, gpointer data)
172 {
173 if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
174 if (file_is_archive(path)) {
175 skinlist_add(path);
176 }
177 }
178 else if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
179 skinlist_add(path);
180 }
181
182 return FALSE;
183 }
184
185 static void
186 scan_skindir(const gchar * path)
187 {
188 GError *error = NULL;
189
190 g_return_if_fail(path != NULL);
191
192 if (path[0] == '.')
193 return;
194
195 if (!dir_foreach(path, scan_skindir_func, NULL, &error)) {
196 g_warning("Failed to open directory (%s): %s", path, error->message);
197 g_error_free(error);
198 return;
199 }
200 }
201
202 static gint
203 skinlist_compare_func(gconstpointer a, gconstpointer b)
204 {
205 g_return_val_if_fail(a != NULL && SKIN_NODE(a)->name != NULL, 1);
206 g_return_val_if_fail(b != NULL && SKIN_NODE(b)->name != NULL, 1);
207 return strcasecmp(SKIN_NODE(a)->name, SKIN_NODE(b)->name);
208 }
209
210 static void
211 skin_free_func(gpointer data)
212 {
213 g_return_if_fail(data != NULL);
214 g_free(SKIN_NODE(data)->name);
215 g_free(SKIN_NODE(data)->path);
216 g_free(data);
217 }
218
219
220 static void
221 skinlist_clear(void)
222 {
223 if (!skinlist)
224 return;
225
226 g_list_foreach(skinlist, (GFunc) skin_free_func, NULL);
227 g_list_free(skinlist);
228 skinlist = NULL;
229 }
230
231 void
232 skinlist_update(void)
233 {
234 gchar *skinsdir;
235
236 skinlist_clear();
237
238 scan_skindir(bmp_paths[BMP_PATH_USER_SKIN_DIR]);
239 scan_skindir(DATA_DIR G_DIR_SEPARATOR_S BMP_SKIN_DIR_BASENAME);
240
241 skinsdir = getenv("SKINSDIR");
242 if (skinsdir) {
243 gchar **dir_list = g_strsplit(skinsdir, ":", 0);
244 gchar **dir;
245
246 for (dir = dir_list; *dir; dir++)
247 scan_skindir(*dir);
248 g_strfreev(dir_list);
249 }
250
251 skinlist = g_list_sort(skinlist, skinlist_compare_func);
252
253 g_assert(skinlist != NULL);
254 }
255
256 void
257 skin_view_update(GtkTreeView * treeview)
258 {
259 GtkTreeSelection *selection = NULL;
260 GtkListStore *store;
261 GtkTreeIter iter, iter_current_skin;
262 GtkTreePath *path;
263
264 GdkPixbuf *thumbnail;
265 gchar *name;
266 GList *entry;
267
268 gtk_widget_set_sensitive(GTK_WIDGET(treeview), FALSE);
269
270 store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
271
272 gtk_list_store_clear(store);
273
274 skinlist_update();
275
276 for (entry = skinlist; entry; entry = g_list_next(entry)) {
277 thumbnail = skin_get_thumbnail(SKIN_NODE(entry->data)->path);
278
279 if (!thumbnail)
280 continue;
281
282 name = SKIN_NODE(entry->data)->name;
283
284 gtk_list_store_append(store, &iter);
285 gtk_list_store_set(store, &iter,
286 SKIN_VIEW_COL_PREVIEW, thumbnail,
287 SKIN_VIEW_COL_NAME, name, -1);
288 g_object_unref(thumbnail);
289
290 if (g_strstr_len(bmp_active_skin->path,
291 strlen(bmp_active_skin->path), name)) {
292 iter_current_skin = iter;
293 }
294
295 while (gtk_events_pending())
296 gtk_main_iteration();
297 }
298
299 selection = gtk_tree_view_get_selection(treeview);
300 gtk_tree_selection_select_iter(selection, &iter_current_skin);
301
302 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter_current_skin);
303 gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.5);
304 gtk_tree_path_free(path);
305
306 gtk_widget_set_sensitive(GTK_WIDGET(treeview), TRUE);
307 }
308
309
310 static void
311 skin_view_on_cursor_changed(GtkTreeView * treeview,
312 gpointer data)
313 {
314 GtkTreeModel *model;
315 GtkTreeSelection *selection;
316 GtkTreeIter iter;
317
318 GList *node;
319 gchar *name;
320 gchar *comp = NULL;
321
322 selection = gtk_tree_view_get_selection(treeview);
323 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
324 return;
325
326 gtk_tree_model_get(model, &iter, SKIN_VIEW_COL_NAME, &name, -1);
327
328 /* FIXME: store name in skinlist */
329 for (node = skinlist; node; node = g_list_next(node)) {
330 comp = SKIN_NODE(node->data)->path;
331 if (g_strrstr(comp, name))
332 break;
333 }
334
335 g_free(name);
336
337 bmp_active_skin_load(comp);
338 }
339
340
341 void
342 skin_view_realize(GtkTreeView * treeview)
343 {
344 GtkListStore *store;
345 GtkTreeViewColumn *column;
346 GtkCellRenderer *renderer;
347 GtkTreeSelection *selection;
348
349 gtk_widget_show_all(GTK_WIDGET(treeview));
350
351 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
352 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
353
354 store = gtk_list_store_new(SKIN_VIEW_N_COLS, GDK_TYPE_PIXBUF,
355 G_TYPE_STRING);
356 gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store));
357
358 column = gtk_tree_view_column_new();
359 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
360 gtk_tree_view_column_set_spacing(column, 16);
361 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
362 GTK_TREE_VIEW_COLUMN(column));
363
364 renderer = gtk_cell_renderer_pixbuf_new();
365 gtk_tree_view_column_pack_start(column, renderer, FALSE);
366 gtk_tree_view_column_set_attributes(column, renderer, "pixbuf",
367 SKIN_VIEW_COL_PREVIEW, NULL);
368
369 renderer = gtk_cell_renderer_text_new();
370 gtk_tree_view_column_pack_start(column, renderer, TRUE);
371 gtk_tree_view_column_set_attributes(column, renderer, "text",
372 SKIN_VIEW_COL_NAME, NULL);
373
374 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
375 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
376
377 g_signal_connect(treeview, "cursor-changed",
378 G_CALLBACK(skin_view_on_cursor_changed), NULL);
379 }