libsax 7.2

storage.cpp

00001 /**************
00002 FILE          : storage.cpp
00003 ***************
00004 PROJECT       : SaX2 - library interface
00005               :
00006 AUTHOR        : Marcus Schäfer <ms@suse.de>
00007               :
00008 BELONGS TO    : SaX2 - SuSE advanced X11 configuration 
00009               : 
00010               :
00011 DESCRIPTION   : native C++ class library to access SaX2
00012               : functionality. Easy to use interface for
00013               : //.../
00014               : - importing/exporting X11 configurations
00015               : - modifying/creating X11 configurations 
00016               : ---
00017               :
00018               :
00019 STATUS        : Status: Development
00020 **************/
00021 #include "storage.h"
00022 //Added by qt3to4:
00023 #include <Q3PtrList>
00024 
00025 namespace SaX {
00026 //====================================
00027 // Constructor...
00028 //------------------------------------
00029 SaXStorage::SaXStorage (void) {
00030         // .../
00034         // ----
00035         mCurrentID = 0;
00036 
00037         mData.insert (mCurrentID, new Q3Dict<QString>);
00038 }
00039 
00040 //====================================
00041 // Set standard key/value item...
00042 //------------------------------------
00043 void SaXStorage::setItem ( const QString & key, const QString & val ) {
00044         // .../
00047         // ----
00048         QString* data = new QString (val);
00049         mData.at (mCurrentID) -> replace (key,data);
00050 }
00051 
00052 //====================================
00053 // add standard key/value item...
00054 //------------------------------------
00055 void SaXStorage::addItem ( const QString & key, const QString & val ) {
00056         // .../
00060         // ----
00061         QString* currentValue = mData.at (mCurrentID) -> take (key);
00062         if ((currentValue) && (! currentValue->isEmpty())) {
00063                 QString newValue;
00064                 QTextOStream(&newValue) << *currentValue << "," << val;
00065                 newValue.replace (QRegExp("^,"),"");
00066                 setItem (key,newValue);
00067         } else {
00068                 setItem (key,val);
00069         }
00070 }
00071 
00072 //====================================
00073 // remove standard key/value item...
00074 //------------------------------------
00075 void SaXStorage::removeItem ( const QString & key, const QString & val ) {
00076         // .../
00080         // ----
00081         QString* currentValue = mData.at (mCurrentID) -> take (key);
00082         if (currentValue) {
00083                 QStringList optlist = QStringList::split ( ",", *currentValue );
00084                 QStringList result;
00085                 for ( QStringList::Iterator
00086                         in = optlist.begin(); in != optlist.end(); ++in
00087                 ) {
00088                         QString item (*in);
00089                         if (item != val) {
00090                                 result.append (item);
00091                         }
00092                 }
00093                 QString newValue = result.join (",");
00094                 setItem (key,newValue);
00095         }
00096 }
00097 
00098 //====================================
00099 // remove key/value entry...
00100 //------------------------------------
00101 void SaXStorage::removeEntry ( const QString & key ) {
00102         // .../
00105         // ----
00106         mData.at (mCurrentID) -> remove (key);
00107 }
00108 
00109 //====================================
00110 // Get copy of contents of item key...
00111 //------------------------------------
00112 QString SaXStorage::getItem ( const QString & key ) {
00113         // .../
00116         // ----
00117         if (! mData.at (mCurrentID) -> operator[] (key)) {
00118                 QString* nope = new QString;
00119                 return *nope;
00120         }
00121         return *mData.at (mCurrentID) -> operator[] (key);
00122 }
00123 
00124 //====================================
00125 // Set vendor;name item...
00126 //------------------------------------
00127 void SaXStorage::setDenomination (
00128         const QString & key, const QString & vendor,const QString & name 
00129 ) {
00130         // .../
00134         // ----
00135         QString value (vendor+";"+name);
00136         setItem (key,value);
00137 }
00138 
00139 //====================================
00140 // Set raw item...
00141 //------------------------------------
00142 void SaXStorage::setRawItem (
00143         const QString & key, const QString & optname,const QString & optval
00144 ) {
00145         // .../
00150         // ----
00151         QString value (optname+" "+optval);
00152         setItem (key,value);
00153 }
00154 
00155 //====================================
00156 // Add to raw item...
00157 //------------------------------------
00158 void SaXStorage::addRawItem (
00159         const QString & key, const QString & optname,const QString & optval
00160 ) {
00161         // .../
00166         // ----
00167         QString* currentValue = mData.at (mCurrentID) -> take (key);
00168         if ((currentValue) && (! currentValue->isEmpty())) {
00169                 QString newValue;
00170                 QString newOptVal (optname+" "+optval);
00171                 QTextOStream(&newValue) << *currentValue << "," << newOptVal;
00172                 newValue.replace (QRegExp("^,"),"");
00173                 setItem (key,newValue);
00174         } else {
00175                 setRawItem (key,optname,optval);
00176         }
00177 }
00178 
00179 //====================================
00180 // Delete from raw item...
00181 //------------------------------------
00182 void SaXStorage::removeRawItem (
00183         const QString & key, const QString & opt
00184 ) {
00185         // .../
00190         // ----
00191         QString optname = opt;
00192         QString expression (",");
00193         if (key == "RawData") {
00194                 expression = ",Option";
00195                 optname.replace (QRegExp("^Option"),"");
00196         }
00197         QString* currentValue = mData.at (mCurrentID) -> take (key);
00198         if (currentValue) {
00199                 QStringList optlist = QStringList::split ( expression, *currentValue );
00200                 QStringList result;
00201                 for ( QStringList::Iterator
00202                         in = optlist.begin(); in != optlist.end(); ++in
00203                 ) {
00204                         QString item (*in);
00205                         if (! item.contains(optname)) {
00206                                 result.append (item);
00207                         }
00208                 }
00209                 QString newValue = result.join (expression);
00210                 QRegExp rx ("^Option");
00211                 if (rx.search (newValue) == -1) {
00212                         newValue = "Option" + newValue;
00213                 }
00214                 if (newValue == "Option") {
00215                         setItem (key,"");
00216                 } else {
00217                         setItem (key,newValue);
00218                 }
00219         }
00220 }
00221 
00222 //====================================
00223 // merge data into object...
00224 //------------------------------------
00225 void SaXStorage::merge (Q3PtrList< Q3Dict<QString> > data) {
00226         // .../
00230         // ----
00231         for (unsigned int n=0;n<data.count();n++) {
00232                 Q3Dict<QString>* table = data.at(n);
00233                 if ((! table) || (table->isEmpty())) {
00234                         continue;
00235                 }
00236                 Q3DictIterator<QString> it (*table);
00237                 addID (n);
00238                 setID (n);
00239                 for (; it.current(); ++it) {
00240                         setItem (it.currentKey(),*it.current());
00241                 }
00242         }
00243 }
00244 
00245 //====================================
00246 // add new section ID...
00247 //------------------------------------
00248 bool SaXStorage::addID ( int id ) {
00249         // .../
00253         // ----
00254         if (! mData.at (id)) {
00255                 while (mCurrentID < id) {
00256                         mData.append ( new Q3Dict<QString>);
00257                         mCurrentID = mData.at();
00258                 }
00259                 return true;
00260         }
00261         mCurrentID = id;
00262         return false;
00263 }
00264 
00265 //====================================
00266 // remove and reorganize section ID...
00267 //------------------------------------
00268 bool SaXStorage::delID ( int id ) {
00269         // .../
00272         // ----
00273         if ((! mData.at (id)) || (mData.at(id)->isEmpty())) {
00274                 return false;
00275         }
00276         int step = 1;
00277         int type = SAX_DESKTOP_TYPE;
00278         QString ident = *mData.at(id)->find ("Identifier");
00279         if (ident.contains ("Mouse")) {
00280                 type = SAX_POINTER_TYPE;
00281                 step = 2;
00282         }
00283         if (ident.contains ("Keyboard")) {
00284                 type = SAX_KEYBOARD_TYPE;
00285                 step = 2;
00286         }
00287         int index = -1;
00288         Q3Dict<QString>*  data;
00289         foreach (data,mData) {
00290                 index++;
00291 //              Q3Dict<QString>* data = in;
00292                 QString* ident = data->find ("Identifier");
00293                 if (! ident) {
00294                         continue;
00295                 }
00296                 int curType = SAX_DESKTOP_TYPE;
00297                 if (ident->contains("Mouse")) {
00298                         curType = SAX_POINTER_TYPE;
00299                 }
00300                 if (ident->contains("Keyboard")) {
00301                         curType = SAX_KEYBOARD_TYPE;
00302                 }
00303                 if ((data->isEmpty()) || (index <= id) || (curType != type)) {
00304                         continue;
00305                 }
00306                 QString oIDstr;
00307                 QString nIDstr;
00308                 oIDstr.sprintf ("%d",index);
00309                 nIDstr.sprintf ("%d",index - step);
00310                 QString mouseIDstr    ("Mouse["   + oIDstr +"]");
00311                 QString keyboardIDstr ("Keyboard["+ oIDstr +"]");
00312                 QString deviceIDstr   ("Device["  + oIDstr +"]");
00313                 QString monitorIDstr  ("Monitor[" + oIDstr +"]");
00314                 QString screenIDstr   ("Screen["  + oIDstr +"]");
00315                 Q3DictIterator<QString> it (*data);
00316                 for (; it.current(); ++it) {
00317                         QString val = *it.current();
00318                         QString key = it.currentKey();
00319                         if (val == mouseIDstr) {
00320                                 QString* nMouseIDstr = new QString ("Mouse["+nIDstr+"]");
00321                                 data -> replace (key,nMouseIDstr);
00322                         }
00323                         if (val == keyboardIDstr) {
00324                                 QString* nKbdIDstr = new QString ("Keyboard["+nIDstr+"]");
00325                                 data -> replace (key,nKbdIDstr);
00326                         }
00327                         if (val == deviceIDstr) {
00328                                 QString* nDeviceIDstr = new QString ("Device["+nIDstr+"]");
00329                                 data -> replace (key,nDeviceIDstr);
00330                         }
00331                         if (val == monitorIDstr) {
00332                                 QString* nMonitorIDstr = new QString ("Monitor["+nIDstr+"]");
00333                                 data -> replace (key,nMonitorIDstr);
00334                         }
00335                         if (val == screenIDstr) {
00336                                 QString* nScreenIDstr = new QString ("Screen["+nIDstr+"]");
00337                                 data -> replace (key,nScreenIDstr);
00338                         }
00339                         if ((key == "Screen") && (val == oIDstr)) {
00340                                 QString* nScreenIDstr = new QString (nIDstr);
00341                                 data -> replace (key,nScreenIDstr);
00342                         }
00343                 }
00344         }
00345         mData.remove (id);
00346         if ((mData.at(id)) && (mData.at(id)->isEmpty())) {
00347                 mData.remove (id);
00348         }
00349         return true;
00350 }
00351 
00352 //====================================
00353 // set section ID...
00354 //------------------------------------
00355 bool SaXStorage::setID ( int id ) {
00356         // .../
00360         // ----
00361         if (! mData.at (id)) {
00362                 excSetStorageIDFailed (id);
00363                 qError (errorString(),EXC_SETSTORAGEIDFAILED);
00364                 return false;
00365         }
00366         mCurrentID = id;
00367         return true;
00368 }
00369 
00370 //====================================
00371 // Get current section ID...
00372 //------------------------------------
00373 int SaXStorage::getCurrentID ( void ) {
00374         // .../
00376         // ----
00377         return mCurrentID;
00378 }
00379 
00380 //====================================
00381 // Get dict for section ID X...
00382 //------------------------------------
00383 Q3Dict<QString> SaXStorage::getTable ( int id ) {
00384         // .../
00386         // ----
00387         if (mData.at (id)) {
00388                 return *mData.at (id);
00389         } else {
00390                 Q3Dict<QString>* nope = new Q3Dict<QString>;
00391                 return *nope;
00392         }
00393 }
00394 
00395 //====================================
00396 // Get dict for current section ID...
00397 //------------------------------------
00398 Q3Dict<QString> SaXStorage::getCurrentTable ( void ) {
00399         // .../
00401         // ----
00402         return *mData.at (mCurrentID);
00403 }
00404 
00405 //====================================
00406 // Get dict ptr for section ID X...
00407 //------------------------------------
00408 Q3Dict<QString>* SaXStorage::getTablePointer ( int id ) {
00409         // .../
00411         // ----
00412         return mData.at (id);
00413 }
00414 
00415 //====================================
00416 // Get dict ptr for current section ID
00417 //------------------------------------
00418 Q3Dict<QString>* SaXStorage::getCurrentTablePointer ( void ) {
00419         // .../
00421         // ----
00422         return mData.at (mCurrentID);
00423 }
00424 
00425 //====================================
00426 // Get number of elements
00427 //------------------------------------
00428 int SaXStorage::getCount (bool noEmptyItem) {
00429         // .../
00434         // ----
00435         int count = 0;
00436         if (noEmptyItem) {
00437                 Q3Dict<QString>* it;
00438                 foreach (it,mData) {
00439                 if (! it->isEmpty()) {
00440                         count++;
00441                 }
00442                 }
00443         } else {
00444                 count = mData.count();
00445         }
00446         return count;
00447 }
00448 
00449 //====================================
00450 // add data to CDB dict
00451 //------------------------------------
00452 void SaXStorage::addGroup (
00453         const QString & group,const QString & key, const QString & value
00454 ) {
00455         // .../
00459         // ----
00460         if ( ! mCDB[group] ) {
00461                 mCDB.insert (group, new Q3Dict<QString>);
00462         }
00463         mCDB[group]->insert (key,new QString(value));
00464 }
00465 
00466 
00467 //====================================
00468 // return CDB data pointer
00469 //------------------------------------
00470 Q3Dict< Q3Dict<QString> > SaXStorage::getTablePointerCDB ( void ) {
00471         // .../
00474         // ----
00475         return mCDB;
00476 }
00477 
00478 //====================================
00479 // return CDB entry pointer 
00480 //------------------------------------
00481 Q3PtrList< Q3Dict<QString> > SaXStorage::getTablePointerCDB_DATA (
00482         const QString & group
00483 ) {
00484         // .../
00489         // ----
00490         Q3PtrList< Q3Dict<QString> > list;
00491         if ( mCDB[group] ) {
00492                 list.append (mCDB[group]);
00493         }
00494         return list;
00495 }
00496 
00497 //====================================
00498 // return mData data pointer
00499 //------------------------------------
00500 Q3PtrList< Q3Dict<QString> > SaXStorage::getTablePointerDATA ( void ) {
00501         // .../
00504         // ----
00505         return mData;
00506 }
00507 } // end namespace