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 
00023 namespace SaX {
00024 //====================================
00025 // Constructor...
00026 //------------------------------------
00027 SaXStorage::SaXStorage (void) {
00028         // .../
00032         // ----
00033         mCurrentID = 0;
00034         mData.insert (mCurrentID, new QDict<QString>);
00035 }
00036 
00037 //====================================
00038 // Set standard key/value item...
00039 //------------------------------------
00040 void SaXStorage::setItem ( const QString & key, const QString & val ) {
00041         // .../
00044         // ----
00045         QString* data = new QString (val);
00046         mData.at (mCurrentID) -> replace (key,data);
00047 }
00048 
00049 //====================================
00050 // add standard key/value item...
00051 //------------------------------------
00052 void SaXStorage::addItem ( const QString & key, const QString & val ) {
00053         // .../
00057         // ----
00058         QString* currentValue = mData.at (mCurrentID) -> take (key);
00059         if ((currentValue) && (! currentValue->isEmpty())) {
00060                 QString newValue;
00061                 QTextOStream(&newValue) << *currentValue << "," << val;
00062                 newValue.replace (QRegExp("^,"),"");
00063                 setItem (key,newValue);
00064         } else {
00065                 setItem (key,val);
00066         }
00067 }
00068 
00069 //====================================
00070 // remove standard key/value item...
00071 //------------------------------------
00072 void SaXStorage::removeItem ( const QString & key, const QString & val ) {
00073         // .../
00077         // ----
00078         QString* currentValue = mData.at (mCurrentID) -> take (key);
00079         if (currentValue) {
00080                 QStringList optlist = QStringList::split ( ",", *currentValue );
00081                 QStringList result;
00082                 for ( QStringList::Iterator
00083                         in = optlist.begin(); in != optlist.end(); ++in
00084                 ) {
00085                         QString item (*in);
00086                         if (item != val) {
00087                                 result.append (item);
00088                         }
00089                 }
00090                 QString newValue = result.join (",");
00091                 setItem (key,newValue);
00092         }
00093 }
00094 
00095 //====================================
00096 // remove key/value entry...
00097 //------------------------------------
00098 void SaXStorage::removeEntry ( const QString & key ) {
00099         // .../
00102         // ----
00103         mData.at (mCurrentID) -> remove (key);
00104 }
00105 
00106 //====================================
00107 // Get copy of contents of item key...
00108 //------------------------------------
00109 QString SaXStorage::getItem ( const QString & key ) {
00110         // .../
00113         // ----
00114         if (! mData.at (mCurrentID) -> operator[] (key)) {
00115                 return QString();
00116         }
00117         return *mData.at (mCurrentID) -> operator[] (key);
00118 }
00119 
00120 //====================================
00121 // Set vendor;name item...
00122 //------------------------------------
00123 void SaXStorage::setDenomination (
00124         const QString & key, const QString & vendor,const QString & name 
00125 ) {
00126         // .../
00130         // ----
00131         QString value (vendor+";"+name);
00132         setItem (key,value);
00133 }
00134 
00135 //====================================
00136 // Set raw item...
00137 //------------------------------------
00138 void SaXStorage::setRawItem (
00139         const QString & key, const QString & optname,const QString & optval
00140 ) {
00141         // .../
00146         // ----
00147         QString value (optname+" "+optval);
00148         setItem (key,value);
00149 }
00150 
00151 //====================================
00152 // Add to raw item...
00153 //------------------------------------
00154 void SaXStorage::addRawItem (
00155         const QString & key, const QString & optname,const QString & optval
00156 ) {
00157         // .../
00162         // ----
00163         QString* currentValue = mData.at (mCurrentID) -> take (key);
00164         if ((currentValue) && (! currentValue->isEmpty())) {
00165                 QString newValue;
00166                 QString newOptVal (optname+" "+optval);
00167                 QTextOStream(&newValue) << *currentValue << "," << newOptVal;
00168                 newValue.replace (QRegExp("^,"),"");
00169                 setItem (key,newValue);
00170         } else {
00171                 setRawItem (key,optname,optval);
00172         }
00173 }
00174 
00175 //====================================
00176 // Delete from raw item...
00177 //------------------------------------
00178 void SaXStorage::removeRawItem (
00179         const QString & key, const QString & opt
00180 ) {
00181         // .../
00186         // ----
00187         QString optname = opt;
00188         QString expression (",");
00189         if (key == "RawData") {
00190                 expression = ",Option";
00191                 optname.replace (QRegExp("^Option"),"");
00192         }
00193         QString* currentValue = mData.at (mCurrentID) -> take (key);
00194         if (currentValue) {
00195                 QStringList     optlist = QStringList::split ( expression, *currentValue );
00196                 QStringList result;
00197                 for ( QStringList::Iterator
00198                         in = optlist.begin(); in != optlist.end(); ++in
00199                 ) {
00200                         QString item (*in);
00201                         if (! item.contains(optname)) {
00202                                 result.append (item);
00203                         }
00204                 }
00205                 QString newValue = result.join (expression);
00206                 QRegExp rx ("^Option");
00207                 if (rx.search (newValue) == -1) {
00208                         newValue = "Option" + newValue;
00209                 }
00210                 if (newValue == "Option") {
00211                         setItem (key,"");
00212                 } else {
00213                         setItem (key,newValue);
00214                 }
00215         }
00216 }
00217 
00218 //====================================
00219 // merge data into object...
00220 //------------------------------------
00221 void SaXStorage::merge (QList< QDict<QString> > data) {
00222         // .../
00226         // ----
00227         for (unsigned int n=0;n<data.count();n++) {
00228                 QDict<QString>* table = data.at(n);
00229                 QDictIterator<QString> it (*table);
00230                 if (! table) {
00231                         continue;
00232                 }
00233                 addID (n);
00234                 setID (n);
00235                 for (; it.current(); ++it) {
00236                         setItem (it.currentKey(),*it.current());
00237                 }
00238         }
00239 }
00240 
00241 //====================================
00242 // add new section ID...
00243 //------------------------------------
00244 bool SaXStorage::addID ( int id ) {
00245         // .../
00249         // ----
00250         if (! mData.at (id)) {
00251                 while (mCurrentID < id) {
00252                         mData.append ( new QDict<QString>);
00253                         mCurrentID = mData.at();
00254                 }
00255                 return true;
00256         }
00257         mCurrentID = id;
00258         return false;
00259 }
00260 
00261 //====================================
00262 // remove and reorganize section ID...
00263 //------------------------------------
00264 bool SaXStorage::delID ( int id ) {
00265         // .../
00268         // ----
00269         if ((! mData.at (id)) || (mData.at(id)->isEmpty())) {
00270                 return false;
00271         }
00272         int step = 1;
00273         int type = SAX_DESKTOP_TYPE;
00274         QString ident = *mData.at(id)->find ("Identifier");
00275         if (ident.contains ("Mouse")) {
00276                 type = SAX_POINTER_TYPE;
00277                 step = 2;
00278         }
00279         if (ident.contains ("Keyboard")) {
00280                 type = SAX_KEYBOARD_TYPE;
00281                 step = 2;
00282         }
00283         int index = -1;
00284         QListIterator < QDict<QString> > in (mData);
00285         for (; in.current(); ++in) {
00286                 index++;
00287                 QDict<QString>* data = in.current();
00288                 QString* ident = data->find ("Identifier");
00289                 if (! ident) {
00290                         continue;
00291                 }
00292                 int curType = SAX_DESKTOP_TYPE;
00293                 if (ident->contains("Mouse")) {
00294                         curType = SAX_POINTER_TYPE;
00295                 }
00296                 if (ident->contains("Keyboard")) {
00297                         curType = SAX_KEYBOARD_TYPE;
00298                 }
00299                 if ((data->isEmpty()) || (index <= id) || (curType != type)) {
00300                         continue;
00301                 }
00302                 QString oIDstr;
00303                 QString nIDstr;
00304                 oIDstr.sprintf ("%d",index);
00305                 nIDstr.sprintf ("%d",index - step);
00306                 QString mouseIDstr    ("Mouse["   + oIDstr +"]");
00307                 QString keyboardIDstr ("Keyboard["+ oIDstr +"]");
00308                 QString deviceIDstr   ("Device["  + oIDstr +"]");
00309                 QString monitorIDstr  ("Monitor[" + oIDstr +"]");
00310                 QString screenIDstr   ("Screen["  + oIDstr +"]");
00311                 QDictIterator<QString> it (*data);
00312                 for (; it.current(); ++it) {
00313                         QString val = *it.current();
00314                         QString key = it.currentKey();
00315                         if (val == mouseIDstr) {
00316                                 QString* nMouseIDstr = new QString ("Mouse["+nIDstr+"]");
00317                                 data -> replace (key,nMouseIDstr);
00318                         }
00319                         if (val == keyboardIDstr) {
00320                                 QString* nKbdIDstr = new QString ("Keyboard["+nIDstr+"]");
00321                                 data -> replace (key,nKbdIDstr);
00322                         }
00323                         if (val == deviceIDstr) {
00324                                 QString* nDeviceIDstr = new QString ("Device["+nIDstr+"]");
00325                                 data -> replace (key,nDeviceIDstr);
00326                         }
00327                         if (val == monitorIDstr) {
00328                                 QString* nMonitorIDstr = new QString ("Monitor["+nIDstr+"]");
00329                                 data -> replace (key,nMonitorIDstr);
00330                         }
00331                         if (val == screenIDstr) {
00332                                 QString* nScreenIDstr = new QString ("Screen["+nIDstr+"]");
00333                                 data -> replace (key,nScreenIDstr);
00334                         }
00335                         if ((key == "Screen") && (val == oIDstr)) {
00336                                 QString* nScreenIDstr = new QString (nIDstr);
00337                                 data -> replace (key,nScreenIDstr);
00338                         }
00339                 }
00340         }
00341         mData.remove (id);
00342         if ((mData.at(id)) && (mData.at(id)->isEmpty())) {
00343                 mData.remove (id);
00344         }
00345         return true;
00346 }
00347 
00348 //====================================
00349 // set section ID...
00350 //------------------------------------
00351 bool SaXStorage::setID ( int id ) {
00352         // .../
00356         // ----
00357         if (! mData.at (id)) {
00358                 excSetStorageIDFailed (id);
00359                 qError (errorString(),EXC_SETSTORAGEIDFAILED);
00360                 return false;
00361         }
00362         mCurrentID = id;
00363         return true;
00364 }
00365 
00366 //====================================
00367 // Get current section ID...
00368 //------------------------------------
00369 int SaXStorage::getCurrentID ( void ) {
00370         // .../
00372         // ----
00373         return mCurrentID;
00374 }
00375 
00376 //====================================
00377 // Get dict for section ID X...
00378 //------------------------------------
00379 QDict<QString> SaXStorage::getTable ( int id ) {
00380         // .../
00382         // ----
00383         if (mData.at (id)) {
00384                 return *mData.at (id);
00385         } else {
00386                 return QDict<QString>();
00387         }
00388 }
00389 
00390 //====================================
00391 // Get dict for current section ID...
00392 //------------------------------------
00393 QDict<QString> SaXStorage::getCurrentTable ( void ) {
00394         // .../
00396         // ----
00397         return *mData.at (mCurrentID);
00398 }
00399 
00400 //====================================
00401 // Get dict ptr for section ID X...
00402 //------------------------------------
00403 QDict<QString>* SaXStorage::getTablePointer ( int id ) {
00404         // .../
00406         // ----
00407     return mData.at (id);
00408 }
00409 
00410 //====================================
00411 // Get dict ptr for current section ID
00412 //------------------------------------
00413 QDict<QString>* SaXStorage::getCurrentTablePointer ( void ) {
00414         // .../
00416         // ----
00417     return mData.at (mCurrentID);
00418 }
00419 
00420 //====================================
00421 // Get number of elements
00422 //------------------------------------
00423 int SaXStorage::getCount (bool noEmptyItem) {
00424         // .../
00429         // ----
00430         int count = 0;
00431         if (noEmptyItem) {
00432                 QListIterator< QDict<QString> > it (mData);
00433                 for (; it.current();++it) {
00434                 if (! it.current()->isEmpty()) {
00435                         count++;
00436                 }
00437                 }
00438         } else {
00439                 count = mData.count();
00440         }
00441         return count;
00442 }
00443 
00444 //====================================
00445 // add data to CDB dict
00446 //------------------------------------
00447 void SaXStorage::addGroup (
00448         const QString & group,const QString & key, const QString & value
00449 ) {
00450         // .../
00454         // ----
00455         if ( ! mCDB[group] ) {
00456                 mCDB.insert (group, new QDict<QString>);
00457         }
00458         mCDB[group]->insert (key,new QString(value));
00459 }
00460 
00461 
00462 //====================================
00463 // return CDB data pointer
00464 //------------------------------------
00465 QDict< QDict<QString> > SaXStorage::getTablePointerCDB ( void ) {
00466         // .../
00469         // ----
00470         return mCDB;
00471 }
00472 
00473 //====================================
00474 // return CDB entry pointer 
00475 //------------------------------------
00476 QList< QDict<QString> > SaXStorage::getTablePointerCDB_DATA (
00477         const QString & group
00478 ) {
00479         // .../
00484         // ----
00485         QList< QDict<QString> > list;
00486         if ( mCDB[group] ) {
00487                 list.append (mCDB[group]);
00488         }
00489         return list;
00490 }
00491 
00492 //====================================
00493 // return mData data pointer
00494 //------------------------------------
00495 QList< QDict<QString> > SaXStorage::getTablePointerDATA ( void ) {
00496         // .../
00499         // ----
00500         return mData;
00501 }
00502 } // end namespace

Generated on Mon Jan 7 17:25:36 2008 for libsax by  doxygen 1.4.6