kbuildsycoca.cpp

00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 1999 David Faure <faure@kde.org>
00003  *  Copyright (C) 2002-2003 Waldo Bastian <bastian@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation;
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include <qdir.h>
00021 #include <qeventloop.h>
00022 #include <config.h>
00023 
00024 #include "kbuildsycoca.h"
00025 #include "kresourcelist.h"
00026 #include "vfolder_menu.h"
00027 
00028 #include <kservice.h>
00029 #include <kmimetype.h>
00030 #include <kbuildservicetypefactory.h>
00031 #include <kbuildservicefactory.h>
00032 #include <kbuildservicegroupfactory.h>
00033 #include <kbuildimageiofactory.h>
00034 #include <kbuildprotocolinfofactory.h>
00035 #include <kctimefactory.h>
00036 #include <kdatastream.h>
00037 
00038 #include <qdatastream.h>
00039 #include <qfile.h>
00040 #include <qtimer.h>
00041 
00042 #include <assert.h>
00043 #include <kapplication.h>
00044 #include <dcopclient.h>
00045 #include <kglobal.h>
00046 #include <kdebug.h>
00047 #include <kdirwatch.h>
00048 #include <kstandarddirs.h>
00049 #include <ksavefile.h>
00050 #include <klocale.h>
00051 #include <kaboutdata.h>
00052 #include <kcmdlineargs.h>
00053 #include <kcrash.h>
00054 
00055 #ifdef KBUILDSYCOCA_GUI // KBUILDSYCOCA_GUI is used on win32 to build 
00056                         // GUI version of kbuildsycoca, so-called "kbuildsycocaw".
00057 # include <qlabel.h>
00058 # include <kmessagebox.h>
00059   bool silent;
00060   bool showprogress;
00061 #endif
00062 
00063 #include <stdlib.h>
00064 #include <unistd.h>
00065 #include <time.h>
00066 #include <memory> // auto_ptr
00067 
00068 typedef QDict<KSycocaEntry> KBSEntryDict;
00069 typedef QValueList<KSycocaEntry::List> KSycocaEntryListList;
00070 
00071 static Q_UINT32 newTimestamp = 0;
00072 
00073 static KBuildServiceFactory *g_bsf = 0;
00074 static KBuildServiceGroupFactory *g_bsgf = 0;
00075 static KSycocaFactory *g_factory = 0;
00076 static KCTimeInfo *g_ctimeInfo = 0;
00077 static QDict<Q_UINT32> *g_ctimeDict = 0;
00078 static const char *g_resource = 0;
00079 static KBSEntryDict *g_entryDict = 0;
00080 static KBSEntryDict *g_serviceGroupEntryDict = 0;
00081 static KSycocaEntryListList *g_allEntries = 0;
00082 static QStringList *g_changeList = 0;
00083 static QStringList *g_allResourceDirs = 0;
00084 static bool g_changed = false;
00085 static KSycocaEntry::List g_tempStorage;
00086 static VFolderMenu *g_vfolder = 0;
00087 
00088 static const char *cSycocaPath = 0;
00089 
00090 static bool bGlobalDatabase = false;
00091 static bool bMenuTest = false;
00092 
00093 void crashHandler(int)
00094 {
00095    // If we crash while reading sycoca, we delete the database
00096    // in an attempt to recover.
00097    if (cSycocaPath)
00098       unlink(cSycocaPath);
00099 }
00100 
00101 static QString sycocaPath()
00102 {
00103   QString path;
00104 
00105   if (bGlobalDatabase)
00106   {
00107      path = KGlobal::dirs()->saveLocation("services")+"ksycoca";
00108   }
00109   else
00110   {
00111      QCString ksycoca_env = getenv("KDESYCOCA");
00112      if (ksycoca_env.isEmpty())
00113         path = KGlobal::dirs()->saveLocation("cache")+"ksycoca";
00114      else
00115         path = QFile::decodeName(ksycoca_env);
00116   }
00117 
00118   return path;
00119 }
00120 
00121 static QString oldSycocaPath()
00122 {
00123   QCString ksycoca_env = getenv("KDESYCOCA");
00124   if (ksycoca_env.isEmpty())
00125      return KGlobal::dirs()->saveLocation("tmp")+"ksycoca";
00126 
00127   return QString::null;
00128 }
00129 
00130 KBuildSycoca::KBuildSycoca()
00131   : KSycoca( true )
00132 {
00133 }
00134 
00135 KBuildSycoca::~KBuildSycoca()
00136 {
00137 
00138 }
00139 
00140 void KBuildSycoca::processGnomeVfs()
00141 {
00142    QString file = locate("app-reg", "gnome-vfs.applications");
00143    if (file.isEmpty())
00144    {
00145 //      kdDebug(7021) << "gnome-vfs.applications not found." << endl;
00146       return;
00147    }
00148 
00149    QString app;
00150 
00151    char line[1024*64];
00152 
00153    FILE *f = fopen(QFile::encodeName(file), "r");
00154    while (!feof(f))
00155    {
00156       if (!fgets(line, sizeof(line)-1, f))
00157       {
00158         break;
00159       }
00160 
00161       if (line[0] != '\t')
00162       {
00163           app = QString::fromLatin1(line);
00164           app.truncate(app.length()-1);
00165       }
00166       else if (strncmp(line+1, "mime_types=", 11) == 0)
00167       {
00168           QString mimetypes = QString::fromLatin1(line+12);
00169           mimetypes.truncate(mimetypes.length()-1);
00170           mimetypes.replace(QRegExp("\\*"), "all");
00171           KService *s = g_bsf->findServiceByName(app);
00172           if (!s)
00173              continue;
00174 
00175           QStringList &serviceTypes = s->accessServiceTypes();
00176           if (serviceTypes.count() <= 1)
00177           {
00178              serviceTypes += QStringList::split(',', mimetypes);
00179 //             kdDebug(7021) << "Adding gnome mimetypes for '" << app << "'.\n";
00180 //             kdDebug(7021) << "ServiceTypes=" << s->serviceTypes().join(":") << endl;
00181           }
00182       }
00183    }
00184    fclose( f );
00185 }
00186 
00187 KSycocaEntry *KBuildSycoca::createEntry(const QString &file, bool addToFactory)
00188 {
00189    Q_UINT32 timeStamp = g_ctimeInfo->ctime(file);
00190    if (!timeStamp)
00191    {
00192       timeStamp = KGlobal::dirs()->calcResourceHash( g_resource, file, true);
00193    }
00194    KSycocaEntry* entry = 0;
00195    if (g_allEntries)
00196    {
00197       assert(g_ctimeDict);
00198       Q_UINT32 *timeP = (*g_ctimeDict)[file];
00199       Q_UINT32 oldTimestamp = timeP ? *timeP : 0;
00200 
00201       if (timeStamp && (timeStamp == oldTimestamp))
00202       {
00203          // Re-use old entry
00204          if (g_factory == g_bsgf) // Strip .directory from service-group entries
00205          {
00206             entry = g_entryDict->find(file.left(file.length()-10));
00207          }
00208          else if (g_factory == g_bsf)
00209          {
00210             entry = g_entryDict->find(file);
00211          }
00212          else
00213          {
00214             entry = g_entryDict->find(file);
00215          }
00216          // remove from g_ctimeDict; if g_ctimeDict is not empty
00217          // after all files have been processed, it means
00218          // some files were removed since last time
00219          g_ctimeDict->remove( file );
00220       }
00221       else if (oldTimestamp)
00222       {
00223          g_changed = true;
00224          kdDebug(7021) << "modified: " << file << endl;
00225       }
00226       else
00227       {
00228          g_changed = true;
00229          kdDebug(7021) << "new: " << file << endl;
00230       }
00231    }
00232    g_ctimeInfo->addCTime(file, timeStamp );
00233    if (!entry)
00234    {
00235       // Create a new entry
00236       entry = g_factory->createEntry( file, g_resource );
00237    }
00238    if ( entry && entry->isValid() )
00239    {
00240       if (addToFactory)
00241          g_factory->addEntry( entry, g_resource );
00242       else
00243          g_tempStorage.append(entry);
00244       return entry;
00245    }
00246    return 0;
00247 }
00248 
00249 void KBuildSycoca::slotCreateEntry(const QString &file, KService **service)
00250 {
00251    KSycocaEntry *entry = createEntry(file, false);
00252    *service = dynamic_cast<KService *>(entry);
00253 }
00254 
00255 // returns false if the database is up to date
00256 bool KBuildSycoca::build()
00257 {
00258   typedef QPtrList<KBSEntryDict> KBSEntryDictList;
00259   KBSEntryDictList *entryDictList = 0;
00260   KBSEntryDict *serviceEntryDict = 0;
00261 
00262   entryDictList = new KBSEntryDictList();
00263   // Convert for each factory the entryList to a Dict.
00264   int i = 0;
00265   // For each factory
00266   for (KSycocaFactory *factory = m_lstFactories->first();
00267        factory;
00268        factory = m_lstFactories->next() )
00269   {
00270      KBSEntryDict *entryDict = new KBSEntryDict();
00271      if (g_allEntries)
00272      {
00273          KSycocaEntry::List list = (*g_allEntries)[i++];
00274          for( KSycocaEntry::List::Iterator it = list.begin();
00275             it != list.end();
00276             ++it)
00277          {
00278             entryDict->insert( (*it)->entryPath(), static_cast<KSycocaEntry *>(*it));
00279          }
00280      }
00281      if (factory == g_bsf)
00282         serviceEntryDict = entryDict;
00283      else if (factory == g_bsgf)
00284         g_serviceGroupEntryDict = entryDict;
00285      entryDictList->append(entryDict);
00286   }
00287 
00288   QStringList allResources;
00289   // For each factory
00290   for (KSycocaFactory *factory = m_lstFactories->first();
00291        factory;
00292        factory = m_lstFactories->next() )
00293   {
00294     // For each resource the factory deals with
00295     const KSycocaResourceList *list = factory->resourceList();
00296     if (!list) continue;
00297 
00298     for( KSycocaResourceList::ConstIterator it1 = list->begin();
00299          it1 != list->end();
00300          ++it1 )
00301     {
00302       KSycocaResource res = (*it1);
00303       if (!allResources.contains(res.resource))
00304          allResources.append(res.resource);
00305     }
00306   }
00307 
00308   g_ctimeInfo = new KCTimeInfo(); // This is a build factory too, don't delete!!
00309   bool uptodate = true;
00310   // For all resources
00311   for( QStringList::ConstIterator it1 = allResources.begin();
00312        it1 != allResources.end();
00313        ++it1 )
00314   {
00315      g_changed = false;
00316      g_resource = (*it1).ascii();
00317 
00318      QStringList relFiles;
00319 
00320      (void) KGlobal::dirs()->findAllResources( g_resource,
00321                                                QString::null,
00322                                                true, // Recursive!
00323                                                true, // uniq
00324                                                relFiles);
00325 
00326 
00327      // Now find all factories that use this resource....
00328      // For each factory
00329      g_entryDict = entryDictList->first();
00330      for (g_factory = m_lstFactories->first();
00331           g_factory;
00332           g_factory = m_lstFactories->next(),
00333           g_entryDict = entryDictList->next() )
00334      {
00335         // For each resource the factory deals with
00336         const KSycocaResourceList *list = g_factory->resourceList();
00337         if (!list) continue;
00338 
00339         for( KSycocaResourceList::ConstIterator it2 = list->begin();
00340              it2 != list->end();
00341              ++it2 )
00342         {
00343            KSycocaResource res = (*it2);
00344            if (res.resource != (*it1)) continue;
00345 
00346            // For each file in the resource
00347            for( QStringList::ConstIterator it3 = relFiles.begin();
00348                 it3 != relFiles.end();
00349                 ++it3 )
00350            {
00351                // Check if file matches filter
00352                if ((*it3).endsWith(res.extension))
00353                    createEntry(*it3, true);
00354            }
00355         }
00356         if ((g_factory == g_bsf) && (strcmp(g_resource, "services") == 0))
00357            processGnomeVfs();
00358      }
00359      if (g_changed || !g_allEntries)
00360      {
00361         uptodate = false;
00362         g_changeList->append(g_resource);
00363      }
00364   }
00365 
00366   bool result = !uptodate || !g_ctimeDict->isEmpty();
00367 
00368   if (result || bMenuTest)
00369   {
00370      g_resource = "apps";
00371      g_factory = g_bsf;
00372      g_entryDict = serviceEntryDict;
00373      g_changed = false;
00374 
00375      g_vfolder = new VFolderMenu;
00376      if (!m_trackId.isEmpty())
00377         g_vfolder->setTrackId(m_trackId);
00378 
00379      connect(g_vfolder, SIGNAL(newService(const QString &, KService **)),
00380              this, SLOT(slotCreateEntry(const QString &, KService **)));
00381              
00382      VFolderMenu::SubMenu *kdeMenu = g_vfolder->parseMenu("applications.menu", true);
00383 
00384      KServiceGroup *entry = g_bsgf->addNew("/", kdeMenu->directoryFile, 0, false);
00385      entry->setLayoutInfo(kdeMenu->layoutList);
00386      createMenu(QString::null, QString::null, kdeMenu);
00387 
00388      KServiceGroup::Ptr g(entry);
00389      createMenuAttribute( g );
00390 
00391      (void) existingResourceDirs();
00392      *g_allResourceDirs += g_vfolder->allDirectories();
00393 
00394      disconnect(g_vfolder, SIGNAL(newService(const QString &, KService **)),
00395              this, SLOT(slotCreateEntry(const QString &, KService **)));
00396 
00397      if (g_changed || !g_allEntries)
00398      {
00399         uptodate = false;
00400         g_changeList->append(g_resource);
00401      }
00402      if (bMenuTest)
00403         return false;
00404   }
00405 
00406   return result;
00407 }
00408 
00409 void KBuildSycoca::createMenuAttribute( KServiceGroup::Ptr entry )
00410 {
00411     KServiceGroup::List list = entry->entries(true, true);
00412     KServiceGroup::List::ConstIterator it = list.begin();
00413     for (; it != list.end(); ++it) {
00414         KSycocaEntry * e = *it;
00415         if (e->isType(KST_KServiceGroup)) {
00416             KServiceGroup::Ptr g(static_cast<KServiceGroup *>(e));
00417             createMenuAttribute( g );
00418         }
00419     }
00420 }
00421 
00422 
00423 void KBuildSycoca::createMenu(QString caption, QString name, VFolderMenu::SubMenu *menu)
00424 {
00425   for(VFolderMenu::SubMenu *subMenu = menu->subMenus.first(); subMenu; subMenu = menu->subMenus.next())
00426   {
00427      QString subName = name+subMenu->name+"/";
00428 
00429      QString directoryFile = subMenu->directoryFile;
00430      if (directoryFile.isEmpty())
00431         directoryFile = subName+".directory";
00432      Q_UINT32 timeStamp = g_ctimeInfo->ctime(directoryFile);
00433      if (!timeStamp)
00434      {
00435         timeStamp = KGlobal::dirs()->calcResourceHash( g_resource, directoryFile, true);
00436      }
00437 
00438      KServiceGroup* entry = 0;
00439      if (g_allEntries)
00440      {
00441         Q_UINT32 *timeP = (*g_ctimeDict)[directoryFile];
00442         Q_UINT32 oldTimestamp = timeP ? *timeP : 0;
00443 
00444         if (timeStamp && (timeStamp == oldTimestamp))
00445         {
00446             entry = dynamic_cast<KServiceGroup *> (g_serviceGroupEntryDict->find(subName));
00447             if (entry && (entry->directoryEntryPath() != directoryFile))
00448                 entry = 0; // Can't reuse this one!
00449         }
00450      }
00451      g_ctimeInfo->addCTime(directoryFile, timeStamp);
00452 
00453      entry = g_bsgf->addNew(subName, subMenu->directoryFile, entry, subMenu->isDeleted);
00454      entry->setLayoutInfo(subMenu->layoutList);
00455      if (! (bMenuTest && entry->noDisplay()) )
00456         createMenu(caption + entry->caption() + "/", subName, subMenu);
00457   }
00458   if (caption.isEmpty())
00459      caption += "/";
00460   if (name.isEmpty())
00461      name += "/";
00462   for(QDictIterator<KService> it(menu->items); it.current(); ++it)
00463   {
00464      if (bMenuTest)
00465      {
00466         if (!menu->isDeleted && !it.current()->noDisplay())
00467           printf("%s\t%s\t%s\n", caption.local8Bit().data(), it.current()->menuId().local8Bit().data(), locate("apps", it.current()->desktopEntryPath()).local8Bit().data());
00468      }
00469      else
00470      {
00471         g_bsf->addEntry( it.current(), g_resource );
00472         g_bsgf->addNewEntryTo(name, it.current());
00473      }
00474   }
00475 }
00476 
00477 bool KBuildSycoca::recreate()
00478 {
00479   QString path(sycocaPath());
00480 #ifdef Q_WS_WIN
00481   printf("kbuildsycoca: path='%s'\n", (const char*)path);
00482 #endif
00483 
00484   // KSaveFile first writes to a temp file.
00485   // Upon close() it moves the stuff to the right place.
00486   std::auto_ptr<KSaveFile> database( new KSaveFile(path) );
00487   if (database->status() == EACCES && QFile::exists(path))
00488   {
00489     QFile::remove( path );
00490     database.reset( new KSaveFile(path) ); // try again
00491   }
00492   if (database->status() != 0)
00493   {
00494     fprintf(stderr, "kbuildsycoca: ERROR creating database '%s'! %s\n", path.local8Bit().data(),strerror(database->status()));
00495 #ifdef KBUILDSYCOCA_GUI // KBUILDSYCOCA_GUI is used on win32 to build 
00496                         // GUI version of kbuildsycoca, so-called "kbuildsycocaw".
00497     if (!silent)
00498       KMessageBox::error(0, i18n("Error creating database '%1'.\nCheck that the permissions are correct on the directory and the disk is not full.\n").arg(path.local8Bit().data()), i18n("KBuildSycoca"));
00499 #endif
00500     return false;
00501   }
00502 
00503   m_str = database->dataStream();
00504 
00505   kdDebug(7021) << "Recreating ksycoca file (" << path << ", version " << KSycoca::version() << ")" << endl;
00506 
00507   // It is very important to build the servicetype one first
00508   // Both are registered in KSycoca, no need to keep the pointers
00509   KSycocaFactory *stf = new KBuildServiceTypeFactory;
00510   g_bsgf = new KBuildServiceGroupFactory();
00511   g_bsf = new KBuildServiceFactory(stf, g_bsgf);
00512   (void) new KBuildImageIOFactory();
00513   (void) new KBuildProtocolInfoFactory();
00514 
00515   if( build()) // Parse dirs
00516   {
00517     save(); // Save database
00518     if (m_str->device()->status())
00519       database->abort(); // Error
00520     m_str = 0L;
00521     if (!database->close())
00522     {
00523       fprintf(stderr, "kbuildsycoca: ERROR writing database '%s'!\n", database->name().local8Bit().data());
00524       fprintf(stderr, "kbuildsycoca: Disk full?\n");
00525 #ifdef KBUILDSYCOCA_GUI
00526       if (!silent)
00527         KMessageBox::error(0, i18n("Error writing database '%1'.\nCheck that the permissions are correct on the directory and the disk is not full.\n").arg(path.local8Bit().data()), i18n("KBuildSycoca"));
00528 #endif
00529       return false;
00530     }
00531   }
00532   else
00533   {
00534     m_str = 0L;
00535     database->abort();
00536     if (bMenuTest)
00537        return true;
00538     kdDebug(7021) << "Database is up to date" << endl;
00539   }
00540 
00541   if (!bGlobalDatabase)
00542   {
00543     // update the timestamp file
00544     QString stamppath = path + "stamp";
00545     QFile ksycocastamp(stamppath);
00546     ksycocastamp.open( IO_WriteOnly );
00547     QDataStream str( &ksycocastamp );
00548     str << newTimestamp;
00549     str << existingResourceDirs();
00550     if (g_vfolder)
00551         str << g_vfolder->allDirectories(); // Extra resource dirs
00552   }
00553   return true;
00554 }
00555 
00556 void KBuildSycoca::save()
00557 {
00558    // Write header (#pass 1)
00559    m_str->device()->at(0);
00560 
00561    (*m_str) << (Q_INT32) KSycoca::version();
00562    KSycocaFactory * servicetypeFactory = 0L;
00563    KSycocaFactory * serviceFactory = 0L;
00564    for(KSycocaFactory *factory = m_lstFactories->first();
00565        factory;
00566        factory = m_lstFactories->next())
00567    {
00568       Q_INT32 aId;
00569       Q_INT32 aOffset;
00570       aId = factory->factoryId();
00571       if ( aId == KST_KServiceTypeFactory )
00572          servicetypeFactory = factory;
00573       else if ( aId == KST_KServiceFactory )
00574          serviceFactory = factory;
00575       aOffset = factory->offset();
00576       (*m_str) << aId;
00577       (*m_str) << aOffset;
00578    }
00579    (*m_str) << (Q_INT32) 0; // No more factories.
00580    // Write KDEDIRS
00581    (*m_str) << KGlobal::dirs()->kfsstnd_prefixes();
00582    (*m_str) << newTimestamp;
00583    (*m_str) << KGlobal::locale()->language();
00584    (*m_str) << KGlobal::dirs()->calcResourceHash("services", "update_ksycoca", true);
00585    (*m_str) << (*g_allResourceDirs);
00586 
00587    // Write factory data....
00588    for(KSycocaFactory *factory = m_lstFactories->first();
00589        factory;
00590        factory = m_lstFactories->next())
00591    {
00592       factory->save(*m_str);
00593       if (m_str->device()->status())
00594          return; // error
00595    }
00596 
00597    int endOfData = m_str->device()->at();
00598 
00599    // Write header (#pass 2)
00600    m_str->device()->at(0);
00601 
00602    (*m_str) << (Q_INT32) KSycoca::version();
00603    for(KSycocaFactory *factory = m_lstFactories->first();
00604        factory;
00605        factory = m_lstFactories->next())
00606    {
00607       Q_INT32 aId;
00608       Q_INT32 aOffset;
00609       aId = factory->factoryId();
00610       aOffset = factory->offset();
00611       (*m_str) << aId;
00612       (*m_str) << aOffset;
00613    }
00614    (*m_str) << (Q_INT32) 0; // No more factories.
00615 
00616    // Jump to end of database
00617    m_str->device()->at(endOfData);
00618 }
00619 
00620 bool KBuildSycoca::checkDirTimestamps( const QString& dirname, const QDateTime& stamp, bool top )
00621 {
00622    if( top )
00623    {
00624       QFileInfo inf( dirname );
00625       if( inf.lastModified() > stamp )
00626          {
00627          kdDebug( 7021 ) << "timestamp changed:" << dirname << endl;
00628          return false;
00629          }
00630    }
00631    QDir dir( dirname );
00632    const QFileInfoList *list = dir.entryInfoList( QDir::DefaultFilter, QDir::Unsorted );
00633    if (!list)
00634       return true;
00635 
00636    for( QFileInfoListIterator it( *list );
00637         it.current() != NULL;
00638         ++it )
00639    {
00640       QFileInfo* fi = it.current();
00641       if( fi->fileName() == "." || fi->fileName() == ".." )
00642          continue;
00643       if( fi->lastModified() > stamp )
00644       {
00645          kdDebug( 7201 ) << "timestamp changed:" << fi->filePath() << endl;
00646          return false;
00647       }
00648       if( fi->isDir() && !checkDirTimestamps( fi->filePath(), stamp, false ))
00649             return false;
00650    }
00651    return true;
00652 }
00653 
00654 // check times of last modification of all files on which ksycoca depens,
00655 // and also their directories
00656 // if all of them all older than the timestamp in file ksycocastamp, this
00657 // means that there's no need to rebuild ksycoca
00658 bool KBuildSycoca::checkTimestamps( Q_UINT32 timestamp, const QStringList &dirs )
00659 {
00660    kdDebug( 7021 ) << "checking file timestamps" << endl;
00661    QDateTime stamp;
00662    stamp.setTime_t( timestamp );
00663    for( QStringList::ConstIterator it = dirs.begin();
00664         it != dirs.end();
00665         ++it )
00666    {
00667       if( !checkDirTimestamps( *it, stamp, true ))
00668             return false;
00669    }
00670    kdDebug( 7021 ) << "timestamps check ok" << endl;
00671    return true;
00672 }
00673 
00674 QStringList KBuildSycoca::existingResourceDirs()
00675 {
00676    static QStringList* dirs = NULL;
00677    if( dirs != NULL )
00678        return *dirs;
00679    dirs = new QStringList;
00680    g_allResourceDirs = new QStringList;
00681    // these are all resources cached by ksycoca
00682    QStringList resources;
00683    resources += KBuildServiceTypeFactory::resourceTypes();
00684    resources += KBuildServiceGroupFactory::resourceTypes();
00685    resources += KBuildServiceFactory::resourceTypes();
00686    resources += KBuildImageIOFactory::resourceTypes();
00687    resources += KBuildProtocolInfoFactory::resourceTypes();
00688    while( !resources.empty())
00689    {
00690       QString res = resources.front();
00691       *dirs += KGlobal::dirs()->resourceDirs( res.latin1());
00692       resources.remove( res ); // remove this 'res' and all its duplicates
00693    }
00694 
00695    *g_allResourceDirs = *dirs;
00696 
00697    for( QStringList::Iterator it = dirs->begin();
00698         it != dirs->end(); )
00699    {
00700       QFileInfo inf( *it );
00701       if( !inf.exists() || !inf.isReadable() )
00702          it = dirs->remove( it );
00703       else
00704          ++it;
00705    }
00706    return *dirs;
00707 }
00708 
00709 static KCmdLineOptions options[] = {
00710    { "nosignal", I18N_NOOP("Do not signal applications to update"), 0 },
00711    { "noincremental", I18N_NOOP("Disable incremental update, re-read everything"), 0 },
00712    { "checkstamps", I18N_NOOP("Check file timestamps"), 0 },
00713    { "nocheckfiles", I18N_NOOP("Disable checking files (dangerous)"), 0 },
00714    { "global", I18N_NOOP("Create global database"), 0 },
00715    { "menutest", I18N_NOOP("Perform menu generation test run only"), 0 },
00716    { "track <menu-id>", I18N_NOOP("Track menu id for debug purposes"), 0 },
00717 #ifdef KBUILDSYCOCA_GUI
00718    { "silent", I18N_NOOP("Silent - work without windows and stderr"), 0 },
00719    { "showprogress", I18N_NOOP("Show progress information (even if 'silent' mode is on)"), 0 },
00720 #endif
00721    KCmdLineLastOption
00722 };
00723 
00724 static const char appName[] = "kbuildsycoca";
00725 static const char appVersion[] = "1.1";
00726 
00727 class WaitForSignal : public QObject
00728 {
00729 public:
00730    ~WaitForSignal() { kapp->eventLoop()->exitLoop(); }
00731 };
00732 
00733 extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
00734 {
00735    KLocale::setMainCatalogue("kdelibs");
00736    KAboutData d(appName, I18N_NOOP("KBuildSycoca"), appVersion,
00737                 I18N_NOOP("Rebuilds the system configuration cache."),
00738                 KAboutData::License_GPL, "(c) 1999-2002 KDE Developers");
00739    d.addAuthor("David Faure", I18N_NOOP("Author"), "faure@kde.org");
00740    d.addAuthor("Waldo Bastian", I18N_NOOP("Author"), "bastian@kde.org");
00741 
00742    KCmdLineArgs::init(argc, argv, &d);
00743    KCmdLineArgs::addCmdLineOptions(options);
00744    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00745    bGlobalDatabase = args->isSet("global");
00746    bMenuTest = args->isSet("menutest");
00747 
00748    if (bGlobalDatabase)
00749    {
00750      setenv("KDEHOME", "-", 1);
00751      setenv("KDEROOTHOME", "-", 1);
00752    }
00753 
00754    KApplication::disableAutoDcopRegistration();
00755 #ifdef KBUILDSYCOCA_GUI
00756    KApplication k;
00757 #else
00758    KApplication k(false, false);
00759 #endif
00760    k.disableSessionManagement();
00761 
00762 #ifdef KBUILDSYCOCA_GUI
00763    silent = args->isSet("silent");
00764    showprogress = args->isSet("showprogress");
00765    QLabel progress( QString("<p><br><nobr>    %1    </nobr><br>").arg( i18n("Reloading KDE configuration, please wait...") ), 0, "", Qt::WType_Dialog | Qt::WStyle_DialogBorder  | Qt::WStyle_Customize| Qt::WStyle_Title );
00766    QString capt = i18n("KDE Configuration Manager");
00767    if (!silent) {
00768      if (KMessageBox::No == KMessageBox::questionYesNo(0, i18n("Do you want to reload KDE configuration?"), capt, i18n("Reload"), i18n("Do Not Reload")))
00769        return 0;
00770    }
00771    if (!silent || showprogress) {
00772      progress.setCaption( capt );
00773      progress.show();
00774    }
00775 #endif
00776 
00777    KCrash::setCrashHandler(KCrash::defaultCrashHandler);
00778    KCrash::setEmergencySaveFunction(crashHandler);
00779    KCrash::setApplicationName(QString(appName));
00780 
00781    // this program is in kdelibs so it uses kdelibs as catalog
00782    KLocale::setMainCatalogue("kdelibs");
00783    // force generating of KLocale object. if not, the database will get
00784    // be translated
00785    KGlobal::locale();
00786    KGlobal::dirs()->addResourceType("app-reg", "share/application-registry" );
00787 
00788    DCOPClient *dcopClient = new DCOPClient();
00789 
00790    while(true)
00791    {
00792      QCString registeredName = dcopClient->registerAs(appName, false);
00793      if (registeredName.isEmpty())
00794      {
00795        fprintf(stderr, "Warning: %s is unable to register with DCOP.\n", appName);
00796        break;
00797      }
00798      else if (registeredName == appName)
00799      {
00800        break; // Go
00801      }
00802      fprintf(stderr, "Waiting for already running %s to finish.\n", appName);
00803 
00804      dcopClient->setNotifications( true );
00805      while (dcopClient->isApplicationRegistered(appName))
00806      {
00807        WaitForSignal *obj = new WaitForSignal;
00808        obj->connect(dcopClient, SIGNAL(applicationRemoved(const QCString &)),
00809                SLOT(deleteLater()));
00810        kapp->eventLoop()->enterLoop();
00811      }
00812      dcopClient->setNotifications( false );
00813    }
00814    fprintf(stderr, "%s running...\n", appName);
00815 
00816    bool checkfiles = bGlobalDatabase || args->isSet("checkfiles");
00817 
00818    bool incremental = !bGlobalDatabase && args->isSet("incremental") && checkfiles;
00819    if (incremental || !checkfiles)
00820    {
00821      KSycoca::self()->disableAutoRebuild(); // Prevent deadlock
00822      QString current_language = KGlobal::locale()->language();
00823      QString ksycoca_language = KSycoca::self()->language();
00824      Q_UINT32 current_update_sig = KGlobal::dirs()->calcResourceHash("services", "update_ksycoca", true);
00825      Q_UINT32 ksycoca_update_sig = KSycoca::self()->updateSignature();
00826 
00827      if ((current_update_sig != ksycoca_update_sig) ||
00828          (current_language != ksycoca_language) ||
00829          (KSycoca::self()->timeStamp() == 0))
00830      {
00831         incremental = false;
00832         checkfiles = true;
00833         delete KSycoca::self();
00834      }
00835    }
00836 
00837    g_changeList = new QStringList;
00838 
00839    bool checkstamps = incremental && args->isSet("checkstamps") && checkfiles;
00840    Q_UINT32 filestamp = 0;
00841    QStringList oldresourcedirs;
00842    if( checkstamps && incremental )
00843    {
00844        QString path = sycocaPath()+"stamp";
00845        QCString qPath = QFile::encodeName(path);
00846        cSycocaPath = qPath.data(); // Delete timestamps on crash
00847        QFile ksycocastamp(path);
00848        if( ksycocastamp.open( IO_ReadOnly ))
00849        {
00850            QDataStream str( &ksycocastamp );
00851            if (!str.atEnd())
00852                str >> filestamp;
00853            if (!str.atEnd())
00854            {
00855                str >> oldresourcedirs;
00856                if( oldresourcedirs != KBuildSycoca::existingResourceDirs())
00857                    checkstamps = false;
00858            }
00859            else
00860            {
00861                checkstamps = false;
00862            }
00863            if (!str.atEnd())
00864            {
00865                QStringList extraResourceDirs;
00866                str >> extraResourceDirs;
00867                oldresourcedirs += extraResourceDirs;
00868            }
00869        }
00870        else
00871        {
00872            checkstamps = false;
00873        }
00874        cSycocaPath = 0;
00875    }
00876 
00877    newTimestamp = (Q_UINT32) time(0);
00878 
00879    if( checkfiles && ( !checkstamps || !KBuildSycoca::checkTimestamps( filestamp, oldresourcedirs )))
00880    {
00881       QCString qSycocaPath = QFile::encodeName(sycocaPath());
00882       cSycocaPath = qSycocaPath.data();
00883 
00884       g_allEntries = 0;
00885       g_ctimeDict = 0;
00886       if (incremental)
00887       {
00888          qWarning("Reusing existing ksycoca");
00889          KSycoca *oldSycoca = KSycoca::self();
00890          KSycocaFactoryList *factories = new KSycocaFactoryList;
00891          g_allEntries = new KSycocaEntryListList;
00892          g_ctimeDict = new QDict<Q_UINT32>(523);
00893 
00894          // Must be in same order as in KBuildSycoca::recreate()!
00895          factories->append( new KServiceTypeFactory );
00896          factories->append( new KServiceGroupFactory );
00897          factories->append( new KServiceFactory );
00898          factories->append( new KImageIOFactory );
00899          factories->append( new KProtocolInfoFactory );
00900 
00901          // For each factory
00902          for (KSycocaFactory *factory = factories->first();
00903               factory;
00904               factory = factories->next() )
00905          {
00906              KSycocaEntry::List list;
00907              list = factory->allEntries();
00908              g_allEntries->append( list );
00909          }
00910          delete factories; factories = 0;
00911          KCTimeInfo *ctimeInfo = new KCTimeInfo;
00912          ctimeInfo->fillCTimeDict(*g_ctimeDict);
00913          delete oldSycoca;
00914       }
00915       cSycocaPath = 0;
00916 
00917       KBuildSycoca *sycoca= new KBuildSycoca; // Build data base
00918       if (args->isSet("track"))
00919          sycoca->setTrackId(QString::fromLocal8Bit(args->getOption("track")));
00920       if (!sycoca->recreate()) {
00921 #ifdef KBUILDSYCOCA_GUI
00922         if (!silent || showprogress)
00923           progress.close();
00924 #endif
00925         return -1;
00926       }
00927 
00928       if (bGlobalDatabase)
00929       {
00930         // These directories may have been created with 0700 permission
00931         // better delete them if they are empty
00932         QString applnkDir = KGlobal::dirs()->saveLocation("apps", QString::null, false);
00933         ::rmdir(QFile::encodeName(applnkDir));
00934         QString servicetypesDir = KGlobal::dirs()->saveLocation("servicetypes", QString::null, false);
00935         ::rmdir(QFile::encodeName(servicetypesDir));
00936       }
00937    }
00938 
00939    if (!bGlobalDatabase)
00940    {
00941      // Recreate compatibility symlink
00942      QString oldPath = oldSycocaPath();
00943      if (!oldPath.isEmpty())
00944      {
00945        KTempFile tmp;
00946        if (tmp.status() == 0)
00947        {
00948          QString tmpFile = tmp.name();
00949          tmp.unlink();
00950          symlink(QFile::encodeName(sycocaPath()), QFile::encodeName(tmpFile));
00951          rename(QFile::encodeName(tmpFile), QFile::encodeName(oldPath));
00952        }
00953      }
00954    }
00955 
00956    if (args->isSet("signal"))
00957    {
00958      // Notify ALL applications that have a ksycoca object, using a broadcast
00959      QByteArray data;
00960      QDataStream stream(data, IO_WriteOnly);
00961      stream << *g_changeList;
00962      dcopClient->send( "*", "ksycoca", "notifyDatabaseChanged(QStringList)", data );
00963    }
00964 
00965 #ifdef KBUILDSYCOCA_GUI
00966    if (!silent) {
00967      progress.close();
00968      KMessageBox::information(0, i18n("Configuration information reloaded successfully."), capt);
00969    }
00970 #endif
00971    return 0;
00972 }
00973 
00974 #include "kbuildsycoca.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys