]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/urlnavigator.cpp
Cleanup of the URL navigator, so that the DolphinMainWindow and the DolphinView are...
[dolphin.git] / src / urlnavigator.cpp
index 1f9295cb5fbfcb6129b3cfa9400eaa79bd9bd290..93de43e67817e186c654bfe057ec9d51c5f1ce5a 100644 (file)
@@ -16,7 +16,7 @@
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
  ***************************************************************************/
 
 #include "urlnavigator.h"
@@ -34,6 +34,7 @@
 #include <qsizepolicy.h>
 #include <qtooltip.h>
 //Added by qt3to4:
+#include <QDir>
 #include <Q3ValueList>
 #include <QKeyEvent>
 #include <QCheckBox>
@@ -42,6 +43,7 @@
 #include <kactioncollection.h>
 #include <kiconloader.h>
 #include <kio/job.h>
+#include <kfileitem.h>
 #include <klocale.h>
 #include <kprotocolinfo.h>
 #include <kurl.h>
 #include <kvbox.h>
 
 #include "bookmarkselector.h"
-#include "dolphin.h"
 #include "dolphinsettings.h"
-#include "dolphinstatusbar.h"
-#include "dolphinview.h"
 #include "generalsettings.h"
 #include "protocolcombo.h"
 #include "urlnavigatorbutton.h"
 
-UrlNavigator::HistoryElem::HistoryElem()
:  m_url(),
+UrlNavigator::HistoryElem::HistoryElem() :
   m_url(),
     m_currentFileName(),
     m_contentsX(0),
     m_contentsY(0)
 {
 }
 
-UrlNavigator::HistoryElem::HistoryElem(const KUrl& url)
:  m_url(url),
+UrlNavigator::HistoryElem::HistoryElem(const KUrl& url) :
   m_url(url),
     m_currentFileName(),
     m_contentsX(0),
     m_contentsY(0)
@@ -80,10 +79,10 @@ UrlNavigator::HistoryElem::~HistoryElem()
 }
 
 UrlNavigator::UrlNavigator(const KUrl& url,
-                           DolphinView* dolphinView) :
-    KHBox(dolphinView),
+                           QWidget* parent) :
+    KHBox(parent),
+    m_active(true),
     m_historyIndex(0),
-    m_dolphinView(dolphinView),
     m_protocols(0),
     m_protocolSeparator(0),
     m_host(0)
@@ -105,8 +104,8 @@ UrlNavigator::UrlNavigator(const KUrl& url,
     }
 
     m_bookmarkSelector = new BookmarkSelector(this);
-    connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(int)),
-            this, SLOT(slotBookmarkActivated(int)));
+    connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(const KUrl&)),
+            this, SLOT(setUrl(const KUrl&)));
 
     m_pathBox = new KUrlComboBox(KUrlComboBox::Directories, true, this);
 
@@ -119,12 +118,8 @@ UrlNavigator::UrlNavigator(const KUrl& url,
     connect(m_pathBox, SIGNAL(urlActivated(const KUrl&)),
             this, SLOT(slotUrlActivated(const KUrl&)));
 
-    connect(dolphinView, SIGNAL(contentsMoved(int, int)),
-            this, SLOT(slotContentsMoved(int, int)));
-    connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
-            this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
-/*    connect(dolphinView, SIGNAL(redirection(const KUrl&)),
-            this, SLOT(slotRedirection(const KUrl&)));*/
+    //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
+    //        this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
     updateContent();
 }
 
@@ -132,65 +127,6 @@ UrlNavigator::~UrlNavigator()
 {
 }
 
