]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Do a custom error handling in for the 'Create New...' submenu. Thanks to David for...
[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::instance()->instanceName());
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::cut()
492 {
493 QMimeData* mimeData = new QMimeData();
494 const KUrl::List kdeUrls = m_activeView->selectedUrls();
495 const KUrl::List mostLocalUrls;
496 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
497 QApplication::clipboard()->setMimeData(mimeData);
498 }
499
500 void DolphinMainWindow::copy()
501 {
502 QMimeData* mimeData = new QMimeData();
503 const KUrl::List kdeUrls = m_activeView->selectedUrls();
504 const KUrl::List mostLocalUrls;
505 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
506
507 QApplication::clipboard()->setMimeData(mimeData);
508 }
509
510 void DolphinMainWindow::paste()
511 {
512 QClipboard* clipboard = QApplication::clipboard();
513 const QMimeData* mimeData = clipboard->mimeData();
514
515 clearStatusBar();
516
517 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
518
519 // per default the pasting is done into the current Url of the view
520 KUrl destUrl(m_activeView->url());
521
522 // check whether the pasting should be done into a selected directory
523 KUrl::List selectedUrls = m_activeView->selectedUrls();
524 if (selectedUrls.count() == 1) {
525 const KFileItem fileItem(S_IFDIR,
526 KFileItem::Unknown,
527 selectedUrls.first(),
528 true);
529 if (fileItem.isDir()) {
530 // only one item is selected which is a directory, hence paste
531 // into this directory
532 destUrl = selectedUrls.first();
533 }
534 }
535
536 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
537 moveUrls(sourceUrls, destUrl);
538 clipboard->clear();
539 }
540 else {
541 copyUrls(sourceUrls, destUrl);
542 }
543 }
544
545 void DolphinMainWindow::updatePasteAction()
546 {
547 QAction* pasteAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
548 if (pasteAction == 0) {
549 return;
550 }
551
552 QString text(i18n("Paste"));
553 QClipboard* clipboard = QApplication::clipboard();
554 const QMimeData* mimeData = clipboard->mimeData();
555
556 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
557 if (!urls.isEmpty()) {
558 pasteAction->setEnabled(true);
559
560 const int count = urls.count();
561 if (count == 1) {
562 pasteAction->setText(i18n("Paste 1 File"));
563 }
564 else {
565 pasteAction->setText(i18n("Paste %1 Files").arg(count));
566 }
567 }
568 else {
569 pasteAction->setEnabled(false);
570 pasteAction->setText(i18n("Paste"));
571 }
572
573 if (pasteAction->isEnabled()) {
574 KUrl::List urls = m_activeView->selectedUrls();
575 const uint count = urls.count();
576 if (count > 1) {
577 // pasting should not be allowed when more than one file
578 // is selected
579 pasteAction->setEnabled(false);
580 }
581 else if (count == 1) {
582 // Only one file is selected. Pasting is only allowed if this
583 // file is a directory.
584 // TODO: this doesn't work with remote protocols; instead we need a
585 // m_activeView->selectedFileItems() to get the real KFileItems
586 const KFileItem fileItem(S_IFDIR,
587 KFileItem::Unknown,
588 urls.first(),
589 true);
590 pasteAction->setEnabled(fileItem.isDir());
591 }
592 }
593 }
594
595 void DolphinMainWindow::selectAll()
596 {
597 clearStatusBar();
598 m_activeView->selectAll();
599 }
600
601 void DolphinMainWindow::invertSelection()
602 {
603 clearStatusBar();
604 m_activeView->invertSelection();
605 }
606 void DolphinMainWindow::setIconsView()
607 {
608 m_activeView->setMode(DolphinView::IconsView);
609 }
610
611 void DolphinMainWindow::setDetailsView()
612 {
613 m_activeView->setMode(DolphinView::DetailsView);
614 }
615
616 void DolphinMainWindow::sortByName()
617 {
618 m_activeView->setSorting(DolphinView::SortByName);
619 }
620
621 void DolphinMainWindow::sortBySize()
622 {
623 m_activeView->setSorting(DolphinView::SortBySize);
624 }
625
626 void DolphinMainWindow::sortByDate()
627 {
628 m_activeView->setSorting(DolphinView::SortByDate);
629 }
630
631 void DolphinMainWindow::toggleSortOrder()
632 {
633 const Qt::SortOrder order = (m_activeView->sortOrder() == Qt::Ascending) ?
634 Qt::Descending :
635 Qt::Ascending;
636 m_activeView->setSortOrder(order);
637 }
638
639 void DolphinMainWindow::toggleSplitView()
640 {
641 if (m_view[SecondaryIdx] == 0) {
642 const int newWidth = (m_view[PrimaryIdx]->width() - m_splitter->handleWidth()) / 2;
643 // create a secondary view
644 m_view[SecondaryIdx] = new DolphinView(this,
645 0,
646 m_view[PrimaryIdx]->url(),
647 m_view[PrimaryIdx]->mode(),
648 m_view[PrimaryIdx]->showHiddenFiles());
649 connectViewSignals(SecondaryIdx);
650 m_splitter->addWidget(m_view[SecondaryIdx]);
651 m_splitter->setSizes(QList<int>() << newWidth << newWidth);
652 m_view[SecondaryIdx]->show();
653 }
654 else {
655 // remove secondary view
656 if (m_activeView == m_view[PrimaryIdx]) {
657 m_view[SecondaryIdx]->close();
658 m_view[SecondaryIdx]->deleteLater();
659 m_view[SecondaryIdx] = 0;
660 setActiveView(m_view[PrimaryIdx]);
661 }
662 else {
663 // The secondary view is active, hence from the users point of view
664 // the content of the secondary view should be moved to the primary view.
665 // From an implementation point of view it is more efficient to close
666 // the primary view and exchange the internal pointers afterwards.
667 m_view[PrimaryIdx]->close();
668 delete m_view[PrimaryIdx];
669 m_view[PrimaryIdx] = m_view[SecondaryIdx];
670 m_view[SecondaryIdx] = 0;
671 setActiveView(m_view[PrimaryIdx]);
672 }
673 }
674 }
675
676 void DolphinMainWindow::reloadView()
677 {
678 clearStatusBar();
679 m_activeView->reload();
680 }
681
682 void DolphinMainWindow::stopLoading()
683 {
684 }
685
686 void DolphinMainWindow::togglePreview()
687 {
688 clearStatusBar();
689
690 const KToggleAction* showPreviewAction =
691 static_cast<KToggleAction*>(actionCollection()->action("show_preview"));
692 const bool show = showPreviewAction->isChecked();
693 m_activeView->setShowPreview(show);
694 }
695
696 void DolphinMainWindow::toggleShowHiddenFiles()
697 {
698 clearStatusBar();
699
700 const KToggleAction* showHiddenFilesAction =
701 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
702 const bool show = showHiddenFilesAction->isChecked();
703 m_activeView->setShowHiddenFiles(show);
704 }
705
706 void DolphinMainWindow::showFilterBar()
707 {
708 const KToggleAction* showFilterBarAction =
709 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
710 const bool show = showFilterBarAction->isChecked();
711 m_activeView->showFilterBar(show);
712 }
713
714 void DolphinMainWindow::zoomIn()
715 {
716 m_activeView->zoomIn();
717 updateViewActions();
718 }
719
720 void DolphinMainWindow::zoomOut()
721 {
722 m_activeView->zoomOut();
723 updateViewActions();
724 }
725
726 void DolphinMainWindow::toggleEditLocation()
727 {
728 clearStatusBar();
729
730 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
731
732 bool editOrBrowse = action->isChecked();
733 m_activeView->setUrlEditable(editOrBrowse);
734 }
735
736 void DolphinMainWindow::editLocation()
737 {
738 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
739 action->setChecked(true);
740 m_activeView->setUrlEditable(true);
741 }
742
743 void DolphinMainWindow::adjustViewProperties()
744 {
745 clearStatusBar();
746 ViewPropertiesDialog dlg(m_activeView);
747 dlg.exec();
748 }
749
750 void DolphinMainWindow::goBack()
751 {
752 clearStatusBar();
753 m_activeView->goBack();
754 }
755
756 void DolphinMainWindow::goForward()
757 {
758 clearStatusBar();
759 m_activeView->goForward();
760 }
761
762 void DolphinMainWindow::goUp()
763 {
764 clearStatusBar();
765 m_activeView->goUp();
766 }
767
768 void DolphinMainWindow::goHome()
769 {
770 clearStatusBar();
771 m_activeView->goHome();
772 }
773
774 void DolphinMainWindow::openTerminal()
775 {
776 QString command("konsole --workdir \"");
777 command.append(m_activeView->url().path());
778 command.append('\"');
779
780 KRun::runCommand(command, "Konsole", "konsole");
781 }
782
783 void DolphinMainWindow::findFile()
784 {
785 KRun::run("kfind", m_activeView->url());
786 }
787
788 void DolphinMainWindow::compareFiles()
789 {
790 // The method is only invoked if exactly 2 files have
791 // been selected. The selected files may be:
792 // - both in the primary view
793 // - both in the secondary view
794 // - one in the primary view and the other in the secondary
795 // view
796 assert(m_view[PrimaryIdx] != 0);
797
798 KUrl urlA;
799 KUrl urlB;
800 KUrl::List urls = m_view[PrimaryIdx]->selectedUrls();
801
802 switch (urls.count()) {
803 case 0: {
804 assert(m_view[SecondaryIdx] != 0);
805 urls = m_view[SecondaryIdx]->selectedUrls();
806 assert(urls.count() == 2);
807 urlA = urls[0];
808 urlB = urls[1];
809 break;
810 }
811
812 case 1: {
813 urlA = urls[0];
814 assert(m_view[SecondaryIdx] != 0);
815 urls = m_view[SecondaryIdx]->selectedUrls();
816 assert(urls.count() == 1);
817 urlB = urls[0];
818 break;
819 }
820
821 case 2: {
822 urlA = urls[0];
823 urlB = urls[1];
824 break;
825 }
826
827 default: {
828 // may not happen: compareFiles may only get invoked if 2
829 // files are selected
830 assert(false);
831 }
832 }
833
834 QString command("kompare -c \"");
835 command.append(urlA.pathOrUrl());
836 command.append("\" \"");
837 command.append(urlB.pathOrUrl());
838 command.append('\"');
839 KRun::runCommand(command, "Kompare", "kompare");
840
841 }
842
843 void DolphinMainWindow::editSettings()
844 {
845 // TODO: make a static method for opening the settings dialog
846 DolphinSettingsDialog dlg(this);
847 dlg.exec();
848 }
849
850 void DolphinMainWindow::init()
851 {
852 // Check whether Dolphin runs the first time. If yes then
853 // a proper default window size is given at the end of DolphinMainWindow::init().
854 GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
855 const bool firstRun = generalSettings->firstRun();
856
857 setAcceptDrops(true);
858
859 m_splitter = new QSplitter(this);
860
861 DolphinSettings& settings = DolphinSettings::instance();
862
863 KBookmarkManager* manager = settings.bookmarkManager();
864 assert(manager != 0);
865 KBookmarkGroup root = manager->root();
866 if (root.first().isNull()) {
867 root.addBookmark(manager, i18n("Home"), settings.generalSettings()->homeUrl(), "folder_home");
868 root.addBookmark(manager, i18n("Storage Media"), KUrl("media:/"), "blockdevice");
869 root.addBookmark(manager, i18n("Network"), KUrl("remote:/"), "network_local");
870 root.addBookmark(manager, i18n("Root"), KUrl("/"), "folder_red");
871 root.addBookmark(manager, i18n("Trash"), KUrl("trash:/"), "trashcan_full");
872 }
873
874 setupActions();
875
876 const KUrl& homeUrl = root.first().url();
877 setCaption(homeUrl.fileName());
878 ViewProperties props(homeUrl);
879 m_view[PrimaryIdx] = new DolphinView(this,
880 m_splitter,
881 homeUrl,
882 props.viewMode(),
883 props.showHiddenFiles());
884 connectViewSignals(PrimaryIdx);
885 m_view[PrimaryIdx]->show();
886
887 m_activeView = m_view[PrimaryIdx];
888
889 setCentralWidget(m_splitter);
890 setupDockWidgets();
891
892 setupGUI(Keys|Save|Create|ToolBar);
893 createGUI();
894
895 stateChanged("new_file");
896 setAutoSaveSettings();
897
898 QClipboard* clipboard = QApplication::clipboard();
899 connect(clipboard, SIGNAL(dataChanged()),
900 this, SLOT(updatePasteAction()));
901 updatePasteAction();
902 updateGoActions();
903
904 loadSettings();
905
906 if (firstRun) {
907 // assure a proper default size if Dolphin runs the first time
908 resize(640, 480);
909 }
910 }
911
912 void DolphinMainWindow::loadSettings()
913 {
914 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
915
916 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
917 if (settings->splitView()) {
918 splitAction->setChecked(true);
919 toggleSplitView();
920 }
921
922 updateViewActions();
923
924 // TODO: I assume there will be a generic way in KDE 4 to restore the docks
925 // of the main window. In the meantime they are restored manually (see also
926 // DolphinMainWindow::closeEvent() for more details):
927 QString filename = KStandardDirs::locateLocal("data", KGlobal::instance()->instanceName());
928 filename.append("/panels_layout");
929 QFile file(filename);
930 if (file.open(QIODevice::ReadOnly)) {
931 QByteArray data = file.readAll();
932 restoreState(data);
933 file.close();
934 }
935 }
936
937 void DolphinMainWindow::setupActions()
938 {
939 // setup 'File' menu
940 m_newMenu = new DolphinNewMenu(this);
941 KMenu* menu = m_newMenu->menu();
942 menu->setTitle(i18n("Create New..."));
943 menu->setIcon(SmallIcon("filenew"));
944 connect(menu, SIGNAL(aboutToShow()),
945 this, SLOT(updateNewMenu()));
946
947 QAction* action = actionCollection()->addAction("new_window");
948 action->setIcon(KIcon("window_new"));
949 action->setText(i18n("New &Window"));
950 connect(action, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
951
952 QAction* rename = actionCollection()->addAction("rename");
953 rename->setText(i18n("Rename"));
954 rename->setShortcut(Qt::Key_F2);
955 connect(rename, SIGNAL(triggered()), this, SLOT(rename()));
956
957 QAction* moveToTrash = actionCollection()->addAction("move_to_trash");
958 moveToTrash->setText(i18n("Move to Trash"));
959 moveToTrash->setIcon(KIcon("edittrash"));
960 moveToTrash->setShortcut(QKeySequence::Delete);
961 connect(moveToTrash, SIGNAL(triggered()), this, SLOT(moveToTrash()));
962
963 QAction* deleteAction = actionCollection()->addAction("delete");
964 deleteAction->setText(i18n("Delete"));
965 deleteAction->setShortcut(Qt::ALT | Qt::Key_Delete);
966 deleteAction->setIcon(KIcon("editdelete"));
967 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItems()));
968
969 QAction* properties = actionCollection()->addAction("properties");
970 properties->setText(i18n("Propert&ies"));
971 properties->setShortcut(Qt::Key_Alt | Qt::Key_Return);
972 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
973
974 KStandardAction::quit(this, SLOT(quit()), actionCollection());
975
976 // setup 'Edit' menu
977 KStandardAction::undo(KonqUndoManager::self(),
978 SLOT(undo()),
979 actionCollection());
980
981 KStandardAction::cut(this, SLOT(cut()), actionCollection());
982 KStandardAction::copy(this, SLOT(copy()), actionCollection());
983 KStandardAction::paste(this, SLOT(paste()), actionCollection());
984
985 QAction* selectAll = actionCollection()->addAction("select_all");
986 selectAll->setText(i18n("Select All"));
987 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
988 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
989
990 QAction* invertSelection = actionCollection()->addAction("invert_selection");
991 invertSelection->setText(i18n("Invert Selection"));
992 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
993 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
994
995 // setup 'View' menu
996 KStandardAction::zoomIn(this,
997 SLOT(zoomIn()),
998 actionCollection());
999
1000 KStandardAction::zoomOut(this,
1001 SLOT(zoomOut()),
1002 actionCollection());
1003
1004 KToggleAction* iconsView = actionCollection()->add<KToggleAction>("icons");
1005 iconsView->setText(i18n("Icons"));
1006 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
1007 iconsView->setIcon(KIcon("view_icon"));
1008 connect(iconsView, SIGNAL(triggered()), this, SLOT(setIconsView()));
1009
1010 KToggleAction* detailsView = actionCollection()->add<KToggleAction>("details");
1011 detailsView->setText(i18n("Details"));
1012 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1013 detailsView->setIcon(KIcon("view_text"));
1014 connect(detailsView, SIGNAL(triggered()), this, SLOT(setDetailsView()));
1015
1016 QActionGroup* viewModeGroup = new QActionGroup(this);
1017 viewModeGroup->addAction(iconsView);
1018 viewModeGroup->addAction(detailsView);
1019
1020 KToggleAction* sortByName = actionCollection()->add<KToggleAction>("by_name");
1021 sortByName->setText(i18n("By Name"));
1022 connect(sortByName, SIGNAL(triggered()), this, SLOT(sortByName()));
1023
1024 KToggleAction* sortBySize = actionCollection()->add<KToggleAction>("by_size");
1025 sortBySize->setText(i18n("By Size"));
1026 connect(sortBySize, SIGNAL(triggered()), this, SLOT(sortBySize()));
1027
1028 KToggleAction* sortByDate = actionCollection()->add<KToggleAction>("by_date");
1029 sortByDate->setText(i18n("By Date"));
1030 connect(sortByDate, SIGNAL(triggered()), this, SLOT(sortByDate()));
1031
1032 QActionGroup* sortGroup = new QActionGroup(this);
1033 sortGroup->addAction(sortByName);
1034 sortGroup->addAction(sortBySize);
1035 sortGroup->addAction(sortByDate);
1036
1037 KToggleAction* sortDescending = actionCollection()->add<KToggleAction>("descending");
1038 sortDescending->setText(i18n("Descending"));
1039 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
1040
1041 KToggleAction* showPreview = actionCollection()->add<KToggleAction>("show_preview");
1042 showPreview->setText(i18n("Show Preview"));
1043 connect(showPreview, SIGNAL(triggered()), this, SLOT(togglePreview()));
1044
1045 KToggleAction* showHiddenFiles = actionCollection()->add<KToggleAction>("show_hidden_files");
1046 showHiddenFiles->setText(i18n("Show Hidden Files"));
1047 //showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_ KDE4-TODO: what Qt-Key represents '.'?
1048 connect(showHiddenFiles, SIGNAL(triggered()), this, SLOT(toggleShowHiddenFiles()));
1049
1050 KToggleAction* split = actionCollection()->add<KToggleAction>("split_view");
1051 split->setText(i18n("Split View"));
1052 split->setShortcut(Qt::Key_F10);
1053 split->setIcon(KIcon("view_left_right"));
1054 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1055
1056 QAction* reload = actionCollection()->addAction("reload");
1057 reload->setText(i18n("Reload"));
1058 reload->setShortcut(Qt::Key_F5);
1059 reload->setIcon(KIcon("reload"));
1060 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1061
1062 QAction* stop = actionCollection()->addAction("stop");
1063 stop->setText(i18n("Stop"));
1064 stop->setIcon(KIcon("stop"));
1065 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1066
1067 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1068 showFullLocation->setText(i18n("Show Full Location"));
1069 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1070 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1071
1072 KToggleAction* editLocation = actionCollection()->add<KToggleAction>("edit_location");
1073 editLocation->setText(i18n("Edit Location"));
1074 editLocation->setShortcut(Qt::Key_F6);
1075 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
1076
1077 QAction* adjustViewProps = actionCollection()->addAction("view_properties");
1078 adjustViewProps->setText(i18n("Adjust View Properties..."));
1079 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
1080
1081 // setup 'Go' menu
1082 KStandardAction::back(this, SLOT(goBack()), actionCollection());
1083 KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1084 KStandardAction::up(this, SLOT(goUp()), actionCollection());
1085 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1086
1087 // setup 'Tools' menu
1088 QAction* openTerminal = actionCollection()->addAction("open_terminal");
1089 openTerminal->setText(i18n("Open Terminal"));
1090 openTerminal->setShortcut(Qt::Key_F4);
1091 openTerminal->setIcon(KIcon("konsole"));
1092 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1093
1094 QAction* findFile = actionCollection()->addAction("find_file");
1095 findFile->setText(i18n("Find File..."));
1096 findFile->setShortcut(Qt::Key_F);
1097 findFile->setIcon(KIcon("filefind"));
1098 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
1099
1100 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1101 showFilterBar->setText(i18n("Show Filter Bar"));
1102 showFilterBar->setShortcut(Qt::Key_Slash);
1103 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1104
1105 QAction* compareFiles = actionCollection()->addAction("compare_files");
1106 compareFiles->setText(i18n("Compare Files"));
1107 compareFiles->setIcon(KIcon("kompare"));
1108 compareFiles->setEnabled(false);
1109 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1110
1111 // setup 'Settings' menu
1112 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1113 }
1114
1115 void DolphinMainWindow::setupDockWidgets()
1116 {
1117 QDockWidget* shortcutsDock = new QDockWidget(i18n("Bookmarks"));
1118 shortcutsDock->setObjectName("bookmarksDock");
1119 shortcutsDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1120 shortcutsDock->setWidget(new BookmarksSidebarPage(this));
1121
1122 shortcutsDock->toggleViewAction()->setText(i18n("Show Bookmarks Panel"));
1123 actionCollection()->addAction("show_bookmarks_panel", shortcutsDock->toggleViewAction());
1124
1125 addDockWidget(Qt::LeftDockWidgetArea, shortcutsDock);
1126
1127 QDockWidget* infoDock = new QDockWidget(i18n("Information"));
1128 infoDock->setObjectName("infoDock");
1129 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1130 infoDock->setWidget(new InfoSidebarPage(this));
1131
1132 infoDock->toggleViewAction()->setText(i18n("Show Information Panel"));
1133 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1134
1135 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1136 }
1137
1138 void DolphinMainWindow::updateHistory()
1139 {
1140 int index = 0;
1141 const QLinkedList<UrlNavigator::HistoryElem> list = m_activeView->urlHistory(index);
1142
1143 QAction* backAction = actionCollection()->action("go_back");
1144 if (backAction != 0) {
1145 backAction->setEnabled(index < static_cast<int>(list.count()) - 1);
1146 }
1147
1148 QAction* forwardAction = actionCollection()->action("go_forward");
1149 if (forwardAction != 0) {
1150 forwardAction->setEnabled(index > 0);
1151 }
1152 }
1153
1154 void DolphinMainWindow::updateEditActions()
1155 {
1156 const KFileItemList list = m_activeView->selectedItems();
1157 if (list.isEmpty()) {
1158 stateChanged("has_no_selection");
1159 }
1160 else {
1161 stateChanged("has_selection");
1162
1163 QAction* renameAction = actionCollection()->action("rename");
1164 if (renameAction != 0) {
1165 renameAction->setEnabled(list.count() >= 1);
1166 }
1167
1168 bool enableMoveToTrash = true;
1169
1170 KFileItemList::const_iterator it = list.begin();
1171 const KFileItemList::const_iterator end = list.end();
1172 while (it != end) {
1173 KFileItem* item = *it;
1174 const KUrl& url = item->url();
1175 // only enable the 'Move to Trash' action for local files
1176 if (!url.isLocalFile()) {
1177 enableMoveToTrash = false;
1178 }
1179 ++it;
1180 }
1181
1182 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
1183 moveToTrashAction->setEnabled(enableMoveToTrash);
1184 }
1185 updatePasteAction();
1186 }
1187
1188 void DolphinMainWindow::updateViewActions()
1189 {
1190 QAction* zoomInAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::ZoomIn));
1191 if (zoomInAction != 0) {
1192 zoomInAction->setEnabled(m_activeView->isZoomInPossible());
1193 }
1194
1195 QAction* zoomOutAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::ZoomOut));
1196 if (zoomOutAction != 0) {
1197 zoomOutAction->setEnabled(m_activeView->isZoomOutPossible());
1198 }
1199
1200 QAction* action = 0;
1201 switch (m_activeView->mode()) {
1202 case DolphinView::IconsView:
1203 action = actionCollection()->action("icons");
1204 break;
1205 case DolphinView::DetailsView:
1206 action = actionCollection()->action("details");
1207 break;
1208 default:
1209 break;
1210 }
1211
1212 if (action != 0) {
1213 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
1214 toggleAction->setChecked(true);
1215 }
1216
1217 slotSortingChanged(m_activeView->sorting());
1218 slotSortOrderChanged(m_activeView->sortOrder());
1219
1220 KToggleAction* showFilterBarAction =
1221 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
1222 showFilterBarAction->setChecked(m_activeView->isFilterBarVisible());
1223
1224 KToggleAction* showHiddenFilesAction =
1225 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
1226 showHiddenFilesAction->setChecked(m_activeView->showHiddenFiles());
1227
1228 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
1229 splitAction->setChecked(m_view[SecondaryIdx] != 0);
1230 }
1231
1232 void DolphinMainWindow::updateGoActions()
1233 {
1234 QAction* goUpAction = actionCollection()->action(KStandardAction::stdName(KStandardAction::Up));
1235 const KUrl& currentUrl = m_activeView->url();
1236 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1237 }
1238
1239 void DolphinMainWindow::copyUrls(const KUrl::List& source, const KUrl& dest)
1240 {
1241 KonqOperations::copy(this, KonqOperations::COPY, source, dest);
1242 m_undoOperations.append(KonqOperations::COPY);
1243 }
1244
1245 void DolphinMainWindow::moveUrls(const KUrl::List& source, const KUrl& dest)
1246 {
1247 KonqOperations::copy(this, KonqOperations::MOVE, source, dest);
1248 m_undoOperations.append(KonqOperations::MOVE);
1249 }
1250
1251 void DolphinMainWindow::linkUrls(const KUrl::List& source, const KUrl& dest)
1252 {
1253 KonqOperations::copy(this, KonqOperations::LINK, source, dest);
1254 m_undoOperations.append(KonqOperations::LINK);
1255 }
1256
1257 void DolphinMainWindow::clearStatusBar()
1258 {
1259 m_activeView->statusBar()->clear();
1260 }
1261
1262 void DolphinMainWindow::connectViewSignals(int viewIndex)
1263 {
1264 DolphinView* view = m_view[viewIndex];
1265 connect(view, SIGNAL(modeChanged()),
1266 this, SLOT(slotViewModeChanged()));
1267 connect(view, SIGNAL(showHiddenFilesChanged()),
1268 this, SLOT(slotShowHiddenFilesChanged()));
1269 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
1270 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
1271 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
1272 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
1273 connect(view, SIGNAL(selectionChanged()),
1274 this, SLOT(slotSelectionChanged()));
1275 connect(view, SIGNAL(showFilterBarChanged(bool)),
1276 this, SLOT(updateFilterBarAction(bool)));
1277
1278 const UrlNavigator* navigator = view->urlNavigator();
1279 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1280 this, SLOT(slotUrlChanged(const KUrl&)));
1281 connect(navigator, SIGNAL(historyChanged()),
1282 this, SLOT(slotHistoryChanged()));
1283
1284 }
1285
1286 DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
1287 KonqUndoManager::UiInterface(mainWin),
1288 m_mainWin(mainWin)
1289 {
1290 assert(m_mainWin != 0);
1291 }
1292
1293 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1294 {
1295 }
1296
1297 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1298 {
1299 DolphinStatusBar* statusBar = m_mainWin->activeView()->statusBar();
1300 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1301 }
1302
1303 #include "dolphinmainwindow.moc"