]> 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 de5c95b77b4f90ec25545df19e64785b7bac11bf..0229d3cac23115781d5573177120a1e404d144a6 100644 (file)
@@ -38,6 +38,7 @@
 #include <KMenu>
 #include <KMessageBox>
 #include <KNotification>
+#include "placesitem.h"
 #include "placesitemeditdialog.h"
 #include "placesitemlistgroupheader.h"
 #include "placesitemlistwidget.h"
@@ -76,6 +77,8 @@ 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>());
@@ -105,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));
     }
 }
 
@@ -113,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);
 
@@ -128,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) {
@@ -140,17 +146,17 @@ 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));
@@ -169,13 +175,13 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
     openInNewTabAction->setIcon(KIcon("tab-new"));
 
     QAction* removeAction = 0;
-    if (!isDevice && !isSystemItem) {
+    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);
-    hideAction->setChecked(data.value("isHidden").toBool());
+    hideAction->setChecked(item->isHidden());
 
     QAction* showAllAction = 0;
     if (m_model->hiddenCount() > 0) {
@@ -203,14 +209,16 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
         } else if (action == removeAction) {
             m_model->removeItem(index);
         } else if (action == hideAction) {
-            m_model->setItemHidden(index, hideAction->isChecked());
+            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) {
             m_model->setHiddenItemsShown(showAllAction->isChecked());
-        } else if (action == tearDownAction) {
+        } else if (action == teardownAction) {
+            m_model->requestTeardown(index);
         } else if (action == ejectAction) {
+            m_model->requestEject(index);
         }
     }
 
@@ -260,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:/");
 }
@@ -295,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;
             }
@@ -331,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());
         }
     }
 
@@ -354,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"