history: fix history limit

Since history is now handled by LRC, set the history limit instead
of storing it client side.

There is a race condition in LRC, we need to make sure daemon is
started before creating the history, otherwise dring.yml is not
parsed when we ask what is the currently set historyLimit and returns
default value.

Change-Id: I433a37ab559721df82225a6fe2695525ed85fcd0
Tuleap: #103
diff --git a/src/Constants.h b/src/Constants.h
index f353bf4..9ac6ef8 100644
--- a/src/Constants.h
+++ b/src/Constants.h
@@ -31,6 +31,4 @@
     NSString * const WindowBehaviour = @"window_behaviour";
     /* KVO compliant */
     NSString * const Notifications = @"enable_notifications";
-
-    NSString * const ShowAdvanced = @"show_advanced";
 }
diff --git a/src/GeneralPrefsVC.mm b/src/GeneralPrefsVC.mm
index 30c72bc..0e7e51f 100644
--- a/src/GeneralPrefsVC.mm
+++ b/src/GeneralPrefsVC.mm
@@ -27,31 +27,42 @@
 #import "Constants.h"
 
 @interface GeneralPrefsVC ()
-@property (unsafe_unretained) IBOutlet NSTextField *historyChangedLabel;
-@property (unsafe_unretained) IBOutlet NSView *advancedGeneralSettings;
-@property (unsafe_unretained) IBOutlet NSButton *startUpButton;
-@property (unsafe_unretained) IBOutlet NSButton *toggleAutomaticUpdateCheck;
-@property (unsafe_unretained) IBOutlet NSPopUpButton *checkIntervalPopUp;
-@property (unsafe_unretained) IBOutlet NSView *sparkleContainer;
+@property (unsafe_unretained) IBOutlet NSTextField* historyChangedLabel;
+@property (unsafe_unretained) IBOutlet NSButton* startUpButton;
+@property (unsafe_unretained) IBOutlet NSButton* toggleAutomaticUpdateCheck;
+@property (unsafe_unretained) IBOutlet NSPopUpButton* checkIntervalPopUp;
+@property (unsafe_unretained) IBOutlet NSView* sparkleContainer;
+@property (unsafe_unretained) IBOutlet NSTextField* historyTextField;
+@property (unsafe_unretained) IBOutlet NSStepper* historyStepper;
+@property (unsafe_unretained) IBOutlet NSButton* historySwitch;
 
 @end
 
 @implementation GeneralPrefsVC
 @synthesize historyChangedLabel;
-@synthesize advancedGeneralSettings;
 @synthesize startUpButton;
 @synthesize toggleAutomaticUpdateCheck;
 @synthesize checkIntervalPopUp;
 @synthesize sparkleContainer;
+@synthesize historyTextField;
+@synthesize historyStepper;
+@synthesize historySwitch;
 
 - (void)loadView
 {
     [super loadView];
     [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
-    [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::ShowAdvanced options:NSKeyValueObservingOptionNew context:NULL];
 
     [startUpButton setState:[self isLaunchAtStartup]];
 
+    int historyLimit = CategorizedHistoryModel::instance().historyLimit();
+    [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
+    [historyStepper setIntValue:historyLimit];
+
+    BOOL limited = CategorizedHistoryModel::instance().isHistoryLimited();
+    [historySwitch setState:limited];
+    [historyStepper setEnabled:limited];
+    [historyTextField setEnabled:limited];
 #if ENABLE_SPARKLE
     [sparkleContainer setHidden:NO];
     SUUpdater *updater = [SUUpdater sharedUpdater];
@@ -63,13 +74,11 @@
     [sparkleContainer setHidden:YES];
 #endif
 
-    //[advancedGeneralSettings setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
 }
 
 - (void) dealloc
 {
     [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
-    [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::ShowAdvanced];
 }
 
 - (IBAction)clearHistory:(id)sender {
@@ -77,14 +86,23 @@
     [historyChangedLabel setHidden:NO];
 }
 
+- (IBAction)toggleHistory:(id)sender {
+    CategorizedHistoryModel::instance().setHistoryLimited([sender state]);
+    int historyLimit = CategorizedHistoryModel::instance().historyLimit();
+    [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
+    [historyStepper setIntValue:historyLimit];
+    [historyChangedLabel setHidden:NO];
+    [historyStepper setEnabled:[sender state]];
+    [historyTextField setEnabled:[sender state]];
+}
+
 // KVO handler
 -(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
                        change:(NSDictionary *)aChange context:(void *)aContext
 {
-    if (aKeyPath == Preferences::HistoryLimit) {
+    if ([aKeyPath isEqualToString:Preferences::HistoryLimit]) {
+        CategorizedHistoryModel::instance().setHistoryLimit([[aChange objectForKey: NSKeyValueChangeNewKey] integerValue]);
         [historyChangedLabel setHidden:NO];
-    } else if (aKeyPath == Preferences::ShowAdvanced) {
-        //[advancedGeneralSettings setHidden:[[aChange objectForKey: NSKeyValueChangeNewKey] boolValue]];
     }
 }
 
diff --git a/src/main.mm b/src/main.mm
index 075ee70..fd412b7 100644
--- a/src/main.mm
+++ b/src/main.mm
@@ -33,6 +33,7 @@
 #import <categorizedhistorymodel.h>
 #import <localhistorycollection.h>
 #import <numbercategorymodel.h>
+#import <callmodel.h>
 
 #import "backends/AddressBookBackend.h"
 #import "delegates/ImageManipulationDelegate.h"
@@ -62,6 +63,7 @@
         }
     }
 
+    CallModel::instance();
     CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
 
     /* make sure basic number categories exist, in case user has no contacts