Skip to content

Commit

Permalink
Version 1.09
Browse files Browse the repository at this point in the history
  • Loading branch information
4lex4 committed Feb 7, 2018
2 parents 8084fad + c69bc14 commit d1433e0
Show file tree
Hide file tree
Showing 725 changed files with 27,732 additions and 15,266 deletions.
14 changes: 8 additions & 6 deletions AbstractFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class QDomElement;
*/
class AbstractFilter : public ref_countable {
public:
virtual ~AbstractFilter() {
}
~AbstractFilter() override = default;

virtual QString getName() const = 0;

Expand All @@ -59,16 +58,19 @@ class AbstractFilter : public ref_countable {
return std::vector<PageOrderOption>();
}

virtual void performRelinking(AbstractRelinker const& relinker) = 0;
virtual void performRelinking(const AbstractRelinker& relinker) = 0;

virtual void preUpdateUI(FilterUiInterface* ui, PageId const& page_id) = 0;
virtual void preUpdateUI(FilterUiInterface* ui, const PageId& page_id) = 0;

virtual void updateStatistics() {
}

virtual QDomElement saveSettings(ProjectWriter const& writer, QDomDocument& doc) const = 0;
virtual QDomElement saveSettings(const ProjectWriter& writer, QDomDocument& doc) const = 0;

virtual void loadSettings(const ProjectReader& reader, const QDomElement& filters_el) = 0;

virtual void loadSettings(ProjectReader const& reader, QDomElement const& filters_el) = 0;
virtual void loadDefaultSettings(const PageId& page_id) {
};
};


Expand Down
5 changes: 2 additions & 3 deletions AbstractRelinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ class QString;

class AbstractRelinker : public ref_countable {
public:
virtual ~AbstractRelinker() {
}
~AbstractRelinker() override = default;

/**
* Returns the path to be used instead of the given path.
* The same path will be returned if no substitution is to be made.
*/
virtual QString substitutionPathFor(RelinkablePath const& orig_path) const = 0;
virtual QString substitutionPathFor(const RelinkablePath& orig_path) const = 0;
};


Expand Down
75 changes: 47 additions & 28 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
#include <QtCore/QDir>

Application::Application(int& argc, char** argv)
: QApplication(argc, argv) {
: QApplication(argc, argv),
m_currentLocale("en") {
initTranslations();
}

bool Application::notify(QObject* receiver, QEvent* e) {
try {
return QApplication::notify(receiver, e);
} catch (std::bad_alloc const&) {
} catch (const std::bad_alloc&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();

return false;
Expand All @@ -52,35 +54,52 @@ void Application::installLanguage(const QString& locale) {
return;
}

QString const translation("scantailor_" + locale);
QStringList const translation_dirs(
QString::fromUtf8(TRANSLATION_DIRS).split(QChar(':'), QString::SkipEmptyParts)
);
bool loaded = false;
for (QString const& path : translation_dirs) {
QString absolute_path;
if (QDir::isAbsolutePath(path)) {
absolute_path = path;
} else {
absolute_path = this->applicationDirPath();
absolute_path += QChar('/');
absolute_path += path;
}
absolute_path += QChar('/');
absolute_path += translation;
if (m_translationsMap.find(locale) != m_translationsMap.end()) {
bool loaded = m_translator.load(m_translationsMap[locale]);

loaded = m_translator.load(absolute_path);
if (loaded) {
break;
}
}

this->removeTranslator(&m_translator);
this->installTranslator(&m_translator);
this->removeTranslator(&m_translator);
this->installTranslator(&m_translator);

m_currentLocale = (loaded) ? locale : "en";
m_currentLocale = (loaded) ? locale : "en";
} else {
this->removeTranslator(&m_translator);

m_currentLocale = "en";
}
}

const QString& Application::getCurrentLocale() const {
return m_currentLocale;
}
}

std::list<QString> Application::getLanguagesList() const {
std::list<QString> list{ "en" };
std::transform(m_translationsMap.begin(), m_translationsMap.end(),
std::back_inserter(list),
[](const std::pair<QString, QString>& val) {
return val.first;
});

return list;
}

void Application::initTranslations() {
const QStringList translation_dirs(
QString::fromUtf8(TRANSLATION_DIRS).split(QChar(':'), QString::SkipEmptyParts)
);

const QStringList language_file_filter("scantailor_*.qm");
for (const QString& path : translation_dirs) {
QDir dir(path);
if (dir.exists()) {
QStringList translationFileNames = QDir(path).entryList(language_file_filter);
for (const QString& fileName : translationFileNames) {
QString locale(fileName);
locale.truncate(locale.lastIndexOf('.'));
locale.remove(0, locale.indexOf('_') + 1);

m_translationsMap[locale] = dir.absoluteFilePath(fileName);
}
}
}
}
8 changes: 7 additions & 1 deletion Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ Q_OBJECT
public:
Application(int& argc, char** argv);

virtual bool notify(QObject* receiver, QEvent* e);
bool notify(QObject* receiver, QEvent* e) override;

const QString& getCurrentLocale() const;

void installLanguage(const QString& locale);

std::list<QString> getLanguagesList() const;

private:
void initTranslations();


QTranslator m_translator;
QString m_currentLocale;
std::map<QString, QString> m_translationsMap;
};


