]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinview.cpp
Prevent that Konqueror crashs if pressing F2 (= Rename) although no items are selecte...
[dolphin.git] / src / dolphinview.cpp
index 0caefe87a83b2fcea068dcb0dad44de67301f85f..2f80682ccc23dff4e316a3312cfafc0cbdb0fa13 100644 (file)
@@ -41,6 +41,7 @@
 #include <kmenu.h>
 #include <kmessagebox.h>
 #include <kmimetyperesolver.h>
+#include <konq_fileitemcapabilities.h>
 #include <konq_operations.h>
 #include <konqmimedata.h>
 #include <ktoggleaction.h>
@@ -84,9 +85,10 @@ DolphinView::DolphinView(QWidget* parent,
     m_dirLister(dirLister),
     m_proxyModel(proxyModel),
     m_iconManager(0),
-    m_toolTipManager(0)
+    m_toolTipManager(0),
+    m_rootUrl(),
+    m_currentItemUrl()
 {
-    setFocusPolicy(Qt::StrongFocus);
     m_topLayout = new QVBoxLayout(this);
     m_topLayout->setSpacing(0);
     m_topLayout->setMargin(0);
@@ -94,14 +96,10 @@ DolphinView::DolphinView(QWidget* parent,
     m_controller = new DolphinController(this);
     m_controller->setUrl(url);
 
-    // Receiver of the DolphinView signal 'urlChanged()' don't need
-    // to care whether the internal controller changed the URL already or whether
-    // the controller just requested an URL change and will be updated later.
-    // In both cases the URL has been changed:
     connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
             this, SIGNAL(urlChanged(const KUrl&)));
     connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
-            this, SIGNAL(urlChanged(const KUrl&)));
+            this, SLOT(slotRequestUrlChange(const KUrl&)));
 
     connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
             this, SLOT(openContextMenu(const QPoint&)));
@@ -124,6 +122,11 @@ DolphinView::DolphinView(QWidget* parent,
     connect(m_controller, SIGNAL(viewportEntered()),
             this, SLOT(clearHoverInformation()));
 
+    connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
+            this, SLOT(slotRedirection(KUrl, KUrl)));
+    connect(m_dirLister, SIGNAL(completed()),
+            this, SLOT(restoreCurrentItem()));
+
     applyViewProperties(url);
     m_topLayout->addWidget(itemView());
 }
@@ -149,7 +152,6 @@ void DolphinView::setActive(bool active)
     }
 
     m_active = active;
-    m_selectionModel->clearSelection();
 
     QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
     if (active) {
@@ -170,6 +172,7 @@ void DolphinView::setActive(bool active)
     update();
 
     if (active) {
+        itemView()->setFocus();
         emit activated();
     }
 
@@ -292,6 +295,10 @@ void DolphinView::clearSelection()
 
 KFileItemList DolphinView::selectedItems() const
 {
+    if (isColumnViewActive()) {
+        return m_columnView->selectedItems();
+    }
+
     const QAbstractItemView* view = itemView();
 
     // Our view has a selection, we will map them back to the DolphinModel
@@ -322,10 +329,15 @@ KUrl::List DolphinView::selectedUrls() const
     return urls;
 }
 
-KFileItem DolphinView::fileItem(const QModelIndex& index) const
+int DolphinView::selectedItemsCount() const
 {
-    const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
-    return m_dolphinModel->itemForIndex(dolphinModelIndex);
+    if (isColumnViewActive()) {
+        // TODO: get rid of this special case by adjusting the dir lister
+        // to the current column
+        return m_columnView->selectedItems().count();
+    }
+
+    return itemView()->selectionModel()->selection().count();
 }
 
 void DolphinView::setContentsPosition(int x, int y)
@@ -439,6 +451,7 @@ void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
         return;
     }
 
+    m_iconManager->cancelPreviews();
     m_controller->setUrl(url); // emits urlChanged, which we forward
 
     if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
@@ -480,6 +493,8 @@ void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
 
 void DolphinView::setUrl(const KUrl& url)
 {
+    // remember current item candidate (see restoreCurrentItem())
+    m_currentItemUrl = url;
     updateView(url, KUrl());
 }
 
@@ -507,7 +522,12 @@ void DolphinView::changeSelection(const KFileItemList& selection)
 void DolphinView::renameSelectedItems()
 {
     const KFileItemList items = selectedItems();
-    if (items.count() > 1) {
+    const int itemCount = items.count();
+    if (itemCount < 1) {
+        return;
+    }
+    
+    if (itemCount > 1) {
         // More than one item has been selected for renaming. Open
         // a rename dialog and rename all items afterwards.
         RenameDialog dialog(this, items);
@@ -542,8 +562,6 @@ void DolphinView::renameSelectedItems()
             }
         }
     } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
