35
35
#include <kio/netaccess.h>
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;
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;
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);
62
if (url1Array != url2Array) {
66
if (file1.atEnd() && file2.atEnd()) {
69
kWarning() << "One file ended before the other";
74
RenameResult rename(const KUrl& src, const KUrl& dst_, QWidget* authWindow) {
76
RenameResult result = RenamedOK;
79
QFileInfo fileInfo(dst.fileName());
80
QString prefix = fileInfo.baseName() + '_';
81
QString suffix = '.' + fileInfo.completeSuffix();
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();
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();
95
if (srcSize == dstSize && contentsAreIdentical(src, dst, authWindow)) {
96
// Already imported, skip it
97
KIO::Job* job = KIO::file_delete(src, KIO::HideProgressInfo);
99
job->ui()->setWindow(authWindow);
104
result = RenamedUnderNewName;
106
dst.setFileName(prefix + QString::number(count) + suffix);
111
KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo);
112
if (!KIO::NetAccess::synchronousRun(job, authWindow)) {
113
result = RenameFailed;
43
bool contentsAreIdentical(const KUrl& url1, const KUrl& url2, QWidget* authWindow)
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;
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;
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);
65
if (url1Array != url2Array) {
69
if (file1.atEnd() && file2.atEnd()) {
72
kWarning() << "One file ended before the other";
77
RenameResult rename(const KUrl& src, const KUrl& dst_, QWidget* authWindow)
80
RenameResult result = RenamedOK;
83
QFileInfo fileInfo(dst.fileName());
84
QString prefix = fileInfo.baseName() + '_';
85
QString suffix = '.' + fileInfo.completeSuffix();
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();
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();
99
if (srcSize == dstSize && contentsAreIdentical(src, dst, authWindow)) {
100
// Already imported, skip it
101
KIO::Job* job = KIO::file_delete(src, KIO::HideProgressInfo);
103
job->ui()->setWindow(authWindow);
108
result = RenamedUnderNewName;
110
dst.setFileName(prefix + QString::number(count) + suffix);
115
KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo);
116
if (!KIO::NetAccess::synchronousRun(job, authWindow)) {
117
result = RenameFailed;