]> cloud.milkyroute.net Git - dolphin.git/blob - src/urlnavigator.cpp
Prevent that the popup of the URL navigator is opened if the button has already been...
[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 "generalsettings.h"
27 #include "protocolcombo.h"
28 #include "urlnavigatorbutton.h"
29
30 #include <assert.h>
31
32 #include <kfileitem.h>
33 #include <klocale.h>
34 #include <kprotocolinfo.h>
35 #include <kurlcombobox.h>
36 #include <kurlcompletion.h>
37
38 #include <QDir>
39 #include <QCheckBox>
40 #include <QLabel>
41 #include <QLineEdit>
42
43 UrlNavigator::HistoryElem::HistoryElem() :
44 m_url(),
45 m_currentFileName(),
46 m_contentsX(0),
47 m_contentsY(0)
48 {
49 }
50
51 UrlNavigator::HistoryElem::HistoryElem(const KUrl& url) :
52 m_url(url),
53 m_currentFileName(),
54 m_contentsX(0),
55 m_contentsY(0)
56 {
57 }
58
59 UrlNavigator::HistoryElem::~HistoryElem()
60 {
61 }
62
63 UrlNavigator::UrlNavigator(const KUrl& url,
64 QWidget* parent) :
65 KHBox(parent),
66 m_active(true),
67 m_historyIndex(0),
68 m_protocols(0),
69 m_protocolSeparator(0),
70 m_host(0)
71 {
72 m_history.prepend(HistoryElem(url));
73
74 QFontMetrics fontMetrics(font());
75 setMinimumHeight(fontMetrics.height() + 8);
76
77 m_toggleButton = new QCheckBox(this);
78 //m_toggleButton->setFlat(true);
79 //m_toggleButton->setToggleButton(true);
80 m_toggleButton->setFocusPolicy(Qt::NoFocus);
81 m_toggleButton->setMinimumHeight(minimumHeight());
82 connect(m_toggleButton, SIGNAL(clicked()),
83 this, SLOT(slotClicked()));
84 if (DolphinSettings::instance().generalSettings()->editableUrl()) {
85 m_toggleButton->toggle();
86 }
87
88 m_bookmarkSelector = new BookmarkSelector(this);
89 connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(const KUrl&)),
90 this, SLOT(setUrl(const KUrl&)));
91
92 m_pathBox = new KUrlComboBox(KUrlComboBox::Directories, true, this);
93
94 KUrlCompletion* kurlCompletion = new KUrlCompletion(KUrlCompletion::DirCompletion);
95 m_pathBox->setCompletionObject(kurlCompletion);
96 m_pathBox->setAutoDeleteCompletionObject(true);
97
98 connect(m_pathBox, SIGNAL(returnPressed(const QString&)),
99 this, SLOT(slotReturnPressed(const QString&)));
100 connect(m_pathBox, SIGNAL(urlActivated(const KUrl&)),
101 this, SLOT(slotUrlActivated(const KUrl&)));
102
103 //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)),
104 // this, SLOT(slotRedirection(const KUrl&, const KUrl&)));
105 updateContent();
106 }
107
108 UrlNavigator::~UrlNavigator()
109 {
110 }
111
112 const KUrl& UrlNavigator::url() const
113 {
114 assert(!m_history.empty());
115 QLinkedList<HistoryElem>::const_iterator it = m_history.begin();
116 it += m_historyIndex;
117 return (*it).url();
118 }
119
120 KUrl UrlNavigator::url(int index) const
121 {
122 assert(index >= 0);
123 QString path(url().pathOrUrl());
124 path = path.section('/', 0, index);
125
126 if ( path.length() >= 1 && path.at(path.length()-1) != '/')
127 {
128 path.append('/');
129 }
130
131 return path;
132 }
133
134 const QLinkedList<UrlNavigator::HistoryElem>& UrlNavigator::history(int& index) const
135 {
136 index = m_historyIndex;
137 return m_history;
138 }
139
140 void UrlNavigator::goBack()
141 {
142 updateHistoryElem();
143
144 const int count = m_history.count();
145 if (m_historyIndex < count - 1) {
146 ++m_historyIndex;
147 updateContent();
148 emit urlChanged(url());
149 emit historyChanged();
150 }
151 }
152
153 void UrlNavigator::goForward()
154 {
155 if (m_historyIndex > 0) {
156 --m_historyIndex;
157 updateContent();
158 emit urlChanged(url());
159 emit historyChanged();
160 }
161 }
162
163 void UrlNavigator::goUp()
164 {
165 setUrl(url().upUrl());
166 }
167
168 void UrlNavigator::goHome()
169 {
170 setUrl(DolphinSettings::instance().generalSettings()->homeUrl());
171 }
172
173 void UrlNavigator::setUrlEditable(bool editable)
174 {
175 if (isUrlEditable() != editable) {
176 m_toggleButton->toggle();
177 slotClicked();
178 }
179 }
180
181 bool UrlNavigator::isUrlEditable() const
182 {
183 return m_toggleButton->isChecked();
184 }
185
186 void UrlNavigator::editUrl(bool editOrBrowse)
187 {
188 setUrlEditable(editOrBrowse);
189 if (editOrBrowse) {
190 m_pathBox->setFocus();
191 }
192 }
193
194 void UrlNavigator::setActive(bool active)
195 {
196 if (active != m_active) {
197 m_active = active;
198 update();
199 if (active) {
200 emit activated();
201 }
202 }
203 }
204
205 void UrlNavigator::dropUrls(const KUrl::List& urls,
206 const KUrl& destination)
207 {
208 emit urlsDropped(urls, destination);
209 }
210
211 void UrlNavigator::setUrl(const KUrl& url)
212 {
213 QString urlStr(url.pathOrUrl());
214 //kDebug() << "setUrl(" << url << ")" << endl;
215 if ( urlStr.length() > 0 && urlStr.at(0) == '~') {
216 // replace '~' by the home directory
217 urlStr.remove(0, 1);
218 urlStr.insert(0, QDir::home().path());
219 }
220
221 const KUrl transformedUrl(urlStr);
222
223 if (m_historyIndex > 0) {
224 // Check whether the previous element of the history has the same Url.
225 // If yes, just go forward instead of inserting a duplicate history
226 // element.
227 QLinkedList<HistoryElem>::const_iterator it = m_history.begin();
228 it += m_historyIndex - 1;
229 const KUrl& nextUrl = (*it).url();
230 if (transformedUrl == nextUrl) {
231 goForward();
232 // kDebug() << "goin' forward in history" << endl;
233 return;
234 }
235 }
236
237 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
238 const KUrl& currUrl = (*it).url();
239 if (currUrl == transformedUrl) {
240 // don't insert duplicate history elements
241 // kDebug() << "currUrl == transformedUrl" << endl;
242 return;
243 }
244
245 updateHistoryElem();
246 m_history.insert(it, HistoryElem(transformedUrl));
247
248 updateContent();
249
250 emit urlChanged(transformedUrl);
251 emit historyChanged();
252
253 // Prevent an endless growing of the history: remembering
254 // the last 100 Urls should be enough...
255 if (m_historyIndex > 100) {
256 m_history.erase(m_history.begin());
257 --m_historyIndex;
258 }
259
260 /* kDebug() << "history starting ====================" << endl;
261 int i = 0;
262 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
263 it != m_history.end();
264 ++it, ++i)
265 {
266 kDebug() << i << ": " << (*it).url() << endl;
267 }
268 kDebug() << "history done ========================" << endl;*/
269
270 requestActivation();
271 }
272
273 void UrlNavigator::requestActivation()
274 {
275 setActive(true);
276 }
277
278 void UrlNavigator::storeContentsPosition(int x, int y)
279 {
280 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
281 (*it).setContentsX(x);
282 (*it).setContentsY(y);
283 }
284
285 void UrlNavigator::keyReleaseEvent(QKeyEvent* event)
286 {
287 KHBox::keyReleaseEvent(event);
288 if (isUrlEditable() && (event->key() == Qt::Key_Escape)) {
289 setUrlEditable(false);
290 }
291 }
292
293 void UrlNavigator::slotReturnPressed(const QString& text)
294 {
295 // Parts of the following code have been taken
296 // from the class KateFileSelector located in
297 // kate/app/katefileselector.hpp of Kate.
298 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
299 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
300 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
301
302 KUrl typedUrl(text);
303 if (typedUrl.hasPass()) {
304 typedUrl.setPass(QString::null);
305 }
306
307 QStringList urls = m_pathBox->urls();
308 urls.removeAll(typedUrl.url());
309 urls.prepend(typedUrl.url());
310 m_pathBox->setUrls(urls, KUrlComboBox::RemoveBottom);
311
312 setUrl(typedUrl);
313 // The URL might have been adjusted by UrlNavigator::setUrl(), hence
314 // synchronize the result in the path box.
315 m_pathBox->setUrl(url());
316 }
317
318 void UrlNavigator::slotUrlActivated(const KUrl& url)
319 {
320 setUrl(url);
321 }
322
323 void UrlNavigator::slotRemoteHostActivated()
324 {
325 KUrl u = url();
326
327 QString host = m_host->text();
328 QString user;
329
330 int marker = host.indexOf("@");
331 if (marker != -1)
332 {
333 user = host.left(marker);
334 u.setUser(user);
335 host = host.right(host.length() - marker - 1);
336 }
337
338 marker = host.indexOf("/");
339 if (marker != -1)
340 {
341 u.setPath(host.right(host.length() - marker));
342 host.truncate(marker);
343 }
344 else
345 {
346 u.setPath("");
347 }
348
349 if (m_protocols->currentProtocol() != u.protocol() ||
350 host != u.host() ||
351 user != u.user())
352 {
353 u.setProtocol(m_protocols->currentProtocol());
354 u.setHost(m_host->text());
355
356 //TODO: get rid of this HACK for file:///!
357 if (u.protocol() == "file")
358 {
359 u.setHost("");
360 if (u.path().isEmpty())
361 {
362 u.setPath("/");
363 }
364 }
365
366 setUrl(u);
367 }
368 }
369
370 void UrlNavigator::slotProtocolChanged(const QString& protocol)
371 {
372 KUrl url;
373 url.setProtocol(protocol);
374 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
375 url.setPath("/");
376 QLinkedList<UrlNavigatorButton*>::const_iterator it = m_navButtons.begin();
377 const QLinkedList<UrlNavigatorButton*>::const_iterator itEnd = m_navButtons.end();
378 while (it != itEnd) {
379 (*it)->close();
380 (*it)->deleteLater();
381 ++it;
382 }
383 m_navButtons.clear();
384
385 if (KProtocolInfo::protocolClass(protocol) == ":local") {
386 setUrl(url);
387 }
388 else {
389 if (!m_host) {
390 m_protocolSeparator = new QLabel("://", this);
391 m_host = new QLineEdit(this);
392
393 connect(m_host, SIGNAL(lostFocus()),
394 this, SLOT(slotRemoteHostActivated()));
395 connect(m_host, SIGNAL(returnPressed()),
396 this, SLOT(slotRemoteHostActivated()));
397 }
398 else {
399 m_host->setText("");
400 }
401 m_protocolSeparator->show();
402 m_host->show();
403 m_host->setFocus();
404 }
405 }
406
407 void UrlNavigator::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
408 {
409 // kDebug() << "received redirection to " << newUrl << endl;
410 kDebug() << "received redirection from " << oldUrl << " to " << newUrl << endl;
411 /* UrlStack::iterator it = m_urls.find(oldUrl);
412 if (it != m_urls.end())
413 {
414 m_urls.erase(++it, m_urls.end());
415 }
416
417 m_urls.append(newUrl);*/
418 }
419
420 void UrlNavigator::slotClicked()
421 {
422 if (isUrlEditable()) {
423 m_pathBox->setFocus();
424 updateContent();
425 }
426 else {
427 setUrl(m_pathBox->currentText());
428 emit requestActivation();
429 }
430 }
431
432 void UrlNavigator::updateHistoryElem()
433 {
434 assert(m_historyIndex >= 0);
435 const KFileItem* item = 0; // TODO: m_dolphinView->currentFileItem();
436 if (item != 0) {
437 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
438 (*it).setCurrentFileName(item->name());
439 }
440 }
441
442 void UrlNavigator::updateContent()
443 {
444 m_bookmarkSelector->updateSelection(url());
445
446 m_toggleButton->setToolTip(QString());
447 QString path(url().pathOrUrl());
448
449 // TODO: prevent accessing the DolphinMainWindow out from this scope
450 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
451 // TODO: registry of default shortcuts
452 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
453 const QString shortcut = "Ctrl+L";
454
455 if (m_toggleButton->isChecked()) {
456 delete m_protocols; m_protocols = 0;
457 delete m_protocolSeparator; m_protocolSeparator = 0;
458 delete m_host; m_host = 0;
459 deleteButtons();
460
461 m_toggleButton->setToolTip(i18n("Browse (%1, Escape)", shortcut));
462
463 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
464 m_pathBox->show();
465 m_pathBox->setUrl(url());
466 }
467 else {
468 m_toggleButton->setToolTip(i18n("Edit location (%1)", shortcut));
469
470 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
471 m_pathBox->hide();
472
473 // get the data from the currently selected bookmark
474 KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
475 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
476
477 QString bookmarkPath;
478 if (bookmark.isNull()) {
479 // No bookmark is a part of the current Url.
480 // The following code tries to guess the bookmark
481 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
482 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
483 // navigation indication 'Custom Path > var > lib".
484 int idx = path.indexOf(QString("//"));
485 idx = path.indexOf("/", (idx < 0) ? 0 : idx + 2);
486 bookmarkPath = (idx < 0) ? path : path.left(idx);
487 }
488 else {
489 bookmarkPath = bookmark.url().pathOrUrl();
490 }
491 const uint len = bookmarkPath.length();
492
493 // calculate the start point for the URL navigator buttons by counting
494 // the slashs inside the bookmark URL
495 int slashCount = 0;
496 for (uint i = 0; i < len; ++i) {
497 if (bookmarkPath.at(i) == QChar('/')) {
498 ++slashCount;
499 }
500 }
501 if ((len > 0) && bookmarkPath.at(len - 1) == QChar('/')) {
502 assert(slashCount > 0);
503 --slashCount;
504 }
505
506 if (!url().isLocalFile() && bookmark.isNull()) {
507 QString protocol = url().protocol();
508 if (!m_protocols) {
509 deleteButtons();
510 m_protocols = new ProtocolCombo(protocol, this);
511 connect(m_protocols, SIGNAL(activated(const QString&)),
512 this, SLOT(slotProtocolChanged(const QString&)));
513 }
514 else {
515 m_protocols->setProtocol(protocol);
516 }
517 m_protocols->show();
518
519 if (KProtocolInfo::protocolClass(protocol) != ":local") {
520 QString hostText = url().host();
521
522 if (!url().user().isEmpty()) {
523 hostText = url().user() + "@" + hostText;
524 }
525
526 if (!m_host) {
527 m_protocolSeparator = new QLabel("://", this);
528 m_host = new QLineEdit(hostText, this);
529
530 connect(m_host, SIGNAL(lostFocus()),
531 this, SLOT(slotRemoteHostActivated()));
532 connect(m_host, SIGNAL(returnPressed()),
533 this, SLOT(slotRemoteHostActivated()));
534 }
535 else {
536 m_host->setText(hostText);
537 }
538 m_protocolSeparator->show();
539 m_host->show();
540 }
541 else {
542 delete m_protocolSeparator; m_protocolSeparator = 0;
543 delete m_host; m_host = 0;
544 }
545 }
546 else if (m_protocols) {
547 m_protocols->hide();
548
549 if (m_host) {
550 m_protocolSeparator->hide();
551 m_host->hide();
552 }
553 }
554
555 updateButtons(path, slashCount);
556 }
557 }
558
559 void UrlNavigator::updateButtons(const QString& path, int startIndex)
560 {
561 QLinkedList<UrlNavigatorButton*>::iterator it = m_navButtons.begin();
562 const QLinkedList<UrlNavigatorButton*>::const_iterator itEnd = m_navButtons.end();
563 bool createButton = false;
564
565 int idx = startIndex;
566 bool hasNext = true;
567 do {
568 createButton = (it == itEnd);
569
570 const QString dirName = path.section('/', idx, idx);
571 const bool isFirstButton = (idx == startIndex);
572 hasNext = isFirstButton || !dirName.isEmpty();
573 if (hasNext) {
574 QString text;
575 if (isFirstButton) {
576 // the first URL navigator button should get the name of the
577 // bookmark instead of the directory name
578 const KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
579 text = bookmark.text();
580 if (text.isEmpty()) {
581 if (url().isLocalFile()) {
582 text = i18n("Custom Path");
583 }
584 else {
585 ++idx;
586 continue;
587 }
588 }
589 }
590
591 UrlNavigatorButton* button = 0;
592 if (createButton) {
593 button = new UrlNavigatorButton(idx, this);
594 }
595 else {
596 button = *it;
597 button->setIndex(idx);
598 }
599
600 if (isFirstButton) {
601 button->setText(text);
602 }
603
604 if (createButton) {
605 button->show();
606 m_navButtons.append(button);
607 }
608 else {
609 ++it;
610 }
611 ++idx;
612 }
613 } while (hasNext);
614
615 // delete buttons which are not used anymore
616 QLinkedList<UrlNavigatorButton*>::iterator itBegin = it;
617 while (it != itEnd) {
618 (*it)->close();
619 (*it)->deleteLater();
620 ++it;
621 }
622 m_navButtons.erase(itBegin, m_navButtons.end());
623 }
624
625 void UrlNavigator::deleteButtons()
626 {
627 QLinkedList<UrlNavigatorButton*>::iterator itBegin = m_navButtons.begin();
628 QLinkedList<UrlNavigatorButton*>::iterator itEnd = m_navButtons.end();
629 QLinkedList<UrlNavigatorButton*>::iterator it = itBegin;
630 while (it != itEnd) {
631 (*it)->close();
632 (*it)->deleteLater();
633 ++it;
634 }
635 m_navButtons.erase(itBegin, itEnd);
636 }
637
638 #include "urlnavigator.moc"