]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Cleanup of signal/slot handling between the dolphin view and the main window: now...
[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 <kactioncollection.h>
27 #include <ktoggleaction.h>
28 #include <kbookmarkmanager.h>
29 #include <kglobal.h>
30 #include <kpropertiesdialog.h>
31 #include <kicon.h>
32 #include <kiconloader.h>
33 #include <kdeversion.h>
34 #include <kstatusbar.h>
35 #include <kio/netaccess.h>
36 #include <kfiledialog.h>
37 #include <kconfig.h>
38 #include <kurl.h>
39 #include <kstdaccel.h>
40 #include <kaction.h>
41 #include <kstdaction.h>
42 #include <kmenu.h>
43 #include <kio/renamedlg.h>
44 #include <kinputdialog.h>
45 #include <kshell.h>
46 #include <kdesktopfile.h>
47 #include <kstandarddirs.h>
48 #include <kprotocolinfo.h>
49 #include <kmessagebox.h>
50 #include <kservice.h>
51 #include <kstandarddirs.h>
52 #include <krun.h>
53 #include <klocale.h>
54
55 #include <qclipboard.h>
56 #include <q3dragobject.h>
57 //Added by qt3to4:
58 #include <Q3ValueList>
59 #include <QCloseEvent>
60 #include <QSplitter>
61 #include <QDockWidget>
62
63 #include "urlnavigator.h"
64 #include "viewpropertiesdialog.h"
65 #include "viewproperties.h"
66 #include "dolphinsettings.h"
67 #include "dolphinsettingsdialog.h"
68 #include "dolphinstatusbar.h"
69 #include "dolphinapplication.h"
70 #include "undomanager.h"
71 #include "progressindicator.h"
72 #include "dolphinsettings.h"
73 #include "bookmarkssidebarpage.h"
74 #include "infosidebarpage.h"
75 #include "generalsettings.h"
76 #include "dolphinapplication.h"
77
78
79 DolphinMainWindow::DolphinMainWindow() :
80 KMainWindow(0),
81 m_splitter(0),
82 m_activeView(0),
83 m_clipboardContainsCutData(false)
84 {
85 setObjectName("Dolphin");
86 m_view[PrimaryIdx] = 0;
87 m_view[SecondaryIdx] = 0;
88
89 m_fileGroupActions.setAutoDelete(true);
90
91 // TODO: the following members are not used yet. See documentation
92 // of DolphinMainWindow::linkGroupActions() and DolphinMainWindow::linkToDeviceActions()
93 // in the header file for details.
94 //m_linkGroupActions.setAutoDelete(true);
95 //m_linkToDeviceActions.setAutoDelete(true);
96 }
97
98 DolphinMainWindow::~DolphinMainWindow()
99 {
100 /*
101 * bye, bye managed window
102 */
103 DolphinApplication::app()->removeMainWindow( this );
104 }
105
106 void DolphinMainWindow::setActiveView(DolphinView* view)
107 {
108 assert((view == m_view[PrimaryIdx]) || (view == m_view[SecondaryIdx]));
109 if (m_activeView == view) {
110 return;
111 }
112
113 m_activeView = view;
114
115 updateHistory();
116 updateEditActions();
117 updateViewActions();
118 updateGoActions();
119
120 setCaption(m_activeView->url().fileName());
121
122 emit activeViewChanged();
123 }
124
125 void DolphinMainWindow::dropUrls(const KUrl::List& urls,
126 const KUrl& destination)
127 {
128 int selectedIndex = -1;
129
130 /* KDE4-TODO
131 const ButtonState keyboardState = KApplication::keyboardMouseState();
132 const bool shiftPressed = (keyboardState & ShiftButton) > 0;
133 const bool controlPressed = (keyboardState & ControlButton) > 0;
134
135
136
137 if (shiftPressed && controlPressed) {
138 // shortcut for 'Linke Here' is used
139 selectedIndex = 2;
140 }
141 else if (controlPressed) {
142 // shortcut for 'Copy Here' is used
143 selectedIndex = 1;
144 }
145 else if (shiftPressed) {
146 // shortcut for 'Move Here' is used
147 selectedIndex = 0;
148 }
149 else*/ {
150 // no shortcut is used, hence open a popup menu
151 KMenu popup(this);
152
153 popup.insertItem(SmallIcon("goto"), i18n("&Move Here") + "\t" /* KDE4-TODO: + KKey::modFlagLabel(KKey::SHIFT)*/, 0);
154 popup.insertItem(SmallIcon("editcopy"), i18n( "&Copy Here" ) /* KDE4-TODO + "\t" + KKey::modFlagLabel(KKey::CTRL)*/, 1);
155 popup.insertItem(i18n("&Link Here") /* KDE4-TODO + "\t" + KKey::modFlagLabel((KKey::ModFlag)(KKey::CTRL|KKey::SHIFT)) */, 2);
156 popup.insertSeparator();
157 popup.insertItem(SmallIcon("stop"), i18n("Cancel"), 3);
158 popup.setAccel(i18n("Escape"), 3);
159
160 /* KDE4-TODO: selectedIndex = popup.exec(QCursor::pos()); */
161 popup.exec(QCursor::pos());
162 selectedIndex = 0; // KD4-TODO: use QAction instead of switch below
163 // See libkonq/konq_operations.cc: KonqOperations::doDropFileCopy() (and doDrop, the main method)
164 }
165
166 if (selectedIndex < 0) {
167 return;
168 }
169
170 switch (selectedIndex) {
171 case 0: {
172 // 'Move Here' has been selected
173 updateViewProperties(urls);
174 moveUrls(urls, destination);
175 break;
176 }
177
178 case 1: {
179 // 'Copy Here' has been selected
180 updateViewProperties(urls);
181 copyUrls(urls, destination);
182 break;
183 }
184
185 case 2: {
186 // 'Link Here' has been selected
187 KIO::Job* job = KIO::link(urls, destination);
188 addPendingUndoJob(job, DolphinCommand::Link, urls, destination);
189 break;
190 }
191
192 default:
193 // 'Cancel' has been selected
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.isShowHiddenFilesEnabled());
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->isShowHiddenFilesEnabled());
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::redo()
313 {
314 UndoManager::instance().redo(this);
315 }
316
317 void DolphinMainWindow::undo()
318 {
319 UndoManager::instance().undo(this);
320 }
321
322 void DolphinMainWindow::openNewMainWindow()
323 {
324 DolphinApplication::app()->createMainWindow()->show();
325 }
326
327 void DolphinMainWindow::closeEvent(QCloseEvent* event)
328 {
329 // KDE4-TODO
330 //KConfig* config = KGlobal::config();
331 //config->setGroup("General");
332 //config->writeEntry("First Run", false);
333
334 DolphinSettings& settings = DolphinSettings::instance();
335 GeneralSettings* generalSettings = settings.generalSettings();
336 generalSettings->setFirstRun(false);
337
338 settings.save();
339
340 KMainWindow::closeEvent(event);
341 }
342
343 void DolphinMainWindow::saveProperties(KConfig* config)
344 {
345 config->setGroup("Primary view");
346 config->writeEntry("Url", m_view[PrimaryIdx]->url().url());
347 config->writeEntry("Editable Url", m_view[PrimaryIdx]->isUrlEditable());
348 if (m_view[SecondaryIdx] != 0) {
349 config->setGroup("Secondary view");
350 config->writeEntry("Url", m_view[SecondaryIdx]->url().url());
351 config->writeEntry("Editable Url", m_view[SecondaryIdx]->isUrlEditable());
352 }
353 }
354
355 void DolphinMainWindow::readProperties(KConfig* config)
356 {
357 config->setGroup("Primary view");
358 m_view[PrimaryIdx]->setUrl(config->readEntry("Url"));
359 m_view[PrimaryIdx]->setUrlEditable(config->readBoolEntry("Editable Url"));
360 if (config->hasGroup("Secondary view")) {
361 config->setGroup("Secondary view");
362 if (m_view[SecondaryIdx] == 0) {
363 toggleSplitView();
364 }
365 m_view[SecondaryIdx]->setUrl(config->readEntry("Url"));
366 m_view[SecondaryIdx]->setUrlEditable(config->readBoolEntry("Editable Url"));
367 }
368 else if (m_view[SecondaryIdx] != 0) {
369 toggleSplitView();
370 }
371 }
372
373 void DolphinMainWindow::createFolder()
374 {
375 // Parts of the following code have been taken
376 // from the class KonqPopupMenu located in
377 // libqonq/konq_popupmenu.h of Konqueror.
378 // (Copyright (C) 2000 David Faure <faure@kde.org>,
379 // Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>)
380
381 clearStatusBar();
382
383 DolphinStatusBar* statusBar = m_activeView->statusBar();
384 const KUrl baseUrl(m_activeView->url());
385
386 QString name(i18n("New Folder"));
387 baseUrl.path(KUrl::AddTrailingSlash);
388
389
390 if (baseUrl.isLocalFile() && QFileInfo(baseUrl.path(KUrl::AddTrailingSlash) + name).exists()) {
391 name = KIO::RenameDlg::suggestName(baseUrl, i18n("New Folder"));
392 }
393
394 bool ok = false;
395 name = KInputDialog::getText(i18n("New Folder"),
396 i18n("Enter folder name:" ),
397 name,
398 &ok,
399 this);
400
401 if (!ok) {
402 // the user has pressed 'Cancel'
403 return;
404 }
405
406 assert(!name.isEmpty());
407
408 KUrl url;
409 if ((name[0] == '/') || (name[0] == '~')) {
410 url.setPath(KShell::tildeExpand(name));
411 }
412 else {
413 name = KIO::encodeFileName(name);
414 url = baseUrl;
415 url.addPath(name);
416 }
417 ok = KIO::NetAccess::mkdir(url, this);
418
419 // TODO: provide message type hint
420 if (ok) {
421 statusBar->setMessage(i18n("Created folder %1.",url.path()),
422 DolphinStatusBar::OperationCompleted);
423
424 DolphinCommand command(DolphinCommand::CreateFolder, KUrl::List(), url);
425 UndoManager::instance().addCommand(command);
426 }
427 else {
428 // Creating of the folder has been failed. Check whether the creating
429 // has been failed because a folder with the same name exists...
430 if (KIO::NetAccess::exists(url, true, this)) {
431 statusBar->setMessage(i18n("A folder named %1 already exists.",url.path()),
432 DolphinStatusBar::Error);
433 }
434 else {
435 statusBar->setMessage(i18n("Creating of folder %1 failed.",url.path()),
436 DolphinStatusBar::Error);
437 }
438
439 }
440 }
441
442 void DolphinMainWindow::createFile()
443 {
444 // Parts of the following code have been taken
445 // from the class KonqPopupMenu located in
446 // libqonq/konq_popupmenu.h of Konqueror.
447 // (Copyright (C) 2000 David Faure <faure@kde.org>,
448 // Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>)
449
450 clearStatusBar();
451
452 // TODO: const Entry& entry = m_createFileTemplates[QString(sender->name())];
453 // should be enough. Anyway: the implemantation of [] does a linear search internally too.
454 KSortableList<CreateFileEntry, QString>::ConstIterator it = m_createFileTemplates.begin();
455 KSortableList<CreateFileEntry, QString>::ConstIterator end = m_createFileTemplates.end();
456
457 const QString senderName(sender()->objectName());
458 bool found = false;
459 CreateFileEntry entry;
460 while (!found && (it != end)) {
461 if ((*it).index() == senderName) {
462 entry = (*it).value();
463 found = true;
464 }
465 else {
466 ++it;
467 }
468 }
469
470 DolphinStatusBar* statusBar = m_activeView->statusBar();
471 if (!found || !QFile::exists(entry.templatePath)) {
472 statusBar->setMessage(i18n("Could not create file."), DolphinStatusBar::Error);
473 return;
474 }
475
476 // Get the source path of the template which should be copied.
477 // The source path is part of the Url entry of the desktop file.
478 const int pos = entry.templatePath.findRev('/');
479 QString sourcePath(entry.templatePath.left(pos + 1));
480 sourcePath += KDesktopFile(entry.templatePath, true).readPathEntry("Url");
481
482 QString name(i18n(entry.name.toAscii()));
483 // Most entry names end with "..." (e. g. "HTML File..."), which is ok for
484 // menus but no good choice for a new file name -> remove the dots...
485 name.replace("...", QString::null);
486
487 // add the file extension to the name
488 name.append(sourcePath.right(sourcePath.length() - sourcePath.findRev('.')));
489
490 // Check whether a file with the current name already exists. If yes suggest automatically
491 // a unique file name (e. g. "HTML File" will be replaced by "HTML File_1").
492 const KUrl viewUrl(m_activeView->url());
493 const bool fileExists = viewUrl.isLocalFile() &&
494 QFileInfo(viewUrl.path(KUrl::AddTrailingSlash) + KIO::encodeFileName(name)).exists();
495 if (fileExists) {
496 name = KIO::RenameDlg::suggestName(viewUrl, name);
497 }
498
499 // let the user change the suggested file name
500 bool ok = false;
501 name = KInputDialog::getText(entry.name,
502 entry.comment,
503 name,
504 &ok,
505 this);
506 if (!ok) {
507 // the user has pressed 'Cancel'
508 return;
509 }
510
511 // before copying the template to the destination path check whether a file
512 // with the given name already exists
513 const QString destPath(viewUrl.pathOrUrl() + "/" + KIO::encodeFileName(name));
514 const KUrl destUrl(destPath);
515 if (KIO::NetAccess::exists(destUrl, false, this)) {
516 statusBar->setMessage(i18n("A file named %1 already exists.",name),
517 DolphinStatusBar::Error);
518 return;
519 }
520
521 // copy the template to the destination path
522 const KUrl sourceUrl(sourcePath);
523 KIO::CopyJob* job = KIO::copyAs(sourceUrl, destUrl);
524 job->setDefaultPermissions(true);
525 if (KIO::NetAccess::synchronousRun(job, this)) {
526 statusBar->setMessage(i18n("Created file %1.",name),
527 DolphinStatusBar::OperationCompleted);
528
529 KUrl::List list;
530 list.append(sourceUrl);
531 DolphinCommand command(DolphinCommand::CreateFile, list, destUrl);
532 UndoManager::instance().addCommand(command);
533
534 }
535 else {
536 statusBar->setMessage(i18n("Creating of file %1 failed.",name),
537 DolphinStatusBar::Error);
538 }
539 }
540
541 void DolphinMainWindow::rename()
542 {
543 clearStatusBar();
544 m_activeView->renameSelectedItems();
545 }
546
547 void DolphinMainWindow::moveToTrash()
548 {
549 clearStatusBar();
550 KUrl::List selectedUrls = m_activeView->selectedUrls();
551 KIO::Job* job = KIO::trash(selectedUrls);
552 addPendingUndoJob(job, DolphinCommand::Trash, selectedUrls, m_activeView->url());
553 }
554
555 void DolphinMainWindow::deleteItems()
556 {
557 clearStatusBar();
558
559 KUrl::List list = m_activeView->selectedUrls();
560 const uint itemCount = list.count();
561 assert(itemCount >= 1);
562
563 QString text;
564 if (itemCount > 1) {
565 text = i18n("Do you really want to delete the %1 selected items?",itemCount);
566 }
567 else {
568 const KUrl& url = list.first();
569 text = i18n("Do you really want to delete '%1'?",url.fileName());
570 }
571
572 const bool del = KMessageBox::warningContinueCancel(this,
573 text,
574 QString::null,
575 KGuiItem(i18n("Delete"), SmallIcon("editdelete"))
576 ) == KMessageBox::Continue;
577 if (del) {
578 KIO::Job* job = KIO::del(list);
579 connect(job, SIGNAL(result(KJob*)),
580 this, SLOT(slotHandleJobError(KJob*)));
581 connect(job, SIGNAL(result(KJob*)),
582 this, SLOT(slotDeleteFileFinished(KJob*)));
583 }
584 }
585
586 void DolphinMainWindow::properties()
587 {
588 const KFileItemList list = m_activeView->selectedItems();
589 new KPropertiesDialog(list, this);
590 }
591
592 void DolphinMainWindow::quit()
593 {
594 close();
595 }
596
597 void DolphinMainWindow::slotHandleJobError(KJob* job)
598 {
599 if (job->error() != 0) {
600 m_activeView->statusBar()->setMessage(job->errorString(),
601 DolphinStatusBar::Error);
602 }
603 }
604
605 void DolphinMainWindow::slotDeleteFileFinished(KJob* job)
606 {
607 if (job->error() == 0) {
608 m_activeView->statusBar()->setMessage(i18n("Delete operation completed."),
609 DolphinStatusBar::OperationCompleted);
610
611 // TODO: In opposite to the 'Move to Trash' operation in the class KFileIconView
612 // no rearranging of the item position is done when a file has been deleted.
613 // This is bypassed by reloading the view, but it might be worth to investigate
614 // deeper for the root of this issue.
615 m_activeView->reload();
616 }
617 }
618
619 void DolphinMainWindow::slotUndoAvailable(bool available)
620 {
621 QAction* undoAction = actionCollection()->action(KStdAction::stdName(KStdAction::Undo));
622 if (undoAction != 0) {
623 undoAction->setEnabled(available);
624 }
625 }
626
627 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
628 {
629 QAction* undoAction = actionCollection()->action(KStdAction::stdName(KStdAction::Undo));
630 if (undoAction != 0) {
631 undoAction->setText(text);
632 }
633 }
634
635 void DolphinMainWindow::slotRedoAvailable(bool available)
636 {
637 QAction* redoAction = actionCollection()->action(KStdAction::stdName(KStdAction::Redo));
638 if (redoAction != 0) {
639 redoAction->setEnabled(available);
640 }
641 }
642
643 void DolphinMainWindow::slotRedoTextChanged(const QString& text)
644 {
645 QAction* redoAction = actionCollection()->action(KStdAction::stdName(KStdAction::Redo));
646 if (redoAction != 0) {
647 redoAction->setText(text);
648 }
649 }
650
651 void DolphinMainWindow::cut()
652 {
653 // TODO: this boolean doesn't work between instances of dolphin or with konqueror or with other
654 // apps. The "application/x-kde-cutselection" mimetype should be used instead, see KonqMimeData
655 // in libkonq
656 m_clipboardContainsCutData = true;
657 /* KDE4-TODO: Q3DragObject* data = new KUrlDrag(m_activeView->selectedUrls(),
658 widget());
659 QApplication::clipboard()->setData(data);*/
660 }
661
662 void DolphinMainWindow::copy()
663 {
664 m_clipboardContainsCutData = false;
665 /* KDE4-TODO:
666 Q3DragObject* data = new KUrlDrag(m_activeView->selectedUrls(),
667 widget());
668 QApplication::clipboard()->setData(data);*/
669 }
670
671 void DolphinMainWindow::paste()
672 {
673 /* KDE4-TODO: - see KonqOperations::doPaste
674 QClipboard* clipboard = QApplication::clipboard();
675 QMimeSource* data = clipboard->data();
676 if (!KUrlDrag::canDecode(data)) {
677 return;
678 }
679
680 clearStatusBar();
681
682 KUrl::List sourceUrls;
683 KUrlDrag::decode(data, sourceUrls);
684
685 // per default the pasting is done into the current Url of the view
686 KUrl destUrl(m_activeView->url());
687
688 // check whether the pasting should be done into a selected directory
689 KUrl::List selectedUrls = m_activeView->selectedUrls();
690 if (selectedUrls.count() == 1) {
691 const KFileItem fileItem(S_IFDIR,
692 KFileItem::Unknown,
693 selectedUrls.first(),
694 true);
695 if (fileItem.isDir()) {
696 // only one item is selected which is a directory, hence paste
697 // into this directory
698 destUrl = selectedUrls.first();
699 }
700 }
701
702
703 updateViewProperties(sourceUrls);
704 if (m_clipboardContainsCutData) {
705 moveUrls(sourceUrls, destUrl);
706 m_clipboardContainsCutData = false;
707 clipboard->clear();
708 }
709 else {
710 copyUrls(sourceUrls, destUrl);
711 }*/
712 }
713
714 void DolphinMainWindow::updatePasteAction()
715 {
716 QAction* pasteAction = actionCollection()->action(KStdAction::stdName(KStdAction::Paste));
717 if (pasteAction == 0) {
718 return;
719 }
720
721 QString text(i18n("Paste"));
722 QClipboard* clipboard = QApplication::clipboard();
723 QMimeSource* data = clipboard->data();
724 /* KDE4-TODO:
725 if (KUrlDrag::canDecode(data)) {
726 pasteAction->setEnabled(true);
727
728 KUrl::List urls;
729 KUrlDrag::decode(data, urls);
730 const int count = urls.count();
731 if (count == 1) {
732 pasteAction->setText(i18n("Paste 1 File"));
733 }
734 else {
735 pasteAction->setText(i18n("Paste %1 Files").arg(count));
736 }
737 }
738 else {*/
739 pasteAction->setEnabled(false);
740 pasteAction->setText(i18n("Paste"));
741 //}
742
743 if (pasteAction->isEnabled()) {
744 KUrl::List urls = m_activeView->selectedUrls();
745 const uint count = urls.count();
746 if (count > 1) {
747 // pasting should not be allowed when more than one file
748 // is selected
749 pasteAction->setEnabled(false);
750 }
751 else if (count == 1) {
752 // Only one file is selected. Pasting is only allowed if this
753 // file is a directory.
754 // TODO: this doesn't work with remote protocols; instead we need a
755 // m_activeView->selectedFileItems() to get the real KFileItems
756 const KFileItem fileItem(S_IFDIR,
757 KFileItem::Unknown,
758 urls.first(),
759 true);
760 pasteAction->setEnabled(fileItem.isDir());
761 }
762 }
763 }
764
765 void DolphinMainWindow::selectAll()
766 {
767 clearStatusBar();
768 m_activeView->selectAll();
769 }
770
771 void DolphinMainWindow::invertSelection()
772 {
773 clearStatusBar();
774 m_activeView->invertSelection();
775 }
776 void DolphinMainWindow::setIconsView()
777 {
778 m_activeView->setMode(DolphinView::IconsView);
779 }
780
781 void DolphinMainWindow::setDetailsView()
782 {
783 m_activeView->setMode(DolphinView::DetailsView);
784 }
785
786 void DolphinMainWindow::setPreviewsView()
787 {
788 m_activeView->setMode(DolphinView::PreviewsView);
789 }
790
791 void DolphinMainWindow::sortByName()
792 {
793 m_activeView->setSorting(DolphinView::SortByName);
794 }
795
796 void DolphinMainWindow::sortBySize()
797 {
798 m_activeView->setSorting(DolphinView::SortBySize);
799 }
800
801 void DolphinMainWindow::sortByDate()
802 {
803 m_activeView->setSorting(DolphinView::SortByDate);
804 }
805
806 void DolphinMainWindow::toggleSortOrder()
807 {
808 const Qt::SortOrder order = (m_activeView->sortOrder() == Qt::Ascending) ?
809 Qt::Descending :
810 Qt::Ascending;
811 m_activeView->setSortOrder(order);
812 }
813
814 void DolphinMainWindow::toggleSplitView()
815 {
816 if (m_view[SecondaryIdx] == 0) {
817 const int newWidth = (m_view[PrimaryIdx]->width() - m_splitter->handleWidth()) / 2;
818 // create a secondary view
819 m_view[SecondaryIdx] = new DolphinView(this,
820 0,
821 m_view[PrimaryIdx]->url(),
822 m_view[PrimaryIdx]->mode(),
823 m_view[PrimaryIdx]->isShowHiddenFilesEnabled());
824 connectViewSignals(SecondaryIdx);
825 m_splitter->addWidget(m_view[SecondaryIdx]);
826 m_splitter->setSizes(QList<int>() << newWidth << newWidth);
827 m_view[SecondaryIdx]->show();
828 }
829 else {
830 // remove secondary view
831 if (m_activeView == m_view[PrimaryIdx]) {
832 m_view[SecondaryIdx]->close();
833 m_view[SecondaryIdx]->deleteLater();
834 m_view[SecondaryIdx] = 0;
835 setActiveView(m_view[PrimaryIdx]);
836 }
837 else {
838 // The secondary view is active, hence from the users point of view
839 // the content of the secondary view should be moved to the primary view.
840 // From an implementation point of view it is more efficient to close
841 // the primary view and exchange the internal pointers afterwards.
842 m_view[PrimaryIdx]->close();
843 delete m_view[PrimaryIdx];
844 m_view[PrimaryIdx] = m_view[SecondaryIdx];
845 m_view[SecondaryIdx] = 0;
846 setActiveView(m_view[PrimaryIdx]);
847 }
848 }
849 }
850
851 void DolphinMainWindow::reloadView()
852 {
853 clearStatusBar();
854 m_activeView->reload();
855 }
856
857 void DolphinMainWindow::stopLoading()
858 {
859 }
860
861 void DolphinMainWindow::showHiddenFiles()
862 {
863 clearStatusBar();
864
865 const KToggleAction* showHiddenFilesAction =
866 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
867 const bool show = showHiddenFilesAction->isChecked();
868 m_activeView->setShowHiddenFilesEnabled(show);
869 }
870
871 void DolphinMainWindow::showFilterBar()
872 {
873 const KToggleAction* showFilterBarAction =
874 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
875 const bool show = showFilterBarAction->isChecked();
876 m_activeView->slotShowFilterBar(show);
877 }
878
879 void DolphinMainWindow::zoomIn()
880 {
881 m_activeView->zoomIn();
882 updateViewActions();
883 }
884
885 void DolphinMainWindow::zoomOut()
886 {
887 m_activeView->zoomOut();
888 updateViewActions();
889 }
890
891 void DolphinMainWindow::toggleEditLocation()
892 {
893 clearStatusBar();
894
895 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
896
897 bool editOrBrowse = action->isChecked();
898 // action->setChecked(action->setChecked);
899 m_activeView->setUrlEditable(editOrBrowse);
900 }
901
902 void DolphinMainWindow::editLocation()
903 {
904 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
905 action->setChecked(true);
906 m_activeView->setUrlEditable(true);
907 }
908
909 void DolphinMainWindow::adjustViewProperties()
910 {
911 clearStatusBar();
912 ViewPropertiesDialog dlg(m_activeView);
913 dlg.exec();
914 }
915
916 void DolphinMainWindow::goBack()
917 {
918 clearStatusBar();
919 m_activeView->goBack();
920 }
921
922 void DolphinMainWindow::goForward()
923 {
924 clearStatusBar();
925 m_activeView->goForward();
926 }
927
928 void DolphinMainWindow::goUp()
929 {
930 clearStatusBar();
931 m_activeView->goUp();
932 }
933
934 void DolphinMainWindow::goHome()
935 {
936 clearStatusBar();
937 m_activeView->goHome();
938 }
939
940 void DolphinMainWindow::openTerminal()
941 {
942 QString command("konsole --workdir \"");
943 command.append(m_activeView->url().path());
944 command.append('\"');
945
946 KRun::runCommand(command, "Konsole", "konsole");
947 }
948
949 void DolphinMainWindow::findFile()
950 {
951 KRun::run("kfind", m_activeView->url());
952 }
953
954 void DolphinMainWindow::compareFiles()
955 {
956 // The method is only invoked if exactly 2 files have
957 // been selected. The selected files may be:
958 // - both in the primary view
959 // - both in the secondary view
960 // - one in the primary view and the other in the secondary
961 // view
962 assert(m_view[PrimaryIdx] != 0);
963
964 KUrl urlA;
965 KUrl urlB;
966 KUrl::List urls = m_view[PrimaryIdx]->selectedUrls();
967
968 switch (urls.count()) {
969 case 0: {
970 assert(m_view[SecondaryIdx] != 0);
971 urls = m_view[SecondaryIdx]->selectedUrls();
972 assert(urls.count() == 2);
973 urlA = urls[0];
974 urlB = urls[1];
975 break;
976 }
977
978 case 1: {
979 urlA = urls[0];
980 assert(m_view[SecondaryIdx] != 0);
981 urls = m_view[SecondaryIdx]->selectedUrls();
982 assert(urls.count() == 1);
983 urlB = urls[0];
984 break;
985 }
986
987 case 2: {
988 urlA = urls[0];
989 urlB = urls[1];
990 break;
991 }
992
993 default: {
994 // may not happen: compareFiles may only get invoked if 2
995 // files are selected
996 assert(false);
997 }
998 }
999
1000 QString command("kompare -c \"");
1001 command.append(urlA.pathOrUrl());
1002 command.append("\" \"");
1003 command.append(urlB.pathOrUrl());
1004 command.append('\"');
1005 KRun::runCommand(command, "Kompare", "kompare");
1006
1007 }
1008
1009 void DolphinMainWindow::editSettings()
1010 {
1011 // TODO: make a static method for opening the settings dialog
1012 DolphinSettingsDialog dlg(this);
1013 dlg.exec();
1014 }
1015
1016 void DolphinMainWindow::addUndoOperation(KJob* job)
1017 {
1018 if (job->error() != 0) {
1019 slotHandleJobError(job);
1020 }
1021 else {
1022 const int id = job->progressId();
1023
1024 // set iterator to the executed command with the current id...
1025 Q3ValueList<UndoInfo>::Iterator it = m_pendingUndoJobs.begin();
1026 const Q3ValueList<UndoInfo>::Iterator end = m_pendingUndoJobs.end();
1027 bool found = false;
1028 while (!found && (it != end)) {
1029 if ((*it).id == id) {
1030 found = true;
1031 }
1032 else {
1033 ++it;
1034 }
1035 }
1036
1037 if (found) {
1038 DolphinCommand command = (*it).command;
1039 if (command.type() == DolphinCommand::Trash) {
1040 // To be able to perform an undo for the 'Move to Trash' operation
1041 // all source Urls must be updated with the trash Url. E. g. when moving
1042 // a file "test.txt" and a second file "test.txt" to the trash,
1043 // then the filenames in the trash are "0-test.txt" and "1-test.txt".
1044 QMap<QString, QString> metaData;
1045 KIO::Job *kiojob = qobject_cast<KIO::Job*>( job );
1046 if ( kiojob )
1047 {
1048 metaData = kiojob->metaData();
1049 }
1050 KUrl::List newSourceUrls;
1051
1052 KUrl::List sourceUrls = command.source();
1053 KUrl::List::Iterator sourceIt = sourceUrls.begin();
1054 const KUrl::List::Iterator sourceEnd = sourceUrls.end();
1055
1056 while (sourceIt != sourceEnd) {
1057 QMap<QString, QString>::ConstIterator metaIt = metaData.find("trashUrl-" + (*sourceIt).path());
1058 if (metaIt != metaData.end()) {
1059 newSourceUrls.append(KUrl(metaIt.data()));
1060 }
1061 ++sourceIt;
1062 }
1063 command.setSource(newSourceUrls);
1064 }
1065
1066 UndoManager::instance().addCommand(command);
1067 m_pendingUndoJobs.erase(it);
1068
1069 DolphinStatusBar* statusBar = m_activeView->statusBar();
1070 switch (command.type()) {
1071 case DolphinCommand::Copy:
1072 statusBar->setMessage(i18n("Copy operation completed."),
1073 DolphinStatusBar::OperationCompleted);
1074 break;
1075 case DolphinCommand::Move:
1076 statusBar->setMessage(i18n("Move operation completed."),
1077 DolphinStatusBar::OperationCompleted);
1078 break;
1079 case DolphinCommand::Trash:
1080 statusBar->setMessage(i18n("Move to trash operation completed."),
1081 DolphinStatusBar::OperationCompleted);
1082 break;
1083 default:
1084 break;
1085 }
1086 }
1087 }
1088 }
1089
1090 void DolphinMainWindow::init()
1091 {
1092 // Check whether Dolphin runs the first time. If yes then
1093 // a proper default window size is given at the end of DolphinMainWindow::init().
1094 GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
1095 const bool firstRun = generalSettings->firstRun();
1096
1097 setAcceptDrops(true);
1098
1099 m_splitter = new QSplitter(this);
1100
1101 DolphinSettings& settings = DolphinSettings::instance();
1102
1103 KBookmarkManager* manager = settings.bookmarkManager();
1104 assert(manager != 0);
1105 KBookmarkGroup root = manager->root();
1106 if (root.first().isNull()) {
1107 root.addBookmark(manager, i18n("Home"), settings.generalSettings()->homeUrl(), "folder_home");
1108 root.addBookmark(manager, i18n("Storage Media"), KUrl("media:/"), "blockdevice");
1109 root.addBookmark(manager, i18n("Network"), KUrl("remote:/"), "network_local");
1110 root.addBookmark(manager, i18n("Root"), KUrl("/"), "folder_red");
1111 root.addBookmark(manager, i18n("Trash"), KUrl("trash:/"), "trashcan_full");
1112 }
1113
1114 setupActions();
1115
1116 const KUrl& homeUrl = root.first().url();
1117 setCaption(homeUrl.fileName());
1118 ViewProperties props(homeUrl);
1119 m_view[PrimaryIdx] = new DolphinView(this,
1120 m_splitter,
1121 homeUrl,
1122 props.viewMode(),
1123 props.isShowHiddenFilesEnabled());
1124 connectViewSignals(PrimaryIdx);
1125 m_view[PrimaryIdx]->show();
1126
1127 m_activeView = m_view[PrimaryIdx];
1128
1129 setCentralWidget(m_splitter);
1130 setupDockWidgets();
1131
1132 setupGUI(Keys|Save|Create|ToolBar);
1133 createGUI();
1134
1135 stateChanged("new_file");
1136 setAutoSaveSettings();
1137
1138 QClipboard* clipboard = QApplication::clipboard();
1139 connect(clipboard, SIGNAL(dataChanged()),
1140 this, SLOT(updatePasteAction()));
1141 updatePasteAction();
1142 updateGoActions();
1143
1144 setupCreateNewMenuActions();
1145
1146 loadSettings();
1147
1148 if (firstRun) {
1149 // assure a proper default size if Dolphin runs the first time
1150 resize(640, 480);
1151 }
1152 }
1153
1154 void DolphinMainWindow::loadSettings()
1155 {
1156 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
1157
1158 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
1159 if (settings->splitView()) {
1160 splitAction->setChecked(true);
1161 toggleSplitView();
1162 }
1163
1164 updateViewActions();
1165 }
1166
1167 void DolphinMainWindow::setupActions()
1168 {
1169 // setup 'File' menu
1170 KAction *action = new KAction(KIcon("window_new"), i18n( "New &Window" ), actionCollection(), "new_window" );
1171 connect(action, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1172
1173 KAction* createFolder = new KAction(i18n("Folder..."), actionCollection(), "create_folder");
1174 createFolder->setIcon(KIcon("folder"));
1175 createFolder->setShortcut(Qt::Key_N);
1176 connect(createFolder, SIGNAL(triggered()), this, SLOT(createFolder()));
1177
1178 KAction* rename = new KAction(i18n("Rename"), actionCollection(), "rename");
1179 rename->setShortcut(Qt::Key_F2);
1180 connect(rename, SIGNAL(triggered()), this, SLOT(rename()));
1181
1182 KAction* moveToTrash = new KAction(i18n("Move to Trash"), actionCollection(), "move_to_trash");
1183 moveToTrash->setIcon(KIcon("edittrash"));
1184 moveToTrash->setShortcut(QKeySequence::Delete);
1185 connect(moveToTrash, SIGNAL(triggered()), this, SLOT(moveToTrash()));
1186
1187 KAction* deleteAction = new KAction(i18n("Delete"), actionCollection(), "delete");
1188 deleteAction->setShortcut(Qt::ALT | Qt::Key_Delete);
1189 deleteAction->setIcon(KIcon("editdelete"));
1190 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItems()));
1191
1192 KAction* properties = new KAction(i18n("Propert&ies"), actionCollection(), "properties");
1193 properties->setShortcut(Qt::Key_Alt | Qt::Key_Return);
1194 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
1195
1196 KStdAction::quit(this, SLOT(quit()), actionCollection());
1197
1198 // setup 'Edit' menu
1199 UndoManager& undoManager = UndoManager::instance();
1200 KStdAction::undo(this,
1201 SLOT(undo()),
1202 actionCollection());
1203 connect(&undoManager, SIGNAL(undoAvailable(bool)),
1204 this, SLOT(slotUndoAvailable(bool)));
1205 connect(&undoManager, SIGNAL(undoTextChanged(const QString&)),
1206 this, SLOT(slotUndoTextChanged(const QString&)));
1207
1208 KStdAction::redo(this,
1209 SLOT(redo()),
1210 actionCollection());
1211 connect(&undoManager, SIGNAL(redoAvailable(bool)),
1212 this, SLOT(slotRedoAvailable(bool)));
1213 connect(&undoManager, SIGNAL(redoTextChanged(const QString&)),
1214 this, SLOT(slotRedoTextChanged(const QString&)));
1215
1216 KStdAction::cut(this, SLOT(cut()), actionCollection());
1217 KStdAction::copy(this, SLOT(copy()), actionCollection());
1218 KStdAction::paste(this, SLOT(paste()), actionCollection());
1219
1220 KAction* selectAll = new KAction(i18n("Select All"), actionCollection(), "select_all");
1221 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1222 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1223
1224 KAction* invertSelection = new KAction(i18n("Invert Selection"), actionCollection(), "invert_selection");
1225 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1226 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1227
1228 // setup 'View' menu
1229 KStdAction::zoomIn(this,
1230 SLOT(zoomIn()),
1231 actionCollection());
1232
1233 KStdAction::zoomOut(this,
1234 SLOT(zoomOut()),
1235 actionCollection());
1236
1237 KToggleAction* iconsView = new KToggleAction(i18n("Icons"), actionCollection(), "icons");
1238 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
1239 iconsView->setIcon(KIcon("view_icon"));
1240 connect(iconsView, SIGNAL(triggered()), this, SLOT(setIconsView()));
1241
1242 KToggleAction* detailsView = new KToggleAction(i18n("Details"), actionCollection(), "details");
1243 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1244 detailsView->setIcon(KIcon("view_text"));
1245 connect(detailsView, SIGNAL(triggered()), this, SLOT(setDetailsView()));
1246
1247 KToggleAction* previewsView = new KToggleAction(i18n("Previews"), actionCollection(), "previews");
1248 previewsView->setShortcut(Qt::CTRL | Qt::Key_3);
1249 previewsView->setIcon(KIcon("gvdirpart"));
1250 connect(previewsView, SIGNAL(triggered()), this, SLOT(setPreviewsView()));
1251
1252 QActionGroup* viewModeGroup = new QActionGroup(this);
1253 viewModeGroup->addAction(iconsView);
1254 viewModeGroup->addAction(detailsView);
1255 viewModeGroup->addAction(previewsView);
1256
1257 KToggleAction* sortByName = new KToggleAction(i18n("By Name"), actionCollection(), "by_name");
1258 connect(sortByName, SIGNAL(triggered()), this, SLOT(sortByName()));
1259
1260 KToggleAction* sortBySize = new KToggleAction(i18n("By Size"), actionCollection(), "by_size");
1261 connect(sortBySize, SIGNAL(triggered()), this, SLOT(sortBySize()));
1262
1263 KToggleAction* sortByDate = new KToggleAction(i18n("By Date"), actionCollection(), "by_date");
1264 connect(sortByDate, SIGNAL(triggered()), this, SLOT(sortByDate()));
1265
1266 QActionGroup* sortGroup = new QActionGroup(this);
1267 sortGroup->addAction(sortByName);
1268 sortGroup->addAction(sortBySize);
1269 sortGroup->addAction(sortByDate);
1270
1271 KToggleAction* sortDescending = new KToggleAction(i18n("Descending"), actionCollection(), "descending");
1272 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
1273
1274 KToggleAction* showHiddenFiles = new KToggleAction(i18n("Show Hidden Files"), actionCollection(), "show_hidden_files");
1275 //showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_ KDE4-TODO: what Qt-Key represents '.'?
1276 connect(showHiddenFiles, SIGNAL(triggered()), this, SLOT(showHiddenFiles()));
1277
1278 KToggleAction* split = new KToggleAction(i18n("Split View"), actionCollection(), "split_view");
1279 split->setShortcut(Qt::Key_F10);
1280 split->setIcon(KIcon("view_left_right"));
1281 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1282
1283 KAction* reload = new KAction(i18n("Reload"), "F5", actionCollection(), "reload");
1284 reload->setShortcut(Qt::Key_F5);
1285 reload->setIcon(KIcon("reload"));
1286 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1287
1288 KAction* stop = new KAction(i18n("Stop"), actionCollection(), "stop");
1289 stop->setIcon(KIcon("stop"));
1290 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1291
1292 KToggleAction* showFullLocation = new KToggleAction(i18n("Show Full Location"), actionCollection(), "editable_location");
1293 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1294 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1295
1296 KToggleAction* editLocation = new KToggleAction(i18n("Edit Location"), actionCollection(), "edit_location");
1297 editLocation->setShortcut(Qt::Key_F6);
1298 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
1299
1300 KAction* adjustViewProps = new KAction(i18n("Adjust View Properties..."), actionCollection(), "view_properties");
1301 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
1302
1303 // setup 'Go' menu
1304 KStdAction::back(this, SLOT(goBack()), actionCollection());
1305 KStdAction::forward(this, SLOT(goForward()), actionCollection());
1306 KStdAction::up(this, SLOT(goUp()), actionCollection());
1307 KStdAction::home(this, SLOT(goHome()), actionCollection());
1308
1309 // setup 'Tools' menu
1310 KAction* openTerminal = new KAction(i18n("Open Terminal"), actionCollection(), "open_terminal");
1311 openTerminal->setShortcut(Qt::Key_F4);
1312 openTerminal->setIcon(KIcon("konsole"));
1313 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1314
1315 KAction* findFile = new KAction(i18n("Find File..."), actionCollection(), "find_file");
1316 findFile->setShortcut(Qt::Key_F);
1317 findFile->setIcon(KIcon("filefind"));
1318 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
1319
1320 KToggleAction* showFilterBar = new KToggleAction(i18n("Show Filter Bar"), actionCollection(), "show_filter_bar");
1321 showFilterBar->setShortcut(Qt::Key_Slash);
1322 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1323
1324 KAction* compareFiles = new KAction(i18n("Compare Files"), actionCollection(), "compare_files");
1325 compareFiles->setIcon(KIcon("kompare"));
1326 compareFiles->setEnabled(false);
1327 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1328
1329 // setup 'Settings' menu
1330 KStdAction::preferences(this, SLOT(editSettings()), actionCollection());
1331 }
1332
1333 void DolphinMainWindow::setupDockWidgets()
1334 {
1335 QDockWidget *shortcutsDock = new QDockWidget(i18n("Shortcuts"));
1336
1337 shortcutsDock->setObjectName("shortcutsDock");
1338 shortcutsDock->setWidget(new BookmarksSidebarPage(this));
1339
1340 shortcutsDock->toggleViewAction()->setObjectName("show_shortcuts_pane");
1341 shortcutsDock->toggleViewAction()->setText(i18n("Show Shortcuts Panel"));
1342 actionCollection()->insert(shortcutsDock->toggleViewAction());
1343
1344 addDockWidget(Qt::LeftDockWidgetArea, shortcutsDock);
1345
1346 QDockWidget *infoDock = new QDockWidget(i18n("Information"));
1347
1348 infoDock->setObjectName("infoDock");
1349 infoDock->setWidget(new InfoSidebarPage(this));
1350
1351 infoDock->toggleViewAction()->setObjectName("show_info_pane");
1352 infoDock->toggleViewAction()->setText(i18n("Show Information Panel"));
1353 actionCollection()->insert(infoDock->toggleViewAction());
1354
1355 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1356 }
1357
1358 void DolphinMainWindow::setupCreateNewMenuActions()
1359 {
1360 // Parts of the following code have been taken
1361 // from the class KNewMenu located in
1362 // libqonq/knewmenu.h of Konqueror.
1363 // Copyright (C) 1998, 1999 David Faure <faure@kde.org>
1364 // 2003 Sven Leiber <s.leiber@web.de>
1365
1366 QStringList files = actionCollection()->instance()->dirs()->findAllResources("templates");
1367 for (QStringList::Iterator it = files.begin() ; it != files.end(); ++it) {
1368 if ((*it)[0] != '.' ) {
1369 KSimpleConfig config(*it, true);
1370 config.setDesktopGroup();
1371
1372 // tricky solution to ensure that TextFile is at the beginning
1373 // because this filetype is the most used (according kde-core discussion)
1374 const QString name(config.readEntry("Name"));
1375 QString key(name);
1376
1377 const QString path(config.readPathEntry("Url"));
1378 if (!path.endsWith("emptydir")) {
1379 if (path.endsWith("TextFile.txt")) {
1380 key = "1" + key;
1381 }
1382 else if (!KDesktopFile::isDesktopFile(path)) {
1383 key = "2" + key;
1384 }
1385 else if (path.endsWith("Url.desktop")){
1386 key = "3" + key;
1387 }
1388 else if (path.endsWith("Program.desktop")){
1389 key = "4" + key;
1390 }
1391 else {
1392 key = "5";
1393 }
1394
1395 const QString icon(config.readEntry("Icon"));
1396 const QString comment(config.readEntry("Comment"));
1397 const QString type(config.readEntry("Type"));
1398
1399 const QString filePath(*it);
1400
1401
1402 if (type == "Link") {
1403 CreateFileEntry entry;
1404 entry.name = name;
1405 entry.icon = icon;
1406 entry.comment = comment;
1407 entry.templatePath = filePath;
1408 m_createFileTemplates.insert(key, entry);
1409 }
1410 }
1411 }
1412 }
1413 m_createFileTemplates.sort();
1414
1415 unplugActionList("create_actions");
1416 KSortableList<CreateFileEntry, QString>::ConstIterator it = m_createFileTemplates.begin();
1417 KSortableList<CreateFileEntry, QString>::ConstIterator end = m_createFileTemplates.end();
1418 /* KDE4-TODO: don't port this code; use KNewMenu instead
1419 while (it != end) {
1420 CreateFileEntry entry = (*it).value();
1421 KAction* action = new KAction(entry.name);
1422 action->setIcon(entry.icon);
1423 action->setName((*it).index());
1424 connect(action, SIGNAL(activated()),
1425 this, SLOT(createFile()));
1426
1427 const QChar section = ((*it).index()[0]);
1428 switch (section) {
1429 case '1':
1430 case '2': {
1431 m_fileGroupActions.append(action);
1432 break;
1433 }
1434
1435 case '3':
1436 case '4': {
1437 // TODO: not used yet. See documentation of DolphinMainWindow::linkGroupActions()
1438 // and DolphinMainWindow::linkToDeviceActions() in the header file for details.
1439 //m_linkGroupActions.append(action);
1440 break;
1441 }
1442
1443 case '5': {
1444 // TODO: not used yet. See documentation of DolphinMainWindow::linkGroupActions()
1445 // and DolphinMainWindow::linkToDeviceActions() in the header file for details.
1446 //m_linkToDeviceActions.append(action);
1447 break;
1448 }
1449 default:
1450 break;
1451 }
1452 ++it;
1453 }
1454
1455 plugActionList("create_file_group", m_fileGroupActions);
1456 //plugActionList("create_link_group", m_linkGroupActions);
1457 //plugActionList("link_to_device", m_linkToDeviceActions);*/
1458 }
1459
1460 void DolphinMainWindow::updateHistory()
1461 {
1462 int index = 0;
1463 const Q3ValueList<UrlNavigator::HistoryElem> list = m_activeView->urlHistory(index);
1464
1465 QAction* backAction = actionCollection()->action("go_back");
1466 if (backAction != 0) {
1467 backAction->setEnabled(index < static_cast<int>(list.count()) - 1);
1468 }
1469
1470 QAction* forwardAction = actionCollection()->action("go_forward");
1471 if (forwardAction != 0) {
1472 forwardAction->setEnabled(index > 0);
1473 }
1474 }
1475
1476 void DolphinMainWindow::updateEditActions()
1477 {
1478 const KFileItemList list = m_activeView->selectedItems();
1479 if (list.isEmpty()) {
1480 stateChanged("has_no_selection");
1481 }
1482 else {
1483 stateChanged("has_selection");
1484
1485 QAction* renameAction = actionCollection()->action("rename");
1486 if (renameAction != 0) {
1487 renameAction->setEnabled(list.count() >= 1);
1488 }
1489
1490 bool enableMoveToTrash = true;
1491
1492 KFileItemList::const_iterator it = list.begin();
1493 const KFileItemList::const_iterator end = list.end();
1494 while (it != end) {
1495 KFileItem* item = *it;
1496 const KUrl& url = item->url();
1497 // only enable the 'Move to Trash' action for local files
1498 if (!url.isLocalFile()) {
1499 enableMoveToTrash = false;
1500 }
1501 ++it;
1502 }
1503
1504 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
1505 moveToTrashAction->setEnabled(enableMoveToTrash);
1506 }
1507 updatePasteAction();
1508 }
1509
1510 void DolphinMainWindow::updateViewActions()
1511 {
1512 QAction* zoomInAction = actionCollection()->action(KStdAction::stdName(KStdAction::ZoomIn));
1513 if (zoomInAction != 0) {
1514 zoomInAction->setEnabled(m_activeView->isZoomInPossible());
1515 }
1516
1517 QAction* zoomOutAction = actionCollection()->action(KStdAction::stdName(KStdAction::ZoomOut));
1518 if (zoomOutAction != 0) {
1519 zoomOutAction->setEnabled(m_activeView->isZoomOutPossible());
1520 }
1521
1522 QAction* action = 0;
1523 switch (m_activeView->mode()) {
1524 case DolphinView::IconsView:
1525 action = actionCollection()->action("icons");
1526 break;
1527 case DolphinView::DetailsView:
1528 action = actionCollection()->action("details");
1529 break;
1530 case DolphinView::PreviewsView:
1531 action = actionCollection()->action("previews");
1532 break;
1533 default:
1534 break;
1535 }
1536
1537 if (action != 0) {
1538 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
1539 toggleAction->setChecked(true);
1540 }
1541
1542 slotSortingChanged(m_activeView->sorting());
1543 slotSortOrderChanged(m_activeView->sortOrder());
1544
1545 KToggleAction* showFilterBarAction =
1546 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
1547 showFilterBarAction->setChecked(m_activeView->isFilterBarVisible());
1548
1549 KToggleAction* showHiddenFilesAction =
1550 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
1551 showHiddenFilesAction->setChecked(m_activeView->isShowHiddenFilesEnabled());
1552
1553 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
1554 splitAction->setChecked(m_view[SecondaryIdx] != 0);
1555 }
1556
1557 void DolphinMainWindow::updateGoActions()
1558 {
1559 QAction* goUpAction = actionCollection()->action(KStdAction::stdName(KStdAction::Up));
1560 const KUrl& currentUrl = m_activeView->url();
1561 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1562 }
1563
1564 void DolphinMainWindow::updateViewProperties(const KUrl::List& urls)
1565 {
1566 if (urls.isEmpty()) {
1567 return;
1568 }
1569
1570 // Updating the view properties might take up to several seconds
1571 // when dragging several thousand Urls. Writing a KIO slave for this
1572 // use case is not worth the effort, but at least the main widget
1573 // must be disabled and a progress should be shown.
1574 ProgressIndicator progressIndicator(this,
1575 i18n("Updating view properties..."),
1576 QString::null,
1577 urls.count());
1578
1579 KUrl::List::ConstIterator end = urls.end();
1580 for(KUrl::List::ConstIterator it = urls.begin(); it != end; ++it) {
1581 progressIndicator.execOperation();
1582
1583 ViewProperties props(*it);
1584 props.save();
1585 }
1586 }
1587
1588 void DolphinMainWindow::copyUrls(const KUrl::List& source, const KUrl& dest)
1589 {
1590 KIO::Job* job = KIO::copy(source, dest);
1591 addPendingUndoJob(job, DolphinCommand::Copy, source, dest);
1592 }
1593
1594 void DolphinMainWindow::moveUrls(const KUrl::List& source, const KUrl& dest)
1595 {
1596 KIO::Job* job = KIO::move(source, dest);
1597 addPendingUndoJob(job, DolphinCommand::Move, source, dest);
1598 }
1599
1600 void DolphinMainWindow::addPendingUndoJob(KIO::Job* job,
1601 DolphinCommand::Type commandType,
1602 const KUrl::List& source,
1603 const KUrl& dest)
1604 {
1605 connect(job, SIGNAL(result(KJob*)),
1606 this, SLOT(addUndoOperation(KJob*)));
1607
1608 UndoInfo undoInfo;
1609 undoInfo.id = job->progressId();
1610 undoInfo.command = DolphinCommand(commandType, source, dest);
1611 m_pendingUndoJobs.append(undoInfo);
1612 }
1613
1614 void DolphinMainWindow::clearStatusBar()
1615 {
1616 m_activeView->statusBar()->clear();
1617 }
1618
1619 void DolphinMainWindow::connectViewSignals(int viewIndex)
1620 {
1621 DolphinView* view = m_view[viewIndex];
1622 connect(view, SIGNAL(modeChanged()),
1623 this, SLOT(slotViewModeChanged()));
1624 connect(view, SIGNAL(showHiddenFilesChanged()),
1625 this, SLOT(slotShowHiddenFilesChanged()));
1626 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
1627 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
1628 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
1629 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
1630 connect(view, SIGNAL(selectionChanged()),
1631 this, SLOT(slotSelectionChanged()));
1632 connect(view, SIGNAL(showFilterBarChanged(bool)),
1633 this, SLOT(updateFilterBarAction(bool)));
1634
1635 const UrlNavigator* navigator = view->urlNavigator();
1636 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1637 this, SLOT(slotUrlChanged(const KUrl&)));
1638 connect(navigator, SIGNAL(historyChanged()),
1639 this, SLOT(slotHistoryChanged()));
1640
1641 }
1642
1643 #include "dolphinmainwindow.moc"