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