lms plugin: add trackable container support (based on lms update id signal)
[profile/ivi/rygel.git] / src / plugins / lms / rygel-lms-album.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.Album : Rygel.LMS.CategoryContainer {
27     private static const string SQL_ALL_TEMPLATE = 
28         "SELECT files.id, files.path, files.size, " +
29                "audios.title as title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " +
30                "audio_artists.name as artist, " +
31                "audio_albums.name " +
32         "FROM audios, files " +
33         "LEFT JOIN audio_artists " +
34         "ON audios.artist_id = audio_artists.id " +
35         "LEFT JOIN audio_albums " +
36         "ON audios.album_id = audio_albums.id " +
37         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = %s " +
38         "LIMIT ? OFFSET ?;";
39
40     private static const string SQL_COUNT_TEMPLATE =
41         "SELECT COUNT(audios.id) " +
42         "FROM audios, files " +
43         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = %s;";
44
45     private static const string SQL_COUNT_WITH_FILTER_TEMPLATE =
46         "SELECT COUNT(audios.id), audios.title as title, " +
47                "audio_artists.name as artist, " +
48                "audio_albums.name " +
49         "FROM audios, files " +
50         "LEFT JOIN audio_artists " +
51         "ON audios.artist_id = audio_artists.id " +
52         "LEFT JOIN audio_albums " +
53         "ON audios.album_id = audio_albums.id " +
54         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = %s;";
55
56     private static const string SQL_FIND_OBJECT_TEMPLATE =
57         "SELECT files.id, files.path, files.size, " +
58                "audios.title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " + 
59                "audio_artists.name, " +
60                "audio_albums.name " +
61         "FROM audios, files " +
62         "LEFT JOIN audio_artists " +
63         "ON audios.artist_id = audio_artists.id " +
64         "LEFT JOIN audio_albums " +
65         "ON audios.album_id = audio_albums.id " +
66         "WHERE dtime = 0 AND files.id = ? AND audios.id = files.id AND audios.album_id = %s;";
67
68     private static const string SQL_ADDED_TEMPLATE =
69         "SELECT files.id, files.path, files.size, " +
70                "audios.title as title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " +
71                "audio_artists.name as artist, " +
72                "audio_albums.name " +
73         "FROM audios, files " +
74         "LEFT JOIN audio_artists " +
75         "ON audios.artist_id = audio_artists.id " +
76         "LEFT JOIN audio_albums " +
77         "ON audios.album_id = audio_albums.id " +
78         "WHERE dtime = 0 AND audios.id = files.id AND audios.album_id = %s " +
79         "AND update_id > ? AND update_id <= ?;";
80
81     private static const string SQL_REMOVED_TEMPLATE =
82         "SELECT files.id, files.path, files.size, " +
83                "audios.title as title, audios.trackno, audios.length, audios.channels, audios.sampling_rate, audios.bitrate, audios.dlna_profile, audios.dlna_mime, " +
84                "audio_artists.name as artist, " +
85                "audio_albums.name " +
86         "FROM audios, files " +
87         "LEFT JOIN audio_artists " +
88         "ON audios.artist_id = audio_artists.id " +
89         "LEFT JOIN audio_albums " +
90         "ON audios.album_id = audio_albums.id " +
91         "WHERE dtime <> 0 AND audios.id = files.id AND audios.album_id = %s " +
92         "AND update_id > ? AND update_id <= ?;";
93
94     protected override MediaObject? object_from_statement (Statement statement) {
95         var id = statement.column_int (0);
96         var path = statement.column_text (1);
97         var mime_type = statement.column_text(10);
98
99         if (mime_type == null || mime_type.length == 0) {
100             /* TODO is this correct? */
101             debug ("Music item %d (%s) has no MIME type",
102                    id,
103                    path);
104         }
105
106         var title = statement.column_text(3);
107         var song_id = this.build_child_id (id);
108         var song = new MusicItem (song_id, this, title);
109         song.ref_id = this.build_reference_id (id);
110         song.size = statement.column_int(2);
111         song.track_number = statement.column_int(4);
112         song.duration = statement.column_int(5);
113         song.channels = statement.column_int(6);
114         song.sample_freq = statement.column_int(7); 
115         song.bitrate = statement.column_int(8);
116         song.dlna_profile = statement.column_text(9);
117         song.mime_type = mime_type;
118         song.artist = statement.column_text(11);
119         song.album = statement.column_text(12);
120         File file = File.new_for_path (path);
121         song.add_uri (file.get_uri ());
122
123         return song;
124     }
125
126     private static string get_sql_all (string db_id) {
127         return (SQL_ALL_TEMPLATE.printf (db_id));
128     }
129     private static string get_sql_find_object (string db_id) {
130         return (SQL_FIND_OBJECT_TEMPLATE.printf (db_id));
131     }
132     private static string get_sql_count (string db_id) {
133         return (SQL_COUNT_TEMPLATE.printf (db_id));
134     }
135     private static string get_sql_added (string db_id) {
136         return (SQL_ADDED_TEMPLATE.printf (db_id));
137     }
138     private static string get_sql_removed (string db_id) {
139         return (SQL_REMOVED_TEMPLATE.printf (db_id));
140     }
141
142     protected override string get_sql_all_with_filter (string filter) {
143         if (filter.length == 0) {
144             return this.sql_all;
145         }
146         var filter_str = "%s AND %s".printf (this.db_id, filter);
147         return (SQL_ALL_TEMPLATE.printf (filter_str));
148     }
149
150     protected override string get_sql_count_with_filter (string filter) {
151         if (filter.length == 0) {
152             return this.sql_count;
153         }
154         var filter_str = "%s AND %s".printf (this.db_id, filter);
155         return (SQL_COUNT_WITH_FILTER_TEMPLATE.printf (filter_str));
156     }
157
158     public Album (string         db_id,
159                   MediaContainer parent,
160                   string         title,
161                   LMS.Database   lms_db) {
162         base (db_id,
163               parent,
164               title,
165               lms_db,
166               get_sql_all (db_id),
167               get_sql_find_object (db_id),
168               get_sql_count (db_id),
169               get_sql_added (db_id),
170               get_sql_removed (db_id)
171              );
172     }
173 }