]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/places/placespanel.cpp
Provide default view properties for system-places
[dolphin.git] / src / panels / places / placespanel.cpp
index ea2ec307271fcdeb0290e1cc28335634a7b1b769..0229d3cac23115781d5573177120a1e404d144a6 100644 (file)
 #include <KMenu>
 #include <KMessageBox>
 #include <KNotification>
+#include "placesitem.h"
 #include "placesitemeditdialog.h"
 #include "placesitemlistgroupheader.h"
+#include "placesitemlistwidget.h"
 #include "placesitemmodel.h"
 #include <views/draganddrophelper.h>
 #include <QVBoxLayout>
@@ -75,8 +77,11 @@ void PlacesPanel::showEvent(QShowEvent* event)
         m_model = new PlacesItemModel(this);
         m_model->setGroupedSorting(true);
         m_model->setSortRole("group");
+        connect(m_model, SIGNAL(errorMessage(QString)),
+                this, SIGNAL(errorMessage(QString)));
 
         KStandardItemListView* view = new KStandardItemListView();
+        view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
         view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
 
         m_controller = new KItemListController(m_model, view, this);
@@ -103,7 +108,7 @@ void PlacesPanel::slotItemActivated(int index)
 {
     const KUrl url = m_model->data(index).value("url").value<KUrl>();
     if (!url.isEmpty()) {
-        emit placeActivated(url);
+        emit placeActivated(PlacesItemModel::convertedUrl(url));
     }
 }
 
