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