blob: 30f36ddb18313ddfeed8d66892e094f937dd5c4d [file] [log] [blame]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -04001/*
2 * Copyright (C) 2018 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20//LRC
21#import <api/lrc.h>
22#import <api/account.h>
23#import <api/newaccountmodel.h>
24
25#import "views/NSColor+RingTheme.h"
26#import "AccountSettingsVC.h"
27#import "AccRingGeneralVC.h"
28#import "AccSipGeneralVC.h"
29#import "AccAdvancedRingVC.h"
30#import "AccAdvancedSipVC.h"
31
32@interface AccountSettingsVC ()
33
34@property (unsafe_unretained) IBOutlet NSScrollView *containerView;
35@property (unsafe_unretained) IBOutlet NSView *settingsView;
36
37@end
38
39@implementation AccountSettingsVC
40
41std::string selectedAccountID;
42NSViewController <AccountGeneralProtocol>* accountGeneralVC;
43NSViewController <AccountAdvancedProtocol>* accountAdvancedVC;
44AccRingGeneralVC* ringGeneralVC;
45AccSipGeneralVC* sipGeneralVC;
46AccAdvancedRingVC* ringAdvancedVC;
47AccAdvancedSipVC* sipAdvancedVC;
48
49CGFloat const VIEW_INSET = 20;
50
51@synthesize accountModel;
52
53-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
54{
55 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
56 {
57 self.accountModel= accountModel;
58 }
59 ringGeneralVC = [[AccRingGeneralVC alloc] initWithNibName:@"AccRingGeneral" bundle:nil accountmodel: accountModel];
60 sipGeneralVC = [[AccSipGeneralVC alloc] initWithNibName:@"AccSipGeneral" bundle:nil accountmodel: accountModel];
61 ringAdvancedVC = [[AccAdvancedRingVC alloc] initWithNibName:@"AccAdvancedRing" bundle:nil accountmodel: accountModel];
62 sipAdvancedVC = [[AccAdvancedSipVC alloc] initWithNibName:@"AccAdvancedSip" bundle:nil accountmodel: accountModel];
63 return self;
64}
65
66- (void)viewDidLoad {
67 [super viewDidLoad];
68 [self.view setWantsLayer:YES];
69 [self.view setLayer:[CALayer layer]];
70 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
71 [self.view.layer setCornerRadius:5.0f];
72}
73
74- (void) initFrame
75{
76 [self.view setFrame:self.view.superview.bounds];
77 [self.view setHidden:YES];
78}
79
80- (void) setSelectedAccount:(std::string) account {
81 selectedAccountID = account;
82 const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID);
83 if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) {
84 accountGeneralVC = ringGeneralVC;
85 accountGeneralVC.delegate = self;
86 accountAdvancedVC = ringAdvancedVC;
87 } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){
88 accountGeneralVC = sipGeneralVC;
89 accountGeneralVC.delegate = self;
90 accountAdvancedVC = sipAdvancedVC;
91 } else {
92 [self hide];
93 return;
94 }
95 [accountGeneralVC setSelectedAccount: selectedAccountID];
96 [accountAdvancedVC setSelectedAccount: selectedAccountID];
97 [self displayGeneralSettings];
98}
99
100- (void) show {
101 [self.view setHidden:NO];
102 [self displayGeneralSettings];
103}
104
105-(void)displayGeneralSettings {
106 self.containerView.documentView = accountGeneralVC.view;
107 int bottomInset = self.containerView.frame.size.height - accountGeneralVC.view.frame.size.height - VIEW_INSET;
108 self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0);
109}
110
111-(void)displayAllSettings {
112 CGRect settingsFrame = accountGeneralVC.view.frame;
113 settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height;
114 NSView* container = [[NSView alloc] initWithFrame:settingsFrame];
115 [container addSubview:accountAdvancedVC.view];
116 CGRect generalSettingsFrame = accountGeneralVC.view.frame;
117 generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height;
118 accountGeneralVC.view.frame = generalSettingsFrame;
119 [container addSubview:accountGeneralVC.view];
120 self.containerView.documentView = container;
121 int bottomInset = self.containerView.frame.size.height - accountGeneralVC.view.frame.size.height - accountAdvancedVC.view.frame.size.height - VIEW_INSET;
122 self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, (bottomInset > 0) ? bottomInset : 0, 0);
123 [self scrollToTopScrollView: self.containerView];
124}
125
126-(void) scrollToTopScrollView: (NSScrollView *) scrollView {
127 NSPoint newScrollOrigin;
128 if ([[scrollView documentView] isFlipped]) {
129 newScrollOrigin=NSMakePoint(0.0,0.0);
130 } else {
131 newScrollOrigin=NSMakePoint(0.0,NSMaxY([[scrollView documentView] frame])
132 -NSHeight([[scrollView contentView] bounds]));
133 }
134
135 [[scrollView documentView] scrollPoint:newScrollOrigin];
136}
137
138
139#pragma mark - AccountGeneralDelegate methods
140
141-(void) updateFrame {
142 if (accountAdvancedVC.view.superview == self.containerView.documentView) {
143 [self displayAllSettings];
144 return;
145 }
146 [self displayGeneralSettings];
147}
148
149-(void) triggerAdvancedOptions {
150 if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) {
151 [self displayGeneralSettings];
152 return;
153 }
154 [self displayAllSettings];
155}
156
157- (void) hide {
158 [self.view setHidden:YES];
159}
160
161@end