blob: ac7aa117b974cd3a72721c811ba404eb6b24422a [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
Kateryna Kostiukef66f972018-11-02 17:10:37 -040049CGFloat const VIEW_INSET = 40;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040050
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
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040066- (void) initFrame
67{
68 [self.view setFrame:self.view.superview.bounds];
69 [self.view setHidden:YES];
70}
71
72- (void) setSelectedAccount:(std::string) account {
73 selectedAccountID = account;
74 const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID);
75 if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) {
76 accountGeneralVC = ringGeneralVC;
77 accountGeneralVC.delegate = self;
78 accountAdvancedVC = ringAdvancedVC;
79 } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){
80 accountGeneralVC = sipGeneralVC;
81 accountGeneralVC.delegate = self;
82 accountAdvancedVC = sipAdvancedVC;
83 } else {
84 [self hide];
85 return;
86 }
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -040087 [self.view.window makeFirstResponder:self.view];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040088 [accountGeneralVC setSelectedAccount: selectedAccountID];
89 [accountAdvancedVC setSelectedAccount: selectedAccountID];
90 [self displayGeneralSettings];
91}
92
93- (void) show {
94 [self.view setHidden:NO];
95 [self displayGeneralSettings];
Kateryna Kostiuka8525942018-10-17 14:33:39 -040096 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInset) name:NSWindowDidResizeNotification object:nil];
97}
98
99- (void)updateInset {
100 if(self.containerView.documentView.frame.size.height > (self.containerView.frame.size.height + VIEW_INSET) && self.containerView.contentInsets.bottom <= VIEW_INSET) {
101 return;
102 }
103 int bottomInset = self.containerView.frame.size.height - self.containerView.documentView.frame.size.height - VIEW_INSET;
104 self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400105}
106
107-(void)displayGeneralSettings {
108 self.containerView.documentView = accountGeneralVC.view;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400109 [self updateInset];
110
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400111}
112
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400113-(void)displayAllSettingAndScrollToAdvanced: (BOOL) shouldScroll {
114 CGRect visibleRect = self.containerView.visibleRect;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400115 CGRect settingsFrame = accountGeneralVC.view.frame;
116 settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height;
117 NSView* container = [[NSView alloc] initWithFrame:settingsFrame];
118 [container addSubview:accountAdvancedVC.view];
119 CGRect generalSettingsFrame = accountGeneralVC.view.frame;
120 generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height;
121 accountGeneralVC.view.frame = generalSettingsFrame;
122 [container addSubview:accountGeneralVC.view];
123 self.containerView.documentView = container;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400124 //return scroll to position it was before changing scroll document view
125 NSPoint oldOrigin = NSMakePoint(0.0, [[self.containerView documentView] frame].size.height
126 - NSHeight([[self.containerView contentView] bounds])
127 - (NSHeight([[accountGeneralVC view] bounds])
128 - visibleRect.size.height));
129 [[self.containerView documentView] scrollPoint: oldOrigin];
130 if(!shouldScroll) {
131 [self updateInset];
132 return;
133 }
134 //animte scroll to advanced option
135 [NSAnimationContext beginGrouping];
136 [[NSAnimationContext currentContext] setDuration:0.5];
137 NSClipView* clipView = [self.containerView contentView];
138 NSPoint newOrigin = NSMakePoint(0.0, accountAdvancedVC.view.frame.size.height
139 - visibleRect.size.height
140 + VIEW_INSET);
141 NSPoint clipViewOrigin = [clipView bounds].origin;
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400142 clipViewOrigin.y = clipViewOrigin.y - NSHeight([[accountGeneralVC view] bounds]) - VIEW_INSET * 0.5;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400143 CGFloat accountHeight = NSHeight([[accountGeneralVC view] bounds]);
144 CGFloat visibleHeight = visibleRect.size.height;
145 if(accountGeneralVC.view.frame.size.height > visibleRect.size.height) {
146 clipViewOrigin.y = clipViewOrigin.y
147 + accountGeneralVC.view.frame.size.height
148 - visibleRect.size.height
149 + VIEW_INSET;
150 }
151 [[clipView animator] setBoundsOrigin:clipViewOrigin];
152 [NSAnimationContext endGrouping];
153 [[self.containerView documentView] scrollPoint: newOrigin];
154 [self updateInset];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400155}
156
157-(void) scrollToTopScrollView: (NSScrollView *) scrollView {
158 NSPoint newScrollOrigin;
159 if ([[scrollView documentView] isFlipped]) {
160 newScrollOrigin=NSMakePoint(0.0,0.0);
161 } else {
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400162 newScrollOrigin=NSMakePoint(0.0, NSMaxY([[ self.containerView documentView] frame])
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400163 -NSHeight([[scrollView contentView] bounds]));
164 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400165 [[scrollView documentView] scrollPoint:newScrollOrigin];
166}
167
168
169#pragma mark - AccountGeneralDelegate methods
170
171-(void) updateFrame {
172 if (accountAdvancedVC.view.superview == self.containerView.documentView) {
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400173 [self displayAllSettingAndScrollToAdvanced: NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400174 return;
175 }
176 [self displayGeneralSettings];
177}
178
179-(void) triggerAdvancedOptions {
180 if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) {
181 [self displayGeneralSettings];
182 return;
183 }
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400184 [self displayAllSettingAndScrollToAdvanced: YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400185}
186
187- (void) hide {
188 [self.view setHidden:YES];
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400189 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400190}
191
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400192
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400193@end