23#include "kdedmodule.h"
25#include <kresourcelist.h>
36#include <dcopclient.h>
38#include <tdeuniqueapplication.h>
39#include <tdecmdlineargs.h>
40#include <tdeaboutdata.h>
43#include <tdeprocess.h>
46#include <tdestandarddirs.h>
47#include <kdatastream.h>
48#include <tdeio/global.h>
49#include <kservicetype.h>
56Kded *Kded::_self =
nullptr;
58static bool checkStamps =
true;
59static bool delayedCheck =
false;
61static void runBuildSycoca(TQObject *callBackObj=
nullptr,
const char *callBackSlot=
nullptr)
64 args.append(
"--incremental");
66 args.append(
"--checkstamps");
68 args.append(
"--nocheckfiles");
74 TQDataStream dataStream( data, IO_WriteOnly );
75 dataStream << TQString(
"tdebuildsycoca") << args;
76 TQCString _launcher = TDEApplication::launcher();
78 tdeApp->dcopClient()->callAsync(_launcher, _launcher,
"tdeinit_exec_wait(TQString,TQStringList)", data, callBackObj, callBackSlot);
82 TDEApplication::tdeinitExecWait(
"tdebuildsycoca", args );
86static void runKonfUpdate()
88 TDEApplication::tdeinitExecWait(
"tdeconf_update", TQStringList(),
nullptr,
nullptr,
"0" );
91static void runDontChangeHostname(
const TQCString &oldName,
const TQCString &newName)
94 args.append(TQFile::decodeName(oldName));
95 args.append(TQFile::decodeName(newName));
96 TDEApplication::tdeinitExecWait(
"kdontchangethehostname", args );
99Kded::Kded(
bool checkUpdates,
bool new_startup)
100 : DCOPObject(
"tdebuildsycoca"), DCOPObjectProxy(),
101 b_checkUpdates(checkUpdates),
102 m_needDelayedCheck(false),
103 m_newStartup( new_startup )
107 TQCString tdesycoca_env = getenv(
"TDESYCOCA");
108 if (tdesycoca_env.isEmpty())
109 cPath = TQFile::encodeName(TDEGlobal::dirs()->saveLocation(
"tmp")+
"tdesycoca");
111 cPath = tdesycoca_env;
112 m_pTimer =
new TQTimer(
this);
113 connect(m_pTimer, TQ_SIGNAL(timeout()),
this, TQ_SLOT(recreate()));
115 TQTimer::singleShot(100,
this, TQ_SLOT(installCrashHandler()));
117 m_pDirWatch =
nullptr;
119 m_windowIdList.setAutoDelete(
true);
122 m_recreateBusy =
false;
133 TQAsciiDictIterator<KDEDModule> it(m_modules);
134 while (!it.isEmpty())
138bool Kded::process(
const TQCString &obj,
const TQCString &fun,
139 const TQByteArray &data,
140 TQCString &replyType, TQByteArray &replyData)
142 if (obj ==
"tdesycoca")
return false;
147 KDEDModule *module = loadModule(obj,
true);
151 module->setCallingDcopClient(tdeApp->dcopClient());
152 return module->process(fun, data, replyType, replyData);
155void Kded::initModules()
158 TDEConfig *config = tdeApp->config();
159 const char *fullSession = getenv(
"TDE_FULL_SESSION" );
160 bool tde_running = fullSession && fullSession[ 0 ] !=
'\0';
162 if( getenv(
"TDE_SESSION_UID" ) && uid_t( atoi( getenv(
"TDE_SESSION_UID" ))) != getuid())
165 KService::List kdedModules = KServiceType::offers(
"KDEDModule");
166 TQString version = getenv(
"KDE_SESSION_VERSION" );
167 TQStringList blacklist;
168 if ( !version.isNull() && version >=
"4" )
170 kdDebug(7020) <<
"KDE4 is running:" << endl;
171 kdDebug(7020) <<
" KDE_SESSION_VERSION: " << version << endl;
172 kdDebug(7020) <<
" Blacklisting mediamanager, medianotifier, kmilod, kwrited." << endl;
173 blacklist <<
"mediamanager" <<
"medianotifier" <<
"kmilod" <<
"kwrited";
175 for(KService::List::ConstIterator it = kdedModules.begin(); it != kdedModules.end(); ++it)
177 KService::Ptr service = *it;
178 bool autoload = service->property(
"X-TDE-Kded-autoload", TQVariant::Bool).toBool();
179 config->setGroup(TQString(
"Module-%1").arg(service->desktopEntryName()));
180 autoload = config->readBoolEntry(
"autoload", autoload);
181 for (TQStringList::Iterator module = blacklist.begin(); module != blacklist.end(); ++module)
183 if (service->desktopEntryName() == *module)
192 TQVariant phasev = service->property(
"X-TDE-Kded-phase", TQVariant::Int );
193 int phase = phasev.isValid() ? phasev.toInt() : 2;
194 bool prevent_autoload =
false;
201 prevent_autoload =
true;
205 prevent_autoload =
true;
208 if (autoload && !prevent_autoload)
209 loadModule(service,
false);
213 if (autoload && tde_running)
214 loadModule(service,
false);
216 bool dontLoad =
false;
217 TQVariant p = service->property(
"X-TDE-Kded-load-on-demand", TQVariant::Bool);
218 if (p.isValid() && (p.toBool() ==
false))
221 noDemandLoad(service->desktopEntryName());
223 if (dontLoad && !autoload)
224 unloadModule(service->desktopEntryName().latin1());
228void Kded::loadSecondPhase()
230 kdDebug(7020) <<
"Loading second phase autoload" << endl;
231 TDEConfig *config = tdeApp->config();
232 KService::List kdedModules = KServiceType::offers(
"KDEDModule");
233 for(KService::List::ConstIterator it = kdedModules.begin(); it != kdedModules.end(); ++it)
235 KService::Ptr service = *it;
236 bool autoload = service->property(
"X-TDE-Kded-autoload", TQVariant::Bool).toBool();
237 config->setGroup(TQString(
"Module-%1").arg(service->desktopEntryName()));
238 autoload = config->readBoolEntry(
"autoload", autoload);
239 TQVariant phasev = service->property(
"X-TDE-Kded-phase", TQVariant::Int );
240 int phase = phasev.isValid() ? phasev.toInt() : 2;
241 if( phase == 2 && autoload )
242 loadModule(service,
false);
246void Kded::noDemandLoad(
const TQString &obj)
248 m_dontLoad.insert(obj.latin1(),
this);
251KDEDModule *Kded::loadModule(
const TQCString &obj,
bool onDemand)
253 KDEDModule *module = m_modules.
find(obj);
256 KService::Ptr s = KService::serviceByDesktopPath(
"kded/"+obj+
".desktop");
257 return loadModule(s, onDemand);
260KDEDModule *Kded::loadModule(
const KService *s,
bool onDemand)
262 KDEDModule *module =
nullptr;
263 if (s && !s->library().isEmpty())
265 TQCString obj = s->desktopEntryName().latin1();
266 KDEDModule *oldModule = m_modules.find(obj);
272 TQVariant p = s->property(
"X-TDE-Kded-load-on-demand", TQVariant::Bool);
273 if (p.isValid() && (p.toBool() ==
false))
275 noDemandLoad(s->desktopEntryName());
281 KLibLoader *loader = KLibLoader::self();
283 TQVariant v = s->property(
"X-TDE-FactoryName", TQVariant::String);
284 TQString factory = v.isValid() ? v.toString() : TQString::null;
285 if (factory.isEmpty())
288 v = s->property(
"X-TDE-Factory", TQVariant::String);
289 factory = v.isValid() ? v.toString() : TQString::null;
291 if (factory.isEmpty())
292 factory = s->library();
294 factory =
"create_" + factory;
295 TQString libname =
"kded_"+s->library();
297 KLibrary *lib = loader->library(TQFile::encodeName(libname));
300 kdWarning() << k_funcinfo <<
"Could not load library. [ "
301 << loader->lastErrorMessage() <<
" ]" << endl;
302 libname.prepend(
"lib");
303 lib = loader->library(TQFile::encodeName(libname));
308 void *create = lib->symbol(TQFile::encodeName(factory));
313 KDEDModule* (*func)(
const TQCString &);
314 func = (KDEDModule* (*)(
const TQCString &)) create;
318 m_modules.insert(obj, module);
319 m_libs.insert(obj, lib);
320 connect(module, TQ_SIGNAL(moduleDeleted(KDEDModule *)), TQ_SLOT(slotKDEDModuleRemoved(KDEDModule *)));
321 kdDebug(7020) <<
"Successfully loaded module '" << obj <<
"'\n";
325 loader->unloadLibrary(TQFile::encodeName(libname));
329 kdWarning() << k_funcinfo <<
"Could not load library. [ "
330 << loader->lastErrorMessage() <<
" ]" << endl;
332 kdDebug(7020) <<
"Could not load module '" << obj <<
"'\n";
337bool Kded::unloadModule(
const TQCString &obj)
339 KDEDModule *module = m_modules.take(obj);
342 kdDebug(7020) <<
"Unloading module '" << obj <<
"'\n";
348QCStringList Kded::loadedModules()
350 QCStringList modules;
351 TQAsciiDictIterator<KDEDModule> it( m_modules );
352 for ( ; it.current(); ++it)
353 modules.append( it.currentKey() );
358QCStringList Kded::functions()
360 QCStringList res = DCOPObject::functions();
361 res +=
"ASYNC recreate()";
365void Kded::slotKDEDModuleRemoved(
KDEDModule *module)
367 m_modules.remove(module->objId());
368 KLibrary *lib = m_libs.take(module->objId());
373void Kded::slotApplicationRemoved(
const TQCString &appId)
375 for(TQAsciiDictIterator<KDEDModule> it(m_modules); it.current(); ++it)
377 it.current()->removeAll(appId);
380 TQValueList<long> *windowIds = m_windowIdList.find(appId);
383 for( TQValueList<long>::ConstIterator it = windowIds->begin();
384 it != windowIds->end(); ++it)
387 m_globalWindowIdList.remove(windowId);
388 for(TQAsciiDictIterator<KDEDModule> it(m_modules); it.current(); ++it)
390 emit it.current()->windowUnregistered(windowId);
393 m_windowIdList.remove(appId);
397void Kded::updateDirWatch()
399 if (!b_checkUpdates)
return;
402 m_pDirWatch =
new KDirWatch;
404 TQObject::connect( m_pDirWatch, TQ_SIGNAL(dirty(
const TQString&)),
405 this, TQ_SLOT(update(
const TQString&)));
406 TQObject::connect( m_pDirWatch, TQ_SIGNAL(created(
const TQString&)),
407 this, TQ_SLOT(update(
const TQString&)));
408 TQObject::connect( m_pDirWatch, TQ_SIGNAL(deleted(
const TQString&)),
409 this, TQ_SLOT(dirDeleted(
const TQString&)));
412 for( TQStringList::ConstIterator it = m_allResourceDirs.begin();
413 it != m_allResourceDirs.end();
416 readDirectory( *it );
420void Kded::updateResourceList()
422 delete KSycoca::self();
424 if (!b_checkUpdates)
return;
426 if (delayedCheck)
return;
428 TQStringList dirs = KSycoca::self()->allResourceDirs();
430 for( TQStringList::ConstIterator it = dirs.begin();
434 if (m_allResourceDirs.find(*it) == m_allResourceDirs.end())
436 m_allResourceDirs.append(*it);
442void Kded::crashHandler(
int)
444 DCOPClient::emergencyClose();
446 tqWarning(
"Last DCOP call before KDED crash was from application '%s'\n"
447 "to object '%s', function '%s'.",
448 DCOPClient::postMortemSender(),
449 DCOPClient::postMortemObject(),
450 DCOPClient::postMortemFunction());
451 tqWarning(
"Restarting KDED...\n");
452 if (system(
"kded") < 0) {
453 tqWarning(
"Unable to restart KDED!\n");
458void Kded::installCrashHandler()
460 TDECrash::setEmergencySaveFunction(crashHandler);
468void Kded::runDelayedCheck()
470 if( m_needDelayedCheck )
472 m_needDelayedCheck =
false;
475void Kded::recreate(
bool initial)
477 m_recreateBusy =
true;
484 runBuildSycoca(
this, TQ_SLOT(recreateDone()));
495 TQTimer::singleShot( 60000,
this, TQ_SLOT( runDelayedCheck()));
496 m_needDelayedCheck =
true;
497 delayedCheck =
false;
500 m_needDelayedCheck =
false;
504void Kded::recreateDone()
506 updateResourceList();
508 for(; m_recreateCount; m_recreateCount--)
510 TQCString replyType =
"void";
511 TQByteArray replyData;
512 DCOPClientTransaction *transaction = m_recreateRequests.first();
514 tdeApp->dcopClient()->endTransaction(transaction, replyType, replyData);
515 m_recreateRequests.remove(m_recreateRequests.begin());
517 m_recreateBusy =
false;
520 if (!m_recreateRequests.isEmpty())
522 m_pTimer->start(2000,
true );
523 m_recreateCount = m_recreateRequests.count();
527void Kded::dirDeleted(
const TQString& path)
532void Kded::update(
const TQString& )
536 m_pTimer->start( 2000,
true );
540 m_recreateRequests.append(
nullptr);
544bool Kded::process(
const TQCString &fun,
const TQByteArray &data,
545 TQCString &replyType, TQByteArray &replyData)
547 if (fun ==
"recreate()") {
550 if (m_recreateRequests.isEmpty())
552 m_pTimer->start(0,
true );
557 m_recreateRequests.append(tdeApp->dcopClient()->beginTransaction());
561 return DCOPObject::process(fun, data, replyType, replyData);
566void Kded::readDirectory(
const TQString& _path )
568 TQString path( _path );
569 if ( path.right(1) !=
"/" )
572 if ( m_pDirWatch->contains( path ) )
575 TQDir d( _path, TQString::null, TQDir::Unsorted, TQDir::Readable | TQDir::Executable | TQDir::Dirs | TQDir::Hidden );
583 m_pDirWatch->addDir(path);
587 kdDebug(7020) << TQString(TQString(
"Does not exist! (%1)").arg(_path)) << endl;
598 unsigned int count = d.count();
599 for( i = 0; i < count; i++ )
601 if (d[i] ==
"." || d[i] ==
".." || d[i] ==
"magic")
607 readDirectory( file );
611bool Kded::isWindowRegistered(
long windowId)
613 return m_globalWindowIdList.find(windowId) !=
nullptr;
618void Kded::registerWindowId(
long windowId)
620 m_globalWindowIdList.replace(windowId, &windowId);
621 TQCString sender = callingDcopClient()->senderId();
622 if( sender.isEmpty())
623 sender = callingDcopClient()->appId();
624 TQValueList<long> *windowIds = m_windowIdList.find(sender);
627 windowIds =
new TQValueList<long>;
628 m_windowIdList.insert(sender, windowIds);
630 windowIds->append(windowId);
633 for(TQAsciiDictIterator<KDEDModule> it(m_modules); it.current(); ++it)
635 emit it.current()->windowRegistered(windowId);
640void Kded::unregisterWindowId(
long windowId)
642 m_globalWindowIdList.remove(windowId);
643 TQCString sender = callingDcopClient()->senderId();
644 if( sender.isEmpty())
645 sender = callingDcopClient()->appId();
646 TQValueList<long> *windowIds = m_windowIdList.find(sender);
649 windowIds->remove(windowId);
650 if (windowIds->isEmpty())
651 m_windowIdList.remove(sender);
654 for(TQAsciiDictIterator<KDEDModule> it(m_modules); it.current(); ++it)
656 emit it.current()->windowUnregistered(windowId);
661static void sighandler(
int )
669 m_pDirWatch =
new KDirWatch;
670 m_pTimer =
new TQTimer;
671 connect(m_pTimer, TQ_SIGNAL(timeout()),
this, TQ_SLOT(runKonfUpdate()));
672 TQObject::connect( m_pDirWatch, TQ_SIGNAL(dirty(
const TQString&)),
673 this, TQ_SLOT(slotNewUpdateFile()));
675 TQStringList dirs = TDEGlobal::dirs()->findDirs(
"data",
"tdeconf_update");
676 for( TQStringList::ConstIterator it = dirs.begin();
681 if (path[path.length()-1] !=
'/')
684 if (!m_pDirWatch->contains(path))
685 m_pDirWatch->addDir(path);
695void KUpdateD::runKonfUpdate()
700void KUpdateD::slotNewUpdateFile()
702 m_pTimer->start( 500,
true );
705KHostnameD::KHostnameD(
int pollInterval)
707 m_Timer.start(pollInterval,
false );
708 connect(&m_Timer, TQ_SIGNAL(timeout()),
this, TQ_SLOT(checkHostname()));
712KHostnameD::~KHostnameD()
717void KHostnameD::checkHostname()
720 if (gethostname(buf, 1024) != 0)
722 buf[
sizeof(buf)-1] =
'\0';
724 if (m_hostname.isEmpty())
730 if (m_hostname == buf)
733 TQCString newHostname = buf;
735 runDontChangeHostname(m_hostname, newHostname);
736 m_hostname = newHostname;
740static TDECmdLineOptions options[] =
742 {
"check", I18N_NOOP(
"Check Sycoca database only once"),
nullptr },
743 {
"new-startup",
"Internal",
nullptr },
747class KDEDQtDCOPObject :
public DCOPObject
750 KDEDQtDCOPObject() : DCOPObject(
"qt/kded") { }
752 virtual bool process(
const TQCString &fun,
const TQByteArray &data,
753 TQCString& replyType, TQByteArray &replyData)
755 if ( tdeApp && (fun ==
"quit()") )
761 return DCOPObject::process(fun, data, replyType, replyData);
764 QCStringList functions()
766 QCStringList res = DCOPObject::functions();
767 res +=
"void quit()";
772class KDEDApplication :
public TDEUniqueApplication
775 KDEDApplication() : TDEUniqueApplication( )
778 dcopClient()->connectDCOPSignal(
"DCOPServer",
"",
"terminateTDE()",
779 objId(),
"quit()",
false );
786 if( Kded::self()->newStartup())
787 Kded::self()->initModules();
789 TQTimer::singleShot(500, Kded::self(), TQ_SLOT(initModules()));
796 QCStringList functions()
798 QCStringList res = TDEUniqueApplication::functions();
799 res +=
"bool loadModule(TQCString)";
800 res +=
"bool unloadModule(TQCString)";
801 res +=
"void registerWindowId(long int)";
802 res +=
"void unregisterWindowId(long int)";
803 res +=
"QCStringList loadedModules()";
804 res +=
"void reconfigure()";
805 res +=
"void loadSecondPhase()";
806 res +=
"void quit()";
810 bool process(
const TQCString &fun,
const TQByteArray &data,
811 TQCString &replyType, TQByteArray &replyData)
813 if (fun ==
"loadModule(TQCString)") {
815 TQDataStream arg( data, IO_ReadOnly );
817 bool result = (Kded::self()->loadModule(module,
false) !=
nullptr);
819 TQDataStream _replyStream( replyData, IO_WriteOnly );
820 _replyStream << result;
823 else if (fun ==
"unloadModule(TQCString)") {
825 TQDataStream arg( data, IO_ReadOnly );
827 bool result = Kded::self()->unloadModule(module);
829 TQDataStream _replyStream( replyData, IO_WriteOnly );
830 _replyStream << result;
833 else if (fun ==
"registerWindowId(long int)") {
835 TQDataStream arg( data, IO_ReadOnly );
837 Kded::self()->setCallingDcopClient(callingDcopClient());
838 Kded::self()->registerWindowId(windowId);
842 else if (fun ==
"unregisterWindowId(long int)") {
844 TQDataStream arg( data, IO_ReadOnly );
846 Kded::self()->setCallingDcopClient(callingDcopClient());
847 Kded::self()->unregisterWindowId(windowId);
851 else if (fun ==
"loadedModules()") {
852 replyType =
"QCStringList";
853 TQDataStream _replyStream(replyData, IO_WriteOnly);
854 _replyStream << Kded::self()->loadedModules();
857 else if (fun ==
"reconfigure()") {
858 config()->reparseConfiguration();
859 Kded::self()->initModules();
863 else if (fun ==
"loadSecondPhase()") {
864 Kded::self()->loadSecondPhase();
868 else if (fun ==
"quit()") {
873 return TDEUniqueApplication::process(fun, data, replyType, replyData);
877 KDEDQtDCOPObject kdedQtDcopObject;
880extern "C" TDE_EXPORT
int kdemain(
int argc,
char *argv[])
882 TDEAboutData aboutData(
"kded", I18N_NOOP(
"TDE Daemon"),
884 I18N_NOOP(
"TDE Daemon - triggers Sycoca database updates when needed"));
886 TDEApplication::installSigpipeHandler();
888 TDECmdLineArgs::init(argc, argv, &aboutData);
890 TDEUniqueApplication::addCmdLineOptions();
892 TDECmdLineArgs::addCmdLineOptions( options );
895 TDELocale::setMainCatalogue(
"tdelibs");
898 putenv(strdup(
"SESSION_MANAGER="));
901 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
906 TQCString dcopName = testDCOP.registerAs(
"kded",
false);
907 if (dcopName.isEmpty())
909 kdFatal() <<
"DCOP communication problem!" << endl;
914 TDEInstance *instance =
new TDEInstance(&aboutData);
915 TDEConfig *config = instance->config();
917 if (args->isSet(
"check"))
919 config->setGroup(
"General");
920 checkStamps = config->readBoolEntry(
"CheckFileStamps",
true);
926 if (!TDEUniqueApplication::start())
928 fprintf(stderr,
"[kded] Daemon (kded) is already running.\n");
932 TDEUniqueApplication::dcopClient()->setQtBridgeEnabled(
false);
934 config->setGroup(
"General");
935 int HostnamePollInterval = config->readNumEntry(
"HostnamePollInterval", 5000);
936 bool bCheckSycoca = config->readBoolEntry(
"CheckSycoca",
true);
937 bool bCheckUpdates = config->readBoolEntry(
"CheckUpdates",
true);
938 bool bCheckHostname = config->readBoolEntry(
"CheckHostname",
true);
939 checkStamps = config->readBoolEntry(
"CheckFileStamps",
true);
940 delayedCheck = config->readBoolEntry(
"DelayedCheck",
false);
942 Kded *kded =
new Kded(bCheckSycoca, args->isSet(
"new-startup"));
944 signal(SIGTERM, sighandler);
945 signal(SIGHUP, sighandler);
948 kded->recreate(
true);
956 (void)
new KHostnameD(HostnamePollInterval);
958 DCOPClient *client = tdeApp->dcopClient();
959 TQObject::connect(client, TQ_SIGNAL(applicationRemoved(
const TQCString&)),
960 kded, TQ_SLOT(slotApplicationRemoved(
const TQCString&)));
961 client->setNotifications(
true);
962 client->setDaemonMode(
true );
971 client->send(
"*",
"tdesycoca",
"notifyDatabaseChanged()", data );
972 client->send(
"ksplash",
"",
"upAndRunning(TQString)", TQString(
"kded"));
975 e.xclient.type = ClientMessage;
976 e.xclient.message_type = XInternAtom( tqt_xdisplay(),
"_KDE_SPLASH_PROGRESS", False );
977 e.xclient.display = tqt_xdisplay();
978 e.xclient.window = tqt_xrootwin();
979 e.xclient.format = 8;
980 strcpy( e.xclient.data.b,
"kded" );
981 XSendEvent( tqt_xdisplay(), tqt_xrootwin(), False, SubstructureNotifyMask, &e );
983 int result = k.exec();
The base class for KDED modules.
TDEShared * find(const TQCString &app, const TQCString &key)
Lookup object indexed with app and key.