]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
clear the status bar when doing an undo operation
[dolphin.git] / src / dolphinmainwindow.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Stefan Monov <logixoul@gmail.com> *
4 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
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 "dolphinmainwindow.h"
23
24 #include <assert.h>
25
26 #include "dolphinapplication.h"
27 #include "dolphinnewmenu.h"
28 #include "dolphinsettings.h"
29 #include "dolphinsettingsdialog.h"
30 #include "dolphinstatusbar.h"
31 #include "dolphinapplication.h"
32 #include "urlnavigator.h"
33 #include "dolphinsettings.h"
34 #include "bookmarkssidebarpage.h"
35 #include "infosidebarpage.h"
36 #include "generalsettings.h"
37 #include "viewpropertiesdialog.h"
38 #include "viewproperties.h"
39
40 #include <kaction.h>
41 #include <kactioncollection.h>
42 #include <kbookmarkmanager.h>
43 #include <kconfig.h>
44 #include <kdesktopfile.h>
45 #include <kdeversion.h>
46 #include <kfiledialog.h>
47 #include <kglobal.h>
48 #include <kicon.h>
49 #include <kiconloader.h>
50 #include <kio/netaccess.h>
51 #include <kio/renamedialog.h>
52 #include <kinputdialog.h>
53 #include <klocale.h>
54 #include <kmenu.h>
55 #include <kmessagebox.h>
56 #include <konqmimedata.h>
57 #include <kpropertiesdialog.h>
58 #include <kprotocolinfo.h>
59 #include <ktoggleaction.h>
60 #include <krun.h>
61 #include <kshell.h>
62 #include <kstandarddirs.h>
63 #include <kstatusbar.h>
64 #include <kstandardaction.h>
65 #include <kurl.h>
66
67 #include <QCloseEvent>
68 #include <QClipboard>
69 #include <QSplitter>
70 #include <QDockWidget>
71
72 DolphinMainWindow::DolphinMainWindow() :
73 KMainWindow(0),
74 m_newMenu(0),
75 m_splitter(0),
76 m_activeView(0)
77 {
78 setObjectName("Dolphin");
79 m_view[PrimaryIdx] = 0;
80 m_view[SecondaryIdx] = 0;
81
82 KonqUndoManager::incRef();
83
84 KonqUndoManager* undoManager = KonqUndoManager::self();
85 undoManager->setUiInterface(new UndoUiInterface(this));
86
87 connect(undoManager, SIGNAL(undoAvailable(bool)),
88 this, SLOT(slotUndoAvailable(bool)));
89 connect(undoManager, SIGNAL(undoTextChanged(const QString&)),
90 this, SLOT(slotUndoTextChanged(const QString&)));
91 }
92
93 DolphinMainWindow::~DolphinMainWindow()
94 {
95 KonqUndoManager::decRef();
96 DolphinApplication::app()->removeMainWindow(this);
97 }
98
99 void DolphinMainWindow::setActiveView(DolphinView* view)
100 {
101 assert((view == m_view[PrimaryIdx]) || (view == m_view[SecondaryIdx]));
102 if (m_activeView == view) {
103 return;
104 }
105
106 m_activeView = view;
107
108 updateHistory();
109 updateEditActions();
110 updateViewActions();
111 updateGoActions();
112
113 setCaption(m_activeView->url().fileName());
114
115 emit activeViewChanged();
116 }
117
118 void DolphinMainWindow::dropUrls(const KUrl::List& urls,
119 const KUrl& destination)
120 {
121 Qt::DropAction action = Qt::CopyAction;
122
123 Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
124 const bool shiftPressed = modifier & Qt::ShiftModifier;
125 const bool controlPressed = modifier & Qt::ControlModifier;
126 if (shiftPressed && controlPressed) {
127 // shortcut for 'Link Here' is used
128 action = Qt::LinkAction;
129 }
130 else if (shiftPressed) {
131 // shortcut for 'Move Here' is used
132 action = Qt::MoveAction;
133 }
134 else if (controlPressed) {
135 // shortcut for 'Copy Here' is used
136 action = Qt::CopyAction;
137 }
138 else {
139 // open a context menu which offers the following actions:
140 // - Move Here
141 // - Copy Here
142 // - Link Here
143 // - Cancel
144
145 KMenu popup(this);
146
147 QString seq = QKeySequence(Qt::ShiftModifier).toString();
148 seq.chop(1); // chop superfluous '+'
149 QAction* moveAction = popup.addAction(KIcon("goto"),
150 i18n("&Move Here") + "\t" + seq);
151
152 seq = QKeySequence(Qt::ControlModifier).toString();
153 seq.chop(1);
154 QAction* copyAction = popup.addAction(KIcon("editcopy"),
155 i18n("&Copy Here") + "\t" + seq);
156
157 seq = QKeySequence(Qt::ControlModifier + Qt::ShiftModifier).toString();
158 seq.chop(1);
159 QAction* linkAction = popup.addAction(KIcon("www"),
160 i18n("&Link Here") + "\t" + seq);
161
162 popup.addSeparator();
163 QAction* cancelAction = popup.addAction(KIcon("stop"), i18n("Cancel"));
164
165 QAction* activatedAction = popup.exec(QCursor::pos());
166 if (activatedAction == moveAction) {
167 action = Qt::MoveAction;
168 }
169 else if (activatedAction == copyAction) {
170 action = Qt::CopyAction;
171 }
172 else if (activatedAction == linkAction) {
173 action = Qt::LinkAction;
174 }
175 else if (activatedAction == cancelAction) {
176 return;
177 }
178 }
179
180 switch (action) {
181 case Qt::MoveAction:
182 moveUrls(urls, destination);
183 break;
184
185 case Qt::CopyAction:
186 copyUrls(urls, destination);
187 break;
188
189 case Qt::LinkAction:
190 linkUrls(urls, destination);
191 break;
192
193 default:
194 break;
195 }
196 }
197
198 void DolphinMainWindow::refreshViews()
199 {
200 const bool split = DolphinSettings::instance().generalSettings()->splitView();
201 const bool isPrimaryViewActive = (m_activeView == m_view[PrimaryIdx]);
202 KUrl url;
203 for (int i = PrimaryIdx; i <= SecondaryIdx; ++i) {
204 if (m_view[i] != 0) {
205 url = m_view[i]->url();
206
207 // delete view instance...
208 m_view[i]->close();
209 m_view[i]->deleteLater();
210 m_view[i] = 0;
211 }
212
213 if (split || (i == PrimaryIdx)) {
214 // ... and recreate it
215 ViewProperties props(url);
216 m_view[i] = new DolphinView(this,
217 m_splitter,
218 url,
219 props.viewMode(),
220 props.showHiddenFiles());
221 connectViewSignals(i);
222 m_view[i]->show();
223 }
224 }
225
226 m_activeView = isPrimaryViewActive ? m_view[PrimaryIdx] : m_view[SecondaryIdx];
227 assert(m_activeView != 0);
228
229 updateViewActions();
230 emit activeViewChanged();
231 }
232
233 void DolphinMainWindow::slotViewModeChanged()
234 {
235 updateViewActions();
236 }
237
238 void DolphinMainWindow::slotShowHiddenFilesChanged()
239 {
240 KToggleAction* showHiddenFilesAction =
241 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
242 showHiddenFilesAction->setChecked(m_activeView->showHiddenFiles());
243 }
244
245 void DolphinMainWindow::slotSortingChanged(DolphinView::Sorting sorting)
246 {
247 QAction* action = 0;
248 switch (sorting) {
249 case DolphinView::SortByName:
250 action = actionCollection()->action("by_name");
251 break;
252 case DolphinView::SortBySize:
253 action = actionCollection()->action("by_size");
254 break;
255 case DolphinView::SortByDate:
256 action = actionCollection()->action("by_date");
257 break;
258 default:
259 break;
260 }
261
262 if (action != 0) {
263 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
264 toggleAction->setChecked(true);
265 }
266 }
267
268 void DolphinMainWindow::slotSortOrderChanged(Qt::SortOrder order)
269 {
270 KToggleAction* descending = static_cast<KToggleAction*>(actionCollection()->action("descending"));
271 const bool sortDescending = (order == Qt::Descending);
272 descending->setChecked(sortDescending);
273 }
274
275 void DolphinMainWindow::slotSelectionChanged()
276 {
277 updateEditActions();
278
279 assert(m_view[PrimaryIdx] != 0);
280 int selectedUrlsCount = m_view[PrimaryIdx]->selectedUrls().count();
281 if (m_view[SecondaryIdx] != 0) {
282 selectedUrlsCount += m_view[SecondaryIdx]->selectedUrls().count();
283 }
284
285 QAction* compareFilesAction = actionCollection()->action("compare_files");
286 compareFilesAction->setEnabled(selectedUrlsCount == 2);
287
288 m_activeView->updateStatusBar();
289
290 emit selectionChanged();
291 }
292
293 void DolphinMainWindow::slotHistoryChanged()
294 {
295 updateHistory();
296 }
297
298 void DolphinMainWindow::slotUrlChanged(const KUrl& url)
299 {
300 updateEditActions();
301 updateGoActions();
302 setCaption(url.fileName());
303 }
304
305 void DolphinMainWindow::updateFilterBarAction(bool show)
306 {
307 KToggleAction* showFilterBarAction =
308 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
309 showFilterBarAction->setChecked(show);
310 }
311
312 void DolphinMainWindow::openNewMainWindow()
313 {
314 DolphinApplication::app()->createMainWindow()->show();
315 }
316
317 void DolphinMainWindow::closeEvent(QCloseEvent* event)
318 {
319 DolphinSettings& settings = DolphinSettings::instance();
320 GeneralSettings* generalSettings = settings.generalSettings();
321 generalSettings->setFirstRun(false);
322
323 settings.save();
324
325 // TODO: I assume there will be a generic way in KDE 4 to store the docks
326 // of the main window. In the meantime they are stored manually:
327 QString filename = KStandardDirs::locateLocal("data", KGlobal::mainComponent().componentName());
328 filename.append("/panels_layout");
329 QFile file(filename);
330 if (file.open(QIODevice::WriteOnly)) {
331 QByteArray data = saveState();
332 file.write(data);
333 file.close();
334 }
335
336 KMainWindow::closeEvent(event);
337 }
338
339 void DolphinMainWindow::saveProperties(KConfig* config)
340 {
341 config->setGroup("Primary view");
342 config->writeEntry("Url", m_view[PrimaryIdx]->url().url());
343 config->writeEntry("Editable Url", m_view[PrimaryIdx]->isUrlEditable());
344 if (m_view[SecondaryIdx] != 0) {
345 config->setGroup("Secondary view");
346 config->writeEntry("Url", m_view[SecondaryIdx]->url().url());
347 config->writeEntry("Editable Url", m_view[SecondaryIdx]->isUrlEditable());
348 }
349 }
350
351 void DolphinMainWindow::readProperties(KConfig* config)
352 {
353 config->setGroup("Primary view");
354 m_view[PrimaryIdx]->setUrl(config->readEntry("Url"));
355 m_view[PrimaryIdx]->setUrlEditable(config->readEntry("Editable Url", false));
356 if (config->hasGroup("Secondary view")) {
357 config->setGroup("Secondary view");
358 if (m_view[SecondaryIdx] == 0) {
359 toggleSplitView();
360 }
361 m_view[SecondaryIdx]->setUrl(config->readEntry("Url"));
362 m_view[SecondaryIdx]->setUrlEditable(config->readEntry("Editable Url", false));
363 }
364 else if (m_view[SecondaryIdx] != 0) {
365 toggleSplitView();
366 }
367 }
368
369 void DolphinMainWindow::updateNewMenu()
370 {
371 m_newMenu->slotCheckUpToDate();
372 m_newMenu->setPopupFiles(activeView()->url());
373 }
374
375 void DolphinMainWindow::rename()
376 {
377 clearStatusBar();
378 m_activeView->renameSelectedItems();
379 }
380
381 void DolphinMainWindow::moveToTrash()
382 {
383 clearStatusBar();
384 const KUrl::List selectedUrls = m_activeView->selectedUrls();
385 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls);
386 m_undoOperations.append(KonqOperations::TRASH);
387 }
388
389 void DolphinMainWindow::deleteItems()
390 {
391 clearStatusBar();
392
393 KUrl::List list = m_activeView->selectedUrls();
394 const uint itemCount = list.count();
395 assert(itemCount >= 1);
396
397 QString text;
398 if (itemCount > 1) {
399 text = i18n("Do you really want to delete the %1 selected items?",itemCount);
400 }
401 else {
402 const KUrl& url = list.first();
403 text = i18n("Do you really want to delete '%1'?",url.fileName());
404 }
405
406 const bool del = KMessageBox::warningContinueCancel(this,
407 text,
408 QString::null,
409 KGuiItem(i18n("Delete"), KIcon("editdelete"))
410 ) == KMessageBox::Continue;
411 if (del) {
412 KIO::Job* job = KIO::del(list);
413 connect(job, SIGNAL(result(KJob*)),
414 this, SLOT(slotHandleJobError(KJob*)));
415 connect(job, SIGNAL(result(KJob*)),
416 this, SLOT(slotDeleteFileFinished(KJob*)));
417 }
418 }
419
420 void DolphinMainWindow::properties()
421 {
422 const KFileItemList list = m_activeView->selectedItems();
423 new KPropertiesDialog(list, this);
424 }
425
426 void DolphinMainWindow::quit()
427 {
428 close();
429 }
430
431 void DolphinMainWindow::slotHandleJobError(KJob* job)
432 {
433 if (job->error() != 0) {
434 DolphinStatusBar* statusBar = m_activeView->statusBar();
435 statusBar->setMessage(job->errorString(),
436 DolphinStatusBar::Error);
437 }
438 }
439
440 void DolphinMainWindow::slotDeleteFileFinished(KJob* job)
441 {
442 if (job->error() == 0) {
443 DolphinStatusBar* statusBar = m_activeView->statusBar();
444 statusBar->setMessage(i18n("Delete operation completed."),
445 DolphinStatusBar::OperationCompleted);
446 }
447 }
448
449 void DolphinMainWindow::slotUndoAvailable(bool available)
450 {
451 QAction* undoAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Undo));
452 if (undoAction != 0) {
453 undoAction->setEnabled(available);
454 }
455
456 if (available && (m_undoOperations.count() > 0)) {
457 const KonqOperations::Operation op = m_undoOperations.takeFirst();
458 DolphinStatusBar* statusBar = m_activeView->statusBar();
459 switch (op) {
460 case KonqOperations::COPY:
461 statusBar->setMessage(i18n("Copy operation completed."),
462 DolphinStatusBar::OperationCompleted);
463 break;
464 case KonqOperations::MOVE:
465 statusBar->setMessage(i18n("Move operation completed."),
466 DolphinStatusBar::OperationCompleted);
467 break;
468 case KonqOperations::LINK:
469 statusBar->setMessage(i18n("Link operation completed."),
470 DolphinStatusBar::OperationCompleted);
471 break;
472 case KonqOperations::TRASH:
473 statusBar->setMessage(i18n("Move to trash operation completed."),
474 DolphinStatusBar::OperationCompleted);
475 break;
476 default:
477 break;
478 }
479
480 }
481 }
482
483 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
484 {
485 QAction* undoAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Undo));
486 if (undoAction != 0) {
487 undoAction->setText(text);
488 }
489 }
490
491 void DolphinMainWindow::undo()
492 {
493 clearStatusBar();
494 KonqUndoManager::self()->undo();
495 }
496
497 void DolphinMainWindow::cut()
498 {
499 QMimeData* mimeData = new QMimeData();
500 const KUrl::List kdeUrls = m_activeView->selectedUrls();
501 const KUrl::List mostLocalUrls;
502 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
503 QApplication::clipboard()->setMimeData(mimeData);
504 }
505
506 void DolphinMainWindow::copy()
507 {
508 QMimeData* mimeData = new QMimeData();
509 const KUrl::List kdeUrls = m_activeView->selectedUrls();
510 const KUrl::List mostLocalUrls;
511 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
512
513 QApplication::clipboard()->setMimeData(mimeData);
514 }
515
516 void DolphinMainWindow::paste()
517 {
518 QClipboard* clipboard = QApplication::clipboard();
519 const QMimeData* mimeData = clipboard->mimeData();
520
521 clearStatusBar();
522
523 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
524
525 // per default the pasting is done into the current Url of the view
526 KUrl destUrl(m_activeView->url());
527
528 // check whether the pasting should be done into a selected directory
529 KUrl::List selectedUrls = m_activeView->selectedUrls();
530 if (selectedUrls.count() == 1) {
531 const KFileItem fileItem(S_IFDIR,
532 KFileItem::Unknown,
533 selectedUrls.first(),
534 true);
535 if (fileItem.isDir()) {
536 // only one item is selected which is a directory, hence paste
537 // into this directory
538 destUrl = selectedUrls.first();
539 }
540 }
541
542 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
543 moveUrls(sourceUrls, destUrl);
544 clipboard->clear();
545 }
546 else {
547 copyUrls(sourceUrls, destUrl);
548 }
549 }
550
551 void DolphinMainWindow::updatePasteAction()
552 {
553 QAction* pasteAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
554 if (pasteAction == 0) {
555 return;
556 }
557
558 QString text(i18n("Paste"));
559 QClipboard* clipboard = QApplication::clipboard();
560 const QMimeData* mimeData = clipboard->mimeData();
561
562 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
563 if (!urls.isEmpty()) {
564 pasteAction->setEnabled(true);
565
566 const int count = urls.count();
567 if (count == 1) {
568 pasteAction->setText(i18n("Paste 1 File"));
569 }
570 else {
571 pasteAction->setText(i18n("Paste %1 Files").arg(count));
572 }
573 }
574 else {
575 pasteAction->setEnabled(false);
576 pasteAction->setText(i18n("Paste"));
577 }
578
579 if (pasteAction->isEnabled()) {
580 KUrl::List urls = m_activeView->selectedUrls();
581 const uint count = urls.count();
582 if (count > 1) {
583 // pasting should not be allowed when more than one file
584 // is selected
585 pasteAction->setEnabled(false);
586 }
587 else if (count == 1) {
588 // Only one file is selected. Pasting is only allowed if this
589 // file is a directory.
590 // TODO: this doesn't work with remote protocols; instead we need a
591 // m_activeView->selectedFileItems() to get the real KFileItems
592 const KFileItem fileItem(S_IFDIR,
593 KFileItem::Unknown,
594 urls.first(),
595 true);
596 pasteAction->setEnabled(fileItem.isDir());
597 }
598 }
599 }
600
601 void DolphinMainWindow::selectAll()
602 {
603 clearStatusBar();
604 m_activeView->selectAll();
605 }
606
607 void DolphinMainWindow::invertSelection()
608 {
609 clearStatusBar();
610 m_activeView->invertSelection();
611 }
612 void DolphinMainWindow::setIconsView()
613 {
614 m_activeView->setMode(DolphinView::IconsView);
615 }
616
617 void DolphinMainWindow::setDetailsView()
618 {
619 m_activeView->setMode(DolphinView::DetailsView);
620 }
621
622 void DolphinMainWindow::sortByName()
623 {
624 m_activeView->setSorting(DolphinView::SortByName);
625 }
626
627 void DolphinMainWindow::sortBySize()
628 {
629 m_activeView->setSorting(DolphinView::SortBySize);
630 }
631
632 void DolphinMainWindow::sortByDate()
633 {
634 m_activeView->setSorting(DolphinView::SortByDate);
635 }
636
637 void DolphinMainWindow::toggleSortOrder()
638 {
639 const Qt::SortOrder order = (m_activeView->sortOrder() == Qt::Ascending) ?
640 Qt::Descending :
641 Qt::Ascending;
642 m_activeView->setSortOrder(order);
643 }
644
645 void DolphinMainWindow::toggleSplitView()
646 {
647 if (m_view[SecondaryIdx] == 0) {
648 const int newWidth = (m_view[PrimaryIdx]->width() - m_splitter->handleWidth()) / 2;
649 // create a secondary view
650 m_view[SecondaryIdx] = new DolphinView(this,
651 0,
652 m_view[PrimaryIdx]->url(),
653 m_view[PrimaryIdx]->mode(),
654 m_view[PrimaryIdx]->showHiddenFiles());
655 connectViewSignals(SecondaryIdx);
656 m_splitter->addWidget(m_view[SecondaryIdx]);
657 m_splitter->setSizes(QList<int>() << newWidth << newWidth);
658 m_view[SecondaryIdx]->show();
659 }
660 else {
661 // remove secondary view
662 if (m_activeView == m_view[PrimaryIdx]) {
663 m_view[SecondaryIdx]->close();
664 m_view[SecondaryIdx]->deleteLater();
665 m_view[SecondaryIdx] = 0;
666 setActiveView(m_view[PrimaryIdx]);
667 }
668 else {
669 // The secondary view is active, hence from the users point of view
670 // the content of the secondary view should be moved to the primary view.
671 // From an implementation point of view it is more efficient to close
672 // the primary view and exchange the internal pointers afterwards.
673 m_view[PrimaryIdx]->close();
674 delete m_view[PrimaryIdx];
675 m_view[PrimaryIdx] = m_view[SecondaryIdx];
676 m_view[SecondaryIdx] = 0;
677 setActiveView(m_view[PrimaryIdx]);
678 }
679 }
680 }
681
682 void DolphinMainWindow::reloadView()
683 {
684 clearStatusBar();
685 m_activeView->reload();
686 }
687
688 void DolphinMainWindow::stopLoading()
689 {
690 }
691
692 void DolphinMainWindow::togglePreview()
693 {
694 clearStatusBar();
695
696 const KToggleAction* showPreviewAction =
697 static_cast<KToggleAction*>(actionCollection()->action("show_preview"));
698 const bool show = showPreviewAction->isChecked();
699 m_activeView->setShowPreview(show);
700 }
701
702 void DolphinMainWindow::toggleShowHiddenFiles()
703 {
704 clearStatusBar();
705
706 const KToggleAction* showHiddenFilesAction =
707 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
708 const bool show = showHiddenFilesAction->isChecked();
709 m_activeView->setShowHiddenFiles(show);
710 }
711
712 void DolphinMainWindow::showFilterBar()
713 {
714 const KToggleAction* showFilterBarAction =
715 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
716 const bool show = showFilterBarAction->isChecked();
717 m_activeView->showFilterBar(show);
718 }
719
720 void DolphinMainWindow::zoomIn()
721 {
722 m_activeView->zoomIn();
723 updateViewActions();
724 }
725
726 void DolphinMainWindow::zoomOut()
727 {
728 m_activeView->zoomOut();
729 updateViewActions();
730 }
731
732 void DolphinMainWindow::toggleEditLocation()
733 {
734 clearStatusBar();
735
736 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
737
738 bool editOrBrowse = action->isChecked();
739 m_activeView->setUrlEditable(editOrBrowse);
740 }
741
742 void DolphinMainWindow::editLocation()
743 {
744 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
745 action->setChecked(true);
746 m_activeView->setUrlEditable(true);
747 }
748
749 void DolphinMainWindow::adjustViewProperties()
750 {
751 clearStatusBar();
752 ViewPropertiesDialog dlg(m_activeView);
753 dlg.exec();
754 }
755
756 void DolphinMainWindow::goBack()
757 {
758 clearStatusBar();
759 m_activeView->goBack();
760 }
761
762 void DolphinMainWindow::goForward()
763 {
764 clearStatusBar();
765 m_activeView->goForward();
766 }
767
768 void DolphinMainWindow::goUp()
769 {
770 clearStatusBar();
771 m_activeView->goUp();
772 }
773
774 void DolphinMainWindow::goHome()
775 {
776 clearStatusBar();
777 m_activeView->goHome();
778 }
779
780 void DolphinMainWindow::openTerminal()
781 {
782 QString command("konsole --workdir \"");
783 command.append(m_activeView->url().path());
784 command.append('\"');
785
786 KRun::runCommand(command, "Konsole", "konsole");
787 }
788
789 void DolphinMainWindow::findFile()
790 {
791 KRun::run("kfind", m_activeView->url());
792 }
793
794 void DolphinMainWindow::compareFiles()
795 {
796 // The method is only invoked if exactly 2 files have
797 // been selected. The selected files may be:
798 // - both in the primary view
799 // - both in the secondary view
800 // - one in the primary view and the other in the secondary
801 // view
802 assert(m_view[PrimaryIdx] != 0);
803
804 KUrl urlA;
805 KUrl urlB;
806 KUrl::List urls = m_view[PrimaryIdx]->selectedUrls();
807
808 switch (urls.count()) {
809 case 0: {
810 assert(m_view[SecondaryIdx] != 0);
811 urls = m_view[SecondaryIdx]->selectedUrls();
812 assert(urls.count() == 2);
813 urlA = urls[0];
814 urlB = urls[1];
815 break;
816 }
817
818 case 1: {
819 urlA = urls[0];
820 assert(m_view[SecondaryIdx] != 0);
821 urls = m_view[SecondaryIdx]->selectedUrls();
822 assert(urls.count() == 1);
823 urlB = urls[0];
824 break;
825 }
826
827 case 2: {
828 urlA = urls[0];
829 urlB = urls[1];
830 break;
831 }
832
833 default: {
834 // may not happen: compareFiles may only get invoked if 2
835 // files are selected
836 assert(false);
837 }
838 }
839
840 QString command("kompare -c \"");
841 command.append(urlA.pathOrUrl());
842 command.append("\" \"");
843 command.append(urlB.pathOrUrl());
844 command.append('\"');
845 KRun::runCommand(command, "Kompare", "kompare");
846
847 }
848
849 void DolphinMainWindow::editSettings()
850 {
851 // TODO: make a static method for opening the settings dialog
852 DolphinSettingsDialog dlg(this);
853 dlg.exec();
854 }
855
856 void DolphinMainWindow::init()
857 {
858 // Check whether Dolphin runs the first time. If yes then
859 // a proper default window size is given at the end of DolphinMainWindow::init().
860 GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
861 const bool firstRun = generalSettings->firstRun();
862
863 setAcceptDrops(true);
864
865 m_splitter = new QSplitter(this);
866
867 DolphinSettings& settings = DolphinSettings::instance();
868
869 KBookmarkManager* manager = settings.bookmarkManager();
870 assert(manager != 0);
871 KBookmarkGroup root = manager->root();
872 if (root.first().isNull()) {
873 root.addBookmark(manager, i18n("Home"), settings.generalSettings()->homeUrl(), "folder_home");
874 root.addBookmark(manager, i18n("Storage Media"), KUrl("media:/"), "blockdevice");
875 root.addBookmark(manager, i18n("Network"), KUrl("remote:/"), "network_local");
876 root.addBookmark(manager, i18n("Root"), KUrl("/"), "folder_red");
877 root.addBookmark(manager, i18n("Trash"), KUrl("trash:/"), "trashcan_full");
878 }
879
880 setupActions();
881
882 const KUrl& homeUrl = root.first().url();
883 setCaption(homeUrl.fileName());
884 ViewProperties props(homeUrl);
885 m_view[PrimaryIdx] = new DolphinView(this,
886 m_splitter,
887 homeUrl,
888 props.viewMode(),
889 props.showHiddenFiles());
890 connectViewSignals(PrimaryIdx);
891 m_view[PrimaryIdx]->show();
892
893 m_activeView = m_view[PrimaryIdx];
894
895 setCentralWidget(m_splitter);
896 setupDockWidgets();
897
898 setupGUI(Keys|Save|Create|ToolBar);
899 createGUI();
900
901 stateChanged("new_file");
902 setAutoSaveSettings();
903
904 QClipboard* clipboard = QApplication::clipboard();
905 connect(clipboard, SIGNAL(dataChanged()),
906 this, SLOT(updatePasteAction()));
907 updatePasteAction();
908 updateGoActions();
909
910 loadSettings();
911
912 if (firstRun) {
913 // assure a proper default size if Dolphin runs the first time
914 resize(640, 480);
915 }
916 }
917
918 void DolphinMainWindow::loadSettings()
919 {
920 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
921
922 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
923 if (settings->splitView()) {
924 splitAction->setChecked(true);
925 toggleSplitView();
926 }
927
928 updateViewActions();
929
930 // TODO: I assume there will be a generic way in KDE 4 to restore the docks
931 // of the main window. In the meantime they are restored manually (see also
932 // DolphinMainWindow::closeEvent() for more details):
933 QString filename = KStandardDirs::locateLocal("data", KGlobal::mainComponent().componentName()); filename.append("/panels_layout");
934 QFile file(filename);
935 if (file.open(QIODevice::ReadOnly)) {
936 QByteArray data = file.readAll();
937 restoreState(data);
938 file.close();
939 }
940 }
941
942 void DolphinMainWindow::setupActions()
943 {
944 // setup 'File' menu
945 m_newMenu = new DolphinNewMenu(this);
946 KMenu* menu = m_newMenu->menu();
947 menu->setTitle(i18n("Create New..."));
948 menu->setIcon(SmallIcon("filenew"));
949 connect(menu, SIGNAL(aboutToShow()),
950 this, SLOT(updateNewMenu()));
951
952 QAction* action = actionCollection()->addAction("new_window");
953 action->setIcon(KIcon("window_new"));
954 action->setText(i18n("New &Window"));
955 connect(action, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
956
957 QAction* rename = actionCollection()->addAction("rename");
958 rename->setText(i18n("Rename"));
959 rename->setShortcut(Qt::Key_F2);
960 connect(rename, SIGNAL(triggered()), this, SLOT(rename()));
961
962 QAction* moveToTrash = actionCollection()->addAction("move_to_trash");
963 moveToTrash->setText(i18n("Move to Trash"));
964 moveToTrash->setIcon(KIcon("edittrash"));
965 moveToTrash->setShortcut(QKeySequence::Delete);
966 connect(moveToTrash, SIGNAL(triggered()), this, SLOT(moveToTrash()));
967
968 QAction* deleteAction = actionCollection()->addAction("delete");
969 deleteAction->setText(i18n("Delete"));
970 deleteAction->setShortcut(Qt::ALT | Qt::Key_Delete);
971 deleteAction->setIcon(KIcon("editdelete"));
972 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItems()));
973
974 QAction* properties = actionCollection()->addAction("properties");
975 properties->setText(i18n("Propert&ies"));
976 properties->setShortcut(Qt::Key_Alt | Qt::Key_Return);
977 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
978
979 KStandardAction::quit(this, SLOT(quit()), actionCollection());
980
981 // setup 'Edit' menu
982 KStandardAction::undo(this,
983 SLOT(undo()),
984 actionCollection());
985
986 KStandardAction::cut(this, SLOT(cut()), actionCollection());
987 KStandardAction::copy(this, SLOT(copy()), actionCollection());
988 KStandardAction::paste(this, SLOT(paste()), actionCollection());
989
990 QAction* selectAll = actionCollection()->addAction("select_all");
991 selectAll->setText(i18n("Select All"));
992 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
993 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
994
995 QAction* invertSelection = actionCollection()->addAction("invert_selection");
996 invertSelection->setText(i18n("Invert Selection"));
997 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
998 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
999
1000 // setup 'View' menu
1001 KStandardAction::zoomIn(this,
1002 SLOT(zoomIn()),
1003 actionCollection());
1004
1005 KStandardAction::zoomOut(this,
1006 SLOT(zoomOut()),
1007 actionCollection());
1008
1009 KToggleAction* iconsView = actionCollection()->add<KToggleAction>("icons");
1010 iconsView->setText(i18n("Icons"));
1011 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
1012 iconsView->setIcon(KIcon("view_icon"));
1013 connect(iconsView, SIGNAL(triggered()), this, SLOT(setIconsView()));
1014
1015 KToggleAction* detailsView = actionCollection()->add<KToggleAction>("details");
1016 detailsView->setText(i18n("Details"));
1017 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1018 detailsView->setIcon(KIcon("view_text"));
1019 connect(detailsView, SIGNAL(triggered()), this, SLOT(setDetailsView()));
1020
1021 QActionGroup* viewModeGroup = new QActionGroup(this);
1022 viewModeGroup->addAction(iconsView);
1023 viewModeGroup->addAction(detailsView);
1024
1025 KToggleAction* sortByName = actionCollection()->add<KToggleAction>("by_name");
1026 sortByName->setText(i18n("By Name"));
1027 connect(sortByName, SIGNAL(triggered()), this, SLOT(sortByName()));
1028
1029 KToggleAction* sortBySize = actionCollection()->add<KToggleAction>("by_size");
1030 sortBySize->setText(i18n("By Size"));
1031 connect(sortBySize, SIGNAL(triggered()), this, SLOT(sortBySize()));
1032
1033 KToggleAction* sortByDate = actionCollection()->add<KToggleAction>("by_date");
1034 sortByDate->setText(i18n("By Date"));
1035 connect(sortByDate, SIGNAL(triggered()), this, SLOT(sortByDate()));
1036
1037 QActionGroup* sortGroup = new QActionGroup(this);
1038 sortGroup->addAction(sortByName);
1039 sortGroup->addAction(sortBySize);
1040 sortGroup->addAction(sortByDate);
1041
1042 KToggleAction* sortDescending = actionCollection()->add<KToggleAction>("descending");
1043 sortDescending->setText(i18n("Descending"));
1044 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
1045
1046 KToggleAction* showPreview = actionCollection()->add<KToggleAction>("show_preview");
1047 showPreview->setText(i18n("Show Preview"));
1048 connect(showPreview, SIGNAL(triggered()), this, SLOT(togglePreview()));
1049
1050 KToggleAction* showHiddenFiles = actionCollection()->add<KToggleAction>("show_hidden_files");
1051 showHiddenFiles->setText(i18n("Show Hidden Files"));
1052 //showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_ KDE4-TODO: what Qt-Key represents '.'?
1053 connect(showHiddenFiles, SIGNAL(triggered()), this, SLOT(toggleShowHiddenFiles()));
1054
1055 KToggleAction* split = actionCollection()->add<KToggleAction>("split_view");
1056 split->setText(i18n("Split View"));
1057 split->setShortcut(Qt::Key_F10);
1058 split->setIcon(KIcon("view_left_right"));
1059 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1060
1061 QAction* reload = actionCollection()->addAction("reload");
1062 reload->setText(i18n("Reload"));
1063 reload->setShortcut(Qt::Key_F5);
1064 reload->setIcon(KIcon("reload"));
1065 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1066
1067 QAction* stop = actionCollection()->addAction("stop");
1068 stop->setText(i18n("Stop"));
1069 stop->setIcon(KIcon("stop"));
1070 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1071
1072 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1073 showFullLocation->setText(i18n("Show Full Location"));
1074 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1075 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1076
1077 KToggleAction* editLocation = actionCollection()->add<KToggleAction>("edit_location");
1078 editLocation->setText(i18n("Edit Location"));
1079 editLocation->setShortcut(Qt::Key_F6);
1080 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
1081
1082 QAction* adjustViewProps = actionCollection()->addAction("view_properties");
1083 adjustViewProps->setText(i18n("Adjust View Properties..."));
1084 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
1085
1086 // setup 'Go' menu
1087 KStandardAction::back(this, SLOT(goBack()), actionCollection());
1088 KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1089 KStandardAction::up(this, SLOT(goUp()), actionCollection());
1090 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1091
1092 // setup 'Tools' menu
1093 QAction* openTerminal = actionCollection()->addAction("open_terminal");
1094 openTerminal->setText(i18n("Open Terminal"));
1095 openTerminal->setShortcut(Qt::Key_F4);
1096 openTerminal->setIcon(KIcon("konsole"));
1097 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1098
1099 QAction* findFile = actionCollection()->addAction("find_file");
1100 findFile->setText(i18n("Find File..."));
1101 findFile->setShortcut(Qt::Key_F);
1102 findFile->setIcon(KIcon("filefind"));
1103 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
1104
1105 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1106 showFilterBar->setText(i18n("Show Filter Bar"));
1107 showFilterBar->setShortcut(Qt::Key_Slash);
1108 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1109
1110 QAction* compareFiles = actionCollection()->addAction("compare_files");
1111 compareFiles->setText(i18n("Compare Files"));
1112 compareFiles->setIcon(KIcon("kompare"));
1113 compareFiles->setEnabled(false);
1114 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1115
1116 // setup 'Settings' menu
1117 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1118 }
1119
1120 void DolphinMainWindow::setupDockWidgets()
1121 {
1122 QDockWidget* shortcutsDock = new QDockWidget(i18n("Bookmarks"));
1123 shortcutsDock->setObjectName("bookmarksDock");
1124 shortcutsDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1125 shortcutsDock->setWidget(new BookmarksSidebarPage(this));
1126
1127 shortcutsDock->toggleViewAction()->setText(i18n("Show Bookmarks Panel"));
1128 actionCollection()->addAction("show_bookmarks_panel", shortcutsDock->toggleViewAction());
1129
1130 addDockWidget(Qt::LeftDockWidgetArea, shortcutsDock);
1131
1132 QDockWidget* infoDock = new QDockWidget(i18n("Information"));
1133 infoDock->setObjectName("infoDock");
1134 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1135 infoDock->setWidget(new InfoSidebarPage(this));
1136
1137 infoDock->toggleViewAction()->setText(i18n("Show Information Panel"));
1138 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1139
1140 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1141 }
1142
1143 void DolphinMainWindow::updateHistory()
1144 {
1145 int index = 0;
1146 const QLinkedList<UrlNavigator::HistoryElem> list = m_activeView->urlHistory(index);
1147
1148 QAction* backAction = actionCollection()->action("go_back");
1149 if (backAction != 0) {
1150 backAction->setEnabled(index < static_cast<int>(list.count()) - 1);
1151 }
1152
1153 QAction* forwardAction = actionCollection()->action("go_forward");
1154 if (forwardAction != 0) {
1155 forwardAction->setEnabled(index > 0);
1156 }
1157 }
1158
1159 void DolphinMainWindow::updateEditActions()
1160 {
1161 const KFileItemList list = m_activeView->selectedItems();
1162 if (list.isEmpty()) {
1163 stateChanged("has_no_selection");
1164 }
1165 else {
1166 stateChanged("has_selection");
1167
1168 QAction* renameAction = actionCollection()->action("rename");
1169 if (renameAction != 0) {
1170 renameAction->setEnabled(list.count() >= 1);
1171 }
1172
1173 bool enableMoveToTrash = true;
1174
1175 KFileItemList::const_iterator it = list.begin();
1176 const KFileItemList::const_iterator end = list.end();
1177 while (it != end) {
1178 KFileItem* item = *it;
1179 const KUrl& url = item->url();
1180 // only enable the 'Move to Trash' action for local files
1181 if (!url.isLocalFile()) {
1182 enableMoveToTrash = false;
1183 }
1184 ++it;
1185 }
1186
1187 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
1188 moveToTrashAction->setEnabled(enableMoveToTrash);
1189 }
1190 updatePasteAction();
1191 }
1192
1193 void DolphinMainWindow::updateViewActions()
1194 {
1195 QAction* zoomInAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::ZoomIn));
1196 if (zoomInAction != 0) {
1197 zoomInAction->setEnabled(m_activeView->isZoomInPossible());
1198 }
1199
1200 QAction* zoomOutAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::ZoomOut));
1201 if (zoomOutAction != 0) {
1202 zoomOutAction->setEnabled(m_activeView->isZoomOutPossible());
1203 }
1204
1205 QAction* action = 0;
1206 switch (m_activeView->mode()) {
1207 case DolphinView::IconsView:
1208 action = actionCollection()->action("icons");
1209 break;
1210 case DolphinView::DetailsView:
1211 action = actionCollection()->action("details");
1212 break;
1213 default:
1214 break;
1215 }
1216
1217 if (action != 0) {
1218 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
1219 toggleAction->setChecked(true);
1220 }
1221
1222 slotSortingChanged(m_activeView->sorting());
1223 slotSortOrderChanged(m_activeView->sortOrder());
1224
1225 KToggleAction* showFilterBarAction =
1226 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
1227 showFilterBarAction->setChecked(m_activeView->isFilterBarVisible());
1228
1229 KToggleAction* showHiddenFilesAction =
1230 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
1231 showHiddenFilesAction->setChecked(m_activeView->showHiddenFiles());
1232
1233 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
1234 splitAction->setChecked(m_view[SecondaryIdx] != 0);
1235 }
1236
1237 void DolphinMainWindow::updateGoActions()
1238 {
1239 QAction* goUpAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Up));
1240 const KUrl& currentUrl = m_activeView->url();
1241 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1242 }
1243
1244 void DolphinMainWindow::copyUrls(const KUrl::List& source, const KUrl& dest)
1245 {
1246 KonqOperations::copy(this, KonqOperations::COPY, source, dest);
1247 m_undoOperations.append(KonqOperations::COPY);
1248 }
1249
1250 void DolphinMainWindow::moveUrls(const KUrl::List& source, const KUrl& dest)
1251 {
1252 KonqOperations::copy(this, KonqOperations::MOVE, source, dest);
1253 m_undoOperations.append(KonqOperations::MOVE);
1254 }
1255
1256 void DolphinMainWindow::linkUrls(const KUrl::List& source, const KUrl& dest)
1257 {
1258 KonqOperations::copy(this, KonqOperations::LINK, source, dest);
1259 m_undoOperations.append(KonqOperations::LINK);
1260 }
1261
1262 void DolphinMainWindow::clearStatusBar()
1263 {
1264 m_activeView->statusBar()->clear();
1265 }
1266
1267 void DolphinMainWindow::connectViewSignals(int viewIndex)
1268 {
1269 DolphinView* view = m_view[viewIndex];
1270 connect(view, SIGNAL(modeChanged()),
1271 this, SLOT(slotViewModeChanged()));
1272 connect(view, SIGNAL(showHiddenFilesChanged()),
1273 this, SLOT(slotShowHiddenFilesChanged()));
1274 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
1275 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
1276 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
1277 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
1278 connect(view, SIGNAL(selectionChanged()),
1279 this, SLOT(slotSelectionChanged()));
1280 connect(view, SIGNAL(showFilterBarChanged(bool)),
1281 this, SLOT(updateFilterBarAction(bool)));
1282
1283 const UrlNavigator* navigator = view->urlNavigator();
1284 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1285 this, SLOT(slotUrlChanged(const KUrl&)));
1286 connect(navigator, SIGNAL(historyChanged()),
1287 this, SLOT(slotHistoryChanged()));
1288
1289 }
1290
1291 DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
1292 KonqUndoManager::UiInterface(mainWin),
1293 m_mainWin(mainWin)
1294 {
1295 assert(m_mainWin != 0);
1296 }
1297
1298 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1299 {
1300 }
1301
1302 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1303 {
1304 DolphinStatusBar* statusBar = m_mainWin->activeView()->statusBar();
1305 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1306 }
1307
1308 #include "dolphinmainwindow.moc"