]>
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 "dolphin_generalsettings.h"
27 #include "protocolcombo.h"
28 #include "urlnavigatorbutton.h"
32 #include <kfileitem.h>
35 #include <kprotocolinfo.h>
36 #include <kurlcombobox.h>
37 #include <kurlcompletion.h>
39 #include <QApplication>
42 #include <QHBoxLayout>
45 #include <QMouseEvent>
46 #include <QToolButton>
48 UrlNavigator::HistoryElem::HistoryElem() :
56 UrlNavigator::HistoryElem::HistoryElem(const KUrl
& url
) :
64 UrlNavigator::HistoryElem::~HistoryElem()
68 UrlNavigator::UrlNavigator(const KUrl
& url
,
72 m_showHiddenFiles(false),
76 m_protocolSeparator(0),
80 m_layout
= new QHBoxLayout();
81 m_layout
->setSpacing(0);
82 m_layout
->setMargin(0);
84 m_history
.prepend(HistoryElem(url
));
86 QFontMetrics
fontMetrics(font());
87 setMinimumHeight(fontMetrics
.height() + 10);
89 // intialize toggle button which switches between the breadcrumb view
90 // and the traditional view
91 m_toggleButton
= new QToolButton();
92 m_toggleButton
->setCheckable(true);
93 m_toggleButton
->setAutoRaise(true);
94 m_toggleButton
->setIcon(KIcon("locationbar_erase")); // TODO: is just a placeholder icon
95 m_toggleButton
->setFocusPolicy(Qt::NoFocus
);
96 m_toggleButton
->setMinimumHeight(minimumHeight());
97 connect(m_toggleButton
, SIGNAL(clicked()),
98 this, SLOT(switchView()));
99 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
100 m_toggleButton
->toggle();
103 // initialize the bookmark selector
104 m_bookmarkSelector
= new BookmarkSelector(this);
105 connect(m_bookmarkSelector
, SIGNAL(bookmarkActivated(const KUrl
&)),
106 this, SLOT(setUrl(const KUrl
&)));
108 // initialize the path box of the traditional view
109 m_pathBox
= new KUrlComboBox(KUrlComboBox::Directories
, true, this);
111 KUrlCompletion
* kurlCompletion
= new KUrlCompletion(KUrlCompletion::DirCompletion
);
112 m_pathBox
->setCompletionObject(kurlCompletion
);
113 m_pathBox
->setAutoDeleteCompletionObject(true);
115 connect(m_pathBox
, SIGNAL(returnPressed(const QString
&)),
116 this, SLOT(slotReturnPressed(const QString
&)));
117 connect(m_pathBox
, SIGNAL(urlActivated(const KUrl
&)),
118 this, SLOT(slotUrlActivated(const KUrl
&)));
120 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
121 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
123 // Append a filler widget at the end, which automatically resizes to the
124 // maximum available width. This assures that the URL navigator uses the
125 // whole width, so that the clipboard content can be dropped.
126 m_filler
= new QWidget();
127 m_filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
129 m_layout
->addWidget(m_toggleButton
);
130 m_layout
->addWidget(m_bookmarkSelector
);
131 m_layout
->addWidget(m_pathBox
);
132 m_layout
->addWidget(m_filler
);
138 UrlNavigator::~UrlNavigator()
142 const KUrl
& UrlNavigator::url() const
144 assert(!m_history
.empty());
145 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
146 it
+= m_historyIndex
;
150 KUrl
UrlNavigator::url(int index
) const
153 QString
path(url().pathOrUrl());
154 path
= path
.section('/', 0, index
);
156 if ( path
.length() >= 1 && path
.at(path
.length()-1) != '/')
164 const QLinkedList
<UrlNavigator::HistoryElem
>& UrlNavigator::history(int& index
) const
166 index
= m_historyIndex
;
170 void UrlNavigator::goBack()
174 const int count
= m_history
.count();
175 if (m_historyIndex
< count
- 1) {
178 emit
urlChanged(url());
179 emit
historyChanged();
183 void UrlNavigator::goForward()
185 if (m_historyIndex
> 0) {
188 emit
urlChanged(url());
189 emit
historyChanged();
193 void UrlNavigator::goUp()
195 setUrl(url().upUrl());
198 void UrlNavigator::goHome()
200 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
203 void UrlNavigator::setUrlEditable(bool editable
)
205 if (isUrlEditable() != editable
) {
206 m_toggleButton
->toggle();
211 bool UrlNavigator::isUrlEditable() const
213 return m_toggleButton
->isChecked();
216 void UrlNavigator::editUrl(bool editOrBrowse
)
218 setUrlEditable(editOrBrowse
);
220 m_pathBox
->setFocus();
224 void UrlNavigator::setActive(bool active
)
226 if (active
!= m_active
) {
235 void UrlNavigator::setShowHiddenFiles( bool show
)
237 m_showHiddenFiles
= show
;
240 void UrlNavigator::dropUrls(const KUrl::List
& urls
,
241 const KUrl
& destination
)
243 emit
urlsDropped(urls
, destination
);
246 void UrlNavigator::setUrl(const KUrl
& url
)
248 QString
urlStr(url
.pathOrUrl());
249 //kDebug() << "setUrl(" << url << ")" << endl;
250 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
251 // replace '~' by the home directory
253 urlStr
.insert(0, QDir::home().path());
256 const KUrl
transformedUrl(urlStr
);
258 if (m_historyIndex
> 0) {
259 // Check whether the previous element of the history has the same Url.
260 // If yes, just go forward instead of inserting a duplicate history
262 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
263 it
+= m_historyIndex
- 1;
264 const KUrl
& nextUrl
= (*it
).url();
265 if (transformedUrl
== nextUrl
) {
267 // kDebug() << "goin' forward in history" << endl;
272 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
273 const KUrl
& currUrl
= (*it
).url();
274 if (currUrl
== transformedUrl
) {
275 // don't insert duplicate history elements
276 // kDebug() << "currUrl == transformedUrl" << endl;
281 m_history
.insert(it
, HistoryElem(transformedUrl
));
285 emit
urlChanged(transformedUrl
);
286 emit
historyChanged();
288 // Prevent an endless growing of the history: remembering
289 // the last 100 Urls should be enough...
290 if (m_historyIndex
> 100) {
291 m_history
.erase(m_history
.begin());
295 /* kDebug() << "history starting ====================" << endl;
297 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
298 it != m_history.end();
301 kDebug() << i << ": " << (*it).url() << endl;
303 kDebug() << "history done ========================" << endl;*/
308 void UrlNavigator::requestActivation()
313 void UrlNavigator::storeContentsPosition(int x
, int y
)
315 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
316 (*it
).setContentsX(x
);
317 (*it
).setContentsY(y
);
320 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
322 QWidget::keyReleaseEvent(event
);
323 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
324 setUrlEditable(false);
328 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
330 if (event
->button() == Qt::MidButton
) {
331 QClipboard
* clipboard
= QApplication::clipboard();
332 const QMimeData
* mimeData
= clipboard
->mimeData();
333 if (mimeData
->hasText()) {
334 const QString text
= mimeData
->text();
338 QWidget::mouseReleaseEvent(event
);
341 void UrlNavigator::slotReturnPressed(const QString
& text
)
343 // Parts of the following code have been taken
344 // from the class KateFileSelector located in
345 // kate/app/katefileselector.hpp of Kate.
346 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
347 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
348 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
351 if (typedUrl
.hasPass()) {
352 typedUrl
.setPass(QString());
355 QStringList urls
= m_pathBox
->urls();
356 urls
.removeAll(typedUrl
.url());
357 urls
.prepend(typedUrl
.url());
358 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
361 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
362 // synchronize the result in the path box.
363 m_pathBox
->setUrl(url());
366 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
371 void UrlNavigator::slotRemoteHostActivated()
375 QString host
= m_host
->text();
378 int marker
= host
.indexOf("@");
381 user
= host
.left(marker
);
383 host
= host
.right(host
.length() - marker
- 1);
386 marker
= host
.indexOf("/");
389 u
.setPath(host
.right(host
.length() - marker
));
390 host
.truncate(marker
);
397 if (m_protocols
->currentProtocol() != u
.protocol() ||
401 u
.setProtocol(m_protocols
->currentProtocol());
402 u
.setHost(m_host
->text());
404 //TODO: get rid of this HACK for file:///!
405 if (u
.protocol() == "file")
408 if (u
.path().isEmpty())
418 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
421 url
.setProtocol(protocol
);
422 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
424 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
425 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
426 while (it
!= itEnd
) {
428 (*it
)->deleteLater();
431 m_navButtons
.clear();
433 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
438 m_protocolSeparator
= new QLabel("://", this);
439 appendWidget(m_protocolSeparator
);
440 m_host
= new QLineEdit(this);
441 appendWidget(m_host
);
443 connect(m_host
, SIGNAL(lostFocus()),
444 this, SLOT(slotRemoteHostActivated()));
445 connect(m_host
, SIGNAL(returnPressed()),
446 this, SLOT(slotRemoteHostActivated()));
451 m_protocolSeparator
->show();
457 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
459 // kDebug() << "received redirection to " << newUrl << endl;
460 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
461 /* UrlStack::iterator it = m_urls.find(oldUrl);
462 if (it != m_urls.end())
464 m_urls.erase(++it, m_urls.end());
467 m_urls.append(newUrl);*/
470 void UrlNavigator::switchView()
473 if (isUrlEditable()) {
474 m_pathBox
->setFocus();
477 setUrl(m_pathBox
->currentText());
479 emit
requestActivation();
482 void UrlNavigator::updateHistoryElem()
484 assert(m_historyIndex
>= 0);
485 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
487 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
488 (*it
).setCurrentFileName(item
->name());
492 void UrlNavigator::updateContent()
494 m_bookmarkSelector
->updateSelection(url());
496 m_toggleButton
->setToolTip(QString());
497 QString
path(url().pathOrUrl());
499 // TODO: prevent accessing the DolphinMainWindow out from this scope
500 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
501 // TODO: registry of default shortcuts
502 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
503 const QString shortcut
= "Ctrl+L";
505 if (m_toggleButton
->isChecked()) {
506 delete m_protocols
; m_protocols
= 0;
507 delete m_protocolSeparator
; m_protocolSeparator
= 0;
508 delete m_host
; m_host
= 0;
512 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
514 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
516 m_pathBox
->setUrl(url());
519 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
521 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
525 // get the data from the currently selected bookmark
526 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
527 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
529 QString bookmarkPath
;
530 if (bookmark
.isNull()) {
531 // No bookmark is a part of the current Url.
532 // The following code tries to guess the bookmark
533 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
534 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
535 // navigation indication 'Custom Path > var > lib".
536 int idx
= path
.indexOf(QString("//"));
537 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
538 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
541 bookmarkPath
= bookmark
.url().pathOrUrl();
543 const uint len
= bookmarkPath
.length();
545 // calculate the start point for the URL navigator buttons by counting
546 // the slashs inside the bookmark URL
548 for (uint i
= 0; i
< len
; ++i
) {
549 if (bookmarkPath
.at(i
) == QChar('/')) {
553 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
554 assert(slashCount
> 0);
558 if (!url().isLocalFile() && bookmark
.isNull()) {
559 QString protocol
= url().protocol();
562 m_protocols
= new ProtocolCombo(protocol
, this);
563 appendWidget(m_protocols
);
564 connect(m_protocols
, SIGNAL(activated(const QString
&)),
565 this, SLOT(slotProtocolChanged(const QString
&)));
568 m_protocols
->setProtocol(protocol
);
572 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
573 QString hostText
= url().host();
575 if (!url().user().isEmpty()) {
576 hostText
= url().user() + '@' + hostText
;
580 m_protocolSeparator
= new QLabel("://", this);
581 appendWidget(m_protocolSeparator
);
582 m_host
= new QLineEdit(hostText
, this);
583 appendWidget(m_host
);
585 connect(m_host
, SIGNAL(lostFocus()),
586 this, SLOT(slotRemoteHostActivated()));
587 connect(m_host
, SIGNAL(returnPressed()),
588 this, SLOT(slotRemoteHostActivated()));
591 m_host
->setText(hostText
);
593 m_protocolSeparator
->show();
597 delete m_protocolSeparator
; m_protocolSeparator
= 0;
598 delete m_host
; m_host
= 0;
601 else if (m_protocols
) {
605 m_protocolSeparator
->hide();
610 updateButtons(path
, slashCount
);
614 void UrlNavigator::updateButtons(const QString
& path
, int startIndex
)
616 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
617 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
618 bool createButton
= false;
620 int idx
= startIndex
;
623 createButton
= (it
== itEnd
);
625 const QString dirName
= path
.section('/', idx
, idx
);
626 const bool isFirstButton
= (idx
== startIndex
);
627 hasNext
= isFirstButton
|| !dirName
.isEmpty();
631 // the first URL navigator button should get the name of the
632 // bookmark instead of the directory name
633 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
634 text
= bookmark
.text();
635 if (text
.isEmpty()) {
636 if (url().isLocalFile()) {
637 text
= i18n("Custom Path");
646 UrlNavigatorButton
* button
= 0;
648 button
= new UrlNavigatorButton(idx
, this);
649 appendWidget(button
);
653 button
->setIndex(idx
);
657 button
->setText(text
);
662 m_navButtons
.append(button
);
671 // delete buttons which are not used anymore
672 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
673 while (it
!= itEnd
) {
675 (*it
)->deleteLater();
678 m_navButtons
.erase(itBegin
, m_navButtons
.end());
681 void UrlNavigator::deleteButtons()
683 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
684 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
685 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
686 while (it
!= itEnd
) {
688 (*it
)->deleteLater();
691 m_navButtons
.erase(itBegin
, itEnd
);
694 void UrlNavigator::appendWidget(QWidget
* widget
)
696 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
699 #include "urlnavigator.moc"