~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/db_print.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License along
16
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "db_print.h"
22
 
#include "db_selection.h"
23
 
#include "db_visitor.h"
24
 
#include "locate.h"
25
 
#include "directory.h"
26
 
#include "database.h"
27
 
#include "client.h"
28
 
#include "song.h"
29
 
#include "song_print.h"
30
 
#include "playlist_vector.h"
31
 
#include "tag.h"
32
 
#include "strset.h"
33
 
 
34
 
#include <glib.h>
35
 
 
36
 
typedef struct _ListCommandItem {
37
 
        int8_t tagType;
38
 
        const struct locate_item_list *criteria;
39
 
} ListCommandItem;
40
 
 
41
 
typedef struct _SearchStats {
42
 
        const struct locate_item_list *criteria;
43
 
        int numberOfSongs;
44
 
        unsigned long playTime;
45
 
} SearchStats;
46
 
 
47
 
static bool
48
 
print_visitor_directory(const struct directory *directory, void *data,
49
 
                        G_GNUC_UNUSED GError **error_r)
50
 
{
51
 
        struct client *client = data;
52
 
 
53
 
        if (!directory_is_root(directory))
54
 
                client_printf(client, "directory: %s\n", directory_get_path(directory));
55
 
 
56
 
        return true;
57
 
}
58
 
 
59
 
static void
60
 
print_playlist_in_directory(struct client *client,
61
 
                            const struct directory *directory,
62
 
                            const char *name_utf8)
63
 
{
64
 
        if (directory_is_root(directory))
65
 
                client_printf(client, "playlist: %s\n", name_utf8);
66
 
        else
67
 
                client_printf(client, "playlist: %s/%s\n",
68
 
                              directory_get_path(directory), name_utf8);
69
 
}
70
 
 
71
 
static bool
72
 
print_visitor_song(struct song *song, void *data,
73
 
                   G_GNUC_UNUSED GError **error_r)
74
 
{
75
 
        assert(song != NULL);
76
 
        assert(song->parent != NULL);
77
 
 
78
 
        struct client *client = data;
79
 
        song_print_uri(client, song);
80
 
 
81
 
        if (song->tag != NULL && song->tag->has_playlist)
82
 
                /* this song file has an embedded CUE sheet */
83
 
                print_playlist_in_directory(client, song->parent,
84
 
                                            song->uri);
85
 
 
86
 
        return true;
87
 
}
88
 
 
89
 
static bool
90
 
print_visitor_song_info(struct song *song, void *data,
91
 
                        G_GNUC_UNUSED GError **error_r)
92
 
{
93
 
        assert(song != NULL);
94
 
        assert(song->parent != NULL);
95
 
 
96
 
        struct client *client = data;
97
 
        song_print_info(client, song);
98
 
 
99
 
        if (song->tag != NULL && song->tag->has_playlist)
100
 
                /* this song file has an embedded CUE sheet */
101
 
                print_playlist_in_directory(client, song->parent,
102
 
                                            song->uri);
103
 
 
104
 
        return true;
105
 
}
106
 
 
107
 
static bool
108
 
print_visitor_playlist(const struct playlist_metadata *playlist,
109
 
                       const struct directory *directory, void *ctx,
110
 
                       G_GNUC_UNUSED GError **error_r)
111
 
{
112
 
        struct client *client = ctx;
113
 
        print_playlist_in_directory(client, directory, playlist->name);
114
 
        return true;
115
 
}
116
 
 
117
 
static bool
118
 
print_visitor_playlist_info(const struct playlist_metadata *playlist,
119
 
                            const struct directory *directory,
120
 
                            void *ctx, G_GNUC_UNUSED GError **error_r)
121
 
{
122
 
        struct client *client = ctx;
123
 
        print_playlist_in_directory(client, directory, playlist->name);
124
 
 
125
 
#ifndef G_OS_WIN32
126
 
        struct tm tm;
127
 
#endif
128
 
        char timestamp[32];
129
 
        time_t t = playlist->mtime;
130
 
        strftime(timestamp, sizeof(timestamp),
131
 
#ifdef G_OS_WIN32
132
 
                 "%Y-%m-%dT%H:%M:%SZ",
133
 
                 gmtime(&t)
134
 
#else
135
 
                 "%FT%TZ",
136
 
                 gmtime_r(&t, &tm)
137
 
#endif
138
 
                 );
139
 
        client_printf(client, "Last-Modified: %s\n", timestamp);
140
 
 
141
 
        return true;
142
 
}
143
 
 
144
 
