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.
960 lines
29 KiB
960 lines
29 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
* Copyright (C) 1999 David Faure <faure@kde.org>
|
||
|
* Copyright (C) 2002-2003 Waldo Bastian <bastian@kde.org>
|
||
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation;
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
15 years ago
|
#include <tqdir.h>
|
||
|
#include <tqeventloop.h>
|
||
15 years ago
|
#include <config.h>
|
||
|
|
||
12 years ago
|
#include "tdebuildsycoca.h"
|
||
15 years ago
|
#include "kresourcelist.h"
|
||
|
#include "vfolder_menu.h"
|
||
|
|
||
|
#include <kservice.h>
|
||
|
#include <kmimetype.h>
|
||
|
#include <kbuildservicetypefactory.h>
|
||
|
#include <kbuildservicefactory.h>
|
||
|
#include <kbuildservicegroupfactory.h>
|
||
|
#include <kbuildimageiofactory.h>
|
||
|
#include <kbuildprotocolinfofactory.h>
|
||
|
#include <kctimefactory.h>
|
||
|
#include <kdatastream.h>
|
||
|
|
||
15 years ago
|
#include <tqdatastream.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqtimer.h>
|
||
15 years ago
|
|
||
|
#include <assert.h>
|
||
|
#include <kapplication.h>
|
||
|
#include <dcopclient.h>
|
||
|
#include <kglobal.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kdirwatch.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <ksavefile.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kaboutdata.h>
|
||
|
#include <kcmdlineargs.h>
|
||
|
#include <kcrash.h>
|
||
|
|
||
|
#ifdef KBUILDSYCOCA_GUI // KBUILDSYCOCA_GUI is used on win32 to build
|
||
12 years ago
|
// GUI version of tdebuildsycoca, so-called "tdebuildsycocaw".
|
||
15 years ago
|
# include <tqlabel.h>
|
||
15 years ago
|
# include <kmessagebox.h>
|
||
|
bool silent;
|
||
|
bool showprogress;
|
||
|
#endif
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <time.h>
|
||
|
#include <memory> // auto_ptr
|
||
|
|
||
15 years ago
|
typedef TQDict<KSycocaEntry> KBSEntryDict;
|
||
|
typedef TQValueList<KSycocaEntry::List> KSycocaEntryListList;
|
||
15 years ago
|
|
||
14 years ago
|
static TQ_UINT32 newTimestamp = 0;
|
||
15 years ago
|
|
||
|
static KBuildServiceFactory *g_bsf = 0;
|
||
|
static KBuildServiceGroupFactory *g_bsgf = 0;
|
||
|
static KSycocaFactory *g_factory = 0;
|
||
|
static KCTimeInfo *g_ctimeInfo = 0;
|
||
14 years ago
|
static TQDict<TQ_UINT32> *g_ctimeDict = 0;
|
||
15 years ago
|
static const char *g_resource = 0;
|
||
|
static KBSEntryDict *g_entryDict = 0;
|
||
|
static KBSEntryDict *g_serviceGroupEntryDict = 0;
|
||
|
static KSycocaEntryListList *g_allEntries = 0;
|
||
15 years ago
|
static TQStringList *g_changeList = 0;
|
||
|
static TQStringList *g_allResourceDirs = 0;
|
||
15 years ago
|
static bool g_changed = false;
|
||
|
static KSycocaEntry::List g_tempStorage;
|
||
|
static VFolderMenu *g_vfolder = 0;
|
||
|
|
||
|
static const char *cSycocaPath = 0;
|
||
|
|
||
|
static bool bGlobalDatabase = false;
|
||
|
static bool bMenuTest = false;
|
||
|
|
||
|
void crashHandler(int)
|
||
|
{
|
||
|
// If we crash while reading sycoca, we delete the database
|
||
|
// in an attempt to recover.
|
||
|
if (cSycocaPath)
|
||
|
unlink(cSycocaPath);
|
||
|
}
|
||
|
|
||
15 years ago
|
static TQString sycocaPath()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString path;
|
||
15 years ago
|
|
||
|
if (bGlobalDatabase)
|
||
|
{
|
||
12 years ago
|
path = TDEGlobal::dirs()->saveLocation("services")+"tdesycoca";
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
12 years ago
|
TQCString tdesycoca_env = getenv("TDESYCOCA");
|
||
|
if (tdesycoca_env.isEmpty())
|
||
|
path = TDEGlobal::dirs()->saveLocation("cache")+"tdesycoca";
|
||
15 years ago
|
else
|
||
12 years ago
|
path = TQFile::decodeName(tdesycoca_env);
|
||
15 years ago
|
}
|
||
|
|
||
|
return path;
|
||
|
}
|
||
|
|
||
15 years ago
|
static TQString oldSycocaPath()
|
||
15 years ago
|
{
|
||
12 years ago
|
TQCString tdesycoca_env = getenv("TDESYCOCA");
|
||
|
if (tdesycoca_env.isEmpty())
|
||
|
return TDEGlobal::dirs()->saveLocation("tmp")+"tdesycoca";
|
||
15 years ago
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
KBuildSycoca::KBuildSycoca()
|
||
|
: KSycoca( true )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
KBuildSycoca::~KBuildSycoca()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void KBuildSycoca::processGnomeVfs()
|
||
|
{
|
||
15 years ago
|
TQString file = locate("app-reg", "gnome-vfs.applications");
|
||
15 years ago
|
if (file.isEmpty())
|
||
|
{
|
||
|
// kdDebug(7021) << "gnome-vfs.applications not found." << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString app;
|
||
15 years ago
|
|
||
|
char line[1024*64];
|
||
|
|
||
15 years ago
|
FILE *f = fopen(TQFile::encodeName(file), "r");
|
||
15 years ago
|
while (!feof(f))
|
||
|
{
|
||
|
if (!fgets(line, sizeof(line)-1, f))
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (line[0] != '\t')
|
||
|
{
|
||
13 years ago
|
app = TQString::fromLatin1(line);
|
||
15 years ago
|
app.truncate(app.length()-1);
|
||
|
}
|
||
|
else if (strncmp(line+1, "mime_types=", 11) == 0)
|
||
|
{
|
||
13 years ago
|
TQString mimetypes = TQString::fromLatin1(line+12);
|
||
15 years ago
|
mimetypes.truncate(mimetypes.length()-1);
|
||
14 years ago
|
mimetypes.replace(TQRegExp("\\*"), "all");
|
||
|
KService *s = g_bsf->findServiceByName(app);
|
||
15 years ago
|
if (!s)
|
||
|
continue;
|
||
|
|
||
15 years ago
|
TQStringList &serviceTypes = s->accessServiceTypes();
|
||
15 years ago
|
if (serviceTypes.count() <= 1)
|
||
|
{
|
||
15 years ago
|
serviceTypes += TQStringList::split(',', mimetypes);
|
||
15 years ago
|
// kdDebug(7021) << "Adding gnome mimetypes for '" << app << "'.\n";
|
||
|
// kdDebug(7021) << "ServiceTypes=" << s->serviceTypes().join(":") << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
fclose( f );
|
||
|
}
|
||
|
|
||
15 years ago
|
KSycocaEntry *KBuildSycoca::createEntry(const TQString &file, bool addToFactory)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQ_UINT32 timeStamp = g_ctimeInfo->ctime(file);
|
||
15 years ago
|
if (!timeStamp)
|
||
|
{
|
||
12 years ago
|
timeStamp = TDEGlobal::dirs()->calcResourceHash( g_resource, file, true);
|
||
15 years ago
|
}
|
||
|
KSycocaEntry* entry = 0;
|
||
|
if (g_allEntries)
|
||
|
{
|
||
|
assert(g_ctimeDict);
|
||
14 years ago
|
TQ_UINT32 *timeP = (*g_ctimeDict)[file];
|
||
|
TQ_UINT32 oldTimestamp = timeP ? *timeP : 0;
|
||
15 years ago
|
|
||
|
if (timeStamp && (timeStamp == oldTimestamp))
|
||
|
{
|
||
|
// Re-use old entry
|
||
|
if (g_factory == g_bsgf) // Strip .directory from service-group entries
|
||
|
{
|
||
13 years ago
|
entry = g_entryDict->find(file.left(file.length()-10));
|
||
15 years ago
|
}
|
||
|
else if (g_factory == g_bsf)
|
||
|
{
|
||
13 years ago
|
entry = g_entryDict->find(file);
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
13 years ago
|
entry = g_entryDict->find(file);
|
||
15 years ago
|
}
|
||
|
// remove from g_ctimeDict; if g_ctimeDict is not empty
|
||
|
// after all files have been processed, it means
|
||
|
// some files were removed since last time
|
||
|
g_ctimeDict->remove( file );
|
||
|
}
|
||
|
else if (oldTimestamp)
|
||
|
{
|
||
|
g_changed = true;
|
||
|
kdDebug(7021) << "modified: " << file << endl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
g_changed = true;
|
||
|
kdDebug(7021) << "new: " << file << endl;
|
||
|
}
|
||
|
}
|
||
|
g_ctimeInfo->addCTime(file, timeStamp );
|
||
|
if (!entry)
|
||
|
{
|
||
|
// Create a new entry
|
||
|
entry = g_factory->createEntry( file, g_resource );
|
||
|
}
|
||
|
if ( entry && entry->isValid() )
|
||
|
{
|
||
|
if (addToFactory)
|
||
|
g_factory->addEntry( entry, g_resource );
|
||
|
else
|
||
|
g_tempStorage.append(entry);
|
||
|
return entry;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KBuildSycoca::slotCreateEntry(const TQString &file, KService **service)
|
||
15 years ago
|
{
|
||
|
KSycocaEntry *entry = createEntry(file, false);
|
||
|
*service = dynamic_cast<KService *>(entry);
|
||
|
}
|
||
|
|
||
|
// returns false if the database is up to date
|
||
|
bool KBuildSycoca::build()
|
||
|
{
|
||
15 years ago
|
typedef TQPtrList<KBSEntryDict> KBSEntryDictList;
|
||
15 years ago
|
KBSEntryDictList *entryDictList = 0;
|
||
|
KBSEntryDict *serviceEntryDict = 0;
|
||
|
|
||
|
entryDictList = new KBSEntryDictList();
|
||
|
// Convert for each factory the entryList to a Dict.
|
||
|
int i = 0;
|
||
|
// For each factory
|
||
|
for (KSycocaFactory *factory = m_lstFactories->first();
|
||
|
factory;
|
||
|
factory = m_lstFactories->next() )
|
||
|
{
|
||
|
KBSEntryDict *entryDict = new KBSEntryDict();
|
||
|
if (g_allEntries)
|
||
|
{
|
||
|
KSycocaEntry::List list = (*g_allEntries)[i++];
|
||
|
for( KSycocaEntry::List::Iterator it = list.begin();
|
||
|
it != list.end();
|
||
|
++it)
|
||
|
{
|
||
|
entryDict->insert( (*it)->entryPath(), static_cast<KSycocaEntry *>(*it));
|
||
|
}
|
||
|
}
|
||
|
if (factory == g_bsf)
|
||
|
serviceEntryDict = entryDict;
|
||
|
else if (factory == g_bsgf)
|
||
|
g_serviceGroupEntryDict = entryDict;
|
||
|
entryDictList->append(entryDict);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList allResources;
|
||
15 years ago
|
// For each factory
|
||
|
for (KSycocaFactory *factory = m_lstFactories->first();
|
||
|
factory;
|
||
|
factory = m_lstFactories->next() )
|
||
|
{
|
||
|
// For each resource the factory deals with
|
||
|
const KSycocaResourceList *list = factory->resourceList();
|
||
|
if (!list) continue;
|
||
|
|
||
|
for( KSycocaResourceList::ConstIterator it1 = list->begin();
|
||
|
it1 != list->end();
|
||
|
++it1 )
|
||
|
{
|
||
|
KSycocaResource res = (*it1);
|
||
13 years ago
|
if (!allResources.contains(res.resource))
|
||
15 years ago
|
allResources.append(res.resource);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
g_ctimeInfo = new KCTimeInfo(); // This is a build factory too, don't delete!!
|
||
|
bool uptodate = true;
|
||
|
// For all resources
|
||
15 years ago
|
for( TQStringList::ConstIterator it1 = allResources.begin();
|
||
15 years ago
|
it1 != allResources.end();
|
||
|
++it1 )
|
||
|
{
|
||
|
g_changed = false;
|
||
|
g_resource = (*it1).ascii();
|
||
|
|
||
15 years ago
|
TQStringList relFiles;
|
||
15 years ago
|
|
||
12 years ago
|
(void) TDEGlobal::dirs()->findAllResources( g_resource,
|
||
15 years ago
|
TQString::null,
|
||
15 years ago
|
true, // Recursive!
|
||
|
true, // uniq
|
||
|
relFiles);
|
||
|
|
||
|
|
||
14 years ago
|
// Now find all factories that use this resource....
|
||
15 years ago
|
// For each factory
|
||
|
g_entryDict = entryDictList->first();
|
||
|
for (g_factory = m_lstFactories->first();
|
||
|
g_factory;
|
||
|
g_factory = m_lstFactories->next(),
|
||
|
g_entryDict = entryDictList->next() )
|
||
|
{
|
||
|
// For each resource the factory deals with
|
||
|
const KSycocaResourceList *list = g_factory->resourceList();
|
||
|
if (!list) continue;
|
||
|
|
||
|
for( KSycocaResourceList::ConstIterator it2 = list->begin();
|
||
|
it2 != list->end();
|
||
|
++it2 )
|
||
|
{
|
||
|
KSycocaResource res = (*it2);
|
||
|
if (res.resource != (*it1)) continue;
|
||
|
|
||
|
// For each file in the resource
|
||
15 years ago
|
for( TQStringList::ConstIterator it3 = relFiles.begin();
|
||
15 years ago
|
it3 != relFiles.end();
|
||
|
++it3 )
|
||
|
{
|
||
|
// Check if file matches filter
|
||
|
if ((*it3).endsWith(res.extension))
|
||
|
createEntry(*it3, true);
|
||
|
}
|
||
|
}
|
||
|
if ((g_factory == g_bsf) && (strcmp(g_resource, "services") == 0))
|
||
|
processGnomeVfs();
|
||
|
}
|
||
|
if (g_changed || !g_allEntries)
|
||
|
{
|
||
|
uptodate = false;
|
||
|
g_changeList->append(g_resource);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool result = !uptodate || !g_ctimeDict->isEmpty();
|
||
|
|
||
|
if (result || bMenuTest)
|
||
|
{
|
||
|
g_resource = "apps";
|
||
|
g_factory = g_bsf;
|
||
|
g_entryDict = serviceEntryDict;
|
||
|
g_changed = false;
|
||
|
|
||
|
g_vfolder = new VFolderMenu;
|
||
|
if (!m_trackId.isEmpty())
|
||
|
g_vfolder->setTrackId(m_trackId);
|
||
|
|
||
15 years ago
|
connect(g_vfolder, TQT_SIGNAL(newService(const TQString &, KService **)),
|
||
|
this, TQT_SLOT(slotCreateEntry(const TQString &, KService **)));
|
||
15 years ago
|
|
||
|
VFolderMenu::SubMenu *kdeMenu = g_vfolder->parseMenu("applications.menu", true);
|
||
|
|
||
|
KServiceGroup *entry = g_bsgf->addNew("/", kdeMenu->directoryFile, 0, false);
|
||
14 years ago
|
entry->setLayoutInfo(kdeMenu->layoutList);
|
||
15 years ago
|
createMenu(TQString::null, TQString::null, kdeMenu);
|
||
15 years ago
|
|
||
|
KServiceGroup::Ptr g(entry);
|
||
|
|
||
|
(void) existingResourceDirs();
|
||
|
*g_allResourceDirs += g_vfolder->allDirectories();
|
||
|
|
||
15 years ago
|
disconnect(g_vfolder, TQT_SIGNAL(newService(const TQString &, KService **)),
|
||
|
this, TQT_SLOT(slotCreateEntry(const TQString &, KService **)));
|
||
15 years ago
|
|
||
|
if (g_changed || !g_allEntries)
|
||
|
{
|
||
|
uptodate = false;
|
||
|
g_changeList->append(g_resource);
|
||
|
}
|
||
|
if (bMenuTest)
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KBuildSycoca::createMenu(TQString caption, TQString name, VFolderMenu::SubMenu *menu)
|
||
15 years ago
|
{
|
||
|
for(VFolderMenu::SubMenu *subMenu = menu->subMenus.first(); subMenu; subMenu = menu->subMenus.next())
|
||
|
{
|
||
15 years ago
|
TQString subName = name+subMenu->name+"/";
|
||
15 years ago
|
|
||
15 years ago
|
TQString directoryFile = subMenu->directoryFile;
|
||
15 years ago
|
if (directoryFile.isEmpty())
|
||
|
directoryFile = subName+".directory";
|
||
14 years ago
|
TQ_UINT32 timeStamp = g_ctimeInfo->ctime(directoryFile);
|
||
15 years ago
|
if (!timeStamp)
|
||
|
{
|
||
12 years ago
|
timeStamp = TDEGlobal::dirs()->calcResourceHash( g_resource, directoryFile, true);
|
||
15 years ago
|
}
|
||
|
|
||
|
KServiceGroup* entry = 0;
|
||
|
if (g_allEntries)
|
||
|
{
|
||
14 years ago
|
TQ_UINT32 *timeP = (*g_ctimeDict)[directoryFile];
|
||
|
TQ_UINT32 oldTimestamp = timeP ? *timeP : 0;
|
||
15 years ago
|
|
||
|
if (timeStamp && (timeStamp == oldTimestamp))
|
||
|
{
|
||
13 years ago
|
entry = dynamic_cast<KServiceGroup *> (g_serviceGroupEntryDict->find(subName));
|
||
15 years ago
|
if (entry && (entry->directoryEntryPath() != directoryFile))
|
||
|
entry = 0; // Can't reuse this one!
|
||
|
}
|
||
|
}
|
||
|
g_ctimeInfo->addCTime(directoryFile, timeStamp);
|
||
|
|
||
|
entry = g_bsgf->addNew(subName, subMenu->directoryFile, entry, subMenu->isDeleted);
|
||
14 years ago
|
entry->setLayoutInfo(subMenu->layoutList);
|
||
15 years ago
|
if (! (bMenuTest && entry->noDisplay()) )
|
||
|
createMenu(caption + entry->caption() + "/", subName, subMenu);
|
||
|
}
|
||
|
if (caption.isEmpty())
|
||
|
caption += "/";
|
||
|
if (name.isEmpty())
|
||
|
name += "/";
|
||
15 years ago
|
for(TQDictIterator<KService> it(menu->items); it.current(); ++it)
|
||
15 years ago
|
{
|
||
|
if (bMenuTest)
|
||
|
{
|
||
|
if (!menu->isDeleted && !it.current()->noDisplay())
|
||
|
printf("%s\t%s\t%s\n", caption.local8Bit().data(), it.current()->menuId().local8Bit().data(), locate("apps", it.current()->desktopEntryPath()).local8Bit().data());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
g_bsf->addEntry( it.current(), g_resource );
|
||
|
g_bsgf->addNewEntryTo(name, it.current());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool KBuildSycoca::recreate()
|
||
|
{
|
||
15 years ago
|
TQString path(sycocaPath());
|
||
15 years ago
|
#ifdef Q_WS_WIN
|
||
12 years ago
|
printf("tdebuildsycoca: path='%s'\n", (const char*)path);
|
||
15 years ago
|
#endif
|
||
|
|
||
|
// KSaveFile first writes to a temp file.
|
||
|
// Upon close() it moves the stuff to the right place.
|
||
|
std::auto_ptr<KSaveFile> database( new KSaveFile(path) );
|
||
15 years ago
|
if (database->status() == EACCES && TQFile::exists(path))
|
||
15 years ago
|
{
|
||
15 years ago
|
TQFile::remove( path );
|
||
15 years ago
|
database.reset( new KSaveFile(path) ); // try again
|
||
|
}
|
||
|
if (database->status() != 0)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdebuildsycoca] ERROR creating database '%s'! %s\n", path.local8Bit().data(),strerror(database->status()));
|
||
15 years ago
|
#ifdef KBUILDSYCOCA_GUI // KBUILDSYCOCA_GUI is used on win32 to build
|
||
12 years ago
|
// GUI version of tdebuildsycoca, so-called "tdebuildsycocaw".
|
||
15 years ago
|
if (!silent)
|
||
|
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"));
|
||
|
#endif
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
m_str = database->dataStream();
|
||
|
|
||
12 years ago
|
kdDebug(7021) << "Recreating tdesycoca file (" << path << ", version " << KSycoca::version() << ")" << endl;
|
||
15 years ago
|
|
||
|
// It is very important to build the servicetype one first
|
||
|
// Both are registered in KSycoca, no need to keep the pointers
|
||
|
KSycocaFactory *stf = new KBuildServiceTypeFactory;
|
||
|
g_bsgf = new KBuildServiceGroupFactory();
|
||
|
g_bsf = new KBuildServiceFactory(stf, g_bsgf);
|
||
|
(void) new KBuildImageIOFactory();
|
||
|
(void) new KBuildProtocolInfoFactory();
|
||
|
|
||
|
if( build()) // Parse dirs
|
||
|
{
|
||
|
save(); // Save database
|
||
13 years ago
|
if (m_str->device()->status())
|
||
15 years ago
|
database->abort(); // Error
|
||
|
m_str = 0L;
|
||
|
if (!database->close())
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdebuildsycoca] ERROR writing database '%s'!\n", database->name().local8Bit().data());
|
||
|
fprintf(stderr, "[tdebuildsycoca] Disk full?\n");
|
||
15 years ago
|
#ifdef KBUILDSYCOCA_GUI
|
||
|
if (!silent)
|
||
12 years ago
|
KMessageBox::error(0, i18n("[tdebuildsycoca] 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"));
|
||
15 years ago
|
#endif
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
m_str = 0L;
|
||
|
database->abort();
|
||
|
if (bMenuTest)
|
||
|
return true;
|
||
|
kdDebug(7021) << "Database is up to date" << endl;
|
||
|
}
|
||
|
|
||
|
if (!bGlobalDatabase)
|
||
|
{
|
||
|
// update the timestamp file
|
||
15 years ago
|
TQString stamppath = path + "stamp";
|
||
12 years ago
|
TQFile tdesycocastamp(stamppath);
|
||
|
tdesycocastamp.open( IO_WriteOnly );
|
||
|
TQDataStream str( &tdesycocastamp );
|
||
15 years ago
|
str << newTimestamp;
|
||
|
str << existingResourceDirs();
|
||
|
if (g_vfolder)
|
||
|
str << g_vfolder->allDirectories(); // Extra resource dirs
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void KBuildSycoca::save()
|
||
|
{
|
||
|
// Write header (#pass 1)
|
||
13 years ago
|
m_str->device()->at(0);
|
||
15 years ago
|
|
||
14 years ago
|
(*m_str) << (TQ_INT32) KSycoca::version();
|
||
15 years ago
|
KSycocaFactory * servicetypeFactory = 0L;
|
||
|
KSycocaFactory * serviceFactory = 0L;
|
||
|
for(KSycocaFactory *factory = m_lstFactories->first();
|
||
|
factory;
|
||
|
factory = m_lstFactories->next())
|
||
|
{
|
||
14 years ago
|
TQ_INT32 aId;
|
||
|
TQ_INT32 aOffset;
|
||
15 years ago
|
aId = factory->factoryId();
|
||
|
if ( aId == KST_KServiceTypeFactory )
|
||
|
servicetypeFactory = factory;
|
||
|
else if ( aId == KST_KServiceFactory )
|
||
|
serviceFactory = factory;
|
||
|
aOffset = factory->offset();
|
||
|
(*m_str) << aId;
|
||
|
(*m_str) << aOffset;
|
||
|
}
|
||
14 years ago
|
(*m_str) << (TQ_INT32) 0; // No more factories.
|
||
13 years ago
|
// Write TDEDIRS
|
||
12 years ago
|
(*m_str) << TDEGlobal::dirs()->kfsstnd_prefixes();
|
||
15 years ago
|
(*m_str) << newTimestamp;
|
||
12 years ago
|
(*m_str) << TDEGlobal::locale()->language();
|
||
12 years ago
|
(*m_str) << TDEGlobal::dirs()->calcResourceHash("services", "update_tdesycoca", true);
|
||
15 years ago
|
(*m_str) << (*g_allResourceDirs);
|
||
|
|
||
|
// Write factory data....
|
||
|
for(KSycocaFactory *factory = m_lstFactories->first();
|
||
|
factory;
|
||
|
factory = m_lstFactories->next())
|
||
|
{
|
||
|
factory->save(*m_str);
|
||
13 years ago
|
if (m_str->device()->status())
|
||
15 years ago
|
return; // error
|
||
|
}
|
||
|
|
||
13 years ago
|
int endOfData = m_str->device()->at();
|
||
15 years ago
|
|
||
|
// Write header (#pass 2)
|
||
13 years ago
|
m_str->device()->at(0);
|
||
15 years ago
|
|
||
14 years ago
|
(*m_str) << (TQ_INT32) KSycoca::version();
|
||
15 years ago
|
for(KSycocaFactory *factory = m_lstFactories->first();
|
||
|
factory;
|
||
|
factory = m_lstFactories->next())
|
||
|
{
|
||
14 years ago
|
TQ_INT32 aId;
|
||
|
TQ_INT32 aOffset;
|
||
15 years ago
|
aId = factory->factoryId();
|
||
|
aOffset = factory->offset();
|
||
|
(*m_str) << aId;
|
||
|
(*m_str) << aOffset;
|
||
|
}
|
||
14 years ago
|
(*m_str) << (TQ_INT32) 0; // No more factories.
|
||
15 years ago
|
|
||
|
// Jump to end of database
|
||
13 years ago
|
m_str->device()->at(endOfData);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
bool KBuildSycoca::checkDirTimestamps( const TQString& dirname, const TQDateTime& stamp, bool top )
|
||
15 years ago
|
{
|
||
|
if( top )
|
||
|
{
|
||
15 years ago
|
TQFileInfo inf( dirname );
|
||
15 years ago
|
if( inf.lastModified() > stamp )
|
||
|
{
|
||
|
kdDebug( 7021 ) << "timestamp changed:" << dirname << endl;
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
15 years ago
|
TQDir dir( dirname );
|
||
14 years ago
|
const TQFileInfoList *list = dir.entryInfoList( TQDir::DefaultFilter, TQDir::Unsorted );
|
||
15 years ago
|
if (!list)
|
||
|
return true;
|
||
|
|
||
14 years ago
|
for( TQFileInfoListIterator it( *list );
|
||
15 years ago
|
it.current() != NULL;
|
||
|
++it )
|
||
|
{
|
||
15 years ago
|
TQFileInfo* fi = it.current();
|
||
15 years ago
|
if( fi->fileName() == "." || fi->fileName() == ".." )
|
||
|
continue;
|
||
|
if( fi->lastModified() > stamp )
|
||
|
{
|
||
|
kdDebug( 7201 ) << "timestamp changed:" << fi->filePath() << endl;
|
||
|
return false;
|
||
|
}
|
||
|
if( fi->isDir() && !checkDirTimestamps( fi->filePath(), stamp, false ))
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
12 years ago
|
// check times of last modification of all files on which tdesycoca depens,
|
||
15 years ago
|
// and also their directories
|
||
12 years ago
|
// if all of them all older than the timestamp in file tdesycocastamp, this
|
||
|
// means that there's no need to rebuild tdesycoca
|
||
14 years ago
|
bool KBuildSycoca::checkTimestamps( TQ_UINT32 timestamp, const TQStringList &dirs )
|
||
15 years ago
|
{
|
||
|
kdDebug( 7021 ) << "checking file timestamps" << endl;
|
||
15 years ago
|
TQDateTime stamp;
|
||
15 years ago
|
stamp.setTime_t( timestamp );
|
||
15 years ago
|
for( TQStringList::ConstIterator it = dirs.begin();
|
||
15 years ago
|
it != dirs.end();
|
||
|
++it )
|
||
|
{
|
||
|
if( !checkDirTimestamps( *it, stamp, true ))
|
||
|
return false;
|
||
|
}
|
||
|
kdDebug( 7021 ) << "timestamps check ok" << endl;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KBuildSycoca::existingResourceDirs()
|
||
15 years ago
|
{
|
||
15 years ago
|
static TQStringList* dirs = NULL;
|
||
15 years ago
|
if( dirs != NULL )
|
||
|
return *dirs;
|
||
15 years ago
|
dirs = new TQStringList;
|
||
|
g_allResourceDirs = new TQStringList;
|
||
12 years ago
|
// these are all resources cached by tdesycoca
|
||
15 years ago
|
TQStringList resources;
|
||
15 years ago
|
resources += KBuildServiceTypeFactory::resourceTypes();
|
||
|
resources += KBuildServiceGroupFactory::resourceTypes();
|
||
|
resources += KBuildServiceFactory::resourceTypes();
|
||
|
resources += KBuildImageIOFactory::resourceTypes();
|
||
|
resources += KBuildProtocolInfoFactory::resourceTypes();
|
||
|
while( !resources.empty())
|
||
|
{
|
||
15 years ago
|
TQString res = resources.front();
|
||
12 years ago
|
*dirs += TDEGlobal::dirs()->resourceDirs( res.latin1());
|
||
15 years ago
|
resources.remove( res ); // remove this 'res' and all its duplicates
|
||
|
}
|
||
|
|
||
|
*g_allResourceDirs = *dirs;
|
||
|
|
||
15 years ago
|
for( TQStringList::Iterator it = dirs->begin();
|
||
15 years ago
|
it != dirs->end(); )
|
||
|
{
|
||
15 years ago
|
TQFileInfo inf( *it );
|
||
15 years ago
|
if( !inf.exists() || !inf.isReadable() )
|
||
|
it = dirs->remove( it );
|
||
|
else
|
||
|
++it;
|
||
|
}
|
||
|
return *dirs;
|
||
|
}
|
||
|
|
||
|
static KCmdLineOptions options[] = {
|
||
|
{ "nosignal", I18N_NOOP("Do not signal applications to update"), 0 },
|
||
|
{ "noincremental", I18N_NOOP("Disable incremental update, re-read everything"), 0 },
|
||
|
{ "checkstamps", I18N_NOOP("Check file timestamps"), 0 },
|
||
12 years ago
|
{ "nochectdefiles", I18N_NOOP("Disable checking files (dangerous)"), 0 },
|
||
15 years ago
|
{ "global", I18N_NOOP("Create global database"), 0 },
|
||
|
{ "menutest", I18N_NOOP("Perform menu generation test run only"), 0 },
|
||
|
{ "track <menu-id>", I18N_NOOP("Track menu id for debug purposes"), 0 },
|
||
|
#ifdef KBUILDSYCOCA_GUI
|
||
|
{ "silent", I18N_NOOP("Silent - work without windows and stderr"), 0 },
|
||
|
{ "showprogress", I18N_NOOP("Show progress information (even if 'silent' mode is on)"), 0 },
|
||
|
#endif
|
||
|
KCmdLineLastOption
|
||
|
};
|
||
|
|
||
12 years ago
|
static const char appName[] = "tdebuildsycoca";
|
||
15 years ago
|
static const char appVersion[] = "1.1";
|
||
|
|
||
14 years ago
|
class WaitForSignal : public QObject
|
||
15 years ago
|
{
|
||
|
public:
|
||
|
~WaitForSignal() { kapp->eventLoop()->exitLoop(); }
|
||
|
};
|
||
|
|
||
|
extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
|
||
|
{
|
||
13 years ago
|
KLocale::setMainCatalogue("tdelibs");
|
||
12 years ago
|
TDEAboutData d(appName, I18N_NOOP("KBuildSycoca"), appVersion,
|
||
15 years ago
|
I18N_NOOP("Rebuilds the system configuration cache."),
|
||
12 years ago
|
TDEAboutData::License_GPL, "(c) 1999-2002 KDE Developers");
|
||
15 years ago
|
d.addAuthor("David Faure", I18N_NOOP("Author"), "faure@kde.org");
|
||
|
d.addAuthor("Waldo Bastian", I18N_NOOP("Author"), "bastian@kde.org");
|
||
|
|
||
12 years ago
|
TDECmdLineArgs::init(argc, argv, &d);
|
||
|
TDECmdLineArgs::addCmdLineOptions(options);
|
||
|
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
|
||
15 years ago
|
bGlobalDatabase = args->isSet("global");
|
||
|
bMenuTest = args->isSet("menutest");
|
||
|
|
||
|
if (bGlobalDatabase)
|
||
|
{
|
||
13 years ago
|
setenv("TDEHOME", "-", 1);
|
||
13 years ago
|
setenv("TDEROOTHOME", "-", 1);
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
TDEApplication::disableAutoDcopRegistration();
|
||
15 years ago
|
#ifdef KBUILDSYCOCA_GUI
|
||
12 years ago
|
TDEApplication k;
|
||
15 years ago
|
#else
|
||
12 years ago
|
TDEApplication k(false, false);
|
||
15 years ago
|
#endif
|
||
|
k.disableSessionManagement();
|
||
|
|
||
|
#ifdef KBUILDSYCOCA_GUI
|
||
|
silent = args->isSet("silent");
|
||
|
showprogress = args->isSet("showprogress");
|
||
13 years ago
|
TQLabel progress( TQString("<p><br><nobr> %1 </nobr><br>").arg( i18n("Reloading TDE configuration, please wait...") ), 0, "", Qt::WType_Dialog | Qt::WStyle_DialogBorder | Qt::WStyle_Customize| Qt::WStyle_Title );
|
||
|
TQString capt = i18n("TDE Configuration Manager");
|
||
15 years ago
|
if (!silent) {
|
||
13 years ago
|
if (KMessageBox::No == KMessageBox::questionYesNo(0, i18n("Do you want to reload TDE configuration?"), capt, i18n("Reload"), i18n("Do Not Reload")))
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
if (!silent || showprogress) {
|
||
|
progress.setCaption( capt );
|
||
|
progress.show();
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
KCrash::setCrashHandler(KCrash::defaultCrashHandler);
|
||
|
KCrash::setEmergencySaveFunction(crashHandler);
|
||
15 years ago
|
KCrash::setApplicationName(TQString(appName));
|
||
15 years ago
|
|
||
13 years ago
|
// this program is in tdelibs so it uses tdelibs as catalog
|
||
|
KLocale::setMainCatalogue("tdelibs");
|
||
15 years ago
|
// force generating of KLocale object. if not, the database will get
|
||
|
// be translated
|
||
12 years ago
|
TDEGlobal::locale();
|
||
|
TDEGlobal::dirs()->addResourceType("app-reg", "share/application-registry" );
|
||
15 years ago
|
|
||
|
DCOPClient *dcopClient = new DCOPClient();
|
||
|
|
||
|
while(true)
|
||
|
{
|
||
15 years ago
|
TQCString registeredName = dcopClient->registerAs(appName, false);
|
||
15 years ago
|
if (registeredName.isEmpty())
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdebuildsycoca] Warning: %s is unable to register with DCOP.\n", appName);
|
||
15 years ago
|
break;
|
||
|
}
|
||
|
else if (registeredName == appName)
|
||
|
{
|
||
|
break; // Go
|
||
|
}
|
||
12 years ago
|
fprintf(stderr, "[tdebuildsycoca] Waiting for already running %s to finish.\n", appName);
|
||
15 years ago
|
|
||
|
dcopClient->setNotifications( true );
|
||
|
while (dcopClient->isApplicationRegistered(appName))
|
||
|
{
|
||
|
WaitForSignal *obj = new WaitForSignal;
|
||
15 years ago
|
obj->connect(dcopClient, TQT_SIGNAL(applicationRemoved(const TQCString &)),
|
||
|
TQT_SLOT(deleteLater()));
|
||
15 years ago
|
kapp->eventLoop()->enterLoop();
|
||
|
}
|
||
|
dcopClient->setNotifications( false );
|
||
|
}
|
||
12 years ago
|
fprintf(stderr, "[tdebuildsycoca] %s running...\n", appName);
|
||
15 years ago
|
|
||
12 years ago
|
bool chectdefiles = bGlobalDatabase || args->isSet("chectdefiles");
|
||
15 years ago
|
|
||
12 years ago
|
bool incremental = !bGlobalDatabase && args->isSet("incremental") && chectdefiles;
|
||
|
if (incremental || !chectdefiles)
|
||
15 years ago
|
{
|
||
|
KSycoca::self()->disableAutoRebuild(); // Prevent deadlock
|
||
12 years ago
|
TQString current_language = TDEGlobal::locale()->language();
|
||
12 years ago
|
TQString tdesycoca_language = KSycoca::self()->language();
|
||
|
TQ_UINT32 current_update_sig = TDEGlobal::dirs()->calcResourceHash("services", "update_tdesycoca", true);
|
||
|
TQ_UINT32 tdesycoca_update_sig = KSycoca::self()->updateSignature();
|
||
15 years ago
|
|
||
12 years ago
|
if ((current_update_sig != tdesycoca_update_sig) ||
|
||
|
(current_language != tdesycoca_language) ||
|
||
15 years ago
|
(KSycoca::self()->timeStamp() == 0))
|
||
|
{
|
||
|
incremental = false;
|
||
12 years ago
|
chectdefiles = true;
|
||
15 years ago
|
delete KSycoca::self();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
g_changeList = new TQStringList;
|
||
15 years ago
|
|
||
12 years ago
|
bool checkstamps = incremental && args->isSet("checkstamps") && chectdefiles;
|
||
14 years ago
|
TQ_UINT32 filestamp = 0;
|
||
15 years ago
|
TQStringList oldresourcedirs;
|
||
15 years ago
|
if( checkstamps && incremental )
|
||
|
{
|
||
15 years ago
|
TQString path = sycocaPath()+"stamp";
|
||
|
TQCString qPath = TQFile::encodeName(path);
|
||
15 years ago
|
cSycocaPath = qPath.data(); // Delete timestamps on crash
|
||
12 years ago
|
TQFile tdesycocastamp(path);
|
||
|
if( tdesycocastamp.open( IO_ReadOnly ))
|
||
15 years ago
|
{
|
||
12 years ago
|
TQDataStream str( &tdesycocastamp );
|
||
15 years ago
|
if (!str.atEnd())
|
||
|
str >> filestamp;
|
||
|
if (!str.atEnd())
|
||
|
{
|
||
|
str >> oldresourcedirs;
|
||
|
if( oldresourcedirs != KBuildSycoca::existingResourceDirs())
|
||
|
checkstamps = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
checkstamps = false;
|
||
|
}
|
||
|
if (!str.atEnd())
|
||
|
{
|
||
15 years ago
|
TQStringList extraResourceDirs;
|
||
15 years ago
|
str >> extraResourceDirs;
|
||
|
oldresourcedirs += extraResourceDirs;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
checkstamps = false;
|
||
|
}
|
||
|
cSycocaPath = 0;
|
||
|
}
|
||
|
|
||
14 years ago
|
newTimestamp = (TQ_UINT32) time(0);
|
||
15 years ago
|
|
||
12 years ago
|
if( chectdefiles && ( !checkstamps || !KBuildSycoca::checkTimestamps( filestamp, oldresourcedirs )))
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString qSycocaPath = TQFile::encodeName(sycocaPath());
|
||
15 years ago
|
cSycocaPath = qSycocaPath.data();
|
||
|
|
||
|
g_allEntries = 0;
|
||
|
g_ctimeDict = 0;
|
||
|
if (incremental)
|
||
|
{
|
||
12 years ago
|
tqWarning("[tdebuildsycoca] Reusing existing tdesycoca.");
|
||
15 years ago
|
KSycoca *oldSycoca = KSycoca::self();
|
||
|
KSycocaFactoryList *factories = new KSycocaFactoryList;
|
||
|
g_allEntries = new KSycocaEntryListList;
|
||
14 years ago
|
g_ctimeDict = new TQDict<TQ_UINT32>(523);
|
||
15 years ago
|
|
||
|
// Must be in same order as in KBuildSycoca::recreate()!
|
||
|
factories->append( new KServiceTypeFactory );
|
||
|
factories->append( new KServiceGroupFactory );
|
||
|
factories->append( new KServiceFactory );
|
||
|
factories->append( new KImageIOFactory );
|
||
|
factories->append( new KProtocolInfoFactory );
|
||
|
|
||
|
// For each factory
|
||
|
for (KSycocaFactory *factory = factories->first();
|
||
|
factory;
|
||
|
factory = factories->next() )
|
||
|
{
|
||
|
KSycocaEntry::List list;
|
||
|
list = factory->allEntries();
|
||
|
g_allEntries->append( list );
|
||
|
}
|
||
|
delete factories; factories = 0;
|
||
|
KCTimeInfo *ctimeInfo = new KCTimeInfo;
|
||
|
ctimeInfo->fillCTimeDict(*g_ctimeDict);
|
||
|
delete oldSycoca;
|
||
|
}
|
||
|
cSycocaPath = 0;
|
||
|
|
||
|
KBuildSycoca *sycoca= new KBuildSycoca; // Build data base
|
||
|
if (args->isSet("track"))
|
||
15 years ago
|
sycoca->setTrackId(TQString::fromLocal8Bit(args->getOption("track")));
|
||
15 years ago
|
if (!sycoca->recreate()) {
|
||
|
#ifdef KBUILDSYCOCA_GUI
|
||
|
if (!silent || showprogress)
|
||
|
progress.close();
|
||
|
#endif
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (bGlobalDatabase)
|
||
|
{
|
||
|
// These directories may have been created with 0700 permission
|
||
|
// better delete them if they are empty
|
||
12 years ago
|
TQString applnkDir = TDEGlobal::dirs()->saveLocation("apps", TQString::null, false);
|
||
15 years ago
|
::rmdir(TQFile::encodeName(applnkDir));
|
||
12 years ago
|
TQString servicetypesDir = TDEGlobal::dirs()->saveLocation("servicetypes", TQString::null, false);
|
||
15 years ago
|
::rmdir(TQFile::encodeName(servicetypesDir));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (!bGlobalDatabase)
|
||
|
{
|
||
|
// Recreate compatibility symlink
|
||
15 years ago
|
TQString oldPath = oldSycocaPath();
|
||
15 years ago
|
if (!oldPath.isEmpty())
|
||
|
{
|
||
|
KTempFile tmp;
|
||
|
if (tmp.status() == 0)
|
||
|
{
|
||
15 years ago
|
TQString tmpFile = tmp.name();
|
||
15 years ago
|
tmp.unlink();
|
||
15 years ago
|
symlink(TQFile::encodeName(sycocaPath()), TQFile::encodeName(tmpFile));
|
||
|
rename(TQFile::encodeName(tmpFile), TQFile::encodeName(oldPath));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (args->isSet("signal"))
|
||
|
{
|
||
12 years ago
|
// Notify ALL applications that have a tdesycoca object, using a broadcast
|
||
15 years ago
|
TQByteArray data;
|
||
|
TQDataStream stream(data, IO_WriteOnly);
|
||
15 years ago
|
stream << *g_changeList;
|
||
12 years ago
|
dcopClient->send( "*", "tdesycoca", "notifyDatabaseChanged(TQStringList)", data );
|
||
15 years ago
|
}
|
||
|
|
||
|
#ifdef KBUILDSYCOCA_GUI
|
||
|
if (!silent) {
|
||
|
progress.close();
|
||
12 years ago
|
KMessageBox::information(0, i18n("[tdebuildsycoca] Configuration information reloaded successfully."), capt);
|
||
15 years ago
|
}
|
||
|
#endif
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
#include "tdebuildsycoca.moc"
|