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