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