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