Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / kopetewalletmanager.cpp
blob6b50541e3f52ec30d94f0137a8cac2eabd752d9a
1 /*
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 *************************************************************************
8 * *
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. *
13 * *
14 *************************************************************************
17 #include "kopetewalletmanager.h"
19 #include "kopeteuiglobal.h"
21 #include <kdebug.h>
22 #include <kwallet.h>
24 #include <qtimer.h>
25 #include <qwidget.h>
26 #include <qapplication.h>
28 static WId mainWindowID()
30 if ( QWidget *w = Kopete::UI::Global::mainWidget() )
31 return w->winId();
32 return 0;
35 class Kopete::WalletManager::Private
37 public:
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
47 // to receive it.
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()
54 : d( new Private )
58 Kopete::WalletManager::~WalletManager()
60 closeWallet();
61 delete d;
64 Kopete::WalletManager *Kopete::WalletManager::self()
66 static WalletManager s;
67 return &s;
70 void Kopete::WalletManager::openWallet( QObject *object, const char *slot )
72 if ( !d->signal )
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 );
77 openWalletInner();
80 void Kopete::WalletManager::openWalletInner()
82 // do we already have a wallet?
83 if ( d->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() ) );
92 else
94 kDebug(14010) << " still waiting for earlier request";
96 return;
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 if ( !d->wallet )
107 emitWalletOpened( 0 );
108 return;
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 !";
126 // success!
127 QObject::connect( d->wallet, SIGNAL( walletClosed() ), this, SLOT( closeWallet() ) );
129 else
131 // opened OK, but we can't use it
132 delete d->wallet;
133 d->wallet = 0;
136 else
138 // failed to open
139 delete d->wallet;
140 d->wallet = 0;
143 emitWalletOpened( d->wallet );
146 void Kopete::WalletManager::slotGiveExistingWallet()
148 kDebug(14010) << " with d->wallet " << d->wallet;
150 if ( 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.
158 else
159 kDebug(14010) << " wallet gone, waiting for another wallet";
161 else
163 // the wallet was lost between us trying to open it and
164 // getting called back. try to reopen it.
165 openWalletInner();
169 void Kopete::WalletManager::closeWallet()
171 if ( !d->wallet ) return;
173 delete d->wallet;
174 d->wallet = 0L;
176 emit walletLost();
179 void Kopete::WalletManager::emitWalletOpened( KWallet::Wallet *wallet )
181 KopeteWalletSignal *signal = d->signal;
182 d->signal = 0;
183 if ( signal )
184 emit signal->walletOpened( wallet );
185 delete signal;
189 #include "kopetewalletmanager.moc"
191 // vim: set noet ts=4 sts=4 sw=4: