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