• 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
foldercompressjob.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011,2012,2013 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 <KArchive>
17 #include <KLocale>
18 #include <KTar>
19 #include <KTemporaryFile>
20 #include <KZip>
21 #include <QDir>
22 #include <QMetaObject>
23 #include <QStringList>
24 
25 class FolderCompressJobPrivate {
26  FolderCompressJob * const q_ptr;
27  Q_DECLARE_PUBLIC(FolderCompressJob)
28 
29 public:
30  FolderCompressJobPrivate(FolderCompressJob *parent, const KUrl::List &sources, const KUrl &dest, KTemporaryFile *tempfile, const QStringList &keys, const QStringList &options, const KGpgEncrypt::EncryptOptions encOptions, const int archive);
31 
32  const QString m_description;
33  const KUrl::List m_sources;
34  const KUrl m_dest;
35  KTemporaryFile * const m_tempfile;
36  const QStringList m_keys;
37  QStringList m_options;
38  const KGpgEncrypt::EncryptOptions m_encOptions;
39  const int m_archiveType;
40 };
41 
42 FolderCompressJobPrivate::FolderCompressJobPrivate(FolderCompressJob *parent, const KUrl::List &sources, const KUrl &dest, KTemporaryFile *tempfile, const QStringList &keys, const QStringList &options, const KGpgEncrypt::EncryptOptions encOptions, const int archive)
43  : q_ptr(parent),
44  m_description(i18n("Processing folder compression and encryption")),
45  m_sources(sources),
46  m_dest(dest),
47  m_tempfile(tempfile),
48  m_keys(keys),
49  m_options(options),
50  m_encOptions(encOptions),
51  m_archiveType(archive)
52 {
53 }
54 
55 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)
56  : KJob(parent),
57  d_ptr(new FolderCompressJobPrivate(this, sources, dest, tempfile, keys, options, encOptions, archive))
58 {
59 }
60 
61 FolderCompressJob::~FolderCompressJob()
62 {
63  delete d_ptr;
64 }
65 
66 void
67 FolderCompressJob::start()
68 {
69  Q_D(FolderCompressJob);
70 
71  emit description(this, d->m_description, qMakePair(i18nc("State of operation as in status", "State"), i18nc("Job is started up", "Startup")));
72  QMetaObject::invokeMethod(this, "doWork", Qt::QueuedConnection);
73 }
74 
75 void
76 FolderCompressJob::doWork()
77 {
78  Q_D(FolderCompressJob);
79  KArchive *arch = NULL;
80 
81  switch (d->m_archiveType) {
82  case 0:
83  arch = new KZip(d->m_tempfile->fileName());
84  break;
85  case 1:
86  arch = new KTar(d->m_tempfile->fileName(), QLatin1String( "application/x-gzip" ));
87  break;
88  case 2:
89  arch = new KTar(d->m_tempfile->fileName(), QLatin1String( "application/x-bzip" ));
90  break;
91  case 3:
92  arch = new KTar(d->m_tempfile->fileName(), QLatin1String( "application/x-tar" ));
93  break;
94  case 4:
95  arch = new KTar(d->m_tempfile->fileName(), QLatin1String( "application/x-xz" ));
96  break;
97  default:
98  Q_ASSERT(0);
99  return;
100  }
101 
102  if (!arch->open(QIODevice::WriteOnly)) {
103  setError(UserDefinedError);
104  setErrorText(i18n("Unable to create temporary file"));
105  delete arch;
106  emitResult();
107  return;
108  }
109 
110  foreach (const KUrl &url, d->m_sources)
111  arch->addLocalDirectory(url.path(), url.fileName());
112  arch->close();
113  delete arch;
114 
115  setPercent(50);
116 
117  QDir outPath = d->m_sources.first().path();
118  outPath.cdUp();
119 
120  d->m_options << QLatin1String("--output") << QDir::toNativeSeparators(outPath.path() + QDir::separator()) + d->m_dest.fileName();
121 
122  emit description(this, d->m_description, qMakePair(i18nc("State of operation as in status", "State"),
123  i18nc("Status message 'Encrypting <filename>' (operation starts)", "Encrypting %1", d->m_dest.path())));
124 
125 
126  KGpgEncrypt *enc = new KGpgEncrypt(this, d->m_keys, KUrl::List(KUrl::fromPath(d->m_tempfile->fileName())), d->m_encOptions, d->m_options);
127  connect(enc, SIGNAL(done(int)), SLOT(slotEncryptionDone(int)));
128  enc->start();
129 }
130 
131 void
132 FolderCompressJob::slotEncryptionDone(int result)
133 {
134  Q_D(FolderCompressJob);
135 
136  sender()->deleteLater();
137 
138  if ((result != KGpgTransaction::TS_OK) && (result != KGpgTransaction::TS_USER_ABORTED)) {
139  setError(KJob::UserDefinedError + 1);
140  setErrorText(i18n("The encryption failed with error code %1", result));
141  emit description(this, d->m_description, qMakePair(i18nc("State of operation as in status", "State"), i18n("Encryption failed.")));
142  } else {
143  emit description(this, d->m_description, qMakePair(i18nc("State of operation as in status", "State"),
144  i18nc("Status message 'Encrypted <filename>' (operation was completed)", "Encrypted %1", d->m_dest.path())));
145  }
146 
147  emitResult();
148 }
149 
150 QString
151 FolderCompressJob::extensionForArchive(const int archive)
152 {
153  switch (archive) {
154  case 0:
155  return QLatin1String(".zip");
156  case 1:
157  return QLatin1String(".tar.gz");
158  case 2:
159  return QLatin1String(".tar.bz2");
160  case 3:
161  return QLatin1String(".tar");
162  case 4:
163  return QLatin1String(".tar.xz");
164  default:
165  Q_ASSERT(archive <= archiveNames().count());
166  Q_ASSERT(archive >= 0);
167  return QString();
168  }
169 }
170 
171 const QStringList &
172 FolderCompressJob::archiveNames()
173 {
174  static const QStringList archives =
175  QStringList(i18n("Zip")) <<
176  i18n("Tar/Gzip") <<
177  i18n("Tar/Bzip2") <<
178  i18n("Tar") <<
179  i18n("Tar/XZ");
180 
181  return archives;
182 }
183 
184 #include "foldercompressjob.moc"
foldercompressjob.h
QDir::toNativeSeparators
QString toNativeSeparators(const QString &pathName)
KGpgTransaction::TS_OK
everything went fine
Definition: kgpgtransaction.h:60
QDir::separator
QChar separator()
QDir::path
QString path() const
QObject
FolderCompressJob::archiveNames
static const QStringList & archiveNames()
get list of supported archive names
Definition: foldercompressjob.cpp:172
QString
FolderCompressJob::~FolderCompressJob
virtual ~FolderCompressJob()
FolderCompressJob destructor.
Definition: foldercompressjob.cpp:61
QDir::cdUp
bool cdUp()
QStringList
KGpgTransaction::TS_USER_ABORTED
the user aborted the transaction
Definition: kgpgtransaction.h:63
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QDir
FolderCompressJob::extensionForArchive
static QString extensionForArchive(const int archive)
query extension for archive type
Definition: foldercompressjob.cpp:151
FolderCompressJob
Create an encrypted archive of the given folders.
Definition: foldercompressjob.h:33
QLatin1String
description
static const char description[]
Definition: main.cpp:23
FolderCompressJob::start
virtual void start()
shows the progress indicator
Definition: foldercompressjob.cpp:67
KJob
KGpgEncrypt
encrypt the given text or files
Definition: kgpgencrypt.h:30
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