Changeset View
Changeset View
Standalone View
Standalone View
importer/fileutils.cpp
Show All 19 Lines | |||||
20 | */ | 20 | */ | ||
21 | // Self | 21 | // Self | ||
22 | #include "fileutils.h" | 22 | #include "fileutils.h" | ||
23 | 23 | | |||
24 | // Qt | 24 | // Qt | ||
25 | #include <QFile> | 25 | #include <QFile> | ||
26 | #include <QFileInfo> | 26 | #include <QFileInfo> | ||
27 | #include <QUrl> | 27 | #include <QUrl> | ||
28 | #include <QBuffer> | ||||
29 | #include <QScopedPointer> | ||||
28 | 30 | | |||
29 | // KDE | 31 | // KDE | ||
30 | #include <QDebug> | 32 | #include <QDebug> | ||
31 | #include <KFileItem> | 33 | #include <KFileItem> | ||
32 | #include <KIO/CopyJob> | 34 | #include <KIO/CopyJob> | ||
33 | #include <KIO/Job> | 35 | #include <KIO/Job> | ||
34 | #include <kio/jobclasses.h> | 36 | #include <kio/jobclasses.h> | ||
35 | #include <KIO/JobUiDelegate> | 37 | #include <KIO/JobUiDelegate> | ||
36 | #include <KJobWidgets> | 38 | #include <KJobWidgets> | ||
37 | 39 | | |||
38 | namespace Gwenview | 40 | namespace Gwenview | ||
39 | { | 41 | { | ||
40 | namespace FileUtils | 42 | namespace FileUtils | ||
41 | { | 43 | { | ||
42 | 44 | | |||
43 | bool contentsAreIdentical(const QUrl& url1, const QUrl& url2, QWidget* authWindow) | 45 | bool contentsAreIdentical(const QUrl& url1, const QUrl& url2, QWidget* authWindow) | ||
44 | { | 46 | { | ||
45 | // FIXME: Support remote urls | 47 | KIO::StatJob *statJob; | ||
46 | KIO::StatJob *statJob = KIO::mostLocalUrl(url1); | 48 | KIO::StoredTransferJob *fileJob; | ||
49 | QScopedPointer<QIODevice> file1, file2; | ||||
50 | QByteArray file1Bytes, file2Bytes; | ||||
51 | | ||||
52 | if (url1.isLocalFile()) { | ||||
53 | statJob = KIO::mostLocalUrl(url1); | ||||
47 | KJobWidgets::setWindow(statJob, authWindow); | 54 | KJobWidgets::setWindow(statJob, authWindow); | ||
48 | if (!statJob->exec()) { | 55 | if (!statJob->exec()) { | ||
49 | qWarning() << "Unable to stat" << url1; | 56 | qWarning() << "Unable to stat" << url1; | ||
50 | return false; | 57 | return false; | ||
51 | } | 58 | } | ||
52 | QFile file1(statJob->mostLocalUrl().toLocalFile()); | 59 | file1.reset(new QFile(statJob->mostLocalUrl().toLocalFile())); | ||
53 | if (!file1.open(QIODevice::ReadOnly)) { | 60 | } else { // file1 is remote | ||
61 | fileJob = KIO::storedGet(url1, KIO::NoReload ,KIO::HideProgressInfo); | ||||
62 | KJobWidgets::setWindow(fileJob, authWindow); | ||||
ngraham: uncomment or remove | |||||
63 | if (!fileJob->exec()) { | ||||
64 | qWarning() << "Can't read" << url1; | ||||
65 | return false; | ||||
66 | } | ||||
67 | file1Bytes = QByteArray(fileJob->data()); | ||||
68 | file1.reset(new QBuffer(&file1Bytes)); | ||||
69 | } | ||||
70 | if (!file1->open(QIODevice::ReadOnly)) { | ||||
54 | // Can't read url1, assume it's different from url2 | 71 | // Can't read url1, assume it's different from url2 | ||
55 | qWarning() << "Can't read" << url1; | 72 | qWarning() << "Can't read" << url1; | ||
56 | return false; | 73 | return false; | ||
57 | } | 74 | } | ||
58 | 75 | | |||
76 | if (url2.isLocalFile()) { | ||||
59 | statJob = KIO::mostLocalUrl(url2); | 77 | statJob = KIO::mostLocalUrl(url2); | ||
ngraham: uncomment or remove | |||||
60 | KJobWidgets::setWindow(statJob, authWindow); | 78 | KJobWidgets::setWindow(statJob, authWindow); | ||
61 | if (!statJob->exec()) { | 79 | if (!statJob->exec()) { | ||
62 | qWarning() << "Unable to stat" << url2; | 80 | qWarning() << "Unable to stat" << url2; | ||
63 | return false; | 81 | return false; | ||
64 | } | 82 | } | ||
65 | 83 | file2.reset(new QFile(statJob->mostLocalUrl().toLocalFile())); | |||
66 | QFile file2(statJob->mostLocalUrl().toLocalFile()); | 84 | } else { // file2 is remote | ||
67 | if (!file2.open(QIODevice::ReadOnly)) { | 85 | fileJob = KIO::storedGet(url2, KIO::NoReload, KIO::HideProgressInfo); | ||
86 | KJobWidgets::setWindow(fileJob, authWindow); | ||||
ngraham: uncomment or remove | |||||
87 | if (!fileJob->exec()) { | ||||
ngraham: misplaced comma | |||||
bdevries: Wow, nice catch! | |||||
88 | qWarning() << "Can't read" << url2; | ||||
89 | return false; | ||||
90 | } | ||||
91 | file2Bytes = QByteArray(fileJob->data()); | ||||
92 | file2.reset(new QBuffer(&file2Bytes)); | ||||
93 | } | ||||
94 | if (!file2->open(QIODevice::ReadOnly)) { | ||||
68 | // Can't read url2, assume it's different from url1 | 95 | // Can't read url2, assume it's different from url1 | ||
69 | qWarning() << "Can't read" << url2; | 96 | qWarning() << "Can't read" << url2; | ||
70 | return false; | 97 | return false; | ||
71 | } | 98 | } | ||
72 | 99 | | |||
73 | const int CHUNK_SIZE = 4096; | 100 | const int CHUNK_SIZE = 4096; | ||
74 | while (!file1.atEnd() && !file2.atEnd()) { | 101 | while (!file1->atEnd() && !file2->atEnd()) { | ||
75 | QByteArray url1Array = file1.read(CHUNK_SIZE); | 102 | QByteArray url1Array = file1->read(CHUNK_SIZE); | ||
76 | QByteArray url2Array = file2.read(CHUNK_SIZE); | 103 | QByteArray url2Array = file2->read(CHUNK_SIZE); | ||
77 | 104 | | |||
78 | if (url1Array != url2Array) { | 105 | if (url1Array != url2Array) { | ||
79 | return false; | 106 | return false; | ||
80 | } | 107 | } | ||
81 | } | 108 | } | ||
82 | if (file1.atEnd() && file2.atEnd()) { | 109 | if (file1->atEnd() && file2->atEnd()) { | ||
83 | return true; | 110 | return true; | ||
84 | } else { | 111 | } else { | ||
85 | qWarning() << "One file ended before the other"; | 112 | qWarning() << "One file ended before the other"; | ||
86 | return false; | 113 | return false; | ||
87 | } | 114 | } | ||
88 | } | 115 | } | ||
89 | 116 | | |||
90 | RenameResult rename(const QUrl& src, const QUrl& dst_, QWidget* authWindow) | 117 | RenameResult rename(const QUrl& src, const QUrl& dst_, QWidget* authWindow) | ||
Show All 35 Lines | 139 | while (statJob->exec()) { | |||
126 | dst.setPath(dst.adjusted(QUrl::RemoveFilename).path() + prefix + QString::number(count) + suffix); | 153 | dst.setPath(dst.adjusted(QUrl::RemoveFilename).path() + prefix + QString::number(count) + suffix); | ||
127 | statJob = KIO::stat(dst); | 154 | statJob = KIO::stat(dst); | ||
128 | KJobWidgets::setWindow(statJob, authWindow); | 155 | KJobWidgets::setWindow(statJob, authWindow); | ||
129 | 156 | | |||
130 | ++count; | 157 | ++count; | ||
131 | } | 158 | } | ||
132 | 159 | | |||
133 | // Rename | 160 | // Rename | ||
134 | KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo); | 161 | KIO::Job* job = KIO::moveAs(src, dst, KIO::HideProgressInfo); | ||
ngraham: What's the reason for this change? | |||||
KIO::rename will only do simple renaming. E.g. it will fail if the file needs to be moved across partitions or over the network. bdevries: `KIO::rename` will only do simple renaming. E.g. it will fail if the file needs to be moved… | |||||
135 | KJobWidgets::setWindow(job, authWindow); | 162 | KJobWidgets::setWindow(job, authWindow); | ||
136 | if (!job->exec()) { | 163 | if (!job->exec()) { | ||
137 | result = RenameFailed; | 164 | result = RenameFailed; | ||
138 | } | 165 | } | ||
139 | return result; | 166 | return result; | ||
140 | } | 167 | } | ||
141 | 168 | | |||
142 | } // namespace | 169 | } // namespace | ||
143 | } // namespace | 170 | } // namespace |
uncomment or remove