~qioeujqioejqioe-deactivatedaccount/exaile/missing-signals

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
#!/usr/bin/env python
# Copyright (C) 2006 Adam Olsen <arolsen@gmail.com>
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.

from xl import media, library, xlmisc, db, common
import os, xl, gobject, gtk, shutil, os.path
from gettext import gettext as _
import copy
import xl.plugins as plugins

PLUGIN_NAME = _("Mass Storage Driver")
PLUGIN_AUTHORS = ['Adam Olsen <arolsen@gmail.com>']
PLUGIN_VERSION = '0.3.4'
PLUGIN_DESCRIPTION = _(r"""Mass Storage Driver for the Devices Panel""")
PLUGIN_ENABLED = False
PLUGIN_ICON = None

PLUGIN = None

def configure():
    """
        Shows the configuration dialog
    """
    exaile = APP
    dialog = plugins.PluginConfigDialog(exaile.window, PLUGIN_NAME)
    table = gtk.Table(1, 2)
    table.set_row_spacings(2)
    bottom = 0
    label = gtk.Label(_("Mount Point:      "))
    label.set_alignment(0.0, 0.5)

    table.attach(label, 0, 1, bottom, bottom + 1)

    location = exaile.settings.get_str("mount", plugin=plugins.name(__file__),
        default="/mnt/device")

    loc_entry = gtk.FileChooserButton(_("Location"))
    loc_entry.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
    loc_entry.set_current_folder(location)
    table.attach(loc_entry, 1, 2, bottom, bottom + 1, gtk.SHRINK)

    save_loc = exaile.settings.get_str('save_loc',
        plugin=plugins.name(__file__), default='')

    bottom += 1
    label = gtk.Label(_("Transfer Save Location:"))
    label.set_alignment(0.0, 0.5)
    table.attach(label, 0, 1, bottom, bottom + 1)

    save_entry = gtk.FileChooserButton(_("Save Directory"))
    save_entry.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
    save_entry.set_current_folder(save_loc)
    table.attach(save_entry, 1, 2, bottom, bottom + 1, gtk.SHRINK)

    dialog.child.pack_start(table)
    dialog.show_all()

    result = dialog.run()
    dialog.hide()
    if result == gtk.RESPONSE_OK:
        exaile.settings.set_str("mount", loc_entry.get_current_folder(),
            plugin=plugins.name(__file__))
        exaile.settings.set_str('save_loc', loc_entry.get_current_folder(),
            plugin=plugins.name(__file__))

class MassStorageTrack(media.Track):
    type = 'device'
    pass

class MassStorageDriver(plugins.DeviceDriver):
    name = "massstorage"

    def __init__(self):
        plugins.DeviceDriver.__init__(self)
        self.db = None
        self.exaile = APP
        self.dp = APP.device_panel
        self.stopped = False

    def check_transfer_items(self, items):
        self.save_loc = self.exaile.settings.get_str('save_loc', default='', 
            plugin=plugins.name(__file__))
        if not self.save_loc:
            common.error(APP.window, _("You have not specified a directory "
                "to transfer your music in.  Please choose one in the "
                "configuration dialog"))
            configure()
            return False

        return True

    def remove_tracks(self, tracks):
        for track in tracks:
            try:
                self.all.remove(tracks)
                os.remove(track.io_loc)
            except:
                xlmisc.log_exception()

    def put_item(self, item):
        (item, nothing) = (item.track, item.target)
        if self.find_dup(item): return
        loc = os.path.basename(item.io_loc)

        try:
            shutil.copy(item.io_loc, os.path.join(self.save_loc, loc))
        except:
            xlmisc.log_exception()

        item = copy.deepcopy(item)
        item.loc = os.path.join(self.save_loc, loc)
        self.all.append(item)

    def find_dup(self, song):
        items = ('artist', 'album', 'genre', 'title', 'year')
        for s in self.all:
            check = False
            for item in items:
                if getattr(s, item) != getattr(song, item):
                    check = True

            if check: continue
            return True
        return False

    def remove_tracks(self, tracks):
        """
            Removes tracks
        """
        for track in tracks:
            if track in self.all: self.all.remove(track)
            try:
                os.unlink(track.io_loc)
            except OSError:
                pass
            except:
                xlmisc.log_exception()

    @common.threaded
    def connect(self, panel):
        """
            Connects and scans the device
        """
        self.stopped = False

        self.mount = self.exaile.settings.get_str("mount",
            plugin=plugins.name(__file__), default="/mnt/device")
        self.all = library.TrackData()

        files = library.scan_dir(str(self.mount), exts=media.SUPPORTED_MEDIA)
        for i, loc in enumerate(files):
            if self.stopped:
                self.stopped = False
                return
            tr = library.read_track(None, self.all, loc,
                track_type=MassStorageTrack)
            if not tr: continue
            self.all.append(tr)

        print 'we have connected, and scanned %d files!' % len(files)
        gobject.idle_add(panel.on_connect_complete, self)

    def search_tracks(self, keyword):

        if keyword:
            check = []
            for track in self.all:
                for item in ('artist', 'album', 'title'):
                    attr = getattr(track, item)
                    if keyword.lower() in attr.lower():
                        check.append(track) 
        else:
            check = self.all
        new = [(a.artist, a.album, a.track, a.title, a) for a in check]
        new.sort()
        return library.TrackData([a[4] for a in new])

    def disconnect(self):
        self.stopped = True

def initialize():
    global PLUGIN

    PLUGIN = MassStorageDriver()
    APP.device_panel.add_driver(PLUGIN, PLUGIN_NAME)

    return True

def destroy():
    global PLUGIN

    if PLUGIN:
        APP.device_panel.remove_driver(PLUGIN)

    PLUGIN = None