v0.3.0
[case] / src / filelist.cpp
1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
3 // 
4 // This program 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.
8 // 
9 // This program 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.
13 // 
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 #include "filelist.h"
19
20 #include <QProcess>
21 #include <QUrl>
22 #include <QEvent>
23 #include <QScrollBar>
24
25 #include <hildon-mime.h>
26 #include <dbus/dbus.h>
27
28 #include "utils.h"
29
30
31 FileList::FileList(QWidget *parent) :
32     QListView(parent),
33     fileSystemModel(new QFileSystemModel),
34     dontSelect(0)
35 {
36     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37
38     //setSelectionMode(QAbstractItemView::SingleSelection);
39     setSelectionMode(QAbstractItemView::MultiSelection);
40
41     setModel(fileSystemModel);
42
43     fileSystemModel->setFilter(fileSystemModel->filter() | QDir::System);
44     setRootIndex(fileSystemModel->setRootPath(QDir::homePath()));
45
46     connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(activateItem(QModelIndex)));
47 }
48
49
50 const QFileInfoList FileList::selection() const {
51     QFileInfoList files;
52     QModelIndexList l = selectionModel()->selectedIndexes();
53     for (int i = 0; i < l.size(); ++i) {
54         files.append(fileSystemModel->fileInfo(l[i]));
55     }
56     return files;
57 }
58
59
60 const QString FileList::path() const {
61     return fileSystemModel->rootPath();
62 }
63
64
65 bool FileList::changePath(QString path) {
66     path = unwindPath(path);
67     QDir dir(fileSystemModel->rootPath());
68     if (dir.cd(path)) {
69         scrollBarPosition[fileSystemModel->rootPath()] = verticalScrollBar()->value();
70         setRootIndex(fileSystemModel->setRootPath(dir.absolutePath()));
71         clearSelection();
72         verticalScrollBar()->setValue(scrollBarPosition[dir.absolutePath()]);
73         emit pathChanged(fileSystemModel->rootPath());
74         return true;
75     }
76
77     return false;
78 }
79
80
81 bool FileList::goUp() {
82     return changePath("..");
83 }
84
85
86 void FileList::toggleShowHiddenFiles() {
87     clearSelection();
88     scrollToTop();
89     fileSystemModel->setFilter(fileSystemModel->filter() ^ QDir::Hidden);
90 }
91
92
93 void FileList::preventNextSelection() {
94     dontSelect = 2;
95 }
96
97
98 void FileList::mousePressEvent(QMouseEvent *event) {
99     emit mousePressed();
100     QListView::mousePressEvent(event);
101 }
102
103
104 QItemSelectionModel::SelectionFlags FileList::selectionCommand(const QModelIndex &index, const QEvent *event) const {
105     if (dontSelect && event && event->type() == QEvent::MouseButtonPress) {
106         --dontSelect;
107     }
108
109     if (dontSelect && event &&
110         (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove))
111     {
112         return QItemSelectionModel::NoUpdate;
113     }
114
115     return QListView::selectionCommand(index, event);
116 }
117
118
119 void FileList::activateItem(QModelIndex index) {
120     const QFileInfo &file = fileSystemModel->fileInfo(index);
121
122     if(file.isDir()) {
123         changePath(file.absoluteFilePath());
124         // hack: this will prevent selecting the item under the cursor on the second click
125         // of the doubleclick that changed the directory
126         dontSelect = 1;
127     } else if(file.isExecutable()) {
128         QProcess::startDetached(file.absoluteFilePath());
129     } else {
130         // TODO: find better solution for this, maybe get fixed in Qt
131         DBusConnection* conn;
132         conn = dbus_bus_get(DBUS_BUS_SESSION, 0);
133         hildon_mime_open_file(conn, QUrl::fromLocalFile(file.absoluteFilePath()).toEncoded().constData());
134
135         // Not working with maemo5. Uses hildon_uri_open function from
136         // libhildonmime which should work, but all files opened in browser.
137         //QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
138     }
139 }