]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Fix the other bug reported by BCoppens: the tab title was the full path, until switch...
[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 <kactioncollection.h>
22 #include <ktoggleaction.h>
23 #include <QActionGroup>
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinmodel.h"
27
28 #include <kdirlister.h>
29 #include <kmessagebox.h>
30 #include <kparts/browserextension.h>
31 #include <kparts/genericfactory.h>
32 #include <QApplication>
33
34 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
35 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
36
37 class DolphinPartBrowserExtension : public KParts::BrowserExtension
38 {
39 public:
40 DolphinPartBrowserExtension( KParts::ReadOnlyPart* part )
41 : KParts::BrowserExtension( part ) {}
42 };
43
44 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
45 : KParts::ReadOnlyPart(parent)
46 {
47 Q_UNUSED(args)
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, const KUrl&)),
82 this, SLOT(slotOpenContextMenu(KFileItem, const KUrl&)));
83 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
84 m_extension, SIGNAL(selectionInfo(KFileItemList)));
85 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
86 this, SLOT(slotRequestItemInfo(KFileItem)));
87 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
88 this, SLOT(slotUrlChanged(const KUrl&)));
89
90 createActions();
91 updateViewActions();
92
93 // TODO provide these actions in the menu, merged with the existing view-mode-actions somehow
94 // [Q_PROPERTY introspection?]
95
96 // TODO sort_by_* actions
97 // TODO show_*_info actions
98
99 // TODO connect to urlsDropped
100
101 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
102 // (sort of spacial navigation)
103
104 // TODO MMB-click should do something like KonqDirPart::mmbClicked
105
106 // TODO updating the paste action
107 // if (paste) emit m_extension->setActionText( "paste", actionText );
108 // emit m_extension->enableAction( "paste", paste );
109
110 // TODO updating the trash and del actions too - or removing special handling of those from konq?
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
127 void DolphinPart::updateViewActions()
128 {
129 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
130 if (action != 0) {
131 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
132 toggleAction->setChecked(true);
133 }
134 }
135
136 KAboutData* DolphinPart::createAboutData()
137 {
138 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
139 }
140
141 bool DolphinPart::openUrl(const KUrl& url)
142 {
143 const bool reload = arguments().reload();
144 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
145 return true;
146 }
147 setUrl(url); // remember it at the KParts level
148 const QString prettyUrl = url.pathOrUrl();
149 emit setWindowCaption(prettyUrl);
150 emit m_extension->setLocationBarUrl(prettyUrl);
151 m_view->setUrl(url);
152 if (reload)
153 m_view->reload();
154 emit started(0); // get the wheel to spin
155 return true;
156 }
157
158 void DolphinPart::slotCompleted(const KUrl& url)
159 {
160 Q_UNUSED(url)
161 emit completed();
162 }
163
164 void DolphinPart::slotCanceled(const KUrl& url)
165 {
166 slotCompleted(url);
167 }
168
169 void DolphinPart::slotInfoMessage(const QString& msg)
170 {
171 emit setStatusBarText(msg);
172 }
173
174 void DolphinPart::slotErrorMessage(const QString& msg)
175 {
176 KMessageBox::error(m_view, msg);
177 }
178
179 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
180 {
181 emit m_extension->mouseOverInfo(item);
182 }
183
184 void DolphinPart::slotItemTriggered(const KFileItem& item)
185 {
186 qDebug() << QApplication::mouseButtons();
187 if (QApplication::mouseButtons() & Qt::MidButton) {
188 qDebug() << "MMB!!" << item.mimetype();
189 if (item.mimeTypePtr()->is("inode/directory")) {
190 KParts::OpenUrlArguments args;
191 args.setMimeType( item.mimetype() );
192 emit m_extension->createNewWindow( item.url(), args );
193 } else {
194 qDebug() << "run()";
195 item.run();
196 }
197 } else {
198 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
199 qDebug() << "LMB";
200 emit m_extension->openUrlRequest(item.url());
201 }
202 }
203
204 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
205 {
206 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
207 | KParts::BrowserExtension::ShowProperties
208 | KParts::BrowserExtension::ShowUrlOperations;
209 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
210 // popupFlags |= KParts::BrowserExtension::NoDeletion;
211
212 KFileItem item(_item);
213
214 if (item.isNull()) { // viewport context menu
215 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
216 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
217 // and use this as fallback:
218 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
219 }
220
221 KFileItemList items; items.append(item);
222 emit m_extension->popupMenu( QCursor::pos(), items, KParts::OpenUrlArguments(), KParts::BrowserArguments(), popupFlags );
223 }
224
225 void DolphinPart::slotViewModeActionTriggered(QAction* action)
226 {
227 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
228 m_view->setMode(mode);
229 }
230
231 void DolphinPart::slotUrlChanged(const KUrl& url)
232 {
233 if (m_view->url() != url) {
234 // If the view URL is not equal to 'url', then an inner URL change has
235 // been done (e. g. by activating an existing column in the column view).
236 // From the hosts point of view this must be handled like changing the URL.
237 emit m_extension->openUrlRequest(url);
238 }
239 }
240
241 #include "dolphinpart.moc"