From 74639a7d3db23d5281604a5caca5c08f23381259 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 26 Jan 2018 15:40:52 +0100 Subject: [PATCH] feat(app): replace some define by constexpr (modern cpp syntax) --- src/app/App.cpp | 44 ++++++++++--------- src/app/AppController.cpp | 24 +++++----- src/app/providers/ImageProvider.cpp | 10 +++-- src/components/other/colors/Colors.cpp | 12 ++--- .../sip-addresses/SipAddressesProxyModel.cpp | 24 +++++----- src/components/sound-player/SoundPlayer.cpp | 8 ++-- src/utils/Utils.cpp | 10 ++--- 7 files changed, 71 insertions(+), 61 deletions(-) diff --git a/src/app/App.cpp b/src/app/App.cpp index 15fd5ae5d..bc2e1726c 100644 --- a/src/app/App.cpp +++ b/src/app/App.cpp @@ -45,25 +45,27 @@ #include "App.hpp" -#define DEFAULT_LOCALE "en" - -#define LANGUAGES_PATH ":/languages/" - -// The main windows of Linphone desktop. -#define QML_VIEW_MAIN_WINDOW "qrc:/ui/views/App/Main/MainWindow.qml" -#define QML_VIEW_CALLS_WINDOW "qrc:/ui/views/App/Calls/CallsWindow.qml" -#define QML_VIEW_SETTINGS_WINDOW "qrc:/ui/views/App/Settings/SettingsWindow.qml" - -#define QML_VIEW_SPLASH_SCREEN "qrc:/ui/views/App/SplashScreen/SplashScreen.qml" - -#define VERSION_UPDATE_CHECK_INTERVAL 86400000 // 24 hours in milliseconds. - using namespace std; // ============================================================================= +namespace { + constexpr char cDefaultLocale[] = "en"; + + constexpr char cLanguagePath[] = ":/languages/"; + + // The main windows of Linphone desktop. + constexpr char cQmlViewMainWindow[] = "qrc:/ui/views/App/Main/MainWindow.qml"; + constexpr char cQmlViewCallsWindow[] = "qrc:/ui/views/App/Calls/CallsWindow.qml"; + constexpr char cQmlViewSettingsWindow[] = "qrc:/ui/views/App/Settings/SettingsWindow.qml"; + + constexpr char cQmlViewSplashScreen[] = "qrc:/ui/views/App/SplashScreen/SplashScreen.qml"; + + constexpr int cVersionUpdateCheckInterval = 86400000; // 24 hours in milliseconds. +} + static inline bool installLocale (App &app, QTranslator &translator, const QLocale &locale) { - return translator.load(locale, LANGUAGES_PATH) && app.installTranslator(&translator); + return translator.load(locale, cLanguagePath) && app.installTranslator(&translator); } static inline shared_ptr getConfigIfExists (const QCommandLineParser &parser) { @@ -89,7 +91,7 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U Logger::getInstance()->setVerbose(true); // List available locales. - for (const auto &locale : QDir(LANGUAGES_PATH).entryList()) + for (const auto &locale : QDir(cLanguagePath).entryList()) mAvailableLocales << QLocale(locale); // Init locale. @@ -138,7 +140,7 @@ static QQuickWindow *createSubWindow (QQmlApplicationEngine *engine, const char static void activeSplashScreen (QQmlApplicationEngine *engine) { qInfo() << QStringLiteral("Open splash screen..."); - QQuickWindow *splashScreen = ::createSubWindow(engine, QML_VIEW_SPLASH_SCREEN); + QQuickWindow *splashScreen = ::createSubWindow(engine, cQmlViewSplashScreen); QObject::connect(CoreManager::getInstance()->getHandlers().get(), &CoreHandlers::coreStarted, splashScreen, [splashScreen] { splashScreen->close(); splashScreen->deleteLater(); @@ -240,7 +242,7 @@ void App::initContentApp () { // Load main view. qInfo() << QStringLiteral("Loading main view..."); - mEngine->load(QUrl(QML_VIEW_MAIN_WINDOW)); + mEngine->load(QUrl(cQmlViewMainWindow)); if (mEngine->rootObjects().isEmpty()) qFatal("Unable to open main window."); @@ -281,7 +283,7 @@ QString App::getCommandArgument () { QQuickWindow *App::getCallsWindow () { if (!mCallsWindow) - mCallsWindow = ::createSubWindow(mEngine, QML_VIEW_CALLS_WINDOW); + mCallsWindow = ::createSubWindow(mEngine, cQmlViewCallsWindow); return mCallsWindow; } @@ -294,7 +296,7 @@ QQuickWindow *App::getMainWindow () const { QQuickWindow *App::getSettingsWindow () { if (!mSettingsWindow) { - mSettingsWindow = ::createSubWindow(mEngine, QML_VIEW_SETTINGS_WINDOW); + mSettingsWindow = ::createSubWindow(mEngine, cQmlViewSettingsWindow); QObject::connect(mSettingsWindow, &QWindow::visibilityChanged, this, [](QWindow::Visibility visibility) { if (visibility == QWindow::Hidden) { qInfo() << QStringLiteral("Update nat policy."); @@ -514,7 +516,7 @@ void App::initLocale (const shared_ptr &config) { } // Use english. - mLocale = DEFAULT_LOCALE; + mLocale = cDefaultLocale; if (!::installLocale(*this, *mTranslator, QLocale(mLocale))) qFatal("Unable to install default translator."); } @@ -570,7 +572,7 @@ void App::openAppAfterInit (bool mustBeIconified) { #ifdef ENABLE_UPDATE_CHECK QTimer *timer = new QTimer(mEngine); - timer->setInterval(VERSION_UPDATE_CHECK_INTERVAL); + timer->setInterval(cVersionUpdateCheckInterval); QObject::connect(timer, &QTimer::timeout, this, &App::checkForUpdate); timer->start(); diff --git a/src/app/AppController.cpp b/src/app/AppController.cpp index 711e1a9b1..e3af58bd1 100644 --- a/src/app/AppController.cpp +++ b/src/app/AppController.cpp @@ -29,19 +29,21 @@ #include "AppController.hpp" -// Must be unique. Used by `SingleApplication` and `Paths`. -#define APPLICATION_NAME "linphone" -#define APPLICATION_VERSION LINPHONE_QT_GIT_VERSION -#define APPLICATION_MINIMAL_QT_VERSION "5.9.0" - -#define DEFAULT_FONT "Noto Sans" - using namespace std; // ============================================================================= +namespace { + // Must be unique. Used by `SingleApplication` and `Paths`. + constexpr char cApplicationName[] = "linphone"; + constexpr char cApplicationVersion[] = LINPHONE_QT_GIT_VERSION; + constexpr char cApplicationMinimalQtVersion[] = "5.9.0"; + + constexpr char cDefaultFont[] = "Noto Sans"; +} + AppController::AppController (int &argc, char *argv[]) { - QT_REQUIRE_VERSION(argc, argv, APPLICATION_MINIMAL_QT_VERSION); + QT_REQUIRE_VERSION(argc, argv, cApplicationMinimalQtVersion); Q_ASSERT(!mApp); // Disable QML cache. Avoid malformed cache. @@ -82,8 +84,8 @@ AppController::AppController (int &argc, char *argv[]) { // App creation. // --------------------------------------------------------------------------- - QCoreApplication::setApplicationName(APPLICATION_NAME); - QCoreApplication::setApplicationVersion(APPLICATION_VERSION); + QCoreApplication::setApplicationName(cApplicationName); + QCoreApplication::setApplicationVersion(cApplicationVersion); mApp = new App(argc, argv); QQuickStyle::setStyle("Default"); @@ -113,7 +115,7 @@ AppController::AppController (int &argc, char *argv[]) { } } - mApp->setFont(QFont(DEFAULT_FONT)); + mApp->setFont(QFont(cDefaultFont)); } AppController::~AppController () { diff --git a/src/app/providers/ImageProvider.cpp b/src/app/providers/ImageProvider.cpp index 91680a23a..e10873859 100644 --- a/src/app/providers/ImageProvider.cpp +++ b/src/app/providers/ImageProvider.cpp @@ -29,13 +29,15 @@ #include "ImageProvider.hpp" -// Max image size in bytes. (100Kb) -#define MAX_IMAGE_SIZE 102400 - using namespace std; // ============================================================================= +namespace { + // Max image size in bytes. (100Kb) + constexpr size_t cMaxImageSize = 102400; +} + static void removeAttribute (QXmlStreamAttributes &readerAttributes, const QString &name) { auto it = find_if(readerAttributes.cbegin(), readerAttributes.cend(), [&name](const QXmlStreamAttribute &attribute) { return name == attribute.name() && !attribute.prefix().length(); @@ -250,7 +252,7 @@ QImage ImageProvider::requestImage (const QString &id, QSize *size, const QSize // 1. Read and update XML content. QFile file(path); - if (Q_UNLIKELY(QFileInfo(file).size() > MAX_IMAGE_SIZE)) { + if (Q_UNLIKELY(QFileInfo(file).size() > cMaxImageSize)) { qWarning() << QStringLiteral("Unable to open large file: `%1`.").arg(path); return QImage(); } diff --git a/src/components/other/colors/Colors.cpp b/src/components/other/colors/Colors.cpp index a8f44ce1b..dfb4f7984 100644 --- a/src/components/other/colors/Colors.cpp +++ b/src/components/other/colors/Colors.cpp @@ -26,22 +26,22 @@ #include "Colors.hpp" -#define COLORS_SECTION "ui_colors" - #if LINPHONE_FRIDAY -#include + #include #endif // if LINPHONE_FRIDAY using namespace std; // ============================================================================= -#if LINPHONE_FRIDAY +namespace { + constexpr char cColorsSection[] = "ui_colors"; +} +#if LINPHONE_FRIDAY static inline bool isLinphoneFriday () { return QDate::currentDate().dayOfWeek() == 5; } - #endif // if LINPHONE_FRIDAY Colors::Colors (QObject *parent) : QObject(parent) { @@ -74,7 +74,7 @@ void Colors::overrideColors (const shared_ptr &config) { for (int i = info->propertyOffset(); i < info->propertyCount(); ++i) { const QMetaProperty metaProperty = info->property(i); const string colorName = metaProperty.name(); - const string colorValue = config->getString(COLORS_SECTION, colorName, ""); + const string colorValue = config->getString(cColorsSection, colorName, ""); if (!colorValue.empty()) setProperty(colorName.c_str(), QColor(::Utils::coreStringToAppString(colorValue))); diff --git a/src/components/sip-addresses/SipAddressesProxyModel.cpp b/src/components/sip-addresses/SipAddressesProxyModel.cpp index 29abfab33..563ae3a5b 100644 --- a/src/components/sip-addresses/SipAddressesProxyModel.cpp +++ b/src/components/sip-addresses/SipAddressesProxyModel.cpp @@ -24,14 +24,16 @@ #include "SipAddressesProxyModel.hpp" -#define WEIGHT_POS_0 5 -#define WEIGHT_POS_1 4 -#define WEIGHT_POS_2 3 -#define WEIGHT_POS_3 2 -#define WEIGHT_POS_OTHER 1 - // ============================================================================= +namespace { + constexpr int cWeightPos0 = 5; + constexpr int cWeightPos1 = 4; + constexpr int cWeightPos2 = 3; + constexpr int cWeightPos3 = 2; + constexpr int cWeightPosOther = 1; +} + const QRegExp SipAddressesProxyModel::mSearchSeparators("^[^_.-;@ ][_.-;@ ]"); // ----------------------------------------------------------------------------- @@ -116,12 +118,12 @@ int SipAddressesProxyModel::computeStringWeight (const QString &string) const { switch (offset) { case -1: return 0; - case 0: return WEIGHT_POS_0; - case 1: return WEIGHT_POS_1; - case 2: return WEIGHT_POS_2; - case 3: return WEIGHT_POS_3; + case 0: return cWeightPos0; + case 1: return cWeightPos1; + case 2: return cWeightPos2; + case 3: return cWeightPos3; default: break; } - return WEIGHT_POS_OTHER; + return cWeightPosOther; } diff --git a/src/components/sound-player/SoundPlayer.cpp b/src/components/sound-player/SoundPlayer.cpp index e9562d417..58f839ecd 100644 --- a/src/components/sound-player/SoundPlayer.cpp +++ b/src/components/sound-player/SoundPlayer.cpp @@ -27,12 +27,14 @@ #include "SoundPlayer.hpp" -#define FORCE_CLOSE_TIMER_INTERVAL 20 - using namespace std; // ============================================================================= +namespace { + int cForceCloseTimerInterval = 20; +} + class SoundPlayer::Handlers : public linphone::PlayerListener { public: Handlers (SoundPlayer *soundPlayer) { @@ -58,7 +60,7 @@ private: SoundPlayer::SoundPlayer (QObject *parent) : QObject(parent) { mForceCloseTimer = new QTimer(this); - mForceCloseTimer->setInterval(FORCE_CLOSE_TIMER_INTERVAL); + mForceCloseTimer->setInterval(cForceCloseTimerInterval); QObject::connect(mForceCloseTimer, &QTimer::timeout, this, &SoundPlayer::handleEof); diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index aac172e50..c8b3603bd 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -26,6 +26,10 @@ // ============================================================================= +namespace { + constexpr int cSafeFilePathLimit = 100; +} + char *Utils::rstrstr (const char *a, const char *b) { size_t a_len = strlen(a); size_t b_len = strlen(b); @@ -43,8 +47,6 @@ char *Utils::rstrstr (const char *a, const char *b) { // ----------------------------------------------------------------------------- -#define SAFE_FILE_PATH_LIMIT 100 - QString Utils::getSafeFilePath (const QString &filePath, bool *soFarSoGood) { if (soFarSoGood) *soFarSoGood = true; @@ -56,7 +58,7 @@ QString Utils::getSafeFilePath (const QString &filePath, bool *soFarSoGood) { const QString prefix = QStringLiteral("%1/%2").arg(info.absolutePath()).arg(info.baseName()); const QString ext = info.completeSuffix(); - for (int i = 1; i < SAFE_FILE_PATH_LIMIT; ++i) { + for (int i = 1; i < cSafeFilePathLimit; ++i) { QString safePath = QStringLiteral("%1 (%3).%4").arg(prefix).arg(i).arg(ext); if (!QFileInfo::exists(safePath)) return safePath; @@ -67,5 +69,3 @@ QString Utils::getSafeFilePath (const QString &filePath, bool *soFarSoGood) { return QString(""); } - -#undef SAFE_FILE_PATH_LIMIT