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