aboutsummaryrefslogtreecommitdiffstats
path: root/kgpg/transactions/kgpgdecrypt.cpp
blob: 20f0c501df6739d78d97a9acb4920ffe8d5ded7c (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
/*
 * Copyright (C) 2010,2011,2012 Rolf Eike Beer <[email protected]>
 */

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kgpgdecrypt.h"

#include "../gpgproc.h"
//#include "kgpgsettings.h"

//#include <KLocale>

KGpgDecrypt::KGpgDecrypt(QObject *parent, const QString &text)
	: KGpgTextOrFileTransaction(parent, text),
	m_fileIndex(-1),
	m_plainLength(-1)
{
}

KGpgDecrypt::KGpgDecrypt(QObject *parent, const QList<QUrl> &files)
	: KGpgTextOrFileTransaction(parent, files),
	m_fileIndex(0),
	m_plainLength(-1)
{
}

KGpgDecrypt::KGpgDecrypt(QObject* parent, const QUrl& infile, const QUrl& outfile)
    : KGpgTextOrFileTransaction(parent, QList<QUrl>() << infile ),
	m_fileIndex(0),
	m_plainLength(-1),
	m_outFilename(outfile.toLocalFile())
{
}

KGpgDecrypt::~KGpgDecrypt()
{
}

QStringList
KGpgDecrypt::command() const
{
	QStringList ret;

	ret << QLatin1String("--decrypt") << QLatin1String("--command-fd=0");

	if (!m_outFilename.isEmpty())
		ret << QLatin1String("-o") << m_outFilename;

    //ret << KGpgSettings::customDecrypt().simplified().split(QLatin1Char(' '), QString::SkipEmptyParts);

	return ret;
}

QStringList
KGpgDecrypt::decryptedText() const
{
	QStringList result;
	int txtlength = 0;

	foreach (const QString &line, getMessages())
		if (!line.startsWith(QLatin1String("[GNUPG:] "))) {
			result.append(line);
			txtlength += line.length() + 1;
		}

	if (result.isEmpty())
		return result;

	QString last = result.last();
	// this may happen when the original text did not end with a newline
	if (last.endsWith(QLatin1String("[GNUPG:] DECRYPTION_OKAY"))) {
		// if GnuPG doesn't tell us the length assume that this happend
		// if it told us the length then check if it _really_ happend
		if (((m_plainLength != -1) && (txtlength != m_plainLength)) ||
				(m_plainLength == -1)) {
			last.chop(24);
			result[result.count() - 1] = last;
		}
	}

	return result;
}

bool
KGpgDecrypt::isEncryptedText(const QString &text, int *startPos, int *endPos)
{
	int posStart = text.indexOf(QLatin1String("-----BEGIN PGP MESSAGE-----"));
	if (posStart == -1)
		return false;

	int posEnd = text.indexOf(QLatin1String("-----END PGP MESSAGE-----"), posStart);
	if (posEnd == -1)
		return false;

	if (startPos != NULL)
		*startPos = posStart;
	if (endPos != NULL)
		*endPos = posEnd;

	return true;
}

bool
KGpgDecrypt::nextLine(const QString& line)
{
    const QList<QUrl> &inputFiles = getInputFiles();

	if (!inputFiles.isEmpty()) {
		if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
            emit statusMessage(tr("Status message 'Decrypting <filename>' (operation starts)", "Decrypting %1").arg(inputFiles.at(m_fileIndex).toLocalFile()));
			emit infoProgress(2 * m_fileIndex + 1, inputFiles.count() * 2);
		} else if (line == QLatin1String("[GNUPG:] END_DECRYPTION")) {
            emit statusMessage(tr("Status message 'Decrypted <filename>' (operation was completed)", "Decrypted %1").arg(inputFiles.at(m_fileIndex).toLocalFile()));
			m_fileIndex++;
			emit infoProgress(2 * m_fileIndex, inputFiles.count() * 2);
		}
	} else {
		if (line.startsWith(QLatin1String("[GNUPG:] PLAINTEXT_LENGTH "))) {
			bool ok;
			m_plainLength = line.mid(26).toInt(&ok);
			if (!ok)
				m_plainLength = -1;
		} else if (line == QLatin1String("[GNUPG:] BEGIN_DECRYPTION")) {
			// close the command channel (if any) to signal GnuPG that it
			// can start sending the output.
			getProcess()->closeWriteChannel();
		}
	}

	return KGpgTextOrFileTransaction::nextLine(line);
}

//#include "kgpgdecrypt.moc"