-        Q_ASSERT(items.count() == 1);
-
         if (isColumnViewActive()) {
             m_columnView->editItem(items.first());
         } else {
@@ -552,8 +570,6 @@ void DolphinView::renameSelectedItems()
             itemView()->edit(proxyIndex);
         }
     } else {
-        Q_ASSERT(items.count() == 1);
-
         RenameDialog dialog(this, items);
         if (dialog.exec() == QDialog::Rejected) {
             return;
@@ -768,10 +784,14 @@ void DolphinView::emitSelectionChangedSignal()
 void DolphinView::openContextMenu(const QPoint& pos)
 {
     KFileItem item;
-
-    const QModelIndex index = itemView()->indexAt(pos);
-    if (index.isValid() && (index.column() == DolphinModel::Name)) {
-        item = fileItem(index);
+    if (isColumnViewActive()) {
+        item = m_columnView->itemAt(pos);
+    } else {
+        const QModelIndex index = itemView()->indexAt(pos);
+        if (index.isValid() && (index.column() == DolphinModel::Name)) {
+            const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
+            item = m_dolphinModel->itemForIndex(dolphinModelIndex);
+        }
     }
 
     if (m_toolTipManager != 0) {
@@ -788,24 +808,18 @@ void DolphinView::dropUrls(const KUrl::List& urls,
                            const KFileItem& destItem)
 {
     Q_ASSERT(!urls.isEmpty());
-    const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
-                              destItem.url() : destPath;
+    const KUrl destination = !destItem.isNull() && destItem.isDir() ?
+                             destItem.url() : destPath;
     const KUrl sourceDir = KUrl(urls.first().directory());
     if (sourceDir != destination) {
-        dropUrls(urls, destination);
+        DolphinDropController dropController(this);
+        // forward doingOperation signal up to the mainwindow
+        connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
+                this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
+        dropController.dropUrls(urls, destination);
     }
 }
 
-void DolphinView::dropUrls(const KUrl::List& urls,
-                           const KUrl& destination)
-{
-    DolphinDropController dropController(this);
-    // forward doingOperation signal up to the mainwindow
-    connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
-            this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
-    dropController.dropUrls(urls, destination);
-}
-
 void DolphinView::updateSorting(DolphinView::Sorting sorting)
 {
     ViewProperties props(viewPropertiesUrl());
@@ -897,7 +911,10 @@ QPair<bool, QString> DolphinView::pasteInfo() const
 
     KUrl::List urls = KUrl::List::fromMimeData(mimeData);
     if (!urls.isEmpty()) {
-        ret.first = true;
+        // disable the paste action if no writing is supported
+        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url());
+        ret.first = KonqFileItemCapabilities(KFileItemList() << item).supportsWriting();
+
         if (urls.count() == 1) {
             const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
             ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
@@ -956,6 +973,21 @@ void DolphinView::slotDeleteFileFinished(KJob* job)
     }
 }
 
+
+void DolphinView::restoreCurrentItem()
+{
+    const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
+    if (dirIndex.isValid()) {
+        const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
+        QAbstractItemView* view = itemView();
+        const bool clearSelection = !hasSelection();
+        view->setCurrentIndex(proxyIndex);
+        if (clearSelection) {
+            view->clearSelection();
+        }
+    }
+}
+
 void DolphinView::loadDirectory(const KUrl& url, bool reload)
 {
     if (!url.isValid()) {
@@ -1102,7 +1134,6 @@ void DolphinView::createView()
 
     view->setSelectionMode(QAbstractItemView::ExtendedSelection);
 
-    new KMimeTypeResolver(view, m_dolphinModel);
     m_iconManager = new IconManager(view, m_proxyModel);
     m_iconManager->setShowPreview(m_showPreview);
 
@@ -1124,6 +1155,12 @@ void DolphinView::deleteView()
 {
     QAbstractItemView* view = itemView();
     if (view != 0) {
+        // It's important to set the keyboard focus to the parent
+        // before deleting the view: Otherwise when having a split
+        // view the other view will get the focus and will request
+        // an activation (see DolphinView::eventFilter()).
+        setFocus();
+
         m_topLayout->removeWidget(view);
         view->close();
         view->deleteLater();
@@ -1182,4 +1219,17 @@ void DolphinView::pasteToUrl(const KUrl& url)
     }
 }
 
+void DolphinView::slotRequestUrlChange(const KUrl& url)
+{
+    emit requestUrlChange(url);
+    m_controller->setUrl(url);
+}
+
+void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
+{
+    if (oldUrl == m_controller->url()) {
+        m_controller->setUrl(newUrl);
+    }
+}
+
 #include "dolphinview.moc"