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