]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
prevent that the user can open more than one instance of the settings dialog
[dolphin.git] / src / dolphinpart.cpp
1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "dolphinpart.h"
21 #include "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
25
26 #include <konq_fileitemcapabilities.h>
27 #include <konq_operations.h>
28
29 #include <kaboutdata.h>
30 #include <kactioncollection.h>
31 #include <kconfiggroup.h>
32 #include <kdirlister.h>
33 #include <kglobalsettings.h>
34 #include <kiconloader.h>
35 #include <klocale.h>
36 #include <kmessagebox.h>
37 #include <kpluginfactory.h>
38 #include <ktoggleaction.h>
39 #include <kio/netaccess.h>
40 #include <ktoolinvocation.h>
41 #include <kauthorized.h>
42 #include <knewmenu.h>
43 #include <kmenu.h>
44
45 #include <QActionGroup>
46 #include <QApplication>
47 #include <QClipboard>
48
49 K_PLUGIN_FACTORY(DolphinPartFactory, registerPlugin<DolphinPart>();)
50 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
51
52 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
53 : KParts::ReadOnlyPart(parent)
54 {
55 Q_UNUSED(args)
56 setComponentData(DolphinPartFactory::componentData(), false);
57 m_extension = new DolphinPartBrowserExtension(this);
58
59 // make sure that other apps using this part find Dolphin's view-file-columns icons
60 KIconLoader::global()->addAppDir("dolphin");
61
62 m_dirLister = new KDirLister;
63 m_dirLister->setAutoUpdate(true);
64 m_dirLister->setMainWindow(parentWidget->window());
65 m_dirLister->setDelayedMimeTypes(true);
66
67 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
68 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
69 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
70 connect(m_dirLister, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
71
72 m_dolphinModel = new DolphinModel(this);
73 m_dolphinModel->setDirLister(m_dirLister);
74
75 m_proxyModel = new DolphinSortFilterProxyModel(this);
76 m_proxyModel->setSourceModel(m_dolphinModel);
77
78 m_view = new DolphinView(parentWidget,
79 KUrl(),
80 m_dirLister,
81 m_dolphinModel,
82 m_proxyModel);
83 m_view->setTabsForFilesEnabled(true);
84 setWidget(m_view);
85
86 setXMLFile("dolphinpart.rc");
87
88 connect(m_view, SIGNAL(infoMessage(QString)),
89 this, SLOT(slotInfoMessage(QString)));
90 connect(m_view, SIGNAL(errorMessage(QString)),
91 this, SLOT(slotErrorMessage(QString)));
92 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
93 this, SLOT(slotItemTriggered(KFileItem)));
94 connect(m_view, SIGNAL(tabRequested(KUrl)),
95 this, SLOT(createNewWindow(KUrl)));
96 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)),
97 this, SLOT(slotOpenContextMenu(KFileItem,KUrl)));
98 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
99 m_extension, SIGNAL(selectionInfo(KFileItemList)));
100 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
101 this, SLOT(slotSelectionChanged(KFileItemList)));
102 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
103 this, SLOT(slotRequestItemInfo(KFileItem)));
104 connect(m_view, SIGNAL(urlChanged(KUrl)),
105 this, SLOT(slotUrlChanged(KUrl)));
106 connect(m_view, SIGNAL(requestUrlChange(KUrl)),
107 this, SLOT(slotRequestUrlChange(KUrl)));
108 connect(m_view, SIGNAL(modeChanged()),
109 this, SIGNAL(viewModeChanged())); // relay signal
110
111 // Watch for changes that should result in updates to the
112 // status bar text.
113 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
114 this, SLOT(updateStatusBar()));
115 connect(m_dirLister, SIGNAL(clear()),
116 this, SLOT(updateStatusBar()));
117
118
119 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
120 m_actionHandler->setCurrentView(m_view);
121
122 QClipboard* clipboard = QApplication::clipboard();
123 connect(clipboard, SIGNAL(dataChanged()),
124 this, SLOT(updatePasteAction()));
125
126 createActions();
127 m_actionHandler->updateViewActions();
128 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
129
130 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
131 // (sort of spacial navigation)
132
133 loadPlugins(this, this, componentData());
134 }
135
136 DolphinPart::~DolphinPart()
137 {
138 delete m_dirLister;
139 }
140
141 void DolphinPart::createActions()
142 {
143 // Edit menu
144
145 m_newMenu = new KNewMenu(actionCollection(), widget(), "new_menu");
146 connect(m_newMenu->menu(), SIGNAL(aboutToShow()),
147 this, SLOT(updateNewMenu()));
148
149 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
150 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
151 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
152
153 // View menu: all done by DolphinViewActionHandler
154
155 // Go menu
156
157 QActionGroup* goActionGroup = new QActionGroup(this);
158 connect(goActionGroup, SIGNAL(triggered(QAction*)),
159 this, SLOT(slotGoTriggered(QAction*)));
160
161 createGoAction("go_applications", "start-here-kde",
162 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
163 goActionGroup);
164 createGoAction("go_network_folders", "folder-remote",
165 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
166 goActionGroup);
167 createGoAction("go_settings", "preferences-system",
168 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
169 goActionGroup);
170 createGoAction("go_trash", "user-trash",
171 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
172 goActionGroup);
173 createGoAction("go_autostart", "",
174 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
175 goActionGroup);
176
177 // Tools menu
178 if (KAuthorized::authorizeKAction("shell_access")) {
179 KAction* action = actionCollection()->addAction("open_terminal");
180 action->setIcon(KIcon("utilities-terminal"));
181 action->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
182 connect(action, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
183 action->setShortcut(Qt::Key_F4);
184 }
185
186 }
187
188 void DolphinPart::createGoAction(const char* name, const char* iconName,
189 const QString& text, const QString& url,
190 QActionGroup* actionGroup)
191 {
192 KAction* action = actionCollection()->addAction(name);
193 action->setIcon(KIcon(iconName));
194 action->setText(text);
195 action->setData(url);
196 action->setActionGroup(actionGroup);
197 }
198
199 void DolphinPart::slotGoTriggered(QAction* action)
200 {
201 const QString url = action->data().toString();
202 emit m_extension->openUrlRequest(KUrl(url));
203 }
204
205 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
206 {
207 const bool hasSelection = !selection.isEmpty();
208
209 QAction* renameAction = actionCollection()->action("rename");
210 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
211 QAction* deleteAction = actionCollection()->action("delete");
212 QAction* editMimeTypeAction = actionCollection()->action("editMimeType");
213 QAction* propertiesAction = actionCollection()->action("properties");
214 QAction* deleteWithTrashShortcut = actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
215
216 if (!hasSelection) {
217 stateChanged("has_no_selection");
218
219 emit m_extension->enableAction("cut", false);
220 emit m_extension->enableAction("copy", false);
221 deleteWithTrashShortcut->setEnabled(false);
222 editMimeTypeAction->setEnabled(false);
223 } else {
224 stateChanged("has_selection");
225
226 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
227 // in libkonq
228 KonqFileItemCapabilities capabilities(selection);
229 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
230
231 renameAction->setEnabled(capabilities.supportsMoving());
232 moveToTrashAction->setEnabled(enableMoveToTrash);
233 deleteAction->setEnabled(capabilities.supportsDeleting());
234 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
235 editMimeTypeAction->setEnabled(true);
236 propertiesAction->setEnabled(true);
237 emit m_extension->enableAction("cut", capabilities.supportsMoving());
238 emit m_extension->enableAction("copy", true);
239 }
240 }
241
242 void DolphinPart::updatePasteAction()
243 {
244 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
245 emit m_extension->enableAction( "paste", pasteInfo.first );
246 emit m_extension->setActionText( "paste", pasteInfo.second );
247 }
248
249 KAboutData* DolphinPart::createAboutData()
250 {
251 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
252 }
253
254 bool DolphinPart::openUrl(const KUrl& url)
255 {
256 bool reload = arguments().reload();
257 // A bit of a workaround so that changing the namefilter works: force reload.
258 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
259 if (m_nameFilter != m_dirLister->nameFilter())
260 reload = true;
261 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
262 return true;
263 }
264 setUrl(url); // remember it at the KParts level
265 KUrl visibleUrl(url);
266 if (!m_nameFilter.isEmpty()) {
267 visibleUrl.addPath(m_nameFilter);
268 }
269 QString prettyUrl = visibleUrl.pathOrUrl();
270 emit setWindowCaption(prettyUrl);
271 emit m_extension->setLocationBarUrl(prettyUrl);
272 emit started(0); // get the wheel to spin
273 m_dirLister->setNameFilter(m_nameFilter);
274 m_view->setUrl(url);
275 emit aboutToOpenURL();
276 if (reload)
277 m_view->reload();
278 return true;
279 }
280
281 void DolphinPart::slotCompleted(const KUrl& url)
282 {
283 Q_UNUSED(url)
284 emit completed();
285 }
286
287 void DolphinPart::slotCanceled(const KUrl& url)
288 {
289 slotCompleted(url);
290 }
291
292 void DolphinPart::slotInfoMessage(const QString& msg)
293 {
294 emit setStatusBarText(msg);
295 }
296
297 void DolphinPart::slotErrorMessage(const QString& msg)
298 {
299 KMessageBox::error(m_view, msg);
300 }
301
302 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
303 {
304 emit m_extension->mouseOverInfo(item);
305 if (item.isNull()) {
306 updateStatusBar();
307 } else {
308 ReadOnlyPart::setStatusBarText(item.getStatusBarInfo());
309 }
310 }
311
312 void DolphinPart::slotItemTriggered(const KFileItem& item)
313 {
314 KParts::OpenUrlArguments args;
315 args.setMimeType(item.mimetype());
316
317 // Ideally, konqueror should be changed to not require trustedSource for directory views,
318 // since the idea was not to need BrowserArguments for non-browser stuff...
319 KParts::BrowserArguments browserArgs;
320 browserArgs.trustedSource = true;
321 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
322 }
323
324 void DolphinPart::createNewWindow(const KUrl& url)
325 {
326 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
327 // should be moved into DolphinPart::slotItemTriggered()
328 KFileItem item(S_IFDIR, (mode_t)-1, url);
329 KParts::OpenUrlArguments args;
330 args.setMimeType(item.mimetype());
331 emit m_extension->createNewWindow(url, args);
332 }
333
334 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
335 {
336 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
337 | KParts::BrowserExtension::ShowProperties
338 | KParts::BrowserExtension::ShowUrlOperations;
339
340 KFileItem item(_item);
341
342 if (item.isNull()) { // viewport context menu
343 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
344 item = m_dirLister->rootItem();
345 if (item.isNull())
346 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
347 }
348
349 KParts::BrowserExtension::ActionGroupMap actionGroups;
350 QList<QAction *> editActions;
351
352 if (!_item.isNull()) { // only for context menu on one or more items
353 bool sDeleting = true;
354 bool sMoving = true;
355
356 // If the parent directory of the selected item is writable, moving
357 // and deleting are possible.
358 KFileItem parentDir = m_dirLister->rootItem();
359 if (!parentDir.isNull() && !parentDir.isWritable()) {
360 popupFlags |= KParts::BrowserExtension::NoDeletion;
361 sDeleting = false;
362 sMoving = false;
363 }
364
365 if ( sMoving )
366 editActions.append(actionCollection()->action("rename"));
367
368 bool addTrash = false;
369 bool addDel = false;
370
371 bool isIntoTrash = _item.url().protocol() == "trash";
372
373 if ( sMoving && !isIntoTrash && item.isLocalFile() )
374 addTrash = true;
375
376 if ( sDeleting ) {
377 if ( !item.isLocalFile() )
378 addDel = true;
379 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
380 addTrash = false;
381 addDel = true;
382 }
383 else {
384 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
385 KConfigGroup configGroup(globalConfig, "KDE");
386 if ( configGroup.readEntry("ShowDeleteCommand", false) )
387 addDel = true;
388 }
389 }
390
391 if (addTrash)
392 editActions.append(actionCollection()->action("move_to_trash"));
393 if (addDel)
394 editActions.append(actionCollection()->action("delete"));
395 actionGroups.insert("editactions", editActions);
396
397 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
398 // since otherwise the created file would not be visible.
399 // But in treeview mode we should allow it.
400 if (m_view->itemsExpandable())
401 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
402
403 }
404
405 // TODO: We should change the signature of the slots (and signals) for being able
406 // to tell for which items we want a popup.
407 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
408 : KFileItemList() << item);
409 emit m_extension->popupMenu(QCursor::pos(),
410 items,
411 KParts::OpenUrlArguments(),
412 KParts::BrowserArguments(),
413 popupFlags,
414 actionGroups);
415 }
416
417 void DolphinPart::slotUrlChanged(const KUrl& url)
418 {
419 KParts::ReadOnlyPart::setUrl(url);
420 QString prettyUrl = url.pathOrUrl();
421 emit m_extension->setLocationBarUrl(prettyUrl);
422 }
423
424 void DolphinPart::slotRequestUrlChange(const KUrl& url)
425 {
426 if (m_view->url() != url) {
427 // If the view URL is not equal to 'url', then an inner URL change has
428 // been done (e. g. by activating an existing column in the column view).
429 openUrl(url);
430 emit m_extension->openUrlNotify();
431 }
432 }
433
434 ////
435
436 void DolphinPartBrowserExtension::cut()
437 {
438 m_part->view()->cutSelectedItems();
439 }
440
441 void DolphinPartBrowserExtension::copy()
442 {
443 m_part->view()->copySelectedItems();
444 }
445
446 void DolphinPartBrowserExtension::paste()
447 {
448 m_part->view()->paste();
449 }
450
451 void DolphinPartBrowserExtension::reparseConfiguration()
452 {
453 m_part->view()->refresh();
454 }
455
456 ////
457
458 void DolphinPart::slotEditMimeType()
459 {
460 const KFileItemList items = m_view->selectedItems();
461 if (!items.isEmpty()) {
462 KonqOperations::editMimeType(items.first().mimetype(), m_view);
463 }
464 }
465
466 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
467 {
468 QAction* action = actionCollection()->action(viewModeName);
469 Q_ASSERT(action);
470 action->trigger();
471 }
472
473 QString DolphinPart::currentViewMode() const
474 {
475 return m_actionHandler->currentViewModeActionName();
476 }
477
478 void DolphinPart::setNameFilter(const QString& nameFilter)
479 {
480 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
481 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
482 m_nameFilter = nameFilter;
483 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
484 }
485
486 void DolphinPart::slotOpenTerminal()
487 {
488 QString dir(QDir::homePath());
489
490 KUrl u(url());
491
492 // If the given directory is not local, it can still be the URL of an
493 // ioslave using UDS_LOCAL_PATH which to be converted first.
494 u = KIO::NetAccess::mostLocalUrl(u, widget());
495
496 //If the URL is local after the above conversion, set the directory.
497 if (u.isLocalFile()) {
498 dir = u.path();
499 }
500
501 KToolInvocation::invokeTerminal(QString(), dir);
502 }
503
504 void DolphinPart::updateNewMenu()
505 {
506 // As requested by KNewMenu :
507 m_newMenu->slotCheckUpToDate();
508 // And set the files that the menu apply on :
509 m_newMenu->setPopupFiles(url());
510 }
511
512 void DolphinPart::updateStatusBar()
513 {
514 emit ReadOnlyPart::setStatusBarText(m_view->statusBarText());
515 }
516
517 void DolphinPart::updateProgress(int percent)
518 {
519 m_extension->loadingProgress(percent);
520 }
521
522 #include "dolphinpart.moc"