]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/infosidebarpage.cpp
move the QT3_SUPPORT definition only where it's needed
[dolphin.git] / src / infosidebarpage.cpp
index 9c400b19877ab4eed020dd10ef9d9e6b57eaad6e..889fd21f15ce0def661abbfdd015c7759d7b07dd 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 <klocale.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"
+#include "metadatawidget.h"
 
-InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent) :
-    SidebarPage(mainWindow, parent),
-    m_multipleSelection(false),
+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),
-    m_metadata(DolphinApplication::app()->metadataLoader())
+    m_infos(0)
 {
     const int spacing = KDialog::spacingHint();
 
@@ -91,18 +94,10 @@ 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);
@@ -118,47 +113,44 @@ InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent)
     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);
     setLayout(layout);
-    connect(mainWindow, SIGNAL(selectionChanged()),
-            this, SLOT(showItemInfo()));
-
-    connectToActiveView();
 }
 
 InfoSidebarPage::~InfoSidebarPage()
 {
 }
 
-void InfoSidebarPage::activeViewChanged()
+void InfoSidebarPage::setUrl(const KUrl& url)
 {
-    connectToActiveView();
+    if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
+        cancelRequest();
+        m_shownUrl = url;
+        showItemInfo();
+    }
 }
 
-void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
+void InfoSidebarPage::setSelection(const KFileItemList& selection)
 {
     cancelRequest();
-
-    if (!url.isEmpty() && !m_multipleSelection) {
-        m_urlCandidate = url;
-        m_timer->setSingleShot(true);
-        m_timer->start(300);
-    }
+    m_currentSelection = selection;
+    m_multipleSelection = (m_currentSelection.size() > 1);
+    showItemInfo();
 }
 
-void InfoSidebarPage::requestItemInfo(const KUrl& url)
+void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
 {
     cancelRequest();
 
     if (!url.isEmpty() && !m_multipleSelection) {
-        m_shownUrl = url;
-        showItemInfo();
+        m_urlCandidate = url;
+        m_timer->setSingleShot(true);
+        m_timer->start(300);
     }
 }
 
@@ -166,15 +158,9 @@ 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();
@@ -233,9 +219,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,30 +231,16 @@ 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]);
+    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 {
-        KDEDesktopMimeType::executeService(m_shownUrl, m_actionsVector[index]);
+        // TODO: likewise
+        KDesktopFileActions::executeService(m_shownUrl, m_actionsVector[index]);
     }
 }
 
-void InfoSidebarPage::connectToActiveView()
-{
-    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&)));
-
-    m_shownUrl = view->url();
-    showItemInfo();
-}
-
 bool InfoSidebarPage::applyBookmark(const KUrl& url)
 {
     KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
@@ -301,18 +274,18 @@ 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);
+        if ( MetaDataWidget::metaDataAvailable() )
+            m_metadataWidget->setFile( fileItem.url() );
     }
-    else if (view->selectedItems().count() == 1) {
-        KFileItem* fileItem = view->selectedItems()[0];
+    else if (m_currentSelection.count() == 1) {
+        KFileItem* fileItem = m_currentSelection.at(0);
         addInfoLine(i18n("Type:"), fileItem->mimeComment());
 
         QString sizeText(KIO::convertSize(fileItem->size()));
@@ -325,16 +298,18 @@ void InfoSidebarPage::createMetaInfo()
             for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
                 if (showMetaInfo(*it)) {
                     KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
-                    addInfoLine(*it, metaInfoItem.string());
+                    addInfoLine(*it, metaInfoItem.value().toString());
                 }
             }
         }
-        showAnnotation(fileItem->url());
+        if ( MetaDataWidget::metaDataAvailable() )
+            m_metadataWidget->setFile( fileItem->url() );
     }
     else {
-        showAnnotations(view->selectedItems().urlList());
+        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));
@@ -399,17 +374,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 +390,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);
@@ -471,10 +443,10 @@ void InfoSidebarPage::insertActions()
 
                     if (insert) {
                         const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
-                        Q3PopupMenu* popup = 0;
+                        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,16 +454,15 @@ 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,
@@ -499,7 +470,6 @@ void InfoSidebarPage::insertActions()
                                                                           actionsIndex);
                                 connect(button, SIGNAL(requestServiceStart(int)),
                                         this, SLOT(startService(int)));
-                                m_actionWidgets.append(button);
                                 button->show();
                             }
                             else {
@@ -516,70 +486,7 @@ void InfoSidebarPage::insertActions()
     }
 }
 
-void InfoSidebarPage::showAnnotation(const KUrl& file)
-{
-    if(m_metadata->storageUp()) {
-        QString text = m_metadata->getAnnotation(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)
-{
-    static unsigned int maxShownAnnot = 3; //The maximum number of show annotations when selecting multiple 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->getAnnotation(file);
-            if (!annotation.isEmpty()) {
-                hasAnnotation = true;
-                if(annotateNum < maxShownAnnot) {
-                    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->getAnnotation(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,