aboutsummaryrefslogtreecommitdiffstats
path: root/kgpg/transactions/kgpgsigntext.cpp
blob: c97121442b0a19d500353f90a8c745ecf2864e8f (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
/*
 * Copyright (C) 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 "kgpgsigntext.h"
#include <QDebug>
#include "../gpgproc.h"

//#include "kgpgsettings.h"

KGpgSignText::KGpgSignText(QObject *parent, const QString &signId, const QString &text, const SignOptions &options, const QStringList &extraOptions)
	: KGpgTextOrFileTransaction(parent, text),
	m_fileIndex(-1),
	m_options(options),
	m_signId(signId),
    m_extraOptions(extraOptions),
    m_text(text)
{
}

KGpgSignText::KGpgSignText(QObject *parent, const QString &signId, const QList<QUrl> &files, const SignOptions &options, const QStringList &extraOptions)
	: KGpgTextOrFileTransaction(parent, files),
	m_fileIndex(0),
	m_options(options),
	m_signId(signId),
	m_extraOptions(extraOptions)
{
	/* GnuPG can only handle one file at a time when signing */
	Q_ASSERT(files.count() == 1);
}

KGpgSignText::~KGpgSignText()
{
}

QStringList
KGpgSignText::command() const
{
	QStringList ret = m_extraOptions;

    const QList<QUrl> &files = getInputFiles();
	QString fileName;

	if (!files.isEmpty())
		fileName = files.first().path();

	ret << QLatin1String("-u") << m_signId;

	if (m_options & AsciiArmored) {
		if (fileName.isEmpty())
			ret << QLatin1String("--clearsign");
		else
			ret << QLatin1String("--armor");
	}
    /*if (KGpgSettings::pgpCompatibility())
        ret << QLatin1String("--pgp6");*/

	if (!fileName.isEmpty()) {
		if (m_options & DetachedSignature)
			ret << QLatin1String("--detach-sign") <<
					QLatin1String("--output") << fileName + QLatin1String(".sig");

		ret << fileName;
	}

    // command-fd for pass?
    ret << QLatin1String("--command-fd=0");

	return ret;
}

QStringList
KGpgSignText::signedText() const
{
	QStringList result;

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

	return result;
}

void
KGpgSignText::postStart()
{
    // do nothing, its to early
}

bool KGpgSignText::nextLine(const QString &line)  {

    if (line.startsWith(QLatin1String("[GNUPG:] BEGIN_SIGNING")) &! m_text.isEmpty()) {
        GPGProc *proc = getProcess();
        proc->write(m_text.toUtf8());
        proc->closeWriteChannel();
    } else if (!line.startsWith(QLatin1String("[GNUPG:] SIGEXPIRED")) && !line.startsWith(QLatin1String("[GNUPG:] KEYEXPIRED ")))
        m_messages.append(line);

    return false;
}

const QStringList &
KGpgSignText::getMessages() const
{
    return m_messages;
}

//#include "kgpgsigntext.moc"