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