]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/infosidebarpage.cpp
Added Rafael López's item categorizer into Dolphin (it's currently deactivated in...
[dolphin.git] / src / infosidebarpage.cpp
index 61a7b06438560694b2a6a3f377d2b6c6a7a95a4a..42352429b5729ebaa6309375b8f0b2a304b1ceb6 100644 (file)
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
  ***************************************************************************/
 
+#include <config-kmetadata.h>
+
 #include "infosidebarpage.h"
-#include <assert.h>
-
-#include <qlayout.h>
-#include <qpixmap.h>
-#include <qlabel.h>
-#include <qtimer.h>
-#include <qpushbutton.h>
-
-#include <q3popupmenu.h>
-#include <qpainter.h>
-#include <qfontmetrics.h>
-#include <Q3ValueList>
+
+#include <QLayout>
+#include <QPixmap>
+#include <QLabel>
+#include <QTimer>
+#include <QPushButton>
+#include <QMenu>
+#include <QPainter>
+#include <QFontMetrics>
 #include <QEvent>
 #include <QInputDialog>
+#include <QDir>
 
-#include <kbookmarkmanager.h>
+#include <kfileplacesmodel.h>
 #include <klocale.h>
 #include <kstandarddirs.h>
 #include <kio/previewjob.h>
 #include <kfilemetainfo.h>
 #include <kvbox.h>
 #include <kseparator.h>
+#include <kiconloader.h>
+
+#ifdef HAVE_KMETADATA
+#include <kratingwidget.h>
+#endif
 
-#include "dolphinmainwindow.h"
-#include "dolphinapplication.h"
 #include "pixmapviewer.h"
 #include "dolphinsettings.h"
-#include "metadataloader.h"
-
-InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent) :
-    SidebarPage(mainWindow, parent),
-    m_multipleSelection(false),
-    m_pendingPreview(false),
-    m_timer(0),
-    m_preview(0),
-    m_name(0),
-    m_infos(0),
-    m_metadata(DolphinApplication::app()->metadataLoader())
+#include "metadatawidget.h"
+
+InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
+        SidebarPage(parent),
+        m_multipleSelection(false), //TODO:check if I'm needed
+        m_pendingPreview(false),
+        m_timer(0),
+        m_currentSelection(KFileItemList()),
+        m_preview(0),
+        m_name(0),
+        m_infos(0)
 {
     const int spacing = KDialog::spacingHint();
 
@@ -91,54 +94,49 @@ InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent)
 
     KSeparator* sep2 = new KSeparator(this);
 
-    // annotation
-    KSeparator* sep3 = 0;
-    if (m_metadata->storageUp()) {
-        m_annotationLabel = new QLabel(this);
-        m_annotationLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
-        m_annotationLabel->setTextFormat(Qt::RichText);
-        m_annotationLabel->setWordWrap(true);
-        m_annotationButton = new QPushButton("", this);
-        m_annotationButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
-        connect(m_annotationButton, SIGNAL(released()), this, SLOT(changeAnnotation()));
-        sep3 = new KSeparator(this);
-    }
+    if (MetaDataWidget::metaDataAvailable())
+        m_metadataWidget = new MetaDataWidget(this);
+    else
+        m_metadataWidget = 0;
 
     // actions
     m_actionBox = new KVBox(this);
     m_actionBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
 
-    // Add a dummy widget with no restriction regarding a vertical resizing.
-    // This assures that information is always top aligned.
-    QWidget* dummy = new QWidget(this);
-
     layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
     layout->addWidget(m_preview);
     layout->addWidget(m_name);
     layout->addWidget(sep1);
     layout->addWidget(m_infos);
     layout->addWidget(sep2);
-    if (m_metadata->storageUp()) {
-        layout->addWidget(m_annotationLabel);
-        layout->addWidget(m_annotationButton);
-        layout->addWidget(sep3);
+    if (m_metadataWidget) {
+        layout->addWidget(m_metadataWidget);
+        layout->addWidget(new KSeparator(this));
     }
     layout->addWidget(m_actionBox);
-    layout->addWidget(dummy);
+    // ensure that widgets in the information side bar are aligned towards the top
+    layout->addStretch(1);
     setLayout(layout);
-    connect(mainWindow, SIGNAL(selectionChanged()),
-            this, SLOT(showItemInfo()));
-
-    connectToActiveView();
 }
 
 InfoSidebarPage::~InfoSidebarPage()
+{}
+
+void InfoSidebarPage::setUrl(const KUrl& url)
 {
+    if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
+        cancelRequest();
+        m_shownUrl = url;
+        showItemInfo();
+    }
 }
 
-void InfoSidebarPage::activeViewChanged()
+void InfoSidebarPage::setSelection(const KFileItemList& selection)
 {
-    connectToActiveView();
+    cancelRequest();
+    m_currentSelection = selection;
+    m_multipleSelection = (m_currentSelection.size() > 1);
+    showItemInfo();
 }
 
 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
@@ -152,29 +150,13 @@ void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
     }
 }
 
-void InfoSidebarPage::requestItemInfo(const KUrl& url)
-{
-    cancelRequest();
-
-    if (!url.isEmpty() && !m_multipleSelection) {
-        m_shownUrl = url;
-        showItemInfo();
-    }
-}
-
 void InfoSidebarPage::showItemInfo()
 {
     cancelRequest();
 
-    m_multipleSelection = false;
-
-    // show the preview...
-    DolphinView* view = mainWindow()->activeView();
-    const KFileItemList selectedItems = view->selectedItems();
+    KFileItemList selectedItems = m_currentSelection;
     KUrl file;
-    if (selectedItems.count() > 1) {
-        m_multipleSelection = true;
-    } else if(selectedItems.count() == 0) {
+    if (selectedItems.count() == 0) {
         file = m_shownUrl;
     } else {
         file = selectedItems[0]->url();
@@ -185,9 +167,8 @@ void InfoSidebarPage::showItemInfo()
                                            K3Icon::NoGroup,
                                            K3Icon::SizeEnormous);
         m_preview->setPixmap(icon);
-        m_name->setText(i18n("%1 items selected",selectedItems.count()));
-    }
-    else if (!applyBookmark(file)) {
+        m_name->setText(i18n("%1 items selected", selectedItems.count()));
+    } else if (!applyBookmark(file)) {
         // try to get a preview pixmap from the item...
         KUrl::List list;
         list.append(file);
@@ -233,9 +214,10 @@ void InfoSidebarPage::slotPreviewFailed(const KFileItem* item)
     }
 }
 
