~ubuntu-branches/ubuntu/trusty/kwalletmanager/trusty-proposed

« back to all changes in this revision

Viewing changes to src/manager/walletcontrolwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2014-03-04 21:07:36 UTC
  • Revision ID: package-import@ubuntu.com-20140304210736-b174n11viq7zvdig
Tags: upstream-4.12.3
ImportĀ upstreamĀ versionĀ 4.12.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2013 Valentin Rusu <kde@rusu.info>
 
3
 
 
4
   This program is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; see the file COPYING.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "walletcontrolwidget.h"
 
21
#include "kwalleteditor.h"
 
22
#include "applicationsmanager.h"
 
23
 
 
24
#include <QPropertyAnimation>
 
25
#include <QTimer>
 
26
#include <QFrame>
 
27
#include <QToolButton>
 
28
#include <qevent.h>
 
29
#include <kwallet.h>
 
30
#include <kmessagebox.h>
 
31
#include <kmenu.h>
 
32
#include <KTabWidget>
 
33
#include <kdebug.h>
 
34
 
 
35
WalletControlWidget::WalletControlWidget(QWidget* parent, const QString& walletName):
 
36
    QWidget(parent),
 
37
    _walletName(walletName),
 
38
    _wallet(0),
 
39
    _walletEditor(0),
 
40
    _applicationsManager(0)
 
41
{
 
42
    setupUi(this);
 
43
    onSetupWidget();
 
44
 
 
45
    QTimer::singleShot(1, this, SLOT(onSetupWidget()));
 
46
}
 
47
 
 
48
bool WalletControlWidget::openWallet()
 
49
{
 
50
    bool result = false;
 
51
    if (_wallet && _wallet->isOpen()) {
 
52
        result = true; // already opened
 
53
    } else {
 
54
        _wallet = KWallet::Wallet::openWallet(_walletName, winId());
 
55
        result = _wallet != 0;
 
56
        onSetupWidget();
 
57
    }
 
58
    return result;
 
59
}
 
60
 
 
61
void WalletControlWidget::onSetupWidget()
 
62
{
 
63
    if (KWallet::Wallet::isOpen(_walletName)) {
 
64
        if (0 == _wallet) {
 
65
            _wallet = KWallet::Wallet::openWallet(_walletName, winId());
 
66
            if (0 == _wallet) {
 
67
                kDebug() << "Weird situation: wallet could not be opened when setting-up the widget.";
 
68
            }
 
69
        }
 
70
    }
 
71
 
 
72
    if (_wallet) {
 
73
        connect(_wallet, SIGNAL(walletClosed()), this, SLOT(onWalletClosed()));
 
74
        _openClose->setText(i18n("&Close"));
 
75
 
 
76
        if (0 == _walletEditor) {
 
77
            _walletEditor = new KWalletEditor(_editorFrame);
 
78
            _editorFrameLayout->addWidget(_walletEditor);
 
79
            _walletEditor->setVisible(true);
 
80
        }
 
81
        _walletEditor->setWallet(_wallet);
 
82
 
 
83
        if (0 == _applicationsManager) {
 
84
            _applicationsManager = new ApplicationsManager(_applicationsFrame);
 
85
            _applicationsFrameLayout->addWidget(_applicationsManager);
 
86
            _applicationsManager->setVisible(true);
 
87
        }
 
88
        _applicationsManager->setWallet(_wallet);
 
89
 
 
90
        _changePassword->setEnabled(true);
 
91
        _stateLabel->setText(i18nc("the 'kdewallet' is currently open (e.g. %1 will be replaced with current wallet name)", "The '%1' wallet is currently open", _walletName));
 
92
        _tabs->setTabIcon(0, QIcon::fromTheme( QLatin1String("wallet-open")).pixmap(16));
 
93
    } else {
 
94
        _openClose->setText(i18n("&Open..."));
 
95
 
 
96
        if (_walletEditor) {
 
97
            _walletEditor->setVisible(false);
 
98
            delete _walletEditor, _walletEditor =0;
 
99
        }
 
100
 
 
101
        if (_applicationsManager) {
 
102
            _applicationsManager->setVisible(false);
 
103
            delete _applicationsManager, _applicationsManager = 0;
 
104
        }
 
105
        _changePassword->setEnabled(false);
 
106
        _stateLabel->setText(i18n("The wallet is currently closed"));
 
107
        _tabs->setTabIcon(0, QIcon::fromTheme( QLatin1String("wallet-closed")).pixmap(16));
 
108
    }
 
109
}
 
110
 
 
111
void WalletControlWidget::onOpenClose()
 
112
{
 
113
    // TODO create some fancy animation here to make _walletEditor appear or dissapear in a fancy way
 
114
    if (_wallet) {
 
115
        // Wallet is open, attempt close it
 
116
        int rc = KWallet::Wallet::closeWallet(_walletName, false);
 
117
        if (rc != 0) {
 
118
            rc = KMessageBox::warningYesNo(this, i18n("Unable to close wallet cleanly. It is probably in use by other applications. Do you wish to force it closed?"), QString(), KGuiItem(i18n("Force Closure")), KGuiItem(i18n("Do Not Force")));
 
119
            if (rc == KMessageBox::Yes) {
 
120
                rc = KWallet::Wallet::closeWallet(_walletName, true);
 
121
                if (rc != 0) {
 
122
                    KMessageBox::sorry(this, i18n("Unable to force the wallet closed. Error code was %1.", rc));
 
123
                } else {
 
124
                    _wallet = 0;
 
125
                }
 
126
            }
 
127
        } else {
 
128
            _wallet = 0;
 
129
        }
 
130
    }
 
131
    else {
 
132
        _wallet = KWallet::Wallet::openWallet(_walletName, winId());
 
133
    }
 
134
    onSetupWidget();
 
135
}
 
136
 
 
137
void WalletControlWidget::onWalletClosed()
 
138
{
 
139
    _wallet = 0;
 
140
    onSetupWidget();
 
141
}
 
142
 
 
143
void WalletControlWidget::updateWalletDisplay()
 
144
{
 
145
//     QList<QAction*> existingActions = _disconnect->actions();
 
146
//     QList<QAction*>::const_iterator i = existingActions.constBegin();
 
147
//     QList<QAction*>::const_iterator ie = existingActions.constEnd();
 
148
//     for ( ; i != ie; i++ ) {
 
149
//         _disconnect->removeAction(*i);
 
150
//     }
 
151
// 
 
152
}
 
153
 
 
154
void WalletControlWidget::onDisconnectApplication()
 
155
{
 
156
    QAction *a = qobject_cast<QAction *>(sender());
 
157
    Q_ASSERT(a);
 
158
    if (a)  {
 
159
        KWallet::Wallet::disconnectApplication(_walletName, a->data().toString());
 
160
    }
 
161
}
 
162
 
 
163
void WalletControlWidget::onChangePassword()
 
164
{
 
165
    KWallet::Wallet::changePassword(_walletName, winId());
 
166
}
 
167
 
 
168
void WalletControlWidget::hideEvent(QHideEvent* )
 
169
{
 
170
}
 
171
 
 
172
void WalletControlWidget::showEvent(QShowEvent* ev)
 
173
{
 
174
}
 
175
 
 
176
#include "walletcontrolwidget.moc"