@@ -111,14 +116,16 @@ void PlacesPanel::slotItemMiddleClicked(int index)
 {
     const KUrl url = m_model->data(index).value("url").value<KUrl>();
     if (!url.isEmpty()) {
-        emit placeMiddleClicked(url);
+        emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
     }
 }
 
 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
 {
-    const QHash<QByteArray, QVariant> data = m_model->data(index);
-    const QString label = data.value("text").toString();
+    PlacesItem* item = m_model->placesItem(index);
+    if (!item) {
+        return;
+    }
 
     KMenu menu(this);
 
@@ -126,11 +133,12 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
     QAction* addAction = 0;
     QAction* mainSeparator = 0;
     QAction* editAction = 0;
-    QAction* tearDownAction = 0;
+    QAction* teardownAction = 0;
     QAction* ejectAction = 0;
 
-    const bool isSystemItem = m_model->isSystemItem(index);
-    const bool isDevice = !data.value("udi").toString().isEmpty();
+    const QString label = item->text();
+
+    const bool isDevice = !item->udi().isEmpty();
     if (isDevice) {
         ejectAction = m_model->ejectAction(index);
         if (ejectAction) {
@@ -138,36 +146,42 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
             menu.addAction(ejectAction);
         }
 
-        tearDownAction = m_model->tearDownAction(index);
-        if (tearDownAction) {
-            tearDownAction->setParent(&menu);
-            menu.addAction(tearDownAction);
+        teardownAction = m_model->teardownAction(index);
+        if (teardownAction) {
+            teardownAction->setParent(&menu);
+            menu.addAction(teardownAction);
         }
 
-        if (tearDownAction || ejectAction) {
+        if (teardownAction || ejectAction) {
             mainSeparator = menu.addSeparator();
         }
     } else {
-        if (data.value("url").value<KUrl>() == KUrl("trash:/")) {
+        if (item->url() == KUrl("trash:/")) {
             emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
             KConfig trashConfig("trashrc", KConfig::SimpleConfig);
             emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
             menu.addSeparator();
         }
         addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
-        if (!isSystemItem) {
-            mainSeparator = menu.addSeparator();
-            editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit Entry '%1'...", label));
-        }
+        mainSeparator = menu.addSeparator();
+        editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
     }
 
     if (!addAction) {
         addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
     }
 
-    QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide Entry '%1'", label));
+    QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
+    openInNewTabAction->setIcon(KIcon("tab-new"));
+
+    QAction* removeAction = 0;
+    if (!isDevice && !item->isSystemItem()) {
+        removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
+    }
+
+    QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
     hideAction->setCheckable(true);
-    //hideEntry->setChecked(data.value("hidden").toBool());
+    hideAction->setChecked(item->isHidden());
 
     QAction* showAllAction = 0;
     if (m_model->hiddenCount() > 0) {
@@ -176,12 +190,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
         }
         showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
         showAllAction->setCheckable(true);
-        //showAllEntries->setChecked(showAll)
-    }
-
-    QAction* removeAction = 0;
-    if (!isDevice && !isSystemItem) {
-        removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove Entry '%1'", label));
+        showAllAction->setChecked(m_model->hiddenItemsShown());
     }
 
     menu.addSeparator();
@@ -200,9 +209,16 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
         } else if (action == removeAction) {
             m_model->removeItem(index);
         } else if (action == hideAction) {
+            item->setHidden(hideAction->isChecked());
+        } else if (action == openInNewTabAction) {
+            const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
+            emit placeMiddleClicked(url);
         } else if (action == showAllAction) {
-        } else if (action == tearDownAction) {
+            m_model->setHiddenItemsShown(showAllAction->isChecked());
+        } else if (action == teardownAction) {
+            m_model->requestTeardown(index);
         } else if (action == ejectAction) {
+            m_model->requestEject(index);
         }
     }
 
@@ -214,14 +230,26 @@ void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
     KMenu menu(this);
 
     QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
+
+    QAction* showAllAction = 0;
+    if (m_model->hiddenCount() > 0) {
+        showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
+        showAllAction->setCheckable(true);
+        showAllAction->setChecked(m_model->hiddenItemsShown());
+    }
+
     menu.addSeparator();
     foreach (QAction* action, customContextMenuActions()) {
         menu.addAction(action);
     }
 
     QAction* action = menu.exec(pos.toPoint());
-    if (action == addAction) {
-        addEntry();
+    if (action) {
+        if (action == addAction) {
+            addEntry();
+        } else if (action == showAllAction) {
+            m_model->setHiddenItemsShown(showAllAction->isChecked());
+        }
     }
 
     selectClosestItem();
@@ -240,7 +268,7 @@ void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget*
 void PlacesPanel::slotTrashUpdated(KJob* job)
 {
     if (job->error()) {
-        // TODO: Show error-string inside Dolphin, don't use job->ui->showErrorMessage().
+        emit errorMessage(job->errorString());
     }
     org::kde::KDirNotify::emitFilesAdded("trash:/");
 }
@@ -275,17 +303,17 @@ void PlacesPanel::addEntry()
     dialog->setAllowGlobal(true);
     dialog->setUrl(url);
     if (dialog->exec() == QDialog::Accepted) {
-        KStandardItem* item = createStandardItemFromDialog(dialog);
+        PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
 
         // Insert the item as last item of the corresponding group.
         int i = 0;
-        while (i < m_model->count() && m_model->item(i)->group() != item->group()) {
+        while (i < m_model->count() && m_model->placesItem(i)->group() != item->group()) {
             ++i;
         }
 
         bool inserted = false;
         while (!inserted && i < m_model->count()) {
-            if (m_model->item(i)->group() != item->group()) {
+            if (m_model->placesItem(i)->group() != item->group()) {
                 m_model->insertItem(i, item);
                 inserted = true;
             }
@@ -311,14 +339,11 @@ void PlacesPanel::editEntry(int index)
     dialog->setUrl(data.value("url").value<KUrl>());
     dialog->setAllowGlobal(true);
     if (dialog->exec() == QDialog::Accepted) {
-        KStandardItem* oldItem = m_model->item(index);
+        PlacesItem* oldItem = m_model->placesItem(index);
         if (oldItem) {
-            KStandardItem* item = createStandardItemFromDialog(dialog);
-            // Although the user might have changed the URL of the item in a way
-            // that another group should be assigned, we still apply the old
-            // group to keep the same position for the item.
-            item->setGroup(oldItem->group());
-            m_model->replaceItem(index, item);
+            oldItem->setText(dialog->text());
+            oldItem->setUrl(dialog->url());
+            oldItem->setIcon(dialog->icon());
         }
     }
 
@@ -334,18 +359,4 @@ void PlacesPanel::selectClosestItem()
     selectionManager->setSelected(index);
 }
 
-KStandardItem* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog* dialog) const
-{
-    Q_ASSERT(dialog);
-
-    const KUrl newUrl = dialog->url();
-    KStandardItem* item = new KStandardItem();
-    item->setIcon(KIcon(dialog->icon()));
-    item->setText(dialog->text());
-    item->setDataValue("url", newUrl);
-    item->setGroup(m_model->groupName(newUrl));
-
-    return item;
-}
-
 #include "placespanel.moc"