Changeset View
Changeset View
Standalone View
Standalone View
lgpl/generic/kpColorCollection.cpp
Show All 24 Lines | |||||
25 | // KDE color collection | 25 | // KDE color collection | ||
26 | 26 | | |||
27 | #define DEBUG_KP_COLOR_COLLECTION 0 | 27 | #define DEBUG_KP_COLOR_COLLECTION 0 | ||
28 | 28 | | |||
29 | #include "kpColorCollection.h" | 29 | #include "kpColorCollection.h" | ||
30 | 30 | | |||
31 | #include "kpUrlFormatter.h" | 31 | #include "kpUrlFormatter.h" | ||
32 | 32 | | |||
33 | #include <kio/netaccess.h> // kdelibs4support | 33 | #include <KJobWidgets> | ||
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> | ||
38 | 39 | | |||
39 | #include <QDir> | 40 | #include <QDir> | ||
40 | #include <QFile> | 41 | #include <QFile> | ||
41 | #include <QSaveFile> | 42 | #include <QSaveFile> | ||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | 119 | KMessageBox::sorry (parent, | |||
119 | i18n ("Could not open color palette \"%1\".", | 120 | i18n ("Could not open color palette \"%1\".", | ||
120 | kpUrlFormatter::PrettyFilename (url))); | 121 | kpUrlFormatter::PrettyFilename (url))); | ||
121 | } | 122 | } | ||
122 | 123 | | |||
123 | // TODO: Set d->editable? | 124 | // TODO: Set d->editable? | ||
124 | bool | 125 | bool | ||
125 | kpColorCollection::open(const QUrl &url, QWidget *parent) | 126 | kpColorCollection::open(const QUrl &url, QWidget *parent) | ||
126 | { | 127 | { | ||
127 | QString tempPaletteFilePath; | 128 | if (url.isEmpty()) { | ||
128 | if (url.isEmpty () || !KIO::NetAccess::download (url, tempPaletteFilePath, parent)) | | |||
129 | { | | |||
130 | #if DEBUG_KP_COLOR_COLLECTION | | |||
131 | qCDebug(kpLogColorCollection) << "\tcould not download"; | | |||
132 | #endif | | |||
133 | ::CouldNotOpenDialog (url, parent); | | |||
134 | return false; | 129 | return false; | ||
135 | } | 130 | } | ||
136 | 131 | | |||
137 | // sync: remember to "KIO::NetAccess::removeTempFile (tempPaletteFilePath)" in all exit paths | 132 | KIO::StoredTransferJob *job = KIO::storedGet (url); | ||
133 | KJobWidgets::setWindow (job, parent); | ||||
138 | 134 | | |||
139 | QFile paletteFile(tempPaletteFilePath); | 135 | if (!job->exec ()) | ||
140 | if (!paletteFile.exists() || | | |||
141 | !paletteFile.open(QIODevice::ReadOnly)) | | |||
142 | { | 136 | { | ||
143 | #if DEBUG_KP_COLOR_COLLECTION | 137 | #if DEBUG_KP_COLOR_COLLECTION | ||
144 | qCDebug(kpLogColorCollection) << "\tcould not open qfile"; | 138 | qCDebug(kpLogColorCollection) << "\tcould not download"; | ||
145 | #endif | 139 | #endif | ||
146 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
147 | ::CouldNotOpenDialog (url, parent); | 140 | ::CouldNotOpenDialog (url, parent); | ||
148 | return false; | 141 | return false; | ||
149 | } | 142 | } | ||
150 | 143 | | |||
144 | const QByteArray &data = job->data(); | ||||
145 | QTextStream stream(data); | ||||
146 | | ||||
151 | // Read first line | 147 | // Read first line | ||
152 | // Expected "GIMP Palette" | 148 | // Expected "GIMP Palette" | ||
153 | QString line = QString::fromLocal8Bit(paletteFile.readLine()); | 149 | QString line = stream.readLine(); | ||
154 | if (line.indexOf(QLatin1String(" Palette")) == -1) | 150 | if (line.indexOf(QLatin1String(" Palette")) == -1) | ||
155 | { | 151 | { | ||
156 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
157 | KMessageBox::sorry (parent, | 152 | KMessageBox::sorry (parent, | ||
158 | i18n ("Could not open color palette \"%1\" - unsupported format.\n" | 153 | i18n ("Could not open color palette \"%1\" - unsupported format.\n" | ||
159 | "The file may be corrupt.", | 154 | "The file may be corrupt.", | ||
160 | kpUrlFormatter::PrettyFilename (url))); | 155 | kpUrlFormatter::PrettyFilename (url))); | ||
161 | return false; | 156 | return false; | ||
162 | } | 157 | } | ||
163 | 158 | | |||
164 | QList <ColorNode> newColorList; | 159 | QList <ColorNode> newColorList; | ||
165 | QString newDesc; | 160 | QString newDesc; | ||
166 | 161 | | |||
167 | while( !paletteFile.atEnd() ) | 162 | while( !stream.atEnd() ) | ||
168 | { | 163 | { | ||
169 | line = QString::fromLocal8Bit(paletteFile.readLine()); | 164 | line = stream.readLine(); | ||
170 | if (line[0] == '#') | 165 | if (line[0] == '#') | ||
171 | { | 166 | { | ||
172 | // This is a comment line | 167 | // This is a comment line | ||
173 | line = line.mid(1); // Strip '#' | 168 | line = line.mid(1); // Strip '#' | ||
174 | line = line.trimmed(); // Strip remaining white space.. | 169 | line = line.trimmed(); // Strip remaining white space.. | ||
175 | if (!line.isEmpty()) | 170 | if (!line.isEmpty()) | ||
176 | { | 171 | { | ||
177 | newDesc += line+'\n'; // Add comment to description | 172 | newDesc += line+'\n'; // Add comment to description | ||
Show All 16 Lines | 176 | { | |||
194 | } | 189 | } | ||
195 | } | 190 | } | ||
196 | } | 191 | } | ||
197 | 192 | | |||
198 | d->colorList = newColorList; | 193 | d->colorList = newColorList; | ||
199 | d->name.clear (); | 194 | d->name.clear (); | ||
200 | d->desc = newDesc; | 195 | d->desc = newDesc; | ||
201 | 196 | | |||
202 | KIO::NetAccess::removeTempFile (tempPaletteFilePath); | | |||
203 | return true; | 197 | return true; | ||
204 | } | 198 | } | ||
205 | 199 | | |||
206 | static void CouldNotOpenKDEDialog (const QString &name, QWidget *parent) | 200 | static void CouldNotOpenKDEDialog (const QString &name, QWidget *parent) | ||
207 | { | 201 | { | ||
208 | KMessageBox::sorry (parent, | 202 | KMessageBox::sorry (parent, | ||
209 | i18n ("Could not open KDE color palette \"%1\".", name)); | 203 | i18n ("Could not open KDE color palette \"%1\".", name)); | ||
210 | } | 204 | } | ||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Line(s) | 269 | { | |||
281 | node.color.getRgb(&r, &g, &b); | 275 | node.color.getRgb(&r, &g, &b); | ||
282 | str << r << " " << g << " " << b << " " << node.name << "\n"; | 276 | str << r << " " << g << " " << b << " " << node.name << "\n"; | ||
283 | } | 277 | } | ||
284 | 278 | | |||
285 | str.flush(); | 279 | str.flush(); | ||
286 | } | 280 | } | ||
287 | 281 | | |||
288 | bool | 282 | bool | ||
289 | kpColorCollection::saveAs(const QUrl &url, bool showOverwritePrompt, | 283 | kpColorCollection::saveAs(const QUrl &url, QWidget *parent) const | ||
290 | QWidget *parent) const | | |||
291 | { | | |||
292 | if (showOverwritePrompt && | | |||
293 | KIO::NetAccess::exists (url, KIO::NetAccess::DestinationSide/*write*/, parent)) | | |||
294 | { | 284 | { | ||
295 | int result = KMessageBox::warningContinueCancel (parent, | | |||
296 | i18n ("A color palette called \"%1\" already exists.\n" | | |||
297 | "Do you want to overwrite it?", | | |||
298 | kpUrlFormatter::PrettyFilename (url)), | | |||
299 | QString (), | | |||
300 | KStandardGuiItem::overwrite ()); | | |||
301 | if (result != KMessageBox::Continue) | | |||
302 | return false; | | |||
303 | } | | |||
304 | | ||||
305 | if (url.isLocalFile ()) | 285 | if (url.isLocalFile ()) | ||
306 | { | 286 | { | ||
307 | const QString filename = url.toLocalFile (); | 287 | const QString filename = url.toLocalFile (); | ||
308 | 288 | | |||
309 | // sync: All failure exit paths _must_ call QSaveFile::cancelWriting() or | 289 | // sync: All failure exit paths _must_ call QSaveFile::cancelWriting() or | ||
310 | // else, the QSaveFile destructor will overwrite the file, | 290 | // else, the QSaveFile destructor will overwrite the file, | ||
311 | // <filename>, despite the failure. | 291 | // <filename>, despite the failure. | ||
312 | QSaveFile atomicFileWriter (filename); | 292 | QSaveFile atomicFileWriter (filename); | ||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Line(s) | 348 | #endif | |||
374 | #if DEBUG_KP_COLOR_COLLECTION | 354 | #if DEBUG_KP_COLOR_COLLECTION | ||
375 | qCDebug(kpLogColorCollection) << "\treturning false because could not close"; | 355 | qCDebug(kpLogColorCollection) << "\treturning false because could not close"; | ||
376 | #endif | 356 | #endif | ||
377 | ::CouldNotSaveDialog (url, parent); | 357 | ::CouldNotSaveDialog (url, parent); | ||
378 | return false; | 358 | return false; | ||
379 | } | 359 | } | ||
380 | 360 | | |||
381 | // Copy local temporary file to overwrite remote. | 361 | // Copy local temporary file to overwrite remote. | ||
382 | // TODO: No one seems to know how to do this atomically | 362 | KIO::FileCopyJob *job = KIO::file_copy (QUrl::fromLocalFile (tempFileName), | ||
383 | // [http://lists.kde.org/?l=kde-core-devel&m=117845162728484&w=2]. | 363 | url, | ||
384 | // At least, fish:// (ssh) is definitely not atomic. | 364 | -1, | ||
385 | if (!KIO::NetAccess::upload (tempFileName, url, parent)) | 365 | KIO::Overwrite); | ||
366 | KJobWidgets::setWindow (job, parent); | ||||
367 | if (!job->exec ()) | ||||
386 | { | 368 | { | ||
387 | #if DEBUG_KP_COLOR_COLLECTION | 369 | #if DEBUG_KP_COLOR_COLLECTION | ||
388 | qCDebug(kpLogColorCollection) << "\treturning false because could not upload"; | 370 | qCDebug(kpLogColorCollection) << "\treturning false because could not upload"; | ||
389 | #endif | 371 | #endif | ||
390 | ::CouldNotSaveDialog (url, parent); | 372 | ::CouldNotSaveDialog (url, parent); | ||
391 | return false; | 373 | return false; | ||
392 | } | 374 | } | ||
393 | } | 375 | } | ||
394 | 376 | | |||
395 | d->name.clear (); | 377 | d->name.clear (); | ||
396 | return true; | 378 | return true; | ||
397 | } | 379 | } | ||
398 | 380 | | |||
399 | bool | 381 | bool | ||
400 | kpColorCollection::saveKDE(QWidget *parent) const | 382 | kpColorCollection::saveKDE(QWidget *parent) const | ||
401 | { | 383 | { | ||
402 | const QString name = d->name; | 384 | const QString name = d->name; | ||
403 | QString filename = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) | 385 | QString filename = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) | ||
404 | + "colors/" + name; | 386 | + "colors/" + name; | ||
405 | const bool ret = saveAs (QUrl::fromLocalFile (filename), false/*no overwite prompt*/, parent); | 387 | const bool ret = saveAs (QUrl::fromLocalFile (filename), parent); | ||
406 | // (d->name is wiped by saveAs()). | 388 | // (d->name is wiped by saveAs()). | ||
407 | d->name = name; | 389 | d->name = name; | ||
408 | return ret; | 390 | return ret; | ||
409 | } | 391 | } | ||
410 | 392 | | |||
411 | QString kpColorCollection::description() const | 393 | QString kpColorCollection::description() const | ||
412 | { | 394 | { | ||
413 | return d->desc; | 395 | return d->desc; | ||
▲ Show 20 Lines • Show All 124 Lines • Show Last 20 Lines |