summaryrefslogtreecommitdiff
path: root/src/plugins/lms/rygel-lms-albums.vala
blob: cf2fe51c00e4305f91aa015e62b161be2f6c1ce4 (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
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Jussi Kukkonen <jussi.kukkonen@intel.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using Rygel;
using Rygel.Database;
using Sqlite;

public class Rygel.LMS.Albums : Rygel.LMS.CategoryContainer {
    private static const string SQL_ALL =
        "SELECT audio_albums.id, audio_albums.name as title, " +
               "audio_artists.name as artist " +
        "FROM audio_albums " +
        "LEFT JOIN audio_artists " +
        "ON audio_albums.artist_id = audio_artists.id " +
        "LIMIT ? OFFSET ?;";

    private static const string SQL_ALL_WITH_FILTER_TEMPLATE =
        "SELECT audio_albums.id, audio_albums.name as title, " +
               "audio_artists.name as artist " +
        "FROM audio_albums " +
        "LEFT JOIN audio_artists " +
        "ON audio_albums.artist_id = audio_artists.id " +
        "WHERE %s " +
        "LIMIT ? OFFSET ?;";

    private static const string SQL_COUNT =
        "SELECT COUNT(audio_albums.id) " +
        "FROM audio_albums;";

    private static const string SQL_COUNT_WITH_FILTER_TEMPLATE =
        "SELECT COUNT(audio_albums.id), audio_albums.name as title, " +
               "audio_artists.name as artist " +
        "FROM audio_albums " +
        "LEFT JOIN audio_artists " +
        "ON audio_albums.artist_id = audio_artists.id " +
        "WHERE %s;";

    /* count songs inside albums */
    private static const string SQL_CHILD_COUNT_WITH_FILTER_TEMPLATE =
        "SELECT COUNT(audios.id), audios.title as title, " +
               "audio_artists.name as artist " +
        "FROM audios, files, audio_albums " +
        "LEFT JOIN audio_artists " +
        "ON audios.artist_id = audio_artists.id " +
        "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = audio_albums.id %s;";

    /* select songs inside albums */
    private static const string SQL_CHILD_ALL_WITH_FILTER_TEMPLATE =
        "SELECT files.id, files.path, files.size, " +
               "audios.title as title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " +
               "audio_artists.name as artist, " +
               "audio_albums.name, audio_albums.id " +
        "FROM audios, files, audio_albums " +
        "LEFT JOIN audio_artists " +
        "ON audios.artist_id = audio_artists.id " +
        "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = audio_albums.id %s " +
        "LIMIT ? OFFSET ?;";


    private static const string SQL_FIND_OBJECT =
        "SELECT audio_albums.id, audio_albums.name " +
        "FROM audio_albums " +
        "WHERE audio_albums.id = ?;";

    protected override string get_sql_all_with_filter (string filter) {
        if (filter.length == 0) {
            return Albums.SQL_ALL;
        }
        return (Albums.SQL_ALL_WITH_FILTER_TEMPLATE.printf (filter));
    }

    protected override string get_sql_count_with_filter (string filter) {
        if (filter.length == 0) {
            return Albums.SQL_COUNT;
        }
        return (Albums.SQL_COUNT_WITH_FILTER_TEMPLATE.printf (filter));
    }

    protected override uint get_child_count_with_filter (string     where_filter,
                                                         ValueArray args) {

        /* search the children (albums) as usual */
        var count = base.get_child_count_with_filter (where_filter, args);

        /* now search the album contents */
        var filter = "";
        if (where_filter.length > 0) {
            filter = "AND %s".printf (where_filter);
        }
        var query = Albums.SQL_CHILD_COUNT_WITH_FILTER_TEMPLATE.printf (filter);
        try {
            count += this.lms_db.query_value (query, args.values);
        } catch (DatabaseError e) {
            warning ("Query failed: %s", e.message);
        }

        return count;
    }

    protected override MediaObjects? get_children_with_filter
                                            (string     where_filter,
                                             ValueArray args,
                                             string     sort_criteria,
                                             uint       offset,
                                             uint       max_count) {
        var children = base. get_children_with_filter (where_filter,
                                                       args,
                                                       sort_criteria,
                                                       offset,
                                                       max_count);
        var filter = "";
        if (where_filter.length > 0) {
            filter = "AND %s".printf (where_filter);
        }
        var query = Albums.SQL_CHILD_ALL_WITH_FILTER_TEMPLATE.printf (filter);
        try {
            var cursor = this.lms_db.exec_cursor (query, args.values);
            foreach (var stmt in cursor) {
                var album_id = stmt.column_text (13);
                var album = new Album (album_id, this, "", this.lms_db);

                var song = album.object_from_statement (stmt);
                song.parent_ref = song.parent;
                children.add (song);

            }
        } catch (DatabaseError e) {
            warning ("Query failed: %s", e.message);
        }

        return children;
    }

    protected override MediaObject? object_from_statement (Statement statement) {
        var id = "%d".printf (statement.column_int (0));
        LMS.Album album = new LMS.Album (id,
                                         this,
                                         statement.column_text (1),
                                         this.lms_db);
        return album;
    }

    public Albums (MediaContainer parent,
                   LMS.Database   lms_db) {
        base ("albums",
              parent,
              _("Albums"),
              lms_db,
              Albums.SQL_ALL,
              Albums.SQL_FIND_OBJECT,
              Albums.SQL_COUNT,
              null, null);
    }
}