• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
kgpgfirstassistant.cpp
Go to the documentation of this file.
1 /* Copyright 2008,2010,2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License as
5  * published by the Free Software Foundation; either version 2 of
6  * the License or (at your option) version 3 or any later version
7  * accepted by the membership of KDE e.V. (or its successor approved
8  * by the membership of KDE e.V.), which shall act as a proxy
9  * defined in Section 14 of version 3 of the license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "kgpgfirstassistant.h"
21 
22 #include "gpgproc.h"
23 #include "kgpginterface.h"
24 #include "core/kgpgkey.h"
25 
26 #include <KComboBox>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KStandardDirs>
30 #include <KUrlRequester>
31 #include <QCheckBox>
32 #include <QGridLayout>
33 #include <QHBoxLayout>
34 #include <QLabel>
35 #include <QSpacerItem>
36 #include <QTextStream>
37 #include <QWidget>
38 
39 using namespace KgpgCore;
40 
41 KGpgFirstAssistant::KGpgFirstAssistant(QWidget *parent)
42  : KAssistantDialog(parent)
43 {
44  setCaption(i18n("KGpg Assistant"));
45 
46  QWidget *page = new QWidget(this);
47  QGridLayout *gridLayout = new QGridLayout(page);
48  gridLayout->setSpacing(6);
49  gridLayout->setMargin(11);
50  gridLayout->setContentsMargins(0, 0, 0, 0);
51 
52  QLabel *label = new QLabel(page);
53  QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
54  sizePolicy.setHorizontalStretch(0);
55  sizePolicy.setVerticalStretch(0);
56  sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
57  label->setSizePolicy(sizePolicy);
58  label->setFrameShape(QFrame::NoFrame);
59  label->setFrameShadow(QFrame::Plain);
60  label->setScaledContents(false);
61  label->setAlignment(Qt::AlignTop);
62  label->setWordWrap(false);
63 
64  gridLayout->addWidget(label, 0, 0, 3, 1);
65 
66  label = new QLabel(page);
67  label->setAlignment(Qt::AlignTop);
68  label->setWordWrap(true);
69  label->setText(i18n("This assistant will first setup some basic configuration options required for KGpg to work properly. Next, it will allow you to create your own key pair, enabling you to encrypt your files and emails."));
70 
71  gridLayout->addWidget(label, 0, 1, 1, 1);
72 
73  QSpacerItem *spacerItem = new QSpacerItem(20, 41, QSizePolicy::Minimum, QSizePolicy::Expanding);
74 
75  gridLayout->addItem(spacerItem, 1, 1, 1, 1);
76 
77  page_welcome = addPage(page, i18n("Welcome to the KGpg Assistant"));
78 
79  page = new QWidget(this);
80 
81  gridLayout = new QGridLayout(page);
82  gridLayout->setSpacing(6);
83  gridLayout->setMargin(11);
84  gridLayout->setContentsMargins(0, 0, 0, 0);
85 
86  label = new QLabel(page);
87  label->setAlignment(Qt::AlignTop);
88  label->setWordWrap(true);
89  label->setText(i18n("KGpg needs to know which GnuPG binary to use."));
90 
91  gridLayout->addWidget(label, 0, 1, 1, 1);
92 
93  label = new QLabel(page);
94  label->setAlignment(Qt::AlignTop);
95  label->setWordWrap(true);
96  label->setText(i18n("Unless you want to try some unusual settings, just click on the \"next\" button."));
97 
98  gridLayout->addWidget(label, 1, 1, 1, 1);
99 
100  spacerItem = new QSpacerItem(20, 60, QSizePolicy::Minimum, QSizePolicy::Expanding);
101 
102  gridLayout->addItem(spacerItem, 4, 1, 1, 1);
103 
104  txtGpgVersion = new QLabel(page);
105  txtGpgVersion->setWordWrap(true);
106 
107  gridLayout->addWidget(txtGpgVersion, 3, 1, 1, 1);
108 
109  binURL = new KUrlRequester(page);
110  binURL->setFilter(i18nc("search filter for gpg binary", "gpg|GnuPG binary\n*|All files"));
111  QString gpgBin = KStandardDirs::findExe(QLatin1String("gpg2"));
112  if (gpgBin.isEmpty())
113  gpgBin = KStandardDirs::findExe(QLatin1String("gpg"));
114  if (gpgBin.isEmpty())
115  gpgBin = QLatin1String("gpg");
116  binURL->setUrl(KUrl::fromPath(gpgBin));
117 
118  connect(binURL, SIGNAL(textChanged(QString)), SLOT(slotBinaryChanged(QString)));
119  slotBinaryChanged(gpgBin);
120 
121  gridLayout->addWidget(binURL, 2, 1, 1, 1);
122 
123  page_binary = addPage(page, i18n("GnuPG Binary"));
124 
125  page = new QWidget(this);
126 
127  gridLayout = new QGridLayout(page);
128  gridLayout->setSpacing(6);
129  gridLayout->setMargin(11);
130  gridLayout->setContentsMargins(0, 0, 0, 0);
131  text_optionsfound = new QLabel(page);
132  text_optionsfound->setAlignment(Qt::AlignTop);
133  text_optionsfound->setWordWrap(true);
134  text_optionsfound->setText(i18n("Unless you want to try some unusual settings, just click on the \"next\" button."));
135 
136  gridLayout->addWidget(text_optionsfound, 1, 1, 1, 1);
137 
138  label = new QLabel(page);
139  label->setAlignment(Qt::AlignTop);
140  label->setWordWrap(true);
141  label->setText(i18n("KGpg needs to know where your GnuPG configuration file is stored."));
142 
143  gridLayout->addWidget(label, 0, 1, 1, 1);
144 
145  spacerItem = new QSpacerItem(20, 60, QSizePolicy::Minimum, QSizePolicy::Expanding);
146 
147  gridLayout->addItem(spacerItem, 4, 1, 1, 1);
148 
149  pathURL = new KUrlRequester(page);
150 
151  gridLayout->addWidget(pathURL, 3, 1, 1, 1);
152 
153  label = new QLabel(page);
154  label->setAlignment(Qt::AlignVCenter);
155  label->setWordWrap(true);
156  label->setText(i18n("Path to your GnuPG configuration file:"));
157 
158  gridLayout->addWidget(label, 2, 1, 1, 1);
159 
160  label = new QLabel(page);
161  sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
162  label->setSizePolicy(sizePolicy);
163  label->setFrameShape(QFrame::NoFrame);
164  label->setFrameShadow(QFrame::Plain);
165  label->setScaledContents(false);
166  label->setAlignment(Qt::AlignTop);
167  label->setWordWrap(false);
168 
169  gridLayout->addWidget(label, 0, 0, 5, 1);
170  page_config = addPage(page, i18n("Configuration File"));
171 
172  page = new QWidget(this);
173  gridLayout = new QGridLayout(page);
174  gridLayout->setSpacing(6);
175  gridLayout->setMargin(11);
176  gridLayout->setContentsMargins(0, 0, 0, 0);
177 
178  QHBoxLayout *hboxLayout = new QHBoxLayout();
179  hboxLayout->setSpacing(6);
180  label = new QLabel(page);
181  label->setText(i18n("Your default key:"));
182 
183  hboxLayout->addWidget(label);
184 
185  CBdefault = new KComboBox(page);
186  QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Fixed);
187  sizePolicy1.setHorizontalStretch(0);
188  sizePolicy1.setVerticalStretch(0);
189  sizePolicy1.setHeightForWidth(CBdefault->sizePolicy().hasHeightForWidth());
190  CBdefault->setSizePolicy(sizePolicy1);
191 
192  hboxLayout->addWidget(CBdefault);
193 
194  gridLayout->addLayout(hboxLayout, 0, 1, 1, 1);
195 
196  spacerItem = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);
197 
198  gridLayout->addItem(spacerItem, 1, 1, 1, 1);
199 
200  page_defaultkey = addPage(page, i18n("Default Key"));
201 
202  page = new QWidget(this);
203 
204  gridLayout = new QGridLayout(page);
205  gridLayout->setSpacing(6);
206  gridLayout->setMargin(11);
207  gridLayout->setContentsMargins(0, 0, 0, 0);
208 
209  binlabel = new QLabel(page);
210 
211  gridLayout->addWidget(binlabel, 0, 1, 1, 1);
212 
213  versionLabel = new QLabel(page);
214 
215  gridLayout->addWidget(versionLabel, 1, 1, 1, 1);
216 
217  defaultkeylabel = new QLabel(page);
218 
219  gridLayout->addWidget(defaultkeylabel, 2, 1, 1, 1);
220 
221  generateCB = new QCheckBox(page);
222  generateCB->setText(i18n("Generate new key"));
223 
224  gridLayout->addWidget(generateCB, 3, 1, 1, 1);
225 
226  spacerItem = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);
227 
228  gridLayout->addItem(spacerItem, 4, 1, 1, 1);
229 
230  autostartCB = new QCheckBox(page);
231  autostartCB->setChecked(true);
232  autostartCB->setText(i18n("Start KGpg automatically at KDE startup."));
233 
234  gridLayout->addWidget(autostartCB, 5, 1, 1, 1);
235 
236  page_done = addPage(page, i18n("Done"));
237 }
238 
239 void
240 KGpgFirstAssistant::findConfigPath()
241 {
242  const QString gpgHome = GPGProc::getGpgHome(binURL->url().path());
243  QString confPath = gpgHome + QLatin1String( "gpg.conf" );
244 
245  if (!QFile(confPath).exists()) {
246  confPath = gpgHome + QLatin1String( "options" );
247  if (!QFile(confPath).exists()) {
248  if (KMessageBox::questionYesNo(0, i18n("<qt><b>The GnuPG configuration file was not found</b>. Should KGpg try to create a config file ?</qt>"), QString(), KGuiItem(i18n("Create Config")), KGuiItem(i18n("Do Not Create"))) == KMessageBox::Yes) {
249  confPath = gpgHome + QLatin1String( "gpg.conf" );
250  QFile file(confPath);
251  if (file.open(QIODevice::WriteOnly)) {
252  QTextStream stream(&file);
253  stream << "# GnuPG config file created by KGpg" << "\n";
254  file.close();
255  }
256  } else {
257  text_optionsfound->setText(i18n("<qt><b>The GnuPG configuration file was not found</b>.</qt>"));
258  confPath.clear();
259  }
260  }
261  }
262 
263  pathURL->setUrl(confPath);
264 
265  QStringList secids = KgpgInterface::readSecretKeys();
266  if (secids.isEmpty()) {
267  setAppropriate(page_defaultkey, false);
268  generateCB->setChecked(true);
269  defaultkeylabel->setVisible(false);
270  return;
271  }
272 
273  KgpgKeyList publiclist = KgpgInterface::readPublicKeys(secids);
274 
275  generateCB->setChecked(false);
276  setAppropriate(page_defaultkey, true);
277 
278  CBdefault->clear();
279 
280  foreach (const KgpgKey &k, publiclist) {
281  QString s;
282 
283  if (k.email().isEmpty())
284  s = i18nc("Name: ID", "%1: %2", k.name(), k.id());
285  else
286  s = i18nc("Name (Email): ID", "%1 (%2): %3", k.name(), k.email(), k.id());
287 
288  CBdefault->addItem(s, k.fingerprint());
289  }
290 
291  CBdefault->setCurrentIndex(0);
292 }
293 
294 void
295 KGpgFirstAssistant::next()
296 {
297  if (currentPage() == page_binary) {
298  binlabel->setText(i18n("Your GnuPG binary is: %1", binURL->url().path()));
299  findConfigPath();
300  } else if (currentPage() == page_config) {
301  QString tst, name;
302  m_confPath = pathURL->url().path();
303 
304  QString defaultID = KgpgInterface::getGpgSetting(QLatin1String( "default-key" ), m_confPath);
305 
306  if (!defaultID.isEmpty()) {
307  for (int i = 0; i < CBdefault->count(); i++) {
308  if (defaultID == CBdefault->itemData(i).toString().right(defaultID.length())) {
309  CBdefault->setCurrentIndex(i);
310  break;
311  }
312  }
313  }
314  versionLabel->setText(i18n("You have GnuPG version: %1", m_gpgVersion));
315  } else if (currentPage() == page_defaultkey) {
316  defaultkeylabel->setVisible(true);
317  defaultkeylabel->setText(i18n("Your default key is: %1", CBdefault->currentText()));
318  int i = CBdefault->currentIndex();
319  if (i >= 0) {
320  defaultkeylabel->setToolTip(CBdefault->itemData(i).toString());
321  }
322  }
323  KAssistantDialog::next();
324 }
325 
326 bool
327 KGpgFirstAssistant::runKeyGenerate() const
328 {
329  return generateCB->isChecked();
330 }
331 
332 QString
333 KGpgFirstAssistant::getConfigPath() const
334 {
335  return m_confPath;
336 }
337 
338 QString
339 KGpgFirstAssistant::getDefaultKey() const
340 {
341  int i = CBdefault->currentIndex();
342  if (i < 0)
343  return QString();
344  else
345  return CBdefault->itemData(i).toString();
346 }
347 
348 bool
349 KGpgFirstAssistant::getAutoStart() const
350 {
351  return autostartCB->isChecked();
352 }
353 
354 void
355 KGpgFirstAssistant::slotBinaryChanged(const QString &binary)
356 {
357  if (binary.isEmpty()) {
358  setValid(page_binary, false);
359  return;
360  }
361 
362  m_gpgVersion = GPGProc::gpgVersionString(binary);
363  setValid(page_binary, !m_gpgVersion.isEmpty());
364  if (!m_gpgVersion.isEmpty()) {
365  const int gpgver = GPGProc::gpgVersion(m_gpgVersion);
366 
367  if (gpgver < 0x10400) {
368  txtGpgVersion->setText(i18n("Your GnuPG version (%1) seems to be too old.<br />Compatibility with versions before 1.4.0 is no longer guaranteed.", m_gpgVersion));
369  } else {
370  txtGpgVersion->setText(i18n("You have GnuPG version: %1", m_gpgVersion));
371  }
372  }
373 }
374 
375 #include "kgpgfirstassistant.moc"
QSizePolicy::setVerticalStretch
void setVerticalStretch(uchar stretchFactor)
QSpacerItem
QWidget
KGpgFirstAssistant::getAutoStart
bool getAutoStart() const
check if KGpg autostart should be activated
Definition: kgpgfirstassistant.cpp:349
QLayout::setContentsMargins
void setContentsMargins(int left, int top, int right, int bottom)
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
GPGProc::getGpgHome
static QString getGpgHome(const QString &binary)
find users GnuPG directory
Definition: gpgproc.cpp:336
QFrame::setFrameShape
void setFrameShape(Shape)
KGpgFirstAssistant::getDefaultKey
QString getDefaultKey() const
get fingerprint of default key
Definition: kgpgfirstassistant.cpp:339
QSizePolicy
QWidget::setVisible
virtual void setVisible(bool visible)
QHBoxLayout
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
QGridLayout
kgpgkey.h
KgpgCore::KgpgKey
Definition: kgpgkey.h:236
KgpgCore::KgpgKey::id
QString id() const
Definition: kgpgkey.cpp:233
QFile
QTextStream
QGridLayout::setSpacing
void setSpacing(int spacing)
KgpgCore::KgpgKey::fingerprint
const QString & fingerprint() const
Definition: kgpgkey.cpp:258
KgpgCore::KgpgKey::name
QString name() const
Definition: kgpgkey.cpp:243
KAssistantDialog
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
kgpgfirstassistant.h
KgpgCore::KgpgKey::email
QString email() const
Definition: kgpgkey.cpp:248
KgpgInterface::readPublicKeys
KgpgCore::KgpgKeyList readPublicKeys(const QStringList &ids=QStringList())
Definition: kgpginterface.cpp:288
QCheckBox
QList::isEmpty
bool isEmpty() const
GPGProc::gpgVersion
static int gpgVersion(const QString &vstr)
parse GnuPG version string and return version as number
Definition: gpgproc.cpp:288
QString::isEmpty
bool isEmpty() const
gpgproc.h
QLabel::setScaledContents
void setScaledContents(bool)
kgpginterface.h
QLabel::setText
void setText(const QString &)
QString
QLayout::setMargin
void setMargin(int margin)
QWidget::sizePolicy
sizePolicy
QStringList
KGpgFirstAssistant::getConfigPath
QString getConfigPath() const
get user selected GnuPG home directory
Definition: kgpgfirstassistant.cpp:333
KgpgCore::KgpgKeyList
Definition: kgpgkey.h:293
QFrame::setFrameShadow
void setFrameShadow(Shadow)
QAbstractButton::setChecked
void setChecked(bool)
KGpgFirstAssistant::runKeyGenerate
bool runKeyGenerate() const
check if key generation dialog should be started
Definition: kgpgfirstassistant.cpp:327
QGridLayout::addLayout
void addLayout(QLayout *layout, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
KgpgInterface::readSecretKeys
KgpgCore::KgpgKeyList readSecretKeys(const QStringList &ids=QStringList())
Definition: kgpginterface.cpp:402
KGpgFirstAssistant::next
virtual void next()
called when "next" button is pressed
Definition: kgpgfirstassistant.cpp:295
QLatin1String
GPGProc::gpgVersionString
static QString gpgVersionString(const QString &binary)
get the GnuPG version string of the given binary
Definition: gpgproc.cpp:300
QGridLayout::addItem
void addItem(QLayoutItem *item, int row, int column, int rowSpan, int columnSpan, QFlags< Qt::AlignmentFlag > alignment)
QString::length
int length() const
QAbstractButton::setText
void setText(const QString &text)
QSizePolicy::setHorizontalStretch
void setHorizontalStretch(uchar stretchFactor)
QWidget::setToolTip
void setToolTip(const QString &)
QLabel
QLabel::setWordWrap
void setWordWrap(bool on)
KGpgFirstAssistant::KGpgFirstAssistant
KGpgFirstAssistant(QWidget *parent=0)
constructor of KGpgFirstAssistant
Definition: kgpgfirstassistant.cpp:41
QBoxLayout::setSpacing
void setSpacing(int spacing)
QSizePolicy::setHeightForWidth
void setHeightForWidth(bool dependent)
KgpgInterface::getGpgSetting
QString getGpgSetting(const QString &name, const QString &configfile)
Definition: kgpginterface.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal