]>
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 "generalsettings.h"
27 #include "protocolcombo.h"
28 #include "urlnavigatorbutton.h"
32 #include <kfileitem.h>
34 #include <kprotocolinfo.h>
35 #include <kurlcombobox.h>
36 #include <kurlcompletion.h>
38 #include <QApplication>
42 #include <QHBoxLayout>
45 #include <QMouseEvent>
47 UrlNavigator::HistoryElem::HistoryElem() :
55 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
63 UrlNavigator::HistoryElem::~HistoryElem()
67 UrlNavigator::UrlNavigator(const KUrl
& url
,
74 m_protocolSeparator(0),
77 m_layout
= new QHBoxLayout();
78 m_layout
->setSpacing(0);
79 m_layout
->setMargin(0);
81 m_history
.prepend(HistoryElem(url
));
83 QFontMetrics
fontMetrics(font());
84 setMinimumHeight(fontMetrics
.height() + 8);
86 m_toggleButton
= new QCheckBox();
87 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
88 m_toggleButton
->setMinimumHeight(minimumHeight());
89 connect(m_toggleButton
, SIGNAL(clicked()),
90 this, SLOT(slotClicked()));
91 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
92 m_toggleButton
->toggle();
95 m_bookmarkSelector
= new BookmarkSelector(this);
96 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
97 this, SLOT(setUrl(const KUrl
&)));
99 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, this);
101 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
102 m_pathBox
->setCompletionObject(kurlCompletion
);
103 m_pathBox
->setAutoDeleteCompletionObject(true);
105 connect(m_pathBox
, SIGNAL(returnPressed(const QString
&)),
106 this, SLOT(slotReturnPressed(const QString
&)));
107 connect(m_pathBox
, SIGNAL(urlActivated(const KUrl
&)),
108 this, SLOT(slotUrlActivated(const KUrl
&)));
110 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
111 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
113 // Append a filler widget at the end, which automatically resizes to the
114 // maximum available width. This assures that the URL navigator uses the
115 // whole width, so that the clipboard content can be dropped.
116 QWidget
* filler
= new QWidget();
117 filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
119 m_layout
->addWidget(m_toggleButton
);
120 m_layout
->addWidget(m_bookmarkSelector
);
121 m_layout
->addWidget(m_pathBox
);
122 m_layout
->addWidget(filler
);
128 UrlNavigator::~UrlNavigator()
132 const KUrl
& UrlNavigator::url() const
134 assert(!m_history
.empty());
135 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
136 it
+= m_historyIndex
;
140 KUrl
UrlNavigator::url(int index
) const
143 QString
path(url().pathOrUrl());
144 path
= path
.section('/', 0, index
);
146 if ( path
.length() >= 1 && path
.at(path
.length()-1) != '/')
154 const QLinkedList
<UrlNavigator::HistoryElem
>& UrlNavigator::history(int& index
) const
156 index
= m_historyIndex
;
160 void UrlNavigator::goBack()
164 const int count
= m_history
.count();
165 if (m_historyIndex
< count
- 1) {
168 emit
urlChanged(url());
169 emit
historyChanged();
173 void UrlNavigator::goForward()
175 if (m_historyIndex
> 0) {
178 emit
urlChanged(url());
179 emit
historyChanged();
183 void UrlNavigator::goUp()
185 setUrl(url().upUrl());
188 void UrlNavigator::goHome()
190 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
193 void UrlNavigator::setUrlEditable(bool editable
)
195 if (isUrlEditable() != editable
) {
196 m_toggleButton
->toggle();
201 bool UrlNavigator::isUrlEditable() const
203 return m_toggleButton
->isChecked();
206 void UrlNavigator::editUrl(bool editOrBrowse
)
208 setUrlEditable(editOrBrowse
);
210 m_pathBox
->setFocus();
214 void UrlNavigator::setActive(bool active
)
216 if (active
!= m_active
) {
225 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
226 const KUrl
& destination
)
228 emit
urlsDropped(urls
, destination
);
231 void UrlNavigator::setUrl(const KUrl
& url
)
233 QString
urlStr(url
.pathOrUrl());
234 //kDebug() << "setUrl(" << url << ")" << endl;
235 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
236 // replace '~' by the home directory
238 urlStr
.insert(0, QDir::home().path());
241 const KUrl
transformedUrl(urlStr
);
243 if (m_historyIndex
> 0) {
244 // Check whether the previous element of the history has the same Url.
245 // If yes, just go forward instead of inserting a duplicate history
247 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
248 it
+= m_historyIndex
- 1;
249 const KUrl
& nextUrl
= (*it
).url();
250 if (transformedUrl
== nextUrl
) {
252 // kDebug() << "goin' forward in history" << endl;
257 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
258 const KUrl
& currUrl
= (*it
).url();
259 if (currUrl
== transformedUrl
) {
260 // don't insert duplicate history elements
261 // kDebug() << "currUrl == transformedUrl" << endl;
266 m_history
.insert(it
, HistoryElem(transformedUrl
));
270 emit
urlChanged(transformedUrl
);
271 emit
historyChanged();
273 // Prevent an endless growing of the history: remembering
274 // the last 100 Urls should be enough...
275 if (m_historyIndex
> 100) {
276 m_history
.erase(m_history
.begin());
280 /* kDebug() << "history starting ====================" << endl;
282 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
283 it != m_history.end();
286 kDebug() << i << ": " << (*it).url() << endl;
288 kDebug() << "history done ========================" << endl;*/
293 void UrlNavigator::requestActivation()
298 void UrlNavigator::storeContentsPosition(int x
, int y
)
300 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
301 (*it
).setContentsX(x
);
302 (*it
).setContentsY(y
);
305 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
307 QWidget::keyReleaseEvent(event
);
308 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
309 setUrlEditable(false);
313 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
315 if (event
->button() == Qt::MidButton
) {
316 QClipboard
* clipboard
= QApplication::clipboard();
317 const QMimeData
* mimeData
= clipboard
->mimeData();
318 if (mimeData
->hasText()) {
319 const QString text
= mimeData
->text();
323 QWidget::mouseReleaseEvent(event
);
326 void UrlNavigator::slotReturnPressed(const QString
& text
)
328 // Parts of the following code have been taken
329 // from the class KateFileSelector located in
330 // kate/app/katefileselector.hpp of Kate.
331 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
332 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
333 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
336 if (typedUrl
.hasPass()) {
337 typedUrl
.setPass(QString::null
);
340 QStringList urls
= m_pathBox
->urls();
341 urls
.removeAll(typedUrl
.url());
342 urls
.prepend(typedUrl
.url());
343 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
346 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
347 // synchronize the result in the path box.
348 m_pathBox
->setUrl(url());
351 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
356 void UrlNavigator::slotRemoteHostActivated()
360 QString host
= m_host
->text();
363 int marker
= host
.indexOf("@");
366 user
= host
.left(marker
);
368 host
= host
.right(host
.length() - marker
- 1);
371 marker
= host
.indexOf("/");
374 u
.setPath(host
.right(host
.length() - marker
));
375 host
.truncate(marker
);
382 if (m_protocols
->currentProtocol() != u
.protocol() ||
386 u
.setProtocol(m_protocols
->currentProtocol());
387 u
.setHost(m_host
->text());
389 //TODO: get rid of this HACK for file:///!
390 if (u
.protocol() == "file")
393 if (u
.path().isEmpty())
403 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
406 url
.setProtocol(protocol
);
407 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
409 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
410 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
411 while (it
!= itEnd
) {
413 (*it
)->deleteLater();
416 m_navButtons
.clear();
418 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
423 m_protocolSeparator
= new QLabel("://", this);
424 m_host
= new QLineEdit(this);
426 connect(m_host
, SIGNAL(lostFocus()),
427 this, SLOT(slotRemoteHostActivated()));
428 connect(m_host
, SIGNAL(returnPressed()),
429 this, SLOT(slotRemoteHostActivated()));
434 m_protocolSeparator
->show();
440 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
442 // kDebug() << "received redirection to " << newUrl << endl;
443 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
444 /* UrlStack::iterator it = m_urls.find(oldUrl);
445 if (it != m_urls.end())
447 m_urls.erase(++it, m_urls.end());
450 m_urls.append(newUrl);*/
453 void UrlNavigator::slotClicked()
455 if (isUrlEditable()) {
456 m_pathBox
->setFocus();
460 setUrl(m_pathBox
->currentText());
461 emit
requestActivation();
465 void UrlNavigator::updateHistoryElem()
467 assert(m_historyIndex
>= 0);
468 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
470 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
471 (*it
).setCurrentFileName(item
->name());
475 void UrlNavigator::updateContent()
477 m_bookmarkSelector
->updateSelection(url());
479 m_toggleButton
->setToolTip(QString());
480 QString
path(url().pathOrUrl());
482 // TODO: prevent accessing the DolphinMainWindow out from this scope
483 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
484 // TODO: registry of default shortcuts
485 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
486 const QString shortcut
= "Ctrl+L";
488 if (m_toggleButton
->isChecked()) {
489 delete m_protocols
; m_protocols
= 0;
490 delete m_protocolSeparator
; m_protocolSeparator
= 0;
491 delete m_host
; m_host
= 0;
494 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
496 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
498 m_pathBox
->setUrl(url());
501 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
503 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
506 // get the data from the currently selected bookmark
507 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
508 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
510 QString bookmarkPath
;
511 if (bookmark
.isNull()) {
512 // No bookmark is a part of the current Url.
513 // The following code tries to guess the bookmark
514 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
515 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
516 // navigation indication 'Custom Path > var > lib".
517 int idx
= path
.indexOf(QString("//"));
518 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
519 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
522 bookmarkPath
= bookmark
.url().pathOrUrl();
524 const uint len
= bookmarkPath
.length();
526 // calculate the start point for the URL navigator buttons by counting
527 // the slashs inside the bookmark URL
529 for (uint i
= 0; i
< len
; ++i
) {
530 if (bookmarkPath
.at(i
) == QChar('/')) {
534 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
535 assert(slashCount
> 0);
539 if (!url().isLocalFile() && bookmark
.isNull()) {
540 QString protocol
= url().protocol();
543 m_protocols
= new ProtocolCombo(protocol
, this);
544 connect(m_protocols
, SIGNAL(activated(const QString
&)),
545 this, SLOT(slotProtocolChanged(const QString
&)));
548 m_protocols
->setProtocol(protocol
);
552 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
553 QString hostText
= url().host();
555 if (!url().user().isEmpty()) {
556 hostText
= url().user() + "@" + hostText
;
560 m_protocolSeparator
= new QLabel("://", this);
561 appendWidget(m_protocolSeparator
);
562 m_host
= new QLineEdit(hostText
, this);
563 appendWidget(m_host
);
565 connect(m_host
, SIGNAL(lostFocus()),
566 this, SLOT(slotRemoteHostActivated()));
567 connect(m_host
, SIGNAL(returnPressed()),
568 this, SLOT(slotRemoteHostActivated()));
571 m_host
->setText(hostText
);
573 m_protocolSeparator
->show();
577 delete m_protocolSeparator
; m_protocolSeparator
= 0;
578 delete m_host
; m_host
= 0;
581 else if (m_protocols
) {
585 m_protocolSeparator
->hide();
590 updateButtons(path
, slashCount
);
594 void UrlNavigator::updateButtons(const QString
& path
, int startIndex
)
596 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
597 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
598 bool createButton
= false;
600 int idx
= startIndex
;
603 createButton
= (it
== itEnd
);
605 const QString dirName
= path
.section('/', idx
, idx
);
606 const bool isFirstButton
= (idx
== startIndex
);
607 hasNext
= isFirstButton
|| !dirName
.isEmpty();
611 // the first URL navigator button should get the name of the
612 // bookmark instead of the directory name
613 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
614 text
= bookmark
.text();
615 if (text
.isEmpty()) {
616 if (url().isLocalFile()) {
617 text
= i18n("Custom Path");
626 UrlNavigatorButton
* button
= 0;
628 button
= new UrlNavigatorButton(idx
, this);
629 appendWidget(button
);
633 button
->setIndex(idx
);
637 button
->setText(text
);
642 m_navButtons
.append(button
);
651 // delete buttons which are not used anymore
652 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
653 while (it
!= itEnd
) {
655 (*it
)->deleteLater();
658 m_navButtons
.erase(itBegin
, m_navButtons
.end());
661 void UrlNavigator::deleteButtons()
663 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
664 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
665 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
666 while (it
!= itEnd
) {
668 (*it
)->deleteLater();
671 m_navButtons
.erase(itBegin
, itEnd
);
674 void UrlNavigator::appendWidget(QWidget
* widget
)
676 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
679 #include "urlnavigator.moc"