]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Re-enable name filtering (e.g. /home/dfaure/*.txt) in konqueror
[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 <kaboutdata.h>
29 #include <kactioncollection.h>
30 #include <kconfiggroup.h>
31 #include <kdebug.h>
32 #include <kdirlister.h>
33 #include <kglobalsettings.h>
34 #include <kiconloader.h>
35 #include <klocale.h>
36 #include <kmessagebox.h>
37 #include <kpluginfactory.h>
38 #include <kpropertiesdialog.h>
39 #include <ktoggleaction.h>
40
41 #include <QActionGroup>
42 #include <QApplication>
43 #include <QClipboard>
44
45 K_PLUGIN_FACTORY(DolphinPartFactory, registerPlugin<DolphinPart>();)
46 // The componentdata name must be dolphinpart so that dolphinpart.rc is found
47 // Alternatively we would have to install it as dolphin/dolphinpart.rc
48 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart"))
49
50 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
51 : KParts::ReadOnlyPart(parent)
52 {
53 Q_UNUSED(args)
54 setComponentData(DolphinPartFactory::componentData(), false);
55 m_extension = new DolphinPartBrowserExtension(this);
56
57 // make sure that other apps using this part find Dolphin's view-file-columns icons
58 KIconLoader::global()->addAppDir("dolphin");
59
60 m_dirLister = new KDirLister;
61 m_dirLister->setAutoUpdate(true);
62 m_dirLister->setMainWindow(parentWidget->window());
63 m_dirLister->setDelayedMimeTypes(true);
64
65 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
66 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
67 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
68
69 m_dolphinModel = new DolphinModel(this);
70 m_dolphinModel->setDirLister(m_dirLister);
71
72 m_proxyModel = new DolphinSortFilterProxyModel(this);
73 m_proxyModel->setSourceModel(m_dolphinModel);
74
75 m_view = new DolphinView(parentWidget,
76 KUrl(),
77 m_dirLister,
78 m_dolphinModel,
79 m_proxyModel);
80 setWidget(m_view);
81
82 setXMLFile("dolphinpart.rc");
83
84 connect(m_view, SIGNAL(infoMessage(QString)),
85 this, SLOT(slotInfoMessage(QString)));
86 connect(m_view, SIGNAL(errorMessage(QString)),
87 this, SLOT(slotErrorMessage(QString)));
88 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
89 this, SLOT(slotItemTriggered(KFileItem)));
90 connect(m_view, SIGNAL(tabRequested(KUrl)),
91 this, SLOT(createNewWindow(KUrl)));
92 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)),
93 this, SLOT(slotOpenContextMenu(KFileItem,KUrl)));
94 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
95 m_extension, SIGNAL(selectionInfo(KFileItemList)));
96 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
97 this, SLOT(slotSelectionChanged(KFileItemList)));
98 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
99 this, SLOT(slotRequestItemInfo(KFileItem)));
100 connect(m_view, SIGNAL(urlChanged(KUrl)),
101 this, SLOT(slotUrlChanged(KUrl)));
102 connect(m_view, SIGNAL(modeChanged()),
103 this, SIGNAL(viewModeChanged())); // relay signal
104
105 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
106 m_actionHandler->setCurrentView(m_view);
107
108 QClipboard* clipboard = QApplication::clipboard();
109 connect(clipboard, SIGNAL(dataChanged()),
110 this, SLOT(updatePasteAction()));
111
112 createActions();
113 m_actionHandler->updateViewActions();
114 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
115
116 // TODO sort_by_* actions
117
118 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
119 // (sort of spacial navigation)
120
121 loadPlugins(this, this, componentData());
122 }
123
124 DolphinPart::~DolphinPart()
125 {
126 delete m_dirLister;
127 }
128
129 void DolphinPart::createActions()
130 {
131 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
132 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
133 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
134
135 KAction *propertiesAction = actionCollection()->addAction( "properties" );
136 propertiesAction->setText( i18nc("@action:inmenu Edit", "Properties") );
137 propertiesAction->setShortcut(Qt::ALT+Qt::Key_Return);
138 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
139
140 // View menu: all done by DolphinViewActionHandler
141
142 // Go menu
143
144 QActionGroup* goActionGroup = new QActionGroup(this);
145 connect(goActionGroup, SIGNAL(triggered(QAction*)),
146 this, SLOT(slotGoTriggered(QAction*)));
147
148 createGoAction("go_applications", "start-here-kde",
149 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
150 goActionGroup);
151 createGoAction("go_network_folders", "folder-remote",
152 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
153 goActionGroup);
154 createGoAction("go_settings", "preferences-system",
155 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
156 goActionGroup);
157 createGoAction("go_trash", "user-trash",
158 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
159 goActionGroup);
160 createGoAction("go_autostart", "",
161 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
162 goActionGroup);
163 }
164
165 void DolphinPart::createGoAction(const char* name, const char* iconName,
166 const QString& text, const QString& url,
167 QActionGroup* actionGroup)
168 {
169 KAction* action = actionCollection()->addAction(name);
170 action->setIcon(KIcon(iconName));
171 action->setText(text);
172 action->setData(url);
173 action->setActionGroup(actionGroup);
174 }
175
176 void DolphinPart::slotGoTriggered(QAction* action)
177 {
178 const QString url = action->data().toString();
179 emit m_extension->openUrlRequest(KUrl(url));
180 }
181
182 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
183 {
184 const bool hasSelection = !selection.isEmpty();
185 if (!hasSelection) {
186 stateChanged("has_no_selection");
187 } else {
188 stateChanged("has_selection");
189 }
190
191 QStringList actions;
192 actions << "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
193 foreach(const QString& actionName, actions) {
194 QAction* action = actionCollection()->action(actionName);
195 Q_ASSERT(action);
196 if (action) {
197 action->setEnabled(hasSelection);
198 }
199 }
200
201 emit m_extension->enableAction("cut", hasSelection);
202 emit m_extension->enableAction("copy", hasSelection);
203 }
204
205 void DolphinPart::updatePasteAction()
206 {
207 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
208 emit m_extension->enableAction( "paste", pasteInfo.first );
209 emit m_extension->setActionText( "paste", pasteInfo.second );
210 }
211
212 KAboutData* DolphinPart::createAboutData()
213 {
214 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
215 }
216
217 bool DolphinPart::openUrl(const KUrl& url)
218 {
219 bool reload = arguments().reload();
220 // A bit of a workaround so that changing the namefilter works: force reload.
221 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
222 if (m_nameFilter != m_dirLister->nameFilter())
223 reload = true;
224 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
225 return true;
226 }
227 setUrl(url); // remember it at the KParts level
228 KUrl visibleUrl(url);
229 if (!m_nameFilter.isEmpty()) {
230 visibleUrl.addPath(m_nameFilter);
231 }
232 QString prettyUrl = visibleUrl.pathOrUrl();
233 emit setWindowCaption(prettyUrl);
234 emit m_extension->setLocationBarUrl(prettyUrl);
235 emit started(0); // get the wheel to spin
236 m_dirLister->setNameFilter(m_nameFilter);
237 m_view->setUrl(url);
238 emit aboutToOpenURL();
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 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
280 }
281
282 void DolphinPart::createNewWindow(const KUrl& url)
283 {
284 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
285 // should be moved into DolphinPart::slotItemTriggered()
286 KFileItem item(S_IFDIR, (mode_t)-1, url);
287 Q_ASSERT(item.mimeTypePtr()->is("inode/directory")); // the signal 'tabRequested' is only emitted for dirs
288 KParts::OpenUrlArguments args;
289 args.setMimeType(item.mimetype());
290 emit m_extension->createNewWindow(url, args);
291 }
292
293 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
294 {
295 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
296 | KParts::BrowserExtension::ShowProperties
297 | KParts::BrowserExtension::ShowUrlOperations;
298 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
299 // popupFlags |= KParts::BrowserExtension::NoDeletion;
300
301 KFileItem item(_item);
302
303 if (item.isNull()) { // viewport context menu
304 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
305 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
306 // and use this as fallback:
307 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
308 }
309
310 KParts::BrowserExtension::ActionGroupMap actionGroups;
311 QList<QAction *> editActions;
312
313 if (!_item.isNull()) { // only for context menu on one or more items
314 // TODO if ( sMoving )
315 editActions.append(actionCollection()->action("rename"));
316
317 bool addTrash = false;
318 bool addDel = false;
319
320 bool isIntoTrash = _item.url().protocol() == "trash";
321
322 if ( /*TODO sMoving &&*/ !isIntoTrash )
323 addTrash = true;
324
325 /* TODO if ( sDeleting ) */ {
326 if ( !item.isLocalFile() )
327 addDel = true;
328 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
329 addTrash = false;
330 addDel = true;
331 }
332 else {
333 KConfigGroup configGroup( KGlobal::config(), "KDE" );
334 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
335 addDel = true;
336 }
337 }
338
339 if (addTrash)
340 editActions.append(actionCollection()->action("move_to_trash"));
341 if (addDel)
342 editActions.append(actionCollection()->action("delete"));
343 actionGroups.insert("editactions", editActions);
344 }
345
346 // TODO: We should change the signature of the slots (and signals) for being able
347 // to tell for which items we want a popup.
348 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
349 : KFileItemList() << item);
350 emit m_extension->popupMenu(QCursor::pos(),
351 items,
352 KParts::OpenUrlArguments(),
353 KParts::BrowserArguments(),
354 popupFlags,
355 actionGroups);
356 }
357
358 void DolphinPart::slotUrlChanged(const KUrl& url)
359 {
360 if (m_view->url() != url) {
361 // If the view URL is not equal to 'url', then an inner URL change has
362 // been done (e. g. by activating an existing column in the column view).
363 openUrl(url);
364 emit m_extension->openUrlNotify();
365 }
366 }
367
368 ////
369
370 void DolphinPartBrowserExtension::cut()
371 {
372 m_part->view()->cutSelectedItems();
373 }
374
375 void DolphinPartBrowserExtension::copy()
376 {
377 m_part->view()->copySelectedItems();
378 }
379
380 void DolphinPartBrowserExtension::paste()
381 {
382 m_part->view()->paste();
383 }
384
385 void DolphinPartBrowserExtension::reparseConfiguration()
386 {
387 m_part->view()->refresh();
388 }
389
390 ////
391
392 void DolphinPart::slotEditMimeType()
393 {
394 const KFileItemList items = m_view->selectedItems();
395 if (!items.isEmpty()) {
396 KonqOperations::editMimeType(items.first().mimetype(), m_view);
397 }
398 }
399
400 void DolphinPart::slotProperties()
401 {
402 const KFileItemList items = m_view->selectedItems();
403 if (!items.isEmpty()) {
404 KPropertiesDialog dialog(items.first().url(), m_view);
405 dialog.exec();
406 }
407 }
408
409 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
410 {
411 QAction* action = actionCollection()->action(viewModeName);
412 Q_ASSERT(action);
413 action->trigger();
414 }
415
416 QString DolphinPart::currentViewMode() const
417 {
418 return m_actionHandler->currentViewModeActionName();
419 }
420
421 void DolphinPart::setNameFilter(const QString& nameFilter)
422 {
423 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
424 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
425 m_nameFilter = nameFilter;
426 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
427 }
428
429 #include "dolphinpart.moc"