fix account selection after first account creation

Account wasn't properly set due to a not handled exception. This is no
longer the case as the first account of the list is now selected by
default when no account is yet saved in user configuration.

Moreover, having an empty AccountModel shouldn't cause any crash as
this case is now handled with exceptions.

Change-Id: Ibb3e386d80e946b2507916b3e44cb28af62bc3c7
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index e657fd3..a054095 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -108,7 +108,12 @@
 
     [currentCallVC initFrame];
     [offlineVC initFrame];
-    [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
+    @try {
+        [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
+    }
+    @catch (NSException *ex) {
+        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
+    }
 
     // Fresh run, we need to make sure RingID appears
     [shareButton sendActionOn:NSLeftMouseDownMask];
@@ -168,25 +173,32 @@
  */
 - (void) updateRingID
 {
-    auto& account = [chooseAccountVC selectedAccount];
+    @try {
+        auto& account = [chooseAccountVC selectedAccount];
 
-    [ringIDLabel setStringValue:@""];
+        [ringIDLabel setStringValue:@""];
 
-    if(account.profileInfo.type != lrc::api::profile::Type::RING) {
-        self.hideRingID = YES;
-        return;
+        if(account.profileInfo.type != lrc::api::profile::Type::RING) {
+            self.hideRingID = YES;
+            return;
+        }
+        self.hideRingID = NO;
+        auto& registeredName = account.registeredName;
+        auto& ringID = account.profileInfo.uri;
+        NSString* uriToDisplay = nullptr;
+        if (!registeredName.empty()) {
+            uriToDisplay = @(registeredName.c_str());
+        } else {
+            uriToDisplay = @(ringID.c_str());
+        }
+        [ringIDLabel setStringValue:uriToDisplay];
+        [self drawQRCode:@(ringID.c_str())];
     }
-    self.hideRingID = NO;
-    auto& registeredName = account.registeredName;
-    auto& ringID = account.profileInfo.uri;
-    NSString* uriToDisplay = nullptr;
-    if (!registeredName.empty()) {
-        uriToDisplay = @(registeredName.c_str());
-    } else {
-        uriToDisplay = @(ringID.c_str());
+    @catch (NSException *ex) {
+        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
+        self.hideRingID = NO;
+        [ringIDLabel setStringValue:@"No account available"];
     }
-    [ringIDLabel setStringValue:uriToDisplay];
-    [self drawQRCode:@(ringID.c_str())];
 }
 
 - (IBAction)shareRingID:(id)sender {