]>
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 "protocolcombo.h"
26 #include "urlnavigatorbutton.h"
30 #include <kfileitem.h>
33 #include <kprotocolinfo.h>
34 #include <kurlcombobox.h>
35 #include <kurlcompletion.h>
37 #include <QApplication>
40 #include <QHBoxLayout>
43 #include <QLinkedList>
44 #include <QMouseEvent>
45 #include <QToolButton>
47 UrlNavigator::HistoryElem::HistoryElem() :
55 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
63 UrlNavigator::HistoryElem::~HistoryElem()
67 class UrlNavigator::Private
70 Private(UrlNavigator
* q
, KBookmarkManager
* bookmarkManager
);
72 void slotReturnPressed(const QString
&);
73 void slotRemoteHostActivated();
74 void slotProtocolChanged(const QString
&);
77 * Appends the widget at the end of the URL navigator. It is assured
78 * that the filler widget remains as last widget to fill the remaining
81 void appendWidget(QWidget
* widget
);
84 * Switches the navigation bar between the breadcrumb view and the
85 * traditional view (see setUrlEditable()) and is connected to the clicked signal
86 * of the navigation bar button.
91 * Updates the history element with the current file item
92 * and the contents position.
94 void updateHistoryElem();
98 * Updates all buttons to have one button for each part of the
99 * path \a path. Existing buttons, which are available by m_navButtons,
100 * are reused if possible. If the path is longer, new buttons will be
101 * created, if the path is shorter, the remaining buttons will be deleted.
102 * @param startIndex Start index of path part (/), where the buttons
103 * should be created for each following part.
105 void updateButtons(const QString
& path
, int startIndex
);
108 * Deletes all URL navigator buttons. m_navButtons is
109 * empty after this operation.
111 void deleteButtons();
115 bool m_showHiddenFiles
;
118 QHBoxLayout
* m_layout
;
120 QList
<HistoryElem
> m_history
;
121 QToolButton
* m_toggleButton
;
122 BookmarkSelector
* m_bookmarkSelector
;
123 KUrlComboBox
* m_pathBox
;
124 ProtocolCombo
* m_protocols
;
125 QLabel
* m_protocolSeparator
;
127 QLinkedList
<UrlNavigatorButton
*> m_navButtons
;
134 UrlNavigator::Private::Private(UrlNavigator
* q
, KBookmarkManager
* bookmarkManager
)
137 m_showHiddenFiles(false),
139 m_layout(new QHBoxLayout
),
141 m_protocolSeparator(0),
146 m_layout
->setSpacing(0);
147 m_layout
->setMargin(0);
149 // initialize toggle button which switches between the breadcrumb view
150 // and the traditional view
151 m_toggleButton
= new QToolButton();
152 m_toggleButton
->setCheckable(true);
153 m_toggleButton
->setAutoRaise(true);
154 m_toggleButton
->setIcon(KIcon("editinput")); // TODO: is just a placeholder icon (?)
155 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
156 m_toggleButton
->setMinimumHeight(q
->minimumHeight());
157 connect(m_toggleButton
, SIGNAL(clicked()),
158 q
, SLOT(switchView()));
160 // initialize the bookmark selector
161 m_bookmarkSelector
= new BookmarkSelector(q
, bookmarkManager
);
162 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
163 q
, SLOT(setUrl(const KUrl
&)));
165 // initialize the path box of the traditional view
166 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, q
);
168 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
169 m_pathBox
->setCompletionObject(kurlCompletion
);
170 m_pathBox
->setAutoDeleteCompletionObject(true);
172 connect(m_pathBox
, SIGNAL(returnPressed(QString
)),
173 q
, SLOT(slotReturnPressed(QString
)));
174 connect(m_pathBox
, SIGNAL(urlActivated(KUrl
)),
175 q
, SLOT(setUrl(KUrl
)));
177 // Append a filler widget at the end, which automatically resizes to the
178 // maximum available width. This assures that the URL navigator uses the
179 // whole width, so that the clipboard content can be dropped.
180 m_filler
= new QWidget();
181 m_filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
183 m_layout
->addWidget(m_toggleButton
);
184 m_layout
->addWidget(m_bookmarkSelector
);
185 m_layout
->addWidget(m_pathBox
);
186 m_layout
->addWidget(m_filler
);
189 void UrlNavigator::Private::appendWidget(QWidget
* widget
)
191 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
194 void UrlNavigator::Private::slotReturnPressed(const QString
& text
)
196 // Parts of the following code have been taken
197 // from the class KateFileSelector located in
198 // kate/app/katefileselector.hpp of Kate.
199 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
200 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
201 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
204 if (typedUrl
.hasPass()) {
205 typedUrl
.setPass(QString());
208 QStringList urls
= m_pathBox
->urls();
209 urls
.removeAll(typedUrl
.url());
210 urls
.prepend(typedUrl
.url());
211 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
214 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
215 // synchronize the result in the path box.
216 m_pathBox
->setUrl(q
->url());
219 void UrlNavigator::Private::slotRemoteHostActivated()
223 QString host
= m_host
->text();
226 int marker
= host
.indexOf("@");
229 user
= host
.left(marker
);
231 host
= host
.right(host
.length() - marker
- 1);
234 marker
= host
.indexOf("/");
237 u
.setPath(host
.right(host
.length() - marker
));
238 host
.truncate(marker
);
245 if (m_protocols
->currentProtocol() != u
.protocol() ||
249 u
.setProtocol(m_protocols
->currentProtocol());
250 u
.setHost(m_host
->text());
252 //TODO: get rid of this HACK for file:///!
253 if (u
.protocol() == "file")
256 if (u
.path().isEmpty())
266 void UrlNavigator::Private::slotProtocolChanged(const QString
& protocol
)
269 url
.setProtocol(protocol
);
270 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
272 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
273 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
274 while (it
!= itEnd
) {
276 (*it
)->deleteLater();
279 m_navButtons
.clear();
281 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
286 m_protocolSeparator
= new QLabel("://", q
);
287 appendWidget(m_protocolSeparator
);
288 m_host
= new QLineEdit(q
);
289 appendWidget(m_host
);
291 connect(m_host
, SIGNAL(lostFocus()),
292 q
, SLOT(slotRemoteHostActivated()));
293 connect(m_host
, SIGNAL(returnPressed()),
294 q
, SLOT(slotRemoteHostActivated()));
299 m_protocolSeparator
->show();
306 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
308 // kDebug() << "received redirection to " << newUrl << endl;
309 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
310 /* UrlStack::iterator it = m_urls.find(oldUrl);
311 if (it != m_urls.end())
313 m_urls.erase(++it, m_urls.end());
316 m_urls.append(newUrl);*/
320 void UrlNavigator::Private::switchView()
323 if (q
->isUrlEditable()) {
324 m_pathBox
->setFocus();
326 q
->setUrl(m_pathBox
->currentText());
328 emit q
->requestActivation();
331 void UrlNavigator::Private::updateHistoryElem()
333 assert(m_historyIndex
>= 0);
334 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
336 HistoryElem
& hist
= m_history
[m_historyIndex
];
337 hist
.setCurrentFileName(item
->name());
341 void UrlNavigator::Private::updateContent()
343 m_bookmarkSelector
->updateSelection(q
->url());
345 m_toggleButton
->setToolTip(QString());
346 QString
path(q
->url().pathOrUrl());
348 // TODO: prevent accessing the DolphinMainWindow out from this scope
349 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
350 // TODO: registry of default shortcuts
351 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
352 const QString shortcut
= "Ctrl+L";
354 if (m_toggleButton
->isChecked()) {
355 delete m_protocols
; m_protocols
= 0;
356 delete m_protocolSeparator
; m_protocolSeparator
= 0;
357 delete m_host
; m_host
= 0;
361 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
363 q
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
365 m_pathBox
->setUrl(q
->url());
368 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
370 q
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
374 // get the data from the currently selected bookmark
375 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
377 QString bookmarkPath
;
378 if (bookmark
.isNull()) {
379 // No bookmark is a part of the current Url.
380 // The following code tries to guess the bookmark
381 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
382 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
383 // navigation indication 'Custom Path > var > lib".
384 int idx
= path
.indexOf(QString("//"));
385 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
386 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
389 bookmarkPath
= bookmark
.url().pathOrUrl();
391 const uint len
= bookmarkPath
.length();
393 // calculate the start point for the URL navigator buttons by counting
394 // the slashs inside the bookmark URL
396 for (uint i
= 0; i
< len
; ++i
) {
397 if (bookmarkPath
.at(i
) == QChar('/')) {
401 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
402 assert(slashCount
> 0);
406 const KUrl currentUrl
= q
->url();
407 if (!currentUrl
.isLocalFile() && bookmark
.isNull()) {
408 QString protocol
= currentUrl
.protocol();
411 m_protocols
= new ProtocolCombo(protocol
, q
);
412 appendWidget(m_protocols
);
413 connect(m_protocols
, SIGNAL(activated(QString
)),
414 q
, SLOT(slotProtocolChanged(QString
)));
417 m_protocols
->setProtocol(protocol
);
421 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
422 QString hostText
= currentUrl
.host();
424 if (!currentUrl
.user().isEmpty()) {
425 hostText
= currentUrl
.user() + '@' + hostText
;
429 // ######### TODO: this code is duplicated from slotProtocolChanged!
430 m_protocolSeparator
= new QLabel("://", q
);
431 appendWidget(m_protocolSeparator
);
432 m_host
= new QLineEdit(hostText
, q
);
433 appendWidget(m_host
);
435 connect(m_host
, SIGNAL(lostFocus()),
436 q
, SLOT(slotRemoteHostActivated()));
437 connect(m_host
, SIGNAL(returnPressed()),
438 q
, SLOT(slotRemoteHostActivated()));
441 m_host
->setText(hostText
);
443 m_protocolSeparator
->show();
447 delete m_protocolSeparator
; m_protocolSeparator
= 0;
448 delete m_host
; m_host
= 0;
451 else if (m_protocols
) {
455 m_protocolSeparator
->hide();
460 updateButtons(path
, slashCount
);
464 void UrlNavigator::Private::updateButtons(const QString
& path
, int startIndex
)
466 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
467 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
468 bool createButton
= false;
469 const KUrl currentUrl
= q
->url();
471 int idx
= startIndex
;
474 createButton
= (it
== itEnd
);
476 const QString dirName
= path
.section('/', idx
, idx
);
477 const bool isFirstButton
= (idx
== startIndex
);
478 hasNext
= isFirstButton
|| !dirName
.isEmpty();
482 // the first URL navigator button should get the name of the
483 // bookmark instead of the directory name
484 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
485 text
= bookmark
.text();
486 if (text
.isEmpty()) {
487 if (currentUrl
.isLocalFile()) {
488 text
= i18n("Custom Path");
497 UrlNavigatorButton
* button
= 0;
499 button
= new UrlNavigatorButton(idx
, q
);
500 appendWidget(button
);
504 button
->setIndex(idx
);
508 button
->setText(text
);
513 m_navButtons
.append(button
);
522 // delete buttons which are not used anymore
523 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
524 while (it
!= itEnd
) {
526 (*it
)->deleteLater();
529 m_navButtons
.erase(itBegin
, m_navButtons
.end());
532 void UrlNavigator::Private::deleteButtons()
534 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
535 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
536 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
537 while (it
!= itEnd
) {
539 (*it
)->deleteLater();
542 m_navButtons
.erase(itBegin
, itEnd
);
548 UrlNavigator::UrlNavigator(KBookmarkManager
* bookmarkManager
,
552 d( new Private(this, bookmarkManager
) )
554 d
->m_history
.prepend(HistoryElem(url
));
556 QFontMetrics
fontMetrics(font());
557 setMinimumHeight(fontMetrics
.height() + 10);
559 setLayout(d
->m_layout
);
564 UrlNavigator::~UrlNavigator()
569 const KUrl
& UrlNavigator::url() const
571 assert(!d
->m_history
.empty());
572 return d
->m_history
[d
->m_historyIndex
].url();
575 KUrl
UrlNavigator::url(int index
) const
578 // keep scheme, hostname etc. maybe we will need this in the future
579 // for e.g. browsing ftp repositories.
581 newurl
.setPath(QString());
582 QString
path(url().path());
584 if (!path
.isEmpty()) {
585 if (index
== 0) //prevent the last "/" from being stripped
586 path
= "/"; //or we end up with an empty path
588 path
= path
.section('/', 0, index
);
591 newurl
.setPath(path
);
595 UrlNavigator::HistoryElem
UrlNavigator::currentHistoryItem() const
597 return d
->m_history
[d
->m_historyIndex
];
600 int UrlNavigator::historySize() const
602 return d
->m_history
.count();
605 void UrlNavigator::goBack()
607 d
->updateHistoryElem();
609 const int count
= d
->m_history
.count();
610 if (d
->m_historyIndex
< count
- 1) {
613 emit
urlChanged(url());
614 emit
historyChanged();
618 void UrlNavigator::goForward()
620 if (d
->m_historyIndex
> 0) {
623 emit
urlChanged(url());
624 emit
historyChanged();
628 void UrlNavigator::goUp()
630 setUrl(url().upUrl());
633 void UrlNavigator::goHome()
635 if (d
->m_homeUrl
.isEmpty())
636 setUrl(QDir::homePath());
638 setUrl(d
->m_homeUrl
);
641 bool UrlNavigator::isUrlEditable() const
643 return d
->m_toggleButton
->isChecked();
646 void UrlNavigator::setUrlEditable(bool editable
)
648 if (isUrlEditable() != editable
) {
649 d
->m_toggleButton
->toggle();
654 void UrlNavigator::setActive(bool active
)
656 if (active
!= d
->m_active
) {
657 d
->m_active
= active
;
665 void UrlNavigator::setShowHiddenFiles( bool show
)
667 d
->m_showHiddenFiles
= show
;
670 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
671 const KUrl
& destination
)
673 emit
urlsDropped(urls
, destination
);
676 void UrlNavigator::setUrl(const KUrl
& url
)
678 QString
urlStr(url
.pathOrUrl());
680 // TODO: a patch has been submitted by Filip Brcic which adjusts
681 // the URL for tar and zip files. See https://bugs.kde.org/show_bug.cgi?id=142781
682 // for details. The URL navigator part of the patch has not been committed yet,
683 // as the URL navigator will be subject of change and
684 // we might think of a more generic approach to check the protocol + MIME type for
687 //kDebug() << "setUrl(" << url << ")" << endl;
688 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
689 // replace '~' by the home directory
691 urlStr
.insert(0, QDir::homePath());
694 const KUrl
transformedUrl(urlStr
);
696 if (d
->m_historyIndex
> 0) {
697 // Check whether the previous element of the history has the same Url.
698 // If yes, just go forward instead of inserting a duplicate history
700 HistoryElem
& prevHistoryElem
= d
->m_history
[d
->m_historyIndex
- 1];
701 if (transformedUrl
== prevHistoryElem
.url()) {
703 // kDebug() << "goin' forward in history" << endl;
708 if (this->url() == transformedUrl
) {
709 // don't insert duplicate history elements
710 // kDebug() << "current url == transformedUrl" << endl;
714 d
->updateHistoryElem();
715 d
->m_history
.insert(d
->m_historyIndex
, HistoryElem(transformedUrl
));
719 emit
urlChanged(transformedUrl
);
720 emit
historyChanged();
722 // Prevent an endless growing of the history: remembering
723 // the last 100 Urls should be enough...
724 if (d
->m_historyIndex
> 100) {
725 d
->m_history
.removeFirst();
729 /* kDebug() << "history starting ====================" << endl;
731 for (QValueListIterator<UrlNavigator::HistoryElem> it = d->m_history.begin();
732 it != d->m_history.end();
735 kDebug() << i << ": " << (*it).url() << endl;
737 kDebug() << "history done ========================" << endl;*/
742 void UrlNavigator::requestActivation()
747 void UrlNavigator::storeContentsPosition(int x
, int y
)
749 HistoryElem
& hist
= d
->m_history
[d
->m_historyIndex
];
750 hist
.setContentsX(x
);
751 hist
.setContentsY(y
);
754 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
756 QWidget::keyReleaseEvent(event
);
757 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
758 setUrlEditable(false);
762 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
764 if (event
->button() == Qt::MidButton
) {
765 QClipboard
* clipboard
= QApplication::clipboard();
766 const QMimeData
* mimeData
= clipboard
->mimeData();
767 if (mimeData
->hasText()) {
768 const QString text
= mimeData
->text();
772 QWidget::mouseReleaseEvent(event
);
775 bool UrlNavigator::isActive() const
780 bool UrlNavigator::showHiddenFiles() const
782 return d
->m_showHiddenFiles
;
785 void UrlNavigator::setHomeUrl(const QString
& homeUrl
)
787 d
->m_homeUrl
= homeUrl
;
790 #include "urlnavigator.moc"