kitchensync

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., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, 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     }
00113 }
00114 void KonnectorUIDHelper::removeId( const QString &appName,  const QString &id )
00115 {
00116     QMap<QString,  Kontainer::ValueList >::Iterator it;
00117     it = m_ids.find( appName );
00118     if ( it != m_ids.end() ) {
00119         Kontainer::ValueList &kontainer = it.data();
00120         Kontainer::ValueList::Iterator it;
00121         for ( it = kontainer.begin(); it != kontainer.end(); ++it ) {
00122             if ( (*it).first == id || (*it).second == id ) {
00123                 it  = kontainer.remove( it );
00124                 return;
00125             }
00126         }
00127     }
00128 }
00129 void KonnectorUIDHelper::replaceIds( const QString &app,
00130                                      Kontainer::ValueList ids )
00131 {
00132     m_ids.replace( app,  ids );
00133 }
00134 void KonnectorUIDHelper::clear()
00135 {
00136     m_ids.clear();
00137     save();
00138 }
00139 void KonnectorUIDHelper::save()
00140 {
00141     QString string;
00142     QMap<QString,  Kontainer::ValueList >::Iterator mapIt;
00143     Kontainer::ValueList::Iterator kontainerIt;
00144     for ( mapIt = m_ids.begin(); mapIt != m_ids.end(); ++mapIt ) {
00145         for ( kontainerIt = mapIt.data().begin();
00146               kontainerIt != mapIt.data().end();
00147               ++kontainerIt ) {
00148 
00149             /*  AppName||%%||KonnectorId||%%||KDEID%%||%%AppName||%%||KonnectorId||%%||KDEID */
00150             //kdDebug() << mapIt.key() << " "
00151             //          << (*kontainerIt).first()
00152             //          << " " << (*kontainerIt).second() << endl;
00153 
00154             string.append(mapIt.key()+ "||%%||"
00155                           + (*kontainerIt).first +
00156                           "||%%||" + (*kontainerIt).second+ "%%||%%");
00157         }
00158     }
00159     m_config->writeEntry( "ids",  string );
00160     m_config->sync();
00161 }
KDE Home | KDE Accessibility Home | Description of Access Keys