kitchensync Library API Documentation

idhelper.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002 Holger Freyther <freyther@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <kconfig.h>
00023 #include <kdebug.h>
00024 
00025 #include "idhelper.h"
00026 
00027 using namespace KSync;
00028 
00029 // TypeORAppName||%%||KonnectorId||%%||KDEID
00030 KonnectorUIDHelper::KonnectorUIDHelper( const QString &dir )
00031 {
00032 //    kdDebug(5201) << "new KonnectorUIDHelper " << dir <<endl;
00033     m_config = new KConfig( dir + "/konnector-ids.conf");
00034     m_config->setGroup("uids");
00035     QString string = m_config->readEntry( "ids" );
00036     QStringList list = QStringList::split( "%%||%%",  string );
00037 
00038     for ( QStringList::Iterator it = list.begin(); it != list.end() ; ++it ) {
00039         QStringList list2 = QStringList::split("||%%||",(*it), true ); // allow empty entries
00040         addId( list2[0],  list2[1], list2[2] );
00041     }
00042 
00043 }
00044 KonnectorUIDHelper::~KonnectorUIDHelper()
00045 {
00046     save();
00047     delete m_config;
00048 }
00049 QString KonnectorUIDHelper::konnectorId( const QString &appName,  const QString &kdeId, const QString &defaultId )
00050 {
00051 //    kdDebug(5201) << "IdHelper: KonnectorIdAppName: "
00052 //                  << appName << " KDE Id: "
00053 //                  << kdeId << " defaultId "
00054 //                  << defaultId << endl;
00055 
00056     QMap<QString,  Kontainer::ValueList >::Iterator it;
00057     it = m_ids.find( appName );
00058     if ( it != m_ids.end() ) {
00059         Kontainer::ValueList kontainer = it.data();
00060         Kontainer::ValueList::Iterator it;
00061         for ( it = kontainer.begin(); it != kontainer.end(); ++it ) {
00062             if ( kdeId.stripWhiteSpace() == (*it).second().stripWhiteSpace() ) {
00063 //                kdDebug(5201) << "it.first = " << (*it).first() << endl;
00064                 return (*it).first();
00065             }
00066         }
00067     }
00068     return defaultId;
00069 }
00070 QString KonnectorUIDHelper::kdeId( const QString &appName,  const QString &konnectorId, const QString &defaultId )
00071 {
00072 //    kdDebug(5201) << "kdeId: AppName: "
00073 //                  << appName  << " konnectorId "
00074 //                  << konnectorId << endl;
00075 
00076     QMap<QString,  Kontainer::ValueList >::Iterator it;
00077     it = m_ids.find( appName );
00078     if ( it != m_ids.end() ) {
00079         Kontainer::ValueList kontainer = it.data();
00080         Kontainer::ValueList::Iterator it;
00081         for ( it = kontainer.begin(); it != kontainer.end(); ++it ) {
00082             if ( konnectorId.stripWhiteSpace() == (*it).first().stripWhiteSpace() ) {
00083 //                kdDebug(5201) << "it.second " << (*it).second() << endl;
00084                 return (*it).second();
00085             }
00086         }
00087     }
00088     return defaultId;
00089 }
00090 void KonnectorUIDHelper::addId( const QString& appName,
00091                                 const QString& konnectorId,
00092                                 const QString& kdeId )
00093 {
00094 //    kdDebug(5201) << "addId " << appName
00095 //                  << "  konId "  << konnectorId
00096 //                  << " kdeId " << kdeId << endl;
00097 
00098     QMap<QString,  Kontainer::ValueList >::Iterator it;
00099     it = m_ids.find( appName );
00100 
00101     if ( it == m_ids.end() ) {
00102 //        kdDebug(5201) << "First insert" << endl;
00103         Kontainer::ValueList kontainer;
00104         kontainer.append( Kontainer( konnectorId,  kdeId ) );
00105         m_ids.replace( appName,  kontainer );
00106     }else{
00107 //        kdDebug(5201) << "Already inserted" << endl;
00108         Kontainer::ValueList kontainer = it.data();
00109         Kontainer kont( konnectorId,  kdeId );
00110         kontainer.remove( kont );
00111         kontainer.append( kont );
00112         m_ids.replace( appName,  kontainer );
00113     }
00114 }
00115 void KonnectorUIDHelper::removeId( const QString &appName,  const QString &id )
00116 {
00117     QMap<QString,  Kontainer::ValueList >::Iterator it;
00118     it = m_ids.find( appName );
00119     if ( it== m_ids.end() ) {
00120         Kontainer::ValueList kontainer = it.data();
00121         Kontainer::ValueList::Iterator it;
00122         for ( it = kontainer.begin(); it != kontainer.end(); ++it ) {
00123             if ( (*it).first() == id || (*it).second() == id ) {
00124                 it  = kontainer.remove( it );
00125                 return;
00126             }
00127         }
00128     }
00129 }
00130 void KonnectorUIDHelper::replaceIds( const QString &app,
00131                                      Kontainer::ValueList ids )
00132 {
00133     m_ids.replace( app,  ids );
00134 }
00135 void KonnectorUIDHelper::clear()
00136 {
00137     m_ids.clear();
00138     save();
00139 }
00140 void KonnectorUIDHelper::save()
00141 {
00142     QString string;
00143     QMap<QString,  Kontainer::ValueList >::Iterator mapIt;
00144     Kontainer::ValueList::Iterator kontainerIt;
00145     for ( mapIt = m_ids.begin(); mapIt != m_ids.end(); ++mapIt ) {
00146         for ( kontainerIt = mapIt.data().begin();
00147               kontainerIt != mapIt.data().end();
00148               ++kontainerIt ) {
00149 
00150             /*  AppName||%%||KonnectorId||%%||KDEID%%||%%AppName||%%||KonnectorId||%%||KDEID */
00151             //kdDebug() << mapIt.key() << " "
00152             //          << (*kontainerIt).first()
00153             //          << " " << (*kontainerIt).second() << endl;
00154 
00155             string.append(mapIt.key()+ "||%%||"
00156                           + (*kontainerIt).first() +
00157                           "||%%||" + (*kontainerIt).second()+ "%%||%%");
00158         }
00159     }
00160     m_config->writeEntry( "ids",  string );
00161     m_config->sync();
00162 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jan 31 15:53:45 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003