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() && !newName.isEmpty())
75 if(QFile::rename(file.absoluteFilePath(), file.absolutePath()+"/"+newName))
78 QMessageBox::critical(this,tr("Error!")
79 ,tr("Renaming file %1 failed")
86 Selected files will be moved when actionPaste is called
88 void FileListWidget::actionCut()
92 selectedFiles = this->selectedIndexes();
95 Selected files will be copied when actionPaste is called
97 void FileListWidget::actionCopy()
101 selectedFiles = this->selectedIndexes();
105 Moves or copies files that were selected when actionCut or actionCopy called
107 void FileListWidget::actionPaste()
109 fileSystemModel->setReadOnly(false);
111 //Copy files until filelist is empty or error occured
112 while(!selectedFiles.isEmpty()) {
113 if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
114 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
115 selectedFiles.removeFirst();
117 else if(QFile::copy(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
118 , fileSystemModel->rootPath()+"/copy_"+fileSystemModel->fileName(selectedFiles.first()))) {
119 selectedFiles.removeFirst();
121 QMessageBox::critical(this,tr("Error!")
122 ,tr("Copying file %1 failed")
123 .arg(fileSystemModel->fileName(selectedFiles.first()))
128 if(selectedFiles.isEmpty())
130 } else if(mode_cut) {
131 //Move files until filelist is empty or error occured
132 while(!selectedFiles.isEmpty()) {
133 if(QFile::rename(fileSystemModel->fileInfo(selectedFiles.first()).absoluteFilePath()
134 , fileSystemModel->rootPath()+"/"+fileSystemModel->fileName(selectedFiles.first()))) {
135 selectedFiles.removeFirst();
137 QMessageBox::critical(this,tr("Error!")
138 ,tr("Moving file %1 failed")
139 .arg(fileSystemModel->fileName(selectedFiles.first()))
144 if(selectedFiles.isEmpty())
147 fileSystemModel->setReadOnly(true);
148 this->clearSelection();
149 setSelectMode(false);
153 Deletes selected files
155 void FileListWidget::actionDelete()
159 if(QMessageBox::Yes == QMessageBox::warning(this, tr("Deleting file")
160 ,tr("You are about to delete %1 file(s).\nAre you sure you want to continue?")
161 .arg(this->selectedIndexes().count())
162 , QMessageBox::Yes, QMessageBox::No)) {
163 fileSystemModel->setReadOnly(false);
164 selectedFiles = this->selectedIndexes();
165 //delete files until filelist empty or error occured
166 while(!selectedFiles.isEmpty()) {
167 if(fileSystemModel->remove(selectedFiles.first())) {
168 selectedFiles.removeFirst();
170 QMessageBox::critical(this,tr("Error!")
171 ,tr("Deleting file %1 failed")
172 .arg(fileSystemModel->fileName(selectedFiles.first()))
177 fileSystemModel->setReadOnly(true);
178 this->clearSelection();
180 setSelectMode(false);
184 @return Current directory shown
186 QString FileListWidget::getPath()
188 return currentDir.absolutePath();
192 Changes current directory
193 @param path directory to change to
195 void FileListWidget::changePath(QString path)
198 QString newPath = currentDir.absolutePath();
199 fileSystemModel->setRootPath(newPath);
200 this->clearSelection();
201 this->setRootIndex(fileSystemModel->index(newPath));
202 emit pathChanged(newPath);
203 setSelectMode(false);
207 Equivalent to changePath("..")
209 void FileListWidget::changePathUp()
214 void FileListWidget::handleItemActivation(QModelIndex index)
217 QFileInfo file = fileSystemModel->fileInfo(index);
219 changePath(file.absoluteFilePath());
220 } else if(file.isExecutable()) {
222 QProcess::startDetached(file.absoluteFilePath());
224 #ifdef Q_WS_MAEMO_5 // Uses native file opening method
225 //TODO: find better solution for this, maybe get fixed in Qt
226 DBusConnection* conn;
227 conn = dbus_bus_get(DBUS_BUS_SESSION, 0);
228 hildon_mime_open_file(conn, QUrl::fromLocalFile(file.absoluteFilePath()).toEncoded().constData());
231 Not working with maemo5.
232 Uses hildon_uri_open function from
233 libhildonmime which should work,
234 but all files opened in browser.
236 QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
240 setSelectMode(false);
243 @param mode true activates file selection
245 void FileListWidget::setSelectMode(bool mode)
248 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
250 this->setSelectionMode(QAbstractItemView::SingleSelection);
255 Opens native bluetooth dialog to choose receiving device and sends selected files there.
257 void FileListWidget::actionSendFiles()
260 // Create list of file urls
263 foreach(QModelIndex index, this->selectedIndexes()) {
264 file = fileSystemModel->fileInfo(index);
266 QMessageBox::warning(this,
268 tr("Sending directories not supported"),
269 QMessageBox::Cancel);
272 files.append(QUrl::fromLocalFile(file.absoluteFilePath()).toString());
275 // Make dbuscall to send files
276 QDBusInterface interface("com.nokia.bt_ui", "/com/nokia/bt_ui", "com.nokia.bt_ui",QDBusConnection::systemBus());
277 interface.call(QDBus::Block, "show_send_file_dlg", files);
280 QMessageBox::information(this,
282 tr("Only in maemo5 for now"),
283 QMessageBox::Cancel);
285 setSelectMode(false);