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