platform/iphone/Classes/Server/ServerHost.m in rhodes-1.4.2 vs platform/iphone/Classes/Server/ServerHost.m in rhodes-1.5.0

- old
+ new

@@ -9,19 +9,20 @@ #pragma mark Includes #include <assert.h> #include <unistd.h> -#include "defs.h" -#include "Server.h" -#include "HttpContext.h" +#include "common/RhoPort.h" +#include "common/rhoparams.h" +//#include "Server.h" +//#include "HttpContext.h" #include "ServerHost.h" //#include "Dispatcher.h" #include "AppManagerI.h" #include "common/RhoConf.h" #include "logging/RhoLogConf.h" -#include "sync/syncthread.h" +//#include "sync/syncthread.h" #include "common/RhodesApp.h" #include "JSString.h" #import "WebViewUrl.h" #import "ParamsWrapper.h" #import "DateTime.h" @@ -31,27 +32,26 @@ #import "logging/RhoLog.h" #undef DEFAULT_LOGCATEGORY #define DEFAULT_LOGCATEGORY "ServerHost" -//extern char* get_current_location(); -extern void geo_init(); +extern void rho_geoimpl_init(); #pragma mark - #pragma mark Constant Definitions #define kServiceType CFSTR("_http._tcp.") #pragma mark - #pragma mark Static Function Declarations -static void AcceptConnection(ServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info); +//static void AcceptConnection(ServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info); -/* static */ void -AcceptConnection(ServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info) { +/* static */ //void +/*AcceptConnection(ServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info) { if (sock == ((CFSocketNativeHandle)(-1))) { RAWLOG_INFO2("AcceptConnection - Received an error (%d, %d)", (int)error->domain, (int)error->error ); @@ -65,22 +65,22 @@ HttpContextRef http = HttpContextCreate(NULL, sock); if ((http != NULL) && !HttpContextOpen(http)) HttpContextRelease(http); } -} +}*/ #pragma mark - static ServerHost* sharedSH = nil; @implementation ServerHost @synthesize actionTarget, /*onStartFailure,*/ onStartSuccess, onRefreshView, onNavigateTo, onExecuteJs; @synthesize /*onSetViewHomeUrl, onSetViewOptionsUrl,*/ onTakePicture, onChoosePicture, onChooseDateTime; @synthesize onCreateNativeBar, onRemoveNativeBar, onSwitchTab; -@synthesize onShowPopup, onVibrate, onPlayFile, onSysCall, onMapLocation, onCreateMap, onActiveTab; +@synthesize onShowPopup, onVibrate, onPlayFile, onSysCall, onMapLocation, onCreateMap, onActiveTab, onShowLog; - (void)serverStarted:(NSString*)data { if(actionTarget && [actionTarget respondsToSelector:onStartSuccess]) { [actionTarget performSelector:onStartSuccess withObject:data]; } @@ -170,10 +170,16 @@ [actionTarget performSelectorOnMainThread:onSwitchTab withObject:value waitUntilDone:YES]; [value release]; } } +- (void)showLog { + if (actionTarget && [actionTarget respondsToSelector:onShowLog]) { + [actionTarget performSelectorOnMainThread:onShowLog withObject:nil waitUntilDone:NO]; + } +} + /* - (void)setViewOptionsUrl:(NSString*)url { if(actionTarget && [actionTarget respondsToSelector:onSetViewOptionsUrl]) { [actionTarget performSelector:onSetViewOptionsUrl withObject:url]; } @@ -201,13 +207,15 @@ if(actionTarget && [actionTarget respondsToSelector:onMapLocation]) { [actionTarget performSelectorOnMainThread:onMapLocation withObject:query waitUntilDone:NO]; } } -- (void)createMap:(NSMutableArray*)items { +- (void)createMap:(rho_param*)p { if(actionTarget && [actionTarget respondsToSelector:onCreateMap]) { - [actionTarget performSelectorOnMainThread:onCreateMap withObject:items waitUntilDone:NO]; + NSValue *value = [NSValue valueWithPointer:p]; + if (!value) return; + [actionTarget performSelectorOnMainThread:onCreateMap withObject:value waitUntilDone:NO]; } } - (int)activeTab { int retval = 0; @@ -277,11 +285,13 @@ - (void)ServerHostThreadRoutine:(id)anObject { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; runLoop = CFRunLoopGetCurrent(); m_geoThread = [NSThread currentThread]; - geo_init(); + rho_geoimpl_init(); + [self performSelectorOnMainThread:@selector(serverStarted:) + withObject:NULL waitUntilDone:NO]; [[NSRunLoop currentRunLoop] run]; RAWLOG_INFO("Server host thread routine is completed"); [pool release]; } @@ -294,11 +304,11 @@ return sqlite3_open([path UTF8String], &database); }*/ //extern const char* RhoGetRootPath(); --(void) start { +-(void) create { //Create appManager = [AppManager instance]; //Configure AppManager [appManager configure]; //Init log and settings @@ -310,10 +320,13 @@ // Start server thread [NSThread detachNewThreadSelector:@selector(ServerHostThreadRoutine:) toTarget:self withObject:nil]; rho_rhodesapp_create(rho_native_rhopath()); +} + +-(void)start { #if !defined(RHO_USE_OWN_HTTPD) || defined(RHO_HTTPD_COMMON_IMPL) rho_rhodesapp_start(); #endif } @@ -422,11 +435,12 @@ void perform_webview_refresh() { [[ServerHost sharedInstance] performRefreshView]; } -void rho_conf_show_log() { +void rho_conf_show_log() { + [[ServerHost sharedInstance] showLog]; } char* webview_current_location(int index) { return (char*)rho_rhodesapp_getcurrenturl(index); } @@ -443,11 +457,11 @@ void alert_show_popup(char* message) { if (message==NULL) { RAWLOG_ERROR("Alert.show_popup - wrong arguments"); } else { - [[ServerHost sharedInstance] showPopup:[NSString stringWithCString:message]]; + [[ServerHost sharedInstance] showPopup:[NSString stringWithUTF8String:message]]; } } void alert_vibrate(int duration) { [[ServerHost sharedInstance] vibrate:duration]; @@ -455,61 +469,97 @@ void alert_play_file(char* file_name, char* media_type) { if (file_name==NULL) { RAWLOG_ERROR("Alert.play_file - please specify file name to play"); } else { - [[ServerHost sharedInstance] playFile:[NSString stringWithCString:file_name] - mediaType:media_type?[NSString stringWithCString:media_type]:NULL]; + [[ServerHost sharedInstance] playFile:[NSString stringWithUTF8String:file_name] + mediaType:media_type?[NSString stringWithUTF8String:media_type]:NULL]; } } void take_picture(char* callback_url) { - [[ServerHost sharedInstance] takePicture:[NSString stringWithCString:callback_url]]; + [[ServerHost sharedInstance] takePicture:[NSString stringWithUTF8String:callback_url]]; } void choose_picture(char* callback_url) { - [[ServerHost sharedInstance] choosePicture:[NSString stringWithCString:callback_url]]; + [[ServerHost sharedInstance] choosePicture:[NSString stringWithUTF8String:callback_url]]; } void choose_datetime(char* callback, char* title, long initial_time, int format, char* data) { - [[ServerHost sharedInstance] chooseDateTime:[NSString stringWithCString:callback] - title:[NSString stringWithCString:title] + [[ServerHost sharedInstance] chooseDateTime:[NSString stringWithUTF8String:callback] + title:[NSString stringWithUTF8String:title] initialTime:initial_time format:format - data:[NSString stringWithCString:data]]; + data:[NSString stringWithUTF8String:data]]; } void rho_map_location(char* query) { - [[ServerHost sharedInstance] mapLocation:[NSString stringWithCString:query]]; + [[ServerHost sharedInstance] mapLocation:[NSString stringWithUTF8String:query]]; } -void mapview_create(int nparams, char** params, int nannotations, char** annotation) { -#ifdef __IPHONE_3_0 - NSMutableArray *settings = parse_settings(nparams, params); - NSMutableArray *annotations = parse_annotations(nannotations,annotation); - NSMutableArray *items = [NSMutableArray arrayWithCapacity:2]; - [items addObject:settings]; - [items addObject:annotations]; - [[ServerHost sharedInstance] createMap:items]; +void mapview_create(rho_param *p) { +#ifdef __IPHONE_3_0 + [[ServerHost sharedInstance] createMap:rho_param_dup(p)]; #endif } void _rho_ext_syscall(PARAMS_WRAPPER* params) { [[ServerHost sharedInstance] doSysCall:params]; } -void create_nativebar(int bar_type, int nparams, char** params) { - NSMutableArray *items = [NSMutableArray arrayWithCapacity:nparams]; - for(int i = 0; i < nparams; i++) { - if (params[i]) { - printf("param: %s\n", params[i]); - [items addObject:[NSString stringWithCString:params[i]]]; - } else { - printf("param: nil\n"); - [items addObject:@""]; - } - } - [[ServerHost sharedInstance] createNativeBar:bar_type dataArray:items]; +void create_nativebar(int bar_type, rho_param *p) +{ + if (p->type != RHO_PARAM_ARRAY) { + RAWLOG_ERROR("Unexpected parameter type for create_nativebar, should be Array"); + return; + } + + int size = p->v.array->size; + NSMutableArray *items = [NSMutableArray arrayWithCapacity:size]; + for (int i = 0; i < size; ++i) { + rho_param *hash = p->v.array->value[i]; + if (hash->type != RHO_PARAM_HASH) { + RAWLOG_ERROR("Unexpected type of array item for create_nativebar, should be Hash"); + return; + } + + const char *label = NULL; + const char *action = NULL; + const char *icon = NULL; + const char *reload = NULL; + + for (int j = 0, lim = hash->v.hash->size; j < lim; ++j) { + const char *name = hash->v.hash->name[j]; + rho_param *value = hash->v.hash->value[j]; + if (value->type != RHO_PARAM_STRING) { + RAWLOG_ERROR1("Unexpected '%s' type, should be String", name); + return; + } + + if (strcasecmp(name, "label") == 0) + label = value->v.string; + else if (strcasecmp(name, "action") == 0) + action = value->v.string; + else if (strcasecmp(name, "icon") == 0) + icon = value->v.string; + else if (strcasecmp(name, "reload") == 0) + reload = value->v.string; + } + + if (label == NULL && bar_type == TOOLBAR_TYPE) + label = ""; + + if (label == NULL || action == NULL) { + RAWLOG_ERROR("Illegal argument for create_nativebar"); + return; + } + + [items addObject:[NSString stringWithUTF8String:label]]; + [items addObject:[NSString stringWithUTF8String:action]]; + [items addObject:[NSString stringWithUTF8String:(icon ? icon : "")]]; + [items addObject:[NSString stringWithUTF8String:(reload ? reload : "false")]]; + } + [[ServerHost sharedInstance] createNativeBar:bar_type dataArray:items]; } void remove_nativebar() { [[ServerHost sharedInstance] removeNativeBar]; } \ No newline at end of file