From d0f4cdcf6e5d53967ef40a902b857aeee90f5c4d Mon Sep 17 00:00:00 2001 From: vng Date: Sat, 25 Jun 2011 18:45:04 +0300 Subject: [PATCH] [Refactoring] Add BasePlatformImpl class for most Platform's member functions realization. @TODO: - Fix iPhone build; - Get most Platform's params from Settings; --- platform/platform.cpp | 89 +++++++++++++ platform/platform.hpp | 51 +++++--- platform/platform.pro | 1 + platform/platform_tests/platform_test.cpp | 6 +- platform/qtplatform.cpp | 148 ++++------------------ qt/widgets.cpp | 4 +- qt_tstfrm/tstwidgets.cpp | 4 +- 7 files changed, 156 insertions(+), 147 deletions(-) create mode 100644 platform/platform.cpp diff --git a/platform/platform.cpp b/platform/platform.cpp new file mode 100644 index 0000000000..668aad4738 --- /dev/null +++ b/platform/platform.cpp @@ -0,0 +1,89 @@ +#include "platform.hpp" + +#include "../coding/internal/file_data.hpp" + +#include "../base/logging.hpp" + +#include "../base/start_mem_debug.hpp" + + +string BasePlatformImpl::ReadPathForFile(string const & file) const +{ + string fullPath = m_writableDir + file; + if (!IsFileExists(fullPath)) + { + fullPath = m_resourcesDir + file; + if (!IsFileExists(fullPath)) + MYTHROW(FileAbsentException, ("File doesn't exist", fullPath)); + } + return fullPath; +} + +bool BasePlatformImpl::GetFileSize(string const & file, uint64_t & size) const +{ + return my::GetFileSize(file, size); +} + +void BasePlatformImpl::GetFontNames(FilesList & res) const +{ + res.clear(); + GetFilesInDir(m_resourcesDir, "*.ttf", res); + + sort(res.begin(), res.end()); + + for (size_t i = 0; i < res.size(); ++i) + res[i] = m_resourcesDir + res[i]; +} + +double BasePlatformImpl::VisualScale() const +{ + return 1.0; +} + +string BasePlatformImpl::SkinName() const +{ + return "basic.skn"; +} + +bool BasePlatformImpl::IsMultiSampled() const +{ + return true; +} + +bool BasePlatformImpl::DoPeriodicalUpdate() const +{ + return true; +} + +double BasePlatformImpl::PeriodicalUpdateInterval() const +{ + return 0.3; +} + +bool BasePlatformImpl::IsBenchmarking() const +{ + bool res = false; +#ifndef OMIM_PRODUCTION + if (res) + { + static bool first = true; + if (first) + { + LOG(LCRITICAL, ("benchmarking only defined in production configuration")); + first = false; + } + res = false; + } +#endif + return res; +} + +bool BasePlatformImpl::IsVisualLog() const +{ + return false; +} + +int BasePlatformImpl::ScaleEtalonSize() const +{ + return 512 + 256; +} diff --git a/platform/platform.hpp b/platform/platform.hpp index 93c6e10228..ac5184edb5 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -6,10 +6,10 @@ #include "../std/vector.hpp" #include "../std/utility.hpp" -#include "../base/start_mem_debug.hpp" DECLARE_EXCEPTION(FileAbsentException, RootException); + class Platform { public: @@ -23,18 +23,13 @@ public: return WritableDir() + file; } + /// @return resource dir (on some platforms it's differ from Writable dir) virtual string ResourcesDir() const = 0; /// Throws FileAbsentException /// @param[in] file just file name which we want to read - /// @return fullPath fully resolved path including file name - virtual string ReadPathForFile(char const * file) const = 0; - /// Throws FileAbsentException - /// @return fullPath fully resolved path including file name - string ReadPathForFile(string const & file) const - { - return ReadPathForFile(file.c_str()); - } + /// @return fully resolved path including file name + virtual string ReadPathForFile(string const & file) const = 0; /// @name File operations //@{ @@ -43,12 +38,12 @@ public: /// @param directory directory path with slash at the end /// @param mask files extension to find, like ".map" etc /// @return number of files found in outFiles - virtual int GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const = 0; + virtual void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const = 0; /// @return false if file is not exist virtual bool GetFileSize(string const & file, uint64_t & size) const = 0; /// Renamed to avoid conflict with Windows macroses virtual bool RenameFileX(string const & original, string const & newName) const = 0; - /// Simple check + /// Simple file existing check bool IsFileExists(string const & file) const { uint64_t dummy; @@ -60,7 +55,7 @@ public: virtual double VisualScale() const = 0; - virtual string const SkinName() const = 0; + virtual string SkinName() const = 0; virtual bool IsMultiSampled() const = 0; @@ -68,17 +63,39 @@ public: virtual double PeriodicalUpdateInterval() const = 0; - virtual vector GetFontNames() const = 0; + virtual void GetFontNames(FilesList & res) const = 0; virtual bool IsBenchmarking() const = 0; virtual bool IsVisualLog() const = 0; - virtual string const DeviceID() const = 0; + virtual string DeviceID() const = 0; - virtual unsigned ScaleEtalonSize() const = 0; + virtual int ScaleEtalonSize() const = 0; +}; + +class BasePlatformImpl : public Platform +{ +protected: + string m_writableDir, m_resourcesDir; + +public: + virtual string WritableDir() const { return m_writableDir; } + virtual string ResourcesDir() const { return m_resourcesDir; } + virtual string ReadPathForFile(string const & file) const; + + virtual bool GetFileSize(string const & file, uint64_t & size) const; + + virtual void GetFontNames(FilesList & res) const; + + virtual double VisualScale() const; + virtual string SkinName() const; + virtual bool IsMultiSampled() const; + virtual bool DoPeriodicalUpdate() const; + virtual double PeriodicalUpdateInterval() const; + virtual bool IsBenchmarking() const; + virtual bool IsVisualLog() const; + virtual int ScaleEtalonSize() const; }; extern "C" Platform & GetPlatform(); - -#include "../base/stop_mem_debug.hpp" diff --git a/platform/platform.pro b/platform/platform.pro index 7a5f8b7410..2889e7ab9f 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -57,3 +57,4 @@ SOURCES += \ location_manager.cpp \ preferred_languages.cpp \ settings.cpp \ + platform.cpp \ diff --git a/platform/platform_tests/platform_test.cpp b/platform/platform_tests/platform_test.cpp index 7fcd08b03b..90fb92e02f 100644 --- a/platform/platform_tests/platform_test.cpp +++ b/platform/platform_tests/platform_test.cpp @@ -54,9 +54,11 @@ UNIT_TEST(GetFilesInDir) { Platform & pl = GetPlatform(); Platform::FilesList files; - TEST_GREATER(pl.GetFilesInDir(pl.WritableDir(), "*" DATA_FILE_EXTENSION, files), 0, ("/data/ folder should contain some data files")); - TEST_EQUAL(pl.GetFilesInDir(pl.WritableDir(), "asdnonexistentfile.dsa", files), 0, ()); + pl.GetFilesInDir(pl.WritableDir(), "*" DATA_FILE_EXTENSION, files); + TEST_GREATER(files.size(), 0, ("/data/ folder should contain some data files")); + + pl.GetFilesInDir(pl.WritableDir(), "asdnonexistentfile.dsa", files); TEST_EQUAL(files.size(), 0, ()); } diff --git a/platform/qtplatform.cpp b/platform/qtplatform.cpp index 288755f96f..0d2fcfe8f6 100644 --- a/platform/qtplatform.cpp +++ b/platform/qtplatform.cpp @@ -161,52 +161,54 @@ static bool IsDirectoryWritable(string const & dir) } //////////////////////////////////////////////////////////////////////////////////////// -class QtPlatform : public Platform +class QtPlatform : public BasePlatformImpl { - string m_writableDir; - string m_resourcesDir; + static bool IsDirExists(string const & file) + { + QFileInfo fileInfo(file.c_str()); + return fileInfo.exists(); + } /// Scans all upper directories for the presence of given directory /// @param[in] startPath full path to lowest file in hierarchy (usually binary) /// @param[in] dirName directory name we want to be present /// @return if not empty, contains full path to existing directory - string DirFinder(string const & startPath, string const & dirName) + static string DirFinder(string const & startPath, string dirName) { + dirName = DIR_SLASH + dirName + DIR_SLASH; + size_t slashPos = startPath.size(); - while ((slashPos = startPath.rfind(DIR_SLASH, slashPos - 1)) != string::npos) + while (slashPos > 0 && (slashPos = startPath.rfind(DIR_SLASH, slashPos - 1)) != string::npos) { - string const dir = startPath.substr(0, slashPos) + DIR_SLASH + dirName + DIR_SLASH; - if (IsFileExists(dir)) + string const dir = startPath.substr(0, slashPos) + dirName; + if (IsDirExists(dir)) return dir; - if (slashPos == 0) - break; } return string(); } - bool GetOSSpecificResourcesDir(string const & exePath, string & dir) + static bool GetOSSpecificResourcesDir(string const & exePath, string & dir) { dir = DirFinder(exePath, RESOURCES_DIR); return !dir.empty(); } - void InitResourcesDir(string & dir) + static void InitResourcesDir(string & dir) { // Resources dir can be any "data" folder found in the nearest upper directory, // where all necessary resources files are present and accessible string exePath; CHECK( GetPathToBinary(exePath), ("Can't get full path to executable") ); dir = DirFinder(exePath, MAPDATA_DIR); - if (!dir.empty()) + if (dir.empty()) { - // @TODO: check if all necessary resources are present in found dir - return; + CHECK( GetOSSpecificResourcesDir(exePath, dir), ("Can't retrieve resources directory") ); } - // retrieve OS-specific resources dir - CHECK( GetOSSpecificResourcesDir(exePath, dir), ("Can't retrieve resources directory") ); + + /// @todo Check all necessary files } - void InitWritableDir(string & dir) + static void InitWritableDir(string & dir) { // Writable dir can be any "data" folder found in the nearest upper directory // ./data - For Windows portable builds @@ -218,7 +220,7 @@ class QtPlatform : public Platform string path; CHECK( GetPathToBinary(path), ("Can't get full path to executable") ); dir = DirFinder(path, MAPDATA_DIR); - if (!(!dir.empty() && IsDirectoryWritable(dir))) + if (dir.empty() || !IsDirectoryWritable(dir)) { CHECK( GetUserWritableDir(dir), ("Can't get User's Application Data writable directory") ); } @@ -231,31 +233,7 @@ public: InitResourcesDir(m_resourcesDir); } - /// @return path to /data/ with ending slash - virtual string WritableDir() const - { - return m_writableDir; - } - - /// @return path to /data/ with ending slash - virtual string ResourcesDir() const - { - return m_resourcesDir; - } - - virtual string ReadPathForFile(char const * file) const - { - string fullPath = m_writableDir + file; - if (!IsFileExists(fullPath)) - { - fullPath = m_resourcesDir + file; - if (!IsFileExists(fullPath)) - MYTHROW(FileAbsentException, ("File doesn't exist", fullPath)); - } - return fullPath; - } - - virtual int GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const + virtual void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const { outFiles.clear(); QDir dir(directory.c_str(), mask.c_str(), QDir::Unsorted, @@ -263,18 +241,6 @@ public: int const count = dir.count(); for (int i = 0; i < count; ++i) outFiles.push_back(dir[i].toUtf8().data()); - return count; - } - - virtual bool GetFileSize(string const & file, uint64_t & size) const - { - QFileInfo fileInfo(file.c_str()); - if (fileInfo.exists()) - { - size = fileInfo.size(); - return true; - } - return false; } virtual bool RenameFileX(string const & original, string const & newName) const @@ -314,80 +280,10 @@ public: return 1; } - double VisualScale() const - { - return 1.0; - } - - string const SkinName() const - { - return "basic.skn"; - } - - bool IsMultiSampled() const - { - return true; - } - - bool DoPeriodicalUpdate() const - { - return true; - } - - double PeriodicalUpdateInterval() const - { - return 0.3; - } - - vector GetFontNames() const - { - vector res; - - string fontFolder = m_resourcesDir; - //string fontFolder = "/Library/Fonts/"; - - GetFilesInDir(fontFolder, "*.ttf", res); - - sort(res.begin(), res.end()); - - for (size_t i = 0; i < res.size(); ++i) - res[i] = fontFolder + res[i]; - - return res; - } - - bool IsBenchmarking() const - { - bool res = false; -#ifndef OMIM_PRODUCTION - if (res) - { - static bool first = true; - if (first) - { - LOG(LCRITICAL, ("benchmarking only defined in production configuration")); - first = false; - } - res = false; - } -#endif - return res; - } - - string const DeviceID() const + string QtPlatform::DeviceID() const { return "DesktopVersion"; } - - bool IsVisualLog() const - { - return false; - } - - unsigned ScaleEtalonSize() const - { - return 512 + 256; - } }; extern "C" Platform & GetPlatform() diff --git a/qt/widgets.cpp b/qt/widgets.cpp index 8eb2d5a1b8..537651b6b7 100644 --- a/qt/widgets.cpp +++ b/qt/widgets.cpp @@ -59,7 +59,9 @@ namespace qt !yg::gl::g_isBufferObjectsSupported, !GetPlatform().IsMultiSampled())); - m_resourceManager->addFonts(GetPlatform().GetFontNames()); + Platform::FilesList fonts; + GetPlatform().GetFontNames(fonts); + m_resourceManager->addFonts(fonts); DrawerYG::params_t p; diff --git a/qt_tstfrm/tstwidgets.cpp b/qt_tstfrm/tstwidgets.cpp index e8967bcabc..ae51956a4c 100644 --- a/qt_tstfrm/tstwidgets.cpp +++ b/qt_tstfrm/tstwidgets.cpp @@ -65,7 +65,9 @@ void GLDrawWidget::initializeGL() !yg::gl::g_isBufferObjectsSupported, !GetPlatform().IsMultiSampled())); - m_resourceManager->addFonts(GetPlatform().GetFontNames()); + Platform::FilesList fonts; + GetPlatform().GetFontNames(fonts); + m_resourceManager->addFonts(fonts); m_frameBuffer = make_shared_ptr(new yg::gl::FrameBuffer());