• 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
kgpgdecrypt.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010,2011,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 "kgpgdecrypt.h"
15 
16 #include "gpgproc.h"
17 #include "kgpgsettings.h"
18 
19 #include <KLocale>
20 
21 KGpgDecrypt::KGpgDecrypt(QObject *parent, const QString &text)
22  : KGpgTextOrFileTransaction(parent, text),
23  m_fileIndex(-1),
24  m_plainLength(-1)
25 {
26 }
27 
28 KGpgDecrypt::KGpgDecrypt(QObject *parent, const KUrl::List &files)
29  : KGpgTextOrFileTransaction(parent, files),
30  m_fileIndex(0),
31  m_plainLength(-1)
32 {
33 }
34 
35 KGpgDecrypt::KGpgDecrypt(QObject* parent, const KUrl& infile, const KUrl& outfile)
36  : KGpgTextOrFileTransaction(parent, KUrl::List(infile)),
37  m_fileIndex(0),
38  m_plainLength(-1),
39  m_outFilename(outfile.toLocalFile())
40 {
41 }
42 
43 KGpgDecrypt::~KGpgDecrypt()
44 {
45 }
46 
47 QStringList
48 KGpgDecrypt::command() const
49 {
50  QStringList ret;
51 
52  ret << QLatin1String("--decrypt") << QLatin1String("--command-fd=0");
53 
54  if (!m_outFilename.isEmpty())
55  ret << QLatin1String("-o") << m_outFilename;
56 
57  ret << KGpgSettings::customDecrypt().simplified().split(QLatin1Char(' '), QString::SkipEmptyParts);
58 
59  return ret;
60 }
61 
62 QStringList
63 KGpgDecrypt::decryptedText() const
64 {
65  QStringList result;
66  int txtlength = 0;
67 
68  foreach (const QString &line, getMessages())
69  if (!line.startsWith(QLatin1String("[GNUPG:] "))) {
70  result.append(line);
71  txtlength += line.length() + 1;
72  }
73 
74  if (result.isEmpty())
75  return result;
76 
77  QString last = result.last();
78  // this may happen when the original text did not end with a newline
79  if (last.endsWith(QLatin1String("[GNUPG:] DECRYPTION_OKAY"))) {
80  // if GnuPG doesn't tell us the length assume that this happend
81  // if it told us the length then check if it _really_ happend
82  if (((m_plainLength != -1) && (txtlength != m_plainLength)) ||
83  (m_plainLength == -1)) {
84  last.chop(24);
85  result[result.count() - 1] = last;
86  }
87  }
88 
89  return result;
90 }
91 
92 bool
93 KGpgDecrypt::isEncryptedText(const QString &text, int *startPos, int *endPos)
94 {
95  int posStart = text.indexOf(QLatin1String("-----BEGIN PGP MESSAGE-----"));
96  if (posStart == -1)
97  return false;
98 
99  int posEnd = text.indexOf(QLatin1String("-----END PGP MESSAGE-----"), posStart);
100  if (posEnd == -1)
101  return false;
102 
103  if (startPos != NULL)
104  *startPos = posStart;
105  if (endPos != NULL)
106  *endPos = posEnd;
107 
108  return true;
109 }
110 
111 bool
112 KGpgDecrypt::nextLine(const QString& line)
113 {
114  const KUrl::List &inputFiles = getInputFiles();
115 
116  if (!inputFiles.isEmpty()) {
117  if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
118  emit statusMessage(i18nc("Status message 'Decrypting <filename>' (operation starts)", "Decrypting %1", inputFiles.at(m_fileIndex).fileName()));
119  emit infoProgress(2 * m_fileIndex + 1, inputFiles.count() * 2);
120  } else if (line == QLatin1String("[GNUPG:] END_DECRYPTION")) {
121  emit statusMessage(i18nc("Status message 'Decrypted <filename>' (operation was completed)", "Decrypted %1", inputFiles.at(m_fileIndex).fileName()));
122  m_fileIndex++;
123  emit infoProgress(2 * m_fileIndex, inputFiles.count() * 2);
124  }
125  } else {
126  if (line.startsWith(QLatin1String("[GNUPG:] PLAINTEXT_LENGTH "))) {
127  bool ok;
128  m_plainLength = line.mid(26).toInt(&ok);
129  if (!ok)
130  m_plainLength = -1;
131  } else if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
132  // close the command channel (if any) to signal GnuPG that it
133  // can start sending the output.
134  getProcess()->closeWriteChannel();
135  }
136  }
137 
138  return KGpgTextOrFileTransaction::nextLine(line);
139 }
140 
141 #include "kgpgdecrypt.moc"
KGpgTextOrFileTransaction
feed a text or file through gpg
Definition: kgpgtextorfiletransaction.h:29
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
KGpgDecrypt::nextLine
virtual bool nextLine(const QString &line)
Called for every line the gpg process writes.
Definition: kgpgdecrypt.cpp:112
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KGpgTransaction::infoProgress
void infoProgress(qulonglong processedAmount, qulonglong totalAmount)
emits procentual status information
QString::simplified
QString simplified() const
KGpgDecrypt::~KGpgDecrypt
virtual ~KGpgDecrypt()
destructor
Definition: kgpgdecrypt.cpp:43
KGpgSettings::customDecrypt
static QString customDecrypt()
Get Custom decryption command.
Definition: kgpgsettings.h:79
QString::chop
void chop(int n)
KGpgTransaction::getProcess
GPGProc * getProcess()
get a reference to the gpg process object
Definition: kgpgtransaction.cpp:556
KGpgDecrypt::isEncryptedText
static bool isEncryptedText(const QString &text, int *startPos=NULL, int *endPos=NULL)
check if the given text contains an encoded message
Definition: kgpgdecrypt.cpp:93
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KGpgTransaction::statusMessage
void statusMessage(const QString &msg)
emits textual status information
QObject
QString::toInt
int toInt(bool *ok, int base) const
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
gpgproc.h
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
kgpgdecrypt.h
QString
KGpgTextOrFileTransaction::getInputFiles
const KUrl::List & getInputFiles() const
Definition: kgpgtextorfiletransaction.cpp:176
QStringList
QLatin1Char
KGpgTextOrFileTransaction::nextLine
virtual bool nextLine(const QString &line)
Called for every line the gpg process writes.
Definition: kgpgtextorfiletransaction.cpp:143
kgpgsettings.h
QString::mid
QString mid(int position, int n) const
QLatin1String
QList::last
T & last()
QString::length
int length() const
KGpgDecrypt::command
virtual QStringList command() const
Definition: kgpgdecrypt.cpp:48
KGpgDecrypt::decryptedText
QStringList decryptedText() const
get decryption result
Definition: kgpgdecrypt.cpp:63
KGpgTextOrFileTransaction::getMessages
const QStringList & getMessages() const
get gpg info message
Definition: kgpgtextorfiletransaction.cpp:159
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