Expand Down
17 changes: 8 additions & 9 deletions AtomicFileOverwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
#include <QFile>
#include <QTemporaryFile>

AtomicFileOverwriter::AtomicFileOverwriter() {
}
AtomicFileOverwriter::AtomicFileOverwriter() = default;

AtomicFileOverwriter::~AtomicFileOverwriter() {
abort();
}

QIODevice* AtomicFileOverwriter::startWriting(QString const& file_path) {
QIODevice* AtomicFileOverwriter::startWriting(const QString& file_path) {
abort();

m_ptrTempFile.reset(new QTemporaryFile(file_path));
m_ptrTempFile = std::make_unique<QTemporaryFile>(file_path);
m_ptrTempFile->setAutoRemove(false);
if (!m_ptrTempFile->open()) {
m_ptrTempFile.reset();
Expand All @@ -41,12 +40,12 @@ QIODevice* AtomicFileOverwriter::startWriting(QString const& file_path) {
}

bool AtomicFileOverwriter::commit() {
if (!m_ptrTempFile.get()) {
if (!m_ptrTempFile) {
return false;
}

QString const temp_file_path(m_ptrTempFile->fileName());
QString const target_path(m_ptrTempFile->fileTemplate());
const QString temp_file_path(m_ptrTempFile->fileName());
const QString target_path(m_ptrTempFile->fileTemplate());

// Yes, we have to destroy this object here, because:
// 1. Under Windows, open files can't be renamed or deleted.
Expand All @@ -63,11 +62,11 @@ bool AtomicFileOverwriter::commit() {
}

void AtomicFileOverwriter::abort() {
if (!m_ptrTempFile.get()) {
if (!m_ptrTempFile) {
return;
}

QString const temp_file_path(m_ptrTempFile->fileName());
const QString temp_file_path(m_ptrTempFile->fileName());
m_ptrTempFile.reset(); // See comments in commit()
QFile::remove(temp_file_path);
}
Expand Down
2 changes: 1 addition & 1 deletion AtomicFileOverwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DECLARE_NON_COPYABLE(AtomicFileOverwriter)
* If a file is already being written, it calles abort() and then
* proceeds as usual.
*/
QIODevice* startWriting(QString const& file_path);
QIODevice* startWriting(const QString& file_path);

/**
* \brief Replaces the target file with the temporary one.
Expand Down
37 changes: 18 additions & 19 deletions BackgroundExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include "OutOfMemoryHandler.h"
#include <QCoreApplication>
#include <QThread>
#include <assert.h>
#include <cassert>

class BackgroundExecutor::Dispatcher : public QObject {
public:
Dispatcher(Impl& owner);
explicit Dispatcher(Impl& owner);

protected:
virtual void customEvent(QEvent* event);
void customEvent(QEvent* event) override;

private:
Impl& m_rOwner;
Expand All @@ -36,16 +36,16 @@ class BackgroundExecutor::Dispatcher : public QObject {

class BackgroundExecutor::Impl : public QThread {
public:
Impl(BackgroundExecutor& owner);
explicit Impl(BackgroundExecutor& owner);

~Impl();
~Impl() override;

void enqueueTask(TaskPtr const& task);
void enqueueTask(const TaskPtr& task);

protected:
virtual void run();
void run() override;

virtual void customEvent(QEvent* event);
void customEvent(QEvent* event) override;

private:
BackgroundExecutor& m_rOwner;
Expand All @@ -60,15 +60,14 @@ BackgroundExecutor::BackgroundExecutor()
: m_ptrImpl(new Impl(*this)) {
}

BackgroundExecutor::~BackgroundExecutor() {
}
BackgroundExecutor::~BackgroundExecutor() = default;

void BackgroundExecutor::shutdown() {
m_ptrImpl.reset();
}

void BackgroundExecutor::enqueueTask(TaskPtr const& task) {
if (m_ptrImpl.get()) {
void BackgroundExecutor::enqueueTask(const TaskPtr& task) {
if (m_ptrImpl) {
m_ptrImpl->enqueueTask(task);
}
}
Expand All @@ -81,19 +80,19 @@ BackgroundExecutor::Dispatcher::Dispatcher(Impl& owner)

void BackgroundExecutor::Dispatcher::customEvent(QEvent* event) {
try {
TaskEvent* evt = dynamic_cast<TaskEvent*>(event);
auto* evt = dynamic_cast<TaskEvent*>(event);
assert(evt);

TaskPtr const& task = evt->payload();
const TaskPtr& task = evt->payload();
assert(task);

TaskResultPtr const result((*task)());
const TaskResultPtr result((*task)());
if (result) {
QCoreApplication::postEvent(
&m_rOwner, new ResultEvent(result)
);
}
} catch (std::bad_alloc const&) {
} catch (const std::bad_alloc&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
}
}
Expand All @@ -112,7 +111,7 @@ BackgroundExecutor::Impl::~Impl() {
wait();
}

void BackgroundExecutor::Impl::enqueueTask(TaskPtr const& task) {
void BackgroundExecutor::Impl::enqueueTask(const TaskPtr& task) {
QCoreApplication::postEvent(&m_dispatcher, new TaskEvent(task));
if (!m_threadStarted) {
start();
Expand All @@ -125,10 +124,10 @@ void BackgroundExecutor::Impl::run() {
}

void BackgroundExecutor::Impl::customEvent(QEvent* event) {
ResultEvent* evt = dynamic_cast<ResultEvent*>(event);
auto* evt = dynamic_cast<ResultEvent*>(event);
assert(evt);

TaskResultPtr const& result = evt->payload();
const TaskResultPtr& result = evt->payload();
assert(result);

(*result)();
Expand Down
2 changes: 1 addition & 1 deletion BackgroundExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DECLARE_NON_COPYABLE(BackgroundExecutor)
* to be executed in the thread where this BackgroundExecutor
* object was constructed.
*/
void enqueueTask(TaskPtr const& task);
void enqueueTask(const TaskPtr& task);

private:
class Impl;
Expand Down
2 changes: 1 addition & 1 deletion BackgroundTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "BackgroundTask.h"

char const* BackgroundTask::CancelledException::what() const throw() {
const char* BackgroundTask::CancelledException::what() const throw() {
return "BackgroundTask cancelled";
}

Expand Down
12 changes: 6 additions & 6 deletions BackgroundTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,34 @@ class BackgroundTask : public AbstractCommand0<FilterResultPtr>, public TaskStat

class CancelledException : public std::exception {
public:
virtual char const* what() const throw();
const char* what() const throw() override;
};


BackgroundTask(Type type)
explicit BackgroundTask(Type type)
: m_type(type) {
}

Type type() const {
return m_type;
}

virtual void cancel() {
void cancel() override {
m_cancelFlag.store(1);
}

virtual bool isCancelled() const {
bool isCancelled() const override {
return m_cancelFlag.load() != 0;
}

/**
* \brief If cancelled, throws CancelledException.
*/
virtual void throwIfCancelled() const;
void throwIfCancelled() const override;

private:
QAtomicInt m_cancelFlag;
Type const m_type;
const Type m_type;
};


Expand Down
Loading

0 comments on commit d1433e0

Please sign in to comment.