• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

ark

  • sources
  • kde-4.14
  • kdeutils
  • ark
  • kerfuffle
addtoarchive.cpp
Go to the documentation of this file.
1 /*
2  * ark -- archiver for the KDE project
3  *
4  * Copyright (C) 2008 Harald Hvaal <haraldhv@stud.ntnu.no>
5  * Copyright (C) 2009 Raphael Kubo da Costa <rakuco@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "addtoarchive.h"
30 #include "adddialog.h"
31 #include "archive.h"
32 #include "jobs.h"
33 
34 #include <KConfig>
35 #include <kdebug.h>
36 #include <kjobtrackerinterface.h>
37 #include <kmessagebox.h>
38 #include <klocale.h>
39 #include <kio/job.h>
40 
41 #include <QFileInfo>
42 #include <QDir>
43 #include <QTimer>
44 #include <QWeakPointer>
45 
46 namespace Kerfuffle
47 {
48 AddToArchive::AddToArchive(QObject *parent)
49  : KJob(parent), m_changeToFirstPath(false)
50 {
51 }
52 
53 AddToArchive::~AddToArchive()
54 {
55 }
56 
57 void AddToArchive::setAutoFilenameSuffix(const QString& suffix)
58 {
59  m_autoFilenameSuffix = suffix;
60 }
61 
62 void AddToArchive::setChangeToFirstPath(bool value)
63 {
64  m_changeToFirstPath = value;
65 }
66 
67 void AddToArchive::setFilename(const KUrl& path)
68 {
69  m_filename = path.pathOrUrl();
70 }
71 
72 void AddToArchive::setMimeType(const QString & mimeType)
73 {
74  m_mimeType = mimeType;
75 }
76 
77 bool AddToArchive::showAddDialog(void)
78 {
79  QWeakPointer<Kerfuffle::AddDialog> dialog = new Kerfuffle::AddDialog(
80  m_inputs, // itemsToAdd
81  KUrl(m_firstPath), // startDir
82  QLatin1String( "" ), // filter
83  NULL, // parent
84  NULL); // widget
85 
86  bool ret = dialog.data()->exec();
87 
88  if (ret) {
89  kDebug() << "Returned URL:" << dialog.data()->selectedUrl();
90  kDebug() << "Returned mime:" << dialog.data()->currentMimeFilter();
91  setFilename(dialog.data()->selectedUrl());
92  setMimeType(dialog.data()->currentMimeFilter());
93  }
94 
95  delete dialog.data();
96 
97  return ret;
98 }
99 
100 bool AddToArchive::addInput(const KUrl& url)
101 {
102  m_inputs << url.pathOrUrl(
103  QFileInfo(url.pathOrUrl()).isDir() ?
104  KUrl::AddTrailingSlash :
105  KUrl::RemoveTrailingSlash
106  );
107 
108  if (m_firstPath.isEmpty()) {
109  QString firstEntry = url.pathOrUrl(KUrl::RemoveTrailingSlash);
110  m_firstPath = QFileInfo(firstEntry).dir().absolutePath();
111  }
112 
113  return true;
114 }
115 
116 void AddToArchive::start()
117 {
118  QTimer::singleShot(0, this, SLOT(slotStartJob()));
119 }
120 
121 // TODO: If this class should ever be called outside main.cpp,
122 // the returns should be preceded by emitResult().
123 void AddToArchive::slotStartJob(void)
124 {
125  kDebug();
126 
127  Kerfuffle::CompressionOptions options;
128 
129  if (!m_inputs.size()) {
130  KMessageBox::error(NULL, i18n("No input files were given."));
131  return;
132  }
133 
134  Kerfuffle::Archive *archive;
135  if (!m_filename.isEmpty()) {
136  archive = Kerfuffle::Archive::create(m_filename, m_mimeType, this);
137  kDebug() << "Set filename to " << m_filename;
138  } else {
139  if (m_autoFilenameSuffix.isEmpty()) {
140  KMessageBox::error(NULL, i18n("You need to either supply a filename for the archive or a suffix (such as rar, tar.gz) with the <command>--autofilename</command> argument."));
141  return;
142  }
143 
144  if (m_firstPath.isEmpty()) {
145  kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
146  return;
147  }
148 
149  QString base = QFileInfo(m_inputs.first()).absoluteFilePath();
150  if (base.endsWith(QLatin1Char('/'))) {
151  base.chop(1);
152  }
153 
154  QString finalName = base + QLatin1Char( '.' ) + m_autoFilenameSuffix;
155 
156  //if file already exists, append a number to the base until it doesn't
157  //exist
158  int appendNumber = 0;
159  while (QFileInfo(finalName).exists()) {
160  ++appendNumber;
161  finalName = base + QLatin1Char( '_' ) + QString::number(appendNumber) + QLatin1Char( '.' ) + m_autoFilenameSuffix;
162  }
163 
164  kDebug() << "Autoset filename to "<< finalName;
165  archive = Kerfuffle::Archive::create(finalName, m_mimeType, this);
166  }
167 
168  if (archive == NULL) {
169  KMessageBox::error(NULL, i18n("Failed to create the new archive. Permissions might not be sufficient."));
170  return;
171  } else if (archive->isReadOnly()) {
172  KMessageBox::error(NULL, i18n("It is not possible to create archives of this type."));
173  return;
174  }
175 
176  if (m_changeToFirstPath) {
177  if (m_firstPath.isEmpty()) {
178  kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
179  return;
180  }
181 
182  const QDir stripDir(m_firstPath);
183 
184  for (int i = 0; i < m_inputs.size(); ++i) {
185  m_inputs[i] = stripDir.absoluteFilePath(m_inputs.at(i));
186  }
187 
188  options[QLatin1String( "GlobalWorkDir" )] = stripDir.path();
189  kDebug() << "Setting GlobalWorkDir to " << stripDir.path();
190  }
191 
192  Kerfuffle::AddJob *job =
193  archive->addFiles(m_inputs, options);
194 
195  KIO::getJobTracker()->registerJob(job);
196 
197  connect(job, SIGNAL(result(KJob*)),
198  this, SLOT(slotFinished(KJob*)));
199 
200  job->start();
201 }
202 
203 void AddToArchive::slotFinished(KJob *job)
204 {
205  kDebug();
206 
207  if (job->error()) {
208  KMessageBox::error(NULL, job->errorText());
209  }
210 
211  emitResult();
212 }
213 }
Kerfuffle::AddToArchive::setAutoFilenameSuffix
void setAutoFilenameSuffix(const QString &suffix)
Definition: addtoarchive.cpp:57
Kerfuffle::Job::start
void start()
Definition: jobs.cpp:103
addtoarchive.h
adddialog.h
QList::at
const T & at(int i) const
Kerfuffle::AddToArchive::setFilename
void setFilename(const KUrl &path)
Definition: addtoarchive.cpp:67
Kerfuffle::AddJob
Definition: jobs.h:140
QWeakPointer::data
T * data() const
archive.h
Kerfuffle::AddDialog
Definition: adddialog.h:39
Kerfuffle::AddToArchive::start
void start()
Definition: addtoarchive.cpp:116
QString::chop
void chop(int n)
Kerfuffle::Archive::create
KJob * create()
Definition: archive.cpp:145
QList::size
int size() const
Kerfuffle::AddToArchive::AddToArchive
AddToArchive(QObject *parent=0)
Definition: addtoarchive.cpp:48
QString::number
QString number(int n, int base)
QHash< QString, QVariant >
QObject
QString::isEmpty
bool isEmpty() const
Kerfuffle::AddToArchive::~AddToArchive
~AddToArchive()
Definition: addtoarchive.cpp:53
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QFileInfo::dir
QDir dir() const
QList::first
T & first()
QString
Kerfuffle::Archive
Definition: archive.h:88
QFileInfo
Kerfuffle::AddToArchive::setMimeType
void setMimeType(const QString &mimeType)
Definition: addtoarchive.cpp:72
QLatin1Char
Kerfuffle::AddToArchive::showAddDialog
bool showAddDialog()
Definition: addtoarchive.cpp:77
QDir
Kerfuffle::AddToArchive::setChangeToFirstPath
void setChangeToFirstPath(bool value)
Definition: addtoarchive.cpp:62
jobs.h
QLatin1String
QDir::absolutePath
QString absolutePath() const
Kerfuffle::AddToArchive::addInput
bool addInput(const KUrl &url)
Definition: addtoarchive.cpp:100
QWeakPointer
Kerfuffle::Archive::addFiles
AddJob * addFiles(const QStringList &files, const CompressionOptions &options=CompressionOptions())
Compression options that should be handled by all interfaces:
Definition: archive.cpp:174
KJob
Kerfuffle::Archive::isReadOnly
bool isReadOnly() const
Definition: archive.cpp:135
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ark

Skip menu "ark"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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