a867d8e73b8250cd39db6c4605affea7316d08f1
[profile/ivi/rygel.git] / src / rygel / rygel-client-hacks.vala
1 /*
2  * Copyright (C) 2011 Red Hat, Inc.
3  * Copyright (C) 2010 Nokia Corporation.
4  *
5  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6  *
7  * This file is part of Rygel.
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 using Soup;
25 using GUPnP;
26
27 internal errordomain Rygel.ClientHacksError {
28     NA
29 }
30
31 internal abstract class Rygel.ClientHacks : GLib.Object {
32     private const string CORRECT_OBJECT_ID = "ObjectID";
33
34     public unowned string object_id { get; protected set; }
35
36     protected Regex agent_regex;
37
38     public static ClientHacks create_for_action (ServiceAction action)
39                                                  throws ClientHacksError {
40         try {
41             return new XBoxHacks.for_action (action);
42         } catch {}
43
44         try {
45             return new PanasonicHacks.for_action (action);
46         } catch {}
47
48         return new XBMCHacks.for_action (action);
49     }
50
51     public static ClientHacks create_for_headers (MessageHeaders headers)
52                                                   throws ClientHacksError {
53         try {
54             return new XBoxHacks.for_headers (headers);
55         } catch {}
56
57         try {
58             return new PanasonicHacks.for_headers (headers);
59         } catch {};
60
61         return new XBMCHacks.for_headers (headers);
62     }
63
64     protected ClientHacks (string agent_pattern, MessageHeaders? headers = null)
65                            throws ClientHacksError {
66         try {
67             this.agent_regex = new Regex (agent_pattern,
68                                           RegexCompileFlags.CASELESS,
69                                           0);
70         } catch (RegexError error) {
71             // This means subclasses did not provide a proper regular expression
72             assert_not_reached ();
73         }
74
75         if (headers != null) {
76             this.check_headers (headers);
77         }
78
79         this.object_id = CORRECT_OBJECT_ID;
80     }
81
82     public bool is_album_art_request (Soup.Message message) {
83         unowned string query = message.get_uri ().query;
84
85         if (query == null) {
86             return false;
87         }
88
89         var params = Soup.Form.decode (query);
90         var album_art = params.lookup ("albumArt");
91
92         return (album_art != null) && bool.parse (album_art);
93     }
94
95     public virtual void translate_container_id (MediaQueryAction action,
96                                                 ref string       container_id) {}
97
98     public virtual void apply (MediaItem item) {}
99
100     public virtual void filter_sort_criteria (ref string sort_criteria) {}
101
102     public virtual async MediaObjects? search
103                                         (SearchableContainer container,
104                                          SearchExpression?   expression,
105                                          uint                offset,
106                                          uint                max_count,
107                                          out uint            total_matches,
108                                          Cancellable?        cancellable)
109                                          throws Error {
110         return yield container.search (expression,
111                                        offset,
112                                        max_count,
113                                        out total_matches,
114                                        cancellable);
115     }
116
117     private void check_headers (MessageHeaders headers)
118                                           throws ClientHacksError {
119         var agent = headers.get_one ("User-Agent");
120         if (agent == null || !(this.agent_regex.match (agent))) {
121             throw new ClientHacksError.NA (_("Not Applicable"));
122         }
123     }
124 }