From 944e5e488a7d173baaa2e30d1cc3722f13e25914 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Tue, 25 Oct 2011 18:48:41 +0300 Subject: [PATCH] Common Downloader code now is used on iOS and Mac OS X platforms --- iphone/Maps/Maps.xcodeproj/project.pbxproj | 10 -- iphone/Maps/Platform/IPhoneDownloadManager.mm | 96 --------------- .../apple_download.h | 2 +- .../apple_download.mm | 39 ++++--- platform/apple_download_manager.mm | 110 ++++++++++++++++++ platform/apple_location_service.mm | 4 +- platform/platform.pro | 23 ++-- platform/platform_tests/platform_tests.pro | 2 +- qt/qt.pro | 3 +- 9 files changed, 155 insertions(+), 134 deletions(-) delete mode 100644 iphone/Maps/Platform/IPhoneDownloadManager.mm rename iphone/Maps/Platform/IPhoneDownload.h => platform/apple_download.h (99%) rename iphone/Maps/Platform/IPhoneDownload.mm => platform/apple_download.mm (88%) create mode 100644 platform/apple_download_manager.mm diff --git a/iphone/Maps/Maps.xcodeproj/project.pbxproj b/iphone/Maps/Maps.xcodeproj/project.pbxproj index 2c91621e0f..202cc638ba 100644 --- a/iphone/Maps/Maps.xcodeproj/project.pbxproj +++ b/iphone/Maps/Maps.xcodeproj/project.pbxproj @@ -90,8 +90,6 @@ FAD4906C13EFF61F005E7D43 /* search.png in Resources */ = {isa = PBXBuildFile; fileRef = FAD4906A13EFF61F005E7D43 /* search.png */; }; FAD4906D13EFF61F005E7D43 /* search@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FAD4906B13EFF61F005E7D43 /* search@2x.png */; }; FAEA8B2A1437CA80002A6737 /* libjansson.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FAEA8B291437CA80002A6737 /* libjansson.a */; }; - FAF37EFF126DCE6F005EA154 /* IPhoneDownload.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAF37EFA126DCE6F005EA154 /* IPhoneDownload.mm */; }; - FAF37F00126DCE6F005EA154 /* IPhoneDownloadManager.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAF37EFB126DCE6F005EA154 /* IPhoneDownloadManager.mm */; }; FAF8003C1417D7E50024E8C1 /* ad.png in Resources */ = {isa = PBXBuildFile; fileRef = FAF8FF3E1417D7E50024E8C1 /* ad.png */; }; FAF8003D1417D7E50024E8C1 /* ae.png in Resources */ = {isa = PBXBuildFile; fileRef = FAF8FF3F1417D7E50024E8C1 /* ae.png */; }; FAF8003E1417D7E50024E8C1 /* af.png in Resources */ = {isa = PBXBuildFile; fileRef = FAF8FF401417D7E50024E8C1 /* af.png */; }; @@ -706,9 +704,6 @@ FAD4906A13EFF61F005E7D43 /* search.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = search.png; sourceTree = ""; }; FAD4906B13EFF61F005E7D43 /* search@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "search@2x.png"; sourceTree = ""; }; FAEA8B291437CA80002A6737 /* libjansson.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libjansson.a; sourceTree = SOURCE_ROOT; }; - FAF37EFA126DCE6F005EA154 /* IPhoneDownload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = IPhoneDownload.mm; path = Platform/IPhoneDownload.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; - FAF37EFB126DCE6F005EA154 /* IPhoneDownloadManager.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; name = IPhoneDownloadManager.mm; path = Platform/IPhoneDownloadManager.mm; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; - FAF37F03126DCF11005EA154 /* IPhoneDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = IPhoneDownload.h; path = Platform/IPhoneDownload.h; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; FAF800001417D7E50024E8C1 /* sc.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = sc.png; path = ../../data/flags/sc.png; sourceTree = SOURCE_ROOT; }; FAF800011417D7E50024E8C1 /* sd.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = sd.png; path = ../../data/flags/sd.png; sourceTree = SOURCE_ROOT; }; FAF800021417D7E50024E8C1 /* se.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = se.png; path = ../../data/flags/se.png; sourceTree = SOURCE_ROOT; }; @@ -1452,9 +1447,6 @@ FA6E1F1B124E6B2800F59149 /* Platform */ = { isa = PBXGroup; children = ( - FAF37EFB126DCE6F005EA154 /* IPhoneDownloadManager.mm */, - FAF37F03126DCF11005EA154 /* IPhoneDownload.h */, - FAF37EFA126DCE6F005EA154 /* IPhoneDownload.mm */, FAA5C2A0144F135F005337F6 /* LocationManager.h */, FAA5C2A1144F135F005337F6 /* LocationManager.mm */, ); @@ -2604,8 +2596,6 @@ FA4135ED120A263C0062D5B4 /* SettingsManager.mm in Sources */, EE7F29811219ECA300EB67A9 /* RenderBuffer.mm in Sources */, EE7F29821219ECA300EB67A9 /* RenderContext.mm in Sources */, - FAF37EFF126DCE6F005EA154 /* IPhoneDownload.mm in Sources */, - FAF37F00126DCE6F005EA154 /* IPhoneDownloadManager.mm in Sources */, FAFCB63613366E78001A5C59 /* WebViewController.mm in Sources */, FA34BECA1338D72F00FFB2A7 /* CustomAlertView.mm in Sources */, FA09E01113F71F6C007E69CA /* SearchVC.mm in Sources */, diff --git a/iphone/Maps/Platform/IPhoneDownloadManager.mm b/iphone/Maps/Platform/IPhoneDownloadManager.mm deleted file mode 100644 index 5a3d7906ed..0000000000 --- a/iphone/Maps/Platform/IPhoneDownloadManager.mm +++ /dev/null @@ -1,96 +0,0 @@ -#include "../../platform/download_manager.hpp" - -#import "IPhoneDownload.h" -#import "MapsAppDelegate.h" - -#import - -class IPhoneDownloadManager : public DownloadManager -{ - NSMutableArray * activeDownloads; - -public: - IPhoneDownloadManager() - { - activeDownloads = [[NSMutableArray alloc] init]; - } - - virtual ~IPhoneDownloadManager() - { - for (NSUInteger i = 0; i < [activeDownloads count]; ++i) - [[activeDownloads objectAtIndex:i] release]; - - [activeDownloads removeAllObjects]; - [activeDownloads release]; - } - - virtual void HttpRequest(HttpStartParams const & params) - { - // check if download is already active - for (NSUInteger i = 0; i < [activeDownloads count]; ++i) - { - IPhoneDownload * download = [activeDownloads objectAtIndex:i]; - if ([download Url] == params.m_url) - { - NSLog(@"Download is already active for url %s", params.m_url.c_str()); - return; - } - } - - IPhoneDownload * download = [[IPhoneDownload alloc] init]; - if ([download StartDownload:params]) - { - // save download in array to cancel it later if necessary - [activeDownloads addObject:download]; - // prevent device from going to standby - [[MapsAppDelegate theApp] disableStandby]; - } - else - { - // free memory - [download release]; - } - } - - /// @note Doesn't notify clients on canceling! - virtual void CancelDownload(string const & url) - { - // disable network activity indicator in top system toolbar - // note that this method is called also from successful/failed download to "selfdestruct" below - [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; - - for (NSUInteger i = 0; i < [activeDownloads count]; ++i) - { - IPhoneDownload * download = [activeDownloads objectAtIndex:i]; - if ([download Url] == url) - { - [activeDownloads removeObjectAtIndex:i]; - [download Cancel]; - [download release]; - break; - } - } - // enable standby if no more downloads are left - if ([activeDownloads count] == 0) - [[MapsAppDelegate theApp] enableStandby]; - } - - virtual void CancelAllDownloads() - { - // disable network activity indicator in top system toolbar - [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; - - for (NSUInteger i = 0; i < [activeDownloads count]; ++i) { - IPhoneDownload * download = [activeDownloads objectAtIndex:i]; - [download Cancel]; - [download release]; - } - [activeDownloads removeAllObjects]; - } -}; - -DownloadManager & GetDownloadManager() -{ - static IPhoneDownloadManager downloadManager; - return downloadManager; -} diff --git a/iphone/Maps/Platform/IPhoneDownload.h b/platform/apple_download.h similarity index 99% rename from iphone/Maps/Platform/IPhoneDownload.h rename to platform/apple_download.h index 45e7f297bb..5def5cae71 100644 --- a/iphone/Maps/Platform/IPhoneDownload.h +++ b/platform/apple_download.h @@ -7,7 +7,7 @@ @interface IPhoneDownload : NSObject { HttpStartParams m_params; - + string m_currentUrl; UrlGenerator m_urlGenerator; diff --git a/iphone/Maps/Platform/IPhoneDownload.mm b/platform/apple_download.mm similarity index 88% rename from iphone/Maps/Platform/IPhoneDownload.mm rename to platform/apple_download.mm index b61d0160cf..7d9f67f608 100644 --- a/iphone/Maps/Platform/IPhoneDownload.mm +++ b/platform/apple_download.mm @@ -1,6 +1,12 @@ -#import "IPhoneDownload.h" +#import "apple_download.h" -#import +#include "../base/logging.hpp" + +#include "../std/target_os.hpp" + +#ifdef OMIM_OS_IPHONE + #import +#endif #include "../../platform/download_manager.hpp" #include "../../platform/platform.hpp" @@ -12,7 +18,7 @@ static bool NeedToGenerateUrl(string const & url) return url.find("http://") != 0 && url.find("https://") != 0; } -@implementation IPhoneDownload +@implementation AppleDownload - (string const &) Url { @@ -27,7 +33,7 @@ static bool NeedToGenerateUrl(string const & url) - (void) dealloc { -// NSLog(@"~IPhoneDownload() for url: %s", m_url.c_str()); + LOG(LDEBUG, ("destructor for url", m_params.m_url)); if (m_connection) { [m_connection cancel]; @@ -54,9 +60,8 @@ static bool NeedToGenerateUrl(string const & url) long long fileSize = ftello(m_file); if (fileSize > 0) { - NSLog(@"Resuming download for file %s from position %qi", - (m_params.m_fileToSave + DOWNLOADING_FILE_EXTENSION).c_str(), - fileSize); + LOG(LINFO, ("Resuming download for file", m_params.m_fileToSave + DOWNLOADING_FILE_EXTENSION, + "from position", fileSize)); NSString * val = [[NSString alloc] initWithFormat: @"bytes=%qi-", fileSize]; [request addValue:val forHTTPHeaderField:@"Range"]; [val release]; @@ -91,7 +96,7 @@ static bool NeedToGenerateUrl(string const & url) m_file = fopen(tmpFile.c_str(), "ab"); if (m_file == 0) { - NSLog(@"Error opening %s file for download: %s", tmpFile.c_str(), strerror(errno)); + LOG(LERROR, ("Error opening file for download", tmpFile, strerror(errno))); // notify observer about error and exit if (m_params.m_finish) { @@ -113,7 +118,7 @@ static bool NeedToGenerateUrl(string const & url) if (m_connection == 0) { - NSLog(@"Can't create connection for url %s", m_currentUrl.c_str()); + LOG(LERROR, ("Can't create connection for url", m_currentUrl)); // notify observer about error and exit if (m_params.m_finish) { @@ -140,7 +145,7 @@ static bool NeedToGenerateUrl(string const & url) NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; if (statusCode < 200 || statusCode > 299) { - NSLog(@"Received HTTP error code %d, canceling download", statusCode); + LOG(LWARNING, ("Received HTTP error, canceling download", statusCode)); if (m_file) { long long fileSize = ftello(m_file); @@ -165,8 +170,10 @@ static bool NeedToGenerateUrl(string const & url) } } +#ifdef OMIM_OS_IPHONE // enable network activity indicator in top system toolbar [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; +#endif m_projectedFileSize = [response expectedContentLength]; // if server doesn't support resume, make sure we're downloading file from scratch @@ -175,7 +182,7 @@ static bool NeedToGenerateUrl(string const & url) fclose(m_file); m_file = fopen((m_params.m_fileToSave + DOWNLOADING_FILE_EXTENSION).c_str(), "wb"); } - NSLog(@"Projected file size: %qi", m_projectedFileSize); + LOG(LDEBUG, ("Projected download file size", m_projectedFileSize)); } - (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data @@ -211,7 +218,7 @@ static bool NeedToGenerateUrl(string const & url) - (void) connection: (NSURLConnection *)connection didFailWithError: (NSError *)error { // inform the user - NSLog(@"Connection failed for url %s\n%@", m_currentUrl.c_str(), [error localizedDescription]); + LOG(LWARNING, ("Connection failed for", m_currentUrl, [[error localizedDescription] cStringUsingEncoding:NSUTF8StringEncoding])); // retry connection if it's network-specific error if ([self shouldRetry:error]) @@ -227,7 +234,7 @@ static bool NeedToGenerateUrl(string const & url) if (m_connection) return; // successfully restarted connection - NSLog(@"Can't retry connection"); + LOG(LWARNING, ("Can't retry connection")); // notify observer about error and exit after this if-block } } @@ -268,14 +275,12 @@ static bool NeedToGenerateUrl(string const & url) if (rename((m_params.m_fileToSave + DOWNLOADING_FILE_EXTENSION).c_str(), m_params.m_fileToSave.c_str())) { isLocked = true; - NSLog(@"Can't rename to file %s", m_params.m_fileToSave.c_str()); + LOG(LERROR, ("Can't rename to file", m_params.m_fileToSave)); // delete downloaded file remove((m_params.m_fileToSave + DOWNLOADING_FILE_EXTENSION).c_str()); } else - { - NSLog(@"Successfully downloaded %s", m_params.m_url.c_str()); - } + LOG(LDEBUG, ("Successfully downloaded", m_params.m_url)); } if (m_params.m_finish) { diff --git a/platform/apple_download_manager.mm b/platform/apple_download_manager.mm new file mode 100644 index 0000000000..fd05256c08 --- /dev/null +++ b/platform/apple_download_manager.mm @@ -0,0 +1,110 @@ +#include "download_manager.hpp" + +#include "../std/target_os.hpp" + +#include "../base/logging.hpp" + +#import "apple_download.h" +#ifdef OMIM_OS_IPHONE + #import "../iphone/Maps/Classes/MapsAppDelegate.h" + #import +#endif + +class AppleDownloadManager : public DownloadManager +{ + NSMutableArray * activeDownloads; + +public: + AppleDownloadManager() + { + activeDownloads = [[NSMutableArray alloc] init]; + } + + virtual ~AppleDownloadManager() + { + for (NSUInteger i = 0; i < [activeDownloads count]; ++i) + [[activeDownloads objectAtIndex:i] release]; + + [activeDownloads removeAllObjects]; + [activeDownloads release]; + } + + virtual void HttpRequest(HttpStartParams const & params) + { + // check if download is already active + for (NSUInteger i = 0; i < [activeDownloads count]; ++i) + { + AppleDownload * download = [activeDownloads objectAtIndex:i]; + if ([download Url] == params.m_url) + { + LOG(LWARNING, ("Download is already active for url %s", params.m_url)); + return; + } + } + + AppleDownload * download = [[AppleDownload alloc] init]; + if ([download StartDownload:params]) + { + // save download in array to cancel it later if necessary + [activeDownloads addObject:download]; +#ifdef OMIM_OS_IPHONE + // prevent device from going to standby + [[MapsAppDelegate theApp] disableStandby]; +#endif + } + else + { + // free memory + [download release]; + } + } + + /// @note Doesn't notify clients on canceling! + virtual void CancelDownload(string const & url) + { +#ifdef OMIM_OS_IPHONE + // disable network activity indicator in top system toolbar + // note that this method is called also from successful/failed download to "selfdestruct" below + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; +#endif + + for (NSUInteger i = 0; i < [activeDownloads count]; ++i) + { + AppleDownload * download = [activeDownloads objectAtIndex:i]; + if ([download Url] == url) + { + [activeDownloads removeObjectAtIndex:i]; + [download Cancel]; + [download release]; + break; + } + } + +#ifdef OMIM_OS_IPHONE + // enable standby if no more downloads are left + if ([activeDownloads count] == 0) + [[MapsAppDelegate theApp] enableStandby]; +#endif + } + + virtual void CancelAllDownloads() + { +#ifdef OMIM_OS_IPHONE + // disable network activity indicator in top system toolbar + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; +#endif + + for (NSUInteger i = 0; i < [activeDownloads count]; ++i) { + AppleDownload * download = [activeDownloads objectAtIndex:i]; + [download Cancel]; + [download release]; + } + [activeDownloads removeAllObjects]; + } +}; + +DownloadManager & GetDownloadManager() +{ + static AppleDownloadManager downloadManager; + return downloadManager; +} diff --git a/platform/apple_location_service.mm b/platform/apple_location_service.mm index e1261bcd8e..5041fc371c 100644 --- a/platform/apple_location_service.mm +++ b/platform/apple_location_service.mm @@ -1,5 +1,7 @@ #include "location_service.hpp" +#include "../base/logging.hpp" + #include "../std/target_os.hpp" #import @@ -102,7 +104,7 @@ public: - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { - NSLog(@"locationManager failed with error: %ld, %@", error.code, error.description); + LOG(LWARNING, ("locationManager failed with error", error.code, error.description)); if (error.code == kCLErrorDenied) m_service->OnDeniedError(); } diff --git a/platform/platform.pro b/platform/platform.pro index 495d672a96..186f3ad740 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -9,20 +9,16 @@ DEPENDENCIES = coding base jansson include($$ROOT_DIR/common.pri) -QT *= core network - !iphone*:!android*:!bada { + QT *= core network + INCLUDEPATH += $$ROOT_DIR/3party/jansson/src SOURCES += platform_qt.cpp \ wifi_location_service.cpp \ - qt_download_manager.cpp \ - qt_download.cpp \ qt_concurrent_runner.cpp \ location_service.cpp - HEADERS += qt_download_manager.hpp \ - qt_download.hpp \ - wifi_info.hpp \ + HEADERS += wifi_info.hpp \ location_service.hpp win32* { SOURCES += platform_win.cpp \ @@ -43,6 +39,19 @@ QT *= core network SOURCES += platform_android.cpp } +macx*|iphone* { + HEADERS += apple_download.h + OBJECTIVE_SOURCES += apple_download.mm \ + apple_download_manager.mm +} + +win32*|linux* { + HEADERS += qt_download_manager.hpp \ + qt_download.hpp + SOURCES += qt_download_manager.cpp \ + qt_download.cpp +} + # common sources for all platforms HEADERS += \ diff --git a/platform/platform_tests/platform_tests.pro b/platform/platform_tests/platform_tests.pro index 0f16e0e608..82e0e952d3 100644 --- a/platform/platform_tests/platform_tests.pro +++ b/platform/platform_tests/platform_tests.pro @@ -16,7 +16,7 @@ win32* { LIBS *= -lShell32 win32-g++: LIBS *= -lpthread } -macx*: LIBS *= "-framework CoreFoundation" +macx*: LIBS *= "-framework Foundation" "-framework IOKit" SOURCES += \ ../../testing/testingmain.cpp \ diff --git a/qt/qt.pro b/qt/qt.pro index 878776011c..1143a2fd56 100644 --- a/qt/qt.pro +++ b/qt/qt.pro @@ -18,7 +18,8 @@ win32 { } macx* { - LIBS *= "-framework CoreLocation" "-framework Foundation" "-framework CoreWLAN" "-framework QuartzCore" + LIBS *= "-framework CoreLocation" "-framework Foundation" "-framework CoreWLAN" \ + "-framework QuartzCore" "-framework IOKit" ICON = res/mac.icns PLIST_FILE = Info.plist