Changeset View
Changeset View
Standalone View
Standalone View
plugins/kimgalleryplugin/imgalleryplugin.cpp
Show All 26 Lines | |||||
27 | #include <qtextcodec.h> | 27 | #include <qtextcodec.h> | ||
28 | #include <QApplication> | 28 | #include <QApplication> | ||
29 | 29 | | |||
30 | 30 | | |||
31 | #include <kglobal.h> | 31 | #include <kglobal.h> | ||
32 | #include <KLocalizedString> | 32 | #include <KLocalizedString> | ||
33 | #include <kcharsets.h> | 33 | #include <kcharsets.h> | ||
34 | #include <kmessagebox.h> | 34 | #include <kmessagebox.h> | ||
35 | #include <kurl.h> | | |||
36 | #include <kimageio.h> | 35 | #include <kimageio.h> | ||
37 | 36 | | |||
38 | #include <kdebug.h> | 37 | #include <kdebug.h> | ||
39 | #include <kpluginfactory.h> | 38 | #include <kpluginfactory.h> | ||
40 | #include <kio/netaccess.h> | 39 | #include <kio/netaccess.h> | ||
41 | #include <QPushButton> | 40 | #include <QPushButton> | ||
42 | #include <ktoolinvocation.h> | 41 | #include <ktoolinvocation.h> | ||
43 | #include <QProgressDialog> | 42 | #include <QProgressDialog> | ||
44 | #include <qtextdocument.h> | 43 | #include <qtextdocument.h> | ||
45 | #include <kactioncollection.h> | 44 | #include <kactioncollection.h> | ||
46 | #include <kparts/part.h> | 45 | #include <kparts/part.h> | ||
47 | 46 | | |||
48 | #include "imgallerydialog.h" | 47 | #include "imgallerydialog.h" | ||
49 | #include "imgalleryplugin.h" | 48 | #include "imgalleryplugin.h" | ||
50 | 49 | | |||
51 | //KDELibs4Support | 50 | //KDELibs4Support | ||
52 | #include <kdeversion.h> | 51 | #include <kdeversion.h> | ||
53 | 52 | | |||
54 | K_PLUGIN_FACTORY(KImGalleryPluginFactory, registerPlugin<KImGalleryPlugin>();) | 53 | K_PLUGIN_FACTORY(KImGalleryPluginFactory, registerPlugin<KImGalleryPlugin>();) | ||
55 | 54 | | |||
55 | static QString directory(const QUrl &url) { | ||||
dfaure: add static in front
(add space before '{' ?) | |||||
56 | return url.adjusted(QUrl::StripTrailingSlash).adjusted(QUrl::RemoveFilename).toLocalFile(); | ||||
You could combine both flags in a single adjusted() call, no? Are you sure about the FullyDecoded? That usually gives trouble. dfaure: You could combine both flags in a single adjusted() call, no?
Are you sure about the… | |||||
According to the documentation for KUrl::directory, the two calls to QUrl::adjusted shouldn't be combined. Indeed, the results are different: QUrl("file://abc/xyz/").adjusted(QUrl::RemoveTrailingSlash).adjusted(QUrl::RemoveFilename).toDisplayString() gives file://xyz, while QUrl("file://abc/xyz/").adjusted(QUrl::RemoveTrailingSlash|QUrl::RemoveFilename).toDisplayString() gives file://xyz/abc. stefanocrocco: According to the documentation for [`KUrl::directory`](https://api.kde. | |||||
dfaure: Ah OK, thanks for the explanation. | |||||
57 | } | ||||
58 | | ||||
56 | KImGalleryPlugin::KImGalleryPlugin(QObject *parent, const QVariantList &) | 59 | KImGalleryPlugin::KImGalleryPlugin(QObject *parent, const QVariantList &) | ||
57 | : KParts::Plugin(parent), m_commentMap(0) | 60 | : KParts::Plugin(parent), m_commentMap(0) | ||
58 | { | 61 | { | ||
59 | QAction *a = actionCollection()->addAction(QStringLiteral("create_img_gallery")); | 62 | QAction *a = actionCollection()->addAction(QStringLiteral("create_img_gallery")); | ||
60 | a->setText(i18n("&Create Image Gallery...")); | 63 | a->setText(i18n("&Create Image Gallery...")); | ||
61 | a->setIcon(QIcon::fromTheme(QStringLiteral("imagegallery"))); | 64 | a->setIcon(QIcon::fromTheme(QStringLiteral("imagegallery"))); | ||
62 | actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL + Qt::Key_I)); | 65 | actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL + Qt::Key_I)); | ||
63 | connect(a, SIGNAL(triggered()), this, SLOT(slotExecute())); | 66 | connect(a, SIGNAL(triggered()), this, SLOT(slotExecute())); | ||
Show All 19 Lines | 70 | { | |||
83 | if (m_configDlg->exec() == QDialog::Accepted) { | 86 | if (m_configDlg->exec() == QDialog::Accepted) { | ||
84 | kDebug(90170) << "dialog is ok"; | 87 | kDebug(90170) << "dialog is ok"; | ||
85 | m_configDlg->writeConfig(); | 88 | m_configDlg->writeConfig(); | ||
86 | m_copyFiles = m_configDlg->copyOriginalFiles(); | 89 | m_copyFiles = m_configDlg->copyOriginalFiles(); | ||
87 | m_recurseSubDirectories = m_configDlg->recurseSubDirectories(); | 90 | m_recurseSubDirectories = m_configDlg->recurseSubDirectories(); | ||
88 | m_useCommentFile = m_configDlg->useCommentFile(); | 91 | m_useCommentFile = m_configDlg->useCommentFile(); | ||
89 | m_imagesPerRow = m_configDlg->getImagesPerRow(); | 92 | m_imagesPerRow = m_configDlg->getImagesPerRow(); | ||
90 | 93 | | |||
91 | KUrl url(m_configDlg->getImageUrl()); | 94 | QUrl url(m_configDlg->getImageUrl()); | ||
92 | if (!url.isEmpty() && url.isValid()) { | 95 | if (!url.isEmpty() && url.isValid()) { | ||
93 | m_progressDlg = new QProgressDialog(m_part->widget()); | 96 | m_progressDlg = new QProgressDialog(m_part->widget()); | ||
94 | QObject::connect(m_progressDlg, SIGNAL(canceled()), this, SLOT(slotCancelled())); | 97 | QObject::connect(m_progressDlg, SIGNAL(canceled()), this, SLOT(slotCancelled())); | ||
95 | 98 | | |||
96 | m_progressDlg->setLabelText(i18n("Creating thumbnails")); | 99 | m_progressDlg->setLabelText(i18n("Creating thumbnails")); | ||
97 | QPushButton *button = new QPushButton(m_progressDlg); | 100 | QPushButton *button = new QPushButton(m_progressDlg); | ||
98 | KGuiItem::assign(button, KStandardGuiItem::cancel()); | 101 | KGuiItem::assign(button, KStandardGuiItem::cancel()); | ||
99 | m_progressDlg->setCancelButton(button); | 102 | m_progressDlg->setCancelButton(button); | ||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 165 | { | |||
166 | if (imageFormat == QLatin1String("JPEG")) { | 169 | if (imageFormat == QLatin1String("JPEG")) { | ||
167 | return QStringLiteral(".jpg"); | 170 | return QStringLiteral(".jpg"); | ||
168 | } | 171 | } | ||
169 | Q_ASSERT(false); | 172 | Q_ASSERT(false); | ||
170 | return QString(); | 173 | return QString(); | ||
171 | } | 174 | } | ||
172 | 175 | | |||
173 | void KImGalleryPlugin::createBody(QTextStream &stream, const QString &sourceDirName, const QStringList &subDirList, | 176 | void KImGalleryPlugin::createBody(QTextStream &stream, const QString &sourceDirName, const QStringList &subDirList, | ||
174 | const QDir &imageDir, const KUrl &url, const QString &imageFormat) | 177 | const QDir &imageDir, const QUrl &url, const QString &imageFormat) | ||
175 | { | 178 | { | ||
176 | int numOfImages = imageDir.count(); | 179 | int numOfImages = imageDir.count(); | ||
177 | const QString imgGalleryDir = url.directory(); | 180 | const QString imgGalleryDir = directory(url); | ||
178 | const QString today(KLocale::global()->formatDate(QDate::currentDate())); | 181 | const QString today(KLocale::global()->formatDate(QDate::currentDate())); | ||
179 | 182 | | |||
180 | stream << "<body>\n<h1>" << m_configDlg->getTitle().toHtmlEscaped() << "</h1><p>" << endl; | 183 | stream << "<body>\n<h1>" << m_configDlg->getTitle().toHtmlEscaped() << "</h1><p>" << endl; | ||
181 | stream << i18n("<i>Number of images</i>: %1", numOfImages) << "<br/>" << endl; | 184 | stream << i18n("<i>Number of images</i>: %1", numOfImages) << "<br/>" << endl; | ||
182 | stream << i18n("<i>Created on</i>: %1", today) << "</p>" << endl; | 185 | stream << i18n("<i>Created on</i>: %1", today) << "</p>" << endl; | ||
183 | 186 | | |||
184 | stream << "<hr/>" << endl; | 187 | stream << "<hr/>" << endl; | ||
185 | 188 | | |||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | 237 | #endif | |||
253 | imgIndex++; | 256 | imgIndex++; | ||
254 | } | 257 | } | ||
255 | stream << "</tr>" << endl; | 258 | stream << "</tr>" << endl; | ||
256 | } | 259 | } | ||
257 | //close the HTML | 260 | //close the HTML | ||
258 | stream << "</table>\n</body>\n</html>" << endl; | 261 | stream << "</table>\n</body>\n</html>" << endl; | ||
259 | } | 262 | } | ||
260 | 263 | | |||
261 | bool KImGalleryPlugin::createHtml(const KUrl &url, const QString &sourceDirName, int recursionLevel, const QString &imageFormat) | 264 | bool KImGalleryPlugin::createHtml(const QUrl &url, const QString &sourceDirName, int recursionLevel, const QString &imageFormat) | ||
262 | { | 265 | { | ||
263 | if (m_cancelled) { | 266 | if (m_cancelled) { | ||
264 | return false; | 267 | return false; | ||
265 | } | 268 | } | ||
266 | 269 | | |||
267 | if (!parent() || !parent()->inherits("DolphinPart")) { | 270 | if (!parent() || !parent()->inherits("DolphinPart")) { | ||
268 | return false; | 271 | return false; | ||
269 | } | 272 | } | ||
270 | 273 | | |||
271 | QStringList subDirList; | 274 | QStringList subDirList; | ||
272 | if (m_recurseSubDirectories && (recursionLevel >= 0)) { //recursionLevel == 0 means endless | 275 | if (m_recurseSubDirectories && (recursionLevel >= 0)) { //recursionLevel == 0 means endless | ||
273 | QDir toplevel_dir = QDir(sourceDirName); | 276 | QDir toplevel_dir = QDir(sourceDirName); | ||
274 | toplevel_dir.setFilter(QDir::Dirs | QDir::Readable | QDir::Writable); | 277 | toplevel_dir.setFilter(QDir::Dirs | QDir::Readable | QDir::Writable); | ||
275 | subDirList = toplevel_dir.entryList(); | 278 | subDirList = toplevel_dir.entryList(); | ||
276 | 279 | | |||
277 | for (QStringList::ConstIterator it = subDirList.constBegin(); it != subDirList.constEnd() && !m_cancelled; it++) { | 280 | for (QStringList::ConstIterator it = subDirList.constBegin(); it != subDirList.constEnd() && !m_cancelled; it++) { | ||
278 | const QString currentDir = *it; | 281 | const QString currentDir = *it; | ||
279 | if (currentDir == QLatin1String(".") || currentDir == QLatin1String("..")) { | 282 | if (currentDir == QLatin1String(".") || currentDir == QLatin1String("..")) { | ||
280 | continue; //disregard the "." and ".." directories | 283 | continue; //disregard the "." and ".." directories | ||
281 | } | 284 | } | ||
282 | QDir subDir = QDir(url.directory() + '/' + currentDir); | 285 | QDir subDir = QDir(directory(url) + '/' + currentDir); | ||
clearly directory() must return a local path then, not a fully decoded URL (with file scheme in front). I think you need toLocalFile() in directory(). dfaure: clearly directory() must return a local path then, not a fully decoded URL (with file scheme in… | |||||
283 | if (!subDir.exists()) { | 286 | if (!subDir.exists()) { | ||
284 | subDir.setPath(url.directory()); | 287 | subDir.setPath(directory(url)); | ||
285 | if (!(subDir.mkdir(currentDir/*, false*/))) { | 288 | if (!(subDir.mkdir(currentDir/*, false*/))) { | ||
286 | KMessageBox::sorry(m_part->widget(), i18n("Could not create folder: %1", subDir.path())); | 289 | KMessageBox::sorry(m_part->widget(), i18n("Could not create folder: %1", subDir.path())); | ||
287 | continue; | 290 | continue; | ||
288 | } else { | 291 | } else { | ||
289 | subDir.setPath(url.directory() + '/' + currentDir); | 292 | subDir.setPath(directory(url) + '/' + currentDir); | ||
290 | } | 293 | } | ||
291 | } | 294 | } | ||
292 | if (!createHtml(KUrl(subDir.path() + '/' + url.fileName()), sourceDirName + '/' + currentDir, | 295 | if (!createHtml(QUrl::fromLocalFile(subDir.path() + '/' + url.fileName()), sourceDirName + '/' + currentDir, | ||
293 | recursionLevel > 1 ? recursionLevel - 1 : 0, imageFormat)) { | 296 | recursionLevel > 1 ? recursionLevel - 1 : 0, imageFormat)) { | ||
294 | return false; | 297 | return false; | ||
295 | } | 298 | } | ||
296 | } | 299 | } | ||
297 | } | 300 | } | ||
298 | 301 | | |||
299 | if (m_useCommentFile) { | 302 | if (m_useCommentFile) { | ||
300 | loadCommentFile(); | 303 | loadCommentFile(); | ||
301 | } | 304 | } | ||
302 | 305 | | |||
303 | kDebug(90170) << "sourceDirName: " << sourceDirName; | 306 | kDebug(90170) << "sourceDirName: " << sourceDirName; | ||
304 | //We're interested in only the patterns, so look for the first | | 307 | //We're interested in only the patterns, so look for the first | | ||
305 | //#### perhaps an accessor should be added to KImageIO instead? | 308 | //#### perhaps an accessor should be added to KImageIO instead? | ||
306 | QString filter = KImageIO::pattern(KImageIO::Reading).section('|', 0, 0); | 309 | QString filter = KImageIO::pattern(KImageIO::Reading).section('|', 0, 0); | ||
307 | 310 | | |||
308 | QDir imageDir(sourceDirName, filter.toLatin1(), | 311 | QDir imageDir(sourceDirName, filter.toLatin1(), | ||
309 | QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::Readable); | 312 | QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::Readable); | ||
310 | 313 | | |||
311 | const QString imgGalleryDir = url.directory(); | 314 | const QString imgGalleryDir = directory(url); | ||
312 | kDebug(90170) << "imgGalleryDir: " << imgGalleryDir; | 315 | kDebug(90170) << "imgGalleryDir: " << imgGalleryDir; | ||
313 | 316 | | |||
314 | // Create the "thumbs" subdirectory if necessary | 317 | // Create the "thumbs" subdirectory if necessary | ||
315 | QDir thumb_dir(imgGalleryDir + QLatin1String("/thumbs/")); | 318 | QDir thumb_dir(imgGalleryDir + QLatin1String("/thumbs/")); | ||
316 | if (createDirectory(thumb_dir, imgGalleryDir, QStringLiteral("thumbs")) == false) { | 319 | if (createDirectory(thumb_dir, imgGalleryDir, QStringLiteral("thumbs")) == false) { | ||
317 | return false; | 320 | return false; | ||
318 | } | 321 | } | ||
319 | 322 | | |||
Show All 16 Lines | 335 | if (imageDir.exists() && file.open(QIODevice::WriteOnly)) { | |||
336 | createHead(stream); | 339 | createHead(stream); | ||
337 | createBody(stream, sourceDirName, subDirList, imageDir, url, imageFormat); //ugly | 340 | createBody(stream, sourceDirName, subDirList, imageDir, url, imageFormat); //ugly | ||
338 | 341 | | |||
339 | file.close(); | 342 | file.close(); | ||
340 | 343 | | |||
341 | return !m_cancelled; | 344 | return !m_cancelled; | ||
342 | 345 | | |||
343 | } else { | 346 | } else { | ||
344 | KMessageBox::sorry(m_part->widget(), i18n("Could not open file: %1", url.path(KUrl::AddTrailingSlash))); | 347 | QString path = url.toLocalFile(); | ||
348 | if (!path.endsWith("/")) { | ||||
349 | path += '/'; | ||||
350 | } | ||||
351 | KMessageBox::sorry(m_part->widget(), i18n("Could not open file: %1", path)); | ||||
dfaure: toLocalFile() | |||||
345 | return false; | 352 | return false; | ||
346 | } | 353 | } | ||
347 | } | 354 | } | ||
348 | 355 | | |||
349 | void KImGalleryPlugin::deleteCancelledGallery(const KUrl &url, const QString &sourceDirName, int recursionLevel, const QString &imageFormat) | 356 | void KImGalleryPlugin::deleteCancelledGallery(const QUrl &url, const QString &sourceDirName, int recursionLevel, const QString &imageFormat) | ||
350 | { | 357 | { | ||
351 | if (m_recurseSubDirectories && (recursionLevel >= 0)) { | 358 | if (m_recurseSubDirectories && (recursionLevel >= 0)) { | ||
352 | QStringList subDirList; | 359 | QStringList subDirList; | ||
353 | QDir toplevel_dir = QDir(sourceDirName); | 360 | QDir toplevel_dir = QDir(sourceDirName); | ||
354 | toplevel_dir.setFilter(QDir::Dirs); | 361 | toplevel_dir.setFilter(QDir::Dirs); | ||
355 | subDirList = toplevel_dir.entryList(); | 362 | subDirList = toplevel_dir.entryList(); | ||
356 | 363 | | |||
357 | for (QStringList::ConstIterator it = subDirList.constBegin(); it != subDirList.constEnd(); it++) { | 364 | for (QStringList::ConstIterator it = subDirList.constBegin(); it != subDirList.constEnd(); it++) { | ||
358 | if (*it == QLatin1String(".") || *it == QLatin1String("..") || *it == QLatin1String("thumbs") || (m_copyFiles && *it == QLatin1String("images"))) { | 365 | if (*it == QLatin1String(".") || *it == QLatin1String("..") || *it == QLatin1String("thumbs") || (m_copyFiles && *it == QLatin1String("images"))) { | ||
359 | continue; //disregard the "." and ".." directories | 366 | continue; //disregard the "." and ".." directories | ||
360 | } | 367 | } | ||
361 | deleteCancelledGallery(KUrl(url.directory() + '/' + *it + '/' + url.fileName()), | 368 | deleteCancelledGallery(QUrl::fromLocalFile(directory(url) + '/' + *it + '/' + url.fileName()), | ||
dfaure: QUrl::fromLocalFile here as well | |||||
362 | sourceDirName + '/' + *it, | 369 | sourceDirName + '/' + *it, | ||
363 | recursionLevel > 1 ? recursionLevel - 1 : 0, imageFormat); | 370 | recursionLevel > 1 ? recursionLevel - 1 : 0, imageFormat); | ||
364 | } | 371 | } | ||
365 | } | 372 | } | ||
366 | 373 | | |||
367 | const QString imgGalleryDir = url.directory(); | 374 | const QString imgGalleryDir = directory(url); | ||
368 | QDir thumb_dir(imgGalleryDir + QLatin1String("/thumbs/")); | 375 | QDir thumb_dir(imgGalleryDir + QLatin1String("/thumbs/")); | ||
369 | QDir images_dir(imgGalleryDir + QLatin1String("/images/")); | 376 | QDir images_dir(imgGalleryDir + QLatin1String("/images/")); | ||
370 | QDir imageDir(sourceDirName, QStringLiteral("*.png *.PNG *.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP"), | 377 | QDir imageDir(sourceDirName, QStringLiteral("*.png *.PNG *.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP"), | ||
371 | QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::Readable); | 378 | QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::Readable); | ||
372 | QFile file(url.path()); | 379 | QFile file(url.path()); | ||
373 | 380 | | |||
374 | // Remove the image file .. | 381 | // Remove the image file .. | ||
375 | file.remove(); | 382 | file.remove(); | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
443 | 450 | | |||
444 | bool KImGalleryPlugin::createThumb(const QString &imgName, const QString &sourceDirName, | 451 | bool KImGalleryPlugin::createThumb(const QString &imgName, const QString &sourceDirName, | ||
445 | const QString &imgGalleryDir, const QString &imageFormat) | 452 | const QString &imgGalleryDir, const QString &imageFormat) | ||
446 | { | 453 | { | ||
447 | QImage img; | 454 | QImage img; | ||
448 | const QString pixPath = sourceDirName + QLatin1String("/") + imgName; | 455 | const QString pixPath = sourceDirName + QLatin1String("/") + imgName; | ||
449 | 456 | | |||
450 | if (m_copyFiles) { | 457 | if (m_copyFiles) { | ||
451 | KUrl srcURL = KUrl(pixPath); | 458 | QUrl srcURL = QUrl::fromLocalFile(pixPath); | ||
452 | //kDebug(90170) << "srcURL: " << srcURL; | 459 | //kDebug(90170) << "srcURL: " << srcURL; | ||
453 | KUrl destURL = KUrl(imgGalleryDir + QLatin1String("/images/") + imgName); | 460 | QUrl destURL = QUrl::fromLocalFile(imgGalleryDir + QLatin1String("/images/") + imgName); | ||
454 | //kDebug(90170) << "destURL: " << destURL; | 461 | //kDebug(90170) << "destURL: " << destURL; | ||
455 | KIO::NetAccess::file_copy(srcURL, destURL, static_cast<KParts::Part *>(parent())->widget()); | 462 | KIO::NetAccess::file_copy(srcURL, destURL, static_cast<KParts::Part *>(parent())->widget()); | ||
456 | } | 463 | } | ||
457 | 464 | | |||
458 | const QString imgNameFormat = imgName + extension(imageFormat); | 465 | const QString imgNameFormat = imgName + extension(imageFormat); | ||
459 | const QString thumbDir = imgGalleryDir + QLatin1String("/thumbs/"); | 466 | const QString thumbDir = imgGalleryDir + QLatin1String("/thumbs/"); | ||
460 | int extent = m_configDlg->getThumbnailSize(); | 467 | int extent = m_configDlg->getThumbnailSize(); | ||
461 | 468 | | |||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |
add static in front
(add space before '{' ?)