~ubuntu-branches/ubuntu/precise/kgpg/precise-updates

« back to all changes in this revision

Viewing changes to foldercompressjob.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-12-24 23:16:12 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111224231612-1esf4004sfou258b
Tags: 4:4.7.95-0ubuntu1
New upstream release candidate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 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 "foldercompressjob.h"
 
15
 
 
16
#include <QDir>
 
17
#include <QTimer>
 
18
 
 
19
#include <KArchive>
 
20
#include <KLocale>
 
21
#include <KTar>
 
22
#include <KTemporaryFile>
 
23
#include <KZip>
 
24
 
 
25
#include "transactions/kgpgencrypt.h"
 
26
 
 
27
FolderCompressJob::FolderCompressJob(QObject *parent, const KUrl &source, const KUrl &dest, KTemporaryFile *tempfile, const QStringList &keys, const QStringList &options,  const KGpgEncrypt::EncryptOptions encOptions)
 
28
        : KJob(parent),
 
29
        m_description(i18n("Processing folder compression and encryption")),
 
30
        m_source(source),
 
31
        m_dest(dest),
 
32
        m_tempfile(tempfile),
 
33
        m_keys(keys),
 
34
        m_options(options),
 
35
        m_encOptions(encOptions)
 
36
{
 
37
}
 
38
 
 
39
FolderCompressJob::~FolderCompressJob()
 
40
{
 
41
}
 
42
 
 
43
void
 
44
FolderCompressJob::start()
 
45
{
 
46
        emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"), i18nc("Job is started up", "Startup")));
 
47
        QTimer::singleShot(0, this, SLOT(doWork()));
 
48
}
 
49
 
 
50
void
 
51
FolderCompressJob::doWork()
 
52
{
 
53
        KArchive *arch = NULL;
 
54
        int compressionScheme = 0;
 
55
        switch (compressionScheme) {
 
56
        case 0:
 
57
                arch = new KZip(m_tempfile->fileName());
 
58
                break;
 
59
        case 1:
 
60
                arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-gzip" ));
 
61
                break;
 
62
        case 2:
 
63
                arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-bzip" ));
 
64
                break;
 
65
        case 3:
 
66
                arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-tar" ));
 
67
                break;
 
68
        case 4:
 
69
                arch = new KTar(m_tempfile->fileName(), QLatin1String( "application/x-xz" ));
 
70
                break;
 
71
        default:
 
72
                Q_ASSERT(0);
 
73
        }
 
74
 
 
75
        if (!arch->open(QIODevice::WriteOnly)) {
 
76
                setError(UserDefinedError);
 
77
                setErrorText(i18n("Unable to create temporary file"));
 
78
                delete arch;
 
79
                emitResult();
 
80
                return;
 
81
        }
 
82
 
 
83
        arch->addLocalDirectory(m_source.path(), m_source.fileName());
 
84
        arch->close();
 
85
        delete arch;
 
86
 
 
87
        setPercent(50);
 
88
 
 
89
        QDir outPath = m_source.path();
 
90
        outPath.cdUp();
 
91
 
 
92
        m_options << QLatin1String("--output") << QDir::toNativeSeparators(outPath.path() + QDir::separator()) + m_dest.fileName();
 
93
 
 
94
        emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"),
 
95
                        i18nc("Status message 'Encrypting <filename>' (operation starts)", "Encrypting %1", m_dest.path())));
 
96
                
 
97
 
 
98
        KGpgEncrypt *enc = new KGpgEncrypt(this, m_keys, KUrl::List(m_tempfile->fileName()), m_encOptions, m_options);
 
99
        connect(enc, SIGNAL(done(int)), SLOT(slotEncryptionDone(int)));
 
100
        enc->start();
 
101
}
 
102
 
 
103
void
 
104
FolderCompressJob::slotEncryptionDone(int result)
 
105
{
 
106
        sender()->deleteLater();
 
107
 
 
108
        if ((result != KGpgTransaction::TS_OK) && (result != KGpgTransaction::TS_USER_ABORTED)) {
 
109
                setError(UserDefinedError + 1);
 
110
                setErrorText(i18n("The encryption failed with error code %1", result));
 
111
                emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"), i18n("Encryption failed.")));
 
112
        } else {
 
113
                emit description(this, m_description, qMakePair(i18nc("State of operation as in status", "State"),
 
114
                                i18nc("Status message 'Encrypted <filename>' (operation was completed)", "Encrypted %1", m_dest.path())));
 
115
        }
 
116
 
 
117
        emitResult();
 
118
}
 
119
 
 
120
#include "foldercompressjob.moc"