summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Filebrowser.py
blob: 71969eb6b6b6733f65217ba21822f136785ed8a7 (plain)
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
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant
import os

from ..clPlugin import Plugin
from ..misc     import ORGNAME, APPNAME

class Filebrowser(Plugin):
    o = None
    def _load(self):
        self.o = wgFilebrowser(self)

    def _unload(self):
        self.o = None

    def getInfo(self):
        return 'A file browser that allows adding files not in collection.'

    def _get_dock_widget(self):
        return self._create_dock(self.o)

class wgFilebrowser(QtGui.QWidget):
    view   = None
    model  = None
    path   = None
    plugin = None

    def __init__(self, plugin):
        QtGui.QWidget.__init__(self)
        self.plugin = plugin

        self.model = QtGui.QDirModel()
        self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.AllEntries)
        self.model.setSorting(QtCore.QDir.DirsFirst)

        self.view  = QtGui.QListView()
        self.view.setModel(self.model)
        self.view.setRootIndex(self.model.index(os.path.expanduser('~')))
        self.view.setSelectionMode(QtGui.QTreeWidget.ExtendedSelection)
        self.connect(self.view, QtCore.SIGNAL('activated(const QModelIndex&)'), self.item_activated)

        self.path = QtGui.QLineEdit(self.model.filePath(self.view.rootIndex()))
        self.connect(self.path, QtCore.SIGNAL('returnPressed()'), 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.set_status('file:// handler not available. Connect via unix domain sockets.')
                return
            paths = []
            for index in self.view.selectedIndexes():
                paths.append(u'file://' + unicode(self.model.filePath(index)))
            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()))