~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to importer/fileutils.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
35
35
#include <kio/netaccess.h>
36
36
#include <kurl.h>
37
37
 
38
 
namespace Gwenview {
39
 
namespace FileUtils {
40
 
 
41
 
bool contentsAreIdentical(const KUrl& url1, const KUrl& url2, QWidget* authWindow) {
42
 
        // FIXME: Support remote urls
43
 
        QFile file1(KIO::NetAccess::mostLocalUrl(url1, authWindow).toLocalFile());
44
 
        if (!file1.open(QIODevice::ReadOnly)) {
45
 
                // Can't read url1, assume it's different from url2
46
 
                kWarning() << "Can't read" << url1;
47
 
                return false;
48
 
        }
49
 
 
50
 
        QFile file2(KIO::NetAccess::mostLocalUrl(url2, authWindow).toLocalFile());
51
 
        if (!file2.open(QIODevice::ReadOnly)) {
52
 
                // Can't read url2, assume it's different from url1
53
 
                kWarning() << "Can't read" << url2;
54
 
                return false;
55
 
        }
56
 
 
57
 
        const int CHUNK_SIZE = 4096;
58
 
        while (!file1.atEnd() && !file2.atEnd()) {
59
 
                QByteArray url1Array = file1.read(CHUNK_SIZE);
60
 
                QByteArray url2Array = file2.read(CHUNK_SIZE);
61
 
 
62
 
                if (url1Array != url2Array) {
63
 
                        return false;
64
 
                }
65
 
        }
66
 
        if (file1.atEnd() && file2.atEnd()) {
67
 
                return true;
68
 
        } else {
69
 
                kWarning() << "One file ended before the other";
70
 
                return false;
71
 
        }
72
 
}
73
 
 
74
 
RenameResult rename(const KUrl& src, const KUrl& dst_, QWidget* authWindow) {
75
 
        KUrl dst = dst_;
76
 
        RenameResult result = RenamedOK;
77
 
        int count = 1;
78
 
 
79
 
        QFileInfo fileInfo(dst.fileName());
80
 
        QString prefix = fileInfo.baseName() + '_';
81
 
        QString suffix = '.' + fileInfo.completeSuffix();
82
 
 
83
 
        // Get src size
84
 
        KIO::UDSEntry udsEntry;
85
 
        KIO::NetAccess::stat(dst, udsEntry, authWindow);
86
 
        KFileItem item(udsEntry, src, true /* delayedMimeTypes */);
87
 
        KIO::filesize_t srcSize = item.size();
88
 
 
89
 
        // Find unique name
90
 
        while (KIO::NetAccess::stat(dst, udsEntry, authWindow)) {
91
 
                // File exists. If it's not the same, try to create a new name
92
 
                item = KFileItem(udsEntry, dst, true /* delayedMimeTypes */);
93
 
                KIO::filesize_t dstSize = item.size();
94
 
 
95
 
                if (srcSize == dstSize && contentsAreIdentical(src, dst, authWindow)) {
96
 
                        // Already imported, skip it
97
 
                        KIO::Job* job = KIO::file_delete(src, KIO::HideProgressInfo);
98
 
                        if (job->ui()) {
99
 
                                job->ui()->setWindow(authWindow);
100
 
                        }
101
 
 
102
 
                        return Skipped;
103
 
                }
104
 
                result = RenamedUnderNewName;
105
 
 
106
 
                dst.setFileName(prefix + QString::number(count) + suffix);
107
 
                ++count;
108
 
        }
109
 
 
110
 
        // Rename
111
 
        KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo);
112
 
        if (!KIO::NetAccess::synchronousRun(job, authWindow)) {
113
 
                result = RenameFailed;
114
 
        }
115
 
        return result;
116
 
}
117
 
 
 
38
namespace Gwenview
 
39
{
 
40
namespace FileUtils
 
41
{
 
42
 
 
43
bool contentsAreIdentical(const KUrl& url1, const KUrl& url2, QWidget* authWindow)
 
44
{
 
45
    // FIXME: Support remote urls
 
46
    QFile file1(KIO::NetAccess::mostLocalUrl(url1, authWindow).toLocalFile());
 
47
    if (!file1.open(QIODevice::ReadOnly)) {
 
48
        // Can't read url1, assume it's different from url2
 
49
        kWarning() << "Can't read" << url1;
 
50
        return false;
 
51
    }
 
52
 
 
53
    QFile file2(KIO::NetAccess::mostLocalUrl(url2, authWindow).toLocalFile());
 
54
    if (!file2.open(QIODevice::ReadOnly)) {
 
55
        // Can't read url2, assume it's different from url1
 
56
        kWarning() << "Can't read" << url2;
 
57
        return false;
 
58
    }
 
59
 
 
60
    const int CHUNK_SIZE = 4096;
 
61
    while (!file1.atEnd() && !file2.atEnd()) {
 
62
        QByteArray url1Array = file1.read(CHUNK_SIZE);
 
63
        QByteArray url2Array = file2.read(CHUNK_SIZE);
 
64
 
 
65
        if (url1Array != url2Array) {
 
66
            return false;
 
67
        }
 
68
    }
 
69
    if (file1.atEnd() && file2.atEnd()) {
 
70
        return true;
 
71
    } else {
 
72
        kWarning() << "One file ended before the other";
 
73
        return false;
 
74
    }
 
75
}
 
76
 
 
77
RenameResult rename(const KUrl& src, const KUrl& dst_, QWidget* authWindow)
 
78
{
 
79
    KUrl dst = dst_;
 
80
    RenameResult result = RenamedOK;
 
81
    int count = 1;
 
82
 
 
83
    QFileInfo fileInfo(dst.fileName());
 
84
    QString prefix = fileInfo.baseName() + '_';
 
85
    QString suffix = '.' + fileInfo.completeSuffix();
 
86
 
 
87
    // Get src size
 
88
    KIO::UDSEntry udsEntry;
 
89
    KIO::NetAccess::stat(dst, udsEntry, authWindow);
 
90
    KFileItem item(udsEntry, src, true /* delayedMimeTypes */);
 
91
    KIO::filesize_t srcSize = item.size();
 
92
 
 
93
    // Find unique name
 
94
    while (KIO::NetAccess::stat(dst, udsEntry, authWindow)) {
 
95
        // File exists. If it's not the same, try to create a new name
 
96
        item = KFileItem(udsEntry, dst, true /* delayedMimeTypes */);
 
97
        KIO::filesize_t dstSize = item.size();
 
98
 
 
99
        if (srcSize == dstSize && contentsAreIdentical(src, dst, authWindow)) {
 
100
            // Already imported, skip it
 
101
            KIO::Job* job = KIO::file_delete(src, KIO::HideProgressInfo);
 
102
            if (job->ui()) {
 
103
                job->ui()->setWindow(authWindow);
 
104
            }
 
105
 
 
106
            return Skipped;
 
107
        }
 
108
        result = RenamedUnderNewName;
 
109
 
 
110
        dst.setFileName(prefix + QString::number(count) + suffix);
 
111
        ++count;
 
112
    }
 
113
 
 
114
    // Rename
 
115
    KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo);
 
116
    if (!KIO::NetAccess::synchronousRun(job, authWindow)) {
 
117
        result = RenameFailed;
 
118
    }
 
119
    return result;
 
120
}
118
121
 
119
122
} // namespace
120
123
} // namespace