-void UrlNavigator::setUrl(const KUrl& url)
-{
-    QString urlStr(url.pathOrUrl());
-    //kDebug() << "setUrl(" << url << ")" << endl;
-    if (urlStr.at(0) == '~') {
-        // replace '~' by the home directory
-        urlStr.remove(0, 1);
-        urlStr.insert(0, QDir::home().path());
-    }
-
-    const KUrl transformedUrl(urlStr);
-
-    if (m_historyIndex > 0) {
-        // Check whether the previous element of the history has the same Url.
-        // If yes, just go forward instead of inserting a duplicate history
-        // element.
-        const KUrl& nextUrl = m_history[m_historyIndex - 1].url();
-        if (transformedUrl == nextUrl) {
-            goForward();
-//             kDebug() << "goin' forward in history" << endl;
-            return;
-        }
-    }
-
-    const KUrl& currUrl = m_history[m_historyIndex].url();
-    if (currUrl == transformedUrl) {
-        // don't insert duplicate history elements
-//         kDebug() << "currUrl == transformedUrl" << endl;
-        return;
-    }
-
-    updateHistoryElem();
-
-    const Q3ValueListIterator<UrlNavigator::HistoryElem> it = m_history.at(m_historyIndex);
-    m_history.insert(it, HistoryElem(transformedUrl));
-
-    updateContent();
-
-    emit urlChanged(transformedUrl);
-    emit historyChanged();
-
-    // Prevent an endless growing of the history: remembering
-    // the last 100 Urls should be enough...
-    if (m_historyIndex > 100) {
-        m_history.erase(m_history.begin());
-        --m_historyIndex;
-    }
-
-/*    kDebug() << "history starting ====================" << endl;
-    int i = 0;
-    for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
-         it != m_history.end();
-         ++it, ++i)
-    {
-        kDebug() << i << ": " << (*it).url() << endl;
-    }
-    kDebug() << "history done ========================" << endl;*/
-}
-
 const KUrl& UrlNavigator::url() const
 {
     assert(!m_history.empty());
@@ -203,7 +139,7 @@ KUrl UrlNavigator::url(int index) const
     QString path(url().pathOrUrl());
     path = path.section('/', 0, index);
 
-    if (path.at(path.length()) != '/')
+    if ( path.length() >= 1 && path.at(path.length()-1) != '/')
     {
         path.append('/');
     }
@@ -273,9 +209,95 @@ void UrlNavigator::editUrl(bool editOrBrowse)
     }
 }
 
-DolphinView* UrlNavigator::dolphinView() const
+void UrlNavigator::setActive(bool active)
+{
+    if (active != m_active) {
+        m_active = active;
+        update();
+        if (active) {
+            emit activated();
+        }
+    }
+}
+
+void UrlNavigator::dropUrls(const KUrl::List& urls,
+                            const KUrl& destination)
+{
+    kDebug() << "------------------- URLS dropped" << endl;
+    emit urlsDropped(urls, destination);
+}
+
+void UrlNavigator::setUrl(const KUrl& url)
+{
+    QString urlStr(url.pathOrUrl());
+    //kDebug() << "setUrl(" << url << ")" << endl;
+    if ( urlStr.length() > 0 && urlStr.at(0) == '~') {
+        // replace '~' by the home directory
+        urlStr.remove(0, 1);
+        urlStr.insert(0, QDir::home().path());
+    }
+
+    const KUrl transformedUrl(urlStr);
+
+    if (m_historyIndex > 0) {
+        // Check whether the previous element of the history has the same Url.
+        // If yes, just go forward instead of inserting a duplicate history
+        // element.
+        const KUrl& nextUrl = m_history[m_historyIndex - 1].url();
+        if (transformedUrl == nextUrl) {
+            goForward();
+//             kDebug() << "goin' forward in history" << endl;
+            return;
+        }
+    }
+
+    const KUrl& currUrl = m_history[m_historyIndex].url();
+    if (currUrl == transformedUrl) {
+        // don't insert duplicate history elements
+//         kDebug() << "currUrl == transformedUrl" << endl;
+        return;
+    }
+
+    updateHistoryElem();
+
+    const Q3ValueListIterator<UrlNavigator::HistoryElem> it = m_history.at(m_historyIndex);
+    m_history.insert(it, HistoryElem(transformedUrl));
+
+    updateContent();
+
+    emit urlChanged(transformedUrl);
+    emit historyChanged();
+
+    // Prevent an endless growing of the history: remembering
+    // the last 100 Urls should be enough...
+    if (m_historyIndex > 100) {
+        m_history.erase(m_history.begin());
+        --m_historyIndex;
+    }
+
+/*    kDebug() << "history starting ====================" << endl;
+    int i = 0;
+    for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
+         it != m_history.end();
+         ++it, ++i)
+    {
+        kDebug() << i << ": " << (*it).url() << endl;
+    }
+    kDebug() << "history done ========================" << endl;*/
+
+    requestActivation();
+}
+
+void UrlNavigator::requestActivation()
+{
+    kDebug() << "--------------------------- request activation" << endl;
+    setActive(true);
+}
+
+void UrlNavigator::storeContentsPosition(int x, int y)
 {
-    return m_dolphinView;
+    m_history[m_historyIndex].setContentsX(x);
+    m_history[m_historyIndex].setContentsY(y);
 }
 
 void UrlNavigator::keyReleaseEvent(QKeyEvent* event)
@@ -301,7 +323,7 @@ void UrlNavigator::slotReturnPressed(const QString& text)
     }
 
     QStringList urls = m_pathBox->urls();
