Ctrl-H shortcut to toggle hidden files' visibility
[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
23 #include <hildon-mime.h>
24 #include <dbus/dbus.h>
25
26
27 FileList::FileList(QWidget *parent) :
28     QListView(parent),
29     fileSystemModel(new QFileSystemModel)
30 {
31     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32
33     //setSelectionMode(QAbstractItemView::SingleSelection);
34     setSelectionMode(QAbstractItemView::MultiSelection);
35
36     setModel(fileSystemModel);
37
38     fileSystemModel->setFilter(fileSystemModel->filter() | QDir::System);
39     setRootIndex(fileSystemModel->setRootPath(QDir::homePath()));
40
41     connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(activateItem(QModelIndex)));
42 }
43
44
45 const QFileInfoList FileList::selection() const {
46     QFileInfoList files;
47     QModelIndexList l = selectionModel()->selectedIndexes();
48     for (int i = 0; i < l.size(); ++i) {
49         files.append(fileSystemModel->fileInfo(l[i]));
50     }
51     return files;
52 }
53
54
55 const QString FileList::path() const {
56     return fileSystemModel->rootPath();
57 }
58
59
60 bool FileList::changePath(QString path) {
61     QDir dir(fileSystemModel->rootPath());
62     if (dir.cd(path)) {
63         setRootIndex(fileSystemModel->setRootPath(dir.absolutePath()));
64         clearSelection();
65         emit pathChanged(fileSystemModel->rootPath());
66         return true;
67     }
68
69     return false;
70 }
71
72
73 bool FileList::goUp() {
74     return changePath("..");
75 }
76
77
78 void FileList::toggleShowHiddenFiles() {
79     clearSelection();
80     scrollToTop();
81     fileSystemModel->setFilter(fileSystemModel->filter() ^ QDir::Hidden);
82 }
83
84
85 void FileList::activateItem(QModelIndex index) {
86     const QFileInfo &file = fileSystemModel->fileInfo(index);
87
88     if(file.isDir()) {
89         changePath(file.absoluteFilePath());
90         // hack: we reset it to MultiSelection again in the mousePressEvent
91         // without this, the item under the cursor gets selected right after changing the directory
92         setSelectionMode(QAbstractItemView::NoSelection);
93     } else if(file.isExecutable()) {
94         QProcess::startDetached(file.absoluteFilePath());
95     } else {
96         // TODO: find better solution for this, maybe get fixed in Qt
97         DBusConnection* conn;
98         conn = dbus_bus_get(DBUS_BUS_SESSION, 0);
99         hildon_mime_open_file(conn, QUrl::fromLocalFile(file.absoluteFilePath()).toEncoded().constData());
100
101         // Not working with maemo5. Uses hildon_uri_open function from
102         // libhildonmime which should work, but all files opened in browser.
103         //QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
104     }
105 }
106
107
108 void FileList::mousePressEvent(QMouseEvent *event) {
109     emit mousePressed();
110     QListView::mousePressEvent(event);
111     // need to reset the selection mode in case it was set to NoSelection in activateItem(...)
112     setSelectionMode(QAbstractItemView::MultiSelection);
113 }