1 #include "filelistwidget.h"
4 #include <QInputDialog>
5 #include <QDesktopServices>
8 #include <QDBusInterface>
10 # include <hildon-mime.h>
11 # include <dbus/dbus.h>
14 Widget that shows filesystemmodel and handles navigation
15 in directory tree and opening files with assosiated programs
17 @todo in symbian and windows filesystems navigating to "root" wont show drives
19 FileListWidget::FileListWidget(QWidget *parent) :
21 fileSystemModel( new QFileSystemModel(this)),
22 currentDir(QDir::homePath()),
27 this->setModel(fileSystemModel);
28 this->setRootIndex(fileSystemModel->index(currentDir.absolutePath()));
29 fileSystemModel->setRootPath(currentDir.absolutePath());
30 fileSystemModel->setFilter(fileSystemModel->filter() | QDir::System);
31 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(handleItemActivation(QModelIndex)));
37 @param iconmode true shows iconview, false shows listview
39 void FileListWidget::actionSwitchMode(bool iconmode)
42 this->setViewMode(QListView::IconMode);
43 this->setWordWrap(true);
44 this->setGridSize(QSize(80,80));
46 this->setViewMode(QListView::ListMode);
47 this->setWordWrap(false);
48 this->setGridSize(QSize());
54 @param show true shows hidden files
56 void FileListWidget::actionShowHidden(bool show)
59 fileSystemModel->setFilter(fileSystemModel->filter() | QDir::Hidden);
61 fileSystemModel->setFilter(fileSystemModel->filter() &~ QDir::Hidden);
63 this->clearSelection();
69 void FileListWidget::actionRename()
71 QFileInfo file = fileSystemModel->fileInfo(this->selectedIndexes().first());
72 QString newName = QInputDialog::getText(this, tr("Rename"), tr("New filename: "), QLineEdit::Normal, file.fileName());
73 if(newName != file.fileName())
75 if(QFile::rename(file.absoluteFilePath(), file.absolutePath()+"/"+newName))
78 QMessageBox::critical(this,tr("Error!")
79 ,tr("Renaming file %1 failed")
85 Selected files will be moved when actionPaste is called
87 void FileListWidget::actionCut()
91 selectedFiles = this->selectedIndexes();
94 Selected files will be copied when actionPaste is called
96 void FileListWidget::actionCopy()
100 selectedFiles = this->selectedIndexes();
104 Moves or copies files that were selected when actionCut or actionCopy called
106 void FileListWidget::actionPaste()
108 fileSystemModel->setReadOnly(false);
110 //Copy files until filelist is empty or error occured
111 while(!selectedFiles.isEmpty()) {
112 if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
113 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
114 selectedFiles.removeFirst();
116 else if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
117 , fileSystemModel->rootPath()+"/copy_"+fileSystemModel->fileName(selectedFiles.first()))) {
118 selectedFiles.removeFirst();
120 QMessageBox::critical(this,tr("Error!")
121 ,tr("Copying file %1 failed")
122 .arg(fileSystemModel->fileName(selectedFiles.first()))
127 if(selectedFiles.isEmpty())
129 } else if(mode_cut) {
130 //Move files until filelist is empty or error occured
131 while(!selectedFiles.isEmpty()) {
132 if(QFile::rename(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
133 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
134 selectedFiles.removeFirst();
136 QMessageBox::critical(this,tr("Error!")
137 ,tr("Moving file %1 failed")
138 .arg(fileSystemModel->fileName(selectedFiles.first()))
143 if(selectedFiles.isEmpty())
146 fileSystemModel->setReadOnly(true);
147 this->clearSelection();
151 Deletes selected files
153 void FileListWidget::actionDelete()
157 if(QMessageBox::Yes == QMessageBox::warning(this, tr("Deleting file")
158 ,tr("You are about to delete %1 file(s).\nAre you sure you want to continue?")
159 .arg(this->selectedIndexes().count())
160 , QMessageBox::Yes, QMessageBox::No)) {
161 fileSystemModel->setReadOnly(false);
162 selectedFiles = this->selectedIndexes();
163 //delete files until filelist empty or error occured
164 while(!selectedFiles.isEmpty()) {
165 if(fileSystemModel->remove(selectedFiles.first())) {
166 selectedFiles.removeFirst();
168 QMessageBox::critical(this,tr("Error!")
169 ,tr("Deleting file %1 failed")
170 .arg(fileSystemModel->fileName(selectedFiles.first()))
175 fileSystemModel->setReadOnly(true);
176 this->clearSelection();
181 @return Current directory shown
183 QString FileListWidget::getPath()
185 return currentDir.absolutePath();
189 Changes current directory
190 @param path directory to change to
192 void FileListWidget::changePath(QString path)
195 QString newPath = currentDir.absolutePath();
196 fileSystemModel->setRootPath(newPath);
197 this->clearSelection();
198 this->setRootIndex(fileSystemModel->index(newPath));
199 emit pathChanged(newPath);
203 Equivalent to changePath("..")
205 void FileListWidget::changePathUp()
210 void FileListWidget::handleItemActivation(QModelIndex index)
213 QFileInfo file = fileSystemModel->fileInfo(index);
215 changePath(file.absoluteFilePath());
216 } else if(file.isExecutable()) {
218 QProcess::startDetached(file.absoluteFilePath());
220 #ifdef Q_WS_MAEMO_5 // Uses native file opening method
221 //TODO: find better solution for this, maybe get fixed in Qt
222 DBusConnection* conn;
223 conn = dbus_bus_get(DBUS_BUS_SESSION, 0);
224 hildon_mime_open_file(conn, QUrl::fromLocalFile(file.absoluteFilePath()).toEncoded().constData());
227 Not working with maemo5.
228 Uses hildon_uri_open function from
229 libhildonmime which should work,
230 but all files opened in browser.
232 QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
238 @param mode true activates file selection
240 void FileListWidget::setSelectMode(bool mode)
246 Opens native bluetooth dialog to choose receiving device and sends selected files there.
248 void FileListWidget::actionSendFiles()
251 // Create list of file urls
253 foreach(QModelIndex index, this->selectedIndexes()) {
254 files.append(QUrl::fromLocalFile(fileSystemModel->fileInfo(index)).absoluteFilePath()).toString());
257 // Make dbuscall to send files
258 QDBusInterface interface("com.nokia.bt_ui", "/com/nokia/bt_ui", "com.nokia.bt_ui",QDBusConnection::systemBus());
259 interface.call("show_send_file_dlg", files);
261 QMessageBox::information(this,
263 tr("Only in maemo5 for now"),
264 QMessageBox::Cancel);