1 | /* This file is part of the KDE project |
2 | Copyright (C) 2004 Frans Englich <frans.englich@telia.com> |
3 | Copyright (C) 2003 Matthias Kretz <kretz@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License version 2 as published by the Free Software Foundation. |
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 "kcmoduleproxy.h" |
21 | #include "kcmoduleproxy_p.h" |
22 | |
23 | #include <QtGui/QApplication> |
24 | #include <QtGui/QCursor> |
25 | #include <QtCore/QDataStream> |
26 | #include <QtGui/QKeyEvent> |
27 | #include <QtCore/QFileInfo> |
28 | #include <QtGui/QFrame> |
29 | #include <QtGui/QLabel> |
30 | #include <QtGui/QLayout> |
31 | #include <QtCore/QPoint> |
32 | |
33 | #include <QtGui/QImage> |
34 | |
35 | #include <QtDBus/QtDBus> |
36 | |
37 | #include <kaboutdata.h> |
38 | #include <kcmodule.h> |
39 | #include <kcmoduleinfo.h> |
40 | |
41 | #include <kdebug.h> |
42 | #include <kdialog.h> |
43 | #include <klocale.h> |
44 | #include <kservice.h> |
45 | #include <kstandarddirs.h> |
46 | #include <kuser.h> |
47 | |
48 | #include <kvbox.h> |
49 | |
50 | #include <kcmoduleloader.h> |
51 | |
52 | #include "kcolorscheme.h" |
53 | |
54 | #include "ksettingswidgetadaptor.h" |
55 | |
56 | /* |
57 | TODO: |
58 | |
59 | - Two Layout problems in runAsRoot: |
60 | * lblBusy doesn't show |
61 | * d->kcm/d->rootInfo doesn't get it right when the user |
62 | presses cancel in the kdesu dialog |
63 | |
64 | - Resizing horizontally is contrained; minimum size is set somewhere. |
65 | It appears to be somehow derived from the module's size. |
66 | |
67 | - Prettify: set icon in KCMultiDialog. |
68 | |
69 | */ |
70 | /***************************************************************/ |
71 | KCModule* KCModuleProxy::realModule() const |
72 | { |
73 | Q_D(const KCModuleProxy); |
74 | /* |
75 | * Note, don't call any function that calls realModule() since |
76 | * that leads to an infinite loop. |
77 | */ |
78 | |
79 | /* Already loaded */ |
80 | if( !d->kcm ) |
81 | { |
82 | QApplication::setOverrideCursor( Qt::WaitCursor ); |
83 | const_cast<KCModuleProxyPrivate *>(d)->loadModule(); |
84 | QApplication::restoreOverrideCursor(); |
85 | } |
86 | return d->kcm; |
87 | } |
88 | |
89 | void KCModuleProxyPrivate::loadModule() |
90 | { |
91 | if( !topLayout ) |
92 | { |
93 | topLayout = new QVBoxLayout( parent ); |
94 | topLayout->setMargin( 0 ); |
95 | |
96 | QString name = modInfo.handle(); |
97 | name.replace("-" , "_" ); //hyphen is not allowed in dbus, only [A-Z][a-z][0-9]_ |
98 | dbusPath = QLatin1String("/internal/KSettingsWidget/" ) + name; |
99 | dbusService = QLatin1String("org.kde.internal.KSettingsWidget_" ) + name; |
100 | } |
101 | |
102 | if( QDBusConnection::sessionBus().registerService( dbusService ) || bogusOccupier ) |
103 | { /* We got the name we requested, because no one was before us, |
104 | * or, it was an random application which had picked that name */ |
105 | kDebug(711) << "Module not already loaded, loading module " << modInfo.moduleName() << " from library " << modInfo.library() << " using symbol " << modInfo.handle(); |
106 | |
107 | kcm = KCModuleLoader::loadModule( modInfo, KCModuleLoader::Inline, parent, args ); |
108 | |
109 | QObject::connect(kcm, SIGNAL(changed(bool)), parent, SLOT(_k_moduleChanged(bool))); |
110 | QObject::connect(kcm, SIGNAL(destroyed()), parent, SLOT(_k_moduleDestroyed())); |
111 | QObject::connect( kcm, SIGNAL(quickHelpChanged()), parent, SIGNAL(quickHelpChanged()) ); |
112 | parent->setWhatsThis( kcm->quickHelp() ); |
113 | |
114 | if ( kcm->layout() ) { |
115 | kcm->layout()->setMargin( 0 ); |
116 | } |
117 | topLayout->addWidget( kcm ); |
118 | if( !modInfo.handle().isEmpty() ) |
119 | QDBusConnection::sessionBus().registerObject(dbusPath, new KSettingsWidgetAdaptor(parent), QDBusConnection::ExportAllSlots); |
120 | |
121 | if ( !rootInfo && /* If it's not already done */ |
122 | kcm->useRootOnlyMessage() && /* kcm wants root message */ |
123 | !KUser().isSuperUser() ) /* Not necessary if we're root */ |
124 | { |
125 | /*rootInfo = new QLabel( parent ); |
126 | topLayout->insertWidget( 0, rootInfo ); |
127 | |
128 | QPalette palette = rootInfo->palette(); |
129 | KStatefulBrush stbrush(KColorScheme::Window, KColorScheme::NeutralBackground); |
130 | qDebug() << stbrush.brush(rootInfo); |
131 | palette.setBrush(QPalette::Window, stbrush.brush(rootInfo)); |
132 | rootInfo->setPalette(palette); |
133 | rootInfo->setAutoFillBackground(true); |
134 | |
135 | const QString message = kcm->rootOnlyMessage(); |
136 | if( message.isEmpty() ) |
137 | rootInfo->setText( i18n( |
138 | "<b>Changes in this section require root access.</b><br />" |
139 | "On applying your changes you will have to supply your root " |
140 | "password." ) ); |
141 | else |
142 | rootInfo->setText(message); |
143 | |
144 | rootInfo->setWhatsThis( i18n( |
145 | "This section requires special permissions, probably " |
146 | "for system-wide changes; therefore, it is " |
147 | "required that you provide the root password to be " |
148 | "able to change the module's properties. If " |
149 | "you cannot provide the password, the changes of the " |
150 | "module cannot be saved " ) );*/ |
151 | } |
152 | } |
153 | else |
154 | { |
155 | kDebug(711) << "Module already loaded, loading KCMError" ; |
156 | |
157 | /* Figure out the name of where the module is already loaded */ |
158 | QDBusInterface proxy( dbusService, dbusPath, "org.kde.internal.KSettingsWidget" ); |
159 | QDBusReply<QString> reply = proxy.call("applicationName" ); |
160 | |
161 | if( reply.isValid() ) |
162 | { |
163 | QObject::connect( QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)), |
164 | parent, SLOT(_k_ownerChanged(QString,QString,QString))); |
165 | kcm = KCModuleLoader::reportError( KCModuleLoader::Inline, |
166 | i18nc( "Argument is application name" , "This configuration section is " |
167 | "already opened in %1" , reply.value() ), " " , parent ); |
168 | topLayout->addWidget( kcm ); |
169 | } |
170 | else |
171 | { |
172 | kDebug(711) << "Calling KCModuleProxy's DBus interface for fetching the name failed." ; |
173 | bogusOccupier = true; |
174 | loadModule(); |
175 | } |
176 | } |
177 | } |
178 | |
179 | void KCModuleProxyPrivate::_k_ownerChanged(const QString &service, const QString &oldOwner, const QString &) |
180 | { |
181 | if (service == dbusService && !oldOwner.isEmpty()) { |
182 | // Violence: Get rid of KCMError & CO, so that |
183 | // realModule() attempts to reload the module |
184 | delete kcm; |
185 | kcm = 0; |
186 | Q_Q(KCModuleProxy); |
187 | q->realModule(); |
188 | |
189 | Q_ASSERT(kcm); |
190 | kcm->show(); |
191 | } |
192 | } |
193 | |
194 | void KCModuleProxy::showEvent( QShowEvent * ev ) |
195 | { |
196 | Q_D(KCModuleProxy); |
197 | |
198 | ( void )realModule(); |
199 | |
200 | /* We have no kcm, if we're in root mode */ |
201 | if( d->kcm ) { |
202 | d->kcm->showEvent(ev); |
203 | } |
204 | |
205 | QWidget::showEvent( ev ); |
206 | |
207 | } |
208 | |
209 | KCModuleProxy::~KCModuleProxy() |
210 | { |
211 | deleteClient(); |
212 | KCModuleLoader::unloadModule(moduleInfo()); |
213 | |
214 | delete d_ptr; |
215 | } |
216 | |
217 | void KCModuleProxy::deleteClient() |
218 | { |
219 | Q_D(KCModuleProxy); |
220 | delete d->kcm; |
221 | d->kcm = 0; |
222 | |
223 | qApp->syncX(); |
224 | } |
225 | |
226 | void KCModuleProxyPrivate::_k_moduleChanged(bool c) |
227 | { |
228 | if(changed == c) { |
229 | return; |
230 | } |
231 | |
232 | Q_Q(KCModuleProxy); |
233 | changed = c; |
234 | emit q->changed(c); |
235 | emit q->changed(q); |
236 | } |
237 | |
238 | void KCModuleProxyPrivate::_k_moduleDestroyed() |
239 | { |
240 | kcm = 0; |
241 | } |
242 | |
243 | KCModuleProxy::KCModuleProxy( const KService::Ptr& service, QWidget * parent, |
244 | const QStringList& args ) |
245 | : QWidget(parent), d_ptr(new KCModuleProxyPrivate(this, KCModuleInfo(service), args)) |
246 | { |
247 | d_ptr->q_ptr = this; |
248 | } |
249 | |
250 | KCModuleProxy::KCModuleProxy( const KCModuleInfo& info, QWidget * parent, |
251 | const QStringList& args ) |
252 | : QWidget(parent), d_ptr(new KCModuleProxyPrivate(this, info, args)) |
253 | { |
254 | d_ptr->q_ptr = this; |
255 | } |
256 | |
257 | KCModuleProxy::KCModuleProxy( const QString& serviceName, QWidget * parent, |
258 | const QStringList& args ) |
259 | : QWidget(parent), d_ptr(new KCModuleProxyPrivate(this, KCModuleInfo(serviceName), args)) |
260 | { |
261 | d_ptr->q_ptr = this; |
262 | } |
263 | |
264 | |
265 | void KCModuleProxy::load() |
266 | { |
267 | Q_D(KCModuleProxy); |
268 | if( realModule() ) |
269 | { |
270 | d->kcm->load(); |
271 | d->_k_moduleChanged(false); |
272 | } |
273 | } |
274 | |
275 | void KCModuleProxy::save() |
276 | { |
277 | Q_D(KCModuleProxy); |
278 | if( d->changed && realModule() ) |
279 | { |
280 | d->kcm->save(); |
281 | d->_k_moduleChanged(false); |
282 | } |
283 | } |
284 | |
285 | void KCModuleProxy::defaults() |
286 | { |
287 | Q_D(KCModuleProxy); |
288 | if( realModule() ) |
289 | d->kcm->defaults(); |
290 | } |
291 | |
292 | QString KCModuleProxy::quickHelp() const |
293 | { |
294 | return realModule() ? realModule()->quickHelp() : QString(); |
295 | } |
296 | |
297 | const KAboutData * KCModuleProxy::aboutData() const |
298 | { |
299 | return realModule() ? realModule()->aboutData() : 0; |
300 | } |
301 | |
302 | KCModule::Buttons KCModuleProxy::buttons() const |
303 | { |
304 | if( realModule() ) |
305 | return realModule()->buttons(); |
306 | return KCModule::Buttons( KCModule::Help | KCModule::Default | KCModule::Apply ); |
307 | } |
308 | |
309 | QString KCModuleProxy::rootOnlyMessage() const |
310 | { |
311 | return realModule() ? realModule()->rootOnlyMessage() : QString(); |
312 | } |
313 | |
314 | bool KCModuleProxy::useRootOnlyMessage() const |
315 | { |
316 | return realModule() ? realModule()->useRootOnlyMessage() : true; |
317 | } |
318 | |
319 | KComponentData KCModuleProxy::componentData() const |
320 | { |
321 | return realModule() ? realModule()->componentData() : KComponentData(); |
322 | } |
323 | |
324 | bool KCModuleProxy::changed() const |
325 | { |
326 | Q_D(const KCModuleProxy); |
327 | return d->changed; |
328 | } |
329 | |
330 | KCModuleInfo KCModuleProxy::moduleInfo() const |
331 | { |
332 | Q_D(const KCModuleProxy); |
333 | return d->modInfo; |
334 | } |
335 | |
336 | QString KCModuleProxy::dbusService() const |
337 | { |
338 | Q_D(const KCModuleProxy); |
339 | return d->dbusService; |
340 | } |
341 | |
342 | QString KCModuleProxy::dbusPath() const |
343 | { |
344 | Q_D(const KCModuleProxy); |
345 | return d->dbusPath; |
346 | } |
347 | |
348 | QSize KCModuleProxy::minimumSizeHint() const |
349 | { |
350 | return QWidget::minimumSizeHint(); |
351 | } |
352 | |
353 | //X void KCModuleProxy::emitQuickHelpChanged() |
354 | //X { |
355 | //X emit quickHelpChanged(); |
356 | //X } |
357 | |
358 | /***************************************************************/ |
359 | #include "kcmoduleproxy.moc" |
360 | |
361 | // vim: ts=4 |
362 | |