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.
290 lines
7.4 KiB
290 lines
7.4 KiB
15 years ago
|
/*
|
||
13 years ago
|
This file is part of libtdepim.
|
||
15 years ago
|
|
||
|
Copyright (c) 2003 Cornelius Schumacher <schumacher@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 as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
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.
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include "tdeconfigpropagator.h"
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
|
#include <tdeconfigskeleton.h>
|
||
15 years ago
|
#include <kstandarddirs.h>
|
||
|
#include <kstringhandler.h>
|
||
|
#include <klocale.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqstringlist.h>
|
||
15 years ago
|
|
||
12 years ago
|
TDEConfigPropagator::Change::~Change()
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::ChangeConfig::ChangeConfig()
|
||
|
: TDEConfigPropagator::Change( i18n("Change Config Value") ),
|
||
15 years ago
|
hideValue( false )
|
||
|
{
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDEConfigPropagator::ChangeConfig::arg1() const
|
||
15 years ago
|
{
|
||
|
return file + "/" + group + "/" + name;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDEConfigPropagator::ChangeConfig::arg2() const
|
||
15 years ago
|
{
|
||
|
if ( hideValue ) return "*";
|
||
|
else return value;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::ChangeConfig::apply()
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfig cfg( file );
|
||
15 years ago
|
cfg.setGroup( group );
|
||
|
cfg.writeEntry( name, value );
|
||
|
|
||
|
cfg.sync();
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::TDEConfigPropagator()
|
||
15 years ago
|
: mSkeleton( 0 )
|
||
|
{
|
||
|
init();
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::TDEConfigPropagator( TDEConfigSkeleton *skeleton,
|
||
15 years ago
|
const TQString &kcfgFile )
|
||
15 years ago
|
: mSkeleton( skeleton ), mKcfgFile( kcfgFile )
|
||
|
{
|
||
|
init();
|
||
|
|
||
|
readKcfgFile();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::init()
|
||
15 years ago
|
{
|
||
|
mChanges.setAutoDelete( true );
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::readKcfgFile()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString filename = locate( "kcfg", mKcfgFile );
|
||
15 years ago
|
if ( filename.isEmpty() ) {
|
||
|
kdError() << "Unable to find kcfg file '" << mKcfgFile << "'" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQFile input( filename );
|
||
|
TQDomDocument doc;
|
||
|
TQString errorMsg;
|
||
15 years ago
|
int errorRow;
|
||
|
int errorCol;
|
||
|
if ( !doc.setContent( &input, &errorMsg, &errorRow, &errorCol ) ) {
|
||
|
kdError() << "Parse error in " << mKcfgFile << ", line " << errorRow << ", col " << errorCol << ": " << errorMsg << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDomElement cfgElement = doc.documentElement();
|
||
15 years ago
|
|
||
|
if ( cfgElement.isNull() ) {
|
||
|
kdError() << "No document in kcfg file" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mRules.clear();
|
||
|
|
||
15 years ago
|
TQDomNode n;
|
||
15 years ago
|
for ( n = cfgElement.firstChild(); !n.isNull(); n = n.nextSibling() ) {
|
||
15 years ago
|
TQDomElement e = n.toElement();
|
||
15 years ago
|
|
||
15 years ago
|
TQString tag = e.tagName();
|
||
15 years ago
|
|
||
|
if ( tag == "propagation" ) {
|
||
|
Rule rule = parsePropagation( e );
|
||
|
mRules.append( rule );
|
||
|
} else if ( tag == "condition" ) {
|
||
|
Condition condition = parseCondition( e );
|
||
15 years ago
|
TQDomNode n2;
|
||
15 years ago
|
for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
|
||
15 years ago
|
TQDomElement e2 = n2.toElement();
|
||
15 years ago
|
if ( e2.tagName() == "propagation" ) {
|
||
|
Rule rule = parsePropagation( e2 );
|
||
|
rule.condition = condition;
|
||
|
mRules.append( rule );
|
||
|
} else {
|
||
|
kdError() << "Unknow tag: " << e2.tagName() << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::Rule TDEConfigPropagator::parsePropagation( const TQDomElement &e )
|
||
15 years ago
|
{
|
||
|
Rule r;
|
||
|
|
||
15 years ago
|
TQString source = e.attribute( "source" );
|
||
15 years ago
|
parseConfigEntryPath( source, r.sourceFile, r.sourceGroup, r.sourceEntry );
|
||
|
|
||
15 years ago
|
TQString target = e.attribute( "target" );
|
||
15 years ago
|
parseConfigEntryPath( target, r.targetFile, r.targetGroup, r.targetEntry );
|
||
|
|
||
|
r.hideValue = e.hasAttribute( "hidevalue" ) &&
|
||
|
e.attribute( "hidevalue" ) == "true";
|
||
|
|
||
|
return r;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::parseConfigEntryPath( const TQString &path,
|
||
15 years ago
|
TQString &file,
|
||
|
TQString &group,
|
||
|
TQString &entry )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList p = TQStringList::split( "/", path );
|
||
15 years ago
|
|
||
|
if ( p.count() != 3 ) {
|
||
|
kdError() << "Path has to be of form file/group/entry" << endl;
|
||
14 years ago
|
file = TQString();
|
||
|
group = TQString();
|
||
|
entry = TQString();
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
file = p[ 0 ];
|
||
|
group = p[ 1 ];
|
||
|
entry = p[ 2 ];
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::Condition TDEConfigPropagator::parseCondition( const TQDomElement &e )
|
||
15 years ago
|
{
|
||
|
Condition c;
|
||
|
|
||
15 years ago
|
TQString key = e.attribute( "key" );
|
||
15 years ago
|
|
||
|
parseConfigEntryPath( key, c.file, c.group, c.key );
|
||
|
|
||
|
c.value = e.attribute( "value" );
|
||
|
|
||
|
c.isValid = true;
|
||
|
|
||
|
return c;
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::commit()
|
||
15 years ago
|
{
|
||
|
updateChanges();
|
||
|
|
||
|
Change *c;
|
||
|
for( c = mChanges.first(); c; c = mChanges.next() ) {
|
||
|
c->apply();
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigSkeletonItem *TDEConfigPropagator::findItem( const TQString &group,
|
||
15 years ago
|
const TQString &name )
|
||
15 years ago
|
{
|
||
12 years ago
|
// kdDebug() << "TDEConfigPropagator::findItem()" << endl;
|
||
15 years ago
|
|
||
|
if ( !mSkeleton ) return 0;
|
||
|
|
||
12 years ago
|
TDEConfigSkeletonItem::List items = mSkeleton->items();
|
||
|
TDEConfigSkeletonItem::List::ConstIterator it;
|
||
15 years ago
|
for( it = items.begin(); it != items.end(); ++it ) {
|
||
|
// kdDebug() << " Item: " << (*it)->name() << " Type: "
|
||
|
// << (*it)->property().typeName() << endl;
|
||
|
if ( (*it)->group() == group && (*it)->name() == name ) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if ( it == items.end() ) return 0;
|
||
|
else return *it;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString TDEConfigPropagator::itemValueAsString( TDEConfigSkeletonItem *item )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQVariant p = item->property();
|
||
15 years ago
|
|
||
15 years ago
|
if ( p.type() == TQVariant::Bool ) {
|
||
15 years ago
|
if ( p.toBool() ) return "true";
|
||
|
else return "false";
|
||
|
}
|
||
|
|
||
|
return p.toString();
|
||
|
}
|
||
|
|
||
12 years ago
|
void TDEConfigPropagator::updateChanges()
|
||
15 years ago
|
{
|
||
|
mChanges.clear();
|
||
|
|
||
|
Rule::List::ConstIterator it;
|
||
|
for( it = mRules.begin(); it != mRules.end(); ++it ) {
|
||
|
Rule r = *it;
|
||
|
Condition c = r.condition;
|
||
|
if ( c.isValid ) {
|
||
12 years ago
|
TDEConfigSkeletonItem *item = findItem( c.group, c.key );
|
||
15 years ago
|
kdDebug() << "Item " << c.group << "/" << c.key << ":" << endl;
|
||
|
if ( !item ) {
|
||
|
kdError() << " Item not found." << endl;
|
||
|
} else {
|
||
15 years ago
|
TQString value = itemValueAsString( item );
|
||
15 years ago
|
kdDebug() << " Value: " << value << endl;
|
||
|
if ( value != c.value ) {
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigSkeletonItem *item = findItem( r.sourceGroup, r.sourceEntry );
|
||
15 years ago
|
if ( !item ) {
|
||
|
kdError() << "Item " << r.sourceGroup << "/" << r.sourceEntry
|
||
|
<< " not found." << endl;
|
||
|
continue;
|
||
|
}
|
||
15 years ago
|
TQString value = itemValueAsString( item );
|
||
15 years ago
|
|
||
12 years ago
|
TDEConfig target( r.targetFile );
|
||
15 years ago
|
target.setGroup( r.targetGroup );
|
||
15 years ago
|
TQString targetValue = target.readEntry( r.targetEntry );
|
||
15 years ago
|
if ( r.hideValue ) targetValue = KStringHandler::obscure( targetValue );
|
||
|
if ( targetValue != value ) {
|
||
|
ChangeConfig *change = new ChangeConfig();
|
||
|
change->file = r.targetFile;
|
||
|
change->group = r.targetGroup;
|
||
|
change->name = r.targetEntry;
|
||
|
if ( r.hideValue ) value = KStringHandler::obscure( value );
|
||
|
change->value = value;
|
||
|
change->hideValue = r.hideValue;
|
||
|
mChanges.append( change );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
addCustomChanges( mChanges );
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::Change::List TDEConfigPropagator::changes()
|
||
15 years ago
|
{
|
||
|
return mChanges;
|
||
|
}
|
||
|
|
||
12 years ago
|
TDEConfigPropagator::Rule::List TDEConfigPropagator::rules()
|
||
15 years ago
|
{
|
||
|
return mRules;
|
||
|
}
|