Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

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) {
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) {
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 & optname
00180 ) {
00181         // .../
00186         // ----
00187         QString expression (",");
00188         if (key == "RawData") {
00189                 expression = "Option";
00190         }
00191         QString* currentValue = mData.at (mCurrentID) -> take (key);
00192         if (currentValue) {
00193                 QStringList optlist = QStringList::split ( ",", *currentValue );
00194                 QStringList result;
00195                 for ( QStringList::Iterator
00196                         in = optlist.begin(); in != optlist.end(); ++in
00197                 ) {
00198                         QString item (*in);
00199                         if (! item.contains(optname)) {
00200                                 result.append (item);
00201                         }
00202                 }
00203                 QString newValue = result.join (",");
00204                 setItem (key,newValue);
00205         }
00206 }
00207 
00208 //====================================
00209 // merge data into object...
00210 //------------------------------------
00211 void SaXStorage::merge (QList< QDict<QString> > data) {
00212         // .../
00216         // ----
00217         for (unsigned int n=0;n<data.count();n++) {
00218                 QDict<QString>* table = data.at(n);
00219                 QDictIterator<QString> it (*table);
00220                 if (! table) {
00221                         continue;
00222                 }
00223                 addID (n);
00224                 setID (n);
00225                 for (; it.current(); ++it) {
00226                         setItem (it.currentKey(),*it.current());
00227                 }
00228         }
00229 }
00230 
00231 //====================================
00232 // add new section ID...
00233 //------------------------------------
00234 bool SaXStorage::addID ( int id ) {
00235         // .../
00239         // ----
00240         if (! mData.at (id)) {
00241                 while (mCurrentID < id) {
00242                         mData.append ( new QDict<QString>);
00243                         mCurrentID = mData.at();
00244                 }
00245                 return true;
00246         }
00247         mCurrentID = id;
00248         return false;
00249 }
00250 
00251 //====================================
00252 // remove and reorganize section ID...
00253 //------------------------------------
00254 bool SaXStorage::delID ( int id ) {
00255         // .../
00258         // ----
00259         if ((! mData.at (id)) || (mData.at(id)->isEmpty())) {
00260                 return false;
00261         }
00262         int step = 1;
00263         int type = SAX_DESKTOP_TYPE;
00264         QString ident = *mData.at(id)->find ("Identifier");
00265         if (ident.contains ("Mouse")) {
00266                 type = SAX_POINTER_TYPE;
00267                 step = 2;
00268         }
00269         if (ident.contains ("Keyboard")) {
00270                 type = SAX_KEYBOARD_TYPE;
00271                 step = 2;
00272         }
00273         int index = -1;
00274         QListIterator < QDict<QString> > in (mData);
00275         for (; in.current(); ++in) {
00276                 index++;
00277                 QDict<QString>* data = in.current();
00278                 QString* ident = data->find ("Identifier");
00279                 if (! ident) {
00280                         continue;
00281                 }
00282                 int curType = SAX_DESKTOP_TYPE;
00283                 if (ident->contains("Mouse")) {
00284                         curType = SAX_POINTER_TYPE;
00285                 }
00286                 if (ident->contains("Keyboard")) {
00287                         curType = SAX_KEYBOARD_TYPE;
00288                 }
00289                 if ((data->isEmpty()) || (index <= id) || (curType != type)) {
00290                         continue;
00291                 }
00292                 QString oIDstr;
00293                 QString nIDstr;
00294                 oIDstr.sprintf ("%d",index);
00295                 nIDstr.sprintf ("%d",index - step);
00296                 QString mouseIDstr    ("Mouse["   + oIDstr +"]");
00297                 QString keyboardIDstr ("Keyboard["+ oIDstr +"]");
00298                 QString deviceIDstr   ("Device["  + oIDstr +"]");
00299                 QString monitorIDstr  ("Monitor[" + oIDstr +"]");
00300                 QString screenIDstr   ("Screen["  + oIDstr +"]");
00301                 QDictIterator<QString> it (*data);
00302                 for (; it.current(); ++it) {
00303                         QString val = *it.current();
00304                         QString key = it.currentKey();
00305                         if (val == mouseIDstr) {
00306                                 QString* nMouseIDstr = new QString ("Mouse["+nIDstr+"]");
00307                                 data -> replace (key,nMouseIDstr);
00308                         }
00309                         if (val == keyboardIDstr) {
00310                                 QString* nKbdIDstr = new QString ("Keyboard["+nIDstr+"]");
00311                                 data -> replace (key,nKbdIDstr);
00312                         }
00313                         if (val == deviceIDstr) {
00314                                 QString* nDeviceIDstr = new QString ("Device["+nIDstr+"]");
00315                                 data -> replace (key,nDeviceIDstr);
00316                         }
00317                         if (val == monitorIDstr) {
00318                                 QString* nMonitorIDstr = new QString ("Monitor["+nIDstr+"]");
00319                                 data -> replace (key,nMonitorIDstr);
00320                         }
00321                         if (val == screenIDstr) {
00322                                 QString* nScreenIDstr = new QString ("Screen["+nIDstr+"]");
00323                                 data -> replace (key,nScreenIDstr);
00324                         }
00325                         if ((key == "Screen") && (val == oIDstr)) {
00326                                 QString* nScreenIDstr = new QString (nIDstr);
00327                                 data -> replace (key,nScreenIDstr);
00328                         }
00329                 }
00330         }
00331         mData.remove (id);
00332         if ((mData.at(id)) && (mData.at(id)->isEmpty())) {
00333                 mData.remove (id);
00334         }
00335         return true;
00336 }
00337 
00338 //====================================
00339 // set section ID...
00340 //------------------------------------
00341 bool SaXStorage::setID ( int id ) {
00342         // .../
00346         // ----
00347         if (! mData.at (id)) {
00348                 excSetStorageIDFailed (id);
00349                 qError (errorString(),EXC_SETSTORAGEIDFAILED);
00350                 return false;
00351         }
00352         mCurrentID = id;
00353         return true;
00354 }
00355 
00356 //====================================
00357 // Get current section ID...
00358 //------------------------------------
00359 int SaXStorage::getCurrentID ( void ) {
00360         // .../
00362         // ----
00363         return mCurrentID;
00364 }
00365 
00366 //====================================
00367 // Get dict for section ID X...
00368 //------------------------------------
00369 QDict<QString> SaXStorage::getTable ( int id ) {
00370         // .../
00372         // ----
00373         if (mData.at (id)) {
00374                 return *mData.at (id);
00375         } else {
00376                 return QDict<QString>();
00377         }
00378 }
00379 
00380 //====================================
00381 // Get dict for current section ID...
00382 //------------------------------------
00383 QDict<QString> SaXStorage::getCurrentTable ( void ) {
00384         // .../
00386         // ----
00387         return *mData.at (mCurrentID);
00388 }
00389 
00390 //====================================
00391 // Get dict ptr for section ID X...
00392 //------------------------------------
00393 QDict<QString>* SaXStorage::getTablePointer ( int id ) {
00394         // .../
00396         // ----
00397     return mData.at (id);
00398 }
00399 
00400 //====================================
00401 // Get dict ptr for current section ID
00402 //------------------------------------
00403 QDict<QString>* SaXStorage::getCurrentTablePointer ( void ) {
00404         // .../
00406         // ----
00407     return mData.at (mCurrentID);
00408 }
00409 
00410 //====================================
00411 // Get number of elements
00412 //------------------------------------
00413 int SaXStorage::getCount (bool noEmptyItem) {
00414         // .../
00419         // ----
00420         int count = 0;
00421         if (noEmptyItem) {
00422                 QListIterator< QDict<QString> > it (mData);
00423                 for (; it.current();++it) {
00424                 if (! it.current()->isEmpty()) {
00425                         count++;
00426                 }
00427                 }
00428         } else {
00429                 count = mData.count();
00430         }
00431         return count;
00432 }
00433 
00434 //====================================
00435 // add data to CDB dict
00436 //------------------------------------
00437 void SaXStorage::addGroup (
00438         const QString & group,const QString & key, const QString & value
00439 ) {
00440         // .../
00444         // ----
00445         if ( ! mCDB[group] ) {
00446                 mCDB.insert (group, new QDict<QString>);
00447         }
00448         mCDB[group]->insert (key,new QString(value));
00449 }
00450 
00451 
00452 //====================================
00453 // return CDB data pointer
00454 //------------------------------------
00455 QDict< QDict<QString> > SaXStorage::getTablePointerCDB ( void ) {
00456         // .../
00459         // ----
00460         return mCDB;
00461 }
00462 
00463 //====================================
00464 // return CDB entry pointer 
00465 //------------------------------------
00466 QList< QDict<QString> > SaXStorage::getTablePointerCDB_DATA (
00467         const QString & group
00468 ) {
00469         // .../
00474         // ----
00475         QList< QDict<QString> > list;
00476         if ( mCDB[group] ) {
00477                 list.append (mCDB[group]);
00478         }
00479         return list;
00480 }
00481 
00482 //====================================
00483 // return mData data pointer
00484 //------------------------------------
00485 QList< QDict<QString> > SaXStorage::getTablePointerDATA ( void ) {
00486         // .../
00489         // ----
00490         return mData;
00491 }
00492 } // end namespace

Generated on Wed Sep 14 10:27:01 2005 for libsax by  doxygen 1.4.4