]> cloud.milkyroute.net Git - dolphin.git/blob - src/urlnavigator.cpp
cleanup of unused forward declarations
[dolphin.git] / 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 *
5 * *
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. *
10 * *
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. *
15 * *
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 ***************************************************************************/
21
22 #include "urlnavigator.h"
23
24 #include "bookmarkselector.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "protocolcombo.h"
28 #include "urlnavigatorbutton.h"
29
30 #include <assert.h>
31
32 #include <kfileitem.h>
33 #include <kicon.h>
34 #include <klocale.h>
35 #include <kprotocolinfo.h>
36 #include <kurlcombobox.h>
37 #include <kurlcompletion.h>
38
39 #include <QApplication>
40 #include <QClipboard>
41 #include <QDir>
42 #include <QHBoxLayout>
43 #include <QLabel>
44 #include <QLineEdit>
45 #include <QMouseEvent>
46 #include <QToolButton>
47
48 UrlNavigator::HistoryElem::HistoryElem() :
49 m_url(),
50 m_currentFileName(),
51 m_contentsX(0),
52 m_contentsY(0)
53 {
54 }
55
56 UrlNavigator::HistoryElem::HistoryElem(const KUrl& url) :
57 m_url(url),
58 m_currentFileName(),
59 m_contentsX(0),
60 m_contentsY(0)
61 {
62 }
63
64 UrlNavigator::HistoryElem::~HistoryElem()
65 {
66 }
67
68 UrlNavigator::UrlNavigator(const KUrl& url,
69 QWidget* parent) :
70 QWidget(parent),
71 m_active(true),
72 m_showHiddenFiles(false),
73 m_historyIndex(0),
74 m_layout(0),
75 m_protocols(0),
76 m_protocolSeparator(0),
77 m_host(0),
78 m_filler(0)
79 {
80 m_layout = new QHBoxLayout();
81 m_layout->setSpacing(0);
82 m_layout->setMargin(0);
83
84 m_history.prepend(HistoryElem(url));
85
86 QFontMetrics fontMetrics(font());
87 setMinimumHeight(fontMetrics.height() + 10);
88
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();
101 }
102
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&)));
107
108 // initialize the path box of the traditional view
109 m_pathBox = new KUrlComboBox(KUrlComboBox::Directories, true, this);
110
111 KUrlCompletion* kurlCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
112 m_pathBox->setCompletionObject(kurlCompletion);
113 m_pathBox->setAutoDeleteCompletionObject(true);
114
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&)));
119
120 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
121 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
122
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);
128
129 m_layout->addWidget(m_toggleButton);
130 m_layout->addWidget(m_bookmarkSelector);
131 m_layout->addWidget(m_pathBox);
132 m_layout->addWidget(m_filler);
133 setLayout(m_layout);
134
135 updateContent();
136 }
137
138 UrlNavigator::~UrlNavigator()
139 {
140 }
141
142 const KUrl& UrlNavigator::url() const
143 {
144 assert(!m_history.empty());
145 QLinkedList<HistoryElem>::const_iterator it = m_history.begin();
146 it += m_historyIndex;
147 return (*it).url();
148 }
149
150 KUrl UrlNavigator::url(int index) const
151 {
152 assert(index >= 0);
153 QString path(url().pathOrUrl());
154 path = path.section('/', 0, index);
155
156 if ( path.length() >= 1 && path.at(path.length()-1) != '/')
157 {
158 path.append('/');
159 }
160
161 return path;
162 }
163
164 const QLinkedList<UrlNavigator::HistoryElem>& UrlNavigator::history(int& index) const
165 {
166 index = m_historyIndex;
167 return m_history;
168 }
169
170 void UrlNavigator::goBack()
171 {
172 updateHistoryElem();
173
174 const int count = m_history.count();
175 if (m_historyIndex < count - 1) {
176 ++m_historyIndex;
177 updateContent();
178 emit urlChanged(url());
179 emit historyChanged();
180 }
181 }
182
183 void UrlNavigator::goForward()
184 {
185 if (m_historyIndex > 0) {
186 --m_historyIndex;
187 updateContent();
188 emit urlChanged(url());
189 emit historyChanged();
190 }
191 }
192
193 void UrlNavigator::goUp()
194 {
195 setUrl(url().upUrl());
196 }
197
198 void UrlNavigator::goHome()
199 {
200 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
201 }
202
203 void UrlNavigator::setUrlEditable(bool editable)
204 {
205 if (isUrlEditable() != editable) {
206 m_toggleButton->toggle();
207 switchView();
208 }
209 }
210
211 bool UrlNavigator::isUrlEditable() const
212 {
213 return m_toggleButton->isChecked();
214 }
215
216 void UrlNavigator::editUrl(bool editOrBrowse)
217 {
218 setUrlEditable(editOrBrowse);
219 if (editOrBrowse) {
220 m_pathBox->setFocus();
221 }
222 }
223
224 void UrlNavigator::setActive(bool active)
225 {
226 if (active != m_active) {
227 m_active = active;
228 update();
229 if (active) {
230 emit activated();
231 }
232 }
233 }
234
235 void UrlNavigator::setShowHiddenFiles( bool show )
236 {
237 m_showHiddenFiles = show;
238 }
239
240 void UrlNavigator::dropUrls(const KUrl::List& urls,
241 const KUrl& destination)
242 {
243 emit urlsDropped(urls, destination);
244 }
245
246 void UrlNavigator::setUrl(const KUrl& url)
247 {
248 QString urlStr(url.pathOrUrl());
249
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
255 // this use case.
256
257 //kDebug() << "setUrl(" << url << ")" << endl;
258 if ( urlStr.length() > 0 && urlStr.at(0) == '~') {
259 // replace '~' by the home directory
260 urlStr.remove(0, 1);
261 urlStr.insert(0, QDir::home().path());
262 }
263
264 const KUrl transformedUrl(urlStr);
265
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
269 // element.
270 QLinkedList<HistoryElem>::const_iterator it = m_history.begin();
271 it += m_historyIndex - 1;
272 const KUrl& nextUrl = (*it).url();
273 if (transformedUrl == nextUrl) {
274 goForward();
275 // kDebug() << "goin' forward in history" << endl;
276 return;
277 }
278 }
279
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;
285 return;
286 }
287
288 updateHistoryElem();
289 m_history.insert(it, HistoryElem(transformedUrl));
290
291 updateContent();
292
293 emit urlChanged(transformedUrl);
294 emit historyChanged();
295
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());
300 --m_historyIndex;
301 }
302
303 /* kDebug() << "history starting ====================" << endl;
304 int i = 0;
305 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
306 it != m_history.end();
307 ++it, ++i)
308 {
309 kDebug() << i << ": " << (*it).url() << endl;
310 }
311 kDebug() << "history done ========================" << endl;*/
312
313 requestActivation();
314 }
315
316 void UrlNavigator::requestActivation()
317 {
318 setActive(true);
319 }
320
321 void UrlNavigator::storeContentsPosition(int x, int y)
322 {
323 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
324 (*it).setContentsX(x);
325 (*it).setContentsY(y);
326 }
327
328 void UrlNavigator::keyReleaseEvent(QKeyEvent* event)
329 {
330 QWidget::keyReleaseEvent(event);
331 if (isUrlEditable() && (event->key() == Qt::Key_Escape)) {
332 setUrlEditable(false);
333 }
334 }
335
336 void UrlNavigator::mouseReleaseEvent(QMouseEvent* event)
337 {
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();
343 setUrl(KUrl(text));
344 }
345 }
346 QWidget::mouseReleaseEvent(event);
347 }
348
349 void UrlNavigator::slotReturnPressed(const QString& text)
350 {
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>
357
358 KUrl typedUrl(text);
359 if (typedUrl.hasPass()) {
360 typedUrl.setPass(QString());
361 }
362
363 QStringList urls = m_pathBox->urls();
364 urls.removeAll(typedUrl.url());
365 urls.prepend(typedUrl.url());
366 m_pathBox->setUrls(urls, KUrlComboBox::RemoveBottom);
367
368 setUrl(typedUrl);
369 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
370 // synchronize the result in the path box.
371 m_pathBox->setUrl(url());
372 }
373
374 void UrlNavigator::slotUrlActivated(const KUrl& url)
375 {
376 setUrl(url);
377 }
378
379 void UrlNavigator::slotRemoteHostActivated()
380 {
381 KUrl u = url();
382
383 QString host = m_host->text();
384 QString user;
385
386 int marker = host.indexOf("@");
387 if (marker != -1)
388 {
389 user = host.left(marker);
390 u.setUser(user);
391 host = host.right(host.length() - marker - 1);
392 }
393
394 marker = host.indexOf("/");
395 if (marker != -1)
396 {
397 u.setPath(host.right(host.length() - marker));
398 host.truncate(marker);
399 }
400 else
401 {
402 u.setPath("");
403 }
404
405 if (m_protocols->currentProtocol() != u.protocol() ||
406 host != u.host() ||
407 user != u.user())
408 {
409 u.setProtocol(m_protocols->currentProtocol());
410 u.setHost(m_host->text());
411
412 //TODO: get rid of this HACK for file:///!
413 if (u.protocol() == "file")
414 {
415 u.setHost("");
416 if (u.path().isEmpty())
417 {
418 u.setPath("/");
419 }
420 }
421
422 setUrl(u);
423 }
424 }
425
426 void UrlNavigator::slotProtocolChanged(const QString& protocol)
427 {
428 KUrl url;
429 url.setProtocol(protocol);
430 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
431 url.setPath("/");
432 QLinkedList<UrlNavigatorButton*>::const_iterator it = m_navButtons.begin();
433 const QLinkedList<UrlNavigatorButton*>::const_iterator itEnd = m_navButtons.end();
434 while (it != itEnd) {
435 (*it)->close();
436 (*it)->deleteLater();
437 ++it;
438 }
439 m_navButtons.clear();
440
441 if (KProtocolInfo::protocolClass(protocol) == ":local") {
442 setUrl(url);
443 }
444 else {
445 if (!m_host) {
446 m_protocolSeparator = new QLabel("://", this);
447 appendWidget(m_protocolSeparator);
448 m_host = new QLineEdit(this);
449 appendWidget(m_host);
450
451 connect(m_host, SIGNAL(lostFocus()),
452 this, SLOT(slotRemoteHostActivated()));
453 connect(m_host, SIGNAL(returnPressed()),
454 this, SLOT(slotRemoteHostActivated()));
455 }
456 else {
457 m_host->setText("");
458 }
459 m_protocolSeparator->show();
460 m_host->show();
461 m_host->setFocus();
462 }
463 }
464
465 void UrlNavigator::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
466 {
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())
471 {
472 m_urls.erase(++it, m_urls.end());
473 }
474
475 m_urls.append(newUrl);*/
476 }
477
478 void UrlNavigator::switchView()
479 {
480 updateContent();
481 if (isUrlEditable()) {
482 m_pathBox->setFocus();
483 }
484 else {
485 setUrl(m_pathBox->currentText());
486 }
487 emit requestActivation();
488 }
489
490 void UrlNavigator::updateHistoryElem()
491 {
492 assert(m_historyIndex >= 0);
493 const KFileItem* item = 0; // TODO: m_dolphinView->currentFileItem();
494 if (item != 0) {
495 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
496 (*it).setCurrentFileName(item->name());
497 }
498 }
499
500 void UrlNavigator::updateContent()
501 {
502 m_bookmarkSelector->updateSelection(url());
503
504 m_toggleButton->setToolTip(QString());
505 QString path(url().pathOrUrl());
506
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";
512
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;
517 deleteButtons();
518 m_filler->hide();
519
520 m_toggleButton->setToolTip(i18n("Browse (%1, Escape)", shortcut));
521
522 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
523 m_pathBox->show();
524 m_pathBox->setUrl(url());
525 }
526 else {
527 m_toggleButton->setToolTip(i18n("Edit location (%1)", shortcut));
528
529 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
530 m_pathBox->hide();
531 m_filler->show();
532
533 // get the data from the currently selected bookmark
534 KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
535 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
536
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);
547 }
548 else {
549 bookmarkPath = bookmark.url().pathOrUrl();
550 }
551 const uint len = bookmarkPath.length();
552
553 // calculate the start point for the URL navigator buttons by counting
554 // the slashs inside the bookmark URL
555 int slashCount = 0;
556 for (uint i = 0; i < len; ++i) {
557 if (bookmarkPath.at(i) == QChar('/')) {
558 ++slashCount;
559 }
560 }
561 if ((len > 0) && bookmarkPath.at(len - 1) == QChar('/')) {
562 assert(slashCount > 0);
563 --slashCount;
564 }
565
566 if (!url().isLocalFile() && bookmark.isNull()) {
567 QString protocol = url().protocol();
568 if (!m_protocols) {
569 deleteButtons();
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&)));
574 }
575 else {
576 m_protocols->setProtocol(protocol);
577 }
578 m_protocols->show();
579
580 if (KProtocolInfo::protocolClass(protocol) != ":local") {
581 QString hostText = url().host();
582
583 if (!url().user().isEmpty()) {
584 hostText = url().user() + '@' + hostText;
585 }
586
587 if (!m_host) {
588 m_protocolSeparator = new QLabel("://", this);
589 appendWidget(m_protocolSeparator);
590 m_host = new QLineEdit(hostText, this);
591 appendWidget(m_host);
592
593 connect(m_host, SIGNAL(lostFocus()),
594 this, SLOT(slotRemoteHostActivated()));
595 connect(m_host, SIGNAL(returnPressed()),
596 this, SLOT(slotRemoteHostActivated()));
597 }
598 else {
599 m_host->setText(hostText);
600 }
601 m_protocolSeparator->show();
602 m_host->show();
603 }
604 else {
605 delete m_protocolSeparator; m_protocolSeparator = 0;
606 delete m_host; m_host = 0;
607 }
608 }
609 else if (m_protocols) {
610 m_protocols->hide();
611
612 if (m_host) {
613 m_protocolSeparator->hide();
614 m_host->hide();
615 }
616 }
617
618 updateButtons(path, slashCount);
619 }
620 }
621
622 void UrlNavigator::updateButtons(const QString& path, int startIndex)
623 {
624 QLinkedList<UrlNavigatorButton*>::iterator it = m_navButtons.begin();
625 const QLinkedList<UrlNavigatorButton*>::const_iterator itEnd = m_navButtons.end();
626 bool createButton = false;
627
628 int idx = startIndex;
629 bool hasNext = true;
630 do {
631 createButton = (it == itEnd);
632
633 const QString dirName = path.section('/', idx, idx);
634 const bool isFirstButton = (idx == startIndex);
635 hasNext = isFirstButton || !dirName.isEmpty();
636 if (hasNext) {
637 QString text;
638 if (isFirstButton) {
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");
646 }
647 else {
648 ++idx;
649 continue;
650 }
651 }
652 }
653
654 UrlNavigatorButton* button = 0;
655 if (createButton) {
656 button = new UrlNavigatorButton(idx, this);
657 appendWidget(button);
658 }
659 else {
660 button = *it;
661 button->setIndex(idx);
662 }
663
664 if (isFirstButton) {
665 button->setText(text);
666 }
667
668 if (createButton) {
669 button->show();
670 m_navButtons.append(button);
671 }
672 else {
673 ++it;
674 }
675 ++idx;
676 }
677 } while (hasNext);
678
679 // delete buttons which are not used anymore
680 QLinkedList<UrlNavigatorButton*>::iterator itBegin = it;
681 while (it != itEnd) {
682 (*it)->close();
683 (*it)->deleteLater();
684 ++it;
685 }
686 m_navButtons.erase(itBegin, m_navButtons.end());
687 }
688
689 void UrlNavigator::deleteButtons()
690 {
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) {
695 (*it)->close();
696 (*it)->deleteLater();
697 ++it;
698 }
699 m_navButtons.erase(itBegin, itEnd);
700 }
701
702 void UrlNavigator::appendWidget(QWidget* widget)
703 {
704 m_layout->insertWidget(m_layout->count() - 1, widget);
705 }
706
707 #include "urlnavigator.moc"