~ubuntu-branches/ubuntu/vivid/gwenview/vivid-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// vim: set tabstop=4 shiftwidth=4 expandtab:
/*
Gwenview: an image viewer
Copyright 2009 Aurélien Gâteau <agateau@kde.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.

*/
// Self
#include "importer.moc"

// Qt

// KDE
#include <KDateTime>
#include <KDebug>
#include <KFileItem>
#include <KLocale>
#include <KUrl>
#include <KIO/CopyJob>
#include <KIO/DeleteJob>
#include <KIO/Job>
#include <KIO/JobUiDelegate>
#include <KIO/NetAccess>
#include <KStandardDirs>

// stdc++
#include <memory>

// Local
#include <fileutils.h>
#include <filenameformater.h>
#include <lib/timeutils.h>

namespace Gwenview
{

struct ImporterPrivate
{
    Importer* q;
    QWidget* mAuthWindow;
    std::auto_ptr<FileNameFormater> mFileNameFormater;
    KUrl mTempImportDir;

    /* @defgroup reset Should be reset in start()
     * @{ */
    KUrl::List mUrlList;
    KUrl::List mImportedUrlList;
    KUrl::List mSkippedUrlList;
    int mRenamedCount;
    int mProgress;
    int mJobProgress;
    /* @} */

    KUrl mCurrentUrl;

    void emitError(const QString& message)
    {
        QMetaObject::invokeMethod(q, "error", Q_ARG(QString, message));
    }

    bool createImportDir(const KUrl url)
    {
        Q_ASSERT(url.isLocalFile());
        // FIXME: Support remote urls

        if (!KStandardDirs::makeDir(url.toLocalFile())) {
            emitError(i18n("Could not create destination folder."));
            return false;
        }
        QString message;
        mTempImportDir = FileUtils::createTempDir(url.toLocalFile(), ".gwenview_importer-", &message);
        if (mTempImportDir.isEmpty()) {
            emitError(i18n("Could not create temporary upload folder:\n%1", message));
            return false;
        }
        return true;
    }

    void importNext()
    {
        if (mUrlList.empty()) {
            q->finalizeImport();
            return;
        }
        mCurrentUrl = mUrlList.takeFirst();
        KUrl dst = mTempImportDir;
        dst.addPath(mCurrentUrl.fileName());
        KIO::Job* job = KIO::copy(mCurrentUrl, dst, KIO::HideProgressInfo);
        if (job->ui()) {
            job->ui()->setWindow(mAuthWindow);
        }
        QObject::connect(job, SIGNAL(result(KJob*)),
                         q, SLOT(slotCopyDone(KJob*)));
        QObject::connect(job, SIGNAL(percent(KJob*,ulong)),
                         q, SLOT(slotPercent(KJob*,ulong)));
    }

    void renameImportedUrl(const KUrl& src)
    {
        KUrl dst = src;
        dst.cd("..");
        QString fileName;
        if (mFileNameFormater.get()) {
            KFileItem item(KFileItem::Unknown, KFileItem::Unknown, src, true /* delayedMimeTypes */);
            // Get the document time, but do not cache the result because the
            // 'src' url is temporary: if we import "foo/image.jpg" and
            // "bar/image.jpg", both images will be temporarily saved in the
            // 'src' url.
            KDateTime dateTime = TimeUtils::dateTimeForFileItem(item, TimeUtils::SkipCache);
            fileName = mFileNameFormater->format(src, dateTime);
        } else {
            fileName = src.fileName();
        }
        dst.setFileName(fileName);

        FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow);
        switch (result) {
        case FileUtils::RenamedOK:
            mImportedUrlList << mCurrentUrl;
            break;
        case FileUtils::RenamedUnderNewName:
            mRenamedCount++;
            mImportedUrlList << mCurrentUrl;
            break;
        case FileUtils::Skipped:
            mSkippedUrlList << mCurrentUrl;
            break;
        case FileUtils::RenameFailed:
            kWarning() << "Rename failed for" << mCurrentUrl;
        }
        q->advance();
        importNext();
    }
};

Importer::Importer(QWidget* parent)
: QObject(parent)
, d(new ImporterPrivate)
{
    d->q = this;
    d->mAuthWindow = parent;
}

Importer::~Importer()
{
    delete d;
}

void Importer::setAutoRenameFormat(const QString& format)
{
    if (format.isEmpty()) {
        d->mFileNameFormater.reset(0);
    } else {
        d->mFileNameFormater.reset(new FileNameFormater(format));
    }
}

void Importer::start(const KUrl::List& list, const KUrl& destination)
{
    d->mUrlList = list;
    d->mImportedUrlList.clear();
    d->mSkippedUrlList.clear();
    d->mRenamedCount = 0;
    d->mProgress = 0;
    d->mJobProgress = 0;

    emitProgressChanged();
    maximumChanged(d->mUrlList.count() * 100);

    if (!d->createImportDir(destination)) {
        kWarning() << "Could not create import dir";
        return;
    }
    d->importNext();
}

void Importer::slotCopyDone(KJob* _job)
{
    KIO::CopyJob* job = static_cast<KIO::CopyJob*>(_job);
    KUrl url = job->destUrl();
    if (job->error()) {
        kWarning() << "FIXME: What do we do with failed urls?";
        advance();
        d->importNext();
        return;
    }

    d->renameImportedUrl(url);
}

void Importer::finalizeImport()
{
    KIO::Job* job = KIO::del(d->mTempImportDir, KIO::HideProgressInfo);
    if (job->ui()) {
        job->ui()->setWindow(d->mAuthWindow);
    }
    importFinished();
}

void Importer::advance()
{
    ++d->mProgress;
    d->mJobProgress = 0;
    emitProgressChanged();
}

void Importer::slotPercent(KJob*, unsigned long percent)
{
    d->mJobProgress = percent;
    emitProgressChanged();
}

void Importer::emitProgressChanged()
{
    progressChanged(d->mProgress * 100 + d->mJobProgress);
}

KUrl::List Importer::importedUrlList() const
{
    return d->mImportedUrlList;
}

KUrl::List Importer::skippedUrlList() const
{
    return d->mSkippedUrlList;
}

int Importer::renamedCount() const
{
    return d->mRenamedCount;
}

} // namespace