1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
3
Gwenview: an image viewer
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22
#include "importer.moc"
27
#include <kdatetime.h>
29
#include <kfileitem.h>
33
#include <kio/copyjob.h>
35
#include <kio/jobuidelegate.h>
36
#include <kio/netaccess.h>
37
#include <kstandarddirs.h>
43
#include <fileutils.h>
44
#include <filenameformater.h>
45
#include <lib/timeutils.h>
49
struct ImporterPrivate {
52
std::auto_ptr<FileNameFormater> mFileNameFormater;
53
std::auto_ptr<KTempDir> mDestImportDir;
55
/* @defgroup reset Should be reset in start()
58
KUrl::List mImportedUrlList;
59
KUrl::List mSkippedUrlList;
67
void emitError(const QString& message) {
68
QMetaObject::invokeMethod(q, "error", Q_ARG(QString, message));
71
bool createImportDir(const KUrl url) {
72
Q_ASSERT(url.isLocalFile());
73
// FIXME: Support remote urls
75
if (!KStandardDirs::makeDir(url.toLocalFile())) {
76
emitError(i18n("Could not create destination folder."));
79
mDestImportDir.reset(new KTempDir(url.toLocalFile() + "/.gwenview_importer-"));
80
mDestImportDir->setAutoRemove(false);
81
if (mDestImportDir->status() != 0) {
82
const QString message = QString::fromLocal8Bit(::strerror(mDestImportDir->status()));
83
emitError(i18n("Could not create temporary upload folder:\n%1", message));
90
if (mUrlList.empty()) {
94
mCurrentUrl = mUrlList.takeFirst();
95
KUrl dst = KUrl(mDestImportDir->name());
96
dst.addPath(mCurrentUrl.fileName());
97
KIO::Job* job = KIO::copy(mCurrentUrl, dst, KIO::HideProgressInfo);
99
job->ui()->setWindow(mAuthWindow);
101
QObject::connect(job, SIGNAL(result(KJob*)),
102
q, SLOT(slotCopyDone(KJob*)));
103
QObject::connect(job, SIGNAL(percent(KJob*, unsigned long)),
104
q, SLOT(slotPercent(KJob*, unsigned long)));
107
void renameImportedUrl(const KUrl& src) {
111
if (mFileNameFormater.get()) {
112
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, src, true /* delayedMimeTypes */);
113
KDateTime dateTime = TimeUtils::dateTimeForFileItem(item);
114
fileName = mFileNameFormater->format(src, dateTime);
116
fileName = src.fileName();
118
dst.setFileName(fileName);
120
FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow);
122
case FileUtils::RenamedOK:
123
mImportedUrlList << mCurrentUrl;
125
case FileUtils::RenamedUnderNewName:
127
mImportedUrlList << mCurrentUrl;
129
case FileUtils::Skipped:
130
mSkippedUrlList << mCurrentUrl;
132
case FileUtils::RenameFailed:
133
kWarning() << "Rename failed for" << mCurrentUrl;
141
Importer::Importer(QWidget* parent)
143
, d(new ImporterPrivate) {
145
d->mAuthWindow = parent;
148
Importer::~Importer() {
152
void Importer::setAutoRenameFormat(const QString& format) {
153
if (format.isEmpty()) {
154
d->mFileNameFormater.reset(0);
156
d->mFileNameFormater.reset(new FileNameFormater(format));
160
void Importer::start(const KUrl::List& list, const KUrl& destination) {
162
d->mImportedUrlList.clear();
163
d->mSkippedUrlList.clear();
164
d->mRenamedCount = 0;
168
emitProgressChanged();
169
maximumChanged(d->mUrlList.count() * 100);
171
if (!d->createImportDir(destination)) {
172
kWarning() << "Could not create import dir";
178
void Importer::slotCopyDone(KJob* _job) {
179
KIO::CopyJob* job = static_cast<KIO::CopyJob*>(_job);
180
KUrl url = job->destUrl();
182
kWarning() << "FIXME: What do we do with failed urls?";
188
d->renameImportedUrl(url);
191
void Importer::finalizeImport() {
192
d->mDestImportDir->unlink();
196
void Importer::advance() {
199
emitProgressChanged();
202
void Importer::slotPercent(KJob*, unsigned long percent) {
203
d->mJobProgress = percent;
204
emitProgressChanged();
207
void Importer::emitProgressChanged() {
208
progressChanged(d->mProgress * 100 + d->mJobProgress);
211
KUrl::List Importer::importedUrlList() const {
212
return d->mImportedUrlList;
215
KUrl::List Importer::skippedUrlList() const {
216
return d->mSkippedUrlList;
219
int Importer::renamedCount() const {
220
return d->mRenamedCount;