switch to PyQt4 API v2 for QStrings
[nephilim.git] / nephilim / plugins / Filebrowser.py
blobd31078930617704726e6573934b408bd96527fba
2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
18 from PyQt4 import QtGui, QtCore
19 from PyQt4.QtCore import QVariant
20 import os
21 import shutil
23 from ..plugin import Plugin
25 class Filebrowser(Plugin):
26 # public, const
27 info = 'A file browser that allows adding files not in collection.'
29 # public, read-only
30 o = None
31 def _load(self):
32 self.o = wgFilebrowser(self)
34 def _unload(self):
35 self.o = None
37 def _get_dock_widget(self):
38 return self._create_dock(self.o)
40 class wgFilebrowser(QtGui.QWidget):
41 view = None
42 model = None
43 path = None
44 plugin = None
45 logger = None
47 class FileView(QtGui.QListView):
48 "context menu"
49 menu = None
50 plugin = None
51 logger = None
53 def __init__(self, model, plugin):
54 QtGui.QListView.__init__(self)
55 self.plugin = plugin
56 self.logger = plugin.logger
58 self.setModel(model)
59 self.setRootIndex(self.model().index(os.path.expanduser('~')))
60 self.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
62 self.menu = QtGui.QMenu('file')
63 self.menu.addAction('&Make file(s) readable for MPD.', self.selection_make_readable)
64 self.menu.addAction('***EXPERIMENTAL DON\'T USE*** &Copy to collection.', self.selection_copy_to_collection)
66 self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
67 self.customContextMenuRequested.connect(self.show_context_menu)
69 def show_context_menu(self, pos):
70 if not self.indexAt(pos).isValid():
71 return
72 self.menu.popup(self.mapToGlobal(pos))
74 def selection_make_readable(self):
75 for index in self.selectedIndexes():
76 filepath = self.model().filePath(index)
77 if os.path.isdir(filepath):
78 perm = 0755
79 else:
80 perm = 0644
82 self.logger.info('Changind permissions of %s to %d.'%(filepath, perm))
83 try:
84 os.chmod(filepath, perm)
85 except OSError, e:
86 self.logger.error('Can\'t change permissions: %s.'%e)
88 def selection_copy_to_collection(self):
89 target_paths = []
90 for index in self.selectedIndexes():
91 filepath = unicode(self.model().filePath(index))
92 self.logger.info('Copying %s to collection.'%filepath)
93 path_base = os.path.basename(filepath)
94 try:
95 if os.path.isdir(filepath):
96 shutil.copytree(filepath, '%s/%s'%(self.plugin.settings.value('MPD/music_dir').toString(), path_base))
97 else:
98 shutil.copy(filepath, unicode(self.plugin.settings.value('MPD/music_dir').toString()))
99 target_paths.append(path_base)
100 except (OSError, IOError), e:
101 self.logger.error('Error copying to collection: %s.'%e)
103 self.plugin.mpclient.update_db(target_paths)
106 def __init__(self, plugin):
107 QtGui.QWidget.__init__(self)
108 self.plugin = plugin
109 self.logger = plugin.logger
111 self.model = QtGui.QDirModel()
112 self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.AllEntries)
113 self.model.setSorting(QtCore.QDir.DirsFirst)
115 self.view = self.FileView(self.model, self.plugin)
116 self.view.activated.connect(self.item_activated)
118 self.path = QtGui.QLineEdit(self.model.filePath(self.view.rootIndex()))
119 self.path.returnPressed.connect(self.path_changed)
121 self.setLayout(QtGui.QVBoxLayout())
122 self.layout().setSpacing(0)
123 self.layout().setMargin(0)
124 self.layout().addWidget(self.path)
125 self.layout().addWidget(self.view)
127 def item_activated(self, index):
128 if self.model.hasChildren(index):
129 self.view.setRootIndex(index)
130 self.path.setText(self.model.filePath(index))
131 else:
132 if not 'file://' in self.plugin.mpclient.urlhandlers():
133 self.logger.error('file:// handler not available. Connect via unix domain sockets.')
134 return
135 paths = []
136 for index in self.view.selectedIndexes():
137 paths.append(u'file://' + unicode(self.model.filePath(index)))
138 self.logger.info('Adding %d song to playlist.'%len(paths))
139 self.plugin.mpclient.add(paths)
141 def path_changed(self):
142 if os.path.isdir(self.path.text()):
143 self.view.setRootIndex(self.model.index(self.path.text()))