]> cloud.milkyroute.net Git - dolphin.git/blob - src/urlnavigator.cpp
Cleanup: don't use deprecated Qt3 classes or methods, removed unnecessary includes
[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
190 if (editOrBrowse)
191 {
192 m_pathBox->setFocus();
193 }
194 }
195
196 void UrlNavigator::setActive(bool active)
197 {
198 if (active != m_active) {
199 m_active = active;
200 update();
201 if (active) {
202 emit activated();
203 }
204 }
205 }
206
207 void UrlNavigator::dropUrls(const KUrl::List& urls,
208 const KUrl& destination)
209 {
210 kDebug() << "------------------- URLS dropped" << endl;
211 emit urlsDropped(urls, destination);
212 }
213
214 void UrlNavigator::setUrl(const KUrl& url)
215 {
216 QString urlStr(url.pathOrUrl());
217 //kDebug() << "setUrl(" << url << ")" << endl;
218 if ( urlStr.length() > 0 && urlStr.at(0) == '~') {
219 // replace '~' by the home directory
220 urlStr.remove(0, 1);
221 urlStr.insert(0, QDir::home().path());
222 }
223
224 const KUrl transformedUrl(urlStr);
225
226 if (m_historyIndex > 0) {
227 // Check whether the previous element of the history has the same Url.
228 // If yes, just go forward instead of inserting a duplicate history
229 // element.
230 QLinkedList<HistoryElem>::const_iterator it = m_history.begin();
231 it += m_historyIndex - 1;
232 const KUrl& nextUrl = (*it).url();
233 if (transformedUrl == nextUrl) {
234 goForward();
235 // kDebug() << "goin' forward in history" << endl;
236 return;
237 }
238 }
239
240 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
241 const KUrl& currUrl = (*it).url();
242 if (currUrl == transformedUrl) {
243 // don't insert duplicate history elements
244 // kDebug() << "currUrl == transformedUrl" << endl;
245 return;
246 }
247
248 updateHistoryElem();
249 m_history.insert(it, HistoryElem(transformedUrl));
250
251 updateContent();
252
253 emit urlChanged(transformedUrl);
254 emit historyChanged();
255
256 // Prevent an endless growing of the history: remembering
257 // the last 100 Urls should be enough...
258 if (m_historyIndex > 100) {
259 m_history.erase(m_history.begin());
260 --m_historyIndex;
261 }
262
263 /* kDebug() << "history starting ====================" << endl;
264 int i = 0;
265 for (QValueListIterator<UrlNavigator::HistoryElem> it = m_history.begin();
266 it != m_history.end();
267 ++it, ++i)
268 {
269 kDebug() << i << ": " << (*it).url() << endl;
270 }
271 kDebug() << "history done ========================" << endl;*/
272
273 requestActivation();
274 }
275
276 void UrlNavigator::requestActivation()
277 {
278 kDebug() << "--------------------------- request activation" << endl;
279 setActive(true);
280 }
281
282 void UrlNavigator::storeContentsPosition(int x, int y)
283 {
284 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
285 (*it).setContentsX(x);
286 (*it).setContentsY(y);
287 }
288
289 void UrlNavigator::keyReleaseEvent(QKeyEvent* event)
290 {
291 KHBox::keyReleaseEvent(event);
292 if (isUrlEditable() && (event->key() == Qt::Key_Escape)) {
293 setUrlEditable(false);
294 }
295 }
296
297 void UrlNavigator::slotReturnPressed(const QString& text)
298 {
299 // Parts of the following code have been taken
300 // from the class KateFileSelector located in
301 // kate/app/katefileselector.hpp of Kate.
302 // Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
303 // Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
304 // Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
305
306 KUrl typedUrl(text);
307 if (typedUrl.hasPass()) {
308 typedUrl.setPass(QString::null);
309 }
310
311 QStringList urls = m_pathBox->urls();
312 urls.removeAll(typedUrl.url());
313 urls.prepend(typedUrl.url());
314 m_pathBox->setUrls(urls, KUrlComboBox::RemoveBottom);
315
316 setUrl(typedUrl);
317 // The Url might have been adjusted by UrlNavigator::setUrl(), hence
318 // synchronize the result in the path box.
319 m_pathBox->setUrl(url());
320 }
321
322 void UrlNavigator::slotUrlActivated(const KUrl& url)
323 {
324 setUrl(url);
325 }
326
327 void UrlNavigator::slotRemoteHostActivated()
328 {
329 KUrl u = url();
330
331 QString host = m_host->text();
332 QString user;
333
334 int marker = host.indexOf("@");
335 if (marker != -1)
336 {
337 user = host.left(marker);
338 u.setUser(user);
339 host = host.right(host.length() - marker - 1);
340 }
341
342 marker = host.indexOf("/");
343 if (marker != -1)
344 {
345 u.setPath(host.right(host.length() - marker));
346 host.truncate(marker);
347 }
348 else
349 {
350 u.setPath("");
351 }
352
353 if (m_protocols->currentProtocol() != u.protocol() ||
354 host != u.host() ||
355 user != u.user())
356 {
357 u.setProtocol(m_protocols->currentProtocol());
358 u.setHost(m_host->text());
359
360 //TODO: get rid of this HACK for file:///!
361 if (u.protocol() == "file")
362 {
363 u.setHost("");
364 if (u.path().isEmpty())
365 {
366 u.setPath("/");
367 }
368 }
369
370 setUrl(u);
371 }
372 }
373
374 void UrlNavigator::slotProtocolChanged(const QString& protocol)
375 {
376 KUrl url;
377 url.setProtocol(protocol);
378 //url.setPath(KProtocolInfo::protocolClass(protocol) == ":local" ? "/" : "");
379 url.setPath("/");
380 QLinkedList<QWidget*>::const_iterator it = m_navButtons.begin();
381 const QLinkedList<QWidget*>::const_iterator itEnd = m_navButtons.end();
382 while (it != itEnd) {
383 (*it)->close();
384 (*it)->deleteLater();
385 ++it;
386 }
387 m_navButtons.clear();
388
389 if (KProtocolInfo::protocolClass(protocol) == ":local") {
390 setUrl(url);
391 }
392 else {
393 if (!m_host) {
394 m_protocolSeparator = new QLabel("://", this);
395 m_host = new QLineEdit(this);
396
397 connect(m_host, SIGNAL(lostFocus()),
398 this, SLOT(slotRemoteHostActivated()));
399 connect(m_host, SIGNAL(returnPressed()),
400 this, SLOT(slotRemoteHostActivated()));
401 }
402 else {
403 m_host->setText("");
404 }
405 m_protocolSeparator->show();
406 m_host->show();
407 m_host->setFocus();
408 }
409 }
410
411 void UrlNavigator::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
412 {
413 // kDebug() << "received redirection to " << newUrl << endl;
414 kDebug() << "received redirection from " << oldUrl << " to " << newUrl << endl;
415 /* UrlStack::iterator it = m_urls.find(oldUrl);
416 if (it != m_urls.end())
417 {
418 m_urls.erase(++it, m_urls.end());
419 }
420
421 m_urls.append(newUrl);*/
422 }
423
424 void UrlNavigator::slotClicked()
425 {
426 if (isUrlEditable()) {
427 m_pathBox->setFocus();
428 updateContent();
429 }
430 else {
431 setUrl(m_pathBox->currentText());
432 emit requestActivation();
433 }
434 }
435
436 void UrlNavigator::updateHistoryElem()
437 {
438 assert(m_historyIndex >= 0);
439 const KFileItem* item = 0; // TODO: m_dolphinView->currentFileItem();
440 if (item != 0) {
441 QLinkedList<HistoryElem>::iterator it = m_history.begin() + m_historyIndex;
442 (*it).setCurrentFileName(item->name());
443 }
444 }
445
446 void UrlNavigator::updateContent()
447 {
448 // delete all existing Url navigator buttons
449 QLinkedList<QWidget*>::const_iterator it = m_navButtons.begin();
450 const QLinkedList<QWidget*>::const_iterator itEnd = m_navButtons.end();
451 while (it != itEnd) {
452 (*it)->close();
453 (*it)->deleteLater();
454 ++it;
455 }
456 m_navButtons.clear();
457
458 m_bookmarkSelector->updateSelection(url());
459
460 m_toggleButton->setToolTip(QString());
461 QString path(url().pathOrUrl());
462
463 // TODO: prevent accessing the DolphinMainWindow out from this scope
464 //const QAction* action = dolphinView()->mainWindow()->actionCollection()->action("editable_location");
465 // TODO: registry of default shortcuts
466 //QString shortcut = action? action->shortcut().toString() : "Ctrl+L";
467 const QString shortcut = "Ctrl+L";
468
469 if (m_toggleButton->isChecked()) {
470 delete m_protocols; m_protocols = 0;
471 delete m_protocolSeparator; m_protocolSeparator = 0;
472 delete m_host; m_host = 0;
473
474 m_toggleButton->setToolTip(i18n("Browse (%1, Escape)", shortcut));
475
476 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
477 m_pathBox->show();
478 m_pathBox->setUrl(url());
479 }
480 else {
481 m_toggleButton->setToolTip(i18n("Edit location (%1)", shortcut));
482
483 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
484 m_pathBox->hide();
485 QString dir_name;
486
487 // get the data from the currently selected bookmark
488 KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
489 //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
490
491 QString bookmarkPath;
492 if (bookmark.isNull()) {
493 // No bookmark is a part of the current Url.
494 // The following code tries to guess the bookmark
495 // path. E. g. "fish://root@192.168.0.2/var/lib" writes
496 // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
497 // navigation indication 'Custom Path > var > lib".
498 int idx = path.indexOf(QString("//"));
499 idx = path.indexOf("/", (idx < 0) ? 0 : idx + 2);
500 bookmarkPath = (idx < 0) ? path : path.left(idx);
501 }
502 else {
503 bookmarkPath = bookmark.url().pathOrUrl();
504 }
505 const uint len = bookmarkPath.length();
506
507 // calculate the start point for the Url navigator buttons by counting
508 // the slashs inside the bookmark Url
509 int slashCount = 0;
510 for (uint i = 0; i < len; ++i) {
511 if (bookmarkPath.at(i) == QChar('/')) {
512 ++slashCount;
513 }
514 }
515 if ((len > 0) && bookmarkPath.at(len - 1) == QChar('/')) {
516 assert(slashCount > 0);
517 --slashCount;
518 }
519
520 if (!url().isLocalFile() && bookmark.isNull()) {
521 QString protocol = url().protocol();
522 if (!m_protocols) {
523 m_protocols = new ProtocolCombo(protocol, this);
524 connect(m_protocols, SIGNAL(activated(const QString&)),
525 this, SLOT(slotProtocolChanged(const QString&)));
526 }
527 else {
528 m_protocols->setProtocol(protocol);
529 }
530 m_protocols->show();
531
532 if (KProtocolInfo::protocolClass(protocol) != ":local")
533 {
534 QString hostText = url().host();
535
536 if (!url().user().isEmpty())
537 {
538 hostText = url().user() + "@" + hostText;
539 }
540
541 if (!m_host) {
542 m_protocolSeparator = new QLabel("://", this);
543 m_host = new QLineEdit(hostText, this);
544
545 connect(m_host, SIGNAL(lostFocus()),
546 this, SLOT(slotRemoteHostActivated()));
547 connect(m_host, SIGNAL(returnPressed()),
548 this, SLOT(slotRemoteHostActivated()));
549 }
550 else {
551 m_host->setText(hostText);
552 }
553 m_protocolSeparator->show();
554 m_host->show();
555 }
556 else {
557 delete m_protocolSeparator; m_protocolSeparator = 0;
558 delete m_host; m_host = 0;
559 }
560 }
561 else if (m_protocols) {
562 m_protocols->hide();
563
564 if (m_host) {
565 m_protocolSeparator->hide();
566 m_host->hide();
567 }
568 }
569
570 // create Url navigator buttons
571 int idx = slashCount;
572 bool hasNext = true;
573 do {
574 dir_name = path.section('/', idx, idx);
575 const bool isFirstButton = (idx == slashCount);
576 hasNext = isFirstButton || !dir_name.isEmpty();
577 if (hasNext) {
578 UrlNavigatorButton* button = new UrlNavigatorButton(idx, this);
579 if (isFirstButton) {
580 // the first Url navigator button should get the name of the
581 // bookmark instead of the directory name
582 QString text = bookmark.text();
583 if (text.isEmpty()) {
584 if (url().isLocalFile())
585 {
586 text = i18n("Custom Path");
587 }
588 else
589 {
590 delete button;
591 ++idx;
592 continue;
593 }
594 }
595 button->setText(text);
596 }
597 button->show();
598 m_navButtons.append(button);
599 ++idx;
600 }
601 } while (hasNext);
602 }
603 }
604
605 #include "urlnavigator.moc"