]>
cloud.milkyroute.net Git - dolphin.git/blob - src/urlnavigator.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (<peter.penz@gmx.at>) *
3 * Copyright (C) 2006 by Aaron J. Seigo (<aseigo@kde.org>) *
4 * Copyright (C) 2006 by Patrice Tremblay *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
22 #include "urlnavigator.h"
24 #include "bookmarkselector.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "protocolcombo.h"
28 #include "urlnavigatorbutton.h"
32 #include <kfileitem.h>
35 #include <kprotocolinfo.h>
36 #include <kurlcombobox.h>
37 #include <kurlcompletion.h>
39 #include <QApplication>
42 #include <QHBoxLayout>
45 #include <QLinkedList>
46 #include <QMouseEvent>
47 #include <QToolButton>
49 UrlNavigator::HistoryElem::HistoryElem() :
57 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
65 UrlNavigator::HistoryElem::~HistoryElem()
69 class UrlNavigator::Private
72 Private(UrlNavigator
* q
, KBookmarkManager
* bookmarkManager
);
74 void slotReturnPressed(const QString
&);
75 void slotRemoteHostActivated();
76 void slotProtocolChanged(const QString
&);
79 * Appends the widget at the end of the URL navigator. It is assured
80 * that the filler widget remains as last widget to fill the remaining
83 void appendWidget(QWidget
* widget
);
86 * Switches the navigation bar between the breadcrumb view and the
87 * traditional view (see setUrlEditable()) and is connected to the clicked signal
88 * of the navigation bar button.
93 * Allows to edit the Url of the navigation bar if \a editable
94 * is true. If \a editable is false, each part of
95 * the Url is presented by a button for a fast navigation.
97 void setUrlEditable(bool editable
);
100 * Updates the history element with the current file item
101 * and the contents position.
103 void updateHistoryElem();
104 void updateContent();
107 * Updates all buttons to have one button for each part of the
108 * path \a path. Existing buttons, which are available by m_navButtons,
109 * are reused if possible. If the path is longer, new buttons will be
110 * created, if the path is shorter, the remaining buttons will be deleted.
111 * @param startIndex Start index of path part (/), where the buttons
112 * should be created for each following part.
114 void updateButtons(const QString
& path
, int startIndex
);
117 * Deletes all URL navigator buttons. m_navButtons is
118 * empty after this operation.
120 void deleteButtons();
124 bool m_showHiddenFiles
;
127 QHBoxLayout
* m_layout
;
129 QList
<HistoryElem
> m_history
;
130 QToolButton
* m_toggleButton
;
131 BookmarkSelector
* m_bookmarkSelector
;
132 KUrlComboBox
* m_pathBox
;
133 ProtocolCombo
* m_protocols
;
134 QLabel
* m_protocolSeparator
;
136 QLinkedList
<UrlNavigatorButton
*> m_navButtons
;
142 UrlNavigator::Private::Private(UrlNavigator
* q
, KBookmarkManager
* bookmarkManager
)
145 m_showHiddenFiles(false),
147 m_layout(new QHBoxLayout
),
149 m_protocolSeparator(0),
154 m_layout
->setSpacing(0);
155 m_layout
->setMargin(0);
157 // initialize toggle button which switches between the breadcrumb view
158 // and the traditional view
159 m_toggleButton
= new QToolButton();
160 m_toggleButton
->setCheckable(true);
161 m_toggleButton
->setAutoRaise(true);
162 m_toggleButton
->setIcon(KIcon("editinput")); // TODO: is just a placeholder icon (?)
163 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
164 m_toggleButton
->setMinimumHeight(q
->minimumHeight());
165 connect(m_toggleButton
, SIGNAL(clicked()),
166 q
, SLOT(switchView()));
168 // initialize the bookmark selector
169 m_bookmarkSelector
= new BookmarkSelector(q
, bookmarkManager
);
170 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
171 q
, SLOT(setUrl(const KUrl
&)));
173 // initialize the path box of the traditional view
174 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, q
);
176 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
177 m_pathBox
->setCompletionObject(kurlCompletion
);
178 m_pathBox
->setAutoDeleteCompletionObject(true);
180 connect(m_pathBox
, SIGNAL(returnPressed(QString
)),
181 q
, SLOT(slotReturnPressed(QString
)));
182 connect(m_pathBox
, SIGNAL(urlActivated(KUrl
)),
183 q
, SLOT(setUrl(KUrl
)));
185 // Append a filler widget at the end, which automatically resizes to the
186 // maximum available width. This assures that the URL navigator uses the
187 // whole width, so that the clipboard content can be dropped.
188 m_filler
= new QWidget();
189 m_filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
191 m_layout
->addWidget(m_toggleButton
);
192 m_layout
->addWidget(m_bookmarkSelector
);
193 m_layout
->addWidget(m_pathBox
);
194 m_layout
->addWidget(m_filler
);
197 void UrlNavigator::Private::appendWidget(QWidget
* widget
)
199 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
202 void UrlNavigator::Private::setUrlEditable(bool editable
)
204 if (q
->isUrlEditable() != editable
) {
205 m_toggleButton
->toggle();
210 void UrlNavigator::Private::slotReturnPressed(const QString
& text
)
212 // Parts of the following code have been taken
213 // from the class KateFileSelector located in
214 // kate/app/katefileselector.hpp of Kate.
215 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
216 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
217 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
220 if (typedUrl
.hasPass()) {
221 typedUrl
.setPass(QString());
224 QStringList urls
= m_pathBox
->urls();
225 urls
.removeAll(typedUrl
.url());
226 urls
.prepend(typedUrl
.url());
227 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
230 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
231 // synchronize the result in the path box.
232 m_pathBox
->setUrl(q
->url());
235 void UrlNavigator::Private::slotRemoteHostActivated()
239 QString host
= m_host
->text();
242 int marker
= host
.indexOf("@");
245 user
= host
.left(marker
);
247 host
= host
.right(host
.length() - marker
- 1);
250 marker
= host
.indexOf("/");
253 u
.setPath(host
.right(host
.length() - marker
));
254 host
.truncate(marker
);
261 if (m_protocols
->currentProtocol() != u
.protocol() ||
265 u
.setProtocol(m_protocols
->currentProtocol());
266 u
.setHost(m_host
->text());
268 //TODO: get rid of this HACK for file:///!
269 if (u
.protocol() == "file")
272 if (u
.path().isEmpty())
282 void UrlNavigator::Private::slotProtocolChanged(const QString
& protocol
)
285 url
.setProtocol(protocol
);
286 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
288 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
289 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
290 while (it
!= itEnd
) {
292 (*it
)->deleteLater();
295 m_navButtons
.clear();
297 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
302 m_protocolSeparator
= new QLabel("://", q
);
303 appendWidget(m_protocolSeparator
);
304 m_host
= new QLineEdit(q
);
305 appendWidget(m_host
);
307 connect(m_host
, SIGNAL(lostFocus()),
308 q
, SLOT(slotRemoteHostActivated()));
309 connect(m_host
, SIGNAL(returnPressed()),
310 q
, SLOT(slotRemoteHostActivated()));
315 m_protocolSeparator
->show();
322 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
324 // kDebug() << "received redirection to " << newUrl << endl;
325 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
326 /* UrlStack::iterator it = m_urls.find(oldUrl);
327 if (it != m_urls.end())
329 m_urls.erase(++it, m_urls.end());
332 m_urls.append(newUrl);*/
336 void UrlNavigator::Private::switchView()
339 if (q
->isUrlEditable()) {
340 m_pathBox
->setFocus();
342 q
->setUrl(m_pathBox
->currentText());
344 emit q
->requestActivation();
347 void UrlNavigator::Private::updateHistoryElem()
349 assert(m_historyIndex
>= 0);
350 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
352 HistoryElem
& hist
= m_history
[m_historyIndex
];
353 hist
.setCurrentFileName(item
->name());
357 void UrlNavigator::Private::updateContent()
359 m_bookmarkSelector
->updateSelection(q
->url());
361 m_toggleButton
->setToolTip(QString());
362 QString
path(q
->url().pathOrUrl());
364 // TODO: prevent accessing the DolphinMainWindow out from this scope
365 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
366 // TODO: registry of default shortcuts
367 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
368 const QString shortcut
= "Ctrl+L";
370 if (m_toggleButton
->isChecked()) {
371 delete m_protocols
; m_protocols
= 0;
372 delete m_protocolSeparator
; m_protocolSeparator
= 0;
373 delete m_host
; m_host
= 0;
377 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
379 q
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
381 m_pathBox
->setUrl(q
->url());
384 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
386 q
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
390 // get the data from the currently selected bookmark
391 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
393 QString bookmarkPath
;
394 if (bookmark
.isNull()) {
395 // No bookmark is a part of the current Url.
396 // The following code tries to guess the bookmark
397 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
398 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
399 // navigation indication 'Custom Path > var > lib".
400 int idx
= path
.indexOf(QString("//"));
401 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
402 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
405 bookmarkPath
= bookmark
.url().pathOrUrl();
407 const uint len
= bookmarkPath
.length();
409 // calculate the start point for the URL navigator buttons by counting
410 // the slashs inside the bookmark URL
412 for (uint i
= 0; i
< len
; ++i
) {
413 if (bookmarkPath
.at(i
) == QChar('/')) {
417 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
418 assert(slashCount
> 0);
422 const KUrl currentUrl
= q
->url();
423 if (!currentUrl
.isLocalFile() && bookmark
.isNull()) {
424 QString protocol
= currentUrl
.protocol();
427 m_protocols
= new ProtocolCombo(protocol
, q
);
428 appendWidget(m_protocols
);
429 connect(m_protocols
, SIGNAL(activated(QString
)),
430 q
, SLOT(slotProtocolChanged(QString
)));
433 m_protocols
->setProtocol(protocol
);
437 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
438 QString hostText
= currentUrl
.host();
440 if (!currentUrl
.user().isEmpty()) {
441 hostText
= currentUrl
.user() + '@' + hostText
;
445 // ######### TODO: this code is duplicated from slotProtocolChanged!
446 m_protocolSeparator
= new QLabel("://", q
);
447 appendWidget(m_protocolSeparator
);
448 m_host
= new QLineEdit(hostText
, q
);
449 appendWidget(m_host
);
451 connect(m_host
, SIGNAL(lostFocus()),
452 q
, SLOT(slotRemoteHostActivated()));
453 connect(m_host
, SIGNAL(returnPressed()),
454 q
, SLOT(slotRemoteHostActivated()));
457 m_host
->setText(hostText
);
459 m_protocolSeparator
->show();
463 delete m_protocolSeparator
; m_protocolSeparator
= 0;
464 delete m_host
; m_host
= 0;
467 else if (m_protocols
) {
471 m_protocolSeparator
->hide();
476 updateButtons(path
, slashCount
);
480 void UrlNavigator::Private::updateButtons(const QString
& path
, int startIndex
)
482 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
483 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
484 bool createButton
= false;
485 const KUrl currentUrl
= q
->url();
487 int idx
= startIndex
;
490 createButton
= (it
== itEnd
);
492 const QString dirName
= path
.section('/', idx
, idx
);
493 const bool isFirstButton
= (idx
== startIndex
);
494 hasNext
= isFirstButton
|| !dirName
.isEmpty();
498 // the first URL navigator button should get the name of the
499 // bookmark instead of the directory name
500 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
501 text
= bookmark
.text();
502 if (text
.isEmpty()) {
503 if (currentUrl
.isLocalFile()) {
504 text
= i18n("Custom Path");
513 UrlNavigatorButton
* button
= 0;
515 button
= new UrlNavigatorButton(idx
, q
);
516 appendWidget(button
);
520 button
->setIndex(idx
);
524 button
->setText(text
);
529 m_navButtons
.append(button
);
538 // delete buttons which are not used anymore
539 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
540 while (it
!= itEnd
) {
542 (*it
)->deleteLater();
545 m_navButtons
.erase(itBegin
, m_navButtons
.end());
548 void UrlNavigator::Private::deleteButtons()
550 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
551 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
552 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
553 while (it
!= itEnd
) {
555 (*it
)->deleteLater();
558 m_navButtons
.erase(itBegin
, itEnd
);
564 UrlNavigator::UrlNavigator(KBookmarkManager
* bookmarkManager
,
568 d( new Private(this, bookmarkManager
) )
570 d
->m_history
.prepend(HistoryElem(url
));
572 QFontMetrics
fontMetrics(font());
573 setMinimumHeight(fontMetrics
.height() + 10);
575 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
576 d
->m_toggleButton
->toggle();
579 setLayout(d
->m_layout
);
584 UrlNavigator::~UrlNavigator()
589 const KUrl
& UrlNavigator::url() const
591 assert(!d
->m_history
.empty());
592 return d
->m_history
[d
->m_historyIndex
].url();
595 KUrl
UrlNavigator::url(int index
) const
598 // keep scheme, hostname etc. maybe we will need this in the future
599 // for e.g. browsing ftp repositories.
601 newurl
.setPath(QString());
602 QString
path(url().path());
604 if (!path
.isEmpty()) {
605 if (index
== 0) //prevent the last "/" from being stripped
606 path
= "/"; //or we end up with an empty path
608 path
= path
.section('/', 0, index
);
611 newurl
.setPath(path
);
615 UrlNavigator::HistoryElem
UrlNavigator::currentHistoryItem() const
617 return d
->m_history
[d
->m_historyIndex
];
620 int UrlNavigator::historySize() const
622 return d
->m_history
.count();
625 void UrlNavigator::goBack()
627 d
->updateHistoryElem();
629 const int count
= d
->m_history
.count();
630 if (d
->m_historyIndex
< count
- 1) {
633 emit
urlChanged(url());
634 emit
historyChanged();
638 void UrlNavigator::goForward()
640 if (d
->m_historyIndex
> 0) {
643 emit
urlChanged(url());
644 emit
historyChanged();
648 void UrlNavigator::goUp()
650 setUrl(url().upUrl());
653 void UrlNavigator::goHome()
655 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
658 bool UrlNavigator::isUrlEditable() const
660 return d
->m_toggleButton
->isChecked();
663 void UrlNavigator::editUrl(bool editOrBrowse
)
665 d
->setUrlEditable(editOrBrowse
);
667 d
->m_pathBox
->setFocus();
671 void UrlNavigator::setActive(bool active
)
673 if (active
!= d
->m_active
) {
674 d
->m_active
= active
;
682 void UrlNavigator::setShowHiddenFiles( bool show
)
684 d
->m_showHiddenFiles
= show
;
687 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
688 const KUrl
& destination
)
690 emit
urlsDropped(urls
, destination
);
693 void UrlNavigator::setUrl(const KUrl
& url
)
695 QString
urlStr(url
.pathOrUrl());
697 // TODO: a patch has been submitted by Filip Brcic which adjusts
698 // the URL for tar and zip files. See https://bugs.kde.org/show_bug.cgi?id=142781
699 // for details. The URL navigator part of the patch has not been committed yet,
700 // as the URL navigator will be subject of change and
701 // we might think of a more generic approach to check the protocol + MIME type for
704 //kDebug() << "setUrl(" << url << ")" << endl;
705 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
706 // replace '~' by the home directory
708 urlStr
.insert(0, QDir::home().path());
711 const KUrl
transformedUrl(urlStr
);
713 if (d
->m_historyIndex
> 0) {
714 // Check whether the previous element of the history has the same Url.
715 // If yes, just go forward instead of inserting a duplicate history
717 HistoryElem
& prevHistoryElem
= d
->m_history
[d
->m_historyIndex
- 1];
718 if (transformedUrl
== prevHistoryElem
.url()) {
720 // kDebug() << "goin' forward in history" << endl;
725 if (this->url() == transformedUrl
) {
726 // don't insert duplicate history elements
727 // kDebug() << "current url == transformedUrl" << endl;
731 d
->updateHistoryElem();
732 d
->m_history
.insert(d
->m_historyIndex
, HistoryElem(transformedUrl
));
736 emit
urlChanged(transformedUrl
);
737 emit
historyChanged();
739 // Prevent an endless growing of the history: remembering
740 // the last 100 Urls should be enough...
741 if (d
->m_historyIndex
> 100) {
742 d
->m_history
.removeFirst();
746 /* kDebug() << "history starting ====================" << endl;
748 for (QValueListIterator<UrlNavigator::HistoryElem> it = d->m_history.begin();
749 it != d->m_history.end();
752 kDebug() << i << ": " << (*it).url() << endl;
754 kDebug() << "history done ========================" << endl;*/
759 void UrlNavigator::requestActivation()
764 void UrlNavigator::storeContentsPosition(int x
, int y
)
766 HistoryElem
& hist
= d
->m_history
[d
->m_historyIndex
];
767 hist
.setContentsX(x
);
768 hist
.setContentsY(y
);
771 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
773 QWidget::keyReleaseEvent(event
);
774 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
775 d
->setUrlEditable(false);
779 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
781 if (event
->button() == Qt::MidButton
) {
782 QClipboard
* clipboard
= QApplication::clipboard();
783 const QMimeData
* mimeData
= clipboard
->mimeData();
784 if (mimeData
->hasText()) {
785 const QString text
= mimeData
->text();
789 QWidget::mouseReleaseEvent(event
);
792 bool UrlNavigator::isActive() const
797 bool UrlNavigator::showHiddenFiles() const
799 return d
->m_showHiddenFiles
;
802 #include "urlnavigator.moc"