Debian Codegraph @

xlgui/panel/device.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27 from gi.repository import GObject
28 from gi.repository import Gtk
29
30 from xl import common, event
31 from xl.nls import gettext as _
32 from xlgui import panel
33 from xlgui.panel.collection import CollectionPanel
34 from xlgui.panel.flatplaylist import FlatPlaylistPanel
35
36
37 class DeviceTransferThread(common.ProgressThread):
38 """
39 Transfers tracks from devices
40 """
41
42 def __init__(self, device):
43 common.ProgressThread.__init__(self)
44
45 self.device = device
46
47 def stop(self):
48 """
49 Stops the thread
50 """
51 self.device.transfer.cancel()
52 common.ProgressThread.stop(self)
53
54 def on_track_transfer_progress(self, type, transfer, progress):
55 """
56 Notifies about progress changes
57 """
58 if progress < 100:
59 self.emit('progress-update', progress)
60 else:
61 self.emit('done')
62
63 def run(self):
64 """
65 Runs the thread
66 """
67 event.add_ui_callback(
68 self.on_track_transfer_progress,
69 'track_transfer_progress',
70 self.device.transfer,
71 )
72 try:
73 self.device.start_transfer()
74 finally:
75 event.remove_callback(
76 self.on_track_transfer_progress,
77 'track_transfer_progress',
78 self.device.transfer,
79 )
80
81
82 class ReceptiveCollectionPanel(CollectionPanel):
83 def drag_data_received(self, widget, context, x, y, data, info, stamp):
84 uris = data.get_uris()
85 tracks, playlists = self.tree.get_drag_data(uris)
86 tracks = [
87 t for t in tracks if not self.collection.loc_is_member(t.get_loc_for_io())
88 ]
89
90 self.add_tracks_func(tracks)
91
92 def add_tracks_func(self, tracks):
93 locs = [t['__loc'] for t in tracks]
94 # FIXME:
95 lib = self.collection.get_libraries()[0]
96
97 # TODO: there should be a queue for ipod and such devices,
98 # otherwise you'll have to write the database on every track add and
99 # that won't be good
100 # this _needs_ to be asynchronous
101 for l in locs:
102 lib.add(l)
103
104
105 class DevicePanel(panel.Panel):
106 """
107 generic panel for devices
108 """
109
110 __gsignals__ = {
111 'append-items': (GObject.SignalFlags.RUN_LAST, None, (object, bool)),
112 'replace-items': (GObject.SignalFlags.RUN_LAST, None, (object,)),
113 'queue-items': (GObject.SignalFlags.RUN_LAST, None, (object,)),
114 'collection-tree-loaded': (GObject.SignalFlags.RUN_LAST, None, ()),
115 }
116
117 ui_info = ('device.ui', 'DevicePanel')
118
119 def __init__(self, parent, main, device, name):
120 label = device.get_name()
121 panel.Panel.__init__(self, parent, name, label)
122 self.device = device
123 self.main = main
124
125 self.notebook = self.builder.get_object("device_notebook")
126
127 self.collectionpanel = ReceptiveCollectionPanel(
128 parent, collection=device.collection, name=name, label=label
129 )
130 self.collectionpanel.add_tracks_func = self.add_tracks_func
131
132 self.collectionpanel.connect(
133 'append-items', lambda *e: self.emit('append-items', *e[1:])
134 )
135 self.collectionpanel.connect(
136 'replace-items', lambda *e: self.emit('replace-items', *e[1:])
137 )
138 self.collectionpanel.connect(
139 'queue-items', lambda *e: self.emit('queue-items', *e[1:])
140 )
141 self.collectionpanel.connect(
142 'collection-tree-loaded', lambda *e: self.emit('collection-tree-loaded')
143 )
144
145 def add_tracks_func(self, tracks):
146 self.device.add_tracks(tracks)
147 thread = DeviceTransferThread(self.device)
148 thread.connect('done', lambda *e: self.load_tree())
149 self.main.controller.progress_manager.add_monitor(
150 thread, _("Transferring to %s...") % self.name, 'drive-harddisk'
151 )
152
153 def get_panel(self):
154 return self.collectionpanel.get_panel()
155
156 def add_panel(self, child, name):
157 label = Gtk.Label(label=name)
158 self.notebook.append_page(child, label)
159
160 def load_tree(self, *args):
161 self.collectionpanel.load_tree(*args)
162
163
164 class FlatPlaylistDevicePanel(panel.Panel):
165 __gsignals__ = {
166 'append-items': (GObject.SignalFlags.RUN_LAST, None, (object, bool)),
167 'replace-items': (GObject.SignalFlags.RUN_LAST, None, (object,)),
168 'queue-items': (GObject.SignalFlags.RUN_LAST, None, (object,)),
169 }
170
171 ui_info = ('device.ui', 'DevicePanel')
172
173 def __init__(self, parent, main, device, name):
174 label = device.get_name()
175 panel.Panel.__init__(self, parent, name, label)
176 self.device = device
177 self.main = main
178
179 self.notebook = self.builder.get_object("device_notebook")
180
181 self.fppanel = FlatPlaylistPanel(self, name, label)
182
183 self.fppanel.connect(
184 'append-items', lambda *e: self.emit('append-items', *e[1:])
185 )
186 self.fppanel.connect(
187 'replace-items', lambda *e: self.emit('replace-items', *e[1:])
188 )
189 self.fppanel.connect('queue-items', lambda *e: self.emit('queue-items', *e[1:]))
190
191 def get_panel(self):
192 return self.fppanel.get_panel()
193
194 def add_panel(self, child, name):
195 label = Gtk.Label(label=name)
196 self.notebook.append_page(child, label)
197
198 def load_tree(self, *e):
199 # TODO: handle *all* the playlists
200 self.fppanel.set_playlist(self.device.get_playlists()[0])