File: folder.py

package info (click to toggle)
miro 3.0.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 36,056 kB
  • ctags: 9,438
  • sloc: python: 52,860; cpp: 832; ansic: 692; xml: 432; sh: 403; makefile: 62
file content (183 lines) | stat: -rw-r--r-- 6,105 bytes parent folder | download
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
# Miro - an RSS based video player application
# Copyright (C) 2005-2010 Participatory Culture Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.

"""``miro.folder`` -- Holds ``Folder`` class and related things.
"""

from miro import feed
from miro import playlist
from miro import util
from miro.database import DDBObject, ObjectNotFoundError
from miro.databasehelper import make_simple_get_set

class FolderBase(DDBObject):
    """Base class for ChannelFolder and Playlist folder classes."""

    def setup_new(self, title):
        self.title = title
        self.expanded = True

    get_title, set_title = make_simple_get_set('title')

    def getExpanded(self):
        self.confirm_db_thread()
        return self.expanded

    def setExpanded(self, newExpanded):
        self.confirm_db_thread()
        self.expanded = newExpanded
        self.signal_change()
        for child in self.getChildrenView():
            child.signal_change(needs_save=False)

    def remove(self, moveItemsTo=None):
        """Remove this folder and children.
        """
        raise NotImplementedError()

    # get_folder and set_folder are here so that channels/playlists and folders
    # have a consistent API.  They don't do much since we don't allow nested
    # folders.
    def get_folder(self):
        return None

    def set_folder(self, newFolder, signal_items=False):
        if newFolder is not None:
            raise TypeError("Nested folders not allowed")

    def getChildrenView(self):
        """Return the children of this folder."""
        raise NotImplementedError()

class ChannelFolder(FolderBase):
    def setup_new(self, title, section=u'video'):
        self.section = section
        FolderBase.setup_new(self, title)

    def remove(self, moveItemsTo=None):
        children = list(self.getChildrenView())
        for child in children:
            if child.is_watched_folder():
                child.setVisible(False) # just hide watched folders
                child.set_folder(None)
            else:
                child.remove(moveItemsTo)
        DDBObject.remove(self)

    @classmethod
    def video_view(cls):
        return cls.make_view("section='video'")

    @classmethod
    def audio_view(cls):
        return cls.make_view("section='audio'")

    @classmethod
    def get_by_title(cls, title):
        return cls.make_view('title=?', (title,)).get_singleton()

    def getChildrenView(self):
        return feed.Feed.folder_view(self.id)

    def hasDownloadedItems(self):
        for feed in self.getChildrenView():
            if feed.hasDownloadedItems():
                return True
        return False

    def hasDownloadingItems(self):
        for feed in self.getChildrenView():
            if feed.hasDownloadingItems():
                return True
        return False

    # Returns string with number of unwatched videos in feed
    def num_unwatched(self):
        unwatched = 0
        for child in self.getChildrenView():
            unwatched += child.num_unwatched()
        return unwatched

    # Returns string with number of available videos in feed
    def num_available(self):
        available = 0
        for child in self.getChildrenView():
            available += child.num_available()
        return available

    def mark_as_viewed(self):
        for child in self.getChildrenView():
            child.mark_as_viewed()

class PlaylistFolderItemMap(playlist.PlaylistItemMap):
    """Single row in the map that associates playlist folders with their 
    child items.
    """
    def setup_new(self, playlist_id, item_id):
        playlist.PlaylistItemMap.setup_new(self, playlist_id, item_id)
        self.count = 1

    def inc_count(self):
        self.count += 1
        self.signal_change()

    def dec_count(self):
        if self.count > 1:
            self.count -= 1
            self.signal_change()
        else:
            self.remove()

    @classmethod
    def add_item_id(cls, playlist_id, item_id):
        view = cls.make_view('playlist_id=? AND item_id=?',
                (playlist_id, item_id))
        try:
            map = view.get_singleton()
            map.inc_count()
        except ObjectNotFoundError:
            cls(playlist_id, item_id)

    @classmethod
    def remove_item_id(cls, playlist_id, item_id):
        view = cls.make_view('playlist_id=? AND item_id=?',
                (playlist_id, item_id))
        map = view.get_singleton()
        map.dec_count()

class PlaylistFolder(FolderBase, playlist.PlaylistMixin):
    MapClass = PlaylistFolderItemMap

    def remove(self, moveItemsTo=None):
        children = list(self.getChildrenView())
        for child in children:
            child.remove(moveItemsTo)
        DDBObject.remove(self)

    def getChildrenView(self):
        return playlist.SavedPlaylist.folder_view(self.id)