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