summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-item-destroyer.vala
blob: 90203cb27d58581d95ac5eb9b9083a7d54120fc3 (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
/*
 * Copyright (C) 2010 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *
 * 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 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

using GUPnP;

private errordomain Rygel.ItemDestroyerError {
    PARSE
}

/**
 * DestroyObject action implementation.
 */
internal class Rygel.ItemDestroyer: GLib.Object, Rygel.StateMachine {
    private string object_id;

    private ContentDirectory content_dir;
    private ServiceAction action;

    public Cancellable cancellable { get; set; }

    public ItemDestroyer (ContentDirectory    content_dir,
                          owned ServiceAction action) {
        this.content_dir = content_dir;
        this.cancellable = content_dir.cancellable;
        this.action = (owned) action;
    }

    public async void run () {
        try {
            this.action.get ("ObjectID", typeof (string), out this.object_id);
            if (this.object_id == null) {
                // Sorry we can't do anything without the ID
                throw new ContentDirectoryError.NO_SUCH_OBJECT
                                        (_("No such object"));
            }

            yield this.remove_object ();

            this.action.return ();

            debug (_("Successfully destroyed object '%s'"), this.object_id);
        } catch (Error error) {
            if (error is ContentDirectoryError) {
                this.action.return_error (error.code, error.message);
            } else {
                this.action.return_error (701, error.message);
            }

            warning (_("Failed to destroy object '%s': %s"),
                     this.object_id,
                     error.message);
        }

        this.completed ();
    }

    private async void remove_object () throws Error {
        var media_object = yield this.fetch_object ();

        if (media_object is MediaItem ) {
            var parent = media_object.parent as WritableContainer;
            yield parent.remove_item (this.object_id, this.cancellable);

            if (!(media_object as MediaItem).place_holder) {
                var writables = yield media_object.get_writables (this.cancellable);
                foreach (var file in writables) {
                    if (file.query_exists (this.cancellable)) {
                        file.delete (this.cancellable);
                    }
                }
            }
        }
    }

    private async MediaObject fetch_object () throws Error {
        var media_object = yield this.content_dir.root_container.find_object
                                        (this.object_id, this.cancellable);

        if (media_object == null) {
            throw new ContentDirectoryError.NO_SUCH_OBJECT
                                        (_("No such object"));
        } else if (!(OCMFlags.DESTROYABLE in media_object.ocm_flags)) {
            throw new ContentDirectoryError.RESTRICTED_OBJECT
                                        (_("Removal of object %s not allowed"),
                                         media_object.id);
        } else if (media_object.parent.restricted) {
            var msg = _("Object removal from %s not allowed");

            throw new ContentDirectoryError.RESTRICTED_PARENT (msg,
                                                               media_object.id);
        }

        return media_object;
    }
}