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