Const orgies.

This commit is contained in:
Martin Rotter 2016-01-14 13:17:49 +01:00
parent 42b44043df
commit e3c28f4681
21 changed files with 191 additions and 131 deletions

View File

@ -119,15 +119,13 @@ void FormDatabaseCleanup::onPurgeFinished(bool finished) {
}
void FormDatabaseCleanup::loadDatabaseInfo() {
qint64 db_size = qApp->database()->getDatabaseSize();
qint64 file_size = qApp->database()->getDatabaseFileSize();
qint64 data_size = qApp->database()->getDatabaseDataSize();
if (db_size > 0) {
m_ui->m_txtFileSize->setText(QString::number(db_size / 1000000.0) + QL1S(" MB"));
}
else {
m_ui->m_txtFileSize->setText(QSL("-"));
}
QString file_size_str = file_size > 0 ? QString::number(file_size / 1000000.0) + QL1S(" MB") : tr("unknown");
QString data_size_str = data_size > 0 ? QString::number(data_size / 1000000.0) + QL1S(" MB") : tr("unknown");
m_ui->m_txtFileSize->setText(tr("file: %1, data: %2").arg(file_size_str, data_size_str));
m_ui->m_txtDatabaseType->setText(qApp->database()->humanDriverName(qApp->database()->activeDatabaseDriver()));
m_ui->m_checkShrink->setEnabled(qApp->database()->activeDatabaseDriver() == DatabaseFactory::SQLITE ||
qApp->database()->activeDatabaseDriver() == DatabaseFactory::SQLITE_MEMORY);

View File

@ -50,7 +50,7 @@ Application::Application(const QString &id, int &argc, char **argv)
}
Application::~Application() {
delete m_updateFeedsLock;
qDebug("Destroying Application instance.");
qDeleteAll(m_feedServices);
}
@ -108,11 +108,13 @@ DownloadManager *Application::downloadManager() {
}
Mutex *Application::feedUpdateLock() {
if (m_updateFeedsLock == NULL) {
m_updateFeedsLock = new Mutex();
if (m_updateFeedsLock.isNull()) {
// NOTE: Cannot use parent hierarchy because this method can be usually called
// from any thread.
m_updateFeedsLock.reset(new Mutex());
}
return m_updateFeedsLock;
return m_updateFeedsLock.data();
}
void Application::backupDatabaseSettings(bool backup_database, bool backup_settings,
@ -261,7 +263,7 @@ void Application::onAboutToQuit() {
eliminateFirstRun(APP_VERSION);
// Make sure that we obtain close lock BEFORE even trying to quit the application.
bool locked_safely = feedUpdateLock()->tryLock(4 * CLOSE_LOCK_TIMEOUT);
const bool locked_safely = feedUpdateLock()->tryLock(4 * CLOSE_LOCK_TIMEOUT);
processEvents();

View File

@ -195,7 +195,7 @@ class Application : public QtSingleApplication {
// But of user decides to close the application (in other words,
// tries to lock the lock for writing), then no other
// action will be allowed to lock for reading.
Mutex *m_updateFeedsLock;
QScopedPointer<Mutex> m_updateFeedsLock;
QList<ServiceEntryPoint*> m_feedServices;
QList<QAction*> m_userActions;
FormMain *m_mainForm;

View File

@ -24,7 +24,6 @@
class AutoSaver : public QObject{
Q_OBJECT
public:

View File

@ -39,7 +39,7 @@ void DatabaseCleaner::purgeDatabaseData(const CleanerOrders &which_data) {
emit purgeStarted();
bool result = true;
int difference = 99 / 8;
const int difference = 99 / 8;
int progress = 0;
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
@ -118,7 +118,7 @@ bool DatabaseCleaner::purgeReadMessages(const QSqlDatabase &database) {
bool DatabaseCleaner::purgeOldMessages(const QSqlDatabase &database, int days) {
QSqlQuery query = QSqlQuery(database);
qint64 since_epoch = QDateTime::currentDateTimeUtc().addDays(-days).toMSecsSinceEpoch();
const qint64 since_epoch = QDateTime::currentDateTimeUtc().addDays(-days).toMSecsSinceEpoch();
query.setForwardOnly(true);
query.prepare(QSL("DELETE FROM Messages WHERE is_important = :is_important AND date_created < :date_created;"));

View File

@ -39,7 +39,7 @@ DatabaseFactory::DatabaseFactory(QObject *parent)
DatabaseFactory::~DatabaseFactory() {
}
qint64 DatabaseFactory::getDatabaseSize() {
qint64 DatabaseFactory::getDatabaseFileSize() const {
if (m_activeDatabaseDriver == SQLITE || m_activeDatabaseDriver == SQLITE_MEMORY) {
return QFileInfo(sqliteDatabaseFilePath()).size();
}
@ -48,6 +48,53 @@ qint64 DatabaseFactory::getDatabaseSize() {
}
}
qint64 DatabaseFactory::getDatabaseDataSize() const {
if (m_activeDatabaseDriver == SQLITE || m_activeDatabaseDriver == SQLITE_MEMORY) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
qint64 result = 1;
QSqlQuery query(database);
if (query.exec(QSL("PRAGMA page_count;"))) {
query.next();
result *= query.value(0).value<qint64>();
}
else {
return 0;
}
if (query.exec(QSL("PRAGMA page_size;"))) {
query.next();
result *= query.value(0).value<qint64>();
}
else {
return 0;
}
return result;
}
else if (m_activeDatabaseDriver == MYSQL) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
qint64 result = 1;
QSqlQuery query(database);
if (query.exec("SELECT Round(Sum(data_length + index_length), 1) "
"FROM information_schema.tables "
"GROUP BY table_schema;")) {
while (query.next()) {
result *= query.value(0).value<qint64>();
}
return result;
}
else {
return 0;
}
}
else {
return 0;
}
}
DatabaseFactory::MySQLError DatabaseFactory::mysqlTestConnection(const QString &hostname, int port, const QString &w_database,
const QString &username, const QString &password) {
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_MYSQL_DRIVER, APP_DB_MYSQL_TEST);
@ -64,14 +111,12 @@ DatabaseFactory::MySQLError DatabaseFactory::mysqlTestConnection(const QString &
return MySQLOk;
}
else {
// Connection failed, do cleanup and return specific
// error code.
MySQLError error_code = static_cast<MySQLError>(database.lastError().number());
return error_code;
// Connection failed, do cleanup and return specific error code.
return static_cast<MySQLError>(database.lastError().number());
}
}
QString DatabaseFactory::mysqlInterpretErrorCode(MySQLError error_code) {
QString DatabaseFactory::mysqlInterpretErrorCode(MySQLError error_code) const {
switch (error_code) {
case MySQLOk:
return tr("MySQL server works as expected.");
@ -112,7 +157,7 @@ void DatabaseFactory::finishRestoration() {
return;
}
QString backup_database_file = m_sqliteDatabaseFilePath + QDir::separator() + BACKUP_NAME_DATABASE + BACKUP_SUFFIX_DATABASE;
const QString backup_database_file = m_sqliteDatabaseFilePath + QDir::separator() + BACKUP_NAME_DATABASE + BACKUP_SUFFIX_DATABASE;
if (QFile::exists(backup_database_file)) {
qWarning("Backup database file '%s' was detected. Restoring it.", qPrintable(QDir::toNativeSeparators(backup_database_file)));
@ -173,7 +218,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeInMemoryDatabase() {
qPrintable(APP_MISC_PATH));
}
QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
const QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
database.transaction();
foreach(const QString &statement, statements) {
@ -203,8 +248,16 @@ QSqlDatabase DatabaseFactory::sqliteInitializeInMemoryDatabase() {
// Copy all stuff.
// WARNING: All tables belong here.
QStringList tables; tables << QSL("Information") << QSL("Categories") << QSL("Feeds") <<
QSL("Accounts") << QSL("TtRssAccounts") << QSL("Messages");
QStringList tables;
if (copy_contents.exec(QSL("SELECT name FROM storage.sqlite_master WHERE type='table';"))) {
while (copy_contents.next()) {
tables.append(copy_contents.value(0).toString());
}
}
else {
qFatal("Cannot obtain list of table names from file-base SQLite database.");
}
foreach (const QString &table, tables) {
copy_contents.exec(QString("INSERT INTO main.%1 SELECT * FROM storage.%1;").arg(table));
@ -229,7 +282,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeFileBasedDatabase(const QString &c
finishRestoration();
// Prepare file paths.
QDir db_path(m_sqliteDatabaseFilePath);
const QDir db_path(m_sqliteDatabaseFilePath);
QFile db_file(db_path.absoluteFilePath(APP_DB_SQLITE_FILE));
// Check if database directory exists.
@ -278,7 +331,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeFileBasedDatabase(const QString &c
qPrintable(APP_MISC_PATH));
}
QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
const QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
database.transaction();
foreach(const QString &statement, statements) {
@ -296,7 +349,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeFileBasedDatabase(const QString &c
}
else {
query_db.next();
QString installed_db_schema = query_db.value(0).toString();
const QString installed_db_schema = query_db.value(0).toString();
query_db.finish();
if (installed_db_schema < APP_DB_SCHEMA_VERSION) {
@ -331,7 +384,7 @@ QString DatabaseFactory::sqliteDatabaseFilePath() const {
bool DatabaseFactory::sqliteUpdateDatabaseSchema(QSqlDatabase database, const QString &source_db_schema_version) {
int working_version = QString(source_db_schema_version).remove('.').toInt();
int current_version = QString(APP_DB_SCHEMA_VERSION).remove('.').toInt();
const int current_version = QString(APP_DB_SCHEMA_VERSION).remove('.').toInt();
// Now, it would be good to create backup of SQLite DB file.
if (IOFactory::copyFile(sqliteDatabaseFilePath(), sqliteDatabaseFilePath() + ".bak")) {
@ -342,7 +395,7 @@ bool DatabaseFactory::sqliteUpdateDatabaseSchema(QSqlDatabase database, const QS
}
while (working_version != current_version) {
QString update_file_name = QString(APP_MISC_PATH) + QDir::separator() +
const QString update_file_name = QString(APP_MISC_PATH) + QDir::separator() +
QString(APP_DB_UPDATE_FILE_PATTERN).arg(QSL("sqlite"),
QString::number(working_version),
QString::number(working_version + 1));
@ -357,7 +410,7 @@ bool DatabaseFactory::sqliteUpdateDatabaseSchema(QSqlDatabase database, const QS
qFatal("Updating of database schema failed. File '%s' cannot be opened.", qPrintable(QDir::toNativeSeparators(update_file_name)));
}
QStringList statements = QString(update_file_handle.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
const QStringList statements = QString(update_file_handle.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
foreach (const QString &statement, statements) {
QSqlQuery query = database.exec(statement);
@ -377,10 +430,10 @@ bool DatabaseFactory::sqliteUpdateDatabaseSchema(QSqlDatabase database, const QS
bool DatabaseFactory::mysqlUpdateDatabaseSchema(QSqlDatabase database, const QString &source_db_schema_version) {
int working_version = QString(source_db_schema_version).remove('.').toInt();
int current_version = QString(APP_DB_SCHEMA_VERSION).remove('.').toInt();
const int current_version = QString(APP_DB_SCHEMA_VERSION).remove('.').toInt();
while (working_version != current_version) {
QString update_file_name = QString(APP_MISC_PATH) + QDir::separator() +
const QString update_file_name = QString(APP_MISC_PATH) + QDir::separator() +
QString(APP_DB_UPDATE_FILE_PATTERN).arg(QSL("mysql"),
QString::number(working_version),
QString::number(working_version + 1));
@ -425,7 +478,7 @@ QSqlDatabase DatabaseFactory::connection(const QString &connection_name, Desired
}
}
QString DatabaseFactory::humanDriverName(DatabaseFactory::UsedDriver driver) {
QString DatabaseFactory::humanDriverName(DatabaseFactory::UsedDriver driver) const {
switch (driver) {
case MYSQL:
return tr("MySQL/MariaDB (dedicated database)");
@ -437,7 +490,7 @@ QString DatabaseFactory::humanDriverName(DatabaseFactory::UsedDriver driver) {
}
}
QString DatabaseFactory::humanDriverName(const QString &driver_code) {
QString DatabaseFactory::humanDriverName(const QString &driver_code) const {
if (driver_code == APP_DB_SQLITE_DRIVER) {
return humanDriverName(SQLITE);
}
@ -466,8 +519,16 @@ void DatabaseFactory::sqliteSaveMemoryDatabase() {
// Copy all stuff.
// WARNING: All tables belong here.
QStringList tables; tables << QSL("Information") << QSL("Categories") << QSL("Feeds") <<
QSL("Accounts") << QSL("TtRssAccounts") << QSL("Messages");
QStringList tables;
if (copy_contents.exec(QSL("SELECT name FROM storage.sqlite_master WHERE type='table';"))) {
while (copy_contents.next()) {
tables.append(copy_contents.value(0).toString());
}
}
else {
qFatal("Cannot obtain list of table names from file-base SQLite database.");
}
foreach (const QString &table, tables) {
copy_contents.exec(QString(QSL("DELETE FROM storage.%1;")).arg(table));
@ -480,7 +541,7 @@ void DatabaseFactory::sqliteSaveMemoryDatabase() {
}
void DatabaseFactory::determineDriver() {
QString db_driver = qApp->settings()->value(GROUP(Database), SETTING(Database::ActiveDriver)).toString();
const QString db_driver = qApp->settings()->value(GROUP(Database), SETTING(Database::ActiveDriver)).toString();
if (db_driver == APP_DB_MYSQL_DRIVER && QSqlDatabase::isDriverAvailable(APP_DB_SQLITE_DRIVER)) {
// User wants to use MySQL and MySQL is actually available. Use it.
@ -556,7 +617,7 @@ QSqlDatabase DatabaseFactory::mysqlConnection(const QString &connection_name) {
QSqlDatabase DatabaseFactory::mysqlInitializeDatabase(const QString &connection_name) {
// Folders are created. Create new QSQLDatabase object.
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_MYSQL_DRIVER, connection_name);
QString database_name = qApp->settings()->value(GROUP(Database), SETTING(Database::MySQLDatabase)).toString();
const QString database_name = qApp->settings()->value(GROUP(Database), SETTING(Database::MySQLDatabase)).toString();
database.setHostName(qApp->settings()->value(GROUP(Database), SETTING(Database::MySQLHostname)).toString());
database.setPort(qApp->settings()->value(GROUP(Database), SETTING(Database::MySQLPort)).toInt());
@ -584,7 +645,7 @@ QSqlDatabase DatabaseFactory::mysqlInitializeDatabase(const QString &connection_
qPrintable(APP_MISC_PATH));
}
QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
const QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
database.transaction();
foreach(QString statement, statements) {
@ -604,7 +665,7 @@ QSqlDatabase DatabaseFactory::mysqlInitializeDatabase(const QString &connection_
// Database was previously initialized. Now just check the schema version.
query_db.next();
QString installed_db_schema = query_db.value(0).toString();
const QString installed_db_schema = query_db.value(0).toString();
if (installed_db_schema < APP_DB_SCHEMA_VERSION) {
if (mysqlUpdateDatabaseSchema(database, installed_db_schema)) {
@ -683,7 +744,7 @@ QSqlDatabase DatabaseFactory::sqliteConnection(const QString &connection_name, D
// yet, add it and set it up.
database = QSqlDatabase::addDatabase(APP_DB_SQLITE_DRIVER, connection_name);
QDir db_path(m_sqliteDatabaseFilePath);
const QDir db_path(m_sqliteDatabaseFilePath);
QFile db_file(db_path.absoluteFilePath(APP_DB_SQLITE_FILE));
// Setup database file path.

View File

@ -61,15 +61,18 @@ class DatabaseFactory : public QObject {
virtual ~DatabaseFactory();
// Returns size of DB file.
qint64 getDatabaseSize();
qint64 getDatabaseFileSize() const;
// Returns size of data contained in the DB file.
qint64 getDatabaseDataSize() const;
// If in-memory is true, then :memory: database is returned
// In-memory database is DEFAULT database.
// NOTE: This always returns OPENED database.
QSqlDatabase connection(const QString &connection_name, DesiredType desired_type = FromSettings);
QString humanDriverName(UsedDriver driver);
QString humanDriverName(const QString &driver_code);
QString humanDriverName(UsedDriver driver) const;
QString humanDriverName(const QString &driver_code) const;
// Removes connection.
void removeConnection(const QString &connection_name = QString());
@ -106,7 +109,7 @@ class DatabaseFactory : public QObject {
const QString &username, const QString &password);
// Interprets MySQL error code.
QString mysqlInterpretErrorCode(MySQLError error_code);
QString mysqlInterpretErrorCode(MySQLError error_code) const;
private:
//

View File

@ -74,8 +74,8 @@ void IconFactory::setCurrentIconTheme(const QString &theme_name) {
}
void IconFactory::loadCurrentIconTheme() {
QStringList installed_themes = installedIconThemes();
QString theme_name_from_settings = qApp->settings()->value(GROUP(GUI), SETTING(GUI::IconTheme)).toString();
const QStringList installed_themes = installedIconThemes();
const QString theme_name_from_settings = qApp->settings()->value(GROUP(GUI), SETTING(GUI::IconTheme)).toString();
if (m_currentIconTheme == theme_name_from_settings) {
qDebug("Icon theme '%s' already loaded.", qPrintable(theme_name_from_settings));
@ -108,7 +108,7 @@ QStringList IconFactory::installedIconThemes() const {
icon_themes_paths.removeDuplicates();
foreach (const QString &icon_path, icon_themes_paths) {
QDir icon_dir(icon_path);
const QDir icon_dir(icon_path);
// Iterate all icon themes in this directory.
foreach (const QString &icon_theme_path, icon_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot |

View File

@ -39,10 +39,10 @@ class IconFactory : public QObject {
// Destructor.
virtual ~IconFactory();
// Used to store/retrieve QIcons from/to database via Base64-encoded
// Used to store/retrieve QIcons from/to Base64-encoded
// byte array.
QIcon fromByteArray(QByteArray array);
QByteArray toByteArray(const QIcon &icon);
static QIcon fromByteArray(QByteArray array);
static QByteArray toByteArray(const QIcon &icon);
void clearCache();

View File

@ -48,8 +48,8 @@ QString IOFactory::ensureUniqueFilename(const QString &name, const QString &appe
while (QFile::exists(tmp_filename)) {
tmp_filename = name;
int index = tmp_filename.lastIndexOf(QL1C('.'));
QString append_string = append_format.arg(i++);
const int index = tmp_filename.lastIndexOf(QL1C('.'));
const QString append_string = append_format.arg(i++);
if (index < 0) {
tmp_filename.append(append_string);

View File

@ -32,7 +32,7 @@ Localization::Localization(QObject *parent)
Localization::~Localization() {
}
QString Localization::desiredLanguage() {
QString Localization::desiredLanguage() const {
return qApp->settings()->value(GROUP(General), SETTING(General::Language)).toString();
}
@ -63,9 +63,9 @@ void Localization::loadActiveLanguage() {
QLocale::setDefault(m_loadedLocale);
}
QList<Language> Localization::installedLanguages() {
QList<Language> Localization::installedLanguages() const {
QList<Language> languages;
QDir file_dir(APP_LANG_PATH);
const QDir file_dir(APP_LANG_PATH);
QTranslator translator;
// Iterate all found language files.

View File

@ -44,14 +44,14 @@ class Localization : public QObject {
// Returns code of language that should
// be loaded according to settings.
QString desiredLanguage();
QString desiredLanguage() const;
// Loads currently active language.
void loadActiveLanguage();
// Returns list of installed application localizations.
// This list is used ie. in settings dialog.
QList<Language> installedLanguages();
QList<Language> installedLanguages() const;
// Returns empty string or loaded language
// name if it is really loaded.

View File

@ -23,7 +23,6 @@ Mutex::Mutex(QMutex::RecursionMode mode, QObject *parent) : QObject(parent), m_m
Mutex::~Mutex() {
qDebug("Destroying Mutex instance.");
delete m_mutex;
}
void Mutex::lock() {

View File

@ -49,7 +49,7 @@ class Mutex : public QObject {
void unlocked();
private:
QMutex *m_mutex;
QScopedPointer<QMutex> m_mutex;
bool m_isLocked;
};

View File

@ -319,7 +319,7 @@ bool Settings::initiateRestoration(const QString &settings_backup_file_path) {
}
void Settings::finishRestoration(const QString &desired_settings_file_path) {
QString backup_settings_file = QFileInfo(desired_settings_file_path).absolutePath() + QDir::separator() +
const QString backup_settings_file = QFileInfo(desired_settings_file_path).absolutePath() + QDir::separator() +
BACKUP_NAME_SETTINGS + BACKUP_SUFFIX_SETTINGS;
if (QFile::exists(backup_settings_file)) {
@ -341,7 +341,7 @@ Settings *Settings::setupSettings(QObject *parent) {
// If settings file exists (and is writable) in executable file working directory
// (in subdirectory APP_CFG_PATH), then use it (portable settings).
// Otherwise use settings file stored in home path.
SettingsProperties properties = determineProperties();
const SettingsProperties properties = determineProperties();
finishRestoration(properties.m_absoluteSettingsFileName);
@ -349,7 +349,7 @@ Settings *Settings::setupSettings(QObject *parent) {
new_settings = new Settings(properties.m_absoluteSettingsFileName, QSettings::IniFormat, properties.m_type, parent);
// Construct icon cache in the same path.
QString web_path = properties.m_baseDirectory + QDir::separator() + QString(APP_DB_WEB_PATH);
const QString web_path = properties.m_baseDirectory + QDir::separator() + QString(APP_DB_WEB_PATH);
QDir(web_path).mkpath(web_path);
QWebSettings::setIconDatabasePath(web_path);
@ -369,16 +369,16 @@ SettingsProperties Settings::determineProperties() {
properties.m_settingsSuffix = QDir::separator() + QString(APP_CFG_PATH) + QDir::separator() + QString(APP_CFG_FILE);
QString app_path = qApp->applicationDirPath();
QString home_path = qApp->homeFolderPath() + QDir::separator() + QString(APP_LOW_H_NAME);
QString home_path_file = home_path + properties.m_settingsSuffix;
const QString app_path = qApp->applicationDirPath();
const QString home_path = qApp->homeFolderPath() + QDir::separator() + QString(APP_LOW_H_NAME);
const QString home_path_file = home_path + properties.m_settingsSuffix;
bool portable_settings_available = QFileInfo(app_path).isWritable();
bool non_portable_settings_exist = QFile::exists(home_path_file);
const bool portable_settings_available = QFileInfo(app_path).isWritable();
const bool non_portable_settings_exist = QFile::exists(home_path_file);
// We will use PORTABLE settings only and only if it is available and NON-PORTABLE
// settings was not initialized before.
bool will_we_use_portable_settings = portable_settings_available && !non_portable_settings_exist;
const bool will_we_use_portable_settings = portable_settings_available && !non_portable_settings_exist;
if (will_we_use_portable_settings) {
properties.m_type = SettingsProperties::Portable;

View File

@ -32,9 +32,9 @@ SkinFactory::~SkinFactory() {
}
void SkinFactory::loadCurrentSkin() {
QString skin_name_from_settings = selectedSkinName();
const QString skin_name_from_settings = selectedSkinName();
bool skin_parsed;
Skin skin_data = skinInfo(skin_name_from_settings, &skin_parsed);
const Skin skin_data = skinInfo(skin_name_from_settings, &skin_parsed);
if (skin_parsed) {
loadSkinFromData(skin_data);
@ -50,7 +50,7 @@ void SkinFactory::loadCurrentSkin() {
}
bool SkinFactory::loadSkinFromData(const Skin &skin) {
QStringList skin_parts = skin.m_baseName.split(QL1C('/'), QString::SkipEmptyParts);
const QStringList skin_parts = skin.m_baseName.split(QL1C('/'), QString::SkipEmptyParts);
// Skin does not contain leading folder name or the actual skin file name.
if (skin_parts.size() != 2) {
@ -64,7 +64,7 @@ bool SkinFactory::loadSkinFromData(const Skin &skin) {
}
// Create needed variables and create QFile object representing skin contents.
QString skin_folder = skin_parts.at(0);
const QString skin_folder = skin_parts.at(0);
// Here we use "/" instead of QDir::separator() because CSS2.1 url field
// accepts '/' as path elements separator.
@ -85,7 +85,7 @@ bool SkinFactory::loadSkinFromData(const Skin &skin) {
}
if (!raw_data.isEmpty()) {
QString parsed_data = raw_data.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_folder);
const QString parsed_data = raw_data.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_folder);
qApp->setStyleSheet(parsed_data);
}
@ -96,11 +96,11 @@ void SkinFactory::setCurrentSkinName(const QString &skin_name) {
qApp->settings()->setValue(GROUP(GUI), GUI::Skin, skin_name);
}
QString SkinFactory::selectedSkinName() {
QString SkinFactory::selectedSkinName() const {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::Skin)).toString();
}
Skin SkinFactory::skinInfo(const QString &skin_name, bool *ok) {
Skin SkinFactory::skinInfo(const QString &skin_name, bool *ok) const {
Skin skin;
QString styles;
QFile skin_file(APP_SKIN_PATH + QDir::separator() + skin_name);
@ -114,7 +114,7 @@ Skin SkinFactory::skinInfo(const QString &skin_name, bool *ok) {
return skin;
}
QDomNode skin_node = dokument.namedItem(QSL("skin"));
const QDomNode skin_node = dokument.namedItem(QSL("skin"));
// Obtain visible skin name.
skin.m_visibleName = skin_node.namedItem(QSL("name")).toElement().text();
@ -155,7 +155,7 @@ Skin SkinFactory::skinInfo(const QString &skin_name, bool *ok) {
skin_file.close();
skin_file.deleteLater();
if (ok) {
if (ok != NULL) {
*ok = !skin.m_author.isEmpty() && !skin.m_version.isEmpty() &&
!skin.m_baseName.isEmpty() && !skin.m_email.isEmpty() &&
!skin.m_layoutMarkup.isEmpty();
@ -164,23 +164,22 @@ Skin SkinFactory::skinInfo(const QString &skin_name, bool *ok) {
return skin;
}
QList<Skin> SkinFactory::installedSkins() {
QList<Skin> SkinFactory::installedSkins() const {
QList<Skin> skins;
bool skin_load_ok;
QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs |
const QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs |
QDir::NoDotAndDotDot |
QDir::NoSymLinks |
QDir::Readable);
foreach (const QString &base_directory, skin_directories) {
// Check skins installed in this base directory.
QStringList skin_files = QDir(APP_SKIN_PATH + QDir::separator() + base_directory).entryList(QStringList() << QSL("*.xml"),
const QStringList skin_files = QDir(APP_SKIN_PATH + QDir::separator() + base_directory).entryList(QStringList() << QSL("*.xml"),
QDir::Files | QDir::Readable | QDir::NoDotAndDotDot | QDir::NoSymLinks);
foreach (const QString &skin_file, skin_files) {
// Check if skin file is valid and add it if it is valid.
Skin skin_info = skinInfo(base_directory + QDir::separator() + skin_file,
&skin_load_ok);
const Skin skin_info = skinInfo(base_directory + QDir::separator() + skin_file, &skin_load_ok);
if (skin_load_ok) {
skins.append(skin_info);

View File

@ -58,13 +58,13 @@ class SkinFactory : public QObject {
// Returns the name of the skin, that should be activated
// after application restart.
QString selectedSkinName();
QString selectedSkinName() const;
// Gets skin about a particular skin.
Skin skinInfo(const QString &skin_name, bool *ok = NULL);
Skin skinInfo(const QString &skin_name, bool *ok = NULL) const;
// Returns list of installed skins.
QList<Skin> installedSkins();
QList<Skin> installedSkins() const;
// Sets the desired skin as the active one if it exists.
void setCurrentSkinName(const QString &skin_name);

View File

@ -49,12 +49,12 @@ SystemFactory::SystemFactory(QObject *parent) : QObject(parent) {
SystemFactory::~SystemFactory() {
}
SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() {
SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() const {
// User registry way to auto-start the application on Windows.
#if defined(Q_OS_WIN)
QSettings registry_key(QSL("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
QSettings::NativeFormat);
bool autostart_enabled = registry_key.value(QSL(APP_LOW_NAME),
const bool autostart_enabled = registry_key.value(QSL(APP_LOW_NAME),
QString()).toString().replace(QL1C('\\'),
QL1C('/')) ==
Application::applicationFilePath();
@ -69,7 +69,7 @@ SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() {
// Use proper freedesktop.org way to auto-start the application on Linux.
// INFO: http://standards.freedesktop.org/autostart-spec/latest/
#elif defined(Q_OS_LINUX)
QString desktop_file_location = SystemFactory::getAutostartDesktopFileLocation();
const QString desktop_file_location = SystemFactory::getAutostartDesktopFileLocation();
// No correct path was found.
if (desktop_file_location.isEmpty()) {
@ -93,7 +93,7 @@ SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() {
#if defined(Q_OS_LINUX)
QString SystemFactory::getAutostartDesktopFileLocation() {
QString xdg_config_path(qgetenv("XDG_CONFIG_HOME"));
const QString xdg_config_path(qgetenv("XDG_CONFIG_HOME"));
QString desktop_file_location;
if (!xdg_config_path.isEmpty()) {
@ -103,7 +103,8 @@ QString SystemFactory::getAutostartDesktopFileLocation() {
}
else {
// Desired variable is not set, look for the default 'autostart' subdirectory.
QString home_directory(qgetenv("HOME"));
const QString home_directory(qgetenv("HOME"));
if (!home_directory.isEmpty()) {
// Home directory exists. Check if target .desktop file exists and
// return according status.
@ -117,7 +118,7 @@ QString SystemFactory::getAutostartDesktopFileLocation() {
#endif
bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) {
SystemFactory::AutoStartStatus current_status = SystemFactory::getAutoStartStatus();
const SystemFactory::AutoStartStatus current_status = SystemFactory::getAutoStartStatus();
// Auto-start feature is not even available, exit.
if (current_status == SystemFactory::Unavailable) {
@ -126,14 +127,17 @@ bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) {
#if defined(Q_OS_WIN)
QSettings registry_key(QSL("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"), QSettings::NativeFormat);
switch (new_status) {
case SystemFactory::Enabled:
registry_key.setValue(APP_LOW_NAME,
Application::applicationFilePath().replace(QL1C('/'), QL1C('\\')));
return true;
case SystemFactory::Disabled:
registry_key.remove(APP_LOW_NAME);
return true;
default:
return false;
}
@ -142,12 +146,13 @@ bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) {
// "rssguard.desktop" desktop file.
switch (new_status) {
case SystemFactory::Enabled:
QFile::link(QString(APP_DESKTOP_ENTRY_PATH) + '/' + APP_DESKTOP_ENTRY_FILE,
getAutostartDesktopFileLocation());
QFile::link(QString(APP_DESKTOP_ENTRY_PATH) + '/' + APP_DESKTOP_ENTRY_FILE, getAutostartDesktopFileLocation());
return true;
case SystemFactory::Disabled:
QFile::remove(getAutostartDesktopFileLocation());
return true;
default:
return false;
}
@ -186,7 +191,7 @@ QString SystemFactory::getUsername() const {
return name;
}
QPair<UpdateInfo, QNetworkReply::NetworkError> SystemFactory::checkForUpdates() {
QPair<UpdateInfo, QNetworkReply::NetworkError> SystemFactory::checkForUpdates() const {
QPair<UpdateInfo, QNetworkReply::NetworkError> result;
QByteArray releases_xml;
QByteArray changelog;
@ -206,8 +211,8 @@ bool SystemFactory::isUpdateNewer(const QString &update_version) {
QStringList new_version_tkn = update_version.split(QL1C('.'));
while (!current_version_tkn.isEmpty() && !new_version_tkn.isEmpty()) {
int current_number = current_version_tkn.takeFirst().toInt();
int new_number = new_version_tkn.takeFirst().toInt();
const int current_number = current_version_tkn.takeFirst().toInt();
const int new_number = new_version_tkn.takeFirst().toInt();
if (new_number > current_number) {
// New version is indeed higher thatn current version.
@ -233,10 +238,10 @@ bool SystemFactory::isUpdateNewer(const QString &update_version) {
}
}
UpdateInfo SystemFactory::parseUpdatesFile(const QByteArray &updates_file, const QByteArray &changelog) {
UpdateInfo SystemFactory::parseUpdatesFile(const QByteArray &updates_file, const QByteArray &changelog) const {
UpdateInfo update;
QDomDocument document; document.setContent(updates_file, false);
QDomNodeList releases = document.elementsByTagName(QSL("release"));
const QDomNodeList releases = document.elementsByTagName(QSL("release"));
if (releases.size() == 1) {
QDomElement rel_elem = releases.at(0).toElement();
@ -266,7 +271,7 @@ UpdateInfo SystemFactory::parseUpdatesFile(const QByteArray &updates_file, const
}
void SystemFactory::checkForUpdatesOnStartup() {
UpdateCheck updates = checkForUpdates();
const UpdateCheck updates = checkForUpdates();
if (updates.second == QNetworkReply::NoError && isUpdateNewer(updates.first.m_availableVersion)) {
qApp->showGuiMessage(tr("New version available"),

View File

@ -63,7 +63,7 @@ class SystemFactory : public QObject {
virtual ~SystemFactory();
// Returns current status of auto-start function.
SystemFactory::AutoStartStatus getAutoStartStatus();
SystemFactory::AutoStartStatus getAutoStartStatus() const;
// Sets new status for auto-start function.
// Function returns false if setting of
@ -84,13 +84,7 @@ class SystemFactory : public QObject {
QString getUsername() const;
// Tries to download list with new updates.
QPair<UpdateInfo, QNetworkReply::NetworkError> checkForUpdates();
// Check whether given pointer belongs to instance of given class or not.
template<typename Base, typename T>
static bool isInstanceOf(T *ptr) {
return dynamic_cast<Base*>(ptr) != NULL;
}
QPair<UpdateInfo, QNetworkReply::NetworkError> checkForUpdates() const;
// Checks if update is newer than current application version.
static bool isUpdateNewer(const QString &update_version);
@ -100,7 +94,7 @@ class SystemFactory : public QObject {
private:
// Performs parsing of downloaded file with list of updates.
UpdateInfo parseUpdatesFile(const QByteArray &updates_file, const QByteArray &changelog);
UpdateInfo parseUpdatesFile(const QByteArray &updates_file, const QByteArray &changelog) const;
};
#endif // SYSTEMFACTORY_H

View File

@ -35,12 +35,12 @@ TextFactory::TextFactory() {
}
int TextFactory::stringHeight(const QString &string, const QFontMetrics &metrics) {
int count_lines = string.split(QL1C('\n')).size();
const int count_lines = string.split(QL1C('\n')).size();
return metrics.height() * count_lines;
}
int TextFactory::stringWidth(const QString &string, const QFontMetrics &metrics) {
QStringList lines = string.split(QL1C('\n'));
const QStringList lines = string.split(QL1C('\n'));
int width = 0;
foreach (const QString &line, lines) {
@ -55,10 +55,10 @@ int TextFactory::stringWidth(const QString &string, const QFontMetrics &metrics)
}
QDateTime TextFactory::parseDateTime(const QString &date_time) {
QString input_date = date_time.simplified();
const QString input_date = date_time.simplified();
QDateTime dt;
QTime time_zone_offset;
QLocale locale(QLocale::C);
const QLocale locale(QLocale::C);
bool positive_time_zone_offset = false;
QStringList date_patterns; date_patterns << QSL("yyyy-MM-ddTHH:mm:ss") << QSL("MMM dd yyyy hh:mm:ss") <<

View File

@ -27,7 +27,7 @@
class TextFactory {
private:
// Constructors and destructors.
explicit TextFactory();
TextFactory();
public:
// Returns true if lhs is smaller than rhs if case-insensitive string comparison is used.