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