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
25 KiB
960 lines
25 KiB
15 years ago
|
/*
|
||
|
*
|
||
|
* This file is part of the KDE libraries
|
||
|
* Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
|
||
|
*
|
||
|
* $Id$
|
||
|
*
|
||
|
* 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.
|
||
|
**/
|
||
|
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include <ksimpleconfig.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kcmdlineargs.h>
|
||
|
#include <kglobal.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kaboutdata.h>
|
||
|
#include <kinstance.h>
|
||
|
#include <ktempfile.h>
|
||
|
|
||
|
static KCmdLineOptions options[] =
|
||
|
{
|
||
|
{ "debug", I18N_NOOP("Keep output results from scripts"), 0 },
|
||
|
{ "check <update-file>", I18N_NOOP("Check whether config file itself requires updating"), 0 },
|
||
|
{ "+[file]", I18N_NOOP("File to read update instructions from"), 0 },
|
||
|
KCmdLineLastOption
|
||
|
};
|
||
|
|
||
|
class KonfUpdate
|
||
|
{
|
||
|
public:
|
||
|
KonfUpdate();
|
||
|
~KonfUpdate();
|
||
14 years ago
|
TQStringList findUpdateFiles(bool dirtyOnly);
|
||
15 years ago
|
|
||
15 years ago
|
TQTextStream &log();
|
||
15 years ago
|
|
||
15 years ago
|
bool checkFile(const TQString &filename);
|
||
|
void checkGotFile(const TQString &_file, const TQString &id);
|
||
15 years ago
|
|
||
15 years ago
|
bool updateFile(const TQString &filename);
|
||
15 years ago
|
|
||
15 years ago
|
void gotId(const TQString &_id);
|
||
|
void gotFile(const TQString &_file);
|
||
|
void gotGroup(const TQString &_group);
|
||
|
void gotRemoveGroup(const TQString &_group);
|
||
|
void gotKey(const TQString &_key);
|
||
|
void gotRemoveKey(const TQString &_key);
|
||
15 years ago
|
void gotAllKeys();
|
||
|
void gotAllGroups();
|
||
15 years ago
|
void gotOptions(const TQString &_options);
|
||
|
void gotScript(const TQString &_script);
|
||
|
void gotScriptArguments(const TQString &_arguments);
|
||
15 years ago
|
void resetOptions();
|
||
|
|
||
12 years ago
|
void copyGroup(TDEConfigBase *cfg1, const TQString &grp1,
|
||
|
TDEConfigBase *cfg2, const TQString &grp2);
|
||
15 years ago
|
|
||
|
protected:
|
||
12 years ago
|
TDEConfig *config;
|
||
15 years ago
|
TQString currentFilename;
|
||
15 years ago
|
bool skip;
|
||
|
bool debug;
|
||
15 years ago
|
TQString id;
|
||
15 years ago
|
|
||
15 years ago
|
TQString oldFile;
|
||
|
TQString newFile;
|
||
|
TQString newFileName;
|
||
12 years ago
|
TDEConfig *oldConfig1; // Config to read keys from.
|
||
|
TDEConfig *oldConfig2; // Config to delete keys from.
|
||
|
TDEConfig *newConfig;
|
||
15 years ago
|
|
||
15 years ago
|
TQString oldGroup;
|
||
|
TQString newGroup;
|
||
|
TQString oldKey;
|
||
|
TQString newKey;
|
||
15 years ago
|
|
||
|
bool m_bCopy;
|
||
|
bool m_bOverwrite;
|
||
|
bool m_bUseConfigInfo;
|
||
15 years ago
|
TQString m_arguments;
|
||
|
TQTextStream *m_textStream;
|
||
|
TQFile *m_file;
|
||
|
TQString m_line;
|
||
15 years ago
|
int m_lineCount;
|
||
|
};
|
||
|
|
||
|
KonfUpdate::KonfUpdate()
|
||
|
: m_textStream(0), m_file(0)
|
||
|
{
|
||
|
bool updateAll = false;
|
||
|
oldConfig1 = 0;
|
||
|
oldConfig2 = 0;
|
||
|
newConfig = 0;
|
||
|
|
||
12 years ago
|
config = new TDEConfig("tdeconf_updaterc");
|
||
15 years ago
|
|
||
15 years ago
|
TQStringList updateFiles;
|
||
12 years ago
|
TDECmdLineArgs *args=TDECmdLineArgs::parsedArgs();
|
||
15 years ago
|
|
||
|
debug = args->isSet("debug");
|
||
|
|
||
|
m_bUseConfigInfo = false;
|
||
|
if (args->isSet("check"))
|
||
|
{
|
||
|
m_bUseConfigInfo = true;
|
||
12 years ago
|
TQString file = locate("data", "tdeconf_update/"+TQFile::decodeName(args->getOption("check")));
|
||
15 years ago
|
if (file.isEmpty())
|
||
|
{
|
||
13 years ago
|
tqWarning("File '%s' not found.", args->getOption("check").data());
|
||
15 years ago
|
log() << "File '" << TQFile::decodeName(args->getOption("check")) << "' passed on command line not found" << endl;
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
updateFiles.append(file);
|
||
|
}
|
||
|
else if (args->count())
|
||
|
{
|
||
|
for(int i = 0; i < args->count(); i++)
|
||
|
{
|
||
|
KURL url = args->url(i);
|
||
|
if (!url.isLocalFile())
|
||
12 years ago
|
TDECmdLineArgs::usage(i18n("Only local files are supported."));
|
||
15 years ago
|
updateFiles.append(url.path());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (config->readBoolEntry("autoUpdateDisabled", false))
|
||
|
return;
|
||
14 years ago
|
updateFiles = findUpdateFiles(true);
|
||
15 years ago
|
updateAll = true;
|
||
|
}
|
||
|
|
||
15 years ago
|
for(TQStringList::ConstIterator it = updateFiles.begin();
|
||
15 years ago
|
it != updateFiles.end();
|
||
|
++it)
|
||
|
{
|
||
15 years ago
|
TQString file = *it;
|
||
15 years ago
|
updateFile(file);
|
||
|
}
|
||
|
|
||
15 years ago
|
config->setGroup(TQString::null);
|
||
15 years ago
|
if (updateAll && !config->readBoolEntry("updateInfoAdded", false))
|
||
|
{
|
||
|
config->writeEntry("updateInfoAdded", true);
|
||
14 years ago
|
updateFiles = findUpdateFiles(false);
|
||
15 years ago
|
|
||
15 years ago
|
for(TQStringList::ConstIterator it = updateFiles.begin();
|
||
15 years ago
|
it != updateFiles.end();
|
||
|
++it)
|
||
|
{
|
||
15 years ago
|
TQString file = *it;
|
||
15 years ago
|
checkFile(file);
|
||
|
}
|
||
|
updateFiles.clear();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
KonfUpdate::~KonfUpdate()
|
||
|
{
|
||
|
delete config;
|
||
|
delete m_file;
|
||
|
delete m_textStream;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQTextStream &
|
||
15 years ago
|
KonfUpdate::log()
|
||
|
{
|
||
|
if (!m_textStream)
|
||
|
{
|
||
12 years ago
|
TQString file = locateLocal("data", "tdeconf_update/log/update.log");
|
||
15 years ago
|
m_file = new TQFile(file);
|
||
15 years ago
|
if (m_file->open(IO_WriteOnly | IO_Append))
|
||
|
{
|
||
15 years ago
|
m_textStream = new TQTextStream(m_file);
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// Error
|
||
15 years ago
|
m_textStream = new TQTextStream(stderr, IO_WriteOnly);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
(*m_textStream) << TQDateTime::currentDateTime().toString( Qt::ISODate ) << " ";
|
||
15 years ago
|
|
||
|
return *m_textStream;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQStringList KonfUpdate::findUpdateFiles(bool dirtyOnly)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList result;
|
||
12 years ago
|
TQStringList list = TDEGlobal::dirs()->findAllResources("data", "tdeconf_update/*.upd", false, true);
|
||
15 years ago
|
for(TQStringList::ConstIterator it = list.begin();
|
||
15 years ago
|
it != list.end();
|
||
|
++it)
|
||
|
{
|
||
15 years ago
|
TQString file = *it;
|
||
15 years ago
|
struct stat buff;
|
||
15 years ago
|
if (stat( TQFile::encodeName(file), &buff) == 0)
|
||
15 years ago
|
{
|
||
13 years ago
|
int i = file.findRev('/');
|
||
15 years ago
|
if (i != -1)
|
||
|
file = file.mid(i+1);
|
||
|
config->setGroup(file);
|
||
|
time_t ctime = config->readUnsignedLongNumEntry("ctime");
|
||
|
time_t mtime = config->readUnsignedLongNumEntry("mtime");
|
||
|
if (!dirtyOnly ||
|
||
|
(ctime != buff.st_ctime) || (mtime != buff.st_mtime))
|
||
|
{
|
||
|
result.append(*it);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KonfUpdate::checkFile(const TQString &filename)
|
||
15 years ago
|
{
|
||
|
currentFilename = filename;
|
||
13 years ago
|
int i = currentFilename.findRev('/');
|
||
15 years ago
|
if (i != -1)
|
||
|
currentFilename = currentFilename.mid(i+1);
|
||
|
skip = true;
|
||
15 years ago
|
TQFile file(filename);
|
||
15 years ago
|
if (!file.open(IO_ReadOnly))
|
||
|
return false;
|
||
|
|
||
15 years ago
|
TQTextStream ts(&file);
|
||
|
ts.setEncoding(TQTextStream::Latin1);
|
||
15 years ago
|
int lineCount = 0;
|
||
|
resetOptions();
|
||
15 years ago
|
TQString id;
|
||
15 years ago
|
while(!ts.atEnd())
|
||
|
{
|
||
15 years ago
|
TQString line = ts.readLine().stripWhiteSpace();
|
||
15 years ago
|
lineCount++;
|
||
|
if (line.isEmpty() || (line[0] == '#'))
|
||
|
continue;
|
||
|
if (line.startsWith("Id="))
|
||
|
id = currentFilename+":"+line.mid(3);
|
||
|
else if (line.startsWith("File="))
|
||
|
checkGotFile(line.mid(5), id);
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::checkGotFile(const TQString &_file, const TQString &id)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString file;
|
||
13 years ago
|
int i = _file.find(',');
|
||
15 years ago
|
if (i == -1)
|
||
|
{
|
||
|
file = _file.stripWhiteSpace();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
file = _file.mid(i+1).stripWhiteSpace();
|
||
|
}
|
||
|
|
||
13 years ago
|
// tqDebug("File %s, id %s", file.latin1(), id.latin1());
|
||
15 years ago
|
|
||
|
KSimpleConfig cfg(file);
|
||
|
cfg.setGroup("$Version");
|
||
15 years ago
|
TQStringList ids = cfg.readListEntry("update_info");
|
||
13 years ago
|
if (ids.contains(id))
|
||
15 years ago
|
return;
|
||
|
ids.append(id);
|
||
|
cfg.writeEntry("update_info", ids);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Syntax:
|
||
|
* # Comment
|
||
|
* Id=id
|
||
|
* File=oldfile[,newfile]
|
||
|
* AllGroups
|
||
|
* Group=oldgroup[,newgroup]
|
||
|
* RemoveGroup=oldgroup
|
||
|
* Options=[copy,][overwrite,]
|
||
|
* Key=oldkey[,newkey]
|
||
|
* RemoveKey=ldkey
|
||
|
* AllKeys
|
||
|
* Keys= [Options](AllKeys|(Key|RemoveKey)*)
|
||
|
* ScriptArguments=arguments
|
||
|
* Script=scriptfile[,interpreter]
|
||
|
*
|
||
|
* Sequence:
|
||
|
* (Id,(File(Group,Keys)*)*)*
|
||
|
**/
|
||
15 years ago
|
bool KonfUpdate::updateFile(const TQString &filename)
|
||
15 years ago
|
{
|
||
|
currentFilename = filename;
|
||
13 years ago
|
int i = currentFilename.findRev('/');
|
||
15 years ago
|
if (i != -1)
|
||
|
currentFilename = currentFilename.mid(i+1);
|
||
|
skip = true;
|
||
15 years ago
|
TQFile file(filename);
|
||
15 years ago
|
if (!file.open(IO_ReadOnly))
|
||
|
return false;
|
||
|
|
||
|
log() << "Checking update-file '" << filename << "' for new updates" << endl;
|
||
|
|
||
15 years ago
|
TQTextStream ts(&file);
|
||
|
ts.setEncoding(TQTextStream::Latin1);
|
||
15 years ago
|
m_lineCount = 0;
|
||
|
resetOptions();
|
||
|
while(!ts.atEnd())
|
||
|
{
|
||
|
m_line = ts.readLine().stripWhiteSpace();
|
||
|
m_lineCount++;
|
||
|
if (m_line.isEmpty() || (m_line[0] == '#'))
|
||
|
continue;
|
||
|
if (m_line.startsWith("Id="))
|
||
|
gotId(m_line.mid(3));
|
||
|
else if (skip)
|
||
|
continue;
|
||
|
else if (m_line.startsWith("Options="))
|
||
|
gotOptions(m_line.mid(8));
|
||
|
else if (m_line.startsWith("File="))
|
||
|
gotFile(m_line.mid(5));
|
||
|
else if (m_line.startsWith("Group="))
|
||
|
gotGroup(m_line.mid(6));
|
||
|
else if (m_line.startsWith("RemoveGroup="))
|
||
|
{
|
||
|
gotRemoveGroup(m_line.mid(12));
|
||
|
resetOptions();
|
||
|
}
|
||
|
else if (m_line.startsWith("Script="))
|
||
|
{
|
||
|
gotScript(m_line.mid(7));
|
||
|
resetOptions();
|
||
|
}
|
||
|
else if (m_line.startsWith("ScriptArguments="))
|
||
|
gotScriptArguments(m_line.mid(16));
|
||
|
else if (m_line.startsWith("Key="))
|
||
|
{
|
||
|
gotKey(m_line.mid(4));
|
||
|
resetOptions();
|
||
|
}
|
||
|
else if (m_line.startsWith("RemoveKey="))
|
||
|
{
|
||
|
gotRemoveKey(m_line.mid(10));
|
||
|
resetOptions();
|
||
|
}
|
||
|
else if (m_line == "AllKeys")
|
||
|
{
|
||
|
gotAllKeys();
|
||
|
resetOptions();
|
||
|
}
|
||
|
else if (m_line == "AllGroups")
|
||
|
{
|
||
|
gotAllGroups();
|
||
|
resetOptions();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
log() << currentFilename << ": parse error in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
}
|
||
|
}
|
||
|
// Flush.
|
||
15 years ago
|
gotId(TQString::null);
|
||
15 years ago
|
|
||
|
struct stat buff;
|
||
15 years ago
|
stat( TQFile::encodeName(filename), &buff);
|
||
15 years ago
|
config->setGroup(currentFilename);
|
||
|
config->writeEntry("ctime", buff.st_ctime);
|
||
|
config->writeEntry("mtime", buff.st_mtime);
|
||
|
config->sync();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotId(const TQString &_id)
|
||
15 years ago
|
{
|
||
|
if (!id.isEmpty() && !skip)
|
||
|
{
|
||
|
config->setGroup(currentFilename);
|
||
15 years ago
|
TQStringList ids = config->readListEntry("done");
|
||
13 years ago
|
if (!ids.contains(id))
|
||
15 years ago
|
{
|
||
|
ids.append(id);
|
||
|
config->writeEntry("done", ids);
|
||
|
config->sync();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Flush pending changes
|
||
15 years ago
|
gotFile(TQString::null);
|
||
15 years ago
|
|
||
|
config->setGroup(currentFilename);
|
||
15 years ago
|
TQStringList ids = config->readListEntry("done");
|
||
15 years ago
|
if (!_id.isEmpty())
|
||
|
{
|
||
13 years ago
|
if (ids.contains(_id))
|
||
15 years ago
|
{
|
||
13 years ago
|
//tqDebug("Id '%s' was already in done-list", _id.latin1());
|
||
15 years ago
|
if (!m_bUseConfigInfo)
|
||
|
{
|
||
|
skip = true;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
skip = false;
|
||
|
id = _id;
|
||
|
if (m_bUseConfigInfo)
|
||
|
log() << currentFilename << ": Checking update '" << _id << "'" << endl;
|
||
|
else
|
||
|
log() << currentFilename << ": Found new update '" << _id << "'" << endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotFile(const TQString &_file)
|
||
15 years ago
|
{
|
||
|
// Reset group
|
||
15 years ago
|
gotGroup(TQString::null);
|
||
15 years ago
|
|
||
|
if (!oldFile.isEmpty())
|
||
|
{
|
||
|
// Close old file.
|
||
|
delete oldConfig1;
|
||
|
oldConfig1 = 0;
|
||
|
|
||
|
oldConfig2->setGroup("$Version");
|
||
15 years ago
|
TQStringList ids = oldConfig2->readListEntry("update_info");
|
||
|
TQString cfg_id = currentFilename + ":" + id;
|
||
13 years ago
|
if (!ids.contains(cfg_id) && !skip)
|
||
15 years ago
|
{
|
||
|
ids.append(cfg_id);
|
||
|
oldConfig2->writeEntry("update_info", ids);
|
||
|
}
|
||
|
oldConfig2->sync();
|
||
|
delete oldConfig2;
|
||
|
oldConfig2 = 0;
|
||
|
|
||
15 years ago
|
TQString file = locateLocal("config", oldFile);
|
||
15 years ago
|
struct stat s_buf;
|
||
15 years ago
|
if (stat(TQFile::encodeName(file), &s_buf) == 0)
|
||
15 years ago
|
{
|
||
|
if (s_buf.st_size == 0)
|
||
|
{
|
||
|
// Delete empty file.
|
||
15 years ago
|
unlink(TQFile::encodeName(file));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
oldFile = TQString::null;
|
||
15 years ago
|
}
|
||
|
if (!newFile.isEmpty())
|
||
|
{
|
||
|
// Close new file.
|
||
|
newConfig->setGroup("$Version");
|
||
15 years ago
|
TQStringList ids = newConfig->readListEntry("update_info");
|
||
|
TQString cfg_id = currentFilename + ":" + id;
|
||
13 years ago
|
if (!ids.contains(cfg_id) && !skip)
|
||
15 years ago
|
{
|
||
|
ids.append(cfg_id);
|
||
|
newConfig->writeEntry("update_info", ids);
|
||
|
}
|
||
|
newConfig->sync();
|
||
|
delete newConfig;
|
||
|
newConfig = 0;
|
||
|
|
||
15 years ago
|
newFile = TQString::null;
|
||
15 years ago
|
}
|
||
|
newConfig = 0;
|
||
|
|
||
13 years ago
|
int i = _file.find(',');
|
||
15 years ago
|
if (i == -1)
|
||
|
{
|
||
|
oldFile = _file.stripWhiteSpace();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
oldFile = _file.left(i).stripWhiteSpace();
|
||
|
newFile = _file.mid(i+1).stripWhiteSpace();
|
||
|
if (oldFile == newFile)
|
||
15 years ago
|
newFile = TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
if (!oldFile.isEmpty())
|
||
|
{
|
||
12 years ago
|
oldConfig2 = new TDEConfig(oldFile, false, false);
|
||
15 years ago
|
TQString cfg_id = currentFilename + ":" + id;
|
||
15 years ago
|
oldConfig2->setGroup("$Version");
|
||
15 years ago
|
TQStringList ids = oldConfig2->readListEntry("update_info");
|
||
13 years ago
|
if (ids.contains(cfg_id))
|
||
15 years ago
|
{
|
||
|
skip = true;
|
||
15 years ago
|
newFile = TQString::null;
|
||
15 years ago
|
log() << currentFilename << ": Skipping update '" << id << "'" << endl;
|
||
|
}
|
||
|
|
||
|
if (!newFile.isEmpty())
|
||
|
{
|
||
12 years ago
|
newConfig = new TDEConfig(newFile, false, false);
|
||
15 years ago
|
newConfig->setGroup("$Version");
|
||
|
ids = newConfig->readListEntry("update_info");
|
||
13 years ago
|
if (ids.contains(cfg_id))
|
||
15 years ago
|
{
|
||
|
skip = true;
|
||
|
log() << currentFilename << ": Skipping update '" << id << "'" << endl;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
newConfig = oldConfig2;
|
||
|
}
|
||
|
|
||
12 years ago
|
oldConfig1 = new TDEConfig(oldFile, true, false);
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
15 years ago
|
newFile = TQString::null;
|
||
15 years ago
|
}
|
||
|
newFileName = newFile;
|
||
|
if (newFileName.isEmpty())
|
||
|
newFileName = oldFile;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotGroup(const TQString &_group)
|
||
15 years ago
|
{
|
||
13 years ago
|
int i = _group.find(',');
|
||
15 years ago
|
if (i == -1)
|
||
|
{
|
||
|
oldGroup = _group.stripWhiteSpace();
|
||
|
newGroup = oldGroup;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
oldGroup = _group.left(i).stripWhiteSpace();
|
||
|
newGroup = _group.mid(i+1).stripWhiteSpace();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotRemoveGroup(const TQString &_group)
|
||
15 years ago
|
{
|
||
|
oldGroup = _group.stripWhiteSpace();
|
||
|
|
||
|
if (!oldConfig1)
|
||
|
{
|
||
|
log() << currentFilename << ": !! RemoveGroup without previous File specification in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!oldConfig1->hasGroup(oldGroup))
|
||
|
return;
|
||
|
// Delete group.
|
||
|
oldConfig2->deleteGroup(oldGroup, true);
|
||
|
log() << currentFilename << ": RemoveGroup removes group " << oldFile << ":" << oldGroup << endl;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotKey(const TQString &_key)
|
||
15 years ago
|
{
|
||
13 years ago
|
int i = _key.find(',');
|
||
15 years ago
|
if (i == -1)
|
||
|
{
|
||
|
oldKey = _key.stripWhiteSpace();
|
||
|
newKey = oldKey;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
oldKey = _key.left(i).stripWhiteSpace();
|
||
|
newKey = _key.mid(i+1).stripWhiteSpace();
|
||
|
}
|
||
|
|
||
|
if (oldKey.isEmpty() || newKey.isEmpty())
|
||
|
{
|
||
|
log() << currentFilename << ": !! Key specifies invalid key in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
if (!oldConfig1)
|
||
|
{
|
||
|
log() << currentFilename << ": !! Key without previous File specification in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
oldConfig1->setGroup(oldGroup);
|
||
|
if (!oldConfig1->hasKey(oldKey))
|
||
|
return;
|
||
15 years ago
|
TQString value = oldConfig1->readEntry(oldKey);
|
||
15 years ago
|
newConfig->setGroup(newGroup);
|
||
|
if (!m_bOverwrite && newConfig->hasKey(newKey))
|
||
|
{
|
||
|
log() << currentFilename << ": Skipping " << newFileName << ":" << newGroup << ":" << newKey << ", already exists."<< endl;
|
||
|
return;
|
||
|
}
|
||
|
log() << currentFilename << ": Updating " << newFileName << ":" << newGroup << ":" << newKey << " to '" << value << "'" << endl;
|
||
|
newConfig->writeEntry(newKey, value);
|
||
|
|
||
|
if (m_bCopy)
|
||
|
return; // Done.
|
||
|
|
||
|
// Delete old entry
|
||
|
if ((oldConfig2 == newConfig) &&
|
||
|
(oldGroup == newGroup) &&
|
||
|
(oldKey == newKey))
|
||
|
return; // Don't delete!
|
||
|
oldConfig2->setGroup(oldGroup);
|
||
|
oldConfig2->deleteEntry(oldKey, false);
|
||
|
log() << currentFilename << ": Removing " << oldFile << ":" << oldGroup << ":" << oldKey << ", moved." << endl;
|
||
|
if (oldConfig2->deleteGroup(oldGroup, false)) { // Delete group if empty.
|
||
|
log() << currentFilename << ": Removing empty group " << oldFile << ":" << oldGroup << endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotRemoveKey(const TQString &_key)
|
||
15 years ago
|
{
|
||
|
oldKey = _key.stripWhiteSpace();
|
||
|
|
||
|
if (oldKey.isEmpty())
|
||
|
{
|
||
|
log() << currentFilename << ": !! RemoveKey specifies invalid key in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!oldConfig1)
|
||
|
{
|
||
|
log() << currentFilename << ": !! Key without previous File specification in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
oldConfig1->setGroup(oldGroup);
|
||
|
if (!oldConfig1->hasKey(oldKey))
|
||
|
return;
|
||
|
log() << currentFilename << ": RemoveKey removes " << oldFile << ":" << oldGroup << ":" << oldKey << endl;
|
||
|
|
||
|
// Delete old entry
|
||
|
oldConfig2->setGroup(oldGroup);
|
||
|
oldConfig2->deleteEntry(oldKey, false);
|
||
|
if (oldConfig2->deleteGroup(oldGroup, false)) { // Delete group if empty.
|
||
|
log() << currentFilename << ": Removing empty group " << oldFile << ":" << oldGroup << endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KonfUpdate::gotAllKeys()
|
||
|
{
|
||
|
if (!oldConfig1)
|
||
|
{
|
||
|
log() << currentFilename << ": !! AllKeys without previous File specification in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQMap<TQString, TQString> list = oldConfig1->entryMap(oldGroup);
|
||
|
for(TQMap<TQString, TQString>::Iterator it = list.begin();
|
||
15 years ago
|
it != list.end(); ++it)
|
||
|
{
|
||
|
gotKey(it.key());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KonfUpdate::gotAllGroups()
|
||
|
{
|
||
|
if (!oldConfig1)
|
||
|
{
|
||
|
log() << currentFilename << ": !! AllGroups without previous File specification in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList allGroups = oldConfig1->groupList();
|
||
|
for(TQStringList::ConstIterator it = allGroups.begin();
|
||
15 years ago
|
it != allGroups.end(); ++it)
|
||
|
{
|
||
|
oldGroup = *it;
|
||
|
newGroup = oldGroup;
|
||
|
gotAllKeys();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotOptions(const TQString &_options)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList options = TQStringList::split(',', _options);
|
||
|
for(TQStringList::ConstIterator it = options.begin();
|
||
15 years ago
|
it != options.end();
|
||
|
++it)
|
||
|
{
|
||
|
if ( (*it).lower().stripWhiteSpace() == "copy")
|
||
|
m_bCopy = true;
|
||
|
|
||
|
if ( (*it).lower().stripWhiteSpace() == "overwrite")
|
||
|
m_bOverwrite = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void KonfUpdate::copyGroup(TDEConfigBase *cfg1, const TQString &grp1,
|
||
|
TDEConfigBase *cfg2, const TQString &grp2)
|
||
15 years ago
|
{
|
||
|
cfg1->setGroup(grp1);
|
||
|
cfg2->setGroup(grp2);
|
||
15 years ago
|
TQMap<TQString, TQString> list = cfg1->entryMap(grp1);
|
||
|
for(TQMap<TQString, TQString>::Iterator it = list.begin();
|
||
15 years ago
|
it != list.end(); ++it)
|
||
|
{
|
||
|
cfg2->writeEntry(it.key(), cfg1->readEntry(it.key()));
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotScriptArguments(const TQString &_arguments)
|
||
15 years ago
|
{
|
||
|
m_arguments = _arguments;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KonfUpdate::gotScript(const TQString &_script)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString script, interpreter;
|
||
13 years ago
|
int i = _script.find(',');
|
||
15 years ago
|
if (i == -1)
|
||
|
{
|
||
|
script = _script.stripWhiteSpace();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
script = _script.left(i).stripWhiteSpace();
|
||
|
interpreter = _script.mid(i+1).stripWhiteSpace();
|
||
|
}
|
||
|
|
||
|
|
||
|
if (script.isEmpty())
|
||
|
{
|
||
|
log() << currentFilename << ": !! Script fails to specifiy filename in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
skip = true;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
12 years ago
|
TQString path = locate("data","tdeconf_update/"+script);
|
||
15 years ago
|
if (path.isEmpty())
|
||
|
{
|
||
|
if (interpreter.isEmpty())
|
||
12 years ago
|
path = locate("lib", "tdeconf_update_bin/"+script);
|
||
15 years ago
|
|
||
|
if (path.isEmpty())
|
||
|
{
|
||
|
log() << currentFilename << ": !! Script '" << script << "' not found in line " << m_lineCount << " : '" << m_line << "'" << endl;
|
||
|
skip = true;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if( !m_arguments.isNull())
|
||
|
log() << currentFilename << ": Running script '" << script << "' with arguments '" << m_arguments << "'" << endl;
|
||
|
else
|
||
|
log() << currentFilename << ": Running script '" << script << "'" << endl;
|
||
|
|
||
15 years ago
|
TQString cmd;
|
||
15 years ago
|
if (interpreter.isEmpty())
|
||
|
cmd = path;
|
||
|
else
|
||
|
cmd = interpreter + " " + path;
|
||
|
|
||
|
if( !m_arguments.isNull())
|
||
|
{
|
||
|
cmd += ' ';
|
||
|
cmd += m_arguments;
|
||
|
}
|
||
|
|
||
|
KTempFile tmp1;
|
||
|
tmp1.setAutoDelete(true);
|
||
|
KTempFile tmp2;
|
||
|
tmp2.setAutoDelete(true);
|
||
|
KTempFile tmp3;
|
||
|
tmp3.setAutoDelete(true);
|
||
|
|
||
|
int result;
|
||
|
if (oldConfig1)
|
||
|
{
|
||
|
if (debug)
|
||
|
{
|
||
|
tmp1.setAutoDelete(false);
|
||
|
log() << "Script input stored in " << tmp1.name() << endl;
|
||
|
}
|
||
|
KSimpleConfig cfg(tmp1.name());
|
||
|
|
||
|
if (oldGroup.isEmpty())
|
||
|
{
|
||
|
// Write all entries to tmpFile;
|
||
15 years ago
|
TQStringList grpList = oldConfig1->groupList();
|
||
|
for(TQStringList::ConstIterator it = grpList.begin();
|
||
15 years ago
|
it != grpList.end();
|
||
|
++it)
|
||
|
{
|
||
|
copyGroup(oldConfig1, *it, &cfg, *it);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
15 years ago
|
copyGroup(oldConfig1, oldGroup, &cfg, TQString::null);
|
||
15 years ago
|
}
|
||
|
cfg.sync();
|
||
15 years ago
|
result = system(TQFile::encodeName(TQString("%1 < %2 > %3 2> %4").arg(cmd, tmp1.name(), tmp2.name(), tmp3.name())));
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// No config file
|
||
15 years ago
|
result = system(TQFile::encodeName(TQString("%1 2> %2").arg(cmd, tmp3.name())));
|
||
15 years ago
|
}
|
||
|
|
||
|
// Copy script stderr to log file
|
||
|
{
|
||
15 years ago
|
TQFile output(tmp3.name());
|
||
15 years ago
|
if (output.open(IO_ReadOnly))
|
||
|
{
|
||
15 years ago
|
TQTextStream ts( &output );
|
||
|
ts.setEncoding(TQTextStream::UnicodeUTF8);
|
||
15 years ago
|
while(!ts.atEnd())
|
||
|
{
|
||
15 years ago
|
TQString line = ts.readLine();
|
||
15 years ago
|
log() << "[Script] " << line << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (result)
|
||
|
{
|
||
|
log() << currentFilename << ": !! An error occured while running '" << cmd << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!oldConfig1)
|
||
|
return; // Nothing to merge
|
||
|
|
||
|
if (debug)
|
||
|
{
|
||
|
tmp2.setAutoDelete(false);
|
||
|
log() << "Script output stored in " << tmp2.name() << endl;
|
||
|
}
|
||
|
|
||
|
// Deleting old entries
|
||
|
{
|
||
15 years ago
|
TQString group = oldGroup;
|
||
|
TQFile output(tmp2.name());
|
||
15 years ago
|
if (output.open(IO_ReadOnly))
|
||
|
{
|
||
15 years ago
|
TQTextStream ts( &output );
|
||
|
ts.setEncoding(TQTextStream::UnicodeUTF8);
|
||
15 years ago
|
while(!ts.atEnd())
|
||
|
{
|
||
15 years ago
|
TQString line = ts.readLine();
|
||
15 years ago
|
if (line.startsWith("["))
|
||
|
{
|
||
13 years ago
|
int j = line.find(']')+1;
|
||
15 years ago
|
if (j > 0)
|
||
|
group = line.mid(1, j-2);
|
||
|
}
|
||
|
else if (line.startsWith("# DELETE "))
|
||
|
{
|
||
15 years ago
|
TQString key = line.mid(9);
|
||
15 years ago
|
if (key[0] == '[')
|
||
|
{
|
||
13 years ago
|
int j = key.find(']')+1;
|
||
15 years ago
|
if (j > 0)
|
||
|
{
|
||
|
group = key.mid(1,j-2);
|
||
|
key = key.mid(j);
|
||
|
}
|
||
|
}
|
||
|
oldConfig2->setGroup(group);
|
||
|
oldConfig2->deleteEntry(key, false);
|
||
|
log() << currentFilename << ": Script removes " << oldFile << ":" << group << ":" << key << endl;
|
||
|
if (oldConfig2->deleteGroup(group, false)) { // Delete group if empty.
|
||
|
log() << currentFilename << ": Removing empty group " << oldFile << ":" << group << endl;
|
||
|
}
|
||
|
}
|
||
|
else if (line.startsWith("# DELETEGROUP"))
|
||
|
{
|
||
15 years ago
|
TQString key = line.mid(13).stripWhiteSpace();
|
||
15 years ago
|
if (key[0] == '[')
|
||
|
{
|
||
13 years ago
|
int j = key.find(']')+1;
|
||
15 years ago
|
if (j > 0)
|
||
|
{
|
||
|
group = key.mid(1,j-2);
|
||
|
}
|
||
|
}
|
||
|
if (oldConfig2->deleteGroup(group, true)) { // Delete group
|
||
|
log() << currentFilename << ": Script removes group " << oldFile << ":" << group << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Merging in new entries.
|
||
|
m_bCopy = true;
|
||
|
{
|
||
12 years ago
|
TDEConfig *saveOldConfig1 = oldConfig1;
|
||
15 years ago
|
TQString saveOldGroup = oldGroup;
|
||
|
TQString saveNewGroup = newGroup;
|
||
12 years ago
|
oldConfig1 = new TDEConfig(tmp2.name(), true, false);
|
||
15 years ago
|
|
||
|
// For all groups...
|
||
15 years ago
|
TQStringList grpList = oldConfig1->groupList();
|
||
|
for(TQStringList::ConstIterator it = grpList.begin();
|
||
15 years ago
|
it != grpList.end();
|
||
|
++it)
|
||
|
{
|
||
|
oldGroup = *it;
|
||
|
if (oldGroup != "<default>")
|
||
|
{
|
||
|
newGroup = oldGroup;
|
||
|
}
|
||
|
gotAllKeys(); // Copy all keys
|
||
|
}
|
||
|
delete oldConfig1;
|
||
|
oldConfig1 = saveOldConfig1;
|
||
|
oldGroup = saveOldGroup;
|
||
|
newGroup = saveNewGroup;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KonfUpdate::resetOptions()
|
||
|
{
|
||
|
m_bCopy = false;
|
||
|
m_bOverwrite = false;
|
||
15 years ago
|
m_arguments = TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
|
||
|
{
|
||
12 years ago
|
TDEAboutData aboutData("tdeconf_update", I18N_NOOP("KConf Update"),
|
||
15 years ago
|
"1.0.2",
|
||
13 years ago
|
I18N_NOOP("TDE Tool for updating user configuration files"),
|
||
12 years ago
|
TDEAboutData::License_GPL,
|
||
15 years ago
|
"(c) 2001, Waldo Bastian");
|
||
|
|
||
|
aboutData.addAuthor("Waldo Bastian", 0, "bastian@kde.org");
|
||
|
|
||
12 years ago
|
TDECmdLineArgs::init(argc, argv, &aboutData);
|
||
|
TDECmdLineArgs::addCmdLineOptions(options);
|
||
15 years ago
|
|
||
12 years ago
|
TDEInstance instance(&aboutData);
|
||
15 years ago
|
|
||
|
KonfUpdate konfUpdate;
|
||
|
|
||
|
return 0;
|
||
|
}
|