]>
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),
78 m_layout
= new QHBoxLayout();
79 m_layout
->setSpacing(0);
80 m_layout
->setMargin(0);
82 m_history
.prepend(HistoryElem(url
));
84 QFontMetrics
fontMetrics(font());
85 setMinimumHeight(fontMetrics
.height() + 8);
87 m_toggleButton
= new QCheckBox();
88 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
89 m_toggleButton
->setMinimumHeight(minimumHeight());
90 connect(m_toggleButton
, SIGNAL(clicked()),
91 this, SLOT(switchView()));
92 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
93 m_toggleButton
->toggle();
96 m_bookmarkSelector
= new BookmarkSelector(this);
97 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
98 this, SLOT(setUrl(const KUrl
&)));
100 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, this);
102 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
103 m_pathBox
->setCompletionObject(kurlCompletion
);
104 m_pathBox
->setAutoDeleteCompletionObject(true);
106 connect(m_pathBox
, SIGNAL(returnPressed(const QString
&)),
107 this, SLOT(slotReturnPressed(const QString
&)));
108 connect(m_pathBox
, SIGNAL(urlActivated(const KUrl
&)),
109 this, SLOT(slotUrlActivated(const KUrl
&)));
111 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
112 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
114 // Append a filler widget at the end, which automatically resizes to the
115 // maximum available width. This assures that the URL navigator uses the
116 // whole width, so that the clipboard content can be dropped.
117 m_filler
= new QWidget();
118 m_filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
120 m_layout
->addWidget(m_toggleButton
);
121 m_layout
->addWidget(m_bookmarkSelector
);
122 m_layout
->addWidget(m_pathBox
);
123 m_layout
->addWidget(m_filler
);
129 UrlNavigator::~UrlNavigator()
133 const KUrl
& UrlNavigator::url() const
135 assert(!m_history
.empty());
136 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
137 it
+= m_historyIndex
;
141 KUrl
UrlNavigator::url(int index
) const
144 QString
path(url().pathOrUrl());
145 path
= path
.section('/', 0, index
);
147 if ( path
.length() >= 1 && path
.at(path
.length()-1) != '/')
155 const QLinkedList
<UrlNavigator::HistoryElem
>& UrlNavigator::history(int& index
) const
157 index
= m_historyIndex
;
161 void UrlNavigator::goBack()
165 const int count
= m_history
.count();
166 if (m_historyIndex
< count
- 1) {
169 emit
urlChanged(url());
170 emit
historyChanged();
174 void UrlNavigator::goForward()
176 if (m_historyIndex
> 0) {
179 emit
urlChanged(url());
180 emit
historyChanged();
184 void UrlNavigator::goUp()
186 setUrl(url().upUrl());
189 void UrlNavigator::goHome()
191 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
194 void UrlNavigator::setUrlEditable(bool editable
)
196 if (isUrlEditable() != editable
) {
197 m_toggleButton
->toggle();
202 bool UrlNavigator::isUrlEditable() const
204 return m_toggleButton
->isChecked();
207 void UrlNavigator::editUrl(bool editOrBrowse
)
209 setUrlEditable(editOrBrowse
);
211 m_pathBox
->setFocus();
215 void UrlNavigator::setActive(bool active
)
217 if (active
!= m_active
) {
226 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
227 const KUrl
& destination
)
229 emit
urlsDropped(urls
, destination
);
232 void UrlNavigator::setUrl(const KUrl
& url
)
234 QString
urlStr(url
.pathOrUrl());
235 //kDebug() << "setUrl(" << url << ")" << endl;
236 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
237 // replace '~' by the home directory
239 urlStr
.insert(0, QDir::home().path());
242 const KUrl
transformedUrl(urlStr
);
244 if (m_historyIndex
> 0) {
245 // Check whether the previous element of the history has the same Url.
246 // If yes, just go forward instead of inserting a duplicate history
248 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
249 it
+= m_historyIndex
- 1;
250 const KUrl
& nextUrl
= (*it
).url();
251 if (transformedUrl
== nextUrl
) {
253 // kDebug() << "goin' forward in history" << endl;
258 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
259 const KUrl
& currUrl
= (*it
).url();
260 if (currUrl
== transformedUrl
) {
261 // don't insert duplicate history elements
262 // kDebug() << "currUrl == transformedUrl" << endl;
267 m_history
.insert(it
, HistoryElem(transformedUrl
));
271 emit
urlChanged(transformedUrl
);
272 emit
historyChanged();
274 // Prevent an endless growing of the history: remembering
275 // the last 100 Urls should be enough...
276 if (m_historyIndex
> 100) {
277 m_history
.erase(m_history
.begin());
281 /* kDebug() << "history starting ====================" << endl;
283 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
284 it != m_history.end();
287 kDebug() << i << ": " << (*it).url() << endl;
289 kDebug() << "history done ========================" << endl;*/
294 void UrlNavigator::requestActivation()
299 void UrlNavigator::storeContentsPosition(int x
, int y
)
301 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
302 (*it
).setContentsX(x
);
303 (*it
).setContentsY(y
);
306 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
308 QWidget::keyReleaseEvent(event
);
309 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
310 setUrlEditable(false);
314 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
316 if (event
->button() == Qt::MidButton
) {
317 QClipboard
* clipboard
= QApplication::clipboard();
318 const QMimeData
* mimeData
= clipboard
->mimeData();
319 if (mimeData
->hasText()) {
320 const QString text
= mimeData
->text();
324 QWidget::mouseReleaseEvent(event
);
327 void UrlNavigator::slotReturnPressed(const QString
& text
)
329 // Parts of the following code have been taken
330 // from the class KateFileSelector located in
331 // kate/app/katefileselector.hpp of Kate.
332 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
333 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
334 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
337 if (typedUrl
.hasPass()) {
338 typedUrl
.setPass(QString::null
);
341 QStringList urls
= m_pathBox
->urls();
342 urls
.removeAll(typedUrl
.url());
343 urls
.prepend(typedUrl
.url());
344 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
347 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
348 // synchronize the result in the path box.
349 m_pathBox
->setUrl(url());
352 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
357 void UrlNavigator::slotRemoteHostActivated()
361 QString host
= m_host
->text();
364 int marker
= host
.indexOf("@");
367 user
= host
.left(marker
);
369 host
= host
.right(host
.length() - marker
- 1);
372 marker
= host
.indexOf("/");
375 u
.setPath(host
.right(host
.length() - marker
));
376 host
.truncate(marker
);
383 if (m_protocols
->currentProtocol() != u
.protocol() ||
387 u
.setProtocol(m_protocols
->currentProtocol());
388 u
.setHost(m_host
->text());
390 //TODO: get rid of this HACK for file:///!
391 if (u
.protocol() == "file")
394 if (u
.path().isEmpty())
404 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
407 url
.setProtocol(protocol
);
408 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
410 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
411 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
412 while (it
!= itEnd
) {
414 (*it
)->deleteLater();
417 m_navButtons
.clear();
419 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
424 m_protocolSeparator
= new QLabel("://", this);
425 appendWidget(m_protocolSeparator
);
426 m_host
= new QLineEdit(this);
427 appendWidget(m_host
);
429 connect(m_host
, SIGNAL(lostFocus()),
430 this, SLOT(slotRemoteHostActivated()));
431 connect(m_host
, SIGNAL(returnPressed()),
432 this, SLOT(slotRemoteHostActivated()));
437 m_protocolSeparator
->show();
443 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
445 // kDebug() << "received redirection to " << newUrl << endl;
446 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
447 /* UrlStack::iterator it = m_urls.find(oldUrl);
448 if (it != m_urls.end())
450 m_urls.erase(++it, m_urls.end());
453 m_urls.append(newUrl);*/
456 void UrlNavigator::switchView()
459 if (isUrlEditable()) {
460 m_pathBox
->setFocus();
463 setUrl(m_pathBox
->currentText());
465 emit
requestActivation();
468 void UrlNavigator::updateHistoryElem()
470 assert(m_historyIndex
>= 0);
471 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
473 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
474 (*it
).setCurrentFileName(item
->name());
478 void UrlNavigator::updateContent()
480 m_bookmarkSelector
->updateSelection(url());
482 m_toggleButton
->setToolTip(QString());
483 QString
path(url().pathOrUrl());
485 // TODO: prevent accessing the DolphinMainWindow out from this scope
486 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
487 // TODO: registry of default shortcuts
488 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
489 const QString shortcut
= "Ctrl+L";
491 if (m_toggleButton
->isChecked()) {
492 delete m_protocols
; m_protocols
= 0;
493 delete m_protocolSeparator
; m_protocolSeparator
= 0;
494 delete m_host
; m_host
= 0;
498 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
500 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
502 m_pathBox
->setUrl(url());
505 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
507 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
511 // get the data from the currently selected bookmark
512 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
513 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
515 QString bookmarkPath
;
516 if (bookmark
.isNull()) {
517 // No bookmark is a part of the current Url.
518 // The following code tries to guess the bookmark
519 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
520 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
521 // navigation indication 'Custom Path > var > lib".
522 int idx
= path
.indexOf(QString("//"));
523 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
524 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
527 bookmarkPath
= bookmark
.url().pathOrUrl();
529 const uint len
= bookmarkPath
.length();
531 // calculate the start point for the URL navigator buttons by counting
532 // the slashs inside the bookmark URL
534 for (uint i
= 0; i
< len
; ++i
) {
535 if (bookmarkPath
.at(i
) == QChar('/')) {
539 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
540 assert(slashCount
> 0);
544 if (!url().isLocalFile() && bookmark
.isNull()) {
545 QString protocol
= url().protocol();
548 m_protocols
= new ProtocolCombo(protocol
, this);
549 appendWidget(m_protocols
);
550 connect(m_protocols
, SIGNAL(activated(const QString
&)),
551 this, SLOT(slotProtocolChanged(const QString
&)));
554 m_protocols
->setProtocol(protocol
);
558 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
559 QString hostText
= url().host();
561 if (!url().user().isEmpty()) {
562 hostText
= url().user() + "@" + hostText
;
566 m_protocolSeparator
= new QLabel("://", this);
567 appendWidget(m_protocolSeparator
);
568 m_host
= new QLineEdit(hostText
, this);
569 appendWidget(m_host
);
571 connect(m_host
, SIGNAL(lostFocus()),
572 this, SLOT(slotRemoteHostActivated()));
573 connect(m_host
, SIGNAL(returnPressed()),
574 this, SLOT(slotRemoteHostActivated()));
577 m_host
->setText(hostText
);
579 m_protocolSeparator
->show();
583 delete m_protocolSeparator
; m_protocolSeparator
= 0;
584 delete m_host
; m_host
= 0;
587 else if (m_protocols
) {
591 m_protocolSeparator
->hide();
596 updateButtons(path
, slashCount
);
600 void UrlNavigator::updateButtons(const QString
& path
, int startIndex
)
602 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
603 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
604 bool createButton
= false;
606 int idx
= startIndex
;
609 createButton
= (it
== itEnd
);
611 const QString dirName
= path
.section('/', idx
, idx
);
612 const bool isFirstButton
= (idx
== startIndex
);
613 hasNext
= isFirstButton
|| !dirName
.isEmpty();
617 // the first URL navigator button should get the name of the
618 // bookmark instead of the directory name
619 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
620 text
= bookmark
.text();
621 if (text
.isEmpty()) {
622 if (url().isLocalFile()) {
623 text
= i18n("Custom Path");
632 UrlNavigatorButton
* button
= 0;
634 button
= new UrlNavigatorButton(idx
, this);
635 appendWidget(button
);
639 button
->setIndex(idx
);
643 button
->setText(text
);
648 m_navButtons
.append(button
);
657 // delete buttons which are not used anymore
658 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
659 while (it
!= itEnd
) {
661 (*it
)->deleteLater();
664 m_navButtons
.erase(itBegin
, m_navButtons
.end());
667 void UrlNavigator::deleteButtons()
669 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
670 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
671 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
672 while (it
!= itEnd
) {
674 (*it
)->deleteLater();
677 m_navButtons
.erase(itBegin
, itEnd
);
680 void UrlNavigator::appendWidget(QWidget
* widget
)
682 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
685 #include "urlnavigator.moc"