]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
1cd336c8cc30cb02605921171c26890183b04eaf
[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->topLevelWidget());
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 // TODO slotSortingChanged
98
99 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
100 m_actionHandler->setCurrentView(m_view);
101
102 QClipboard* clipboard = QApplication::clipboard();
103 connect(clipboard, SIGNAL(dataChanged()),
104 this, SLOT(updatePasteAction()));
105
106 createActions();
107 m_actionHandler->updateViewActions();
108 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
109
110 // TODO sort_by_* actions
111
112 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
113 // (sort of spacial navigation)
114 }
115
116 DolphinPart::~DolphinPart()
117 {
118 delete m_dirLister;
119 }
120
121 void DolphinPart::createActions()
122 {
123 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
124 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
125 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
126
127 KAction *propertiesAction = actionCollection()->addAction( "properties" );
128 propertiesAction->setText( i18nc("@action:inmenu Edit", "Properties") );
129 propertiesAction->setShortcut(Qt::ALT+Qt::Key_Return);
130 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
131
132 // View menu: all done by DolphinViewActionHandler
133
134 // Go menu
135
136 QActionGroup* goActionGroup = new QActionGroup(this);
137 connect(goActionGroup, SIGNAL(triggered(QAction*)),
138 this, SLOT(slotGoTriggered(QAction*)));
139
140 createGoAction("go_applications", "start-here-kde",
141 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
142 goActionGroup);
143 createGoAction("go_network_folders", "folder-remote",
144 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
145 goActionGroup);
146 createGoAction("go_settings", "preferences-system",
147 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
148 goActionGroup);
149 createGoAction("go_trash", "user-trash",
150 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
151 goActionGroup);
152 createGoAction("go_autostart", "",
153 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
154 goActionGroup);
155 }
156
157 void DolphinPart::createGoAction(const char* name, const char* iconName,
158 const QString& text, const QString& url,
159 QActionGroup* actionGroup)
160 {
161 KAction* action = actionCollection()->addAction(name);
162 action->setIcon(KIcon(iconName));
163 action->setText(text);
164 action->setData(url);
165 action->setActionGroup(actionGroup);
166 }
167
168 void DolphinPart::slotGoTriggered(QAction* action)
169 {
170 const QString url = action->data().toString();
171 emit m_extension->openUrlRequest(KUrl(url));
172 }
173
174 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
175 {
176 const bool hasSelection = !selection.isEmpty();
177 if (!hasSelection) {
178 stateChanged("has_no_selection");
179 } else {
180 stateChanged("has_selection");
181 }
182
183 QStringList actions;
184 actions << "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
185 foreach(const QString& actionName, actions) {
186 QAction* action = actionCollection()->action(actionName);
187 Q_ASSERT(action);
188 if (action) {
189 action->setEnabled(hasSelection);
190 }
191 }
192
193 emit m_extension->enableAction("cut", hasSelection);
194 emit m_extension->enableAction("copy", hasSelection);
195 }
196
197 void DolphinPart::updatePasteAction()
198 {
199 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
200 emit m_extension->enableAction( "paste", pasteInfo.first );
201 emit m_extension->setActionText( "paste", pasteInfo.second );
202 }
203
204 KAboutData* DolphinPart::createAboutData()
205 {
206 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
207 }
208
209 bool DolphinPart::openUrl(const KUrl& url)
210 {
211 const bool reload = arguments().reload();
212 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
213 return true;
214 }
215 setUrl(url); // remember it at the KParts level
216 const QString prettyUrl = url.pathOrUrl();
217 emit setWindowCaption(prettyUrl);
218 emit m_extension->setLocationBarUrl(prettyUrl);
219 emit started(0); // get the wheel to spin
220 m_view->setUrl(url);
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.url(), 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 KFileItemList items; items.append(item);
332 emit m_extension->popupMenu(QCursor::pos(),
333 items,
334 KParts::OpenUrlArguments(),
335 KParts::BrowserArguments(),
336 popupFlags,
337 actionGroups);
338 }
339 }
340
341 void DolphinPart::slotUrlChanged(const KUrl& url)
342 {
343 if (m_view->url() != url) {
344 // If the view URL is not equal to 'url', then an inner URL change has
345 // been done (e. g. by activating an existing column in the column view).
346 openUrl(url);
347 emit m_extension->openUrlNotify();
348 }
349 }
350
351 ////
352
353 void DolphinPartBrowserExtension::cut()
354 {
355 m_part->view()->cutSelectedItems();
356 }
357
358 void DolphinPartBrowserExtension::copy()
359 {
360 m_part->view()->copySelectedItems();
361 }
362
363 void DolphinPartBrowserExtension::paste()
364 {
365 m_part->view()->paste();
366 }
367
368 ////
369
370 void DolphinPart::slotEditMimeType()
371 {
372 const KFileItemList items = m_view->selectedItems();
373 if (!items.isEmpty()) {
374 KonqOperations::editMimeType(items.first().mimetype(), m_view);
375 }
376 }
377
378 void DolphinPart::slotProperties()
379 {
380 const KFileItemList items = m_view->selectedItems();
381 if (!items.isEmpty()) {
382 KPropertiesDialog dialog(items.first().url(), m_view);
383 dialog.exec();
384 }
385 }
386
387 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
388 {
389 QAction* action = actionCollection()->action(viewModeName);
390 Q_ASSERT(action);
391 action->trigger();
392 }
393
394 QString DolphinPart::currentViewMode() const
395 {
396 return m_actionHandler->currentViewModeActionName();
397 }
398
399 #include "dolphinpart.moc"