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