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