]>
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("editinput")); // 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());
250 // TODO: a patch has been submitted by Filip Brcic which adjusts
251 // the URL for tar and zip files. See https://bugs.kde.org/show_bug.cgi?id=142781
252 // for details. The URL navigator part of the patch has not been committed yet,
253 // as the URL navigator will be subject of change and
254 // we might think of a more generic approach to check the protocol + MIME type for
257 //kDebug() << "setUrl(" << url << ")" << endl;
258 if ( urlStr
.length() > 0 && urlStr
.at(0) == '~') {
259 // replace '~' by the home directory
261 urlStr
.insert(0, QDir::home().path());
264 const KUrl
transformedUrl(urlStr
);
266 if (m_historyIndex
> 0) {
267 // Check whether the previous element of the history has the same Url.
268 // If yes, just go forward instead of inserting a duplicate history
270 QLinkedList
<HistoryElem
>::const_iterator it
= m_history
.begin();
271 it
+= m_historyIndex
- 1;
272 const KUrl
& nextUrl
= (*it
).url();
273 if (transformedUrl
== nextUrl
) {
275 // kDebug() << "goin' forward in history" << endl;
280 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
281 const KUrl
& currUrl
= (*it
).url();
282 if (currUrl
== transformedUrl
) {
283 // don't insert duplicate history elements
284 // kDebug() << "currUrl == transformedUrl" << endl;
289 m_history
.insert(it
, HistoryElem(transformedUrl
));
293 emit
urlChanged(transformedUrl
);
294 emit
historyChanged();
296 // Prevent an endless growing of the history: remembering
297 // the last 100 Urls should be enough...
298 if (m_historyIndex
> 100) {
299 m_history
.erase(m_history
.begin());
303 /* kDebug() << "history starting ====================" << endl;
305 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
306 it != m_history.end();
309 kDebug() << i << ": " << (*it).url() << endl;
311 kDebug() << "history done ========================" << endl;*/
316 void UrlNavigator::requestActivation()
321 void UrlNavigator::storeContentsPosition(int x
, int y
)
323 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
324 (*it
).setContentsX(x
);
325 (*it
).setContentsY(y
);
328 void UrlNavigator::keyReleaseEvent(QKeyEvent
* event
)
330 QWidget::keyReleaseEvent(event
);
331 if (isUrlEditable() && (event
->key() == Qt::Key_Escape
)) {
332 setUrlEditable(false);
336 void UrlNavigator::mouseReleaseEvent(QMouseEvent
* event
)
338 if (event
->button() == Qt::MidButton
) {
339 QClipboard
* clipboard
= QApplication::clipboard();
340 const QMimeData
* mimeData
= clipboard
->mimeData();
341 if (mimeData
->hasText()) {
342 const QString text
= mimeData
->text();
346 QWidget::mouseReleaseEvent(event
);
349 void UrlNavigator::slotReturnPressed(const QString
& text
)
351 // Parts of the following code have been taken
352 // from the class KateFileSelector located in
353 // kate/app/katefileselector.hpp of Kate.
354 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
355 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
356 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
359 if (typedUrl
.hasPass()) {
360 typedUrl
.setPass(QString());
363 QStringList urls
= m_pathBox
->urls();
364 urls
.removeAll(typedUrl
.url());
365 urls
.prepend(typedUrl
.url());
366 m_pathBox
->setUrls(urls
, KUrlComboBox::RemoveBottom
);
369 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
370 // synchronize the result in the path box.
371 m_pathBox
->setUrl(url());
374 void UrlNavigator::slotUrlActivated(const KUrl
& url
)
379 void UrlNavigator::slotRemoteHostActivated()
383 QString host
= m_host
->text();
386 int marker
= host
.indexOf("@");
389 user
= host
.left(marker
);
391 host
= host
.right(host
.length() - marker
- 1);
394 marker
= host
.indexOf("/");
397 u
.setPath(host
.right(host
.length() - marker
));
398 host
.truncate(marker
);
405 if (m_protocols
->currentProtocol() != u
.protocol() ||
409 u
.setProtocol(m_protocols
->currentProtocol());
410 u
.setHost(m_host
->text());
412 //TODO: get rid of this HACK for file:///!
413 if (u
.protocol() == "file")
416 if (u
.path().isEmpty())
426 void UrlNavigator::slotProtocolChanged(const QString
& protocol
)
429 url
.setProtocol(protocol
);
430 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
432 QLinkedList
<UrlNavigatorButton
*>::const_iterator it
= m_navButtons
.begin();
433 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
434 while (it
!= itEnd
) {
436 (*it
)->deleteLater();
439 m_navButtons
.clear();
441 if (KProtocolInfo::protocolClass(protocol
) == ":local") {
446 m_protocolSeparator
= new QLabel("://", this);
447 appendWidget(m_protocolSeparator
);
448 m_host
= new QLineEdit(this);
449 appendWidget(m_host
);
451 connect(m_host
, SIGNAL(lostFocus()),
452 this, SLOT(slotRemoteHostActivated()));
453 connect(m_host
, SIGNAL(returnPressed()),
454 this, SLOT(slotRemoteHostActivated()));
459 m_protocolSeparator
->show();
465 void UrlNavigator::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
467 // kDebug() << "received redirection to " << newUrl << endl;
468 kDebug() << "received redirection from " << oldUrl
<< " to " << newUrl
<< endl
;
469 /* UrlStack::iterator it = m_urls.find(oldUrl);
470 if (it != m_urls.end())
472 m_urls.erase(++it, m_urls.end());
475 m_urls.append(newUrl);*/
478 void UrlNavigator::switchView()
481 if (isUrlEditable()) {
482 m_pathBox
->setFocus();
485 setUrl(m_pathBox
->currentText());
487 emit
requestActivation();
490 void UrlNavigator::updateHistoryElem()
492 assert(m_historyIndex
>= 0);
493 const KFileItem
* item
= 0; // TODO: m_dolphinView->currentFileItem();
495 QLinkedList
<HistoryElem
>::iterator it
= m_history
.begin() + m_historyIndex
;
496 (*it
).setCurrentFileName(item
->name());
500 void UrlNavigator::updateContent()
502 m_bookmarkSelector
->updateSelection(url());
504 m_toggleButton
->setToolTip(QString());
505 QString
path(url().pathOrUrl());
507 // TODO: prevent accessing the DolphinMainWindow out from this scope
508 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
509 // TODO: registry of default shortcuts
510 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
511 const QString shortcut
= "Ctrl+L";
513 if (m_toggleButton
->isChecked()) {
514 delete m_protocols
; m_protocols
= 0;
515 delete m_protocolSeparator
; m_protocolSeparator
= 0;
516 delete m_host
; m_host
= 0;
520 m_toggleButton
->setToolTip(i18n("Browse (%1, Escape)", shortcut
));
522 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
524 m_pathBox
->setUrl(url());
527 m_toggleButton
->setToolTip(i18n("Edit location (%1)", shortcut
));
529 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
533 // get the data from the currently selected bookmark
534 KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
535 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
537 QString bookmarkPath
;
538 if (bookmark
.isNull()) {
539 // No bookmark is a part of the current Url.
540 // The following code tries to guess the bookmark
541 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
542 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
543 // navigation indication 'Custom Path > var > lib".
544 int idx
= path
.indexOf(QString("//"));
545 idx
= path
.indexOf("/", (idx
< 0) ? 0 : idx
+ 2);
546 bookmarkPath
= (idx
< 0) ? path
: path
.left(idx
);
549 bookmarkPath
= bookmark
.url().pathOrUrl();
551 const uint len
= bookmarkPath
.length();
553 // calculate the start point for the URL navigator buttons by counting
554 // the slashs inside the bookmark URL
556 for (uint i
= 0; i
< len
; ++i
) {
557 if (bookmarkPath
.at(i
) == QChar('/')) {
561 if ((len
> 0) && bookmarkPath
.at(len
- 1) == QChar('/')) {
562 assert(slashCount
> 0);
566 if (!url().isLocalFile() && bookmark
.isNull()) {
567 QString protocol
= url().protocol();
570 m_protocols
= new ProtocolCombo(protocol
, this);
571 appendWidget(m_protocols
);
572 connect(m_protocols
, SIGNAL(activated(const QString
&)),
573 this, SLOT(slotProtocolChanged(const QString
&)));
576 m_protocols
->setProtocol(protocol
);
580 if (KProtocolInfo::protocolClass(protocol
) != ":local") {
581 QString hostText
= url().host();
583 if (!url().user().isEmpty()) {
584 hostText
= url().user() + '@' + hostText
;
588 m_protocolSeparator
= new QLabel("://", this);
589 appendWidget(m_protocolSeparator
);
590 m_host
= new QLineEdit(hostText
, this);
591 appendWidget(m_host
);
593 connect(m_host
, SIGNAL(lostFocus()),
594 this, SLOT(slotRemoteHostActivated()));
595 connect(m_host
, SIGNAL(returnPressed()),
596 this, SLOT(slotRemoteHostActivated()));
599 m_host
->setText(hostText
);
601 m_protocolSeparator
->show();
605 delete m_protocolSeparator
; m_protocolSeparator
= 0;
606 delete m_host
; m_host
= 0;
609 else if (m_protocols
) {
613 m_protocolSeparator
->hide();
618 updateButtons(path
, slashCount
);
622 void UrlNavigator::updateButtons(const QString
& path
, int startIndex
)
624 QLinkedList
<UrlNavigatorButton
*>::iterator it
= m_navButtons
.begin();
625 const QLinkedList
<UrlNavigatorButton
*>::const_iterator itEnd
= m_navButtons
.end();
626 bool createButton
= false;
628 int idx
= startIndex
;
631 createButton
= (it
== itEnd
);
633 const QString dirName
= path
.section('/', idx
, idx
);
634 const bool isFirstButton
= (idx
== startIndex
);
635 hasNext
= isFirstButton
|| !dirName
.isEmpty();
639 // the first URL navigator button should get the name of the
640 // bookmark instead of the directory name
641 const KBookmark bookmark
= m_bookmarkSelector
->selectedBookmark();
642 text
= bookmark
.text();
643 if (text
.isEmpty()) {
644 if (url().isLocalFile()) {
645 text
= i18n("Custom Path");
654 UrlNavigatorButton
* button
= 0;
656 button
= new UrlNavigatorButton(idx
, this);
657 appendWidget(button
);
661 button
->setIndex(idx
);
665 button
->setText(text
);
670 m_navButtons
.append(button
);
679 // delete buttons which are not used anymore
680 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= it
;
681 while (it
!= itEnd
) {
683 (*it
)->deleteLater();
686 m_navButtons
.erase(itBegin
, m_navButtons
.end());
689 void UrlNavigator::deleteButtons()
691 QLinkedList
<UrlNavigatorButton
*>::iterator itBegin
= m_navButtons
.begin();
692 QLinkedList
<UrlNavigatorButton
*>::iterator itEnd
= m_navButtons
.end();
693 QLinkedList
<UrlNavigatorButton
*>::iterator it
= itBegin
;
694 while (it
!= itEnd
) {
696 (*it
)->deleteLater();
699 m_navButtons
.erase(itBegin
, itEnd
);
702 void UrlNavigator::appendWidget(QWidget
* widget
)
704 m_layout
->insertWidget(m_layout
->count() - 1, widget
);
707 #include "urlnavigator.moc"