Changeset View
Changeset View
Standalone View
Standalone View
lgpl/generic/kpColorCollection.cpp
Context not available. | |||||
30 | 30 | | |||
---|---|---|---|---|---|
31 | #include "kpUrlFormatter.h" | 31 | #include "kpUrlFormatter.h" | ||
32 | 32 | | |||
33 | #include <kio/netaccess.h> // kdelibs4support | 33 | #include <KIO/StatJob> | ||
34 | #include <KIO/StoredTransferJob> | ||||
34 | #include <KLocalizedString> | 35 | #include <KLocalizedString> | ||
35 | #include <KMessageBox> | 36 | #include <KMessageBox> | ||
36 | #include "kpLogCategories.h" | 37 | #include "kpLogCategories.h" | ||
37 | #include <kstringhandler.h> | 38 | #include <kstringhandler.h> | ||
39 | #include <KJobWidgets> | ||||
38 | 40 | | |||
41 | #include <QBuffer> | ||||
39 | #include <QDir> | 42 | #include <QDir> | ||
40 | #include <QFile> | 43 | #include <QFile> | ||
41 | #include <QSaveFile> | 44 | #include <QSaveFile> | ||
Context not available. | |||||
122 | bool | 125 | bool | ||
123 | kpColorCollection::open(const QUrl &url, QWidget *parent) | 126 | kpColorCollection::open(const QUrl &url, QWidget *parent) | ||
124 | { | 127 | { | ||
125 | QString tempPaletteFilePath; | 128 | KIO::StoredTransferJob *downloadJob = KIO::storedGet(url); | ||
126 | if (url.isEmpty () || !KIO::NetAccess::download (url, tempPaletteFilePath, parent)) | 129 | KJobWidgets::setWindow(downloadJob, parent); | ||
130 | | ||||
131 | if (url.isEmpty () || !downloadJob->exec()) | ||||
127 | { | 132 | { | ||
128 | #if DEBUG_KP_COLOR_COLLECTION | 133 | #if DEBUG_KP_COLOR_COLLECTION | ||
129 | qCDebug(kpLogMisc) << "\tcould not download"; | 134 | qCDebug(kpLogMisc) << "\tcould not download"; | ||
Context not available. | |||||
132 | return false; | 137 | return false; | ||
133 | } | 138 | } | ||
134 | 139 | | |||
135 | // sync: remember to "KIO::NetAccess::removeTempFile (tempPaletteFilePath)" in all exit paths | 140 | QBuffer paletteBuffer; | ||
136 | 141 | paletteBuffer.setData(downloadJob->data()); | |||
137 | QFile paletteFile(tempPaletteFilePath); | 142 | if (paletteBuffer.data().isEmpty() || | ||
138 | if (!paletteFile.exists() || | 143 | !paletteBuffer.open(QIODevice::ReadOnly)) | ||
139 | !paletteFile.open(QIODevice::ReadOnly)) | | |||
140 | { | 144 | { | ||
141 | #if DEBUG_KP_COLOR_COLLECTION | 145 | #if DEBUG_KP_COLOR_COLLECTION | ||
142 | qCDebug(kpLogMisc) << "\tcould not open qfile"; | 146 | qCDebug(kpLogMisc) << "\tcould not open qfile"; | ||
143 | #endif | 147 | #endif | ||
144 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
145 | ::CouldNotOpenDialog (url, parent); | 148 | ::CouldNotOpenDialog (url, parent); | ||
146 | return false; | 149 | return false; | ||
147 | } | 150 | } | ||
148 | 151 | | |||
149 | // Read first line | 152 | // Read first line | ||
150 | // Expected "GIMP Palette" | 153 | // Expected "GIMP Palette" | ||
151 | QString line = QString::fromLocal8Bit(paletteFile.readLine()); | 154 | QString line = QString::fromLocal8Bit(paletteBuffer.readLine()); | ||
152 | if (line.indexOf(QLatin1String(" Palette")) == -1) | 155 | if (line.indexOf(QLatin1String(" Palette")) == -1) | ||
153 | { | 156 | { | ||
154 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
155 | KMessageBox::sorry (parent, | 157 | KMessageBox::sorry (parent, | ||
156 | i18n ("Could not open color palette \"%1\" - unsupported format.\n" | 158 | i18n ("Could not open color palette \"%1\" - unsupported format.\n" | ||
157 | "The file may be corrupt.", | 159 | "The file may be corrupt.", | ||
Context not available. | |||||
162 | QList <ColorNode> newColorList; | 164 | QList <ColorNode> newColorList; | ||
163 | QString newDesc; | 165 | QString newDesc; | ||
164 | 166 | | |||
165 | while( !paletteFile.atEnd() ) | 167 | while( !paletteBuffer.atEnd() ) | ||
166 | { | 168 | { | ||
167 | line = QString::fromLocal8Bit(paletteFile.readLine()); | 169 | line = QString::fromLocal8Bit(paletteBuffer.readLine()); | ||
168 | if (line[0] == '#') | 170 | if (line[0] == '#') | ||
169 | { | 171 | { | ||
170 | // This is a comment line | 172 | // This is a comment line | ||
Context not available. | |||||
197 | d->name.clear (); | 199 | d->name.clear (); | ||
198 | d->desc = newDesc; | 200 | d->desc = newDesc; | ||
199 | 201 | | |||
200 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
201 | return true; | 202 | return true; | ||
202 | } | 203 | } | ||
203 | 204 | | |||
Context not available. | |||||
287 | kpColorCollection::saveAs(const QUrl &url, bool showOverwritePrompt, | 288 | kpColorCollection::saveAs(const QUrl &url, bool showOverwritePrompt, | ||
288 | QWidget *parent) const | 289 | QWidget *parent) const | ||
289 | { | 290 | { | ||
290 | if (showOverwritePrompt && | 291 | if (showOverwritePrompt) { | ||
291 | KIO::NetAccess::exists (url, KIO::NetAccess::DestinationSide/*write*/, parent)) | 292 | KIO::StatJob *statJob = KIO::stat(url, KIO::StatJob::DestinationSide, 0, KIO::HideProgressInfo); | ||
292 | { | 293 | KJobWidgets::setWindow(statJob, parent); | ||
293 | int result = KMessageBox::warningContinueCancel (parent, | 294 | if (statJob->exec()) { | ||
294 | i18n ("A color palette called \"%1\" already exists.\n" | 295 | int result = KMessageBox::warningContinueCancel (parent, | ||
295 | "Do you want to overwrite it?", | 296 | i18n ("A color palette called \"%1\" already exists.\n" | ||
296 | kpUrlFormatter::PrettyFilename (url)), | 297 | "Do you want to overwrite it?", | ||
297 | QString (), | 298 | kpUrlFormatter::PrettyFilename (url)), | ||
298 | KStandardGuiItem::overwrite ()); | 299 | QString (), | ||
299 | if (result != KMessageBox::Continue) | 300 | KStandardGuiItem::overwrite ()); | ||
300 | return false; | 301 | if (result != KMessageBox::Continue) | ||
301 | } | 302 | return false; | ||
303 | } | ||||
304 | | ||||
305 | } | ||||
302 | 306 | | |||
303 | if (url.isLocalFile ()) | 307 | if (url.isLocalFile ()) | ||
304 | { | 308 | { | ||
Context not available. | |||||
345 | { | 349 | { | ||
346 | // Create temporary file that is deleted when the variable goes | 350 | // Create temporary file that is deleted when the variable goes | ||
347 | // out of scope. | 351 | // out of scope. | ||
348 | QTemporaryFile tempFile; | 352 | QBuffer tempFile; | ||
349 | if (!tempFile.open ()) | 353 | if (!tempFile.open (QIODevice::WriteOnly)) | ||
350 | { | 354 | { | ||
351 | #if DEBUG_KP_COLOR_COLLECTION | 355 | #if DEBUG_KP_COLOR_COLLECTION | ||
352 | qCDebug(kpLogMisc) << "\treturning false because could not open tempFile"; | 356 | qCDebug(kpLogMisc) << "\treturning false because could not open tempFile"; | ||
Context not available. | |||||
360 | 364 | | |||
361 | // Collect name of temporary file now, as QTemporaryFile::fileName() | 365 | // Collect name of temporary file now, as QTemporaryFile::fileName() | ||
362 | // stops working after close() is called. | 366 | // stops working after close() is called. | ||
363 | const QString tempFileName = tempFile.fileName (); | | |||
364 | #if DEBUG_KP_COLOR_COLLECTION | | |||
365 | qCDebug(kpLogMisc) << "\ttempFileName='" << tempFileName << "'"; | | |||
366 | #endif | | |||
367 | Q_ASSERT (!tempFileName.isEmpty ()); | | |||
368 | 367 | | |||
369 | tempFile.close (); | 368 | tempFile.close (); | ||
370 | if (tempFile.error () != QFile::NoError) | 369 | if (tempFile.errorString().isEmpty()) | ||
371 | { | 370 | { | ||
372 | #if DEBUG_KP_COLOR_COLLECTION | 371 | #if DEBUG_KP_COLOR_COLLECTION | ||
373 | qCDebug(kpLogMisc) << "\treturning false because could not close"; | 372 | qCDebug(kpLogMisc) << "\treturning false because could not close"; | ||
Context not available. | |||||
377 | } | 376 | } | ||
378 | 377 | | |||
379 | // Copy local temporary file to overwrite remote. | 378 | // Copy local temporary file to overwrite remote. | ||
380 | // TODO: No one seems to know how to do this atomically | 379 | // TODO: To do it more atomically, instead of doing a separate stat job above, | ||
381 | // [http://lists.kde.org/?l=kde-core-devel&m=117845162728484&w=2]. | 380 | // don't pass KIO::Overwrite if we should prompt in case of overwriting | ||
382 | // At least, fish:// (ssh) is definitely not atomic. | 381 | // and check the return value, and re-do the storedPut() if the user wants to overwrite | ||
383 | if (!KIO::NetAccess::upload (tempFileName, url, parent)) | 382 | // TODO: Skip the temporary file, pass a QBuffer to ::SaveToFile() instead, | ||
383 | // or make it just return a QByteArray we can pass directly to KIO | ||||
384 | KIO::StoredTransferJob *uploadJob = KIO::storedPut(tempFile.data(), url, -1, KIO::Overwrite); | ||||
385 | KJobWidgets::setWindow(uploadJob, parent); | ||||
386 | if (!uploadJob->exec()) | ||||
384 | { | 387 | { | ||
385 | #if DEBUG_KP_COLOR_COLLECTION | 388 | #if DEBUG_KP_COLOR_COLLECTION | ||
386 | qCDebug(kpLogMisc) << "\treturning false because could not upload"; | 389 | qCDebug(kpLogMisc) << "\treturning false because could not upload"; | ||
Context not available. |