summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-plugin.vala
blob: 36e46847d15cc0d359683c0dcfeb3df89b017ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2008 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using Gee;
using GUPnP;

/**
 * RygelPluginCapabilities is a set of flags that represent various
 * capabilities of plugins.
 */
[Flags]
public enum Rygel.PluginCapabilities {
    NONE = 0,
    /* Server caps */

    /// Server plugin supports upload of images
    IMAGE_UPLOAD,

    /// Server plugin supports upload of video files
    VIDEO_UPLOAD,

    /// Server plugin supports upload of audio files
    AUDIO_UPLOAD,

    /// Server supports upload of all kind of items
    UPLOAD = IMAGE_UPLOAD | VIDEO_UPLOAD | AUDIO_UPLOAD,

    /// Server supports tracking changes
    TRACK_CHANGES,

    /// Server supports container creation
    CREATE_CONTAINERS,

    /* Renderer caps */

    /// General capabilities

    /* Diagnostics (DIAGE) support */
    DIAGNOSTICS,

    /* EnergyManagement (LPE) support */
    ENERGY_MANAGEMENT
}

/**
 * This represents a Rygel plugin.
 *
 * Plugin libraries should provide an object of this
 * class or a subclass in their module_init() function.
 *
 * It is generally convenient to derive from 
 * #RygelMediaRendererPlugin from librygel-renderer,
 * or from #RygelMediaServerPlugin from librygel-server.
 *
 * Plugins may change their behaviour based on their
 * configuration. See rygel_meta_config_get_default().
 */
public class Rygel.Plugin : GUPnP.ResourceFactory {
    private static const string PNG_EXT = "png";
    private static const string JPG_EXT = "jpg";

    private static const string ICON_BIG = "file://" +
                                           BuildConfig.BIG_ICON_DIR +
                                           "/rygel";
    private static const string ICON_PNG_BIG = ICON_BIG + "." + PNG_EXT;
    private static const string ICON_JPG_BIG = ICON_BIG + "." + JPG_EXT;

    private static const string ICON_SMALL = "file://" +
                                             BuildConfig.SMALL_ICON_DIR +
                                             "/rygel";
    private static const string ICON_PNG_SMALL = ICON_SMALL + "." + PNG_EXT;
    private static const string ICON_JPG_SMALL = ICON_SMALL + "." + JPG_EXT;

    private static const string ICON_PNG_MIME = "image/png";
    private static const string ICON_JPG_MIME = "image/jpeg";

    private static const int ICON_PNG_DEPTH = 24;
    private static const int ICON_JPG_DEPTH = 24;

    private static const int ICON_BIG_WIDTH = 120;
    private static const int ICON_BIG_HEIGHT = 120;
    private static const int ICON_SMALL_WIDTH = 48;
    private static const int ICON_SMALL_HEIGHT = 48;

    public PluginCapabilities capabilities { get; construct set; }

    public string name { get; construct; }
    public string title { get; construct set; }
    public string description { get; construct; }

    // Path to description document
    public string desc_path { get; construct; }

    public bool active { get; set; }

    public ArrayList<ResourceInfo> resource_infos { get; private set; }
    public ArrayList<IconInfo> icon_infos { get; private set; }

    public ArrayList<IconInfo> default_icons { get; private set; }

    /*
     * TODO: Document the format of the template file, such as which tags/attributes
     * should be present, which should be present but empty, and which
     * tags should not be present.
     */

    /** 
     * Create an instance of the plugin.
     *
     * @param desc_path The path of a template file for an XML description of the UPnP service.
     * @param name The non-human-readable name for the plugin and its service, used in UPnP messages and in the Rygel configuration file.
     * @param title An optional human-readable name (friendlyName) of the UPnP service provided by the plugin. If the title is empty then the name will be used.
     * @param description An optional human-readable description (modelDescription) of the UPnP service provided by the plugin.
     */
    public Plugin (string  desc_path,
                   string  name,
                   string? title,
                   string? description = null,
                   PluginCapabilities capabilities = PluginCapabilities.NONE) {
        Object (desc_path : desc_path,
                name : name,
                title : title,
                description : description,
                capabilities : capabilities);
    }

    public override void constructed () {
        base.constructed ();

        this.active = true;

        if (this.title == null) {
            this.title = this.name;
        }

        this.resource_infos = new ArrayList<ResourceInfo> ();

        /* Enable BasicManagement service on this device if needed */
        var config = MetaConfig.get_default ();
        try {
            if (config.get_bool (this.name, "diagnostics")) {
                var resource = new ResourceInfo (BasicManagement.UPNP_ID,
                                                 BasicManagement.UPNP_TYPE,
                                                 BasicManagement.DESCRIPTION_PATH,
                                                 typeof (BasicManagement));
                this.add_resource (resource);

                this.capabilities |= PluginCapabilities.DIAGNOSTICS;
            }
        } catch (GLib.Error error) {
            if (!(error is ConfigurationError.NO_VALUE_SET))
                warning ("Failed to read configuration: %s", error.message);
        }

        /* Enable EnergyManagement service on this device if needed */
        config = MetaConfig.get_default ();
        try {
            if (config.get_bool (this.name, "energy-management")) {
                var resource = new ResourceInfo (EnergyManagement.UPNP_ID,
                                                 EnergyManagement.UPNP_TYPE,
                                                 EnergyManagement.DESCRIPTION_PATH,
                                                 typeof (EnergyManagement));
                this.add_resource (resource);

                this.capabilities |= PluginCapabilities.ENERGY_MANAGEMENT;

            }
        } catch (GLib.Error error) {
            if (!(error is ConfigurationError.NO_VALUE_SET))
                warning ("Failed to read configuration: %s", error.message);
        }

        this.icon_infos = new ArrayList<IconInfo> ();
        this.default_icons = new ArrayList<IconInfo> ();

        // Add PNG icons
        this.add_default_icon (ICON_PNG_MIME,
                               PNG_EXT,
                               ICON_PNG_BIG,
                               ICON_BIG_WIDTH,
                               ICON_BIG_HEIGHT,
                               ICON_PNG_DEPTH);
        this.add_default_icon (ICON_PNG_MIME,
                               PNG_EXT,
                               ICON_PNG_SMALL,
                               ICON_SMALL_WIDTH,
                               ICON_SMALL_HEIGHT,
                               ICON_PNG_DEPTH);

        // Then add JPEG icons
        this.add_default_icon (ICON_JPG_MIME,
                               JPG_EXT,
                               ICON_JPG_BIG,
                               ICON_BIG_WIDTH,
                               ICON_BIG_HEIGHT,
                               ICON_JPG_DEPTH);
        this.add_default_icon (ICON_JPG_MIME,
                               JPG_EXT,
                               ICON_JPG_SMALL,
                               ICON_SMALL_WIDTH,
                               ICON_SMALL_HEIGHT,
                               ICON_JPG_DEPTH);
    }

    public void add_resource (ResourceInfo resource_info) {
        this.resource_infos.add (resource_info);
        this.register_resource_type (resource_info.upnp_type,
                                     resource_info.type);
    }

    public void add_icon (IconInfo icon_info) {
        this.icon_infos.add (icon_info);
    }

    public virtual void apply_hacks (RootDevice device,
                                     string     description_path)
                                     throws Error {
    }

    private void add_default_icon (string mime_type,
                                   string file_extension,
                                   string uri,
                                   int    width,
                                   int    height,
                                   int    depth) {
        var icon = new IconInfo (mime_type, file_extension);
        icon.uri = uri;
        icon.width = width;
        icon.height = height;
        icon.depth = depth;

        this.default_icons.add (icon);
    }
}