summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/settings/ksettingsgpg.cpp
blob: 3950f4a49224b987e77d0b2b5d3e146e81ec6592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
                             ksettingsgpg.cpp
                             --------------------
    copyright            : (C) 2005, 2008 by Thomas Baumgart
    email                : ipwizard@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// QT Includes

#include <tqgroupbox.h>
#include <tqcheckbox.h>
#include <tqregexp.h>

// ----------------------------------------------------------------------------
// KDE Includes

#include <kled.h>
#include <klineedit.h>
#include <keditlistbox.h>
#include <kcombobox.h>
#include <tdemessagebox.h>
#include <tdelocale.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "ksettingsgpg.h"
#include <kmymoney/kgpgfile.h>

#define RECOVER_KEY_ID      "0xD2B08440"
#define RECOVER_KEY_ID_FULL "59B0F826D2B08440"

KSettingsGpg::KSettingsGpg(TQWidget* parent, const char* name) :
  KSettingsGpgDecl(parent, name),
  m_checkCount(0),
  m_needCheckList(true),
  m_listOk(false)
{
  setEnabled(KGPGFile::GPGAvailable());

  // don't show the widget in which the master key is actually kept
  kcfg_GpgRecipient->hide();

  connect(kcfg_WriteDataEncrypted, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotStatusChanged(bool)));
  connect(m_masterKeyCombo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotIdChanged()));
  connect(kcfg_GpgRecipientList, TQT_SIGNAL(changed()), this, TQT_SLOT(slotIdChanged()));
  connect(kcfg_GpgRecipientList, TQT_SIGNAL(added(const TQString&)), this, TQT_SLOT(slotKeyListChanged()));
  connect(kcfg_GpgRecipientList, TQT_SIGNAL(removed(const TQString&)), this, TQT_SLOT(slotKeyListChanged()));

  // Initial state setup
  slotStatusChanged(kcfg_WriteDataEncrypted->isChecked());
}

KSettingsGpg::~KSettingsGpg()
{
}

void KSettingsGpg::slotKeyListChanged(void)
{
  m_needCheckList = true;
  slotIdChanged();
}

void KSettingsGpg::slotIdChanged(void)
{
  // this looks a bit awkward. Here's why: KGPGFile::keyAvailable() starts
  // an external task and processes UI events while it waits for the external
  // process to finish. Thus, the first time we get here, the external process
  // is started and the user may press a second key which calls this routine
  // again.
  //
  // The second invocation is counted, but the check is not started until the
  // first one finishes. Once the external process finishes, we check if we
  // were called in the meantime and restart the check.
  if(++m_checkCount == 1) {
    while(1) {
      // first we check the current edit field if filled
      bool keysOk = true;
      if(!kcfg_GpgRecipientList->currentText().isEmpty()) {
        keysOk = KGPGFile::keyAvailable(kcfg_GpgRecipientList->currentText());
      }

      // if it is available, then scan the current list if we need to
      if(keysOk) {
        if(m_needCheckList) {
          TQStringList keys = kcfg_GpgRecipientList->items();
          TQStringList::const_iterator it_s;
          for(it_s = keys.begin(); keysOk && it_s != keys.end(); ++it_s) {
            if(!KGPGFile::keyAvailable(*it_s))
              keysOk = false;
          }
          m_listOk = keysOk;
          m_needCheckList = false;

        } else {
          keysOk = m_listOk;
        }
      }

      // did we receive some more requests to check?
      if(m_checkCount > 1) {
        m_checkCount = 1;
        continue;
      }

      // if we have a master key, we store it in the hidden widget
      if(m_masterKeyCombo->currentItem() != 0) {
        TQRegExp keyExp(".* \\((.*)\\)");
        if(keyExp.search(m_masterKeyCombo->currentText()) != -1) {
          kcfg_GpgRecipient->setText(keyExp.cap(1));
        }
      }

      m_userKeysFound->setState(static_cast<KLed::State>(keysOk && (kcfg_GpgRecipientList->items().count() != 0) ? KLed::On : KLed::Off));
      break;
    }

    --m_checkCount;
  }
}

void KSettingsGpg::show(void)
{
  TQString masterKey;

  if(m_masterKeyCombo->currentItem() != 0) {
    TQRegExp keyExp(".* \\((.*)\\)");
    if(keyExp.search(m_masterKeyCombo->currentText()) != -1) {
      masterKey = keyExp.cap(1);
    }
  } else
    masterKey = kcfg_GpgRecipient->text();

  // fill the secret key combobox with a fresh list
  m_masterKeyCombo->clear();
  TQStringList keyList;
  KGPGFile::secretKeyList(keyList);

  for(TQStringList::iterator it = keyList.begin(); it != keyList.end(); ++it) {
    TQStringList fields = TQStringList::split(":", *it);
    if(fields[0] != RECOVER_KEY_ID_FULL) {
      // replace parenthesis in name field with brackets
      TQString name = fields[1];
      name.replace('(', "[");
      name.replace(')', "]");
      name = TQString("%1 (0x%2)").arg(name).arg(fields[0]);
      m_masterKeyCombo->insertItem(name);
      if(name.contains(masterKey))
        m_masterKeyCombo->setCurrentItem(name);
    }
  }

  // if we don't have at least one secret key, we turn off encryption
  if(keyList.isEmpty()) {
    setEnabled(false);
    kcfg_WriteDataEncrypted->setChecked(false);
  }

  slotStatusChanged(kcfg_WriteDataEncrypted->isChecked());
  KSettingsGpgDecl::show();
}

void KSettingsGpg::slotStatusChanged(bool state)
{
  static bool oncePerSession = true;
  if(state && !KGPGFile::GPGAvailable())
    state = false;

  if((state == true) && (oncePerSession == true) && isVisible()) {
    KMessageBox::information(this, TQString("<qt>%1</qt>").arg(i18n("You have turned on the GPG encryption support. This means, that new files will be stored encrypted. Existing files will not be encrypted automatically.  To achieve encryption of existing files, please use the <b>File/Save as...</b> feature and store the file under a different name. Once confident with the result, feel free to delete the old file and rename the encrypted one to the old name.")), i18n("GPG encryption activated"), "GpgEncryptionActivated");
    oncePerSession = false;
  }

  m_idGroup->setEnabled(state);
  kcfg_EncryptRecover->setEnabled(state);
  m_masterKeyCombo->setEnabled(state);
  kcfg_GpgRecipientList->setEnabled(state);

  if(state) {
    m_recoverKeyFound->setState((KLed::State) (KGPGFile::keyAvailable(RECOVER_KEY_ID) ? KLed::On : KLed::Off));
    kcfg_EncryptRecover->setEnabled(m_recoverKeyFound->state() == KLed::On);
    slotIdChanged();

  } else {
    m_recoverKeyFound->setState(KLed::Off);
    m_userKeysFound->setState(KLed::Off);
  }
}

#include "ksettingsgpg.moc"