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