TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kcmoduleproxy.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. 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 version 2 as published by the Free Software Foundation.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include <tqapplication.h>
  17. #include <tqcursor.h>
  18. #include <tqdatastream.h>
  19. #include <tqevent.h>
  20. #include <tqfileinfo.h>
  21. #include <tqframe.h>
  22. #include <tqlabel.h>
  23. #include <tqlayout.h>
  24. #include <tqpoint.h>
  25. #include <tqscrollview.h>
  26. #include <tqtextstream.h>
  27. #include <tqvbox.h>
  28. #include <tqwhatsthis.h>
  29. #include <tqwidget.h>
  30. #include <dcopclient.h>
  31. #include <qxembed.h>
  32. #include <kapplication.h>
  33. #include <kaboutdata.h>
  34. #include <kcmodule.h>
  35. #include <kcmoduleinfo.h>
  36. #include <kcmoduleloader.h>
  37. #include <kdebug.h>
  38. #include <kdialog.h>
  39. #include <klocale.h>
  40. #include <kprocess.h>
  41. #include <kservice.h>
  42. #include <kstandarddirs.h>
  43. #include <kuser.h>
  44. #include <X11/Xlib.h>
  45. #include "kcmoduleproxy.h"
  46. #include "kcmoduleproxyIface.h"
  47. #include "kcmoduleproxyIfaceImpl.h"
  48. /***************************************************************/
  49. class KCModuleProxy::KCModuleProxyPrivate
  50. {
  51. public:
  52. KCModuleProxyPrivate( const KCModuleInfo & info )
  53. : args( 0 )
  54. , kcm( 0 )
  55. //, view( 0 )
  56. , embedWidget( 0 )
  57. , rootProcess ( 0 )
  58. , embedFrame ( 0 )
  59. , dcopObject( 0 )
  60. , dcopClient( 0 )
  61. , topLayout( 0 )
  62. , rootCommunicator( 0 )
  63. , rootInfo( 0 )
  64. , modInfo( info )
  65. , withFallback( false )
  66. , changed( false )
  67. , rootMode( false )
  68. , bogusOccupier( false )
  69. , isInitialized( false )
  70. {}
  71. ~KCModuleProxyPrivate()
  72. {
  73. delete rootInfo; // Delete before embedWidget!
  74. delete embedWidget; // Delete before embedFrame!
  75. delete embedFrame;
  76. delete dcopClient;
  77. delete dcopObject;
  78. delete rootCommunicator;
  79. delete rootProcess;
  80. delete kcm;
  81. }
  82. TQStringList args;
  83. KCModule *kcm;
  84. QXEmbed *embedWidget;
  85. KProcess *rootProcess;
  86. TQVBox *embedFrame;
  87. KCModuleProxyIfaceImpl *dcopObject;
  88. DCOPClient *dcopClient;
  89. TQVBoxLayout *topLayout; /* Contains TQScrollView view, and root stuff */
  90. KCModuleProxyRootCommunicatorImpl *rootCommunicator;
  91. TQLabel *rootInfo;
  92. TQCString dcopName;
  93. KCModuleInfo modInfo;
  94. bool withFallback;
  95. bool changed;
  96. bool rootMode;
  97. bool bogusOccupier;
  98. bool isInitialized;
  99. };
  100. /***************************************************************/
  101. /*
  102. TODO:
  103. - How KCModuleProxy behaves wrt memory leaks and behavior, when exiting
  104. from root mode is not tested, because no code make use of it. It needs
  105. work, if it should be used.
  106. - Should write a document which outlines test cases, to avoid
  107. regressions. This class is a hazard.
  108. - Two Layout problems in runAsRoot:
  109. * lblBusy doesn't show
  110. * d->kcm/d->rootInfo doesn't get it right when the user
  111. presses cancel in the kdesu dialog
  112. - Resizing horizontally is contrained; minimum size is set somewhere.
  113. It appears to be somehow derived from the module's size.
  114. - Prettify: set icon in KCMultiDialog.
  115. - Perhaps it's possible to link against kdesu such that
  116. the dialog is in process?
  117. */
  118. /***************************************************************/
  119. KCModule * KCModuleProxy::realModule() const
  120. {
  121. /*
  122. * Note, don't call any function that calls realModule() since
  123. * that leads to an infinite loop.
  124. */
  125. kdDebug(711) << k_funcinfo << endl;
  126. /* Already loaded */
  127. if( d->kcm )
  128. return d->kcm;
  129. /* /We/ have no kcm, but kcmshell running with root prevs does.. */
  130. if( d->rootMode )
  131. return 0;
  132. TQApplication::setOverrideCursor( Qt::WaitCursor );
  133. KCModuleProxy * that = const_cast<KCModuleProxy*>( this );
  134. if( !d->isInitialized )
  135. {
  136. d->dcopName = TQString(moduleInfo().handle().prepend("KCModuleProxy-")).utf8();
  137. d->topLayout = new TQVBoxLayout( that, 0, 0, "topLayout" );
  138. d->isInitialized = true;
  139. }
  140. if( !d->dcopClient )
  141. d->dcopClient = new DCOPClient();
  142. if( !d->dcopClient->isRegistered() )
  143. d->dcopClient->registerAs( d->dcopName, false );
  144. d->dcopClient->setAcceptCalls( true );
  145. if( d->dcopClient->appId() == d->dcopName || d->bogusOccupier )
  146. { /* We got the name we requested, because no one was before us,
  147. * or, it was an random application which had picked that name */
  148. kdDebug(711) << "Module not already loaded, loading module" << endl;
  149. d->dcopObject = new KCModuleProxyIfaceImpl( d->dcopName, that );
  150. d->kcm = KCModuleLoader::loadModule( moduleInfo(), KCModuleLoader::Inline, d->withFallback,
  151. that, name(), d->args );
  152. connect( d->kcm, TQT_SIGNAL( changed( bool ) ),
  153. TQT_SLOT(moduleChanged(bool)) );
  154. connect( d->kcm, TQT_SIGNAL( destroyed() ),
  155. TQT_SLOT( moduleDestroyed() ) );
  156. connect( d->kcm, TQT_SIGNAL(quickHelpChanged()),
  157. TQT_SIGNAL(quickHelpChanged()));
  158. TQWhatsThis::add( that, d->kcm->quickHelp() );
  159. d->topLayout->addWidget( d->kcm );
  160. if ( !d->rootInfo && /* If it's already done */
  161. moduleInfo().needsRootPrivileges() /* root, anyone? */ &&
  162. !KUser().isSuperUser() ) /* Not necessary if we're root */
  163. {
  164. d->rootInfo = new TQLabel( that, "rootInfo" );
  165. d->topLayout->insertWidget( 0, d->rootInfo );
  166. d->rootInfo->setFrameShape(TQFrame::Box);
  167. d->rootInfo->setFrameShadow(TQFrame::Raised);
  168. const TQString msg = d->kcm->rootOnlyMsg();
  169. if( msg.isEmpty() )
  170. d->rootInfo->setText(i18n(
  171. "<b>Changes in this section requires root access.</b><br />"
  172. "Click the \"Administrator Mode\" button to "
  173. "allow modifications."));
  174. else
  175. d->rootInfo->setText(msg);
  176. TQWhatsThis::add( d->rootInfo, i18n(
  177. "This section requires special permissions, probably "
  178. "for system-wide changes; therefore, it is "
  179. "required that you provide the root password to be "
  180. "able to change the module's properties. If "
  181. "you do not provide the password, the module will be "
  182. "disabled."));
  183. }
  184. }
  185. else
  186. {
  187. kdDebug(711) << "Module already loaded, loading KCMError" << endl;
  188. d->dcopClient->detach();
  189. /* Re-register as anonymous */
  190. d->dcopClient->attach();
  191. d->dcopClient->setNotifications( true );
  192. connect( d->dcopClient, TQT_SIGNAL( applicationRemoved( const TQCString& )),
  193. TQT_SLOT( applicationRemoved( const TQCString& )));
  194. /* Figure out the name of where the module is already loaded */
  195. TQByteArray replyData, data;
  196. TQCString replyType;
  197. TQString result;
  198. TQDataStream arg, stream( replyData, IO_ReadOnly );
  199. if( d->dcopClient->call( d->dcopName, d->dcopName, "applicationName()",
  200. data, replyType, replyData ))
  201. {
  202. stream >> result;
  203. d->kcm = KCModuleLoader::reportError( KCModuleLoader::Inline,
  204. i18n( "Argument is application name", "This configuration section is "
  205. "already opened in %1" ).arg( result ), " ", that );
  206. d->topLayout->addWidget( d->kcm );
  207. }
  208. else
  209. {
  210. kdDebug(711) << "Calling KCModuleProxy's DCOP interface for fetching the name failed." << endl;
  211. d->bogusOccupier = true;
  212. TQApplication::restoreOverrideCursor();
  213. return realModule();
  214. }
  215. }
  216. TQApplication::restoreOverrideCursor();
  217. return d->kcm;
  218. }
  219. void KCModuleProxy::applicationRemoved( const TQCString& app )
  220. {
  221. if( app == d->dcopName )
  222. {
  223. /* Violence: Get rid of KCMError & CO, so that
  224. * realModule() attempts to reload the module */
  225. delete d->kcm;
  226. d->kcm = 0;
  227. d->dcopClient->setNotifications( false );
  228. realModule();
  229. d->kcm->show();
  230. }
  231. }
  232. void KCModuleProxy::showEvent( TQShowEvent * ev )
  233. {
  234. kdDebug(711) << k_funcinfo << endl;
  235. ( void )realModule();
  236. /* We have no kcm, if we're in root mode */
  237. if( d->kcm )
  238. d->kcm->show();
  239. TQWidget::showEvent( ev );
  240. }
  241. void KCModuleProxy::runAsRoot()
  242. {
  243. if ( !moduleInfo().needsRootPrivileges() )
  244. return;
  245. TQApplication::setOverrideCursor( Qt::WaitCursor );
  246. delete d->rootProcess;
  247. delete d->embedWidget;
  248. delete d->embedFrame;
  249. d->embedFrame = new TQVBox( this, "embedFrame" );
  250. d->embedFrame->setFrameStyle( TQFrame::Box | TQFrame::Raised );
  251. TQPalette pal( red );
  252. pal.setColor( TQColorGroup::Background,
  253. tqcolorGroup().background() );
  254. d->embedFrame->setPalette( pal );
  255. d->embedFrame->setLineWidth( 2 );
  256. d->embedFrame->setMidLineWidth( 2 );
  257. d->topLayout->addWidget(d->embedFrame,1);
  258. d->embedWidget = new QXEmbed( d->embedFrame, "embedWidget" );
  259. d->embedFrame->show();
  260. TQLabel *lblBusy = new TQLabel(i18n("<big>Loading...</big>"), d->embedWidget, "lblBusy" );
  261. lblBusy->setTextFormat(RichText);
  262. lblBusy->tqsetAlignment(AlignCenter);
  263. lblBusy->setGeometry(0,0, d->kcm->width(), d->kcm->height());
  264. lblBusy->show();
  265. deleteClient();
  266. /* The DCOP registration is now gone, and it will occur again when kcmshell soon
  267. * registers. Here's a race condition in other words, but how likely is that?
  268. *
  269. * - It's a user initiated action, which means the user have to do weird stuff, very
  270. * quick.
  271. * - If the user _do_ manage to fsck up, the code will recover gracefully, see realModule().
  272. *
  273. * So no worry. At the end of this function, communication with
  274. * the DCOP object is established.
  275. */
  276. /* Prepare the process to run the kcmshell */
  277. TQString cmd = moduleInfo().service()->exec().stripWhiteSpace();
  278. if (cmd.left(5) == "kdesu")
  279. {
  280. cmd = TQString(cmd.remove(0,5)).stripWhiteSpace();
  281. /* Remove all kdesu switches */
  282. while( cmd.length() > 1 && cmd[ 0 ] == '-' )
  283. cmd = TQString(cmd.remove( 0, cmd.find( ' ' ) )).stripWhiteSpace();
  284. }
  285. if (cmd.left(8) == "kcmshell")
  286. cmd = TQString(cmd.remove(0,8)).stripWhiteSpace();
  287. /* Run the process */
  288. TQString kdesu = KStandardDirs::findExe("kdesu");
  289. if (!kdesu.isEmpty())
  290. {
  291. d->rootProcess = new KProcess;
  292. *d->rootProcess << kdesu;
  293. *d->rootProcess << "--nonewdcop" << "-n" << "-d" << TQString( "-i%1" ).arg(moduleInfo().icon());
  294. *d->rootProcess << TQString("%1 %2 --embed-proxy %3 --lang %4").arg(locate("exe", "kcmshell"))
  295. .arg(cmd).arg(d->embedWidget->winId()).arg(KGlobal::locale()->language());
  296. connect(d->rootProcess, TQT_SIGNAL(processExited(KProcess*)), TQT_SLOT(rootExited()));
  297. if ( !d->rootProcess->start( KProcess::NotifyOnExit ))
  298. {
  299. d->rootMode = false;
  300. rootExited();
  301. }
  302. else
  303. {
  304. d->rootMode = true;
  305. kapp->dcopClient();
  306. d->rootCommunicator = new KCModuleProxyRootCommunicatorImpl( d->dcopName + "-RootCommunicator", this );
  307. }
  308. delete lblBusy;
  309. TQApplication::restoreOverrideCursor();
  310. return;
  311. }
  312. /* Clean up in case of failure */
  313. delete d->embedWidget;
  314. d->embedWidget = 0;
  315. delete d->embedFrame;
  316. d->embedFrame = 0;
  317. TQApplication::restoreOverrideCursor();
  318. }
  319. void KCModuleProxy::rootExited()
  320. {
  321. kdDebug(711) << k_funcinfo << endl;
  322. if ( d->embedWidget->embeddedWinId() )
  323. XDestroyWindow(qt_xdisplay(), d->embedWidget->embeddedWinId());
  324. delete d->embedWidget;
  325. d->embedWidget = 0;
  326. delete d->rootProcess;
  327. d->rootProcess = 0;
  328. delete d->embedFrame;
  329. d->embedFrame=0;
  330. delete d->rootCommunicator;
  331. d->rootCommunicator = 0;
  332. /* Such that the "ordinary" module loads again */
  333. d->rootMode = false;
  334. d->topLayout->tqinvalidate();
  335. TQShowEvent ev;
  336. showEvent( &ev );
  337. moduleChanged( false );
  338. emit childClosed();
  339. }
  340. KCModuleProxy::~KCModuleProxy()
  341. {
  342. deleteClient();
  343. KCModuleLoader::unloadModule(moduleInfo());
  344. delete d;
  345. }
  346. void KCModuleProxy::deleteClient()
  347. {
  348. if( d->embedWidget )
  349. XKillClient(qt_xdisplay(), d->embedWidget->embeddedWinId());
  350. delete d->kcm;
  351. d->kcm = 0;
  352. delete d->dcopObject;
  353. d->dcopObject = 0;
  354. if( d->dcopClient && !d->dcopClient->detach() )
  355. kdDebug(711) << "Unregistering from DCOP failed." << endl;
  356. delete d->dcopClient;
  357. d->dcopClient = 0;
  358. kapp->syncX();
  359. }
  360. void KCModuleProxy::moduleChanged( bool c )
  361. {
  362. if( d->changed == c )
  363. return;
  364. d->changed = c;
  365. emit changed( c );
  366. emit changed( this );
  367. }
  368. void KCModuleProxy::moduleDestroyed()
  369. {
  370. d->kcm = 0;
  371. }
  372. KCModuleProxy::KCModuleProxy( const KService::Ptr & service, bool withFallback,
  373. TQWidget * parent, const char * name, const TQStringList & args)
  374. : TQWidget( parent, name )
  375. {
  376. init( KCModuleInfo( service ));
  377. d->args = args;
  378. d->withFallback = withFallback;
  379. }
  380. KCModuleProxy::KCModuleProxy( const KCModuleInfo & info, bool withFallback,
  381. TQWidget * parent, const char * name, const TQStringList & args )
  382. : TQWidget( parent, name )
  383. {
  384. init( info );
  385. d->args = args;
  386. d->withFallback = withFallback;
  387. }
  388. KCModuleProxy::KCModuleProxy( const TQString& serviceName, bool withFallback,
  389. TQWidget * parent, const char * name,
  390. const TQStringList & args)
  391. : TQWidget( parent, name )
  392. {
  393. init( KCModuleInfo( serviceName ));
  394. d->args = args;
  395. d->withFallback = withFallback;
  396. }
  397. void KCModuleProxy::init( const KCModuleInfo& info )
  398. {
  399. kdDebug(711) << k_funcinfo << endl;
  400. d = new KCModuleProxyPrivate( info );
  401. /* This is all we do for now; all the heavy work is
  402. * done in realModule(). It's called when the module
  403. * _actually_ is needed, in for example showEvent().
  404. * The module is loaded "on demand" -- lazy loading.
  405. */
  406. }
  407. void KCModuleProxy::load()
  408. {
  409. if( d->rootMode )
  410. callRootModule( "load()" );
  411. else if( realModule() )
  412. {
  413. d->kcm->load();
  414. moduleChanged( false );
  415. }
  416. }
  417. void KCModuleProxy::save()
  418. {
  419. if( d->rootMode )
  420. callRootModule( "save()" );
  421. else if( d->changed && realModule() )
  422. {
  423. d->kcm->save();
  424. moduleChanged( false );
  425. }
  426. }
  427. void KCModuleProxy::callRootModule( const TQCString& function )
  428. {
  429. TQByteArray sendData, replyData;
  430. TQCString replyType;
  431. /* Note, we don't use d->dcopClient here, because it's used for
  432. * the loaded module(and it's not "us" when this function is called) */
  433. if( !kapp->dcopClient()->call( d->dcopName, d->dcopName, function, sendData,
  434. replyType, replyData, true, -1 ))
  435. kdDebug(711) << "Calling function '" << function << "' failed." << endl;
  436. }
  437. void KCModuleProxy::defaults()
  438. {
  439. if( d->rootMode )
  440. callRootModule( "defaults()" );
  441. if( realModule() )
  442. d->kcm->defaults();
  443. }
  444. TQString KCModuleProxy::quickHelp() const
  445. {
  446. if( !d->rootMode )
  447. return realModule() ? realModule()->quickHelp() : TQString::null;
  448. else
  449. {
  450. TQByteArray data, replyData;
  451. TQCString replyType;
  452. if (kapp->dcopClient()->call(d->dcopName, d->dcopName, "quickHelp()",
  453. data, replyType, replyData))
  454. kdDebug(711) << "Calling DCOP function bool changed() failed." << endl;
  455. else
  456. {
  457. TQDataStream reply(replyData, IO_ReadOnly);
  458. if (replyType == "TQString")
  459. {
  460. TQString result;
  461. reply >> result;
  462. return result;
  463. }
  464. else
  465. kdDebug(711) << "DCOP function changed() returned mumbo jumbo." << endl;
  466. }
  467. return TQString::null;
  468. }
  469. }
  470. const KAboutData * KCModuleProxy::aboutData() const
  471. {
  472. if( !d->rootMode )
  473. return realModule() ? realModule()->aboutData() : 0;
  474. else
  475. /* This needs fixing, perhaps cache a KAboutData copy
  476. * while in root mode? */
  477. return 0;
  478. }
  479. int KCModuleProxy::buttons() const
  480. {
  481. return realModule() ? realModule()->buttons() :
  482. KCModule::Help | KCModule::Default | KCModule::Apply ;
  483. }
  484. TQString KCModuleProxy::rootOnlyMsg() const
  485. {
  486. return realModule() ? realModule()->rootOnlyMsg() : TQString::null;
  487. }
  488. bool KCModuleProxy::useRootOnlyMsg() const
  489. {
  490. return realModule() ? realModule()->useRootOnlyMsg() : true;
  491. }
  492. KInstance * KCModuleProxy::instance() const
  493. {
  494. return realModule() ? realModule()->instance() : 0;
  495. }
  496. bool KCModuleProxy::changed() const
  497. {
  498. return d->changed;
  499. }
  500. const KCModuleInfo& KCModuleProxy::moduleInfo() const
  501. {
  502. return d->modInfo;
  503. }
  504. bool KCModuleProxy::rootMode() const
  505. {
  506. return d->rootMode;
  507. }
  508. TQCString KCModuleProxy::dcopName() const
  509. {
  510. return d->dcopName;
  511. }
  512. void KCModuleProxy::emitQuickHelpChanged()
  513. {
  514. emit quickHelpChanged();
  515. }
  516. /***************************************************************/
  517. #include "kcmoduleproxy.moc"
  518. // vim: sw=4 ts=4 noet