]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
fixed issue that when using "Use common view properties for all folders" the view...
[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 "dolphinsortfilterproxymodel.h"
22 #include "dolphinview.h"
23 #include "dolphinmodel.h"
24
25 #include <konq_operations.h>
26
27 #include <kactioncollection.h>
28 #include <kdirlister.h>
29 #include <kmessagebox.h>
30 #include <kparts/genericfactory.h>
31 #include <ktoggleaction.h>
32 #include <kconfiggroup.h>
33
34 #include <QActionGroup>
35 #include <QApplication>
36 #include <QClipboard>
37
38 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
39 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
40
41 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
42 : KParts::ReadOnlyPart(parent)
43 {
44 Q_UNUSED(args)
45 setComponentData( DolphinPartFactory::componentData() );
46 m_extension = new DolphinPartBrowserExtension(this);
47
48 m_dirLister = new KDirLister;
49 m_dirLister->setAutoUpdate(true);
50 m_dirLister->setMainWindow(parentWidget->topLevelWidget());
51 m_dirLister->setDelayedMimeTypes(true);
52
53 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
54 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
55 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
56
57 m_dolphinModel = new DolphinModel(this);
58 m_dolphinModel->setDirLister(m_dirLister);
59
60 m_proxyModel = new DolphinSortFilterProxyModel(this);
61 m_proxyModel->setSourceModel(m_dolphinModel);
62
63 m_view = new DolphinView(parentWidget,
64 KUrl(),
65 m_dirLister,
66 m_dolphinModel,
67 m_proxyModel);
68 setWidget(m_view);
69
70 setXMLFile("dolphinpart.rc");
71
72 connect(m_view, SIGNAL(infoMessage(QString)),
73 this, SLOT(slotInfoMessage(QString)));
74 connect(m_view, SIGNAL(errorMessage(QString)),
75 this, SLOT(slotErrorMessage(QString)));
76 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
77 this, SLOT(slotItemTriggered(KFileItem)));
78 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)),
79 this, SLOT(slotOpenContextMenu(KFileItem,KUrl)));
80 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
81 m_extension, SIGNAL(selectionInfo(KFileItemList)));
82 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
83 this, SLOT(slotSelectionChanged(KFileItemList)));
84 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
85 this, SLOT(slotRequestItemInfo(KFileItem)));
86 connect(m_view, SIGNAL(urlChanged(KUrl)),
87 this, SLOT(slotUrlChanged(KUrl)));
88 connect(m_view, SIGNAL(modeChanged()),
89 this, SLOT(updateViewActions()));
90
91 QClipboard* clipboard = QApplication::clipboard();
92 connect(clipboard, SIGNAL(dataChanged()),
93 this, SLOT(updatePasteAction()));
94
95 createActions();
96 updateViewActions();
97 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
98
99 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
100 // [Q_PROPERTY introspection?]
101
102 // TODO sort_by_* actions
103 // TODO show_*_info actions
104
105 // TODO connect to urlsDropped
106
107 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
108 // (sort of spacial navigation)
109
110 // TODO MMB-click should do something like KonqDirPart::mmbClicked
111 }
112
113 DolphinPart::~DolphinPart()
114 {
115 delete m_dirLister;
116 }
117
118 void DolphinPart::createActions()
119 {
120 QActionGroup* viewModeActions = new QActionGroup(this);
121 viewModeActions->addAction(DolphinView::iconsModeAction(actionCollection()));
122 viewModeActions->addAction(DolphinView::detailsModeAction(actionCollection()));
123 viewModeActions->addAction(DolphinView::columnsModeAction(actionCollection()));
124 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
125
126 KAction* renameAction = DolphinView::createRenameAction(actionCollection());
127 connect(renameAction, SIGNAL(triggered()), m_view, SLOT(renameSelectedItems()));
128
129 KAction* moveToTrashAction = DolphinView::createMoveToTrashAction(actionCollection());
130 connect(moveToTrashAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)),
131 this, SLOT(slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers)));
132
133 KAction* deleteAction = DolphinView::createDeleteAction(actionCollection());
134 connect(deleteAction, SIGNAL(triggered()), m_view, SLOT(deleteSelectedItems()));
135
136 // This action doesn't appear in the GUI, it's for the shortcut only.
137 // KNewMenu takes care of the GUI stuff.
138 KAction* newDirAction = actionCollection()->addAction( "create_dir" );
139 newDirAction->setText( i18n("Create Folder..." ) );
140 connect(newDirAction, SIGNAL(triggered()), SLOT(slotNewDir()));
141 newDirAction->setShortcut(Qt::Key_F10);
142 widget()->addAction(newDirAction);
143 }
144
145 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
146 {
147 // Yes, DolphinMainWindow has very similar code :/
148 const bool hasSelection = !selection.isEmpty();
149 if (!hasSelection) {
150 stateChanged("has_no_selection");
151 } else {
152 stateChanged("has_selection");
153 }
154
155 QStringList actions;
156 actions << "rename" << "move_to_trash" << "delete";
157 foreach(const QString& actionName, actions) {
158 QAction* action = actionCollection()->action(actionName);
159 Q_ASSERT(action);
160 if (action) {
161 action->setEnabled(hasSelection);
162 }
163 }
164
165 emit m_extension->enableAction("cut", hasSelection);
166 emit m_extension->enableAction("copy", hasSelection);
167 }
168
169 void DolphinPart::updatePasteAction()
170 {
171 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
172 emit m_extension->enableAction( "paste", pasteInfo.first );
173 emit m_extension->setActionText( "paste", pasteInfo.second );
174 }
175
176 void DolphinPart::updateViewActions()
177 {
178 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
179 if (action != 0) {
180 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
181 toggleAction->setChecked(true);
182 }
183 }
184
185 KAboutData* DolphinPart::createAboutData()
186 {
187 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
188 }
189
190 bool DolphinPart::openUrl(const KUrl& url)
191 {
192 const bool reload = arguments().reload();
193 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
194 return true;
195 }
196 setUrl(url); // remember it at the KParts level
197 const QString prettyUrl = url.pathOrUrl();
198 emit setWindowCaption(prettyUrl);
199 emit m_extension->setLocationBarUrl(prettyUrl);
200 emit started(0); // get the wheel to spin
201 m_view->setUrl(url);
202 if (reload)
203 m_view->reload();
204 return true;
205 }
206
207 void DolphinPart::slotCompleted(const KUrl& url)
208 {
209 Q_UNUSED(url)
210 emit completed();
211 }
212
213 void DolphinPart::slotCanceled(const KUrl& url)
214 {
215 slotCompleted(url);
216 }
217
218 void DolphinPart::slotInfoMessage(const QString& msg)
219 {
220 emit setStatusBarText(msg);
221 }
222
223 void DolphinPart::slotErrorMessage(const QString& msg)
224 {
225 KMessageBox::error(m_view, msg);
226 }
227
228 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
229 {
230 emit m_extension->mouseOverInfo(item);
231 }
232
233 void DolphinPart::slotItemTriggered(const KFileItem& item)
234 {
235 qDebug() << QApplication::mouseButtons();
236 if (QApplication::mouseButtons() & Qt::MidButton) {
237 qDebug() << "MMB!!" << item.mimetype();
238 if (item.mimeTypePtr()->is("inode/directory")) {
239 KParts::OpenUrlArguments args;
240 args.setMimeType( item.mimetype() );
241 emit m_extension->createNewWindow( item.url(), args );
242 } else {
243 qDebug() << "run()";
244 item.run();
245 }
246 } else {
247 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
248 qDebug() << "LMB";
249 emit m_extension->openUrlRequest(item.url());
250 }
251 }
252
253 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
254 {
255 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
256 | KParts::BrowserExtension::ShowProperties
257 | KParts::BrowserExtension::ShowUrlOperations;
258 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
259 // popupFlags |= KParts::BrowserExtension::NoDeletion;
260
261 KFileItem item(_item);
262
263 if (item.isNull()) { // viewport context menu
264 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
265 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
266 // and use this as fallback:
267 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
268 }
269
270 KParts::BrowserExtension::ActionGroupMap actionGroups;
271 QList<QAction *> editActions;
272
273 if (!item.isNull()) { // only for context menu on one or more items
274 // TODO if ( sMoving )
275 editActions.append(actionCollection()->action("rename"));
276
277 bool addTrash = false;
278 bool addDel = false;
279
280 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
281 addTrash = true;
282
283 /* TODO if ( sDeleting ) */ {
284 if ( !item.isLocalFile() )
285 addDel = true;
286 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
287 addTrash = false;
288 addDel = true;
289 }
290 else {
291 KConfigGroup configGroup( KGlobal::config(), "KDE" );
292 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
293 addDel = true;
294 }
295 }
296
297 if (addTrash)
298 editActions.append(actionCollection()->action("move_to_trash"));
299 if (addDel)
300 editActions.append(actionCollection()->action("delete"));
301 actionGroups.insert("editactions", editActions);
302
303 KFileItemList items; items.append(item);
304 emit m_extension->popupMenu(QCursor::pos(),
305 items,
306 KParts::OpenUrlArguments(),
307 KParts::BrowserArguments(),
308 popupFlags,
309 actionGroups);
310 }
311 }
312
313 void DolphinPart::slotViewModeActionTriggered(QAction* action)
314 {
315 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
316 m_view->setMode(mode);
317 }
318
319 void DolphinPart::slotUrlChanged(const KUrl& url)
320 {
321 if (m_view->url() != url) {
322 // If the view URL is not equal to 'url', then an inner URL change has
323 // been done (e. g. by activating an existing column in the column view).
324 // From the hosts point of view this must be handled like changing the URL.
325 emit m_extension->openUrlRequest(url);
326 }
327 }
328
329 ////
330
331 void DolphinPartBrowserExtension::cut()
332 {
333 m_part->view()->cutSelectedItems();
334 }
335
336 void DolphinPartBrowserExtension::copy()
337 {
338 m_part->view()->copySelectedItems();
339 }
340
341 void DolphinPartBrowserExtension::paste()
342 {
343 m_part->view()->paste();
344 }
345
346 ////
347
348 void DolphinPart::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
349 {
350 // Note: kde3's konq_mainwindow.cpp used to check
351 // reason == KAction::PopupMenuActivation && ...
352 // but this isn't supported anymore
353 if (modifiers & Qt::ShiftModifier)
354 m_view->deleteSelectedItems();
355 else
356 m_view->trashSelectedItems();
357 }
358
359 void DolphinPart::slotNewDir()
360 {
361 KonqOperations::newDir(widget(), url());
362 }
363
364 #include "dolphinpart.moc"