static const struct db_visitor print_visitor = {
145
 
        .directory = print_visitor_directory,
146
 
        .song = print_visitor_song,
147
 
        .playlist = print_visitor_playlist,
148
 
};
149
 
 
150
 
static const struct db_visitor print_info_visitor = {
151
 
        .directory = print_visitor_directory,
152
 
        .song = print_visitor_song_info,
153
 
        .playlist = print_visitor_playlist_info,
154
 
};
155
 
 
156
 
bool
157
 
db_selection_print(struct client *client, const struct db_selection *selection,
158
 
                   bool full, GError **error_r)
159
 
{
160
 
        return db_visit(selection, full ? &print_info_visitor : &print_visitor,
161
 
                        client, error_r);
162
 
}
163
 
 
164
 
struct search_data {
165
 
        struct client *client;
166
 
        const struct locate_item_list *criteria;
167
 
};
168
 
 
169
 
static bool
170
 
search_visitor_song(struct song *song, void *_data,
171
 
                    G_GNUC_UNUSED GError **error_r)
172
 
{
173
 
        struct search_data *data = _data;
174
 
 
175
 
        if (locate_song_search(song, data->criteria))
176
 
                song_print_info(data->client, song);
177
 
 
178
 
        return true;
179
 
}
180
 
 
181
 
static const struct db_visitor search_visitor = {
182
 
        .song = search_visitor_song,
183
 
};
184
 
 
185
 
bool
186
 
searchForSongsIn(struct client *client, const char *name,
187
 
                 const struct locate_item_list *criteria,
188
 
                 GError **error_r)
189
 
{
190
 
        struct locate_item_list *new_list
191
 
                = locate_item_list_casefold(criteria);
192
 
        struct search_data data;
193
 
 
194
 
        data.client = client;
195
 
        data.criteria = new_list;
196
 
 
197
 
        bool success = db_walk(name, &search_visitor, &data, error_r);
198
 
 
199
 
        locate_item_list_free(new_list);
200
 
 
201
 
        return success;
202
 
}
203
 
 
204
 
static bool
205
 
find_visitor_song(struct song *song, void *_data,
206
 
                  G_GNUC_UNUSED GError **error_r)
207
 
{
208
 
        struct search_data *data = _data;
209
 
 
210
 
        if (locate_song_match(song, data->criteria))
211
 
                song_print_info(data->client, song);
212
 
 
213
 
        return true;
214
 
}
215
 
 
216
 
static const struct db_visitor find_visitor = {
217
 
        .song = find_visitor_song,
218
 
};
219
 
 
220
 
bool
221
 
findSongsIn(struct client *client, const char *name,
222
 
            const struct locate_item_list *criteria,
223
 
            GError **error_r)
224
 
{
225
 
        struct search_data data;
226
 
 
227
 
        data.client = client;
228
 
        data.criteria = criteria;
229
 
 
230
 
        return db_walk(name, &find_visitor, &data, error_r);
231
 
}
232
 
 
233
 
static void printSearchStats(struct client *client, SearchStats *stats)
234
 
{
235
 
        client_printf(client, "songs: %i\n", stats->numberOfSongs);
236
 
        client_printf(client, "playtime: %li\n", stats->playTime);
237
 
}
238
 
 
239
 
static bool
240
 
stats_visitor_song(struct song *song, void *data,
241
 
                   G_GNUC_UNUSED GError **error_r)
242
 
{
243
 
        SearchStats *stats = data;
244
 
 
245
 
        if (locate_song_match(song, stats->criteria)) {
246
 
                stats->numberOfSongs++;
247
 
                stats->playTime += song_get_duration(song);
248
 
        }
249
 
 
250
 
        return true;
251
 
}
252
 
 
253
 
static const struct db_visitor stats_visitor = {
254
 
        .song = stats_visitor_song,
255
 
};
256
 
 
257
 
bool
258
 
searchStatsForSongsIn(struct client *client, const char *name,
259
 
                      const struct locate_item_list *criteria,
260
 
                      GError **error_r)
261
 
