From cddf918910dc031af57f5901404d0207d0cb16e6 Mon Sep 17 00:00:00 2001 From: Yann Diorcet Date: Tue, 24 Jul 2012 14:19:41 +0200 Subject: [PATCH] Fix application startup Improve sqlite3 usage --- Classes/ChatRoomViewController.m | 1 + Classes/LinphoneAppDelegate.h | 2 + Classes/LinphoneAppDelegate.m | 34 +++++++++++--- Classes/LinphoneManager.m | 1 + Classes/LinphoneUI/UICallBar.xib | 16 ++++++- Classes/LinphoneUI/UIStateBar.xib | 8 ++-- Classes/Model/ChatModel.m | 74 +++++++++++++++++++++---------- Classes/SettingsViewController.m | 1 + 8 files changed, 100 insertions(+), 37 deletions(-) diff --git a/Classes/ChatRoomViewController.m b/Classes/ChatRoomViewController.m index b1479529e..4e6f12c64 100644 --- a/Classes/ChatRoomViewController.m +++ b/Classes/ChatRoomViewController.m @@ -188,6 +188,7 @@ static UICompositeViewDescription *compositeDescription = nil; // Save message in database ChatModel *chat = [[ChatModel alloc] init]; [chat setRemoteContact:remoteAddress]; + [chat setLocalContact:@""]; [chat setMessage:message]; [chat setDirection:[NSNumber numberWithInt:0]]; [chat setTime:[NSDate date]]; diff --git a/Classes/LinphoneAppDelegate.h b/Classes/LinphoneAppDelegate.h index 0f348a891..767eb32ab 100644 --- a/Classes/LinphoneAppDelegate.h +++ b/Classes/LinphoneAppDelegate.h @@ -26,6 +26,7 @@ @interface LinphoneAppDelegate : NSObject { UIWindow *window; CTCallCenter* callCenter; + BOOL started; } - (void)loadDefaultSettings:(NSDictionary *) appDefaults; @@ -35,6 +36,7 @@ - (void)startApplication; @property (nonatomic, retain) IBOutlet UIWindow *window; +@property (assign) BOOL started; @end diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 3f23e2296..ce49f55ce 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -41,6 +41,26 @@ int __aeabi_idiv(int a, int b) { @implementation LinphoneAppDelegate @synthesize window; +@synthesize started; + + +#pragma mark - Lifecycle Functions + +- (id)init { + self = [super init]; + if(self != nil) { + self->started = FALSE; + } + return self; +} + +- (void)dealloc { + [window release]; + [super dealloc]; +} + + +#pragma mark - - (void)handleGSMCallInteration: (id) cCenter { CTCallCenter* ct = (CTCallCenter*) cCenter; @@ -97,10 +117,10 @@ int __aeabi_idiv(int a, int b) { && ![[NSUserDefaults standardUserDefaults] boolForKey:@"start_at_boot_preference"]) { // autoboot disabled, doing nothing return; - } else if ([LinphoneManager instance] == nil) { - [self startApplication]; } + [self startApplication]; + [[LinphoneManager instance] becomeActive]; if (callCenter == nil) { @@ -186,6 +206,11 @@ int __aeabi_idiv(int a, int b) { } - (void)startApplication { + if(started) + return; + + started = TRUE; + [[LinphoneManager instance] startLibLinphone]; [self setupUI]; @@ -199,11 +224,6 @@ int __aeabi_idiv(int a, int b) { - (void)applicationWillTerminate:(UIApplication *)application { } -- (void)dealloc { - [window release]; - [super dealloc]; -} - - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if([notification.userInfo objectForKey:@"call"] != nil) { LinphoneCall* call; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index e3c33e0d1..4eb34c7fc 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -339,6 +339,7 @@ static void linphone_iphone_registration_state(LinphoneCore *lc, LinphoneProxyCo // Save message in database ChatModel *chat = [[ChatModel alloc] init]; + [chat setLocalContact:@""]; [chat setRemoteContact:[NSString stringWithUTF8String:fromStr]]; [chat setMessage:[NSString stringWithUTF8String:message]]; [chat setDirection:[NSNumber numberWithInt:1]]; diff --git a/Classes/LinphoneUI/UICallBar.xib b/Classes/LinphoneUI/UICallBar.xib index d5b86f993..4a967672c 100644 --- a/Classes/LinphoneUI/UICallBar.xib +++ b/Classes/LinphoneUI/UICallBar.xib @@ -425,6 +425,7 @@ 0 0 NO + NO NSImage @@ -461,6 +462,7 @@ 0 0 NO + NO NSImage @@ -1107,6 +1109,7 @@ 0 0 NO + NO NSImage @@ -1139,6 +1142,7 @@ 0 0 NO + NO NSImage @@ -1697,6 +1701,14 @@ 138 + + + dialerButton + + + + 139 + onPadClick: @@ -2366,7 +2378,7 @@ com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin - + com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -2434,7 +2446,7 @@ - 138 + 139 diff --git a/Classes/LinphoneUI/UIStateBar.xib b/Classes/LinphoneUI/UIStateBar.xib index 0b3c3d9af..08f79fb0a 100644 --- a/Classes/LinphoneUI/UIStateBar.xib +++ b/Classes/LinphoneUI/UIStateBar.xib @@ -103,7 +103,7 @@ 292 - {{219, 3}, {27, 17}} + {{252, 3}, {27, 17}} @@ -115,7 +115,7 @@ 292 - {{254, 3}, {27, 17}} + {{287, 3}, {27, 17}} @@ -192,7 +192,7 @@ 292 - {{378, 3}, {27, 17}} + {{412, 3}, {27, 17}} @@ -204,7 +204,7 @@ 292 - {{413, 3}, {27, 17}} + {{447, 3}, {27, 17}} diff --git a/Classes/Model/ChatModel.m b/Classes/Model/ChatModel.m index c538d8028..88eec26d8 100644 --- a/Classes/Model/ChatModel.m +++ b/Classes/Model/ChatModel.m @@ -68,14 +68,22 @@ [LinphoneLogger logc:LinphoneLoggerError format:"Database not ready"]; return; } - const char *sql = [[NSString stringWithFormat:@"INSERT INTO chat (localContact, remoteContact, direction, message, time, read) VALUES (\"%@\", \"%@\", %i, \"%@\", %f, %i)", - localContact, remoteContact, [direction intValue], message, [time timeIntervalSince1970], [read intValue]] UTF8String]; + + const char *sql = "INSERT INTO chat (localContact, remoteContact, direction, message, time, read) VALUES (@LOCALCONTACT, @REMOTECONTACT, @DIRECTION, @MESSAGE, @TIME, @READ)"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return; } + // Prepare statement + sqlite3_bind_text(sqlStatement, 1, [localContact UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_text(sqlStatement, 2, [remoteContact UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_int(sqlStatement, 3, [direction intValue]); + sqlite3_bind_text(sqlStatement, 4, [message UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_double(sqlStatement, 5, [time timeIntervalSince1970]); + sqlite3_bind_int(sqlStatement, 6, [read intValue]); + if (sqlite3_step(sqlStatement) != SQLITE_DONE) { [LinphoneLogger logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)]; sqlite3_finalize(sqlStatement); @@ -95,14 +103,16 @@ return nil; } - const char *sql = [[NSString stringWithFormat:@"SELECT id, localContact, remoteContact, direction, message, time, read FROM chat WHERE id=%i", - [chatId intValue]] UTF8String]; + const char *sql = "SELECT id, localContact, remoteContact, direction, message, time, read FROM chat WHERE id=@ID"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return nil; } + // Prepare statement + sqlite3_bind_int(sqlStatement, 1, [chatId intValue]); + ChatModel* line = nil; int err = sqlite3_step(sqlStatement); if (err == SQLITE_ROW) { @@ -124,14 +134,22 @@ return; } - const char *sql = [[NSString stringWithFormat:@"UPDATE chat SET localContact=\"%@\", remoteContact=\"%@\", direction=%i, message=\"%@\", time=%f, read=%i WHERE id=%i", - localContact, remoteContact, [direction intValue], message, [time timeIntervalSince1970], [read intValue], [chatId intValue]] UTF8String]; + const char *sql = "UPDATE chat SET localContact=@LOCALCONTACT, remoteContact=@REMOTECONTACT, direction=@DIRECTION, message=@MESSAGE, time=@TIME, read=@READ WHERE id=@ID"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return; } + // Prepare statement + sqlite3_bind_text(sqlStatement, 1, [localContact UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_text(sqlStatement, 2, [remoteContact UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_int(sqlStatement, 3, [direction intValue]); + sqlite3_bind_text(sqlStatement, 4, [message UTF8String], -1, SQLITE_STATIC); + sqlite3_bind_double(sqlStatement, 5, [time timeIntervalSince1970]); + sqlite3_bind_int(sqlStatement, 6, [read intValue]); + sqlite3_bind_int(sqlStatement, 7, [chatId intValue]); + if (sqlite3_step(sqlStatement) != SQLITE_DONE) { [LinphoneLogger logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)]; sqlite3_finalize(sqlStatement); @@ -148,14 +166,16 @@ return; } - const char *sql = [[NSString stringWithFormat:@"DELETE FROM chat WHERE id=%i", - [chatId intValue]] UTF8String]; + const char *sql = "DELETE FROM chat WHERE id=@ID"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return; } + // Prepare statement + sqlite3_bind_int(sqlStatement, 1, [chatId intValue]); + if (sqlite3_step(sqlStatement) != SQLITE_DONE) { [LinphoneLogger logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)]; sqlite3_finalize(sqlStatement); @@ -178,7 +198,7 @@ const char *sql = "SELECT id, localContact, remoteContact, direction, message, time, read FROM chat GROUP BY remoteContact ORDER BY time DESC"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)]; return array; } @@ -208,14 +228,16 @@ return array; } - const char *sql = [[NSString stringWithFormat:@"SELECT id, localContact, remoteContact, direction, message, time, read FROM chat WHERE remoteContact=\"%@\" ORDER BY time ASC", - contact] UTF8String]; + const char *sql = "SELECT id, localContact, remoteContact, direction, message, time, read FROM chat WHERE remoteContact=@REMOTECONTACT ORDER BY time ASC"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)]; return array; } + // Prepare statement + sqlite3_bind_text(sqlStatement, 1, [contact UTF8String], -1, SQLITE_STATIC); + int err; while ((err = sqlite3_step(sqlStatement)) == SQLITE_ROW) { ChatModel *line = [[ChatModel alloc] initWithData:sqlStatement]; @@ -240,14 +262,16 @@ return; } - const char *sql = [[NSString stringWithFormat:@"DELETE FROM chat WHERE remoteContact=\"%@\"", - contact] UTF8String]; + const char *sql = "DELETE FROM chat WHERE remoteContact=@REMOTECONTACT"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return; } + // Prepare statement + sqlite3_bind_text(sqlStatement, 1, [contact UTF8String], -1, SQLITE_STATIC); + if (sqlite3_step(sqlStatement) != SQLITE_DONE) { [LinphoneLogger logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)]; sqlite3_finalize(sqlStatement); @@ -265,9 +289,9 @@ return count; } - const char *sql = [[NSString stringWithFormat:@"SELECT count(*) FROM chat WHERE read=0"] UTF8String]; + const char *sql = "SELECT count(*) FROM chat WHERE read=0"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return count; } @@ -291,14 +315,16 @@ return; } - const char *sql = [[NSString stringWithFormat:@"UPDATE chat SET read=1 WHERE remoteContact=\"%@\"", - contact] UTF8String]; + const char *sql = "UPDATE chat SET read=1 WHERE remoteContact=@REMOTECONTACT"; sqlite3_stmt *sqlStatement; - if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { + if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) { [LinphoneLogger logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)]; return; } + // Prepare statement + sqlite3_bind_text(sqlStatement, 1, [contact UTF8String], -1, SQLITE_STATIC); + if (sqlite3_step(sqlStatement) != SQLITE_DONE) { [LinphoneLogger logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)]; sqlite3_finalize(sqlStatement); diff --git a/Classes/SettingsViewController.m b/Classes/SettingsViewController.m index c097ce526..69f5085d6 100644 --- a/Classes/SettingsViewController.m +++ b/Classes/SettingsViewController.m @@ -75,6 +75,7 @@ static UICompositeViewDescription *compositeDescription = nil; - (void)viewDidLoad { [super viewDidLoad]; + settingsController.showDoneButton = FALSE; settingsController.delegate = self; settingsController.showCreditsFooter = FALSE; settingsController.hiddenKeys = [self findHiddenKeys];