lms plugin: add trackable container support (based on lms update id signal)
[profile/ivi/rygel.git] / src / plugins / lms / rygel-lms-albums.vala
1 /*
2  * Copyright (C) 2013 Intel Corporation.
3  *
4  * Author: Jussi Kukkonen <jussi.kukkonen@intel.com>
5  *
6  * This file is part of Rygel.
7  *
8  * Rygel is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Rygel is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 using Rygel;
24 using Sqlite;
25
26 public class Rygel.LMS.Albums : Rygel.LMS.CategoryContainer {
27     private static const string SQL_ALL =
28         "SELECT audio_albums.id, audio_albums.name as title, " +
29                "audio_artists.name as artist " +
30         "FROM audio_albums " +
31         "LEFT JOIN audio_artists " +
32         "ON audio_albums.artist_id = audio_artists.id " +
33         "LIMIT ? OFFSET ?;";
34
35     private static const string SQL_ALL_WITH_FILTER_TEMPLATE =
36         "SELECT audio_albums.id, audio_albums.name as title, " +
37                "audio_artists.name as artist " +
38         "FROM audio_albums " +
39         "LEFT JOIN audio_artists " +
40         "ON audio_albums.artist_id = audio_artists.id " +
41         "WHERE %s " +
42         "LIMIT ? OFFSET ?;";
43
44     private static const string SQL_COUNT =
45         "SELECT COUNT(audio_albums.id) " +
46         "FROM audio_albums;";
47
48     private static const string SQL_COUNT_WITH_FILTER_TEMPLATE =
49         "SELECT COUNT(audio_albums.id), audio_albums.name as title, " +
50                "audio_artists.name as artist " +
51         "FROM audio_albums " +
52         "LEFT JOIN audio_artists " +
53         "ON audio_albums.artist_id = audio_artists.id " +
54         "WHERE %s;";
55
56     /* count songs inside albums */
57     private static const string SQL_CHILD_COUNT_WITH_FILTER_TEMPLATE =
58         "SELECT COUNT(audios.id), audios.title as title, " +
59                "audio_artists.name as artist " +
60         "FROM audios, files, audio_albums " +
61         "LEFT JOIN audio_artists " +
62         "ON audios.artist_id = audio_artists.id " +
63         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = audio_albums.id %s;";
64
65     /* select songs inside albums */
66     private static const string SQL_CHILD_ALL_WITH_FILTER_TEMPLATE =
67         "SELECT files.id, files.path, files.size, " +
68                "audios.title as title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " +
69                "audio_artists.name as artist, " +
70                "audio_albums.name, audio_albums.id " +
71         "FROM audios, files, audio_albums " +
72         "LEFT JOIN audio_artists " +
73         "ON audios.artist_id = audio_artists.id " +
74         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = audio_albums.id %s " +
75         "LIMIT ? OFFSET ?;";
76
77
78     private static const string SQL_FIND_OBJECT =
79         "SELECT audio_albums.id, audio_albums.name " +
80         "FROM audio_albums " +
81         "WHERE audio_albums.id = ?;";
82
83     protected override string get_sql_all_with_filter (string filter) {
84         if (filter.length == 0) {
85             return Albums.SQL_ALL;
86         }
87         return (Albums.SQL_ALL_WITH_FILTER_TEMPLATE.printf (filter));
88     }
89
90     protected override string get_sql_count_with_filter (string filter) {
91         if (filter.length == 0) {
92             return Albums.SQL_COUNT;
93         }
94         return (Albums.SQL_COUNT_WITH_FILTER_TEMPLATE.printf (filter));
95     }
96
97     protected override uint get_child_count_with_filter (string     where_filter,
98                                                         ValueArray args)
99     {
100
101         /* search the children (albums) as usual */
102         var count = base.get_child_count_with_filter (where_filter, args);
103
104         /* now search the album contents */
105         var filter = "";
106         if (where_filter.length > 0) {
107             filter = "AND %s".printf (where_filter);
108         }
109         var query = Albums.SQL_CHILD_COUNT_WITH_FILTER_TEMPLATE.printf (filter);
110         try {
111             var stmt = this.lms_db.prepare_and_init (query, args.values);
112             if (stmt.step () == Sqlite.ROW) {
113                 count += stmt.column_int (0);
114             }
115         } catch (DatabaseError e) {
116             warning ("Query failed: %s", e.message);
117         }
118
119         return count;
120     }
121
122     protected override MediaObjects? get_children_with_filter (string     where_filter,
123                                                                ValueArray args,
124                                                                string     sort_criteria,
125                                                                uint       offset,
126                                                                uint       max_count) {
127         var children = base. get_children_with_filter (where_filter,
128                                                        args,
129                                                        sort_criteria,
130                                                        offset,
131                                                        max_count);
132         var filter = "";
133         if (where_filter.length > 0) {
134             filter = "AND %s".printf (where_filter);
135         }
136         var query = Albums.SQL_CHILD_ALL_WITH_FILTER_TEMPLATE.printf (filter);
137         try {
138             var stmt = this.lms_db.prepare_and_init (query, args.values);
139             while (Database.get_children_step (stmt)) {
140                 var album_id = stmt.column_text (13);
141                 var album = new Album (album_id, this, "", this.lms_db);
142
143                 var song = album.object_from_statement (stmt);
144                 song.parent_ref = song.parent;
145                 children.add (song);
146                 
147             }
148         } catch (DatabaseError e) {
149             warning ("Query failed: %s", e.message);
150         }
151
152         return children;
153     }
154
155     protected override MediaObject? object_from_statement (Statement statement) {
156         var id = "%d".printf (statement.column_int (0));
157         LMS.Album album = new LMS.Album (id,
158                                          this,
159                                          statement.column_text (1),
160                                          this.lms_db);
161         return album;
162     }
163
164     public Albums (MediaContainer parent,
165                    LMS.Database   lms_db) {
166         base ("albums",
167               parent,
168               _("Albums"),
169               lms_db,
170               Albums.SQL_ALL,
171               Albums.SQL_FIND_OBJECT,
172               Albums.SQL_COUNT,
173               null, null);
174     }
175 }