]>
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"
26 #include <qcombobox.h>
29 #include <q3listbox.h>
30 #include <qlineedit.h>
32 #include <q3popupmenu.h>
33 #include <qpushbutton.h>
34 #include <qsizepolicy.h>
38 #include <Q3ValueList>
43 #include <kactioncollection.h>
44 #include <kiconloader.h>
46 #include <kfileitem.h>
48 #include <kprotocolinfo.h>
50 #include <kurlcombobox.h>
51 #include <kurlcompletion.h>
52 #include <kbookmarkmanager.h>
55 #include "bookmarkselector.h"
56 #include "dolphinsettings.h"
57 #include "generalsettings.h"
58 #include "protocolcombo.h"
59 #include "urlnavigatorbutton.h"
61 UrlNavigator::HistoryElem::HistoryElem() :
69 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
77 UrlNavigator::HistoryElem::~HistoryElem()
81 UrlNavigator::UrlNavigator(const KUrl
& url
,
87 m_protocolSeparator(0),
90 m_history
.prepend(HistoryElem(url
));
92 QFontMetrics
fontMetrics(font());
93 setMinimumHeight(fontMetrics
.height() + 8);
95 m_toggleButton
= new QCheckBox(this);
96 //m_toggleButton->setFlat(true);
97 //m_toggleButton->setToggleButton(true);
98 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
99 m_toggleButton
->setMinimumHeight(minimumHeight());
100 connect(m_toggleButton
, SIGNAL(clicked()),
101 this, SLOT(slotClicked()));
102 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
103 m_toggleButton
->toggle();
106 m_bookmarkSelector
= new BookmarkSelector(this);
107 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
108 this, SLOT(setUrl(const KUrl
&)));
110 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, this);
112 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
113 m_pathBox
->setCompletionObject(kurlCompletion
);
114 m_pathBox
->setAutoDeleteCompletionObject(true);
116 connect(m_pathBox
, SIGNAL(returnPressed(const QString
&)),
117 this, SLOT(slotReturnPressed(const QString
&)));
118 connect(m_pathBox
, SIGNAL(urlActivated(const KUrl
&)),
119 this, SLOT(slotUrlActivated(const KUrl
&)));
121 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
122 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
126 UrlNavigator::~UrlNavigator()
130 const KUrl
& UrlNavigator::url() const
132 assert(!m_history
.empty());
133 return m_history
[m_historyIndex
].url();
136 KUrl
UrlNavigator::url(int index
) const
139 QString
path(url().pathOrUrl());
140 path
= path
.section('/', 0, index
);
142 if ( path
.length() >= 1 && path
.at(path
.length()-1) != '/')
150 const Q3ValueList
<UrlNavigator::HistoryElem
>& UrlNavigator::history(int& index
) const
152 index
= m_historyIndex
;
156 void UrlNavigator::goBack()
160 const int count
= m_history
.count();
161 if (m_historyIndex
< count
- 1) {
164 emit
urlChanged(url());
165 emit
historyChanged();
169 void UrlNavigator::goForward()
171 if (m_historyIndex
> 0) {
174 emit
urlChanged(url());
175 emit
historyChanged();
179 void UrlNavigator::goUp()
181 setUrl(url().upUrl());
184 void UrlNavigator::goHome()
186 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
189 void UrlNavigator::setUrlEditable(bool editable
)
191 if (isUrlEditable() != editable
) {
192 m_toggleButton
->toggle();
197 bool UrlNavigator::isUrlEditable() const
199 return m_toggleButton
->isChecked();
202 void UrlNavigator::editUrl(bool editOrBrowse
)
204 setUrlEditable(editOrBrowse
);
208 m_pathBox
->setFocus();
212 void UrlNavigator::setActive(bool active
)
214 if (active
!= m_active
) {
223 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
224 const KUrl
& destination
)
226 kDebug() << "------------------- URLS dropped" << endl
;
227 emit
urlsDropped(urls
, destination
);
230 void UrlNavigator::setUrl(const KUrl
& url
)
232 QString
urlStr(url
.pathOrUrl());
233 //kDebug() << "setUrl(" << url << ")" << endl;
234 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
235 // replace '~' by the home directory
237 urlStr
.insert(0, QDir::home().path());
240 const KUrl
transformedUrl(urlStr
);
242 if (m_historyIndex
> 0) {
243 // Check whether the previous element of the history has the same Url.
244 // If yes, just go forward instead of inserting a duplicate history
246 const KUrl
& nextUrl
= m_history
[m_historyIndex
- 1].url();
247 if (transformedUrl
== nextUrl
) {
249 // kDebug() << "goin' forward in history" << endl;
254 const KUrl
& currUrl
= m_history
[m_historyIndex
].url();
255 if (currUrl
== transformedUrl
) {
256 // don't insert duplicate history elements
257 // kDebug() << "currUrl == transformedUrl" << endl;
263 const Q3ValueListIterator
<UrlNavigator::HistoryElem
> it
= m_history
.at(m_historyIndex
);
264 m_history
.insert(it
, HistoryElem(transformedUrl
));
268 emit
urlChanged(transformedUrl
);
269 emit
historyChanged();
271 // Prevent an endless growing of the history: remembering
272 // the last 100 Urls should be enough...
273 if (m_historyIndex
> 100) {
274 m_history
.erase(m_history
.begin());
278 /* kDebug() << "history starting ====================" << endl;
280 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
281 it != m_history.end();
284 kDebug() << i << ": " << (*it).url() << endl;
286 kDebug() << "history done ========================" << endl;*/
291 void UrlNavigator::requestActivation()
293 kDebug() << "--------------------------- request activation" << endl
;
297 void UrlNavigator::storeContentsPosition(int x
, int y
)
299 m_history
[m_historyIndex
].setContentsX(x
);
300 m_history
[m_historyIndex
].setContentsY(y
);
303 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
305 KHBox::keyReleaseEvent(event
);
306 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
307 setUrlEditable(false);
311 void UrlNavigator::slotReturnPressed(const QString
& text
)
313 // Parts of the following code have been taken
314 // from the class KateFileSelector located in
315 // kate/app/katefileselector.hpp of Kate.
316 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
317 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
318 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
321 if (typedUrl
.hasPass()) {
322 typedUrl
.setPass(QString::null
);
325 QStringList urls
= m_pathBox
->urls();
326 urls
.removeAll(typedUrl
.url());
327 urls
.prepend(typedUrl
.url());
328 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
331 // The Url might have been adjusted by UrlNavigator::setUrl(), hence
332 // synchronize the result in the path box.
333 m_pathBox
->setUrl(url());
336 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
341 void UrlNavigator::slotRemoteHostActivated()
345 QString host
= m_host
->text();
348 int marker
= host
.indexOf("@");
351 user
= host
.left(marker
);
353 host
= host
.right(host
.length() - marker
- 1);
356 marker
= host
.indexOf("/");
359 u
.setPath(host
.right(host
.length() - marker
));
360 host
.truncate(marker
);
367 if (m_protocols
->currentProtocol() != u
.protocol() ||
371 u
.setProtocol(m_protocols
->currentProtocol());
372 u
.setHost(m_host
->text());
374 //TODO: get rid of this HACK for file:///!
375 if (u
.protocol() == "file")
378 if (u
.path().isEmpty())
388 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
391 url
.setProtocol(protocol
);
392 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
394 Q3ValueList
<QWidget
*>::const_iterator it
= m_navButtons
.constBegin();
395 while (it
!= m_navButtons
.constEnd()) {
397 (*it
)->deleteLater();
400 m_navButtons
.clear();
402 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
407 m_protocolSeparator
= new QLabel("://", this);
408 m_host
= new QLineEdit(this);
410 connect(m_host
, SIGNAL(lostFocus()),
411 this, SLOT(slotRemoteHostActivated()));
412 connect(m_host
, SIGNAL(returnPressed()),
413 this, SLOT(slotRemoteHostActivated()));
418 m_protocolSeparator
->show();
424 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
426 // kDebug() << "received redirection to " << newUrl << endl;
427 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
428 /* UrlStack::iterator it = m_urls.find(oldUrl);
429 if (it != m_urls.end())
431 m_urls.erase(++it, m_urls.end());
434 m_urls.append(newUrl);*/
437 void UrlNavigator::slotClicked()
439 if (isUrlEditable()) {
440 m_pathBox
->setFocus();
444 setUrl(m_pathBox
->currentText());
445 emit
requestActivation();
449 void UrlNavigator::updateHistoryElem()
451 assert(m_historyIndex
>= 0);
452 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
454 m_history
[m_historyIndex
].setCurrentFileName(item
->name());
458 void UrlNavigator::updateContent()
460 // delete all existing Url navigator buttons
461 Q3ValueList
<QWidget
*>::const_iterator it
= m_navButtons
.constBegin();
462 while (it
!= m_navButtons
.constEnd()) {
464 (*it
)->deleteLater();
467 m_navButtons
.clear();
469 m_bookmarkSelector
->updateSelection(url());
471 m_toggleButton
->setToolTip(QString());
472 QString
path(url().pathOrUrl());
474 // TODO: prevent accessing the DolphinMainWindow out from this scope
475 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
476 // TODO: registry of default shortcuts
477 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
478 const QString shortcut
= "Ctrl+L";
480 if (m_toggleButton
->isChecked()) {
481 delete m_protocols
; m_protocols
= 0;
482 delete m_protocolSeparator
; m_protocolSeparator
= 0;
483 delete m_host
; m_host
= 0;
485 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
487 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
489 m_pathBox
->setUrl(url());
492 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
494 setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
498 // get the data from the currently selected bookmark
499 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
500 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
502 QString bookmarkPath
;
503 if (bookmark
.isNull()) {
504 // No bookmark is a part of the current Url.
505 // The following code tries to guess the bookmark
506 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
507 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
508 // navigation indication 'Custom Path > var > lib".
509 int idx
= path
.indexOf(QString("//"));
510 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
511 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
514 bookmarkPath
= bookmark
.url().pathOrUrl();
516 const uint len
= bookmarkPath
.length();
518 // calculate the start point for the Url navigator buttons by counting
519 // the slashs inside the bookmark Url
521 for (uint i
= 0; i
< len
; ++i
) {
522 if (bookmarkPath
.at(i
) == QChar('/')) {
526 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
527 assert(slashCount
> 0);
531 if (!url().isLocalFile() && bookmark
.isNull()) {
532 QString protocol
= url().protocol();
534 m_protocols
= new ProtocolCombo(protocol
, this);
535 connect(m_protocols
, SIGNAL(activated(const QString
&)),
536 this, SLOT(slotProtocolChanged(const QString
&)));
539 m_protocols
->setProtocol(protocol
);
543 if (KProtocolInfo::protocolClass(protocol
) != ":local")
545 QString hostText
= url().host();
547 if (!url().user().isEmpty())
549 hostText
= url().user() + "@" + hostText
;
553 m_protocolSeparator
= new QLabel("://", this);
554 m_host
= new QLineEdit(hostText
, this);
556 connect(m_host
, SIGNAL(lostFocus()),
557 this, SLOT(slotRemoteHostActivated()));
558 connect(m_host
, SIGNAL(returnPressed()),
559 this, SLOT(slotRemoteHostActivated()));
562 m_host
->setText(hostText
);
564 m_protocolSeparator
->show();
568 delete m_protocolSeparator
; m_protocolSeparator
= 0;
569 delete m_host
; m_host
= 0;
572 else if (m_protocols
) {
576 m_protocolSeparator
->hide();
581 // create Url navigator buttons
582 int idx
= slashCount
;
585 dir_name
= path
.section('/', idx
, idx
);
586 const bool isFirstButton
= (idx
== slashCount
);
587 hasNext
= isFirstButton
|| !dir_name
.isEmpty();
589 UrlNavigatorButton
* button
= new UrlNavigatorButton(idx
, this);
591 // the first Url navigator button should get the name of the
592 // bookmark instead of the directory name
593 QString text
= bookmark
.text();
594 if (text
.isEmpty()) {
595 if (url().isLocalFile())
597 text
= i18n("Custom Path");
606 button
->setText(text
);
609 m_navButtons
.append(button
);
616 #include "urlnavigator.moc"