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
);
107 emitWalletOpened( 0 );
111 connect( d
->wallet
, SIGNAL( walletOpened(bool) ), SLOT( slotWalletChangedStatus() ) );
114 void Kopete::WalletManager::slotWalletChangedStatus()
116 kDebug(14010) << " isOpen: " << d
->wallet
->isOpen();
118 if( d
->wallet
->isOpen() )
120 if ( !d
->wallet
->hasFolder( QString::fromLatin1( "Kopete" ) ) )
121 d
->wallet
->createFolder( QString::fromLatin1( "Kopete" ) );
123 if ( d
->wallet
->setFolder( QString::fromLatin1( "Kopete" ) ) )
125 kDebug(14010) << "Successfully opened the wallet !";
127 QObject::connect( d
->wallet
, SIGNAL( walletClosed() ), this, SLOT( closeWallet() ) );
131 // opened OK, but we can't use it
143 emitWalletOpened( d
->wallet
);
146 void Kopete::WalletManager::slotGiveExistingWallet()
148 kDebug(14010) << " with d->wallet " << d
->wallet
;
152 // the wallet was already open
153 if ( d
->wallet
->isOpen() )
154 emitWalletOpened( d
->wallet
);
155 // if the wallet was not open, but d->wallet is not 0,
156 // then we're waiting for it to open, and will be told
157 // when it's done: do nothing.
159 kDebug(14010) << " wallet gone, waiting for another wallet";
163 // the wallet was lost between us trying to open it and
164 // getting called back. try to reopen it.
169 void Kopete::WalletManager::closeWallet()
171 if ( !d
->wallet
) return;
179 void Kopete::WalletManager::emitWalletOpened( KWallet::Wallet
*wallet
)
181 KopeteWalletSignal
*signal
= d
->signal
;
184 emit signal
->walletOpened( wallet
);
189 #include "kopetewalletmanager.moc"
191 // vim: set noet ts=4 sts=4 sw=4: