• 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
  • transactions
kgpgimport.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008,2009,2010,2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
3  */
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "kgpgimport.h"
15 
16 #include "model/kgpgitemmodel.h"
17 #include "core/KGpgKeyNode.h"
18 
19 #include <KDebug>
20 #include <KLocale>
21 
22 KGpgImport::KGpgImport(QObject *parent, const QString &text)
23  : KGpgTextOrFileTransaction(parent, text, true)
24 {
25 }
26 
27 KGpgImport::KGpgImport(QObject *parent, const KUrl::List &files)
28  : KGpgTextOrFileTransaction(parent, files, true)
29 {
30 }
31 
32 KGpgImport::~KGpgImport()
33 {
34 }
35 
36 QStringList
37 KGpgImport::command() const
38 {
39  QStringList ret;
40 
41  ret << QLatin1String( "--import" ) << QLatin1String( "--allow-secret-key-import" );
42 
43  return ret;
44 }
45 
46 QStringList
47 KGpgImport::getImportedKeys() const
48 {
49  QStringList res;
50 
51  foreach (const QString &str, getMessages())
52  if (str.startsWith(QLatin1String("[GNUPG:] IMPORTED ")))
53  res << str.mid(18);
54 
55  return res;
56 }
57 
58 QStringList
59 KGpgImport::getImportedIds(const QStringList &log, const int reason)
60 {
61  QStringList res;
62 
63  foreach (const QString &str, log) {
64  if (!str.startsWith(QLatin1String("[GNUPG:] IMPORT_OK ")))
65  continue;
66 
67  QString tmpstr(str.mid(19).simplified());
68 
69  int space = tmpstr.indexOf(QLatin1Char( ' ' ));
70  if (space <= 0) {
71  kDebug(2100) << __LINE__ << "invalid format:" << str;
72  continue;
73  }
74 
75  bool ok;
76  unsigned char code = tmpstr.left(space).toUInt(&ok);
77  if (!ok) {
78  kDebug(2100) << __LINE__ << "invalid format:" << str << space << tmpstr.left(space - 1);
79  continue;
80  }
81 
82  if ((reason == -1) || ((reason == 0) && (code == 0)) || ((reason & code) != 0))
83  res << tmpstr.mid(space + 1);
84  }
85 
86  return res;
87 }
88 
89 QStringList
90 KGpgImport::getImportedIds(const int reason) const
91 {
92  return getImportedIds(getMessages(), reason);
93 }
94 
95 QString
96 KGpgImport::getImportMessage() const
97 {
98  return getImportMessage(getMessages());
99 }
100 
101 QString
102 KGpgImport::getImportMessage(const QStringList &log)
103 {
104 #define RESULT_PARTS 14
105  unsigned long rcode[RESULT_PARTS];
106  unsigned int i = 0;
107  int line = 0;
108  bool fine;
109 
110  memset(rcode, 0, sizeof(rcode));
111 
112  foreach (const QString &str, log) {
113  line++;
114  if (!str.startsWith(QLatin1String("[GNUPG:] IMPORT_RES ")))
115  continue;
116 
117  const QStringList rstr(str.mid(20).simplified().split(QLatin1Char( ' ' )));
118 
119  fine = (rstr.count() == RESULT_PARTS);
120 
121  i = 0;
122  while (fine && (i < RESULT_PARTS)) {
123  rcode[i] += rstr.at(i).toULong(&fine);
124  i++;
125  }
126 
127  if (!fine)
128  return i18n("The import result string has an unsupported format in line %1.<br />Please see the detailed log for more information.", line);
129  }
130 
131  fine = false;
132  i = 0;
133  while (!fine && (i < RESULT_PARTS)) {
134  fine = (rcode[i] != 0);
135  i++;
136  }
137 
138  if (!fine)
139  return i18n("No key imported.<br />Please see the detailed log for more information.");
140 
141  QString resultMessage(i18np("<qt>%1 key processed.</qt>", "<qt>%1 keys processed.</qt>", rcode[0]));
142 
143  if (rcode[1])
144  resultMessage += i18np("<qt><br />One key without ID.</qt>", "<qt><br />%1 keys without ID.</qt>", rcode[1]);
145  if (rcode[2])
146  resultMessage += i18np("<qt><br /><b>One key imported:</b></qt>", "<qt><br /><b>%1 keys imported:</b></qt>", rcode[2]);
147  if (rcode[3])
148  resultMessage += i18np("<qt><br />One RSA key imported.</qt>", "<qt><br />%1 RSA keys imported.</qt>", rcode[3]);
149  if (rcode[4])
150  resultMessage += i18np("<qt><br />One key unchanged.</qt>", "<qt><br />%1 keys unchanged.</qt>", rcode[4]);
151  if (rcode[5])
152  resultMessage += i18np("<qt><br />One user ID imported.</qt>", "<qt><br />%1 user IDs imported.</qt>", rcode[5]);
153  if (rcode[6])
154  resultMessage += i18np("<qt><br />One subkey imported.</qt>", "<qt><br />%1 subkeys imported.</qt>", rcode[6]);
155  if (rcode[7])
156  resultMessage += i18np("<qt><br />One signature imported.</qt>", "<qt><br />%1 signatures imported.</qt>", rcode[7]);
157  if (rcode[8])
158  resultMessage += i18np("<qt><br />One revocation certificate imported.</qt>", "<qt><br />%1 revocation certificates imported.</qt>", rcode[8]);
159  if (rcode[9])
160  resultMessage += i18np("<qt><br />One secret key processed.</qt>", "<qt><br />%1 secret keys processed.</qt>", rcode[9]);
161  if (rcode[10])
162  resultMessage += i18np("<qt><br /><b>One secret key imported.</b></qt>", "<qt><br /><b>%1 secret keys imported.</b></qt>", rcode[10]);
163  if (rcode[11])
164  resultMessage += i18np("<qt><br />One secret key unchanged.</qt>", "<qt><br />%1 secret keys unchanged.</qt>", rcode[11]);
165  if (rcode[12])
166  resultMessage += i18np("<qt><br />One secret key not imported.</qt>", "<qt><br />%1 secret keys not imported.</qt>", rcode[12]);
167 
168  if (rcode[9])
169  resultMessage += i18n("<qt><br /><b>You have imported a secret key.</b> <br />"
170  "Please note that imported secret keys are not trusted by default.<br />"
171  "To fully use this secret key for signing and encryption, you must edit the key (double click on it) and set its trust to Full or Ultimate.</qt>");
172 
173  return resultMessage;
174 }
175 
176 static QString
177 beautifyKeyList(const QStringList &keyIds, const KGpgItemModel *model)
178 {
179  QString result;
180 
181  result.append(QLatin1String("\n"));
182  if (model == NULL) {
183  result.append(QLatin1String(" ") + keyIds.join(QLatin1String("\n ")));
184  } else {
185  foreach (const QString &changed, keyIds) {
186  const KGpgKeyNode *node = model->findKeyNode(changed);
187  QString line;
188 
189  if (node == NULL) {
190  line = changed;
191  } else {
192  if (node->getEmail().isEmpty())
193  line = i18nc("ID: Name", "%1: %2", node->getFingerprint(), node->getName());
194  else
195  line = i18nc("ID: Name <Email>", "%1: %2 &lt;%3&gt;", node->getFingerprint(), node->getName(), node->getEmail());
196  }
197 
198  result.append(QLatin1String(" ") + line + QLatin1String("\n"));
199  }
200  }
201 
202  return result;
203 }
204 
205 QString
206 KGpgImport::getDetailedImportMessage(const QStringList &log, const KGpgItemModel *model)
207 {
208  QString result;
209  QMap<QString, unsigned int> resultcodes;
210 
211  foreach (const QString &keyresult, log) {
212  if (!keyresult.startsWith(QLatin1String("[GNUPG:] IMPORT_OK ")))
213  continue;
214 
215  QStringList rc(keyresult.mid(19).split(QLatin1Char( ' ' )));
216  if (rc.count() < 2) {
217  kDebug(2100) << "unexpected syntax:" << keyresult;
218  continue;
219  }
220 
221  resultcodes[rc.at(1)] = rc.at(0).toUInt();
222  }
223 
224  QMap<QString, unsigned int>::const_iterator iterend = resultcodes.constEnd();
225 
226  for (unsigned int flag = 1; flag <= 16; flag <<= 1) {
227  QStringList thischanged;
228 
229  for (QMap<QString, unsigned int>::const_iterator iter = resultcodes.constBegin(); iter != iterend; ++iter) {
230  if (iter.value() & flag)
231  thischanged << iter.key();
232  }
233 
234  if (thischanged.isEmpty())
235  continue;
236 
237  switch (flag) {
238  case 1:
239  result.append(i18np("New Key", "New Keys", thischanged.count()));
240  break;
241  case 2:
242  result.append(i18np("Key with new User Id", "Keys with new User Ids", thischanged.count()));
243  break;
244  case 4:
245  result.append(i18np("Key with new Signatures", "Keys with new Signatures", thischanged.count()));
246  break;
247  case 8:
248  result.append(i18np("Key with new Subkeys", "Keys with new Subkeys", thischanged.count()));
249  break;
250  case 16:
251  result.append(i18np("New Private Key", "New Private Keys", thischanged.count()));
252  break;
253  default:
254  Q_ASSERT(flag == 1);
255  }
256 
257  result.append(beautifyKeyList(thischanged, model));
258  result.append(QLatin1String("\n\n"));
259  }
260 
261  QStringList unchanged(resultcodes.keys(0));
262 
263  if (unchanged.isEmpty()) {
264  // remove empty line at end
265  result.chop(1);
266  } else {
267  result.append(i18np("Unchanged Key", "Unchanged Keys", unchanged.count()));
268  result.append(beautifyKeyList(unchanged, model));
269  result.append(QLatin1String("\n"));
270  }
271 
272  return result;
273 }
274 
275 int
276 KGpgImport::isKey(const QString &text, const bool incomplete)
277 {
278  int markpos = text.indexOf(QLatin1String("-----BEGIN PGP PUBLIC KEY BLOCK-----"));
279  if (markpos >= 0) {
280  markpos = text.indexOf(QLatin1String("-----END PGP PUBLIC KEY BLOCK-----"), markpos);
281  return ((markpos > 0) || incomplete) ? 1 : 0;
282  }
283 
284  markpos = text.indexOf(QLatin1String("-----BEGIN PGP PRIVATE KEY BLOCK-----"));
285  if (markpos < 0)
286  return 0;
287 
288  markpos = text.indexOf(QLatin1String("-----END PGP PRIVATE KEY BLOCK-----"), markpos);
289  if ((markpos < 0) && !incomplete)
290  return 0;
291 
292  return 2;
293 }
294 
295 #include "kgpgimport.moc"
KGpgTextOrFileTransaction
feed a text or file through gpg
Definition: kgpgtextorfiletransaction.h:29
KGpgItemModel
Definition: kgpgitemmodel.h:44
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
kgpgimport.h
QString::append
QString & append(QChar ch)
KGpgKeyNode::getName
virtual QString getName() const
Definition: KGpgKeyNode.cpp:89
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QMap::constBegin
const_iterator constBegin() const
QList::at
const T & at(int i) const
QMap
QString::simplified
QString simplified() const
KGpgImport::getImportMessage
QString getImportMessage() const
get textual summary of the import events
Definition: kgpgimport.cpp:96
QStringList::join
QString join(const QString &separator) const
QString::chop
void chop(int n)
beautifyKeyList
static QString beautifyKeyList(const QStringList &keyIds, const KGpgItemModel *model)
Definition: kgpgimport.cpp:177
KGpgKeyNode
A public key with or without corresponding secret key.
Definition: KGpgKeyNode.h:33
KGpgImport::getDetailedImportMessage
static QString getDetailedImportMessage(const QStringList &log, const KGpgItemModel *model=NULL)
get detailed summary of import
Definition: kgpgimport.cpp:206
QMap::keys
QList< Key > keys() const
QList::count
int count(const T &value) const
KGpgItemModel::findKeyNode
KGpgKeyNode * findKeyNode(const QString &id) const
Definition: kgpgitemmodel.cpp:238
KGpgKeyNode::getFingerprint
const QString & getFingerprint() const
Definition: KGpgKeyNode.cpp:75
KGpgImport::getImportedKeys
QStringList getImportedKeys() const
get the names and short fingerprints of the imported keys
Definition: kgpgimport.cpp:47
QObject
kgpgitemmodel.h
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QMap::const_iterator
KGpgImport::command
virtual QStringList command() const
Definition: kgpgimport.cpp:37
QString
QStringList
QLatin1Char
RESULT_PARTS
#define RESULT_PARTS
QString::mid
QString mid(int position, int n) const
QLatin1String
KGpgKeyNode::getEmail
virtual QString getEmail() const
Definition: KGpgKeyNode.cpp:95
KGpgKeyNode.h
QList::mid
QList< T > mid(int pos, int length) const
QString::left
QString left(int n) const
KGpgImport::getImportedIds
static QStringList getImportedIds(const QStringList &log, const int reason=-1)
get the full fingerprints of the imported keys
Definition: kgpgimport.cpp:59
KGpgImport::KGpgImport
KGpgImport(QObject *parent, const QString &text=QString())
import given text
Definition: kgpgimport.cpp:22
KGpgImport::~KGpgImport
virtual ~KGpgImport()
destructor
Definition: kgpgimport.cpp:32
KGpgImport::isKey
static int isKey(const QString &text, const bool incomplete=false)
check if the given text contains a private or public key
Definition: kgpgimport.cpp:276
KGpgTextOrFileTransaction::getMessages
const QStringList & getMessages() const
get gpg info message
Definition: kgpgtextorfiletransaction.cpp:159
QString::toUInt
uint toUInt(bool *ok, int base) const
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