Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Zolotarev <deathbaba@gmail.com>2011-10-02 02:40:11 +0400
committerAlex Zolotarev <alex@maps.me>2015-09-23 01:24:55 +0300
commitd6b9bf5525a12ab96d16785c6c72b863f519a9ab (patch)
tree34d417e39a83505d89ac672e7b9f37bfd0a4d96f /platform
parent0345b017ffee30c4504cbd91ba0d32be6fec826e (diff)
[ios][qt] Platform refactoring
Diffstat (limited to 'platform')
-rw-r--r--platform/platform.cpp149
-rw-r--r--platform/platform.hpp73
-rw-r--r--platform/platform.pro53
-rw-r--r--platform/platform_ios.mm195
-rw-r--r--platform/platform_linux.cpp43
-rw-r--r--platform/platform_mac.mm89
-rw-r--r--platform/platform_qt.cpp184
-rw-r--r--platform/platform_win.cpp47
-rw-r--r--platform/qtplatform.cpp293
9 files changed, 602 insertions, 524 deletions
diff --git a/platform/platform.cpp b/platform/platform.cpp
deleted file mode 100644
index 465a76e554..0000000000
--- a/platform/platform.cpp
+++ /dev/null
@@ -1,149 +0,0 @@
-#include "platform.hpp"
-#include "settings.hpp"
-
-#include "../coding/internal/file_data.hpp"
-#include "../coding/file_reader.hpp"
-
-#include "../base/logging.hpp"
-
-#if !defined(OMIM_OS_WINDOWS_NATIVE) && !defined(OMIM_OS_BADA)
-#include <unistd.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#endif
-
-#include "../base/start_mem_debug.hpp"
-
-
-string ReadPathForFile(string const & writableDir,
- string const & resourcesDir,
- string const & file)
-{
- string fullPath = writableDir + file;
- if (!GetPlatform().IsFileExists(fullPath))
- {
- fullPath = resourcesDir + file;
- if (!GetPlatform().IsFileExists(fullPath))
- MYTHROW(FileAbsentException, ("File doesn't exist", fullPath));
- }
- return fullPath;
-}
-
-ModelReader * BasePlatformImpl::GetReader(string const & file) const
-{
- return new FileReader(ReadPathForFile(m_writableDir, m_resourcesDir, file), 10, 12);
-}
-
-bool BasePlatformImpl::GetFileSize(string const & file, uint64_t & size) const
-{
- return my::GetFileSize(file, size);
-}
-
-void BasePlatformImpl::GetFilesInDir(string const & directory, string const & mask, FilesList & res) const
-{
-#if !defined(OMIM_OS_WINDOWS_NATIVE) && !defined(OMIM_OS_BADA)
- DIR * dir;
- struct dirent * entry;
-
- if ((dir = opendir(directory.c_str())) == NULL)
- return;
-
- // TODO: take wildcards into account...
- string mask_fixed = mask;
- if (mask_fixed.size() && mask_fixed[0] == '*')
- mask_fixed.erase(0, 1);
-
- do
- {
- if ((entry = readdir(dir)) != NULL)
- {
- string fname(entry->d_name);
- size_t index = fname.rfind(mask_fixed);
- if (index != string::npos && index == fname.size() - mask_fixed.size())
- {
- // TODO: By some strange reason under simulator stat returns -1,
- // may be because of symbolic links?..
- //struct stat fileStatus;
- //if (stat(string(directory + fname).c_str(), &fileStatus) == 0 &&
- // (fileStatus.st_mode & S_IFDIR) == 0)
- //{
- res.push_back(fname);
- //}
- }
- }
- } while (entry != NULL);
-
- closedir(dir);
-#else
- MYTHROW(NotImplementedException, ("Function not implemented"));
-#endif
-}
-
-bool BasePlatformImpl::RenameFileX(string const & fOld, string const & fNew) const
-{
- return my::RenameFileX(fOld, fNew);
-}
-
-void BasePlatformImpl::GetFontNames(FilesList & res) const
-{
- res.clear();
- GetFilesInDir(m_resourcesDir, "*.ttf", res);
-
- sort(res.begin(), res.end());
-}
-
-double BasePlatformImpl::VisualScale() const
-{
- return 1.0;
-}
-
-string BasePlatformImpl::SkinName() const
-{
- return "basic.skn";
-}
-
-bool BasePlatformImpl::IsBenchmarking() const
-{
- bool res = false;
- (void)Settings::Get("IsBenchmarking", res);
-
-/*#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;
-}
-
-int BasePlatformImpl::MaxTilesCount() const
-{
- return 120;
-}
-
-int BasePlatformImpl::TileSize() const
-{
- return 256;
-}
-
-bool BasePlatformImpl::IsMultiThreadedRendering() const
-{
- return true;
-}
diff --git a/platform/platform.hpp b/platform/platform.hpp
index aefca3e0ff..5bed8e580e 100644
--- a/platform/platform.hpp
+++ b/platform/platform.hpp
@@ -8,31 +8,31 @@
#include "../std/vector.hpp"
#include "../std/utility.hpp"
-
DECLARE_EXCEPTION(FileAbsentException, RootException);
DECLARE_EXCEPTION(NotImplementedException, RootException);
-
class Platform
{
+ string m_writableDir, m_resourcesDir;
+ class PlatformImpl;
+ PlatformImpl * m_impl;
+
public:
- virtual ~Platform() {}
+ Platform();
+ ~Platform();
/// @return always the same writable dir for current user with slash at the end
- virtual string WritableDir() const = 0;
+ string WritableDir() const { return m_writableDir; }
/// @return full path to file in user's writable directory
- string WritablePathForFile(string const & file) const
- {
- return WritableDir() + file;
- }
+ string WritablePathForFile(string const & file) const { return WritableDir() + file; }
/// @return resource dir (on some platforms it's differ from Writable dir)
- virtual string ResourcesDir() const = 0;
+ string ResourcesDir() const { return m_resourcesDir; }
/// @return reader for file decriptor.
/// @throws FileAbsentException
/// @param[in] file descriptor which we want to read
- virtual ModelReader * GetReader(string const & file) const = 0;
+ ModelReader * GetReader(string const & file) const;
/// @name File operations
//@{
@@ -41,11 +41,9 @@ 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 void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const = 0;
+ void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const;
/// @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;
+ bool GetFileSize(string const & file, uint64_t & size) const;
/// Simple file existing check
bool IsFileExists(string const & file) const
{
@@ -54,52 +52,25 @@ public:
}
//@}
- virtual int CpuCores() const = 0;
-
- virtual double VisualScale() const = 0;
+ int CpuCores() const;
- virtual string SkinName() const = 0;
+ double VisualScale() const;
- virtual void GetFontNames(FilesList & res) const = 0;
+ string SkinName() const;
- virtual bool IsBenchmarking() const = 0;
+ void GetFontNames(FilesList & res) const;
- virtual bool IsMultiThreadedRendering() const = 0;
+ bool IsMultiThreadedRendering() const;
- virtual int TileSize() const = 0;
+ int TileSize() const;
- virtual int MaxTilesCount() const = 0;
+ int MaxTilesCount() const;
- virtual bool IsVisualLog() const = 0;
+ string DeviceName() const;
- virtual string DeviceID() const = 0;
+ int ScaleEtalonSize() const;
- 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 ModelReader * GetReader(string const & file) const;
-
- virtual void GetFilesInDir(string const & directory, string const & mask, FilesList & res) const;
- virtual bool GetFileSize(string const & file, uint64_t & size) const;
- virtual bool RenameFileX(string const & fOld, string const & fNew) const;
- virtual void GetFontNames(FilesList & res) const;
-
- virtual double VisualScale() const;
- virtual string SkinName() const;
- virtual bool IsBenchmarking() const;
- virtual bool IsVisualLog() const;
- virtual bool IsMultiThreadedRendering() const;
- virtual int ScaleEtalonSize() const;
- virtual int TileSize() const;
- virtual int MaxTilesCount() const;
+ string UniqueClientId() const;
};
extern "C" Platform & GetPlatform();
diff --git a/platform/platform.pro b/platform/platform.pro
index 0f0d716a09..5182c6011e 100644
--- a/platform/platform.pro
+++ b/platform/platform.pro
@@ -11,43 +11,35 @@ include($$ROOT_DIR/common.pri)
QT *= core network
-!iphone*:!android* {
+!iphone*:!android*:!bada {
INCLUDEPATH += $$ROOT_DIR/3party/jansson/src
- SOURCES += \
- qtplatform.cpp \
- wifi_location_service.cpp \
- qt_download_manager.cpp \
- qt_download.cpp \
- qt_concurrent_runner.cpp \
-
- HEADERS += \
- qt_download_manager.hpp \
- qt_download.hpp \
- wifi_info.hpp
+ SOURCES += platform_qt.cpp \
+ wifi_location_service.cpp \
+ qt_download_manager.cpp \
+ qt_download.cpp \
+ qt_concurrent_runner.cpp
+ HEADERS += qt_download_manager.hpp \
+ qt_download.hpp \
+ wifi_info.hpp
+ win32* {
+ SOURCES += platform_win.cpp \
+ wifi_info_windows.cpp
+ } else:macx* {
+ OBJECTIVE_SOURCES += platform_mac.mm \
+ wifi_info_mac.mm \
+ apple_video_timer.mm
+ } else:linux* {
+ SOURCES += platform_linux.cpp
+ }
} else:iphone* {
- SOURCES += ios_concurrent_runner.mm
+ OBJECTIVE_SOURCES += ios_video_timer.mm \
+ ios_concurrent_runner.mm \
+ platform_ios.mm
}
macx|iphone* {
OBJECTIVE_SOURCES += apple_location_service.mm
- LIBS += -framework CoreLocation -framework Foundation
-}
-
-macx:!iphone* {
- OBJECTIVE_SOURCES += wifi_info_mac.mm \
- apple_video_timer.mm
-
- LIBS += -framework CoreWLAN -framework QuartzCore
-}
-
-iphone* {
- OBJECTIVE_SOURCES += ios_video_timer.mm
- LIBS += -framework QuartzCore
-}
-
-win32 {
- SOURCES += wifi_info_windows.cpp
}
# common sources for all platforms
@@ -66,6 +58,5 @@ SOURCES += \
location_manager.cpp \
preferred_languages.cpp \
settings.cpp \
- platform.cpp \
video_timer.cpp \
languages.cpp \
diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm
new file mode 100644
index 0000000000..45751d8ecb
--- /dev/null
+++ b/platform/platform_ios.mm
@@ -0,0 +1,195 @@
+#include "platform.hpp"
+
+#include "../coding/file_reader.hpp"
+
+#include <dirent.h>
+#include <sys/stat.h>
+
+#import <Foundation/NSAutoreleasePool.h>
+#import <Foundation/NSBundle.h>
+#import <Foundation/NSPathUtilities.h>
+#import <Foundation/NSProcessInfo.h>
+
+#import <UIKit/UIDevice.h>
+#import <UIKit/UIScreen.h>
+#import <UIKit/UIScreenMode.h>
+
+class Platform::PlatformImpl
+{
+public:
+ double m_visualScale;
+ int m_scaleEtalonSize;
+ string m_skinName;
+ string m_deviceName;
+};
+
+Platform::Platform()
+{
+ m_impl = new PlatformImpl;
+
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+ NSBundle * bundle = [NSBundle mainBundle];
+ NSString * path = [bundle resourcePath];
+ m_resourcesDir = [path UTF8String];
+ m_resourcesDir += '/';
+
+ NSArray * dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString * docsDir = [dirPaths objectAtIndex:0];
+ m_writableDir = [docsDir UTF8String];
+ m_writableDir += '/';
+
+ // Hardcoding screen resolution depending on the device we are running.
+ m_impl->m_visualScale = 1.0;
+ m_impl->m_skinName = "basic.skn";
+
+ // Calculating resolution
+ UIDevice * device = [UIDevice currentDevice];
+
+ NSRange range = [device.model rangeOfString:@"iPad"];
+ if (range.location != NSNotFound)
+ {
+ m_impl->m_deviceName = "iPad";
+ m_impl->m_visualScale = 1.3;
+ }
+ else
+ {
+ range = [device.model rangeOfString:@"iPod"];
+ if (range.location != NSNotFound)
+ m_impl->m_deviceName = "iPod";
+ else
+ m_impl->m_deviceName = "iPhone";
+ if ([UIScreen mainScreen].currentMode.size.width == 640)
+ {
+ m_impl->m_visualScale = 2.0;
+ m_impl->m_skinName = "basic_highres.skn";
+ }
+ }
+
+ m_impl->m_scaleEtalonSize = 256 * 1.5 * m_impl->m_visualScale;
+
+ NSLog(@"Device: %@, SystemName: %@, SystemVersion: %@", device.model, device.systemName, device.systemVersion);
+
+ [pool release];
+}
+
+Platform::~Platform()
+{
+ delete m_impl;
+}
+
+void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & res) const
+{
+ DIR * dir;
+ struct dirent * entry;
+
+ if ((dir = opendir(directory.c_str())) == NULL)
+ return;
+
+ // TODO: take wildcards into account...
+ string mask_fixed = mask;
+ if (mask_fixed.size() && mask_fixed[0] == '*')
+ mask_fixed.erase(0, 1);
+
+ do
+ {
+ if ((entry = readdir(dir)) != NULL)
+ {
+ string fname(entry->d_name);
+ size_t index = fname.rfind(mask_fixed);
+ if (index != string::npos && index == fname.size() - mask_fixed.size())
+ {
+ // TODO: By some strange reason under simulator stat returns -1,
+ // may be because of symbolic links?..
+ //struct stat fileStatus;
+ //if (stat(string(directory + fname).c_str(), &fileStatus) == 0 &&
+ // (fileStatus.st_mode & S_IFDIR) == 0)
+ //{
+ res.push_back(fname);
+ //}
+ }
+ }
+ } while (entry != NULL);
+
+ closedir(dir);
+}
+
+bool Platform::GetFileSize(string const & file, uint64_t & size) const
+{
+ struct stat s;
+ if (stat(file.c_str(), &s) == 0)
+ {
+ size = s.st_size;
+ return true;
+ }
+ return false;
+}
+
+void Platform::GetFontNames(FilesList & res) const
+{
+ GetFilesInDir(ResourcesDir(), "*.ttf", res);
+ sort(res.begin(), res.end());
+}
+
+static string ReadPathForFile(string const & writableDir,
+ string const & resourcesDir, string const & file)
+{
+ string fullPath = writableDir + file;
+ if (!GetPlatform().IsFileExists(fullPath))
+ {
+ fullPath = resourcesDir + file;
+ if (!GetPlatform().IsFileExists(fullPath))
+ MYTHROW(FileAbsentException, ("File doesn't exist", fullPath));
+ }
+ return fullPath;
+}
+
+ModelReader * Platform::GetReader(string const & file) const
+{
+ return new FileReader(ReadPathForFile(m_writableDir, m_resourcesDir, file), 10, 12);
+}
+
+int Platform::CpuCores() const
+{
+ NSInteger const numCPU = [[NSProcessInfo processInfo] activeProcessorCount];
+ if (numCPU >= 1)
+ return numCPU;
+ return 1;
+}
+
+string Platform::SkinName() const
+{
+ return m_impl->m_skinName;
+}
+
+double Platform::VisualScale() const
+{
+ return m_impl->m_visualScale;
+}
+
+int Platform::ScaleEtalonSize() const
+{
+ return m_impl->m_scaleEtalonSize;
+}
+
+int Platform::MaxTilesCount() const
+{
+ return 120;
+}
+
+int Platform::TileSize() const
+{
+ return 256;
+}
+
+string Platform::DeviceName() const
+{
+ return m_impl->m_deviceName;
+}
+
+////////////////////////////////////////////////////////////////////////
+extern "C" Platform & GetPlatform()
+{
+ static Platform platform;
+ return platform;
+}
diff --git a/platform/platform_linux.cpp b/platform/platform_linux.cpp
new file mode 100644
index 0000000000..f3e1065b74
--- /dev/null
+++ b/platform/platform_linux.cpp
@@ -0,0 +1,43 @@
+#include "platform.hpp"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+static bool GetUserWritableDir(string & outDir)
+{
+ char * path = ::getenv("HOME");
+ if (path)
+ {
+ outDir = path;
+ outDir += "." LOCALAPPDATA_DIR "/";
+ ::mkdir(outDir.c_str(), 0755);
+ return true;
+ }
+ return false;
+}
+
+/// @return full path including binary itself
+static bool GetPathToBinary(string & outPath)
+{
+ char path[4096] = {0};
+ if (0 < ::readlink("/proc/self/exe", path, ARRAY_SIZE(path)))
+ {
+ outPath = path;
+ return true;
+ }
+ return false;
+}
+
+int Platform::CpuCores() const
+{
+ long numCPU = sysconf(_SC_NPROCESSORS_ONLN);
+ if (numCPU >= 1)
+ return static_cast<int>(numCPU);
+ return 1;
+}
+
+string Platform::UniqueClientId() const
+{
+ return "@TODO";
+}
diff --git a/platform/platform_mac.mm b/platform/platform_mac.mm
new file mode 100644
index 0000000000..2eed808312
--- /dev/null
+++ b/platform/platform_mac.mm
@@ -0,0 +1,89 @@
+#include "platform.hpp"
+
+#include "../std/target_os.hpp"
+
+#include <glob.h>
+#include <NSSystemDirectories.h>
+#include <mach-o/dyld.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <IOKit/IOKitLib.h>
+
+#define LOCALAPPDATA_DIR "MapsWithMe"
+
+static string ExpandTildePath(char const * path)
+{
+ glob_t globbuf;
+ string result = path;
+
+ if (::glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success
+ {
+ if (globbuf.gl_pathc > 0)
+ result = globbuf.gl_pathv[0];
+
+ globfree(&globbuf);
+ }
+ return result;
+}
+
+bool GetUserWritableDir(string & outDir)
+{
+ char pathBuf[PATH_MAX];
+ NSSearchPathEnumerationState state = ::NSStartSearchPathEnumeration(NSApplicationSupportDirectory, NSUserDomainMask);
+ while ((state = NSGetNextSearchPathEnumeration(state, pathBuf)))
+ {
+ outDir = ExpandTildePath(pathBuf);
+ ::mkdir(outDir.c_str(), 0755);
+ outDir += "/" LOCALAPPDATA_DIR "/";
+ ::mkdir(outDir.c_str(), 0755);
+ return true;
+ }
+ return false;
+}
+
+/// @return full path including binary itself
+bool GetPathToBinary(string & outPath)
+{
+ char path[MAXPATHLEN] = {0};
+ uint32_t pathSize = ARRAY_SIZE(path);
+ if (0 == ::_NSGetExecutablePath(path, &pathSize))
+ {
+ char fixedPath[MAXPATHLEN] = {0};
+ if (::realpath(path, fixedPath))
+ {
+ outPath = fixedPath;
+ return true;
+ }
+ }
+ return false;
+}
+
+int Platform::CpuCores() const
+{
+ int mib[2], numCPU = 0;
+ size_t len = sizeof(numCPU);
+ mib[0] = CTL_HW;
+ mib[1] = HW_AVAILCPU;
+ sysctl(mib, 2, &numCPU, &len, NULL, 0);
+ if (numCPU >= 1)
+ return numCPU;
+ // second try
+ mib[1] = HW_NCPU;
+ len = sizeof(numCPU);
+ sysctl(mib, 2, &numCPU, &len, NULL, 0);
+ if (numCPU >= 1)
+ return numCPU;
+ return 1;
+}
+
+string Platform::UniqueClientId() const
+{
+ io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
+ CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
+ IOObjectRelease(ioRegistryRoot);
+ char buf[513];
+ CFStringGetCString(uuidCf, buf, 512, kCFStringEncodingMacRoman);
+ CFRelease(uuidCf);
+ return buf;
+}
diff --git a/platform/platform_qt.cpp b/platform/platform_qt.cpp
new file mode 100644
index 0000000000..10ab490789
--- /dev/null
+++ b/platform/platform_qt.cpp
@@ -0,0 +1,184 @@
+#include "platform.hpp"
+
+#include "../coding/file_reader.hpp"
+
+#include "../std/target_os.hpp"
+
+#include <QtCore/QDir>
+#include <QtCore/QFileInfo>
+#include <QtCore/QTemporaryFile>
+
+// default writable directory name for dev/standalone installs
+#define MAPDATA_DIR "data"
+// default writable dir name in LocalAppData or ~/Library/AppData/ folders
+#define LOCALAPPDATA_DIR "MapsWithMe"
+// default Resources read-only dir
+#define RESOURCES_DIR "Resources"
+
+/// @name Platform-dependent implementations in separate files
+//@{
+bool GetUserWritableDir(string & outDir);
+bool GetPathToBinary(string & outDir);
+//@}
+
+static bool IsDirectoryWritable(string const & dir)
+{
+ if (!dir.empty())
+ {
+ QString qDir = dir.c_str();
+ if (dir[dir.size() - 1] != '/' && dir[dir.size() - 1] != '\\')
+ qDir.append('/');
+
+ QTemporaryFile file(qDir + "XXXXXX");
+ if (file.open())
+ return true;
+ }
+ return false;
+}
+
+/// 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
+static string DirFinder(string const & startPath, string dirName)
+{
+ char const SLASH = QDir::separator().toAscii();
+ dirName = SLASH + dirName + SLASH;
+
+ size_t slashPos = startPath.size();
+ while (slashPos > 0 && (slashPos = startPath.rfind(SLASH, slashPos - 1)) != string::npos)
+ {
+ string const dir = startPath.substr(0, slashPos) + dirName;
+ if (QFileInfo(dir.c_str()).exists())
+ return dir;
+ }
+ return string();
+}
+
+static bool GetOSSpecificResourcesDir(string const & exePath, string & dir)
+{
+ dir = DirFinder(exePath, RESOURCES_DIR);
+ return !dir.empty();
+}
+
+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())
+ {
+ CHECK( GetOSSpecificResourcesDir(exePath, dir), ("Can't retrieve resources directory") );
+ }
+
+ /// @todo Check all necessary files
+}
+
+static void InitWritableDir(string & dir)
+{
+ // Writable dir can be any "data" folder found in the nearest upper directory
+ // ./data - For Windows portable builds
+ // ../data
+ // ../../data - For developer builds
+ // etc. (for Mac in can be up to 6 levels above due to packages structure
+ // and if no _writable_ "data" folder was found, User/Application Data/MapsWithMe will be used
+
+ string path;
+ CHECK( GetPathToBinary(path), ("Can't get full path to executable") );
+ dir = DirFinder(path, MAPDATA_DIR);
+ if (dir.empty() || !IsDirectoryWritable(dir))
+ {
+ CHECK( GetUserWritableDir(dir), ("Can't get User's Application Data writable directory") );
+ }
+}
+
+static string ReadPathForFile(string const & writableDir,
+ string const & resourcesDir, string const & file)
+{
+ string fullPath = writableDir + file;
+ if (!GetPlatform().IsFileExists(fullPath))
+ {
+ fullPath = resourcesDir + file;
+ if (!GetPlatform().IsFileExists(fullPath))
+ MYTHROW(FileAbsentException, ("File doesn't exist", fullPath));
+ }
+ return fullPath;
+}
+
+////////////////////////////////////////////////////////////////////////////////////////
+Platform::Platform()
+{
+ InitWritableDir(m_writableDir);
+ InitResourcesDir(m_resourcesDir);
+}
+
+Platform::~Platform()
+{
+}
+
+ModelReader * Platform::GetReader(string const & file) const
+{
+ return new FileReader(ReadPathForFile(m_writableDir, m_resourcesDir, file), 10, 12);
+}
+
+bool Platform::GetFileSize(string const & file, uint64_t & size) const
+{
+ QFileInfo f(file.c_str());
+ size = static_cast<uint64_t>(f.size());
+ return size != 0;
+}
+
+void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const
+{
+ outFiles.clear();
+ QDir dir(directory.c_str(), mask.c_str(), QDir::Unsorted,
+ QDir::Files | QDir::Readable | QDir::Dirs | QDir::NoDotAndDotDot);
+ int const count = dir.count();
+ for (int i = 0; i < count; ++i)
+ outFiles.push_back(dir[i].toUtf8().data());
+}
+
+string Platform::DeviceName() const
+{
+ return OMIM_OS_NAME;
+}
+
+double Platform::VisualScale() const
+{
+ return 1.0;
+}
+
+string Platform::SkinName() const
+{
+ return "basic.skn";
+}
+
+void Platform::GetFontNames(FilesList & res) const
+{
+ GetFilesInDir(ResourcesDir(), "*.ttf", res);
+ sort(res.begin(), res.end());
+}
+
+int Platform::MaxTilesCount() const
+{
+ return 120;
+}
+
+int Platform::TileSize() const
+{
+ return 256;
+}
+
+int Platform::ScaleEtalonSize() const
+{
+ return 512 + 256;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+extern "C" Platform & GetPlatform()
+{
+ static Platform platform;
+ return platform;
+}
diff --git a/platform/platform_win.cpp b/platform/platform_win.cpp
new file mode 100644
index 0000000000..a8c3b8d5c9
--- /dev/null
+++ b/platform/platform_win.cpp
@@ -0,0 +1,47 @@
+#include "platform.hpp"
+
+#include "../std/windows.hpp"
+
+#include <shlobj.h>
+
+static bool GetUserWritableDir(string & outDir)
+{
+ char pathBuf[MAX_PATH] = {0};
+ if (SUCCEEDED(::SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, pathBuf)))
+ {
+ outDir = pathBuf;
+ ::CreateDirectoryA(outDir.c_str(), NULL);
+ outDir += "\\" LOCALAPPDATA_DIR "\\";
+ ::CreateDirectoryA(outDir.c_str(), NULL);
+ return true;
+ }
+ return false;
+}
+
+/// @return full path including binary itself
+static bool GetPathToBinary(string & outPath)
+{
+ // get path to executable
+ char pathBuf[MAX_PATH] = {0};
+ if (0 < ::GetModuleFileNameA(NULL, pathBuf, MAX_PATH))
+ {
+ outPath = pathBuf;
+ return true;
+ }
+ return false;
+}
+
+int Platform::CpuCores() const
+{
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ DWORD numCPU = sysinfo.dwNumberOfProcessors;
+ if (numCPU >= 1)
+ return static_cast<int>(numCPU);
+ return 1;
+}
+
+string Platform::UniqueClientId() const
+{
+ return "@TODO";
+}
diff --git a/platform/qtplatform.cpp b/platform/qtplatform.cpp
deleted file mode 100644
index c4900d2b1c..0000000000
--- a/platform/qtplatform.cpp
+++ /dev/null
@@ -1,293 +0,0 @@
-#include "platform.hpp"
-
-#include "../base/assert.hpp"
-#include "../base/macros.hpp"
-#include "../base/logging.hpp"
-
-#include "../defines.hpp"
-
-#include <QtCore/QDir>
-#include <QtCore/QFileInfo>
-#include <QtCore/QTemporaryFile>
-
-#include "../std/target_os.hpp"
-
-#if defined(OMIM_OS_WINDOWS)
- #include "../std/windows.hpp"
- #include <shlobj.h>
- #define DIR_SLASH "\\"
-
-#elif defined(OMIM_OS_MAC)
- #include <stdlib.h>
- #include <mach-o/dyld.h>
- #include <sys/param.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/sysctl.h>
- #include <glob.h>
- #include <NSSystemDirectories.h>
- #define DIR_SLASH "/"
-
-#elif defined(OMIM_OS_LINUX)
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #define DIR_SLASH "/"
-
-#endif
-
-#include "../base/start_mem_debug.hpp"
-
-// default writable directory name for dev/standalone installs
-#define MAPDATA_DIR "data"
-// default writable dir name in LocalAppData or ~/Library/AppData/ folders
-#define LOCALAPPDATA_DIR "MapsWithMe"
-// default Resources read-only dir
-#define RESOURCES_DIR "Resources"
-
-#ifdef OMIM_OS_MAC
-string ExpandTildePath(char const * path)
-{
-// ASSERT(path, ());
- glob_t globbuf;
- string result = path;
-
- if (::glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success
- {
- if (globbuf.gl_pathc > 0)
- result = globbuf.gl_pathv[0];
-
- globfree(&globbuf);
- }
- return result;
-}
-#endif
-
-static bool GetUserWritableDir(string & outDir)
-{
-#if defined(OMIM_OS_WINDOWS)
- char pathBuf[MAX_PATH] = {0};
- if (SUCCEEDED(::SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, pathBuf)))
- {
- outDir = pathBuf;
- ::CreateDirectoryA(outDir.c_str(), NULL);
- outDir += "\\" LOCALAPPDATA_DIR "\\";
- ::CreateDirectoryA(outDir.c_str(), NULL);
- return true;
- }
-
-#elif defined(OMIM_OS_MAC)
- char pathBuf[PATH_MAX];
- NSSearchPathEnumerationState state = ::NSStartSearchPathEnumeration(NSApplicationSupportDirectory, NSUserDomainMask);
- while ((state = NSGetNextSearchPathEnumeration(state, pathBuf)))
- {
- outDir = ExpandTildePath(pathBuf);
- ::mkdir(outDir.c_str(), 0755);
- outDir += "/" LOCALAPPDATA_DIR "/";
- ::mkdir(outDir.c_str(), 0755);
- return true;
- }
-
-#elif defined(OMIM_OS_LINUX)
- char * path = ::getenv("HOME");
- if (path)
- {
- outDir = path;
- outDir += "." LOCALAPPDATA_DIR "/";
- ::mkdir(outDir.c_str(), 0755);
- return true;
- }
-
-#else
- #error "Implement code for your OS"
-
-#endif
- return false;
-}
-
-/// @return full path including binary itself
-static bool GetPathToBinary(string & outPath)
-{
-#if defined(OMIM_OS_WINDOWS)
- // get path to executable
- char pathBuf[MAX_PATH] = {0};
- if (0 < ::GetModuleFileNameA(NULL, pathBuf, MAX_PATH))
- {
- outPath = pathBuf;
- return true;
- }
-
-#elif defined (OMIM_OS_MAC)
- char path[MAXPATHLEN] = {0};
- uint32_t pathSize = ARRAY_SIZE(path);
- if (0 == ::_NSGetExecutablePath(path, &pathSize))
- {
- char fixedPath[MAXPATHLEN] = {0};
- if (::realpath(path, fixedPath))
- {
- outPath = fixedPath;
- return true;
- }
- }
-
-#elif defined (OMIM_OS_LINUX)
- char path[4096] = {0};
- if (0 < ::readlink("/proc/self/exe", path, ARRAY_SIZE(path)))
- {
- outPath = path;
- return true;
- }
-
-#else
- #error "Add implementation for your operating system, please"
-
-#endif
- return false;
-}
-
-static bool IsDirectoryWritable(string const & dir)
-{
- if (!dir.empty())
- {
- QString qDir = dir.c_str();
- if (dir[dir.size() - 1] != '/' && dir[dir.size() - 1] != '\\')
- qDir.append('/');
-
- QTemporaryFile file(qDir + "XXXXXX");
- if (file.open())
- return true;
- }
- return false;
-}
-
-////////////////////////////////////////////////////////////////////////////////////////
-class QtPlatform : public BasePlatformImpl
-{
- 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
- static string DirFinder(string const & startPath, string dirName)
- {
- dirName = DIR_SLASH + dirName + DIR_SLASH;
-
- size_t slashPos = startPath.size();
- while (slashPos > 0 && (slashPos = startPath.rfind(DIR_SLASH, slashPos - 1)) != string::npos)
- {
- string const dir = startPath.substr(0, slashPos) + dirName;
- if (IsDirExists(dir))
- return dir;
- }
- return string();
- }
-
- static bool GetOSSpecificResourcesDir(string const & exePath, string & dir)
- {
- dir = DirFinder(exePath, RESOURCES_DIR);
- return !dir.empty();
- }
-
- 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())
- {
- CHECK( GetOSSpecificResourcesDir(exePath, dir), ("Can't retrieve resources directory") );
- }
-
- /// @todo Check all necessary files
- }
-
- static void InitWritableDir(string & dir)
- {
- // Writable dir can be any "data" folder found in the nearest upper directory
- // ./data - For Windows portable builds
- // ../data
- // ../../data - For developer builds
- // etc. (for Mac in can be up to 6 levels above due to packages structure
- // and if no _writable_ "data" folder was found, User/Application Data/MapsWithMe will be used
-
- string path;
- CHECK( GetPathToBinary(path), ("Can't get full path to executable") );
- dir = DirFinder(path, MAPDATA_DIR);
- if (dir.empty() || !IsDirectoryWritable(dir))
- {
- CHECK( GetUserWritableDir(dir), ("Can't get User's Application Data writable directory") );
- }
- }
-
-public:
- QtPlatform()
- {
- InitWritableDir(m_writableDir);
- InitResourcesDir(m_resourcesDir);
- }
-
- virtual void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles) const
- {
- outFiles.clear();
- QDir dir(directory.c_str(), mask.c_str(), QDir::Unsorted,
- QDir::Files | QDir::Readable | QDir::Dirs | QDir::NoDotAndDotDot);
- int const count = dir.count();
- for (int i = 0; i < count; ++i)
- outFiles.push_back(dir[i].toUtf8().data());
- }
-
- virtual bool RenameFileX(string const & original, string const & newName) const
- {
- return QFile::rename(original.c_str(), newName.c_str());
- }
-
- virtual int CpuCores() const
- {
-#if defined(OMIM_OS_WINDOWS)
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
- DWORD numCPU = sysinfo.dwNumberOfProcessors;
- if (numCPU >= 1)
- return static_cast<int>(numCPU);
-
-#elif defined(OMIM_OS_MAC)
- int mib[2], numCPU = 0;
- size_t len = sizeof(numCPU);
- mib[0] = CTL_HW;
- mib[1] = HW_AVAILCPU;
- sysctl(mib, 2, &numCPU, &len, NULL, 0);
- if (numCPU >= 1)
- return numCPU;
- // second try
- mib[1] = HW_NCPU;
- len = sizeof(numCPU);
- sysctl(mib, 2, &numCPU, &len, NULL, 0);
- if (numCPU >= 1)
- return numCPU;
-
-#elif defined(OMIM_OS_LINUX)
- long numCPU = sysconf(_SC_NPROCESSORS_ONLN);
- if (numCPU >= 1)
- return static_cast<int>(numCPU);
-#endif
- return 1;
- }
-
- string DeviceID() const
- {
- return "DesktopVersion";
- }
-};
-
-extern "C" Platform & GetPlatform()
-{
- static QtPlatform platform;
- return platform;
-}