blob: 2433d84a34580c374949658761b9b4f4afc0f23b [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>
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040022#import <api/newaccountmodel.h>
23
24#import "views/NSColor+RingTheme.h"
25#import "AccountSettingsVC.h"
26#import "AccRingGeneralVC.h"
27#import "AccSipGeneralVC.h"
28#import "AccAdvancedRingVC.h"
29#import "AccAdvancedSipVC.h"
30
31@interface AccountSettingsVC ()
32
33@property (unsafe_unretained) IBOutlet NSScrollView *containerView;
34@property (unsafe_unretained) IBOutlet NSView *settingsView;
35
36@end
37
38@implementation AccountSettingsVC
39
40std::string selectedAccountID;
41NSViewController <AccountGeneralProtocol>* accountGeneralVC;
42NSViewController <AccountAdvancedProtocol>* accountAdvancedVC;
43AccRingGeneralVC* ringGeneralVC;
44AccSipGeneralVC* sipGeneralVC;
45AccAdvancedRingVC* ringAdvancedVC;
46AccAdvancedSipVC* sipAdvancedVC;
47
Kateryna Kostiukef66f972018-11-02 17:10:37 -040048CGFloat const VIEW_INSET = 40;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040049
50@synthesize accountModel;
51
52-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
53{
54 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
55 {
56 self.accountModel= accountModel;
57 }
58 ringGeneralVC = [[AccRingGeneralVC alloc] initWithNibName:@"AccRingGeneral" bundle:nil accountmodel: accountModel];
59 sipGeneralVC = [[AccSipGeneralVC alloc] initWithNibName:@"AccSipGeneral" bundle:nil accountmodel: accountModel];
60 ringAdvancedVC = [[AccAdvancedRingVC alloc] initWithNibName:@"AccAdvancedRing" bundle:nil accountmodel: accountModel];
61 sipAdvancedVC = [[AccAdvancedSipVC alloc] initWithNibName:@"AccAdvancedSip" bundle:nil accountmodel: accountModel];
62 return self;
63}
64
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040065- (void) initFrame
66{
67 [self.view setFrame:self.view.superview.bounds];
68 [self.view setHidden:YES];
69}
70
71- (void) setSelectedAccount:(std::string) account {
72 selectedAccountID = account;
73 const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID);
74 if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) {
75 accountGeneralVC = ringGeneralVC;
76 accountGeneralVC.delegate = self;
77 accountAdvancedVC = ringAdvancedVC;
78 } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){
79 accountGeneralVC = sipGeneralVC;
80 accountGeneralVC.delegate = self;
81 accountAdvancedVC = sipAdvancedVC;
82 } else {
83 [self hide];
84 return;
85 }
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -040086 [self.view.window makeFirstResponder:self.view];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040087 [accountGeneralVC setSelectedAccount: selectedAccountID];
88 [accountAdvancedVC setSelectedAccount: selectedAccountID];
89 [self displayGeneralSettings];
90}
91
92- (void) show {
93 [self.view setHidden:NO];
94 [self displayGeneralSettings];
Kateryna Kostiuka8525942018-10-17 14:33:39 -040095 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInset) name:NSWindowDidResizeNotification object:nil];
96}
97
98- (void)updateInset {
99 if(self.containerView.documentView.frame.size.height > (self.containerView.frame.size.height + VIEW_INSET) && self.containerView.contentInsets.bottom <= VIEW_INSET) {
100 return;
101 }
102 int bottomInset = self.containerView.frame.size.height - self.containerView.documentView.frame.size.height - VIEW_INSET;
103 self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400104}
105
106-(void)displayGeneralSettings {
107 self.containerView.documentView = accountGeneralVC.view;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400108 [self updateInset];
109
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400110}
111
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400112-(void)displayAllSettingAndScrollToAdvanced: (BOOL) shouldScroll {
113 CGRect visibleRect = self.containerView.visibleRect;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400114 CGRect settingsFrame = accountGeneralVC.view.frame;
115 settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height;
116 NSView* container = [[NSView alloc] initWithFrame:settingsFrame];
117 [container addSubview:accountAdvancedVC.view];
118 CGRect generalSettingsFrame = accountGeneralVC.view.frame;
119 generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height;
120 accountGeneralVC.view.frame = generalSettingsFrame;
121 [container addSubview:accountGeneralVC.view];
122 self.containerView.documentView = container;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400123 //return scroll to position it was before changing scroll document view
124 NSPoint oldOrigin = NSMakePoint(0.0, [[self.containerView documentView] frame].size.height
125 - NSHeight([[self.containerView contentView] bounds])
126 - (NSHeight([[accountGeneralVC view] bounds])
127 - visibleRect.size.height));
128 [[self.containerView documentView] scrollPoint: oldOrigin];
129 if(!shouldScroll) {
130 [self updateInset];
131 return;
132 }
133 //animte scroll to advanced option
134 [NSAnimationContext beginGrouping];
135 [[NSAnimationContext currentContext] setDuration:0.5];
136 NSClipView* clipView = [self.containerView contentView];
137 NSPoint newOrigin = NSMakePoint(0.0, accountAdvancedVC.view.frame.size.height
138 - visibleRect.size.height
139 + VIEW_INSET);
140 NSPoint clipViewOrigin = [clipView bounds].origin;
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400141 clipViewOrigin.y = clipViewOrigin.y - NSHeight([[accountGeneralVC view] bounds]) - VIEW_INSET * 0.5;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400142 CGFloat accountHeight = NSHeight([[accountGeneralVC view] bounds]);
143 CGFloat visibleHeight = visibleRect.size.height;
144 if(accountGeneralVC.view.frame.size.height > visibleRect.size.height) {
145 clipViewOrigin.y = clipViewOrigin.y
146 + accountGeneralVC.view.frame.size.height
147 - visibleRect.size.height
148 + VIEW_INSET;
149 }
150 [[clipView animator] setBoundsOrigin:clipViewOrigin];
151 [NSAnimationContext endGrouping];
152 [[self.containerView documentView] scrollPoint: newOrigin];
153 [self updateInset];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400154}
155
156-(void) scrollToTopScrollView: (NSScrollView *) scrollView {
157 NSPoint newScrollOrigin;
158 if ([[scrollView documentView] isFlipped]) {
159 newScrollOrigin=NSMakePoint(0.0,0.0);
160 } else {
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400161 newScrollOrigin=NSMakePoint(0.0, NSMaxY([[ self.containerView documentView] frame])
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400162 -NSHeight([[scrollView contentView] bounds]));
163 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400164 [[scrollView documentView] scrollPoint:newScrollOrigin];
165}
166
167
168#pragma mark - AccountGeneralDelegate methods
169
170-(void) updateFrame {
171 if (accountAdvancedVC.view.superview == self.containerView.documentView) {
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400172 [self displayAllSettingAndScrollToAdvanced: NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400173 return;
174 }
175 [self displayGeneralSettings];
176}
177
178-(void) triggerAdvancedOptions {
179 if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) {
180 [self displayGeneralSettings];
181 return;
182 }
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400183 [self displayAllSettingAndScrollToAdvanced: YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400184}
185
186- (void) hide {
187 [self.view setHidden:YES];
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400188 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400189}
190
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400191
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400192@end