diff --git a/HTMLGenerator/Generator.cpp b/HTMLGenerator/Generator.cpp
index 35da4d0f..c0a832cb 100644
--- a/HTMLGenerator/Generator.cpp
+++ b/HTMLGenerator/Generator.cpp
@@ -1,805 +1,805 @@
/* Copyright (C) 2003-2020 The KPhotoAlbum Development Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "Generator.h"
#include "ImageSizeCheckBox.h"
#include "Logging.h"
#include "Setup.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace
{
QString readFile(const QString &fileName)
{
if (fileName.isEmpty()) {
KMessageBox::error(nullptr, i18n("
").arg(description));
else
content.replace(QString::fromLatin1("**DESCRIPTION**"), QString::fromLatin1(""));
// -------------------------------------------------- write to file
QString fileName = m_tempDir.filePath(namePage(width, height, currentFile));
bool ok = writeToFile(fileName, content);
if (!ok)
return false;
return true;
}
QString HTMLGenerator::Generator::namePage(int width, int height, const DB::FileName &fileName)
{
QString name = m_filenameMapper.uniqNameFor(fileName);
QString base = QFileInfo(name).completeBaseName();
return QString::fromLatin1("%1-%2.html").arg(base).arg(ImageSizeCheckBox::text(width, height, true));
}
QString HTMLGenerator::Generator::nameImage(const DB::FileName &fileName, int size)
{
QString name = m_filenameMapper.uniqNameFor(fileName);
QString base = QFileInfo(name).completeBaseName();
if (size == maxImageSize() && !Utilities::isVideo(fileName)) {
if (name.endsWith(QString::fromLatin1(".jpg"), Qt::CaseSensitive) || name.endsWith(QString::fromLatin1(".jpeg"), Qt::CaseSensitive))
return name;
else
return base + QString::fromLatin1(".jpg");
} else if (size == maxImageSize() && Utilities::isVideo(fileName)) {
return name;
} else
return QString::fromLatin1("%1-%2.jpg").arg(base).arg(size);
}
QString HTMLGenerator::Generator::createImage(const DB::FileName &fileName, int size)
{
const DB::ImageInfoPtr info = DB::ImageDB::instance()->info(fileName);
if (m_generatedFiles.contains(qMakePair(fileName, size))) {
m_waitCounter--;
} else {
ImageManager::ImageRequest *request = new ImageManager::ImageRequest(fileName, QSize(size, size),
info->angle(), this);
request->setPriority(ImageManager::BatchTask);
ImageManager::AsyncLoader::instance()->load(request);
m_generatedFiles.insert(qMakePair(fileName, size));
}
return nameImage(fileName, size);
}
QString HTMLGenerator::Generator::createVideo(const DB::FileName &fileName)
{
setValue(m_total - m_waitCounter);
qApp->processEvents();
QString baseName = nameImage(fileName, maxImageSize());
QString destName = m_tempDir.filePath(baseName);
if (!m_copiedVideos.contains(fileName)) {
if (m_setup.html5VideoGenerate()) {
// TODO: shouldn't we use avconv library directly instead of KRun
// TODO: should check that the avconv (ffmpeg takes the same parameters on older systems) and ffmpeg2theora exist
// TODO: Figure out avconv parameters to get rid of ffmpeg2theora
KRun::runCommand(QString::fromLatin1("%1 -y -i %2 -vcodec libx264 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s %3 %4")
.arg(m_avconv)
.arg(fileName.absolute())
.arg(QString::fromLatin1("320x240"))
.arg(destName.replace(QRegExp(QString::fromLatin1("\\..*")), QString::fromLatin1(".mp4"))),
MainWindow::Window::theMainWindow());
KRun::runCommand(QString::fromLatin1("ffmpeg2theora -v 7 -o %1 -x %2 %3")
.arg(destName.replace(QRegExp(QString::fromLatin1("\\..*")), QString::fromLatin1(".ogg")))
.arg(QString::fromLatin1("320"))
.arg(fileName.absolute()),
MainWindow::Window::theMainWindow());
} else
Utilities::copyOrOverwrite(fileName.absolute(), destName);
m_copiedVideos.insert(fileName);
}
return baseName;
}
QString HTMLGenerator::Generator::kimFileName(bool relative)
{
if (relative)
return QString::fromLatin1("%2.kim").arg(m_setup.outputDir());
else
return m_tempDir.filePath(QString::fromLatin1("%2.kim").arg(m_setup.outputDir()));
}
bool HTMLGenerator::Generator::writeToFile(const QString &fileName, const QString &str)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
KMessageBox::error(this, i18n("Could not create file '%1'.", fileName),
i18n("Could Not Create File"));
return false;
}
QByteArray data = translateToHTML(str).toUtf8();
file.write(data);
file.close();
return true;
}
QString HTMLGenerator::Generator::translateToHTML(const QString &str)
{
QString res;
for (int i = 0; i < str.length(); ++i) {
if (str[i].unicode() < 128)
res.append(str[i]);
else {
- res.append(QString().sprintf("%u;", (unsigned int)str[i].unicode()));
+ res.append(QStringLiteral("%1;").arg((unsigned int)str[i].unicode()));
}
}
return res;
}
bool HTMLGenerator::Generator::linkIndexFile()
{
ImageSizeCheckBox *resolution = m_setup.activeResolutions()[0];
QString fromFile = QString::fromLatin1("index-%1.html")
.arg(resolution->text(true));
fromFile = m_tempDir.filePath(fromFile);
QString destFile = m_tempDir.filePath(QString::fromLatin1("index.html"));
bool ok = Utilities::copyOrOverwrite(fromFile, destFile);
if (!ok) {
KMessageBox::error(this, i18n("
Unable to copy %1 to %2
", fromFile, destFile));
return false;
}
return ok;
}
void HTMLGenerator::Generator::slotCancelGenerate()
{
ImageManager::AsyncLoader::instance()->stop(this);
m_waitCounter = 0;
if (m_hasEnteredLoop)
m_eventLoop->exit();
}
void HTMLGenerator::Generator::pixmapLoaded(ImageManager::ImageRequest *request, const QImage &image)
{
const DB::FileName fileName = request->databaseFileName();
const QSize imgSize = request->size();
const bool loadedOK = request->loadedOK();
setValue(m_total - m_waitCounter);
m_waitCounter--;
int size = imgSize.width();
QString file = m_tempDir.filePath(nameImage(fileName, size));
bool success = loadedOK && image.save(file, "JPEG");
if (!success) {
// We better stop the imageloading. In case this is a full disk, we will just get all images loaded, while this
// error box is showing, resulting in a bunch of error messages, and memory running out due to all the hanging
// pixmapLoaded methods.
slotCancelGenerate();
KMessageBox::error(this, i18n("Unable to write image '%1'.", file));
}
if (!Utilities::isVideo(fileName)) {
try {
Exif::Info::instance()->writeInfoToFile(fileName, file);
} catch (...) {
}
}
if (m_waitCounter == 0 && m_hasEnteredLoop) {
m_eventLoop->exit();
}
}
int HTMLGenerator::Generator::calculateSteps()
{
int count = m_setup.activeResolutions().count();
return m_setup.imageList().size() * (1 + count); // 1 thumbnail + 1 real image
}
void HTMLGenerator::Generator::getThemeInfo(QString *baseDir, QString *name, QString *author)
{
*baseDir = m_setup.themePath();
KConfig themeconfig(QString::fromLatin1("%1/kphotoalbum.theme").arg(*baseDir), KConfig::SimpleConfig);
KConfigGroup config = themeconfig.group("theme");
*name = config.readEntry("Name");
*author = config.readEntry("Author");
}
int HTMLGenerator::Generator::maxImageSize()
{
int res = 0;
for (QList::ConstIterator sizeIt = m_setup.activeResolutions().begin();
sizeIt != m_setup.activeResolutions().end(); ++sizeIt) {
res = qMax(res, (*sizeIt)->width());
}
return res;
}
void HTMLGenerator::Generator::minImageSize(int &width, int &height)
{
width = height = 0;
for (QList::ConstIterator sizeIt = m_setup.activeResolutions().begin();
sizeIt != m_setup.activeResolutions().end(); ++sizeIt) {
if ((width == 0) && ((*sizeIt)->width() > 0)) {
width = (*sizeIt)->width();
height = (*sizeIt)->height();
} else if ((*sizeIt)->width() > 0) {
width = qMin(width, (*sizeIt)->width());
height = qMin(height, (*sizeIt)->height());
}
}
}
void HTMLGenerator::Generator::showBrowser()
{
if (m_setup.generateKimFile())
ImportExport::Export::showUsageDialog();
if (!m_setup.baseURL().isEmpty())
new KRun(QUrl::fromUserInput(QString::fromLatin1("%1/%2/index.html").arg(m_setup.baseURL()).arg(m_setup.outputDir())),
MainWindow::Window::theMainWindow());
m_eventLoop->exit();
}
QString HTMLGenerator::Generator::populateDescription(QList categories, const DB::ImageInfoPtr info)
{
QString description;
if (m_setup.includeCategory(QString::fromLatin1("**DATE**")))
description += QString::fromLatin1("
%1 %2
").arg(i18n("Date")).arg(info->date().toString());
for (QList::Iterator it = categories.begin(); it != categories.end(); ++it) {
if ((*it)->isSpecialCategory())
continue;
QString name = (*it)->name();
if (!info->itemsOfCategory(name).empty() && m_setup.includeCategory(name)) {
QString val = QStringList(info->itemsOfCategory(name).toList()).join(QString::fromLatin1(", "));
description += QString::fromLatin1("