-void InfoSidebarPage::gotPreview(const KFileItem* /* item */,
+void InfoSidebarPage::gotPreview(const KFileItem* item,
                                  const QPixmap& pixmap)
 {
+    Q_UNUSED(item);
     if (m_pendingPreview) {
         m_preview->setPixmap(pixmap);
         m_pendingPreview = false;
@@ -244,49 +226,32 @@ void InfoSidebarPage::gotPreview(const KFileItem* /* item */,
 
 void InfoSidebarPage::startService(int index)
 {
-    DolphinView* view = mainWindow()->activeView();
-    if (view->hasSelection()) {
-        KUrl::List selectedUrls = view->selectedUrls();
-        KDEDesktopMimeType::executeService(selectedUrls, m_actionsVector[index]);
-    }
-    else {
-        KDEDesktopMimeType::executeService(m_shownUrl, m_actionsVector[index]);
+    if (m_currentSelection.count() > 0) {
+        // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
+        KDesktopFileActions::executeService(m_currentSelection.urlList(), m_actionsVector[index]);
+    } else {
+        // TODO: likewise
+        KDesktopFileActions::executeService(m_shownUrl, m_actionsVector[index]);
     }
 }
 
-void InfoSidebarPage::connectToActiveView()
+bool InfoSidebarPage::applyBookmark(const KUrl& url)
 {
-    cancelRequest();
-
-    DolphinView* view = mainWindow()->activeView();
-    connect(view, SIGNAL(requestItemInfo(const KUrl&)),
-            this, SLOT(requestDelayedItemInfo(const KUrl&)));
-    connect(view, SIGNAL(urlChanged(const KUrl&)),
-            this, SLOT(requestItemInfo(const KUrl&)));
+    KFilePlacesModel *placesModel = DolphinSettings::instance().placesModel();
+    int count = placesModel->rowCount();
 
-    m_shownUrl = view->url();
-    showItemInfo();
-}
+    for (int i = 0; i < count; ++i) {
+        QModelIndex index = placesModel->index(i, 0);
 
-bool InfoSidebarPage::applyBookmark(const KUrl& url)
-{
-    KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
-    KBookmark bookmark = root.first();
-    while (!bookmark.isNull()) {
-        if (url.equals(bookmark.url(), KUrl::CompareWithoutTrailingSlash)) {
+        if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
             QString text("<b>");
-            text.append(bookmark.text());
+            text.append(placesModel->text(index));
             text.append("</b>");
             m_name->setText(text);
 
-            KIconLoader iconLoader;
-            QPixmap icon = iconLoader.loadIcon(bookmark.icon(),
-                                               K3Icon::NoGroup,
-                                               K3Icon::SizeEnormous);
-            m_preview->setPixmap(icon);
+            m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
             return true;
         }
-        bookmark = root.next(bookmark);
     }
 
     return false;
@@ -301,18 +266,17 @@ void InfoSidebarPage::cancelRequest()
 void InfoSidebarPage::createMetaInfo()
 {
     beginInfoLines();
-    DolphinView* view = mainWindow()->activeView();
-    if (!view->hasSelection()) {
+    if (m_currentSelection.size() == 0) {
         KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
         fileItem.refresh();
 
         if (fileItem.isDir()) {
             addInfoLine(i18n("Type:"), i18n("Directory"));
         }
-        showAnnotation(m_shownUrl);
-    }
-    else if (view->selectedItems().count() == 1) {
-        KFileItem* fileItem = view->selectedItems()[0];
+        if (MetaDataWidget::metaDataAvailable())
+            m_metadataWidget->setFile(fileItem.url());
+    } else if (m_currentSelection.count() == 1) {
+        KFileItem* fileItem = m_currentSelection.at(0);
         addInfoLine(i18n("Type:"), fileItem->mimeComment());
 
         QString sizeText(KIO::convertSize(fileItem->size()));
@@ -329,12 +293,13 @@ void InfoSidebarPage::createMetaInfo()
                 }
             }
         }
-        showAnnotation(fileItem->url());
-    }
-    else {
-        showAnnotations(view->selectedItems().urlList());
+        if (MetaDataWidget::metaDataAvailable())
+            m_metadataWidget->setFile(fileItem->url());
+    } else {
+        if (MetaDataWidget::metaDataAvailable())
+            m_metadataWidget->setFiles(m_currentSelection.urlList());
         unsigned long int totSize = 0;
-        foreach(KFileItem* item, view->selectedItems()) {
+        foreach(KFileItem* item, m_currentSelection) {
             totSize += item->size(); //FIXME what to do with directories ? (same with the one-item-selected-code), item->size() does not return the size of the content : not very instinctive for users
         }
         addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
@@ -356,19 +321,19 @@ bool InfoSidebarPage::showMetaInfo(const QString& key) const
 {
     // sorted list of keys, where it's data should be shown
     static const char* keys[] = {
-        "Album",
-        "Artist",
-        "Author",
-        "Bitrate",
-        "Date",
-        "Dimensions",
-        "Genre",
-        "Length",
-        "Lines",
-        "Pages",
-        "Title",
-        "Words"
-    };
+                                    "Album",
+                                    "Artist",
+                                    "Author",
+                                    "Bitrate",
+                                    "Date",
+                                    "Dimensions",
+                                    "Genre",
+                                    "Length",
+                                    "Lines",
+                                    "Pages",
+                                    "Title",
+                                    "Words"
+                                };
 
     // do a binary search for the key...
     int top = 0;
@@ -378,11 +343,9 @@ bool InfoSidebarPage::showMetaInfo(const QString& key) const
         const int result = key.compare(keys[middle]);
         if (result < 0) {
             bottom = middle - 1;
-        }
-        else if (result > 0) {
+        } else if (result > 0) {
             top = middle + 1;
-        }
-        else {
+        } else {
             return true;
         }
     }
@@ -399,17 +362,14 @@ void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoT
 
 void InfoSidebarPage::insertActions()
 {
-    // delete all existing action widgets
-    // TODO: just use children() from QObject...
-    Q3PtrListIterator<QWidget> deleteIter(m_actionWidgets);
+    QListIterator<QPushButton*> deleteIter(m_actionBox->findChildren<QPushButton*>());
     QWidget* widget = 0;
-    while ((widget = deleteIter.current()) != 0) {
+    while (deleteIter.hasNext()) {
+        widget = deleteIter.next();
         widget->close();
         widget->deleteLater();
-        ++deleteIter;
     }
 
-    m_actionWidgets.clear();
     m_actionsVector.clear();
 
     int actionsIndex = 0;
@@ -418,7 +378,7 @@ void InfoSidebarPage::insertActions()
     // of KFileItems. If no selection is given, a temporary KFileItem
     // by the given Url 'url' is created and added to the list.
     KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
-    KFileItemList itemList = mainWindow()->activeView()->selectedItems();
+    KFileItemList itemList = m_currentSelection;
     if (itemList.isEmpty()) {
         fileItem.refresh();
         itemList.append(&fileItem);
@@ -433,7 +393,7 @@ void InfoSidebarPage::insertActions()
         QStringList entries = dir.entryList(QStringList("*.desktop"), QDir::Files);
 
         for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
-            KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal ), "Desktop Entry" );
+            KConfigGroup cfg(KSharedConfig::openConfig(*dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry");
             if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
                 const QStringList types = cfg.readEntry("ServiceTypes", QStringList(), ',');
                 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
@@ -470,11 +430,11 @@ void InfoSidebarPage::insertActions()
                     }
 
                     if (insert) {
-                        const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
-                        Q3PopupMenu* popup = 0;
+                        const QString submenuName = cfg.readEntry("X-KDE-Submenu");
+                        QMenu* popup = 0;
                         if (!submenuName.isEmpty()) {
                             // create a sub menu containing all actions
-                            popup = new Q3PopupMenu();
+                            popup = new QMenu();
                             connect(popup, SIGNAL(activated(int)),
                                     this, SLOT(startService(int)));
 
@@ -482,27 +442,24 @@ void InfoSidebarPage::insertActions()
                             button->setFlat(true);
                             button->setMenu(popup);
                             button->show();
-                            m_actionWidgets.append(button);
                         }
 
-                        Q3ValueList<KDEDesktopMimeType::Service> userServices =
-                            KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
+                        QList<KDesktopFileActions::Service> userServices =
+                            KDesktopFileActions::userDefinedServices(*dirIt + *entryIt, true);
 
                         // iterate through all actions and add them to a widget
-                        Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
+                        QList<KDesktopFileActions::Service>::Iterator serviceIt;
                         for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
-                            KDEDesktopMimeType::Service service = (*serviceIt);
+                            KDesktopFileActions::Service service = (*serviceIt);
                             if (popup == 0) {
                                 ServiceButton* button = new ServiceButton(KIcon(service.m_strIcon),
-                                                                          service.m_strName,
-                                                                          m_actionBox,
-                                                                          actionsIndex);
+                                                        service.m_strName,
+                                                        m_actionBox,
+                                                        actionsIndex);
                                 connect(button, SIGNAL(requestServiceStart(int)),
                                         this, SLOT(startService(int)));
-                                m_actionWidgets.append(button);
                                 button->show();
-                            }
-                            else {
+                            } else {
                                 popup->insertItem(KIcon(service.m_strIcon), service.m_strName, actionsIndex);
                             }
 
@@ -516,81 +473,15 @@ void InfoSidebarPage::insertActions()
     }
 }
 
-void InfoSidebarPage::showAnnotation(const KUrl& file)
-{
-    if(m_metadata->storageUp()) {
-        QString text = m_metadata->annotation(file);
-        if (!text.isEmpty()) {
-            m_annotationLabel->show();
-            m_annotationLabel->setText(QString("<b>%1</b> :<br/>%2").arg(i18n("Annotation")).arg(text));
-            m_annotationButton->setText(i18n("Change annotation"));
-        } else {
-            m_annotationLabel->hide();
-            m_annotationButton->setText(i18n("Annotate file"));
-        }
-    }
-}
-
-void InfoSidebarPage::showAnnotations(const KUrl::List& files)
-{
-    if (m_metadata->storageUp()) {
-        bool hasAnnotation = false;
-        unsigned int annotateNum = 0;
-        QString firsts = QString("<b>%1 :</b><br/>").arg(i18n("Annotations"));
-        foreach (KUrl file, files) {
-            QString annotation = m_metadata->annotation(file);
-            if (!annotation.isEmpty()) {
-                hasAnnotation = true;
-                if (annotateNum < 3) {
-                    // don't show more than 3 annotations
-                    firsts += m_annotationLabel->fontMetrics().elidedText(QString("<b>%1</b> : %2<br/>").arg(file.fileName()).arg(annotation), Qt::ElideRight, width());//FIXME not really the good method, does not handle resizing ...
-                    annotateNum++;
-                }
-            }
-        }
-        if (hasAnnotation) {
-            m_annotationLabel->show();
-            m_annotationLabel->setText(firsts);
-        }
-        else {
-            m_annotationLabel->hide();
-        }
-        m_annotationButton->setText(hasAnnotation ? i18n("Change annotations") : i18n("Annotate files"));
-    }
-}
 
-void InfoSidebarPage::changeAnnotation()
-{
-    bool ok = false;
-    KUrl::List files(mainWindow()->activeView()->selectedItems().urlList());
-    QString name, old;
-    if (files.isEmpty()) {
-        files << m_shownUrl;
-    }
-    else if (files.count() == 1) {
-        name = files[0].url();
-        old = m_metadata->annotation(files[0]);
-    }
-    else {
-        name = QString("%1 files").arg(files.count());
-        old = QString();
-    }
-    QString text = QInputDialog::getText(this, "Annotate", QString("Set annotation for %1").arg(name), QLineEdit::Normal, old, &ok);//FIXME temporary, must move to a real dialog
-    if(ok) {
-        foreach(KUrl file, files) {
-            m_metadata->setAnnotation(file, text);
-        }
-        showAnnotation(files[0]);
-    }
-}
 
 ServiceButton::ServiceButton(const QIcon& icon,
                              const QString& text,
                              QWidget* parent,
                              int index) :
-    QPushButton(icon, text, parent),
-    m_hover(false),
-    m_index(index)
+        QPushButton(icon, text, parent),
+        m_hover(false),
+        m_index(index)
 {
     setEraseColor(palette().brush(QPalette::Background).color());
     setFocusPolicy(Qt::NoFocus);
@@ -599,8 +490,7 @@ ServiceButton::ServiceButton(const QIcon& icon,
 }
 
 ServiceButton::~ServiceButton()
-{
-}
+{}
 
 void ServiceButton::paintEvent(QPaintEvent* event)
 {
@@ -614,8 +504,7 @@ void ServiceButton::paintEvent(QPaintEvent* event)
     if (m_hover) {
         backgroundColor = KGlobalSettings::highlightColor();
         foregroundColor = KGlobalSettings::highlightedTextColor();
-    }
-    else {
+    } else {
         backgroundColor = palette().brush(QPalette::Background).color();
         foregroundColor = KGlobalSettings::buttonTextColor();
     }