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