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