22 #include "tdeconfigpropagator.h"
25 #include <tdeconfig.h>
26 #include <tdeconfigskeleton.h>
27 #include <kstandarddirs.h>
28 #include <kstringhandler.h>
29 #include <tdelocale.h>
32 #include <tqstringlist.h>
34 TDEConfigPropagator::Change::~Change()
38 TDEConfigPropagator::ChangeConfig::ChangeConfig()
39 : TDEConfigPropagator::Change( i18n(
"Change Config Value") ),
44 TQString TDEConfigPropagator::ChangeConfig::arg1()
const
46 return file +
"/" + group +
"/" + name;
49 TQString TDEConfigPropagator::ChangeConfig::arg2()
const
51 if ( hideValue )
return "*";
55 void TDEConfigPropagator::ChangeConfig::apply()
57 TDEConfig cfg( file );
58 cfg.setGroup( group );
59 cfg.writeEntry( name, value );
64 TDEConfigPropagator::TDEConfigPropagator()
70 TDEConfigPropagator::TDEConfigPropagator( TDEConfigSkeleton *skeleton,
71 const TQString &kcfgFile )
72 : mSkeleton( skeleton ), mKcfgFile( kcfgFile )
79 void TDEConfigPropagator::init()
81 mChanges.setAutoDelete(
true );
84 void TDEConfigPropagator::readKcfgFile()
86 TQString filename = locate(
"kcfg", mKcfgFile );
87 if ( filename.isEmpty() ) {
88 kdError() <<
"Unable to find kcfg file '" << mKcfgFile <<
"'" << endl;
92 TQFile input( filename );
97 if ( !doc.setContent( &input, &errorMsg, &errorRow, &errorCol ) ) {
98 kdError() <<
"Parse error in " << mKcfgFile <<
", line " << errorRow <<
", col " << errorCol <<
": " << errorMsg << endl;
102 TQDomElement cfgElement = doc.documentElement();
104 if ( cfgElement.isNull() ) {
105 kdError() <<
"No document in kcfg file" << endl;
112 for ( n = cfgElement.firstChild(); !n.isNull(); n = n.nextSibling() ) {
113 TQDomElement e = n.toElement();
115 TQString tag = e.tagName();
117 if ( tag ==
"propagation" ) {
118 Rule rule = parsePropagation( e );
119 mRules.append( rule );
120 }
else if ( tag ==
"condition" ) {
121 Condition condition = parseCondition( e );
123 for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
124 TQDomElement e2 = n2.toElement();
125 if ( e2.tagName() ==
"propagation" ) {
126 Rule rule = parsePropagation( e2 );
127 rule.condition = condition;
128 mRules.append( rule );
130 kdError() <<
"Unknow tag: " << e2.tagName() << endl;
137 TDEConfigPropagator::Rule TDEConfigPropagator::parsePropagation(
const TQDomElement &e )
141 TQString source = e.attribute(
"source" );
142 parseConfigEntryPath( source, r.sourceFile, r.sourceGroup, r.sourceEntry );
144 TQString target = e.attribute(
"target" );
145 parseConfigEntryPath( target, r.targetFile, r.targetGroup, r.targetEntry );
147 r.hideValue = e.hasAttribute(
"hidevalue" ) &&
148 e.attribute(
"hidevalue" ) ==
"true";
153 void TDEConfigPropagator::parseConfigEntryPath(
const TQString &path,
158 TQStringList p = TQStringList::split(
"/", path );
160 if ( p.count() != 3 ) {
161 kdError() <<
"Path has to be of form file/group/entry" << endl;
175 TDEConfigPropagator::Condition TDEConfigPropagator::parseCondition(
const TQDomElement &e )
179 TQString key = e.attribute(
"key" );
181 parseConfigEntryPath( key, c.file, c.group, c.key );
183 c.value = e.attribute(
"value" );
190 void TDEConfigPropagator::commit()
195 for( c = mChanges.first(); c; c = mChanges.next() ) {
200 TDEConfigSkeletonItem *TDEConfigPropagator::findItem(
const TQString &group,
201 const TQString &name )
205 if ( !mSkeleton )
return 0;
207 TDEConfigSkeletonItem::List items = mSkeleton->items();
208 TDEConfigSkeletonItem::List::ConstIterator it;
209 for( it = items.begin(); it != items.end(); ++it ) {
212 if ( (*it)->group() == group && (*it)->name() == name ) {
216 if ( it == items.end() )
return 0;
220 TQString TDEConfigPropagator::itemValueAsString( TDEConfigSkeletonItem *item )
222 TQVariant p = item->property();
224 if ( p.type() == TQVariant::Bool ) {
225 if ( p.toBool() )
return "true";
232 void TDEConfigPropagator::updateChanges()
236 Rule::List::ConstIterator it;
237 for( it = mRules.begin(); it != mRules.end(); ++it ) {
239 Condition c = r.condition;
241 TDEConfigSkeletonItem *item = findItem( c.group, c.key );
242 kdDebug() <<
"Item " << c.group <<
"/" << c.key <<
":" << endl;
244 kdError() <<
" Item not found." << endl;
246 TQString value = itemValueAsString( item );
247 kdDebug() <<
" Value: " << value << endl;
248 if ( value != c.value ) {
254 TDEConfigSkeletonItem *item = findItem( r.sourceGroup, r.sourceEntry );
256 kdError() <<
"Item " << r.sourceGroup <<
"/" << r.sourceEntry
257 <<
" not found." << endl;
260 TQString value = itemValueAsString( item );
262 TDEConfig target( r.targetFile );
263 target.setGroup( r.targetGroup );
264 TQString targetValue = target.readEntry( r.targetEntry );
265 if ( r.hideValue ) targetValue = KStringHandler::obscure( targetValue );
266 if ( targetValue != value ) {
267 ChangeConfig *change =
new ChangeConfig();
268 change->file = r.targetFile;
269 change->group = r.targetGroup;
270 change->name = r.targetEntry;
271 if ( r.hideValue ) value = KStringHandler::obscure( value );
272 change->value = value;
273 change->hideValue = r.hideValue;
274 mChanges.append( change );
278 addCustomChanges( mChanges );
281 TDEConfigPropagator::Change::List TDEConfigPropagator::changes()
286 TDEConfigPropagator::Rule::List TDEConfigPropagator::rules()