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
|
#
# Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
# Nephilim 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 3 of the License, or
# (at your option) any later version.
#
# Nephilim 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 Nephilim. If not, see <http://www.gnu.org/licenses/>.
#
from PyQt4 import QtGui, QtCore
import os
import shutil
from ..plugin import Plugin
class Filebrowser(Plugin):
# public, const
info = 'A file browser that allows adding files not in collection.'
# public, read-only
o = None
def _load(self):
self.o = wgFilebrowser(self)
def _unload(self):
self.o = None
def _get_dock_widget(self):
return self._create_dock(self.o)
class wgFilebrowser(QtGui.QWidget):
view = None
model = None
path = None
plugin = None
logger = None
class FileView(QtGui.QListView):
"context menu"
menu = None
plugin = None
logger = None
def __init__(self, model, plugin):
QtGui.QListView.__init__(self)
self.plugin = plugin
self.logger = plugin.logger
self.setModel(model)
self.setRootIndex(self.model().index(os.path.expanduser('~')))
self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
self.menu = QtGui.QMenu('file')
self.menu.addAction('&Make file(s) readable for MPD.', self.selection_make_readable)
self.menu.addAction('***EXPERIMENTAL DON\'T USE*** &Copy to collection.', self.selection_copy_to_collection)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
def show_context_menu(self, pos):
if not self.indexAt(pos).isValid():
return
self.menu.popup(self.mapToGlobal(pos))
def selection_make_readable(self):
for index in self.selectedIndexes():
filepath = self.model().filePath(index)
if os.path.isdir(filepath):
perm = 0755
else:
perm = 0644
self.logger.info('Changind permissions of %s to %d.'%(filepath, perm))
try:
os.chmod(filepath, perm)
except OSError, e:
self.logger.error('Can\'t change permissions: %s.'%e)
def selection_copy_to_collection(self):
target_paths = []
for index in self.selectedIndexes():
filepath = self.model().filePath(index)
self.logger.info('Copying %s to collection.'%filepath)
path_base = os.path.basename(filepath)
try:
if os.path.isdir(filepath):
shutil.copytree(filepath, '%s/%s'%(self.plugin.settings.value('MPD/music_dir'), path_base))
else:
shutil.copy(filepath, self.plugin.settings.value('MPD/music_dir'))
target_paths.append(path_base)
except (OSError, IOError), e:
self.logger.error('Error copying to collection: %s.'%e)
self.plugin.mpclient.update_db(target_paths)
def __init__(self, plugin):
QtGui.QWidget.__init__(self)
self.plugin = plugin
self.logger = plugin.logger
self.model = QtGui.QDirModel()
self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.AllEntries)
self.model.setSorting(QtCore.QDir.DirsFirst)
self.view = self.FileView(self.model, self.plugin)
self.view.activated.connect(self.item_activated)
self.path = QtGui.QLineEdit(self.model.filePath(self.view.rootIndex()))
self.path.returnPressed.connect(self.path_changed)
self.setLayout(QtGui.QVBoxLayout())
self.layout().setSpacing(0)
self.layout().setMargin(0)
self.layout().addWidget(self.path)
self.layout().addWidget(self.view)
def item_activated(self, index):
if self.model.hasChildren(index):
self.view.setRootIndex(index)
self.path.setText(self.model.filePath(index))
else:
if not 'file://' in self.plugin.mpclient.urlhandlers:
self.logger.error('file:// handler not available. Connect via unix domain sockets.')
return
paths = []
for index in self.view.selectedIndexes():
paths.append('file://%s'%self.model.filePath(index))
self.logger.info('Adding %d song to playlist.'%len(paths))
self.plugin.mpclient.add(paths)
def path_changed(self):
if os.path.isdir(self.path.text()):
self.view.setRootIndex(self.model.index(self.path.text()))
|