~ubuntu-branches/ubuntu/quantal/kgpg/quantal-updates

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
/*
 * Copyright (C) 2011,2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
 */

/***************************************************************************
 *                                                                         *
 *   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 "foldercompressjob.h"

#include <QDir>
#include <QTimer>

#include <KArchive>
#include <KLocale>
#include <KTar>
#include <KTemporaryFile>
#include <KZip>

#include "transactions/kgpgencrypt.h"

FolderCompressJob::FolderCompressJob(QObject *parent, const KUrl::List &sources, const KUrl &dest, KTemporaryFile *tempfile, const QStringList &keys, const QStringList &options,  const KGpgEncrypt::EncryptOptions encOptions, const int archive)
	: KJob(parent),
	m_description(i18n("Processing folder compression and encryption")),
	m_sources(sources),
	m_dest(dest),
	m_tempfile(tempfile),
	m_keys(keys),
	m_options(options),
	m_encOptions(encOptions),
	m_archiveType(archive)
{
}

FolderCompressJob::~FolderCompressJob()
{
}

void
FolderCompressJob::start()
{
	emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"), i18nc("Job is started up", "Startup")));
	QTimer::singleShot(0, this, SLOT(doWork()));
}

void
FolderCompressJob::doWork()
{
	KArchive *arch = NULL;

	switch (m_archiveType) {
	case 0:
		arch = new KZip(m_tempfile->fileName());
		break;
	case 1:
		arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-gzip" ));
		break;
	case 2:
		arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-bzip" ));
		break;
	case 3:
		arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-tar" ));
		break;
	case 4:
		arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-xz" ));
		break;
	default:
		Q_ASSERT(0);
	}

	if (!arch->open(QIODevice::WriteOnly)) {
		setError(UserDefinedError);
		setErrorText(i18n("Unable to create temporary file"));
		delete arch;
		emitResult();
		return;
	}

	foreach (const KUrl &url, m_sources)
		arch->addLocalDirectory(url.path(), url.fileName());
	arch->close();
	delete arch;

	setPercent(50);

	QDir outPath = m_sources.first().path();
	outPath.cdUp();

	m_options << QLatin1String("--output") << QDir::toNativeSeparators(outPath.path() + QDir::separator()) + m_dest.fileName();

	emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"),
			i18nc("Status message 'Encrypting <filename>' (operation starts)", "Encrypting %1", m_dest.path())));
		

	KGpgEncrypt *enc = new KGpgEncrypt(this, m_keys, KUrl::List(m_tempfile->fileName()), m_encOptions, m_options);
	connect(enc, SIGNAL(done(int)), SLOT(slotEncryptionDone(int)));
	enc->start();
}

void
FolderCompressJob::slotEncryptionDone(int result)
{
	sender()->deleteLater();

	if ((result != KGpgTransaction::TS_OK) && (result != KGpgTransaction::TS_USER_ABORTED)) {
		setError(UserDefinedError + 1);
		setErrorText(i18n("The encryption failed with error code %1", result));
		emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"), i18n("Encryption failed.")));
	} else {
		emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"),
				i18nc("Status message 'Encrypted <filename>' (operation was completed)", "Encrypted %1", m_dest.path())));
	}

	emitResult();
}

#include "foldercompressjob.moc"