]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
5bb59ec045113fc4124e33af0f6d61682601e7bc
[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 <kdirlister.h>
26 #include <kmessagebox.h>
27 #include <kparts/browserextension.h>
28 #include <kparts/genericfactory.h>
29 #include <QApplication>
30
31 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
32 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
33
34 class DolphinPartBrowserExtension : public KParts::BrowserExtension
35 {
36 public:
37 DolphinPartBrowserExtension( KParts::ReadOnlyPart* part )
38 : KParts::BrowserExtension( part ) {}
39 };
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 connect(m_view, SIGNAL(infoMessage(QString)),
71 this, SLOT(slotInfoMessage(QString)));
72 connect(m_view, SIGNAL(errorMessage(QString)),
73 this, SLOT(slotErrorMessage(QString)));
74 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
75 this, SLOT(slotItemTriggered(KFileItem)));
76 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)),
77 this, SLOT(slotOpenContextMenu(KFileItem, const KUrl&)));
78 connect(m_view, SIGNAL(selectionChanged(QList<KFileItem>)),
79 m_extension, SIGNAL(selectionInfo(QList<KFileItem>)));
80
81 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
82 this, SLOT(slotRequestItemInfo(KFileItem)));
83
84 // TODO provide a way to switch from iconview to listview (and others)
85
86 // TODO connect to urlsDropped
87
88 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
89 // (sort of spacial navigation)
90
91 // TODO MMB-click should do something like KonqDirPart::mmbClicked
92
93 // TODO updating the paste action
94 // if (paste) emit m_extension->setActionText( "paste", actionText );
95 // emit m_extension->enableAction( "paste", paste );
96
97 // TODO updating the trash and del actions too - or removing special handling of those from konq?
98 }
99
100 DolphinPart::~DolphinPart()
101 {
102 delete m_dirLister;
103 }
104
105 KAboutData* DolphinPart::createAboutData()
106 {
107 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
108 }
109
110 bool DolphinPart::openUrl(const KUrl& url)
111 {
112 const QString prettyUrl = url.pathOrUrl();
113 emit setWindowCaption(prettyUrl);
114 emit m_extension->setLocationBarUrl(prettyUrl);
115 const bool reload = arguments().reload();
116 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
117 return true;
118 }
119 setUrl(url); // remember it at the KParts level
120 m_view->setUrl(url);
121 if (reload)
122 m_view->reload();
123 emit started(0); // get the wheel to spin
124 return true;
125 }
126
127 void DolphinPart::slotCompleted(const KUrl& url)
128 {
129 Q_UNUSED(url)
130 emit completed();
131 }
132
133 void DolphinPart::slotCanceled(const KUrl& url)
134 {
135 slotCompleted(url);
136 }
137
138 void DolphinPart::slotInfoMessage(const QString& msg)
139 {
140 emit setStatusBarText(msg);
141 }
142
143 void DolphinPart::slotErrorMessage(const QString& msg)
144 {
145 KMessageBox::error(m_view, msg);
146 }
147
148 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
149 {
150 emit m_extension->mouseOverInfo(item);
151 }
152
153 void DolphinPart::slotItemTriggered(const KFileItem& item)
154 {
155 qDebug() << QApplication::mouseButtons();
156 if (QApplication::mouseButtons() & Qt::MidButton) {
157 qDebug() << "MMB!!" << item.mimetype();
158 if (item.mimeTypePtr()->is("inode/directory")) {
159 KParts::OpenUrlArguments args;
160 args.setMimeType( item.mimetype() );
161 emit m_extension->createNewWindow( item.url(), args );
162 } else {
163 qDebug() << "run()";
164 item.run();
165 }
166 } else {
167 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
168 qDebug() << "LMB";
169 emit m_extension->openUrlRequest(item.url());
170 }
171 }
172
173 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
174 {
175 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
176 | KParts::BrowserExtension::ShowProperties
177 | KParts::BrowserExtension::ShowUrlOperations;
178 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
179 // popupFlags |= KParts::BrowserExtension::NoDeletion;
180
181 KFileItem item(_item);
182
183 if (item.isNull()) { // viewport context menu
184 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
185 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
186 // and use this as fallback:
187 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
188 }
189
190 KFileItemList items; items.append(item);
191 emit m_extension->popupMenu( QCursor::pos(), items, KParts::OpenUrlArguments(), KParts::BrowserArguments(), popupFlags );
192 }
193
194 #include "dolphinpart.moc"