summaryrefslogtreecommitdiff
path: root/src/skins/skinselector.cc
blob: 4ecffbbf49172682011cc7b0f31930a7fe0104da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * ui_skinselector.c
 * Copyright 1998-2003 XMMS Development Team
 * Copyright 2003-2004 BMP Development Team
 * Copyright 2011 John Lindgren
 *
 * This file is part of Audacious.
 *
 * Audacious is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, version 2 or version 3 of the License.
 *
 * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Audacious. If not, see <http://www.gnu.org/licenses/>.
 *
 * The Audacious team does not consider modular code linking to Audacious or
 * using our public API to be a derived work.
 */

#include <stdlib.h>
#include <string.h>

#include <libaudcore/audstrings.h>
#include <libaudcore/i18n.h>
#include <libaudcore/runtime.h>
#include <libaudgui/libaudgui-gtk.h>

#include "plugin.h"
#include "skin.h"
#include "skinselector.h"
#include "util.h"
#include "view.h"

enum SkinViewCols {
    SKIN_VIEW_COL_PREVIEW,
    SKIN_VIEW_COL_FORMATTEDNAME,
    SKIN_VIEW_COL_NAME,
    SKIN_VIEW_N_COLS
};

struct SkinNode {
    String name, desc, path;
};

static Index<SkinNode> skinlist;

static void skin_view_on_cursor_changed (GtkTreeView * treeview);

static AudguiPixbuf skin_get_preview (const char * path)
{
    AudguiPixbuf preview;

    StringBuf archive_path;
    if (file_is_archive (path))
    {
        archive_path.steal (archive_decompress (path));
        if (! archive_path)
            return preview;

        path = archive_path;
    }

    StringBuf preview_path = skin_pixmap_locate (path, "main");
    if (preview_path)
        preview.capture (gdk_pixbuf_new_from_file (preview_path, nullptr));

    if (archive_path)
        del_directory (archive_path);

    return preview;
}

static AudguiPixbuf skin_get_thumbnail (const char * path)
{
    StringBuf base = filename_get_base (path);
    base.insert (-1, ".png");

    StringBuf thumbname = filename_build ({skins_get_skin_thumb_dir (), base});
    AudguiPixbuf thumb;

    if (g_file_test (thumbname, G_FILE_TEST_EXISTS))
        thumb.capture (gdk_pixbuf_new_from_file (thumbname, nullptr));

    if (! thumb)
    {
        thumb = skin_get_preview (path);

        if (thumb)
        {
            make_directory (skins_get_skin_thumb_dir ());
            gdk_pixbuf_save (thumb.get (), thumbname, "png", nullptr, nullptr);
        }
    }

    if (thumb)
        audgui_pixbuf_scale_within (thumb, audgui_get_dpi () * 3 / 2);

    return thumb;
}

static void scan_skindir_func (const char * path, const char * basename)
{
    if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
    {
        if (file_is_archive (path))
            skinlist.append (String (archive_basename (basename)),
             String (_("Archived Winamp 2.x skin")), String (path));
    }
    else if (g_file_test (path, G_FILE_TEST_IS_DIR))
        skinlist.append (String (basename),
         String (_("Unarchived Winamp 2.x skin")), String (path));
}

static void skinlist_update ()
{
    skinlist.clear ();

    const char * user_skin_dir = skins_get_user_skin_dir ();
    if (g_file_test (user_skin_dir, G_FILE_TEST_EXISTS))
        dir_foreach (user_skin_dir, scan_skindir_func);

    StringBuf path = filename_build ({aud_get_path (AudPath::DataDir), "Skins"});
    dir_foreach (path, scan_skindir_func);

    const char * skinsdir = getenv ("SKINSDIR");
    if (skinsdir)
    {
        for (const String & dir : str_list_to_index (skinsdir, ":"))
            dir_foreach (dir, scan_skindir_func);
    }

    skinlist.sort ([] (const SkinNode & a, const SkinNode & b)
        { return str_compare (a.name, b.name); });
}

void skin_view_update (GtkTreeView * treeview)
{
    g_signal_handlers_block_by_func (treeview, (void *) skin_view_on_cursor_changed, nullptr);

    auto store = (GtkListStore *) gtk_tree_view_get_model (treeview);
    gtk_list_store_clear (store);

    skinlist_update ();

    String current_path = aud_get_str ("skins", "skin");
    GtkTreePath * current_skin = nullptr;

    for (const SkinNode & node : skinlist)
    {
        AudguiPixbuf thumbnail = skin_get_thumbnail (node.path);
        StringBuf formattedname = str_concat ({"<big><b>", node.name,
         "</b></big>\n<i>", node.desc, "</i>"});

        GtkTreeIter iter;
        gtk_list_store_append (store, & iter);
        gtk_list_store_set (store, & iter,
         SKIN_VIEW_COL_PREVIEW, thumbnail.get (),
         SKIN_VIEW_COL_FORMATTEDNAME, (const char *) formattedname,
         SKIN_VIEW_COL_NAME, (const char *) node.name, -1);

        if (! current_skin && strstr (current_path, node.name))
            current_skin = gtk_tree_model_get_path ((GtkTreeModel *) store, & iter);
    }

    if (current_skin)
    {
        auto selection = gtk_tree_view_get_selection (treeview);
        gtk_tree_selection_select_path (selection, current_skin);
        gtk_tree_view_scroll_to_cell (treeview, current_skin, nullptr, true, 0.5, 0.5);
        gtk_tree_path_free (current_skin);
    }

    g_signal_handlers_unblock_by_func (treeview, (void *) skin_view_on_cursor_changed, nullptr);
}

static void skin_view_on_cursor_changed (GtkTreeView * treeview)
{
    GtkTreeModel * model;
    GtkTreeIter iter;

    auto selection = gtk_tree_view_get_selection (treeview);
    if (! gtk_tree_selection_get_selected (selection, & model, & iter))
        return;

    GtkTreePath * path = gtk_tree_model_get_path (model, & iter);
    int row = gtk_tree_path_get_indices (path)[0];
    g_return_if_fail (row >= 0 && row < skinlist.len ());
    gtk_tree_path_free (path);

    if (skin_load (skinlist[row].path))
        view_apply_skin ();
}

void skin_view_realize (GtkTreeView * treeview)
{
    gtk_widget_show_all ((GtkWidget *) treeview);

    gtk_tree_view_set_rules_hint (treeview, true);
    gtk_tree_view_set_headers_visible (treeview, false);

    GtkListStore * store = gtk_list_store_new (SKIN_VIEW_N_COLS, GDK_TYPE_PIXBUF,
     G_TYPE_STRING, G_TYPE_STRING);
    gtk_tree_view_set_model (treeview, (GtkTreeModel *) store);
    g_object_unref (store);

    GtkTreeViewColumn * column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
    gtk_tree_view_column_set_spacing (column, 16);
    gtk_tree_view_append_column (treeview, column);

    GtkCellRenderer * renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_tree_view_column_pack_start (column, renderer, false);
    gtk_tree_view_column_set_attributes (column, renderer, "pixbuf",
     SKIN_VIEW_COL_PREVIEW, nullptr);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_column_pack_start (column, renderer, true);
    gtk_tree_view_column_set_attributes (column, renderer, "markup",
     SKIN_VIEW_COL_FORMATTEDNAME, nullptr);

    GtkTreeSelection * selection = gtk_tree_view_get_selection (treeview);
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);

    g_signal_connect (treeview, "cursor-changed",
     (GCallback) skin_view_on_cursor_changed, nullptr);
}