30 #include "networkaccount.h"
31 #include "accountmanager.h"
33 #include "globalsettings.h"
35 #include <tdeconfig.h>
36 #include <tdeio/global.h>
37 #include <tdelocale.h>
38 #include <tdemessagebox.h>
40 #include <tdewallet.h>
41 using TDEIO::MetaData;
42 using TDEWallet::Wallet;
50 static TQMap<TQString, int> s_serverConnections;
52 NetworkAccount::NetworkAccount(
AccountManager * parent,
const TQString & name, uint
id )
53 : KMAccount( parent, name, id ),
57 mStorePasswd( false ),
61 mPasswdDirty( false ),
62 mStorePasswdInConfig( false )
67 NetworkAccount::~NetworkAccount() {
71 void NetworkAccount::init() {
74 mSieveConfig = SieveConfig();
79 mPort = defaultPort();
92 void NetworkAccount::setLogin(
const TQString & login ) {
96 TQString NetworkAccount::passwd()
const {
97 if ( storePasswd() && mPasswd.isEmpty() )
98 mOwner->readPasswords();
99 return decryptStr( mPasswd );
102 void NetworkAccount::setPasswd(
const TQString & passwd,
bool storeInConfig ) {
103 if ( mPasswd != encryptStr( passwd ) ) {
104 mPasswd = encryptStr( passwd );
107 setStorePasswd( storeInConfig );
110 void NetworkAccount::clearPasswd() {
111 setPasswd(
"",
false );
114 void NetworkAccount::setAuth(
const TQString & auth ) {
118 void NetworkAccount::setStorePasswd(
bool store ) {
119 if( mStorePasswd != store && store )
121 mStorePasswd = store;
124 void NetworkAccount::setHost(
const TQString & host ) {
128 void NetworkAccount::setPort(
unsigned short int port ) {
132 void NetworkAccount::setUseSSL(
bool use ) {
136 void NetworkAccount::setUseTLS(
bool use ) {
140 void NetworkAccount::setSieveConfig(
const SieveConfig & config ) {
141 mSieveConfig = config;
150 void NetworkAccount::readConfig( TDEConfig & config ) {
151 KMAccount::readConfig( config );
153 setLogin( config.readEntry(
"login" ) );
155 if ( config.readNumEntry(
"store-passwd",
false ) ) {
157 TQString encpasswd = config.readEntry(
"pass" );
158 if ( encpasswd.isEmpty() ) {
159 encpasswd = config.readEntry(
"passwd" );
160 if ( !encpasswd.isEmpty() ) encpasswd = importPassword( encpasswd );
163 if ( !encpasswd.isEmpty() ) {
164 setPasswd( decryptStr( encpasswd ),
true );
166 if ( Wallet::isEnabled() ) {
167 config.deleteEntry(
"pass" );
168 config.deleteEntry(
"passwd" );
170 mStorePasswdInConfig =
false;
172 mPasswdDirty =
false;
173 mStorePasswdInConfig =
true;
177 if ( Wallet::isOpen( Wallet::NetworkWallet() ) )
182 setPasswd(
"",
false );
185 setHost( config.readEntry(
"host" ) );
187 unsigned int port = config.readUnsignedNumEntry(
"port", defaultPort() );
188 if ( port > USHRT_MAX ) port = defaultPort();
191 setAuth( config.readEntry(
"auth",
"*" ) );
192 setUseSSL( config.readBoolEntry(
"use-ssl",
false ) );
193 setUseTLS( config.readBoolEntry(
"use-tls",
false ) );
195 mSieveConfig.readConfig( config );
198 void NetworkAccount::writeConfig( TDEConfig & config ) {
199 KMAccount::writeConfig( config );
201 config.writeEntry(
"login", login() );
202 config.writeEntry(
"store-passwd", storePasswd() );
204 if ( storePasswd() ) {
206 bool passwdStored =
false;
207 if ( mPasswdDirty ) {
208 Wallet *wallet = kmkernel->wallet();
209 if ( wallet && wallet->writePassword(
"account-" + TQString::number(mId), passwd() ) == 0 ) {
211 mPasswdDirty =
false;
212 mStorePasswdInConfig =
false;
215 passwdStored = !mStorePasswdInConfig;
219 if ( !passwdStored && ( mStorePasswdInConfig || KMessageBox::warningYesNo( 0,
220 i18n(
"TDEWallet is not available. It is strongly recommended to use "
221 "TDEWallet for managing your passwords.\n"
222 "However, KMail can store the password in its configuration "
223 "file instead. The password is stored in an obfuscated format, "
224 "but should not be considered secure from decryption efforts "
225 "if access to the configuration file is obtained.\n"
226 "Do you want to store the password for account '%1' in the "
227 "configuration file?").arg( name() ),
228 i18n(
"TDEWallet Not Available"),
229 KGuiItem( i18n(
"Store Password") ),
230 KGuiItem( i18n(
"Do Not Store Password") ) )
231 == KMessageBox::Yes ) ) {
232 config.writeEntry(
"pass", encryptStr( passwd() ) );
233 mStorePasswdInConfig =
true;
238 if (!storePasswd() && !Wallet::keyDoesNotExist(
239 Wallet::NetworkWallet(),
"kmail",
"account-" + TQString::number(mId))) {
240 Wallet *wallet = kmkernel->wallet();
242 wallet->removeEntry(
"account-" + TQString::number(mId) );
245 config.writeEntry(
"host", host() );
246 config.writeEntry(
"port",
static_cast<unsigned int>( port() ) );
247 config.writeEntry(
"auth", auth() );
248 config.writeEntry(
"use-ssl", useSSL() );
249 config.writeEntry(
"use-tls", useTLS() );
251 mSieveConfig.writeConfig( config );
260 KURL NetworkAccount::getUrl()
const {
262 url.setProtocol( protocol() );
263 url.setUser( login() );
264 url.setPass( passwd() );
265 url.setHost( host() );
266 url.setPort( port() );
270 MetaData NetworkAccount::slaveConfig()
const {
272 m.insert(
"tls", useTLS() ?
"on" :
"off" );
276 void NetworkAccount::pseudoAssign(
const KMAccount * a ) {
277 KMAccount::pseudoAssign( a );
279 const NetworkAccount * n =
dynamic_cast<const NetworkAccount*
>( a );
282 setLogin( n->login() );
283 setPasswd( n->passwd(), n->storePasswd() );
284 setHost( n->host() );
285 setPort( n->port() );
286 setAuth( n->auth() );
287 setUseSSL( n->useSSL() );
288 setUseTLS( n->useTLS() );
289 setSieveConfig( n->sieveConfig() );
292 void NetworkAccount::readPassword() {
293 if ( !storePasswd() )
298 if ( Wallet::isOpen( Wallet::NetworkWallet() ) )
300 Wallet *wallet = kmkernel->wallet();
301 if (!wallet || !wallet->hasEntry(
"account-" + TQString::number(mId) ) )
306 if (Wallet::keyDoesNotExist( Wallet::NetworkWallet(),
"kmail",
"account-" + TQString::number(mId) ) )
310 if ( kmkernel->wallet() ) {
312 kmkernel->wallet()->readPassword(
"account-" + TQString::number(mId), passwd );
313 setPasswd( passwd,
true );
314 mPasswdDirty =
false;
318 void NetworkAccount::setCheckingMail(
bool checking )
320 mCheckingMail = checking;
321 if ( host().isEmpty() )
324 if ( s_serverConnections.find( host() ) != s_serverConnections.end() )
325 s_serverConnections[host()] += 1;
327 s_serverConnections[host()] = 1;
328 kdDebug(5006) <<
"check mail started - connections for host "
329 << host() <<
" now is "
330 << s_serverConnections[host()] << endl;
332 if ( s_serverConnections.find( host() ) != s_serverConnections.end() &&
333 s_serverConnections[host()] > 0 ) {
334 s_serverConnections[host()] -= 1;
335 kdDebug(5006) <<
"connections to server " << host()
336 <<
" now " << s_serverConnections[host()] << endl;
341 bool NetworkAccount::mailCheckCanProceed()
const
345 kdDebug(5006) <<
"for host " << host()
346 <<
" current connections="
347 << (s_serverConnections.find(host())==s_serverConnections.end() ? 0 : s_serverConnections[host()])
348 <<
" and limit is " << GlobalSettings::self()->maxConnectionsPerHost()
350 bool connectionLimitForHostReached = !host().isEmpty()
351 && GlobalSettings::self()->maxConnectionsPerHost() > 0
352 && s_serverConnections.find( host() ) != s_serverConnections.end()
353 && s_serverConnections[host()] >= GlobalSettings::self()->maxConnectionsPerHost();
354 kdDebug(5006) <<
"connection limit reached: "
355 << connectionLimitForHostReached << endl;
357 return ( !connectionLimitForHostReached && !offlineMode );
360 void NetworkAccount::resetConnectionList( NetworkAccount* acct )
362 s_serverConnections[ acct->host() ] = 0;
367 #include "networkaccount.moc"