kconfigldapbackend.cpp

00001 /*
00002   This file is part of the KDE libraries
00003   Copyright (c) 2005 Willem Verschuur <willverschuur@yahoo.com>
00004   Copyright (c) 2006 Helio Chissini de Castro <helio@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 <iostream>
00023 
00024 #include "kconfigldapbackend.h"
00025 #include "kconfigdata.h"
00026 #include "kuser.h"
00027 #include "xldaplite.h"
00028 
00029 KConfigLDAPBackEnd::KConfigLDAPBackEnd( KConfigBase *config, 
00030                                         const QString &fileName,
00031                                         const char * resType, 
00032                                         bool useKDEGlobals )
00033 
00034     :   KConfigBackEnd(config, fileName, resType, useKDEGlobals)
00035 
00036 {
00037    _fileImmutable = false;
00038 }
00039 
00040 
00041 
00042 
00043 KConfigLDAPBackEnd::~KConfigLDAPBackEnd()
00044 {
00045 }
00046 
00047 
00048 
00049 bool KConfigLDAPBackEnd::parseConfigFiles()
00050 {
00051     // will result in an invalid search anyway..
00052     if (mfileName.isEmpty()) 
00053        return false;
00054 
00055     // blacklists some entries to avoid uselless queries
00056     if (  mfileName == "kdebugrc" 
00057           || mfileName == "kconf_updaterc" 
00058           || mfileName == "kdedrc"
00059           || mfileName == "kdeinitrc"
00060           || mfileName == "kbuildsycocarc"
00061           || mfileName.startsWith( "/" ) )
00062        return false;
00063 
00064     // connect to ldap server
00065     xLDAPconnection ldap;
00066     if (!ldap.connect()) return false;
00067 
00068     // User and profilelist
00069     KUser user;
00070     QString basedn = QString( "cn=%1,ou=default,ou=KDEConfig,%2" ).arg( mfileName ).arg( ldap.base() );
00071     QStringList profiles( basedn );
00072 
00073     // Query group profile
00074     basedn = QString("uid=%1,ou=People,%2").arg(user.loginName()).arg(ldap.base());
00075     char *attrs[] = { "kdeConfigEntry", 0 };
00076     xLDAPquery query( ldap, "(objectClass=posixAccount)", basedn, attrs );
00077 
00078     for ( xLDAPiterator section(query); section; ++section )
00079     {
00080          for ( xLDAPattribute gattr(section); gattr; ++gattr )
00081          {
00082           basedn = QString( "cn=%1,%2" ).arg( mfileName ).arg( gattr.firstValue() );
00083           profiles.push_front( basedn );
00084          }
00085     }
00086     
00087     for ( QStringList::Iterator it = profiles.begin(); it != profiles.end(); ++it ) 
00088     {
00089        // File immutable
00090        xLDAPquery filequery( ldap, "(objectClass=appConfig)", *it );
00091        for ( xLDAPiterator section( filequery ); section; ++section )
00092        {
00093           for ( xLDAPattribute gattr(section); gattr; ++gattr )
00094           {
00095              if (!strcmp(gattr.attribute(), "immutable")) // immutable
00096                 setFileImmutable(0 == strcmp(gattr.firstValue(), "TRUE"));
00097           }
00098        }
00099        
00100        xLDAPquery query( ldap, "(objectClass=appConfigSection)", *it );
00101        
00102        // write results to application settings 
00103        // iterate through sections/groups
00104        for ( xLDAPiterator section(query); section; ++section )
00105        {
00106           QCString aCurrentGroup = "<default>";
00107           bool groupimmutable = false;
00108           // find out group name and flags
00109           for ( xLDAPattribute gattr(section); gattr; ++gattr )
00110           {
00111              // section name
00112              if (!strcmp(gattr.attribute(), "cn"))
00113                 aCurrentGroup = gattr.firstValue();
00114              else if (!strcmp(gattr.attribute(), "immutable"))
00115              {
00116                 if ( getFileImmutable() )
00117                    groupimmutable = true;
00118                 else
00119                    groupimmutable = (0 == strcmp(gattr.firstValue(), "TRUE"));
00120              }
00121              if ( groupimmutable )
00122                 setGroupImmutable( aCurrentGroup, true );
00123           }
00124           
00125           // backwards compatibility
00126           if (aCurrentGroup == "KDE Desktop Entry")
00127              aCurrentGroup = "Desktop Entry";
00128           
00129           KEntryKey groupKey(aCurrentGroup, 0);
00130           KEntry entry = pConfig->lookupData(groupKey);
00131           bool groupSkip = entry.bImmutable;
00132           
00133           if (groupSkip)
00134              continue;
00135           
00136           entry.bImmutable |= groupimmutable;
00137           pConfig->putData(groupKey, entry, false);
00138   
00139           // set attribute values
00140           //std::cout << "LDAP: " << "[ " << aCurrentGroup << " ] " <<  std::endl;
00141           for ( xLDAPattribute attr(section);  attr; ++attr )
00142           {
00143              if (strcmp(attr.attribute(), "appconfigentry")) continue;
00144              for ( char* cattr = (char*) attr.firstValue();
00145                    cattr;
00146                    cattr = (char*) attr.nextValue() )
00147              {
00148                 QCString cattribute((const char*) cattr); 
00149                 cattr = cattribute.data();
00150                 char* ceq = (char*) strchr(cattr, '=');
00151                 if (!ceq) 
00152                    continue;
00153                 
00154                 *ceq = 0;
00155                 char* cvalue = ++ceq;
00156                 
00157                 // insert the key/value line
00158                 KEntryKey aEntryKey(aCurrentGroup, cattr);
00159                 aEntryKey.bLocal = false; //(locale != 0);
00160                 aEntryKey.bDefault = false; //bDefault; meep?
00161                 
00162                 //std::cout << "LDAP: " << cattr << " = " << cvalue <<  std::endl;
00163                 
00164                 KEntry aEntry;
00165                 aEntry.mValue = cvalue;
00166                 aEntry.bGlobal = true; //bGlobal;
00167                 aEntry.bImmutable = false; //optionImmutable;
00168                 aEntry.bDeleted = false; // optionDeleted;
00169                 aEntry.bExpand = false; // optionExpand;
00170                 aEntry.bNLS = false; //(locale != 0);
00171                 
00172                 pConfig->putData( aEntryKey, // group
00173                       aEntry,    // entry
00174                       false );   
00175              }
00176           }
00177        }
00178     }
00179     // return successful
00180     return true;
00181 }
00182 
00183 
00184 
00185 void KConfigLDAPBackEnd::sync(bool bMerge)
00186 {
00187     // write back to ldap server..
00188 }
00189 
00190 
00191 
00192 void KConfigLDAPBackEnd::virtual_hook(int id, void* data)
00193 {
00194     KConfigBackEnd::virtual_hook( id, data );
00195 }
00196 
KDE Home | KDE Accessibility Home | Description of Access Keys