lms: add dtime=0 condition to all queries involving files table
[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     protected override MediaObject? object_from_statement (Statement statement) {
69         var id = statement.column_int (0);
70         var path = statement.column_text (1);
71         var mime_type = statement.column_text(10);
72
73         if (mime_type == null || mime_type.length == 0) {
74             /* TODO is this correct? */
75             debug ("Music item %d (%s) has no MIME type",
76                    id,
77                    path);
78         }
79
80         var title = statement.column_text(3);
81         var song_id = this.build_child_id (id);
82         var song = new MusicItem (song_id, this, title);
83         song.ref_id = this.build_reference_id (id);
84         song.size = statement.column_int(2);
85         song.track_number = statement.column_int(4);
86         song.duration = statement.column_int(5);
87         song.channels = statement.column_int(6);
88         song.sample_freq = statement.column_int(7); 
89         song.bitrate = statement.column_int(8);
90         song.dlna_profile = statement.column_text(9);
91         song.mime_type = mime_type;
92         song.artist = statement.column_text(11);
93         song.album = statement.column_text(12);
94         File file = File.new_for_path (path);
95         song.add_uri (file.get_uri ());
96
97         return song;
98     }
99
100     private static string get_sql_all (string db_id) {
101         return (SQL_ALL_TEMPLATE.printf (db_id));
102     }
103     private static string get_sql_find_object (string db_id) {
104         return (SQL_FIND_OBJECT_TEMPLATE.printf (db_id));
105     }
106     private static string get_sql_count (string db_id) {
107         return (SQL_COUNT_TEMPLATE.printf (db_id));
108     }
109
110     protected override string get_sql_all_with_filter (string filter) {
111         if (filter.length == 0) {
112             return this.sql_all;
113         }
114         var filter_str = "%s AND %s".printf (this.db_id, filter);
115         return (SQL_ALL_TEMPLATE.printf (filter_str));
116     }
117
118     protected override string get_sql_count_with_filter (string filter) {
119         if (filter.length == 0) {
120             return this.sql_count;
121         }
122         var filter_str = "%s AND %s".printf (this.db_id, filter);
123         return (SQL_COUNT_WITH_FILTER_TEMPLATE.printf (filter_str));
124     }
125
126     public Album (string         db_id,
127                   MediaContainer parent,
128                   string         title,
129                   LMS.Database   lms_db) {
130         base (db_id,
131               parent,
132               title,
133               lms_db,
134               get_sql_all (db_id),
135               get_sql_find_object (db_id),
136               get_sql_count (db_id));
137     }
138 }