• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.14
  • kdepim
  • libkdepim
  • ldap
ldapclientsearchconfig.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2015 Laurent Montel <montel@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "ldapclientsearchconfig.h"
21 #include <kldap/ldapserver.h>
22 
23 
24 #include <KConfig>
25 #include <KConfigGroup>
26 #include <KMessageBox>
27 #include <KLocale>
28 
29 #include <kwallet.h>
30 
31 using namespace KLDAP;
32 
33 class LdapClientSearchConfig::Private
34 {
35 public:
36  Private()
37  : useWallet( false ),
38  wallet( 0 )
39  {
40 
41  }
42  ~Private()
43  {
44  if (useWallet) {
45  wallet->deleteLater();
46  wallet = 0;
47  }
48  }
49  bool useWallet;
50  KWallet::Wallet* wallet;
51 };
52 
53 K_GLOBAL_STATIC_WITH_ARGS( KConfig, s_config, ( QLatin1String("kabldaprc"), KConfig::NoGlobals ) )
54 
55 KConfig* LdapClientSearchConfig::config()
56 {
57  return s_config;
58 }
59 
60 LdapClientSearchConfig::LdapClientSearchConfig(QObject *parent)
61  : QObject(parent), d(new LdapClientSearchConfig::Private())
62 {
63 }
64 
65 LdapClientSearchConfig::~LdapClientSearchConfig()
66 {
67  delete d;
68 }
69 
70 void LdapClientSearchConfig::readConfig( KLDAP::LdapServer &server, KConfigGroup &config, int j, bool active )
71 {
72  QString prefix;
73  if ( active ) {
74  prefix = QLatin1String("Selected");
75  }
76 
77  const QString host = config.readEntry( prefix + QString::fromLatin1( "Host%1" ).arg( j ),
78  QString() ).trimmed();
79  if ( !host.isEmpty() ) {
80  server.setHost( host );
81  }
82 
83  const int port = config.readEntry( prefix + QString::fromLatin1( "Port%1" ).arg( j ), 389 );
84  server.setPort( port );
85 
86  const QString base = config.readEntry( prefix + QString::fromLatin1( "Base%1" ).arg( j ),
87  QString() ).trimmed();
88  if ( !base.isEmpty() ) {
89  server.setBaseDn( KLDAP::LdapDN( base ) );
90  }
91 
92  const QString user = config.readEntry( prefix + QString::fromLatin1( "User%1" ).arg( j ),
93  QString() ).trimmed();
94  if ( !user.isEmpty() ) {
95  server.setUser( user );
96  }
97 
98  const QString bindDN = config.readEntry( prefix + QString::fromLatin1( "Bind%1" ).arg( j ), QString() ).trimmed();
99  if ( !bindDN.isEmpty() ) {
100  server.setBindDn( bindDN );
101  }
102 
103  const QString pwdBindBNEntry = prefix + QString::fromLatin1( "PwdBind%1" ).arg( j );
104  QString pwdBindDN = config.readEntry( pwdBindBNEntry, QString() );
105  if ( !pwdBindDN.isEmpty() ) {
106  if ( KMessageBox::Yes == KMessageBox::questionYesNo(0, i18n("LDAP password is stored as clear text, do you want to store it in kwallet?"),
107  i18n("Store clear text password in KWallet"),
108  KStandardGuiItem::yes(),
109  KStandardGuiItem::no(),
110  QLatin1String("DoAskToStoreToKwallet"))) {
111  d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
112  if ( d->wallet ) {
113  connect(d->wallet, SIGNAL(walletClosed()), SLOT(slotWalletClosed()));
114  d->useWallet = true;
115  if ( !d->wallet->hasFolder( QLatin1String("ldapclient") ) ) {
116  d->wallet->createFolder( QLatin1String("ldapclient") );
117  }
118  d->wallet->setFolder( QLatin1String("ldapclient") );
119  d->wallet->writePassword(pwdBindBNEntry, pwdBindDN );
120  config.deleteEntry(pwdBindBNEntry);
121  config.sync();
122  }
123  }
124  server.setPassword( pwdBindDN );
125  } else { //Look at in Wallet
126  d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
127  if ( d->wallet ) {
128  d->useWallet = true;
129  if ( !d->wallet->setFolder( QLatin1String("ldapclient") ) ) {
130  d->wallet->createFolder( QLatin1String("ldapclient") );
131  d->wallet->setFolder( QLatin1String("ldapclient") );
132  }
133  d->wallet->readPassword( pwdBindBNEntry, pwdBindDN );
134  if (!pwdBindDN.isEmpty())
135  server.setPassword( pwdBindDN );
136  } else {
137  d->useWallet = false;
138  }
139  }
140 
141  server.setTimeLimit( config.readEntry( prefix + QString::fromLatin1( "TimeLimit%1" ).arg( j ), 0 ) );
142  server.setSizeLimit( config.readEntry( prefix + QString::fromLatin1( "SizeLimit%1" ).arg( j ), 0 ) );
143  server.setPageSize( config.readEntry( prefix + QString::fromLatin1( "PageSize%1" ).arg( j ), 0 ) );
144  server.setVersion( config.readEntry( prefix + QString::fromLatin1( "Version%1" ).arg( j ), 3 ) );
145 
146  QString tmp;
147  tmp = config.readEntry( prefix + QString::fromLatin1( "Security%1" ).arg( j ),
148  QString::fromLatin1( "None" ) );
149  server.setSecurity( KLDAP::LdapServer::None );
150  if ( tmp == QLatin1String("SSL") ) {
151  server.setSecurity( KLDAP::LdapServer::SSL );
152  } else if ( tmp == QLatin1String("TLS") ) {
153  server.setSecurity( KLDAP::LdapServer::TLS );
154  }
155 
156  tmp = config.readEntry( prefix + QString::fromLatin1( "Auth%1" ).arg( j ),
157  QString::fromLatin1( "Anonymous" ) );
158  server.setAuth( KLDAP::LdapServer::Anonymous );
159  if ( tmp == QLatin1String("Simple") ) {
160  server.setAuth( KLDAP::LdapServer::Simple );
161  } else if ( tmp == QLatin1String("SASL") ) {
162  server.setAuth( KLDAP::LdapServer::SASL );
163  }
164 
165  server.setMech( config.readEntry( prefix + QString::fromLatin1( "Mech%1" ).arg( j ), QString() ) );
166  server.setFilter( config.readEntry( prefix + QString::fromLatin1( "UserFilter%1" ).arg( j ), QString() ) );
167 }
168 
169 void LdapClientSearchConfig::writeConfig( const KLDAP::LdapServer &server, KConfigGroup &config, int j, bool active )
170 {
171  QString prefix;
172  if ( active ) {
173  prefix = QLatin1String("Selected");
174  }
175 
176  config.writeEntry( prefix + QString::fromLatin1( "Host%1" ).arg( j ), server.host() );
177  config.writeEntry( prefix + QString::fromLatin1( "Port%1" ).arg( j ), server.port() );
178  config.writeEntry( prefix + QString::fromLatin1( "Base%1" ).arg( j ), server.baseDn().toString() );
179  config.writeEntry( prefix + QString::fromLatin1( "User%1" ).arg( j ), server.user() );
180  config.writeEntry( prefix + QString::fromLatin1( "Bind%1" ).arg( j ), server.bindDn() );
181 
182  const QString passwordEntry = prefix + QString::fromLatin1( "PwdBind%1" ).arg( j );
183  const QString password = server.password();
184  if (!password.isEmpty()) {
185  if (d->useWallet && !d->wallet) {
186  d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
187  }
188  if (d->wallet) {
189  d->wallet->writePassword(passwordEntry, password );
190  } else {
191  config.writeEntry( passwordEntry, password );
192  d->useWallet = false;
193  }
194  }
195 
196  config.writeEntry( prefix + QString::fromLatin1( "TimeLimit%1" ).arg( j ), server.timeLimit() );
197  config.writeEntry( prefix + QString::fromLatin1( "SizeLimit%1" ).arg( j ), server.sizeLimit() );
198  config.writeEntry( prefix + QString::fromLatin1( "PageSize%1" ).arg( j ), server.pageSize() );
199  config.writeEntry( prefix + QString::fromLatin1( "Version%1" ).arg( j ), server.version() );
200  QString tmp;
201  switch ( server.security() ) {
202  case KLDAP::LdapServer::TLS:
203  tmp = QLatin1String("TLS");
204  break;
205  case KLDAP::LdapServer::SSL:
206  tmp = QLatin1String("SSL");
207  break;
208  default:
209  tmp = QLatin1String("None");
210  }
211  config.writeEntry( prefix + QString::fromLatin1( "Security%1" ).arg( j ), tmp );
212  switch ( server.auth() ) {
213  case KLDAP::LdapServer::Simple:
214  tmp = QLatin1String("Simple");
215  break;
216  case KLDAP::LdapServer::SSL:
217  tmp = QLatin1String("SASL");
218  break;
219  default:
220  tmp = QLatin1String("Anonymous");
221  }
222  config.writeEntry( prefix + QString::fromLatin1( "Auth%1" ).arg( j ), tmp );
223  config.writeEntry( prefix + QString::fromLatin1( "Mech%1" ).arg( j ), server.mech() );
224  config.writeEntry( prefix + QString::fromLatin1( "UserFilter%1" ).arg( j ), server.filter().trimmed() );
225 }
226 
227 void LdapClientSearchConfig::slotWalletClosed()
228 {
229  delete d->wallet;
230  d->wallet = 0;
231 }
232 
233 
QObject
ldapclientsearchconfig.h
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
QString
K_GLOBAL_STATIC_WITH_ARGS
K_GLOBAL_STATIC_WITH_ARGS(KConfig, s_config,(QLatin1String("kabldaprc"), KConfig::NoGlobals)) KConfig *LdapClientSearchConfig
Definition: ldapclientsearchconfig.cpp:53
QLatin1String
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal