~ubuntu-branches/ubuntu/maverick/pitivi/maverick-proposed

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
# PiTiVi , Non-linear video editor
#
#       pitivi/sourcelist.py
#
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
# Copyright (c) 2009, Alessandro Decina <alessandro.d@gmail.com>
#
# This program 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.
#
# This program 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., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
Handles the list of source for a project
"""

import urllib
from pitivi.discoverer import Discoverer
from pitivi.signalinterface import Signallable
from pitivi.log.loggable import Loggable

class SourceListError(Exception):
    pass

class SourceList(Signallable, Loggable):
    discovererClass = Discoverer

    """
    Contains the sources for a project, stored as SourceFactory objects.

    @ivar discoverer: The discoverer object used internally
    @type discoverer: L{Discoverer}

    Signals:
     - C{source-added} : A source has been discovered and added to the SourceList.
     - C{source-removed} : A source was removed from the SourceList.
     - C{missing-plugins} : A source has been discovered but some plugins are
       missing in order to decode all of its streams.
     - C{discovery-error} : The given uri is not a media file.
     - C{ready} : No more files are being discovered/added.
     - C{starting} : Some files are being discovered/added.
    """

    __signals__ = {
        "ready" : [],
        "starting" : [],
        "missing-plugins": ["uri", "factory", "details", "descriptions"],
        "source-added" : ["factory"],
        "source-removed" : ["uri"],
        "discovery-error" : ["uri", "reason"],
        }

    def __init__(self):
        Loggable.__init__(self)
        Signallable.__init__(self)
        self._sources = {}
        self._ordered_sources = []

        self.discoverer = self.discovererClass()
        self.discoverer.connect("discovery-error", self._discoveryErrorCb)
        self.discoverer.connect("discovery-done", self._discoveryDoneCb)
        self.discoverer.connect("starting", self._discovererStartingCb)
        self.discoverer.connect("ready", self._discovererReadyCb)
        self.discoverer.connect("missing-plugins",
                self._discovererMissingPluginsCb)


    def addUri(self, uri):
        """
        Add c{uri} to the source list.

        The uri will be analyzed before being added.
        """
        if uri in self._sources:
            raise SourceListError("URI already present in the source list", uri)

        uri = urllib.unquote(uri)
        self._sources[uri] = None

        self.discoverer.addUri(uri)

    def addUris(self, uris):
        """
        Add c{uris} to the source list.

        The uris will be analyzed before being added.
        """
        for uri in uris:
            self.addUri(uri)

    def removeUri(self, uri):
        """
        Remove the factory for c{uri} from the source list.
        """
        try:
            factory = self._sources.pop(uri)
        except KeyError:
            raise SourceListError("URI not in the sourcelist", uri)

        try:
            self._ordered_sources.remove(factory)
        except ValueError:
            # this can only happen if discoverer hasn't finished scanning the
            # source, so factory must be None
            assert factory is None

        self.emit("source-removed", uri, factory)

    def getUri(self, uri):
        """
        Get the source corresponding to C{uri}.
        """
        factory = self._sources.get(uri, None)
        if factory is None:
            raise SourceListError("URI not in the sourcelist", uri)

        return factory

    def addFactory(self, factory):
        """
        Add an objectfactory for the given uri.
        """
        if self._sources.get(factory.uri, None) is not None:
            raise SourceListError("We already have a factory for this uri",
                    factory.uri)

        self._sources[factory.uri] = factory
        self._ordered_sources.append(factory)
        self.emit("source-added", factory)

    def getSources(self):
        """ Returns the list of sources used.

        The list will be ordered by the order in which they were added
        """
        return self._ordered_sources

    def _discoveryDoneCb(self, discoverer, uri, factory):
        if factory.uri not in self._sources:
            # the source was removed while it was being scanned
            return

        self.addFactory(factory)

    def _discoveryErrorCb(self, discoverer, uri, reason, extra):
        try:
            del self._sources[uri]
        except KeyError:
            # the source was removed while it was being scanned
            pass

        self.emit("discovery-error", uri, reason, extra)

    def _discovererStartingCb(self, unused_discoverer):
        self.emit("starting")

    def _discovererReadyCb(self, unused_discoverer):
        self.emit("ready")

    def _discovererMissingPluginsCb(self, discoverer, uri, factory,
            details, descriptions, missingPluginsCallback):
        if factory.uri not in self._sources:
            # the source was removed while it was being scanned
            return None

        return self.emit('missing-plugins', uri, factory,
                details, descriptions, missingPluginsCallback)