build,core,plugins: Port to GDBus and GVariant
[profile/ivi/rygel.git] / src / plugins / external / rygel-external-item-factory.vala
1 /*
2  * Copyright (C) 2009 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>.
3  * Copyright (C) 2009,2010 Nokia Corporation.
4  *
5  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6  *                               <zeeshan.ali@nokia.com>
7  *
8  * This file is part of Rygel.
9  *
10  * Rygel is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * Rygel is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24
25 using GUPnP;
26 using FreeDesktop;
27
28 /**
29  * Creates item for external plugins.
30  */
31 public class Rygel.External.ItemFactory {
32     public async MediaItem create (string                    id,
33                                    string                    type,
34                                    string                    title,
35                                    HashTable<string,Variant> props,
36                                    string                    service_name,
37                                    string                    host_ip,
38                                    MediaContainer            parent)
39                                    throws GLib.Error {
40         MediaItem item;
41
42         if (type.has_prefix ("audio")) {
43             item = new AudioItem (id, parent, title);
44
45             this.set_audio_metadata (item as AudioItem,
46                                      props,
47                                      service_name,
48                                      host_ip);
49         } else if (type.has_prefix ("music")) {
50             item = new MusicItem (id, parent, title);
51
52             yield this.set_music_metadata (item as MusicItem,
53                                            props,
54                                            service_name,
55                                            host_ip);
56         } else if (type.has_prefix ("video")) {
57             item = new VideoItem (id, parent, title);
58
59             yield this.set_video_metadata (item as VideoItem,
60                                            props,
61                                            service_name,
62                                            host_ip);
63         } else {
64             item = new ImageItem (id, parent, title);
65
66             yield this.set_visual_metadata (item as VisualItem,
67                                             props,
68                                             service_name,
69                                             host_ip);
70         }
71
72         this.set_generic_metadata (item, props, service_name, host_ip);
73
74         if (parent is DummyContainer) {
75             item.parent_ref = parent;
76         }
77
78         return item;
79     }
80
81     private async void set_music_metadata (
82                                         MusicItem                 music,
83                                         HashTable<string,Variant> props,
84                                         string                    service_name,
85                                         string                    host_ip)
86                                         throws GLib.Error {
87         music.artist = this.get_string (props, "Artist");
88         music.album = this.get_string (props, "Album");
89         music.genre = this.get_string (props, "Genre");
90
91         var value = props.lookup ("AlbumArt");
92         if (value != null) {
93             var cover_factory = new AlbumArtFactory ();
94
95             music.album_art = yield cover_factory.create ((string) value,
96                                                           service_name,
97                                                           host_ip);
98         }
99
100         this.set_audio_metadata (music, props, service_name, host_ip);
101     }
102
103     private void set_audio_metadata (AudioItem                 audio,
104                                      HashTable<string,Variant> props,
105                                      string                    service_name,
106                                      string                    host_ip)
107                                      throws GLib.Error {
108         audio.duration = this.get_int (props, "Duration");
109         audio.bitrate = this.get_int (props, "Bitrate");
110         audio.sample_freq = this.get_int (props, "SampleRate");
111         audio.bits_per_sample = this.get_int (props, "BitsPerSample");
112     }
113
114     private async void set_visual_metadata (
115                                         VisualItem                visual,
116                                         HashTable<string,Variant> props,
117                                         string                    service_name,
118                                         string                    host_ip)
119                                         throws GLib.Error {
120         visual.width = this.get_int (props, "Width");
121         visual.height = this.get_int (props, "Height");
122         visual.color_depth = this.get_int (props, "ColorDepth");
123         visual.pixel_width = this.get_int (props, "PixelWidth");
124         visual.pixel_height = this.get_int (props, "PixelHeight");
125
126         var value = props.lookup ("Thumbnail");
127         if (value != null) {
128             var factory = new ThumbnailFactory ();
129             var thumbnail = yield factory.create ((string) value,
130                                                   service_name,
131                                                   host_ip);
132             visual.thumbnails.add (thumbnail);
133         }
134     }
135
136     private async void set_video_metadata (
137                                         VideoItem                 video,
138                                         HashTable<string,Variant> props,
139                                         string                    service_name,
140                                         string                    host_ip)
141                                         throws GLib.Error {
142         yield this.set_visual_metadata (video, props, service_name, host_ip);
143         this.set_audio_metadata (video, props, service_name, host_ip);
144     }
145
146     private void set_generic_metadata (MediaItem                 item,
147                                        HashTable<string,Variant> props,
148                                        string                    service_name,
149                                        string                    host_ip) {
150         item.mime_type = this.get_string (props, "MIMEType");
151
152         var uris = (string[]) props.lookup ("URLs");
153         if (uris != null) {
154             for (var i = 0; uris[i] != null; i++) {
155                 item.add_uri (uris[i].replace ("@ADDRESS@", host_ip));
156             }
157         }
158
159         // Optional properties
160
161         item.dlna_profile = this.get_string (props, "DLNAProfile");
162
163         var value = props.lookup ("Size");
164         if (value != null) {
165             item.size = (int64) value;
166         }
167
168         item.date = this.get_string (props, "Date");
169     }
170
171     private string? get_string (HashTable<string,Variant> props, string prop) {
172         var value = props.lookup (prop);
173
174         if (value != null) {
175             return (string) value;
176         } else {
177             return null;
178         }
179     }
180
181     private int get_int (HashTable<string,Variant> props, string prop) {
182         var value = props.lookup (prop);
183
184         if (value != null) {
185             return (int) value;
186         } else {
187             return -1;
188         }
189     }
190 }
191