]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Bugfix for konqueror for in dolphin: open in new tab/window opens one of the selected...
[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 if (reload)
221 m_view->reload();
222 return true;
223 }
224
225 void DolphinPart::slotCompleted(const KUrl& url)
226 {
227 Q_UNUSED(url)
228 emit completed();
229 }
230
231 void DolphinPart::slotCanceled(const KUrl& url)
232 {
233 slotCompleted(url);
234 }
235
236 void DolphinPart::slotInfoMessage(const QString& msg)
237 {
238 emit setStatusBarText(msg);
239 }
240
241 void DolphinPart::slotErrorMessage(const QString& msg)
242 {
243 KMessageBox::error(m_view, msg);
244 }
245
246 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
247 {
248 emit m_extension->mouseOverInfo(item);
249 }
250
251 void DolphinPart::slotItemTriggered(const KFileItem& item)
252 {
253 KParts::OpenUrlArguments args;
254 args.setMimeType(item.mimetype());
255
256 // Ideally, konqueror should be changed to not require trustedSource for directory views,
257 // since the idea was not to need BrowserArguments for non-browser stuff...
258 KParts::BrowserArguments browserArgs;
259 browserArgs.trustedSource = true;
260
261 // MMB click support.
262 // TODO: this doesn't work, mouseButtons() is always 0.
263 // Issue N176832 for the missing QAIV signal; task 177399
264 kDebug() << QApplication::mouseButtons();
265 if (QApplication::mouseButtons() & Qt::MidButton) {
266 kDebug() << "MMB!!" << item.mimetype();
267 if (item.mimeTypePtr()->is("inode/directory")) {
268 emit m_extension->createNewWindow(item.url(), args);
269 } else {
270 kDebug() << "run()";
271 item.run();
272 }
273 } else {
274 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
275 kDebug() << "LMB";
276 emit m_extension->openUrlRequest(item.url(), args, browserArgs);
277 }
278 }
279
280 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
281 {
282 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
283 | KParts::BrowserExtension::ShowProperties
284 | KParts::BrowserExtension::ShowUrlOperations;
285 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
286 // popupFlags |= KParts::BrowserExtension::NoDeletion;
287
288 KFileItem item(_item);
289
290 if (item.isNull()) { // viewport context menu
291 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
292 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
293 // and use this as fallback:
294 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
295 }
296
297 KParts::BrowserExtension::ActionGroupMap actionGroups;
298 QList<QAction *> editActions;
299
300 if (!item.isNull()) { // only for context menu on one or more items
301 // TODO if ( sMoving )
302 editActions.append(actionCollection()->action("rename"));
303
304 bool addTrash = false;
305 bool addDel = false;
306
307 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
308 addTrash = true;
309
310 /* TODO if ( sDeleting ) */ {
311 if ( !item.isLocalFile() )
312 addDel = true;
313 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
314 addTrash = false;
315 addDel = true;
316 }
317 else {
318 KConfigGroup configGroup( KGlobal::config(), "KDE" );
319 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
320 addDel = true;
321 }
322 }
323
324 if (addTrash)
325 editActions.append(actionCollection()->action("move_to_trash"));
326 if (addDel)
327 editActions.append(actionCollection()->action("delete"));
328 actionGroups.insert("editactions", editActions);
329
330 KFileItemList items = m_view->selectedItems();
331 emit m_extension->popupMenu(QCursor::pos(),
332 items,
333 KParts::OpenUrlArguments(),
334 KParts::BrowserArguments(),
335 popupFlags,
336 actionGroups);
337 }
338 }
339
340 void DolphinPart::slotUrlChanged(const KUrl& url)
341 {
342 if (m_view->url() != url) {
343 // If the view URL is not equal to 'url', then an inner URL change has
344 // been done (e. g. by activating an existing column in the column view).
345 openUrl(url);
346 emit m_extension->openUrlNotify();
347 }
348 }
349
350 ////
351
352 void DolphinPartBrowserExtension::cut()
353 {
354 m_part->view()->cutSelectedItems();
355 }
356
357 void DolphinPartBrowserExtension::copy()
358 {
359 m_part->view()->copySelectedItems();
360 }
361
362 void DolphinPartBrowserExtension::paste()
363 {
364 m_part->view()->paste();
365 }
366
367 ////
368
369 void DolphinPart::slotEditMimeType()
370 {
371 const KFileItemList items = m_view->selectedItems();
372 if (!items.isEmpty()) {
373 KonqOperations::editMimeType(items.first().mimetype(), m_view);
374 }
375 }
376
377 void DolphinPart::slotProperties()
378 {
379 const KFileItemList items = m_view->selectedItems();
380 if (!items.isEmpty()) {
381 KPropertiesDialog dialog(items.first().url(), m_view);
382 dialog.exec();
383 }
384 }
385
386 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
387 {
388 QAction* action = actionCollection()->action(viewModeName);
389 Q_ASSERT(action);
390 action->trigger();
391 }
392
393 QString DolphinPart::currentViewMode() const
394 {
395 return m_actionHandler->currentViewModeActionName();
396 }
397
398 #include "dolphinpart.moc"