2 kopetewalletmanager.cpp - Kopete Wallet Manager
4 Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
5 Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
7 *************************************************************************
9 * This library is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU Lesser General Public *
11 * License as published by the Free Software Foundation; either *
12 * version 2 of the License, or (at your option) any later version. *
14 *************************************************************************
17 #include "kopetewalletmanager.h"
19 #include "kopeteuiglobal.h"
26 #include <qapplication.h>
28 static WId
mainWindowID()
30 if ( QWidget
*w
= Kopete::UI::Global::mainWidget() )
35 class Kopete::WalletManager::Private
38 Private() : wallet(0), signal(0) {}
39 ~Private() { delete wallet
; delete signal
; }
41 KWallet::Wallet
*wallet
;
43 // we can't just connect every slot that wants the wallet to the
44 // walletOpened signal - since we disconnect all the slots immediately
45 // after emitting the signal, this would result in everyone who asked
46 // for the wallet again in response to a walletOpened signal to fail
48 // instead, we store a KopeteWalletSignal which we connect to, and create
49 // a new one for each set of requests.
50 KopeteWalletSignal
*signal
;
53 Kopete::WalletManager::WalletManager()
58 Kopete::WalletManager::~WalletManager()
64 Kopete::WalletManager
*Kopete::WalletManager::self()
66 static WalletManager s
;
70 void Kopete::WalletManager::openWallet( QObject
*object
, const char *slot
)
73 d
->signal
= new KopeteWalletSignal
;
74 // allow connecting to protected slots by calling object->connect
75 connect( d
->signal
, SIGNAL( walletOpened( KWallet::Wallet
* ) ), object
, slot
);
76 //object->connect( d->signal, SIGNAL( walletOpened( KWallet::Wallet* ) ), slot );
80 void Kopete::WalletManager::openWalletInner()
82 // do we already have a wallet?
85 // if the wallet isn't open yet, we're pending a slotWalletChangedStatus
86 // anyway, so we don't set up a single shot.
87 if ( d
->wallet
->isOpen() )
89 kDebug(14010) << " wallet already open";
90 QTimer::singleShot( 0, this, SLOT( slotGiveExistingWallet() ) );
94 kDebug(14010) << " still waiting for earlier request";
99 kDebug(14010) << " about to open wallet async";
101 // we have no wallet: ask for one.
102 d
->wallet
= KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(),
103 mainWindowID(), KWallet::Wallet::Asynchronous
);
105 connect( d
->wallet
, SIGNAL( walletOpened(bool) ), SLOT( slotWalletChangedStatus() ) );
108 void Kopete::WalletManager::slotWalletChangedStatus()
110 kDebug(14010) << " isOpen: " << d
->wallet
->isOpen();
112 if( d
->wallet
->isOpen() )
114 if ( !d
->wallet
->hasFolder( QString::fromLatin1( "Kopete" ) ) )
115 d
->wallet
->createFolder( QString::fromLatin1( "Kopete" ) );
117 if ( d
->wallet
->setFolder( QString::fromLatin1( "Kopete" ) ) )
119 kDebug(14010) << "Succesfully opened the wallet !";
121 QObject::connect( d
->wallet
, SIGNAL( walletClosed() ), this, SLOT( closeWallet() ) );
125 // opened OK, but we can't use it
137 emitWalletOpened( d
->wallet
);
140 void Kopete::WalletManager::slotGiveExistingWallet()
142 kDebug(14010) << " with d->wallet " << d
->wallet
;
146 // the wallet was already open
147 if ( d
->wallet
->isOpen() )
148 emitWalletOpened( d
->wallet
);
149 // if the wallet was not open, but d->wallet is not 0,
150 // then we're waiting for it to open, and will be told
151 // when it's done: do nothing.
153 kDebug(14010) << " wallet gone, waiting for another wallet";
157 // the wallet was lost between us trying to open it and
158 // getting called back. try to reopen it.
163 void Kopete::WalletManager::closeWallet()
165 if ( !d
->wallet
) return;
173 void Kopete::WalletManager::emitWalletOpened( KWallet::Wallet
*wallet
)
175 KopeteWalletSignal
*signal
= d
->signal
;
178 emit signal
->walletOpened( wallet
);
183 #include "kopetewalletmanager.moc"
185 // vim: set noet ts=4 sts=4 sw=4: