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 "/" 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());
53 @param show true shows hidden files
55 void FileListWidget::actionShowHidden(bool show)
58 fileSystemModel->setFilter(fileSystemModel->filter() | QDir::Hidden);
60 fileSystemModel->setFilter(fileSystemModel->filter() &~ QDir::Hidden);
62 this->clearSelection();
68 void FileListWidget::actionRename()
70 QFileInfo file = fileSystemModel->fileInfo(this->selectedIndexes().first());
71 QString newName = QInputDialog::getText(this, tr("Rename"), tr("New filename: "), QLineEdit::Normal, file.fileName());
72 if(newName != file.fileName())
74 if(QFile::rename(file.absoluteFilePath(), file.absolutePath()+"/"+newName))
77 QMessageBox::critical(this,tr("Error!")
78 ,tr("Renaming file %1 failed")
84 Selected files will be moved when actionPaste is called
86 void FileListWidget::actionCut()
90 selectedFiles = this->selectedIndexes();
93 Selected files will be copied when actionPaste is called
95 void FileListWidget::actionCopy()
99 selectedFiles = this->selectedIndexes();
103 Moves or copies files that were selected when actionCut or actionCopy called
105 void FileListWidget::actionPaste()
107 fileSystemModel->setReadOnly(false);
109 //Copy files until filelist is empty or error occured
110 while(!selectedFiles.isEmpty()) {
111 if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
112 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
113 selectedFiles.removeFirst();
115 else if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
116 , fileSystemModel->rootPath()+"/copy_"+fileSystemModel->fileName(selectedFiles.first()))) {
117 selectedFiles.removeFirst();
119 QMessageBox::critical(this,tr("Error!")
120 ,tr("Copying file %1 failed")
121 .arg(fileSystemModel->fileName(selectedFiles.first()))
126 if(selectedFiles.isEmpty())
128 } else if(mode_cut) {
129 //Move files until filelist is empty or error occured
130 while(!selectedFiles.isEmpty()) {
131 if(QFile::rename(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
132 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
133 selectedFiles.removeFirst();
135 QMessageBox::critical(this,tr("Error!")
136 ,tr("Moving file %1 failed")
137 .arg(fileSystemModel->fileName(selectedFiles.first()))
142 if(selectedFiles.isEmpty())
145 fileSystemModel->setReadOnly(true);
146 this->clearSelection();
150 Deletes selected files
152 void FileListWidget::actionDelete()
156 if(QMessageBox::Yes == QMessageBox::warning(this, tr("Deleting file")
157 ,tr("You are about to delete %1 file(s).\nAre you sure you want to continue?")
158 .arg(this->selectedIndexes().count())
159 , QMessageBox::Yes, QMessageBox::No)) {
160 fileSystemModel->setReadOnly(false);
161 selectedFiles = this->selectedIndexes();
162 //delete files until filelist empty or error occured
163 while(!selectedFiles.isEmpty()) {
164 if(fileSystemModel->remove(selectedFiles.first())) {
165 selectedFiles.removeFirst();
167 QMessageBox::critical(this,tr("Error!")
168 ,tr("Deleting file %1 failed")
169 .arg(fileSystemModel->fileName(selectedFiles.first()))
174 fileSystemModel->setReadOnly(true);
175 this->clearSelection();
180 @return Current directory
182 QString FileListWidget::getPath()
184 return currentDir.absolutePath();
188 Changes current directory
189 @param path directory to change to
191 void FileListWidget::changePath(QString path)
194 QString newPath = currentDir.absolutePath();
195 fileSystemModel->setRootPath(newPath);
196 this->clearSelection();
197 this->setRootIndex(fileSystemModel->index(newPath));
198 emit pathChanged(newPath);
202 Equivalent to changePath("..")
204 void FileListWidget::changePathUp()
209 void FileListWidget::handleItemActivation(QModelIndex index)
212 QFileInfo file = fileSystemModel->fileInfo(index);
214 changePath(file.absoluteFilePath());
215 } else if(file.isExecutable()) {
217 QProcess::startDetached(file.absoluteFilePath());
219 #ifdef Q_WS_MAEMO_5 // Uses native file opening method
220 DBusConnection* conn;
221 conn = dbus_bus_get(DBUS_BUS_SESSION, 0);
222 hildon_mime_open_file(conn, QUrl::fromLocalFile(file.absoluteFilePath()).toEncoded().constData());
225 Not working with maemo5.
226 Uses hildon_uri_open function from
227 libhildonmime which should work,
228 but all files opened in browser.
230 QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
236 @param mode true activates file selection
238 void FileListWidget::setSelectMode(bool mode)
243 void FileListWidget::actionSendFiles()
246 QMessageBox::information(this,
248 tr("To send files, select files you want to send and copy them."),
249 QMessageBox::Cancel);
252 // Create list of file urls
254 while(!selectedFiles.isEmpty()) {
255 files.append(QUrl::fromLocalFile(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()).toString());
256 selectedFiles.removeFirst();
259 // Make dbuscall to send files
260 QDBusInterface interface("com.nokia.bt_ui", "/com/nokia/bt_ui", "com.nokia.bt_ui",QDBusConnection::systemBus());
261 interface.call("show_send_file_dlg", files);
263 QMessageBox::information(this,
265 tr("Only in maemo5 for now"),
266 QMessageBox::Cancel);