]>
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>
43 UrlNavigator::HistoryElem::HistoryElem() :
51 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
59 UrlNavigator::HistoryElem::~HistoryElem()
63 UrlNavigator::UrlNavigator(const KUrl
& url
,
69 m_protocolSeparator(0),
72 m_history
.prepend(HistoryElem(url
));
74 QFontMetrics
fontMetrics(font());
75 setMinimumHeight(fontMetrics
.height() + 8);
77 m_toggleButton
= new QCheckBox(this);
78 //m_toggleButton->setFlat(true);
79 //m_toggleButton->setToggleButton(true);
80 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
81 m_toggleButton
->setMinimumHeight(minimumHeight());
82 connect(m_toggleButton
, SIGNAL(clicked()),
83 this, SLOT(slotClicked()));
84 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
85 m_toggleButton
->toggle();
88 m_bookmarkSelector
= new BookmarkSelector(this);
89 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
90 this, SLOT(setUrl(const KUrl
&)));
92 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, this);
94 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
95 m_pathBox
->setCompletionObject(kurlCompletion
);
96 m_pathBox
->setAutoDeleteCompletionObject(true);
98 connect(m_pathBox
, SIGNAL(returnPressed(const QString
&)),
99 this, SLOT(slotReturnPressed(const QString
&)));
100 connect(m_pathBox
, SIGNAL(urlActivated(const KUrl
&)),
101 this, SLOT(slotUrlActivated(const KUrl
&)));
103 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
104 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
108 UrlNavigator::~UrlNavigator()
112 const KUrl
& UrlNavigator::url() const
114 assert(!m_history
.empty());
115 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
116 it
+= m_historyIndex
;
120 KUrl
UrlNavigator::url(int index
) const
123 QString
path(url().pathOrUrl());
124 path
= path
.section('/', 0, index
);
126 if ( path
.length() >= 1 && path
.at(path
.length()-1) != '/')
134 const QLinkedList
<UrlNavigator::HistoryElem
>& UrlNavigator::history(int& index
) const
136 index
= m_historyIndex
;
140 void UrlNavigator::goBack()
144 const int count
= m_history
.count();
145 if (m_historyIndex
< count
- 1) {
148 emit
urlChanged(url());
149 emit
historyChanged();
153 void UrlNavigator::goForward()
155 if (m_historyIndex
> 0) {
158 emit
urlChanged(url());
159 emit
historyChanged();
163 void UrlNavigator::goUp()
165 setUrl(url().upUrl());
168 void UrlNavigator::goHome()
170 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
173 void UrlNavigator::setUrlEditable(bool editable
)
175 if (isUrlEditable() != editable
) {
176 m_toggleButton
->toggle();
181 bool UrlNavigator::isUrlEditable() const
183 return m_toggleButton
->isChecked();
186 void UrlNavigator::editUrl(bool editOrBrowse
)
188 setUrlEditable(editOrBrowse
);
190 m_pathBox
->setFocus();
194 void UrlNavigator::setActive(bool active
)
196 if (active
!= m_active
) {
205 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
206 const KUrl
& destination
)
208 emit
urlsDropped(urls
, destination
);
211 void UrlNavigator::setUrl(const KUrl
& url
)
213 QString
urlStr(url
.pathOrUrl());
214 //kDebug() << "setUrl(" << url << ")" << endl;
215 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
216 // replace '~' by the home directory
218 urlStr
.insert(0, QDir::home().path());
221 const KUrl
transformedUrl(urlStr
);
223 if (m_historyIndex
> 0) {
224 // Check whether the previous element of the history has the same Url.
225 // If yes, just go forward instead of inserting a duplicate history
227 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
228 it
+= m_historyIndex
- 1;
229 const KUrl
& nextUrl
= (*it
).url();
230 if (transformedUrl
== nextUrl
) {
232 // kDebug() << "goin' forward in history" << endl;
237 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
238 const KUrl
& currUrl
= (*it
).url();
239 if (currUrl
== transformedUrl
) {
240 // don't insert duplicate history elements
241 // kDebug() << "currUrl == transformedUrl" << endl;
246 m_history
.insert(it
, HistoryElem(transformedUrl
));
250 emit
urlChanged(transformedUrl
);
251 emit
historyChanged();
253 // Prevent an endless growing of the history: remembering
254 // the last 100 Urls should be enough...
255 if (m_historyIndex
> 100) {
256 m_history
.erase(m_history
.begin());
260 /* kDebug() << "history starting ====================" << endl;
262 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
263 it != m_history.end();
266 kDebug() << i << ": " << (*it).url() << endl;
268 kDebug() << "history done ========================" << endl;*/
273 void UrlNavigator::requestActivation()
278 void UrlNavigator::storeContentsPosition(int x
, int y
)
280 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
281 (*it
).setContentsX(x
);
282 (*it
).setContentsY(y
);
285 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
287 KHBox::keyReleaseEvent(event
);
288 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
289 setUrlEditable(false);
293 void UrlNavigator::slotReturnPressed(const QString
& text
)
295 // Parts of the following code have been taken
296 // from the class KateFileSelector located in
297 // kate/app/katefileselector.hpp of Kate.
298 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
299 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
300 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
303 if (typedUrl
.hasPass()) {
304 typedUrl
.setPass(QString::null
);
307 QStringList urls
= m_pathBox
->urls();
308 urls
.removeAll(typedUrl
.url());
309 urls
.prepend(typedUrl
.url());
310 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
313 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
314 // synchronize the result in the path box.
315 m_pathBox
->setUrl(url());
318 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
323 void UrlNavigator::slotRemoteHostActivated()
327 QString host
= m_host
->text();
330 int marker
= host
.indexOf("@");
333 user
= host
.left(marker
);
335 host
= host
.right(host
.length() - marker
- 1);
338 marker
= host
.indexOf("/");
341 u
.setPath(host
.right(host
.length() - marker
));
342 host
.truncate(marker
);
349 if (m_protocols
->currentProtocol() != u
.protocol() ||
353 u
.setProtocol(m_protocols
->currentProtocol());
354 u
.setHost(m_host
->text());
356 //TODO: get rid of this HACK for file:///!
357 if (u
.protocol() == "file")
360 if (u
.path().isEmpty())
370 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
373 url
.setProtocol(protocol
);
374 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
376 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
377 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
378 while (it
!= itEnd
) {
380 (*it
)->deleteLater();
383 m_navButtons
.clear();
385 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
390 m_protocolSeparator
= new QLabel("://", this);
391 m_host
= new QLineEdit(this);
393 connect(m_host
, SIGNAL(lostFocus()),
394 this, SLOT(slotRemoteHostActivated()));
395 connect(m_host
, SIGNAL(returnPressed()),
396 this, SLOT(slotRemoteHostActivated()));
401 m_protocolSeparator
->show();
407 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
409 // kDebug() << "received redirection to " << newUrl << endl;
410 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
411 /* UrlStack::iterator it = m_urls.find(oldUrl);
412 if (it != m_urls.end())
414 m_urls.erase(++it, m_urls.end());
417 m_urls.append(newUrl);*/
420 void UrlNavigator::slotClicked()
422 if (isUrlEditable()) {
423 m_pathBox
->setFocus();
427 setUrl(m_pathBox
->currentText());
428 emit
requestActivation();
432 void UrlNavigator::updateHistoryElem()
434 assert(m_historyIndex
>= 0);
435 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
437 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
438 (*it
).setCurrentFileName(item
->name());
442 void UrlNavigator::updateContent()
444 m_bookmarkSelector
->updateSelection(url());
446 m_toggleButton
->setToolTip(QString());
447 QString
path(url().pathOrUrl());
449 // TODO: prevent accessing the DolphinMainWindow out from this scope
450 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
451 // TODO: registry of default shortcuts
452 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
453 const QString shortcut
= "Ctrl+L";
455 if (m_toggleButton
->isChecked()) {
456 delete m_protocols
; m_protocols
= 0;
457 delete m_protocolSeparator
; m_protocolSeparator
= 0;
458 delete m_host
; m_host
= 0;
461 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
463 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
465 m_pathBox
->setUrl(url());
468 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
470 setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
473 // get the data from the currently selected bookmark
474 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
475 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
477 QString bookmarkPath
;
478 if (bookmark
.isNull()) {
479 // No bookmark is a part of the current Url.
480 // The following code tries to guess the bookmark
481 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
482 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
483 // navigation indication 'Custom Path > var > lib".
484 int idx
= path
.indexOf(QString("//"));
485 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
486 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
489 bookmarkPath
= bookmark
.url().pathOrUrl();
491 const uint len
= bookmarkPath
.length();
493 // calculate the start point for the URL navigator buttons by counting
494 // the slashs inside the bookmark URL
496 for (uint i
= 0; i
< len
; ++i
) {
497 if (bookmarkPath
.at(i
) == QChar('/')) {
501 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
502 assert(slashCount
> 0);
506 if (!url().isLocalFile() && bookmark
.isNull()) {
507 QString protocol
= url().protocol();
510 m_protocols
= new ProtocolCombo(protocol
, this);
511 connect(m_protocols
, SIGNAL(activated(const QString
&)),
512 this, SLOT(slotProtocolChanged(const QString
&)));
515 m_protocols
->setProtocol(protocol
);
519 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
520 QString hostText
= url().host();
522 if (!url().user().isEmpty()) {
523 hostText
= url().user() + "@" + hostText
;
527 m_protocolSeparator
= new QLabel("://", this);
528 m_host
= new QLineEdit(hostText
, this);
530 connect(m_host
, SIGNAL(lostFocus()),
531 this, SLOT(slotRemoteHostActivated()));
532 connect(m_host
, SIGNAL(returnPressed()),
533 this, SLOT(slotRemoteHostActivated()));
536 m_host
->setText(hostText
);
538 m_protocolSeparator
->show();
542 delete m_protocolSeparator
; m_protocolSeparator
= 0;
543 delete m_host
; m_host
= 0;
546 else if (m_protocols
) {
550 m_protocolSeparator
->hide();
555 updateButtons(path
, slashCount
);
559 void UrlNavigator::updateButtons(const QString
& path
, int startIndex
)
561 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
562 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
563 bool createButton
= false;
565 int idx
= startIndex
;
568 createButton
= (it
== itEnd
);
570 const QString dirName
= path
.section('/', idx
, idx
);
571 const bool isFirstButton
= (idx
== startIndex
);
572 hasNext
= isFirstButton
|| !dirName
.isEmpty();
576 // the first URL navigator button should get the name of the
577 // bookmark instead of the directory name
578 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
579 text
= bookmark
.text();
580 if (text
.isEmpty()) {
581 if (url().isLocalFile()) {
582 text
= i18n("Custom Path");
591 UrlNavigatorButton
* button
= 0;
593 button
= new UrlNavigatorButton(idx
, this);
597 button
->setIndex(idx
);
601 button
->setText(text
);
606 m_navButtons
.append(button
);
615 // delete buttons which are not used anymore
616 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
617 while (it
!= itEnd
) {
619 (*it
)->deleteLater();
622 m_navButtons
.erase(itBegin
, m_navButtons
.end());
625 void UrlNavigator::deleteButtons()
627 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
628 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
629 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
630 while (it
!= itEnd
) {
632 (*it
)->deleteLater();
635 m_navButtons
.erase(itBegin
, itEnd
);
638 #include "urlnavigator.moc"