make webinterface translatable. there are around 20 short strings, all with context...
[kdenetwork.git] / kopete / libkopete / kopetewalletmanager.cpp
blobf169fe3c0dcfc2c154d8efe72423c7cba4aabb4b
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 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 !";
120 // success!
121 QObject::connect( d->wallet, SIGNAL( walletClosed() ), this, SLOT( closeWallet() ) );
123 else
125 // opened OK, but we can't use it
126 delete d->wallet;
127 d->wallet = 0;
130 else
132 // failed to open
133 delete d->wallet;
134 d->wallet = 0;
137 emitWalletOpened( d->wallet );
140 void Kopete::WalletManager::slotGiveExistingWallet()
142 kDebug(14010) << " with d->wallet " << d->wallet;
144 if ( 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.
152 else
153 kDebug(14010) << " wallet gone, waiting for another wallet";
155 else
157 // the wallet was lost between us trying to open it and
158 // getting called back. try to reopen it.
159 openWalletInner();
163 void Kopete::WalletManager::closeWallet()
165 if ( !d->wallet ) return;
167 delete d->wallet;
168 d->wallet = 0L;
170 emit walletLost();
173 void Kopete::WalletManager::emitWalletOpened( KWallet::Wallet *wallet )
175 KopeteWalletSignal *signal = d->signal;
176 d->signal = 0;
177 if ( signal )
178 emit signal->walletOpened( wallet );
179 delete signal;
183 #include "kopetewalletmanager.moc"
185 // vim: set noet ts=4 sts=4 sw=4: