~ubuntu-branches/ubuntu/maverick/kdegraphics/maverick-proposed

« back to all changes in this revision

Viewing changes to gwenview/importer/importer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-12-02 14:03:43 UTC
  • mfrom: (1.1.35 upstream)
  • Revision ID: james.westby@ubuntu.com-20091202140343-2732gbkj69g89arq
Tags: 4:4.3.80-0ubuntu1
* New upstream beta release:
  - Add build-depend on shared-desktop-ontologies for nepomuk integration
  - Bump .so versions for libkexiv, libkdcraw and libkipi
  - Update various .install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "importer.moc"
 
23
 
 
24
// Qt
 
25
 
 
26
// KDE
 
27
#include <kdatetime.h>
 
28
#include <kdebug.h>
 
29
#include <kfileitem.h>
 
30
#include <klocale.h>
 
31
#include <ktempdir.h>
 
32
#include <kurl.h>
 
33
#include <kio/copyjob.h>
 
34
#include <kio/job.h>
 
35
#include <kio/jobuidelegate.h>
 
36
#include <kio/netaccess.h>
 
37
#include <kstandarddirs.h>
 
38
 
 
39
// stdc++
 
40
#include <memory>
 
41
 
 
42
// Local
 
43
#include <fileutils.h>
 
44
#include <filenameformater.h>
 
45
#include <lib/timeutils.h>
 
46
 
 
47
namespace Gwenview {
 
48
 
 
49
struct ImporterPrivate {
 
50
        Importer* q;
 
51
        QWidget* mAuthWindow;
 
52
        std::auto_ptr<FileNameFormater> mFileNameFormater;
 
53
        std::auto_ptr<KTempDir> mDestImportDir;
 
54
 
 
55
        /* @defgroup reset Should be reset in start()
 
56
         * @{ */
 
57
        KUrl::List mUrlList;
 
58
        KUrl::List mImportedUrlList;
 
59
        KUrl::List mSkippedUrlList;
 
60
        int mRenamedCount;
 
61
        int mProgress;
 
62
        int mJobProgress;
 
63
        /* @} */
 
64
 
 
65
        KUrl mCurrentUrl;
 
66
 
 
67
        void emitError(const QString& message) {
 
68
                QMetaObject::invokeMethod(q, "error", Q_ARG(QString, message));
 
69
        }
 
70
 
 
71
        bool createImportDir(const KUrl url) {
 
72
                Q_ASSERT(url.isLocalFile());
 
73
                // FIXME: Support remote urls
 
74
 
 
75
                if (!KStandardDirs::makeDir(url.toLocalFile())) {
 
76
                        emitError(i18n("Could not create destination folder."));
 
77
                        return false;
 
78
                }
 
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));
 
84
                        return false;
 
85
                }
 
86
                return true;
 
87
        }
 
88
 
 
89
        void importNext() {
 
90
                if (mUrlList.empty()) {
 
91
                        q->finalizeImport();
 
92
                        return;
 
93
                }
 
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);
 
98
                if (job->ui()) {
 
99
                        job->ui()->setWindow(mAuthWindow);
 
100
                }
 
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)));
 
105
        }
 
106
 
 
107
        void renameImportedUrl(const KUrl& src) {
 
108
                KUrl dst = src;
 
109
                dst.cd("..");
 
110
                QString fileName;
 
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);
 
115
                } else {
 
116
                        fileName = src.fileName();
 
117
                }
 
118
                dst.setFileName(fileName);
 
119
 
 
120
                FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow);
 
121
                switch (result) {
 
122
                case FileUtils::RenamedOK:
 
123
                        mImportedUrlList << mCurrentUrl;
 
124
                        break;
 
125
                case FileUtils::RenamedUnderNewName:
 
126
                        mRenamedCount++;
 
127
                        mImportedUrlList << mCurrentUrl;
 
128
                        break;
 
129
                case FileUtils::Skipped:
 
130
                        mSkippedUrlList << mCurrentUrl;
 
131
                        break;
 
132
                case FileUtils::RenameFailed:
 
133
                        kWarning() << "Rename failed for" << mCurrentUrl;
 
134
                }
 
135
                q->advance();
 
136
                importNext();
 
137
        }
 
138
};
 
139
 
 
140
 
 
141
Importer::Importer(QWidget* parent)
 
142
: QObject(parent)
 
143
, d(new ImporterPrivate) {
 
144
        d->q = this;
 
145
        d->mAuthWindow = parent;
 
146
}
 
147
 
 
148
Importer::~Importer() {
 
149
        delete d;
 
150
}
 
151
 
 
152
void Importer::setAutoRenameFormat(const QString& format) {
 
153
        if (format.isEmpty()) {
 
154
                d->mFileNameFormater.reset(0);
 
155
        } else {
 
156
                d->mFileNameFormater.reset(new FileNameFormater(format));
 
157
        }
 
158
}
 
159
 
 
160
void Importer::start(const KUrl::List& list, const KUrl& destination) {
 
161
        d->mUrlList = list;
 
162
        d->mImportedUrlList.clear();
 
163
        d->mSkippedUrlList.clear();
 
164
        d->mRenamedCount = 0;
 
165
        d->mProgress = 0;
 
166
        d->mJobProgress = 0;
 
167
 
 
168
        emitProgressChanged();
 
169
        maximumChanged(d->mUrlList.count() * 100);
 
170
 
 
171
        if (!d->createImportDir(destination)) {
 
172
                kWarning() << "Could not create import dir";
 
173
                return;
 
174
        }
 
175
        d->importNext();
 
176
}
 
177
 
 
178
void Importer::slotCopyDone(KJob* _job) {
 
179
        KIO::CopyJob* job = static_cast<KIO::CopyJob*>(_job);
 
180
        KUrl url = job->destUrl();
 
181
        if (job->error()) {
 
182
                kWarning() << "FIXME: What do we do with failed urls?";
 
183
                advance();
 
184
                d->importNext();
 
185
                return;
 
186
        }
 
187
 
 
188
        d->renameImportedUrl(url);
 
189
}
 
190
 
 
191
void Importer::finalizeImport() {
 
192
        d->mDestImportDir->unlink();
 
193
        importFinished();
 
194
}
 
195
 
 
196
void Importer::advance() {
 
197
        ++d->mProgress;
 
198
        d->mJobProgress = 0;
 
199
        emitProgressChanged();
 
200
}
 
201
 
 
202
void Importer::slotPercent(KJob*, unsigned long percent) {
 
203
        d->mJobProgress = percent;
 
204
        emitProgressChanged();
 
205
}
 
206
 
 
207
void Importer::emitProgressChanged() {
 
208
        progressChanged(d->mProgress * 100 + d->mJobProgress);
 
209
}
 
210
 
 
211
KUrl::List Importer::importedUrlList() const {
 
212
        return d->mImportedUrlList;
 
213
}
 
214
 
 
215
KUrl::List Importer::skippedUrlList() const {
 
216
        return d->mSkippedUrlList;
 
217
}
 
218
 
 
219
int Importer::renamedCount() const {
 
220
        return d->mRenamedCount;
 
221
}
 
222
 
 
223
} // namespace