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