]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Use KonqMimeData for the cut- and copy-operation instead of using bool property insid...
[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 #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"), SmallIcon("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(KStdAction::stdName(KStdAction::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(KStdAction::stdName(KStdAction::Undo));
622 if (undoAction != 0) {
623 undoAction->setText(text);
624 }
625 }
626
627 void DolphinMainWindow::slotRedoAvailable(bool available)
628 {
629 QAction* redoAction = actionCollection()->action(KStdAction::stdName(KStdAction::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(KStdAction::stdName(KStdAction::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(KStdAction::stdName(KStdAction::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 }
841
842 void DolphinMainWindow::toggleShowHiddenFiles()
843 {
844 clearStatusBar();
845
846 const KToggleAction* showHiddenFilesAction =
847 static_cast<KToggleAction*>(actionCollection()->action("show_hidden_files"));
848 const bool show = showHiddenFilesAction->isChecked();
849 m_activeView->setShowHiddenFiles(show);
850 }
851
852 void DolphinMainWindow::showFilterBar()
853 {
854 const KToggleAction* showFilterBarAction =
855 static_cast<KToggleAction*>(actionCollection()->action("show_filter_bar"));
856 const bool show = showFilterBarAction->isChecked();
857 m_activeView->slotShowFilterBar(show);
858 }
859
860 void DolphinMainWindow::zoomIn()
861 {
862 m_activeView->zoomIn();
863 updateViewActions();
864 }
865
866 void DolphinMainWindow::zoomOut()
867 {
868 m_activeView->zoomOut();
869 updateViewActions();
870 }
871
872 void DolphinMainWindow::toggleEditLocation()
873 {
874 clearStatusBar();
875
876 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
877
878 bool editOrBrowse = action->isChecked();
879 // action->setChecked(action->setChecked);
880 m_activeView->setUrlEditable(editOrBrowse);
881 }
882
883 void DolphinMainWindow::editLocation()
884 {
885 KToggleAction* action = static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
886 action->setChecked(true);
887 m_activeView->setUrlEditable(true);
888 }
889
890 void DolphinMainWindow::adjustViewProperties()
891 {
892 clearStatusBar();
893 ViewPropertiesDialog dlg(m_activeView);
894 dlg.exec();
895 }
896
897 void DolphinMainWindow::goBack()
898 {
899 clearStatusBar();
900 m_activeView->goBack();
901 }
902
903 void DolphinMainWindow::goForward()
904 {
905 clearStatusBar();
906 m_activeView->goForward();
907 }
908
909 void DolphinMainWindow::goUp()
910 {
911 clearStatusBar();
912 m_activeView->goUp();
913 }
914
915 void DolphinMainWindow::goHome()
916 {
917 clearStatusBar();
918 m_activeView->goHome();
919 }
920
921 void DolphinMainWindow::openTerminal()
922 {
923 QString command("konsole --workdir \"");
924 command.append(m_activeView->url().path());
925 command.append('\"');
926
927 KRun::runCommand(command, "Konsole", "konsole");
928 }
929
930 void DolphinMainWindow::findFile()
931 {
932 KRun::run("kfind", m_activeView->url());
933 }
934
935 void DolphinMainWindow::compareFiles()
936 {
937 // The method is only invoked if exactly 2 files have
938 // been selected. The selected files may be:
939 // - both in the primary view
940 // - both in the secondary view
941 // - one in the primary view and the other in the secondary
942 // view
943 assert(m_view[PrimaryIdx] != 0);
944
945 KUrl urlA;
946 KUrl urlB;
947 KUrl::List urls = m_view[PrimaryIdx]->selectedUrls();
948
949 switch (urls.count()) {
950 case 0: {
951 assert(m_view[SecondaryIdx] != 0);
952 urls = m_view[SecondaryIdx]->selectedUrls();
953 assert(urls.count() == 2);
954 urlA = urls[0];
955 urlB = urls[1];
956 break;
957 }
958
959 case 1: {
960 urlA = urls[0];
961 assert(m_view[SecondaryIdx] != 0);
962 urls = m_view[SecondaryIdx]->selectedUrls();
963 assert(urls.count() == 1);
964 urlB = urls[0];
965 break;
966 }
967
968 case 2: {
969 urlA = urls[0];
970 urlB = urls[1];
971 break;
972 }
973
974 default: {
975 // may not happen: compareFiles may only get invoked if 2
976 // files are selected
977 assert(false);
978 }
979 }
980
981 QString command("kompare -c \"");
982 command.append(urlA.pathOrUrl());
983 command.append("\" \"");
984 command.append(urlB.pathOrUrl());
985 command.append('\"');
986 KRun::runCommand(command, "Kompare", "kompare");
987
988 }
989
990 void DolphinMainWindow::editSettings()
991 {
992 // TODO: make a static method for opening the settings dialog
993 DolphinSettingsDialog dlg(this);
994 dlg.exec();
995 }
996
997 void DolphinMainWindow::addUndoOperation(KJob* job)
998 {
999 if (job->error() != 0) {
1000 slotHandleJobError(job);
1001 }
1002 else {
1003 const int id = job->progressId();
1004
1005 // set iterator to the executed command with the current id...
1006 Q3ValueList<UndoInfo>::Iterator it = m_pendingUndoJobs.begin();
1007 const Q3ValueList<UndoInfo>::Iterator end = m_pendingUndoJobs.end();
1008 bool found = false;
1009 while (!found && (it != end)) {
1010 if ((*it).id == id) {
1011 found = true;
1012 }
1013 else {
1014 ++it;
1015 }
1016 }
1017
1018 if (found) {
1019 DolphinCommand command = (*it).command;
1020 if (command.type() == DolphinCommand::Trash) {
1021 // To be able to perform an undo for the 'Move to Trash' operation
1022 // all source Urls must be updated with the trash Url. E. g. when moving
1023 // a file "test.txt" and a second file "test.txt" to the trash,
1024 // then the filenames in the trash are "0-test.txt" and "1-test.txt".
1025 QMap<QString, QString> metaData;
1026 KIO::Job *kiojob = qobject_cast<KIO::Job*>( job );
1027 if ( kiojob )
1028 {
1029 metaData = kiojob->metaData();
1030 }
1031 KUrl::List newSourceUrls;
1032
1033 KUrl::List sourceUrls = command.source();
1034 KUrl::List::Iterator sourceIt = sourceUrls.begin();
1035 const KUrl::List::Iterator sourceEnd = sourceUrls.end();
1036
1037 while (sourceIt != sourceEnd) {
1038 QMap<QString, QString>::ConstIterator metaIt = metaData.find("trashUrl-" + (*sourceIt).path());
1039 if (metaIt != metaData.end()) {
1040 newSourceUrls.append(KUrl(metaIt.value()));
1041 }
1042 ++sourceIt;
1043 }
1044 command.setSource(newSourceUrls);
1045 }
1046
1047 UndoManager::instance().addCommand(command);
1048 m_pendingUndoJobs.erase(it);
1049
1050 DolphinStatusBar* statusBar = m_activeView->statusBar();
1051 switch (command.type()) {
1052 case DolphinCommand::Copy:
1053 statusBar->setMessage(i18n("Copy operation completed."),
1054 DolphinStatusBar::OperationCompleted);
1055 break;
1056 case DolphinCommand::Move:
1057 statusBar->setMessage(i18n("Move operation completed."),
1058 DolphinStatusBar::OperationCompleted);
1059 break;
1060 case DolphinCommand::Trash:
1061 statusBar->setMessage(i18n("Move to trash operation completed."),
1062 DolphinStatusBar::OperationCompleted);
1063 break;
1064 default:
1065 break;
1066 }
1067 }
1068 }
1069 }
1070
1071 void DolphinMainWindow::init()
1072 {
1073 // Check whether Dolphin runs the first time. If yes then
1074 // a proper default window size is given at the end of DolphinMainWindow::init().
1075 GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
1076 const bool firstRun = generalSettings->firstRun();
1077
1078 setAcceptDrops(true);
1079
1080 m_splitter = new QSplitter(this);
1081
1082 DolphinSettings& settings = DolphinSettings::instance();
1083
1084 KBookmarkManager* manager = settings.bookmarkManager();
1085 assert(manager != 0);
1086 KBookmarkGroup root = manager->root();
1087 if (root.first().isNull()) {
1088 root.addBookmark(manager, i18n("Home"), settings.generalSettings()->homeUrl(), "folder_home");
1089 root.addBookmark(manager, i18n("Storage Media"), KUrl("media:/"), "blockdevice");
1090 root.addBookmark(manager, i18n("Network"), KUrl("remote:/"), "network_local");
1091 root.addBookmark(manager, i18n("Root"), KUrl("/"), "folder_red");
1092 root.addBookmark(manager, i18n("Trash"), KUrl("trash:/"), "trashcan_full");
1093 }
1094
1095 setupActions();
1096
1097 const KUrl& homeUrl = root.first().url();
1098 setCaption(homeUrl.fileName());
1099 ViewProperties props(homeUrl);
1100 m_view[PrimaryIdx] = new DolphinView(this,
1101 m_splitter,
1102 homeUrl,
1103 props.viewMode(),
1104 props.showHiddenFiles());
1105 connectViewSignals(PrimaryIdx);
1106 m_view[PrimaryIdx]->show();
1107
1108 m_activeView = m_view[PrimaryIdx];
1109
1110 setCentralWidget(m_splitter);
1111 setupDockWidgets();
1112
1113 setupGUI(Keys|Save|Create|ToolBar);
1114 createGUI();
1115
1116 stateChanged("new_file");
1117 setAutoSaveSettings();
1118
1119 QClipboard* clipboard = QApplication::clipboard();
1120 connect(clipboard, SIGNAL(dataChanged()),
1121 this, SLOT(updatePasteAction()));
1122 updatePasteAction();
1123 updateGoActions();
1124
1125 setupCreateNewMenuActions();
1126
1127 loadSettings();
1128
1129 if (firstRun) {
1130 // assure a proper default size if Dolphin runs the first time
1131 resize(640, 480);
1132 }
1133 }
1134
1135 void DolphinMainWindow::loadSettings()
1136 {
1137 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
1138
1139 KToggleAction* splitAction = static_cast<KToggleAction*>(actionCollection()->action("split_view"));
1140 if (settings->splitView()) {
1141 splitAction->setChecked(true);
1142 toggleSplitView();
1143 }
1144
1145 updateViewActions();
1146 }
1147
1148 void DolphinMainWindow::setupActions()
1149 {
1150 // setup 'File' menu
1151 KAction *action = new KAction(KIcon("window_new"), i18n( "New &Window" ), actionCollection(), "new_window" );
1152 connect(action, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1153
1154 KAction* createFolder = new KAction(i18n("Folder..."), actionCollection(), "create_folder");
1155 createFolder->setIcon(KIcon("folder"));
1156 createFolder->setShortcut(Qt::Key_N);
1157 connect(createFolder, SIGNAL(triggered()), this, SLOT(createFolder()));
1158
1159 KAction* rename = new KAction(i18n("Rename"), actionCollection(), "rename");
1160 rename->setShortcut(Qt::Key_F2);
1161 connect(rename, SIGNAL(triggered()), this, SLOT(rename()));
1162
1163 KAction* moveToTrash = new KAction(i18n("Move to Trash"), actionCollection(), "move_to_trash");
1164 moveToTrash->setIcon(KIcon("edittrash"));
1165 moveToTrash->setShortcut(QKeySequence::Delete);
1166 connect(moveToTrash, SIGNAL(triggered()), this, SLOT(moveToTrash()));
1167
1168 KAction* deleteAction = new KAction(i18n("Delete"), actionCollection(), "delete");
1169 deleteAction->setShortcut(Qt::ALT | Qt::Key_Delete);
1170 deleteAction->setIcon(KIcon("editdelete"));
1171 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItems()));
1172
1173 KAction* properties = new KAction(i18n("Propert&ies"), actionCollection(), "properties");
1174 properties->setShortcut(Qt::Key_Alt | Qt::Key_Return);
1175 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
1176
1177 KStdAction::quit(this, SLOT(quit()), actionCollection());
1178
1179 // setup 'Edit' menu
1180 UndoManager& undoManager = UndoManager::instance();
1181 KStdAction::undo(this,
1182 SLOT(undo()),
1183 actionCollection());
1184 connect(&undoManager, SIGNAL(undoAvailable(bool)),
1185 this, SLOT(slotUndoAvailable(bool)));
1186 connect(&undoManager, SIGNAL(undoTextChanged(const QString&)),
1187 this, SLOT(slotUndoTextChanged(const QString&)));
1188
1189 KStdAction::redo(this,
1190 SLOT(redo()),
1191 actionCollection());
1192 connect(&undoManager, SIGNAL(redoAvailable(bool)),
1193 this, SLOT(slotRedoAvailable(bool)));
1194 connect(&undoManager, SIGNAL(redoTextChanged(const QString&)),
1195 this, SLOT(slotRedoTextChanged(const QString&)));
1196
1197 KStdAction::cut(this, SLOT(cut()), actionCollection());
1198 KStdAction::copy(this, SLOT(copy()), actionCollection());
1199 KStdAction::paste(this, SLOT(paste()), actionCollection());
1200
1201 KAction* selectAll = new KAction(i18n("Select All"), actionCollection(), "select_all");
1202 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1203 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1204
1205 KAction* invertSelection = new KAction(i18n("Invert Selection"), actionCollection(), "invert_selection");
1206 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1207 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1208
1209 // setup 'View' menu
1210 KStdAction::zoomIn(this,
1211 SLOT(zoomIn()),
1212 actionCollection());
1213
1214 KStdAction::zoomOut(this,
1215 SLOT(zoomOut()),
1216 actionCollection());
1217
1218 KToggleAction* iconsView = new KToggleAction(i18n("Icons"), actionCollection(), "icons");
1219 iconsView->setShortcut(Qt::CTRL | Qt::Key_1);
1220 iconsView->setIcon(KIcon("view_icon"));
1221 connect(iconsView, SIGNAL(triggered()), this, SLOT(setIconsView()));
1222
1223 KToggleAction* detailsView = new KToggleAction(i18n("Details"), actionCollection(), "details");
1224 detailsView->setShortcut(Qt::CTRL | Qt::Key_2);
1225 detailsView->setIcon(KIcon("view_text"));
1226 connect(detailsView, SIGNAL(triggered()), this, SLOT(setDetailsView()));
1227
1228 QActionGroup* viewModeGroup = new QActionGroup(this);
1229 viewModeGroup->addAction(iconsView);
1230 viewModeGroup->addAction(detailsView);
1231
1232 KToggleAction* sortByName = new KToggleAction(i18n("By Name"), actionCollection(), "by_name");
1233 connect(sortByName, SIGNAL(triggered()), this, SLOT(sortByName()));
1234
1235 KToggleAction* sortBySize = new KToggleAction(i18n("By Size"), actionCollection(), "by_size");
1236 connect(sortBySize, SIGNAL(triggered()), this, SLOT(sortBySize()));
1237
1238 KToggleAction* sortByDate = new KToggleAction(i18n("By Date"), actionCollection(), "by_date");
1239 connect(sortByDate, SIGNAL(triggered()), this, SLOT(sortByDate()));
1240
1241 QActionGroup* sortGroup = new QActionGroup(this);
1242 sortGroup->addAction(sortByName);
1243 sortGroup->addAction(sortBySize);
1244 sortGroup->addAction(sortByDate);
1245
1246 KToggleAction* sortDescending = new KToggleAction(i18n("Descending"), actionCollection(), "descending");
1247 connect(sortDescending, SIGNAL(triggered()), this, SLOT(toggleSortOrder()));
1248
1249 KToggleAction* showPreview = new KToggleAction(i18n("Show Preview"), actionCollection(), "show_preview");
1250 connect(showPreview, SIGNAL(triggered()), this, SLOT(togglePreview()));
1251
1252 KToggleAction* showHiddenFiles = new KToggleAction(i18n("Show Hidden Files"), actionCollection(), "show_hidden_files");
1253 //showHiddenFiles->setShortcut(Qt::ALT | Qt::Key_ KDE4-TODO: what Qt-Key represents '.'?
1254 connect(showHiddenFiles, SIGNAL(triggered()), this, SLOT(toggleShowHiddenFiles()));
1255
1256 KToggleAction* split = new KToggleAction(i18n("Split View"), actionCollection(), "split_view");
1257 split->setShortcut(Qt::Key_F10);
1258 split->setIcon(KIcon("view_left_right"));
1259 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1260
1261 KAction* reload = new KAction(i18n("Reload"), "F5", actionCollection(), "reload");
1262 reload->setShortcut(Qt::Key_F5);
1263 reload->setIcon(KIcon("reload"));
1264 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1265
1266 KAction* stop = new KAction(i18n("Stop"), actionCollection(), "stop");
1267 stop->setIcon(KIcon("stop"));
1268 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1269
1270 KToggleAction* showFullLocation = new KToggleAction(i18n("Show Full Location"), actionCollection(), "editable_location");
1271 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1272 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1273
1274 KToggleAction* editLocation = new KToggleAction(i18n("Edit Location"), actionCollection(), "edit_location");
1275 editLocation->setShortcut(Qt::Key_F6);
1276 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
1277
1278 KAction* adjustViewProps = new KAction(i18n("Adjust View Properties..."), actionCollection(), "view_properties");
1279 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
1280
1281 // setup 'Go' menu
1282 KStdAction::back(this, SLOT(goBack()), actionCollection());
1283 KStdAction::forward(this, SLOT(goForward()), actionCollection());
1284 KStdAction::up(this, SLOT(goUp()), actionCollection());
1285 KStdAction::home(this, SLOT(goHome()), actionCollection());
1286
1287 // setup 'Tools' menu
1288 KAction* openTerminal = new KAction(i18n("Open Terminal"), actionCollection(), "open_terminal");
1289 openTerminal->setShortcut(Qt::Key_F4);
1290 openTerminal->setIcon(KIcon("konsole"));
1291 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1292
1293 KAction* findFile = new KAction(i18n("Find File..."), actionCollection(), "find_file");
1294 findFile->setShortcut(Qt::Key_F);
1295 findFile->setIcon(KIcon("filefind"));
1296 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
1297
1298 KToggleAction* showFilterBar = new KToggleAction(i18n("Show Filter Bar"), actionCollection(), "show_filter_bar");
1299 showFilterBar->setShortcut(Qt::Key_Slash);
1300 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1301
1302 KAction* compareFiles = new KAction(i18n("Compare Files"), actionCollection(), "compare_files");
1303 compareFiles->setIcon(KIcon("kompare"));
1304 compareFiles->setEnabled(false);
1305 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1306
1307 // setup 'Settings' menu
1308 KStdAction::preferences(this, SLOT(editSettings()), actionCollection());
1309 }
1310
1311 void DolphinMainWindow::setupDockWidgets()
1312 {
1313 QDockWidget *shortcutsDock = new QDockWidget(i18n("Shortcuts"));
1314
1315 shortcutsDock->setObjectName("shortcutsDock");
1316 shortcutsDock->setWidget(new BookmarksSidebarPage(this));
1317
1318 shortcutsDock->toggleViewAction()->setObjectName("show_shortcuts_pane");
1319 shortcutsDock->toggleViewAction()->setText(i18n("Show Shortcuts Panel"));
1320 actionCollection()->insert(shortcutsDock->toggleViewAction());
1321
1322 addDockWidget(Qt::LeftDockWidgetArea, shortcutsDock);
1323
1324 QDockWidget *infoDock = new QDockWidget(i18n("Information"));
1325
1326 infoDock->setObjectName("infoDock");
1327 infoDock->setWidget(new InfoSidebarPage(this));
1328
1329 infoDock->toggleViewAction()->setObjectName("show_info_pane");
1330 infoDock->toggleViewAction()->setText(i18n("Show Information Panel"));
1331 actionCollection()->insert(infoDock->toggleViewAction());
1332
1333 addDockWidget(Qt::RightDockWidgetArea, infoDock);
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->showHiddenFiles());
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::connectViewSignals(int viewIndex)
1598 {
1599 DolphinView* view = m_view[viewIndex];
1600 connect(view, SIGNAL(modeChanged()),
1601 this, SLOT(slotViewModeChanged()));
1602 connect(view, SIGNAL(showHiddenFilesChanged()),
1603 this, SLOT(slotShowHiddenFilesChanged()));
1604 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
1605 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
1606 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
1607 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
1608 connect(view, SIGNAL(selectionChanged()),
1609 this, SLOT(slotSelectionChanged()));
1610 connect(view, SIGNAL(showFilterBarChanged(bool)),
1611 this, SLOT(updateFilterBarAction(bool)));
1612
1613 const UrlNavigator* navigator = view->urlNavigator();
1614 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1615 this, SLOT(slotUrlChanged(const KUrl&)));
1616 connect(navigator, SIGNAL(historyChanged()),
1617 this, SLOT(slotHistoryChanged()));
1618
1619 }
1620
1621 #include "dolphinmainwindow.moc"