{
262
 
        SearchStats stats;
263
 
 
264
 
        stats.criteria = criteria;
265
 
        stats.numberOfSongs = 0;
266
 
        stats.playTime = 0;
267
 
 
268
 
        if (!db_walk(name, &stats_visitor, &stats, error_r))
269
 
                return false;
270
 
 
271
 
        printSearchStats(client, &stats);
272
 
        return true;
273
 
}
274
 
 
275
 
bool
276
 
printAllIn(struct client *client, const char *uri_utf8, GError **error_r)
277
 
{
278
 
        struct db_selection selection;
279
 
        db_selection_init(&selection, uri_utf8, true);
280
 
        return db_selection_print(client, &selection, false, error_r);
281
 
}
282
 
 
283
 
bool
284
 
printInfoForAllIn(struct client *client, const char *uri_utf8,
285
 
                  GError **error_r)
286
 
{
287
 
        struct db_selection selection;
288
 
        db_selection_init(&selection, uri_utf8, true);
289
 
        return db_selection_print(client, &selection, true, error_r);
290
 
}
291
 
 
292
 
static ListCommandItem *
293
 
newListCommandItem(int tagType, const struct locate_item_list *criteria)
294
 
{
295
 
        ListCommandItem *item = g_new(ListCommandItem, 1);
296
 
 
297
 
        item->tagType = tagType;
298
 
        item->criteria = criteria;
299
 
 
300
 
        return item;
301
 
}
302
 
 
303
 
static void freeListCommandItem(ListCommandItem * item)
304
 
{
305
 
        g_free(item);
306
 
}
307
 
 
308
 
static void
309
 
visitTag(struct client *client, struct strset *set,
310
 
         struct song *song, enum tag_type tagType)
311
 
{
312
 
        struct tag *tag = song->tag;
313
 
        bool found = false;
314
 
 
315
 
        if (tagType == LOCATE_TAG_FILE_TYPE) {
316
 
                song_print_uri(client, song);
317
 
                return;
318
 
        }
319
 
 
320
 
        if (!tag)
321
 
                return;
322
 
 
323
 
        for (unsigned i = 0; i < tag->num_items; i++) {
324
 
                if (tag->items[i]->type == tagType) {
325
 
                        strset_add(set, tag->items[i]->value);
326
 
                        found = true;
327
 
                }
328
 
        }
329
 
 
330
 
        if (!found)
331
 
                strset_add(set, "");
332
 
}
333
 
 
334
 
struct list_tags_data {
335
 
        struct client *client;
336
 
        ListCommandItem *item;
337
 
        struct strset *set;
338
 
};
339
 
 
340
 
static bool
341
 
unique_tags_visitor_song(struct song *song, void *_data,
342
 
                         G_GNUC_UNUSED GError **error_r)
343
 
{
344
 
        struct list_tags_data *data = _data;
345
 
        ListCommandItem *item = data->item;
346
 
 
347
 
        if (locate_song_match(song, item->criteria))
348
 
                visitTag(data->client, data->set, song, item->tagType);
349
 
 
350
 
        return true;
351
 
}
352
 
 
353
 
static const struct db_visitor unique_tags_visitor = {
354
 
        .song = unique_tags_visitor_song,
355
 
};
356
 
 
357
 
bool
358
 
listAllUniqueTags(struct client *client, int type,
359
 
                  const struct locate_item_list *criteria,
360
 
                  GError **error_r)
361
 
{
362
 
        ListCommandItem *item = newListCommandItem(type, criteria);
363
 
        struct list_tags_data data = {
364
 
                .client = client,
365
 
                .item = item,
366
 
        };
367
 
 
368
 
        if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
369
 
                data.set = strset_new();
370
 
        }
371
 
 
372
 
        if (!db_walk("", &unique_tags_visitor, &data, error_r)) {
373
 
                freeListCommandItem(item);
374
 
                return false;
375
 
        }
376
 
 
377
 
        if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
378
 
                const char *value;
379
 
 
380
 
                strset_rewind(data.set);
381
 
 
382
 
                while ((value = strset_next(data.set)) != NULL)
383
 
                        client_printf(client, "%s: %s\n",
384
 
                                      tag_item_names[type],
385
 
                                      value);
386
 
 
387
 
                strset_free(data.set);
388
 
        }
389
 
 
390
 
        freeListCommandItem(item);
391
 
 
392
 
        return true;
393
 
}