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