-    urls.remove(typedUrl.url());
+    urls.removeAll(typedUrl.url());
     urls.prepend(typedUrl.url());
     m_pathBox->setUrls(urls, KUrlComboBox::RemoveBottom);
 
@@ -323,7 +345,7 @@ void UrlNavigator::slotRemoteHostActivated()
     QString host = m_host->text();
     QString user;
 
-    int marker = host.find("@");
+    int marker = host.indexOf("@");
     if (marker != -1)
     {
         user = host.left(marker);
@@ -331,7 +353,7 @@ void UrlNavigator::slotRemoteHostActivated()
         host = host.right(host.length() - marker - 1);
     }
 
-    marker = host.find("/");
+    marker = host.indexOf("/");
     if (marker != -1)
     {
         u.setPath(host.right(host.length() - marker));
@@ -399,20 +421,6 @@ void UrlNavigator::slotProtocolChanged(const QString& protocol)
     }
 }
 
-void UrlNavigator::slotRequestActivation()
-{
-    m_dolphinView->requestActivation();
-}
-
-void UrlNavigator::slotBookmarkActivated(int index)
-{
-    m_dolphinView->statusBar()->clear();
-    m_dolphinView->requestActivation();
-
-    KBookmark bookmark = DolphinSettings::instance().bookmark(index);
-    m_dolphinView->setUrl(bookmark.url());
-}
-
 void UrlNavigator::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
 {
 // kDebug() << "received redirection to " << newUrl << endl;
@@ -426,12 +434,6 @@ kDebug() << "received redirection from " << oldUrl << " to " << newUrl << endl;
     m_urls.append(newUrl);*/
 }
 
-void UrlNavigator::slotContentsMoved(int x, int y)
-{
-    m_history[m_historyIndex].setContentsX(x);
-    m_history[m_historyIndex].setContentsY(y);
-}
-
 void UrlNavigator::slotClicked()
 {
     if (isUrlEditable()) {
@@ -440,19 +442,17 @@ void UrlNavigator::slotClicked()
     }
     else {
         setUrl(m_pathBox->currentText());
-        m_dolphinView->setFocus();
+        emit requestActivation();
     }
 }
 
 void UrlNavigator::updateHistoryElem()
 {
     assert(m_historyIndex >= 0);
-    const KFileItem* item = m_dolphinView->currentFileItem();
+    const KFileItem* item = 0; // TODO: m_dolphinView->currentFileItem();
     if (item != 0) {
         m_history[m_historyIndex].setCurrentFileName(item->name());
     }
-    m_history[m_historyIndex].setContentsX(m_dolphinView->contentsX());
-    m_history[m_historyIndex].setContentsY(m_dolphinView->contentsY());
 }
 
 void UrlNavigator::updateContent()
@@ -468,24 +468,28 @@ void UrlNavigator::updateContent()
 
     m_bookmarkSelector->updateSelection(url());
 
-    QToolTip::remove(m_toggleButton);
+    m_toggleButton->setToolTip(QString());
     QString path(url().pathOrUrl());
-    const KAction* action = Dolphin::mainWin().actionCollection()->action("editable_location");
+
+    // TODO: prevent accessing the DolphinMainWindow out from this scope
+    //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
     // TODO: registry of default shortcuts
-    QString shortcut = action? action->shortcutText() : "Ctrl+L";
+    //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
+    const QString shortcut = "Ctrl+L";
+
     if (m_toggleButton->isChecked()) {
         delete m_protocols; m_protocols = 0;
         delete m_protocolSeparator; m_protocolSeparator = 0;
         delete m_host; m_host = 0;
 
-        QToolTip::add(m_toggleButton, i18n("Browse (%1, Escape)",shortcut));
+        m_toggleButton->setToolTip(i18n("Browse (%1, Escape)", shortcut));
 
         setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
         m_pathBox->show();
         m_pathBox->setUrl(url());
     }
     else {
-        QToolTip::add(m_toggleButton, i18n("Edit location (%1)",shortcut));
+        m_toggleButton->setToolTip(i18n("Edit location (%1)", shortcut));
 
         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
         m_pathBox->hide();
@@ -502,8 +506,8 @@ void UrlNavigator::updateContent()
             // path. E. g. "fish://root@192.168.0.2/var/lib" writes
             // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
             // navigation indication 'Custom Path > var > lib".
-            int idx = path.find(QString("//"));
-            idx = path.find("/", (idx < 0) ? 0 : idx + 2);
+            int idx = path.indexOf(QString("//"));
+            idx = path.indexOf("/", (idx < 0) ? 0 : idx + 2);
             bookmarkPath = (idx < 0) ? path : path.left(idx);
         }
         else {