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
23 from ..plugin
import Plugin
25 class Filebrowser(Plugin
):
27 info
= 'A file browser that allows adding files not in collection.'
32 self
.o
= wgFilebrowser(self
)
37 def _get_dock_widget(self
):
38 return self
._create
_dock
(self
.o
)
40 class wgFilebrowser(QtGui
.QWidget
):
47 class FileView(QtGui
.QListView
):
53 def __init__(self
, model
, plugin
):
54 QtGui
.QListView
.__init
__(self
)
56 self
.logger
= plugin
.logger
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():
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
):
82 self
.logger
.info('Changind permissions of %s to %d.'%(filepath
, perm
))
84 os
.chmod(filepath
, perm
)
86 self
.logger
.error('Can\'t change permissions: %s.'%e)
88 def selection_copy_to_collection(self
):
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
)
95 if os
.path
.isdir(filepath
):
96 shutil
.copytree(filepath
, '%s/%s'%(self
.plugin
.settings
.value('MPD/music_dir').toString(), path_base
))
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
)
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
))
132 if not 'file://' in self
.plugin
.mpclient
.urlhandlers():
133 self
.logger
.error('file:// handler not available. Connect via unix domain sockets.')
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()))