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