kdeui Library API Documentation

kxmlguiclient.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 Simon Hausmann <hausmann@kde.org> 00003 Copyright (C) 2000 Kurt Granroth <granroth@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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "kxmlguiclient.h" 00021 #include "kxmlguifactory.h" 00022 #include "kxmlguibuilder.h" 00023 00024 #include <qdir.h> 00025 #include <qfile.h> 00026 #include <qdom.h> 00027 #include <qtextstream.h> 00028 #include <qregexp.h> 00029 #include <qguardedptr.h> 00030 00031 #include <kinstance.h> 00032 #include <kstandarddirs.h> 00033 #include <kdebug.h> 00034 #include <kaction.h> 00035 #include <kapplication.h> 00036 00037 #include <assert.h> 00038 00039 class KXMLGUIClientPrivate 00040 { 00041 public: 00042 KXMLGUIClientPrivate() 00043 { 00044 m_instance = KGlobal::instance(); 00045 m_parent = 0L; 00046 m_builder = 0L; 00047 m_actionCollection = 0; 00048 } 00049 ~KXMLGUIClientPrivate() 00050 { 00051 } 00052 00053 KInstance *m_instance; 00054 00055 QDomDocument m_doc; 00056 KActionCollection *m_actionCollection; 00057 QDomDocument m_buildDocument; 00058 QGuardedPtr<KXMLGUIFactory> m_factory; 00059 KXMLGUIClient *m_parent; 00060 //QPtrList<KXMLGUIClient> m_supers; 00061 QPtrList<KXMLGUIClient> m_children; 00062 KXMLGUIBuilder *m_builder; 00063 QString m_xmlFile; 00064 QString m_localXMLFile; 00065 }; 00066 00067 KXMLGUIClient::KXMLGUIClient() 00068 { 00069 d = new KXMLGUIClientPrivate; 00070 } 00071 00072 KXMLGUIClient::KXMLGUIClient( KXMLGUIClient *parent ) 00073 { 00074 d = new KXMLGUIClientPrivate; 00075 parent->insertChildClient( this ); 00076 } 00077 00078 KXMLGUIClient::~KXMLGUIClient() 00079 { 00080 if ( d->m_parent ) 00081 d->m_parent->removeChildClient( this ); 00082 00083 QPtrListIterator<KXMLGUIClient> it( d->m_children ); 00084 for ( ; it.current(); ++it ) { 00085 assert( it.current()->d->m_parent == this ); 00086 it.current()->d->m_parent = 0; 00087 } 00088 00089 delete d->m_actionCollection; 00090 delete d; 00091 } 00092 00093 KAction *KXMLGUIClient::action( const char *name ) const 00094 { 00095 KAction* act = actionCollection()->action( name ); 00096 if ( !act ) { 00097 QPtrListIterator<KXMLGUIClient> childIt( d->m_children ); 00098 for (; childIt.current(); ++childIt ) { 00099 act = childIt.current()->actionCollection()->action( name ); 00100 if ( act ) 00101 break; 00102 } 00103 } 00104 return act; 00105 } 00106 00107 KActionCollection *KXMLGUIClient::actionCollection() const 00108 { 00109 if ( !d->m_actionCollection ) 00110 { 00111 d->m_actionCollection = new KActionCollection( 00112 "KXMLGUIClient-KActionCollection", this ); 00113 } 00114 return d->m_actionCollection; 00115 } 00116 00117 KAction *KXMLGUIClient::action( const QDomElement &element ) const 00118 { 00119 static const QString &attrName = KGlobal::staticQString( "name" ); 00120 return actionCollection()->action( element.attribute( attrName ).latin1() ); 00121 } 00122 00123 KInstance *KXMLGUIClient::instance() const 00124 { 00125 return d->m_instance; 00126 } 00127 00128 QDomDocument KXMLGUIClient::domDocument() const 00129 { 00130 return d->m_doc; 00131 } 00132 00133 QString KXMLGUIClient::xmlFile() const 00134 { 00135 return d->m_xmlFile; 00136 } 00137 00138 QString KXMLGUIClient::localXMLFile() const 00139 { 00140 if ( !d->m_localXMLFile.isEmpty() ) 00141 return d->m_localXMLFile; 00142 00143 if ( d->m_xmlFile[0] == '/' ) 00144 return QString::null; // can't save anything here 00145 00146 return locateLocal( "data", QString::fromLatin1( instance()->instanceName() + '/' ) + d->m_xmlFile ); 00147 } 00148 00149 00150 void KXMLGUIClient::reloadXML() 00151 { 00152 QString file( xmlFile() ); 00153 if ( !file.isEmpty() ) 00154 setXMLFile( file ); 00155 } 00156 00157 void KXMLGUIClient::setInstance( KInstance *instance ) 00158 { 00159 d->m_instance = instance; 00160 actionCollection()->setInstance( instance ); 00161 if ( d->m_builder ) 00162 d->m_builder->setBuilderClient( this ); 00163 } 00164 00165 void KXMLGUIClient::setXMLFile( const QString& _file, bool merge, bool setXMLDoc ) 00166 { 00167 // store our xml file name 00168 if ( !_file.isNull() ) { 00169 d->m_xmlFile = _file; 00170 actionCollection()->setXMLFile( _file ); 00171 } 00172 00173 if ( !setXMLDoc ) 00174 return; 00175 00176 QString file = _file; 00177 if ( file[0] != '/' ) 00178 { 00179 QString doc; 00180 00181 QString filter = QString::fromLatin1( instance()->instanceName() + '/' ) + _file; 00182 00183 QStringList allFiles = instance()->dirs()->findAllResources( "data", filter ) + instance()->dirs()->findAllResources( "data", _file ); 00184 00185 file = findMostRecentXMLFile( allFiles, doc ); 00186 00187 if ( file.isEmpty() ) 00188 { 00189 // this might or might not be an error. for the time being, 00190 // let's treat this as if it isn't a problem and the user just 00191 // wants the global standards file 00192 00193 // however if a non-empty file gets passed and we can't find it we might 00194 // inform the developer using some debug output 00195 if ( !_file.isEmpty() ) 00196 kdWarning() << "KXMLGUIClient::setXMLFile: cannot find .rc file " << _file << endl; 00197 00198 setXML( QString::null, true ); 00199 return; 00200 } 00201 else if ( !doc.isEmpty() ) 00202 { 00203 setXML( doc, merge ); 00204 return; 00205 } 00206 } 00207 00208 QString xml = KXMLGUIFactory::readConfigFile( file ); 00209 setXML( xml, merge ); 00210 } 00211 00212 void KXMLGUIClient::setLocalXMLFile( const QString &file ) 00213 { 00214 d->m_localXMLFile = file; 00215 } 00216 00217 void KXMLGUIClient::setXML( const QString &document, bool merge ) 00218 { 00219 QDomDocument doc; 00220 doc.setContent( document ); 00221 setDOMDocument( doc, merge ); 00222 } 00223 00224 void KXMLGUIClient::setDOMDocument( const QDomDocument &document, bool merge ) 00225 { 00226 if ( merge ) 00227 { 00228 QDomElement base = d->m_doc.documentElement(); 00229 00230 QDomElement e = document.documentElement(); 00231 00232 // merge our original (global) xml with our new one 00233 mergeXML(base, e, actionCollection()); 00234 00235 // reassign our pointer as mergeXML might have done something 00236 // strange to it 00237 base = d->m_doc.documentElement(); 00238 00239 // we want some sort of failsafe.. just in case 00240 if ( base.isNull() ) 00241 d->m_doc = document; 00242 } 00243 else 00244 { 00245 d->m_doc = document; 00246 } 00247 00248 setXMLGUIBuildDocument( QDomDocument() ); 00249 } 00250 00251 bool KXMLGUIClient::mergeXML( QDomElement &base, const QDomElement &additive, KActionCollection *actionCollection ) 00252 { 00253 static const QString &tagAction = KGlobal::staticQString( "Action" ); 00254 static const QString &tagMerge = KGlobal::staticQString( "Merge" ); 00255 static const QString &tagSeparator = KGlobal::staticQString( "Separator" ); 00256 static const QString &attrName = KGlobal::staticQString( "name" ); 00257 static const QString &attrAppend = KGlobal::staticQString( "append" ); 00258 static const QString &attrWeakSeparator = KGlobal::staticQString( "weakSeparator" ); 00259 static const QString &tagMergeLocal = KGlobal::staticQString( "MergeLocal" ); 00260 static const QString &tagText = KGlobal::staticQString( "text" ); 00261 static const QString &attrAlreadyVisited = KGlobal::staticQString( "alreadyVisited" ); 00262 static const QString &attrNoMerge = KGlobal::staticQString( "noMerge" ); 00263 static const QString &attrOne = KGlobal::staticQString( "1" ); 00264 00265 // there is a possibility that we don't want to merge in the 00266 // additive.. rather, we might want to *replace* the base with the 00267 // additive. this can be for any container.. either at a file wide 00268 // level or a simple container level. we look for the 'noMerge' 00269 // tag, in any event and just replace the old with the new 00270 if ( additive.attribute(attrNoMerge) == attrOne ) // ### use toInt() instead? (Simon) 00271 { 00272 base.parentNode().replaceChild(additive, base); 00273 return true; 00274 } 00275 00276 QString tag; 00277 00278 // iterate over all elements in the container (of the global DOM tree) 00279 QDomNode n = base.firstChild(); 00280 while ( !n.isNull() ) 00281 { 00282 QDomElement e = n.toElement(); 00283 n = n.nextSibling(); // Advance now so that we can safely delete e 00284 if (e.isNull()) 00285 continue; 00286 00287 tag = e.tagName(); 00288 00289 // if there's an action tag in the global tree and the action is 00290 // not implemented, then we remove the element 00291 if ( tag == tagAction ) 00292 { 00293 QCString name = e.attribute( attrName ).utf8(); // WABA 00294 if ( !actionCollection->action( name ) || 00295 (kapp && !kapp->authorizeKAction(name))) 00296 { 00297 // remove this child as we aren't using it 00298 base.removeChild( e ); 00299 continue; 00300 } 00301 } 00302 00303 // if there's a separator defined in the global tree, then add an 00304 // attribute, specifying that this is a "weak" separator 00305 else if ( tag == tagSeparator ) 00306 { 00307 e.setAttribute( attrWeakSeparator, (uint)1 ); 00308 00309 // okay, hack time. if the last item was a weak separator OR 00310 // this is the first item in a container, then we nuke the 00311 // current one 00312 QDomElement prev = e.previousSibling().toElement(); 00313 if ( prev.isNull() || 00314 ( prev.tagName() == tagSeparator && !prev.attribute( attrWeakSeparator ).isNull() ) || 00315 ( prev.tagName() == tagText ) ) 00316 { 00317 // the previous element was a weak separator or didn't exist 00318 base.removeChild( e ); 00319 continue; 00320 } 00321 } 00322 00323 // the MergeLocal tag lets us specify where non-standard elements 00324 // of the local tree shall be merged in. After inserting the 00325 // elements we delete this element 00326 else if ( tag == tagMergeLocal ) 00327 { 00328 QDomNode it = additive.firstChild(); 00329 while ( !it.isNull() ) 00330 { 00331 QDomElement newChild = it.toElement(); 00332 it = it.nextSibling(); 00333 if (newChild.isNull() ) 00334 continue; 00335 00336 if ( newChild.tagName() == tagText ) 00337 continue; 00338 00339 if ( newChild.attribute( attrAlreadyVisited ) == attrOne ) 00340 continue; 00341 00342 QString itAppend( newChild.attribute( attrAppend ) ); 00343 QString elemName( e.attribute( attrName ) ); 00344 00345 if ( ( itAppend.isNull() && elemName.isEmpty() ) || 00346 ( itAppend == elemName ) ) 00347 { 00348 // first, see if this new element matches a standard one in 00349 // the global file. if it does, then we skip it as it will 00350 // be merged in, later 00351 QDomElement matchingElement = findMatchingElement( newChild, base ); 00352 if ( matchingElement.isNull() || newChild.tagName() == tagSeparator ) 00353 base.insertBefore( newChild, e ); 00354 } 00355 } 00356 00357 base.removeChild( e ); 00358 continue; 00359 } 00360 00361 // in this last case we check for a separator tag and, if not, we 00362 // can be sure that its a container --> proceed with child nodes 00363 // recursively and delete the just proceeded container item in 00364 // case its empty (if the recursive call returns true) 00365 else if ( tag != tagMerge ) 00366 { 00367 // handle the text tag 00368 if ( tag == tagText ) 00369 continue; 00370 00371 QDomElement matchingElement = findMatchingElement( e, additive ); 00372 00373 if ( !matchingElement.isNull() ) 00374 { 00375 matchingElement.setAttribute( attrAlreadyVisited, (uint)1 ); 00376 00377 if ( mergeXML( e, matchingElement, actionCollection ) ) 00378 { 00379 base.removeChild( e ); 00380 continue; 00381 } 00382 00383 // Merge attributes 00384 QDomNamedNodeMap attribs = matchingElement.attributes(); 00385 for(uint i = 0; i < attribs.count(); i++) 00386 { 00387 QDomNode node = attribs.item(i); 00388 e.setAttribute(node.nodeName(), node.nodeValue()); 00389 } 00390 00391 continue; 00392 } 00393 else 00394 { 00395 // this is an important case here! We reach this point if the 00396 // "local" tree does not contain a container definition for 00397 // this container. However we have to call mergeXML recursively 00398 // and make it check if there are actions implemented for this 00399 // container. *If* none, then we can remove this container now 00400 if ( mergeXML( e, QDomElement(), actionCollection ) ) 00401 base.removeChild( e ); 00402 continue; 00403 } 00404 } 00405 } 00406 00407 //here we append all child elements which were not inserted 00408 //previously via the LocalMerge tag 00409 n = additive.firstChild(); 00410 while ( !n.isNull() ) 00411 { 00412 QDomElement e = n.toElement(); 00413 n = n.nextSibling(); // Advance now so that we can safely delete e 00414 if (e.isNull()) 00415 continue; 00416 00417 QDomElement matchingElement = findMatchingElement( e, base ); 00418 00419 if ( matchingElement.isNull() ) 00420 { 00421 base.appendChild( e ); 00422 } 00423 } 00424 00425 // do one quick check to make sure that the last element was not 00426 // a weak separator 00427 QDomElement last = base.lastChild().toElement(); 00428 if ( (last.tagName() == tagSeparator) && (!last.attribute( attrWeakSeparator ).isNull()) ) 00429 { 00430 base.removeChild( last ); 00431 } 00432 00433 // now we check if we are empty (in which case we return "true", to 00434 // indicate the caller that it can delete "us" (the base element 00435 // argument of "this" call) 00436 bool deleteMe = true; 00437 00438 n = base.firstChild(); 00439 while ( !n.isNull() ) 00440 { 00441 QDomElement e = n.toElement(); 00442 n = n.nextSibling(); // Advance now so that we can safely delete e 00443 if (e.isNull()) 00444 continue; 00445 00446 tag = e.tagName(); 00447 00448 if ( tag == tagAction ) 00449 { 00450 // if base contains an implemented action, then we must not get 00451 // deleted (note that the actionCollection contains both, 00452 // "global" and "local" actions 00453 if ( actionCollection->action( e.attribute( attrName ).utf8() ) ) 00454 { 00455 deleteMe = false; 00456 break; 00457 } 00458 } 00459 else if ( tag == tagSeparator ) 00460 { 00461 // if we have a separator which has *not* the weak attribute 00462 // set, then it must be owned by the "local" tree in which case 00463 // we must not get deleted either 00464 QString weakAttr = e.attribute( attrWeakSeparator ); 00465 if ( weakAttr.isEmpty() || weakAttr.toInt() != 1 ) 00466 { 00467 deleteMe = false; 00468 break; 00469 } 00470 } 00471 00472 // in case of a merge tag we have unlimited lives, too ;-) 00473 else if ( tag == tagMerge ) 00474 { 00475 // deleteMe = false; 00476 // break; 00477 continue; 00478 } 00479 00480 // a text tag is NOT enough to spare this container 00481 else if ( tag == tagText ) 00482 { 00483 continue; 00484 } 00485 00486 // what's left are non-empty containers! *don't* delete us in this 00487 // case (at this position we can be *sure* that the container is 00488 // *not* empty, as the recursive call for it was in the first loop 00489 // which deleted the element in case the call returned "true" 00490 else 00491 { 00492 deleteMe = false; 00493 break; 00494 } 00495 } 00496 00497 return deleteMe; 00498 } 00499 00500 QDomElement KXMLGUIClient::findMatchingElement( const QDomElement &base, const QDomElement &additive ) 00501 { 00502 static const QString &tagAction = KGlobal::staticQString( "Action" ); 00503 static const QString &tagMergeLocal = KGlobal::staticQString( "MergeLocal" ); 00504 static const QString &attrName = KGlobal::staticQString( "name" ); 00505 00506 QDomNode n = additive.firstChild(); 00507 while ( !n.isNull() ) 00508 { 00509 QDomElement e = n.toElement(); 00510 n = n.nextSibling(); // Advance now so that we can safely delete e 00511 if (e.isNull()) 00512 continue; 00513 00514 // skip all action and merge tags as we will never use them 00515 if ( ( e.tagName() == tagAction ) || ( e.tagName() == tagMergeLocal ) ) 00516 { 00517 continue; 00518 } 00519 00520 // now see if our tags are equivalent 00521 if ( ( e.tagName() == base.tagName() ) && 00522 ( e.attribute( attrName ) == base.attribute( attrName ) ) ) 00523 { 00524 return e; 00525 } 00526 } 00527 00528 // nope, return a (now) null element 00529 return QDomElement(); 00530 } 00531 00532 void KXMLGUIClient::conserveMemory() 00533 { 00534 d->m_doc = QDomDocument(); 00535 d->m_buildDocument = QDomDocument(); 00536 } 00537 00538 void KXMLGUIClient::setXMLGUIBuildDocument( const QDomDocument &doc ) 00539 { 00540 d->m_buildDocument = doc; 00541 } 00542 00543 QDomDocument KXMLGUIClient::xmlguiBuildDocument() const 00544 { 00545 return d->m_buildDocument; 00546 } 00547 00548 void KXMLGUIClient::setFactory( KXMLGUIFactory *factory ) 00549 { 00550 d->m_factory = factory; 00551 } 00552 00553 KXMLGUIFactory *KXMLGUIClient::factory() const 00554 { 00555 return d->m_factory; 00556 } 00557 00558 KXMLGUIClient *KXMLGUIClient::parentClient() const 00559 { 00560 return d->m_parent; 00561 } 00562 00563 void KXMLGUIClient::insertChildClient( KXMLGUIClient *child ) 00564 { 00565 if ( child->d->m_parent ) 00566 child->d->m_parent->removeChildClient( child ); 00567 d->m_children.append( child ); 00568 child->d->m_parent = this; 00569 } 00570 00571 void KXMLGUIClient::removeChildClient( KXMLGUIClient *child ) 00572 { 00573 assert( d->m_children.containsRef( child ) ); 00574 d->m_children.removeRef( child ); 00575 child->d->m_parent = 0; 00576 } 00577 00578 /*bool KXMLGUIClient::addSuperClient( KXMLGUIClient *super ) 00579 { 00580 if ( d->m_supers.contains( super ) ) 00581 return false; 00582 d->m_supers.append( super ); 00583 return true; 00584 }*/ 00585 00586 const QPtrList<KXMLGUIClient> *KXMLGUIClient::childClients() 00587 { 00588 return &d->m_children; 00589 } 00590 00591 void KXMLGUIClient::setClientBuilder( KXMLGUIBuilder *builder ) 00592 { 00593 d->m_builder = builder; 00594 if ( builder ) 00595 builder->setBuilderInstance( instance() ); 00596 } 00597 00598 KXMLGUIBuilder *KXMLGUIClient::clientBuilder() const 00599 { 00600 return d->m_builder; 00601 } 00602 00603 void KXMLGUIClient::plugActionList( const QString &name, const QPtrList<KAction> &actionList ) 00604 { 00605 if ( !d->m_factory ) 00606 return; 00607 00608 d->m_factory->plugActionList( this, name, actionList ); 00609 } 00610 00611 void KXMLGUIClient::unplugActionList( const QString &name ) 00612 { 00613 if ( !d->m_factory ) 00614 return; 00615 00616 d->m_factory->unplugActionList( this, name ); 00617 } 00618 00619 QString KXMLGUIClient::findMostRecentXMLFile( const QStringList &files, QString &doc ) 00620 { 00621 00622 QValueList<DocStruct> allDocuments; 00623 00624 QStringList::ConstIterator it = files.begin(); 00625 QStringList::ConstIterator end = files.end(); 00626 for (; it != end; ++it ) 00627 { 00628 //kdDebug() << "KXMLGUIClient::findMostRecentXMLFile " << *it << endl; 00629 QString data = KXMLGUIFactory::readConfigFile( *it ); 00630 DocStruct d; 00631 d.file = *it; 00632 d.data = data; 00633 allDocuments.append( d ); 00634 } 00635 00636 QValueList<DocStruct>::Iterator best = allDocuments.end(); 00637 uint bestVersion = 0; 00638 00639 QValueList<DocStruct>::Iterator docIt = allDocuments.begin(); 00640 QValueList<DocStruct>::Iterator docEnd = allDocuments.end(); 00641 for (; docIt != docEnd; ++docIt ) 00642 { 00643 QString versionStr = findVersionNumber( (*docIt).data ); 00644 if ( versionStr.isEmpty() ) 00645 continue; 00646 00647 bool ok = false; 00648 uint version = versionStr.toUInt( &ok ); 00649 if ( !ok ) 00650 continue; 00651 //kdDebug() << "FOUND VERSION " << version << endl; 00652 00653 if ( version > bestVersion ) 00654 { 00655 best = docIt; 00656 //kdDebug() << "best version is now " << version << endl; 00657 bestVersion = version; 00658 } 00659 } 00660 00661 if ( best != docEnd ) 00662 { 00663 if ( best != allDocuments.begin() ) 00664 { 00665 QValueList<DocStruct>::Iterator local = allDocuments.begin(); 00666 00667 // load the local document and extract the action properties 00668 QDomDocument document; 00669 document.setContent( (*local).data ); 00670 00671 ActionPropertiesMap properties = extractActionProperties( document ); 00672 00673 // in case the document has a ActionProperties section 00674 // we must not delete it but copy over the global doc 00675 // to the local and insert the ActionProperties section 00676 if ( !properties.isEmpty() ) 00677 { 00678 // now load the global one with the higher version number 00679 // into memory 00680 document.setContent( (*best).data ); 00681 // and store the properties in there 00682 storeActionProperties( document, properties ); 00683 00684 (*local).data = document.toString(); 00685 // make sure we pick up the new local doc, when we return later 00686 best = local; 00687 00688 // write out the new version of the local document 00689 QFile f( (*local).file ); 00690 if ( f.open( IO_WriteOnly ) ) 00691 { 00692 QCString utf8data = (*local).data.utf8(); 00693 f.writeBlock( utf8data.data(), utf8data.length() ); 00694 f.close(); 00695 } 00696 } 00697 else 00698 { 00699 QString f = (*local).file; 00700 QString backup = f + QString::fromLatin1( ".backup" ); 00701 QDir dir; 00702 dir.rename( f, backup ); 00703 } 00704 } 00705 doc = (*best).data; 00706 return (*best).file; 00707 } 00708 else if ( files.count() > 0 ) 00709 { 00710 //kdDebug() << "returning first one..." << endl; 00711 doc = (*allDocuments.begin()).data; 00712 return (*allDocuments.begin()).file; 00713 } 00714 00715 return QString::null; 00716 } 00717 00718 00719 00720 QString KXMLGUIClient::findVersionNumber( const QString &xml ) 00721 { 00722 enum { ST_START, ST_AFTER_OPEN, ST_AFTER_GUI, 00723 ST_EXPECT_VERSION, ST_VERSION_NUM} state = ST_START; 00724 for (unsigned int pos = 0; pos < xml.length(); pos++) 00725 { 00726 switch (state) 00727 { 00728 case ST_START: 00729 if (xml[pos] == '<') 00730 state = ST_AFTER_OPEN; 00731 break; 00732 case ST_AFTER_OPEN: 00733 { 00734 //Jump to gui.. 00735 int guipos = xml.find("gui", pos, false /*case-insensitive*/); 00736 if (guipos == -1) 00737 return QString::null; //Reject 00738 00739 pos = guipos + 2; //Position at i, so we're moved ahead to the next character by the ++; 00740 state = ST_AFTER_GUI; 00741 break; 00742 } 00743 case ST_AFTER_GUI: 00744 state = ST_EXPECT_VERSION; 00745 break; 00746 case ST_EXPECT_VERSION: 00747 { 00748 int verpos = xml.find("version=\"", pos, false /*case-insensitive*/); 00749 if (verpos == -1) 00750 return QString::null; //Reject 00751 00752 pos = verpos + 8; //v = 0, e = +1, r = +2, s = +3 , i = +4, o = +5, n = +6, = = +7, " = + 8 00753 state = ST_VERSION_NUM; 00754 break; 00755 } 00756 case ST_VERSION_NUM: 00757 { 00758 unsigned int endpos; 00759 for (endpos = pos; endpos < xml.length(); endpos++) 00760 { 00761 if (xml[endpos].unicode() >= '0' && xml[endpos].unicode() <= '9') 00762 continue; //Number.. 00763 if (xml[endpos].unicode() == '"') //End of parameter 00764 break; 00765 else //This shouldn't be here.. 00766 { 00767 endpos = xml.length(); 00768 } 00769 } 00770 00771 if (endpos != pos && endpos < xml.length() ) 00772 { 00773 QString matchCandidate = xml.mid(pos, endpos - pos); //Don't include " ". 00774 return matchCandidate; 00775 } 00776 00777 state = ST_EXPECT_VERSION; //Try to match a well-formed version.. 00778 break; 00779 } //case.. 00780 } //switch 00781 } //for 00782 00783 return QString::null; 00784 } 00785 00786 KXMLGUIClient::ActionPropertiesMap KXMLGUIClient::extractActionProperties( const QDomDocument &doc ) 00787 { 00788 ActionPropertiesMap properties; 00789 00790 QDomElement actionPropElement = doc.documentElement().namedItem( "ActionProperties" ).toElement(); 00791 00792 if ( actionPropElement.isNull() ) 00793 return properties; 00794 00795 QDomNode n = actionPropElement.firstChild(); 00796 while(!n.isNull()) 00797 { 00798 QDomElement e = n.toElement(); 00799 n = n.nextSibling(); // Advance now so that we can safely delete e 00800 if ( e.isNull() ) 00801 continue; 00802 00803 if ( e.tagName().lower() != "action" ) 00804 continue; 00805 00806 QString actionName = e.attribute( "name" ); 00807 00808 if ( actionName.isEmpty() ) 00809 continue; 00810 00811 QMap<QString, QMap<QString, QString> >::Iterator propIt = properties.find( actionName ); 00812 if ( propIt == properties.end() ) 00813 propIt = properties.insert( actionName, QMap<QString, QString>() ); 00814 00815 QDomNamedNodeMap attributes = e.attributes(); 00816 for ( uint i = 0; i < attributes.length(); ++i ) 00817 { 00818 QDomAttr attr = attributes.item( i ).toAttr(); 00819 00820 if ( attr.isNull() ) 00821 continue; 00822 00823 QString name = attr.name(); 00824 00825 if ( name == "name" || name.isEmpty() ) 00826 continue; 00827 00828 (*propIt)[ name ] = attr.value(); 00829 } 00830 00831 } 00832 00833 return properties; 00834 } 00835 00836 void KXMLGUIClient::storeActionProperties( QDomDocument &doc, const ActionPropertiesMap &properties ) 00837 { 00838 QDomElement actionPropElement = doc.documentElement().namedItem( "ActionProperties" ).toElement(); 00839 00840 if ( actionPropElement.isNull() ) 00841 { 00842 actionPropElement = doc.createElement( "ActionProperties" ); 00843 doc.documentElement().appendChild( actionPropElement ); 00844 } 00845 00846 while ( !actionPropElement.firstChild().isNull() ) 00847 actionPropElement.removeChild( actionPropElement.firstChild() ); 00848 00849 ActionPropertiesMap::ConstIterator it = properties.begin(); 00850 ActionPropertiesMap::ConstIterator end = properties.end(); 00851 for (; it != end; ++it ) 00852 { 00853 QDomElement action = doc.createElement( "Action" ); 00854 action.setAttribute( "name", it.key() ); 00855 actionPropElement.appendChild( action ); 00856 00857 QMap<QString, QString> attributes = (*it); 00858 QMap<QString, QString>::ConstIterator attrIt = attributes.begin(); 00859 QMap<QString, QString>::ConstIterator attrEnd = attributes.end(); 00860 for (; attrIt != attrEnd; ++attrIt ) 00861 action.setAttribute( attrIt.key(), attrIt.data() ); 00862 } 00863 } 00864 00865 void KXMLGUIClient::addStateActionEnabled(const QString& state, 00866 const QString& action) 00867 { 00868 StateChange stateChange = getActionsToChangeForState(state); 00869 00870 stateChange.actionsToEnable.append( action ); 00871 //kdDebug() << "KXMLGUIClient::addStateActionEnabled( " << state << ", " << action << ")" << endl; 00872 00873 m_actionsStateMap.replace( state, stateChange ); 00874 } 00875 00876 00877 void KXMLGUIClient::addStateActionDisabled(const QString& state, 00878 const QString& action) 00879 { 00880 StateChange stateChange = getActionsToChangeForState(state); 00881 00882 stateChange.actionsToDisable.append( action ); 00883 //kdDebug() << "KXMLGUIClient::addStateActionDisabled( " << state << ", " << action << ")" << endl; 00884 00885 m_actionsStateMap.replace( state, stateChange ); 00886 } 00887 00888 00889 KXMLGUIClient::StateChange KXMLGUIClient::getActionsToChangeForState(const QString& state) 00890 { 00891 return m_actionsStateMap[state]; 00892 } 00893 00894 00895 void KXMLGUIClient::stateChanged(const QString &newstate, KXMLGUIClient::ReverseStateChange reverse) 00896 { 00897 StateChange stateChange = getActionsToChangeForState(newstate); 00898 00899 bool setTrue = (reverse == StateNoReverse); 00900 bool setFalse = !setTrue; 00901 00902 // Enable actions which need to be enabled... 00903 // 00904 for ( QStringList::Iterator it = stateChange.actionsToEnable.begin(); 00905 it != stateChange.actionsToEnable.end(); ++it ) { 00906 00907 KAction *action = actionCollection()->action((*it).latin1()); 00908 if (action) action->setEnabled(setTrue); 00909 } 00910 00911 // and disable actions which need to be disabled... 00912 // 00913 for ( QStringList::Iterator it = stateChange.actionsToDisable.begin(); 00914 it != stateChange.actionsToDisable.end(); ++it ) { 00915 00916 KAction *action = actionCollection()->action((*it).latin1()); 00917 if (action) action->setEnabled(setFalse); 00918 } 00919 00920 } 00921 00922 void KXMLGUIClient::beginXMLPlug( QWidget *w ) 00923 { 00924 actionCollection()->beginXMLPlug( w ); 00925 QPtrListIterator<KXMLGUIClient> childIt( d->m_children ); 00926 for (; childIt.current(); ++childIt ) 00927 childIt.current()->actionCollection()->beginXMLPlug( w ); 00928 } 00929 00930 void KXMLGUIClient::endXMLPlug() 00931 { 00932 actionCollection()->endXMLPlug(); 00933 QPtrListIterator<KXMLGUIClient> childIt( d->m_children ); 00934 for (; childIt.current(); ++childIt ) 00935 childIt.current()->actionCollection()->endXMLPlug(); 00936 } 00937 00938 void KXMLGUIClient::prepareXMLUnplug( QWidget * ) 00939 { 00940 actionCollection()->prepareXMLUnplug(); 00941 QPtrListIterator<KXMLGUIClient> childIt( d->m_children ); 00942 for (; childIt.current(); ++childIt ) 00943 childIt.current()->actionCollection()->prepareXMLUnplug(); 00944 } 00945 00946 void KXMLGUIClient::virtual_hook( int, void* ) 00947 { /*BASE::virtual_hook( id, data );*/ }
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:43:30 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003