blob: 373eb8048d58ae87de43edb9004784c98cb7b494 [file] [log] [blame]
Alexandre Lision745e4d62015-03-22 20:03:10 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision745e4d62015-03-22 20:03:10 -04003 * Author: Alexandre Lision <alexandre.lision@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.
Alexandre Lision745e4d62015-03-22 20:03:10 -040018 */
19#import "RingWizardWC.h"
20
Alexandre Lision54187a22016-04-04 09:45:52 -040021//Cocoa
22#import <AddressBook/AddressBook.h>
23#import <Quartz/Quartz.h>
24
Alexandre Lision745e4d62015-03-22 20:03:10 -040025
26#import "AppDelegate.h"
Alexandre Lision34607032016-02-08 16:16:49 -050027#import "Constants.h"
Alexandre Lision261f1b92016-04-04 12:35:34 -040028#import "views/NSImage+Extensions.h"
Alexandre Lision34607032016-02-08 16:16:49 -050029#import "views/NSColor+RingTheme.h"
Loïc Siretfcb4ca62016-09-21 17:12:09 -040030#import "RingWizardNewAccountVC.h"
31#import "RingWizardLinkAccountVC.h"
32#import "RingWizardChooseVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040033
Loïc Siretfcb4ca62016-09-21 17:12:09 -040034
35@interface RingWizardWC ()
36
37@property (retain, nonatomic)IBOutlet NSView* container;
38
39@end
Alexandre Lision08abfac2015-09-22 12:20:51 -040040@implementation RingWizardWC {
Loïc Siretfcb4ca62016-09-21 17:12:09 -040041 IBOutlet RingWizardNewAccountVC* newAccountWC;
42 IBOutlet RingWizardLinkAccountVC* linkAccountWC;
43 IBOutlet RingWizardChooseVC* chooseActiontWC;
44 float initialHeight;
45 float currentHeight;
46 BOOL isCancelable;
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040047}
48
Loïc Siretfcb4ca62016-09-21 17:12:09 -040049- (void)windowDidLoad
50{
Alexandre Lision745e4d62015-03-22 20:03:10 -040051 [super windowDidLoad];
52
Alexandre Lision34607032016-02-08 16:16:49 -050053 [self.window setBackgroundColor:[NSColor ringGreyHighlight]];
Loïc Siretfcb4ca62016-09-21 17:12:09 -040054 chooseActiontWC = [[RingWizardChooseVC alloc] initWithNibName:@"RingWizardChoose" bundle:nil];
55 [chooseActiontWC setDelegate:self];
56 linkAccountWC = [[RingWizardLinkAccountVC alloc] initWithNibName:@"RingWizardLinkAccount" bundle:nil];
57 [linkAccountWC setDelegate:self];
58 newAccountWC = [[RingWizardNewAccountVC alloc] initWithNibName:@"RingWizardNewAccount" bundle:nil];
59 [newAccountWC setDelegate:self];
60 initialHeight = self.window.frame.size.height;
61 currentHeight = self.window.frame.size.height;
62 isCancelable = NO;
63 [self showView:chooseActiontWC.view];
64}
Alexandre Lision34607032016-02-08 16:16:49 -050065
Loïc Siretfcb4ca62016-09-21 17:12:09 -040066- (void)removeSubviews
67{
68 while ([self.container.subviews count] > 0)
69 {
70 [[self.container.subviews firstObject] removeFromSuperview];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040071 }
Alexandre Lision745e4d62015-03-22 20:03:10 -040072}
73
Loïc Siretfcb4ca62016-09-21 17:12:09 -040074#define minHeight 135
75- (void)showView: (NSView*) view
Alexandre Lision54187a22016-04-04 09:45:52 -040076{
Loïc Siretfcb4ca62016-09-21 17:12:09 -040077 [self removeSubviews];
78 NSRect frame = [self.container frame];
79 frame.size.height = MAX(minHeight, view.bounds.size.height);
80 [view setFrame:frame];
81 [self.container setFrame:frame];
82 float size = 0;
83 NSView *container = self.window.contentView;
84 for (NSView *child in container.subviews){
85 size += child.frame.size.height;
Alexandre Lision54187a22016-04-04 09:45:52 -040086 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -040087 if (currentHeight != size){
88 currentHeight = size;
89 NSRect frameWindows = self.window.frame;
90 frameWindows.size.height = currentHeight;
91 [self.window setFrame:frameWindows display:YES animate:YES];
Alexandre Lision34607032016-02-08 16:16:49 -050092 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -040093 [self.container addSubview:view];
94}
95
96- (void)showChooseWithCancelButton:(BOOL)showCancel
97{
98 [self showView: chooseActiontWC.view];
99 [chooseActiontWC showCancelButton:showCancel];
100 isCancelable = showCancel;
101}
102
103- (void)showNewAccountVC
104{
105 [self showView: newAccountWC.view];
106 [newAccountWC show];
107}
108
109- (void)showLinkAccountVC
110{
111 [self showView: linkAccountWC.view];
112 [linkAccountWC show];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400113}
114
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400115# pragma NSWindowDelegate methods
Alexandre Lision745e4d62015-03-22 20:03:10 -0400116
117- (void)windowWillClose:(NSNotification *)notification
118{
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400119 if (!isCancelable){
120 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
121 if ([appDelegate checkForRingAccount]) {
122 [appDelegate showMainWindow];
123 }
124 }
125}
126
127#pragma - WizardChooseDelegate methods
128
129- (void)didCompleteWithAction:(WizardAction)action
130{
131 if (action == WIZARD_ACTION_LINK){
132 [self showLinkAccountVC];
133 } else if (action == WIZARD_ACTION_NEW){
134 [self showNewAccountVC];
135 } else {
136 [self.window close];
137 }
138
139}
140
141#pragma - WizardCreateAccountDelegate methods
142
143- (void)didCreateAccountWithSuccess:(BOOL)success
144{
145 if (success) {
146 [self.window close];
147 if (!isCancelable){
148 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
149 [appDelegate showMainWindow];
150 }
151 } else {
152 [self showChooseWithCancelButton:isCancelable];
153 }
154}
155
156#pragma - WizardLinkAccountDelegate methods
157
158- (void)didLinkAccountWithSuccess:(BOOL)success
159{
160 if (success) {
161 [self.window close];
162 if (!isCancelable){
163 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
164 [appDelegate showMainWindow];
165 }
166 } else {
167 [self showChooseWithCancelButton:isCancelable];
Alexandre Lision76d59692016-01-20 18:06:05 -0500168 }
Alexandre Lision745e4d62015-03-22 20:03:10 -0400169}
170
171@end