RingWizardWC.mm 8.3 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

Alexandre Lision's avatar
Alexandre Lision committed
21 22 23
//Cocoa
#import <Quartz/Quartz.h>

24
#import "RingWizardWC.h"
25
#import "AppDelegate.h"
Alexandre Lision's avatar
Alexandre Lision committed
26
#import "Constants.h"
27
#import "views/NSImage+Extensions.h"
Alexandre Lision's avatar
Alexandre Lision committed
28
#import "views/NSColor+RingTheme.h"
29 30 31
#import "RingWizardNewAccountVC.h"
#import "RingWizardLinkAccountVC.h"
#import "RingWizardChooseVC.h"
32

33
@interface RingWizardWC ()
34

35
@property (retain, nonatomic)IBOutlet NSView* container;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
36 37 38
@property (retain, nonatomic)IBOutlet NSTextField* windowHeader;
@property (retain, nonatomic)IBOutlet NSImageView* ringImage;
@property (retain, nonatomic)IBOutlet NSLayoutConstraint* titleConstraint;
39

40 41 42 43 44
@end
@implementation RingWizardWC {
    IBOutlet RingWizardNewAccountVC* newAccountWC;
    IBOutlet RingWizardLinkAccountVC* linkAccountWC;
    IBOutlet RingWizardChooseVC* chooseActiontWC;
45
    IBOutlet AddSIPAccountVC* addSIPAccountVC;
46
    BOOL isCancelable;
47
    BOOL withAdvanced;
48
}
49

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
50
@synthesize accountModel, ringImage, titleConstraint;
51

52 53 54 55 56 57
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel;
{
    if (self =  [self initWithWindowNibName:nibNameOrNil])
    {
        self.accountModel = accountModel;
    }
58 59 60
    return self;
}

61 62
- (void)windowDidLoad
{
63
    [super windowDidLoad];
64 65 66 67 68 69
    newAccountWC = [[RingWizardNewAccountVC alloc] initWithNibName:@"RingWizardNewAccount" bundle:nil accountmodel:self.accountModel];

    chooseActiontWC = [[RingWizardChooseVC alloc] initWithNibName:@"RingWizardChoose" bundle:nil];
    linkAccountWC = [[RingWizardLinkAccountVC alloc] initWithNibName:@"RingWizardLinkAccount" bundle:nil accountmodel:self.accountModel];
    addSIPAccountVC = [[AddSIPAccountVC alloc] initWithNibName:@"AddSIPAccountVC" bundle:nil accountmodel:self.accountModel];
    [addSIPAccountVC setDelegate:self];
70 71 72
    [chooseActiontWC setDelegate:self];
    [linkAccountWC setDelegate:self];
    [newAccountWC setDelegate:self];
73
    [self showChooseWithCancelButton:isCancelable andAdvanced: withAdvanced];
74 75
}

76
- (void)removeSubviews
Alexandre Lision's avatar
Alexandre Lision committed
77
{
78 79 80 81
    while ([self.container.subviews count] > 0)
    {
        [[self.container.subviews firstObject] removeFromSuperview];
    }
82 83
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
84 85
#define headerHeight 70
#define minHeight 140
86
#define defaultMargin 5
87
- (void)showView:(NSView*)view
88
{
89 90
    [self removeSubviews];
    NSRect frame = [self.container frame];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
91
    CGFloat height = minHeight;
92
    float sizeFrame = MAX(height, view.frame.size.height);
93
    frame.size.height = sizeFrame;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
94
    [view setFrame: frame];
95
    [self.container setFrame:frame];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
96 97 98 99
    float titleBarHeight = self.window.frame.size.height -
    [NSWindow contentRectForFrameRect: self.window.frame styleMask:self.window.styleMask].size.height;
    titleBarHeight = self.window.isSheet ? 0 : titleBarHeight;
    float size = headerHeight + sizeFrame + titleBarHeight;
100 101 102
    NSRect frameWindows = self.window.frame;
    frameWindows.size.height = size;
    [self.window setFrame:frameWindows display:YES animate:YES];
103
    [self.container addSubview:view];
104 105
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
106 107 108 109 110
- (void) updateFrame:(float) height {
    float titleBarHeight = self.window.frame.size.height -
    [NSWindow contentRectForFrameRect: self.window.frame styleMask:self.window.styleMask].size.height;
    titleBarHeight = self.window.isSheet ? 0 : titleBarHeight;
    float size = headerHeight + height + titleBarHeight;
111 112 113 114 115 116
    NSRect frameWindows = self.window.frame;
    frameWindows.size.height = size;
    [self.window setFrame:frameWindows display:YES animate:YES];
}

- (void)showChooseWithCancelButton:(BOOL)showCancel andAdvanced:(BOOL)showAdvanced {
117
    [self.windowHeader setStringValue: NSLocalizedString(@"Welcome to Jami",
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
118 119 120 121
                                                        @"Welcome title")];
    [ringImage setHidden: NO];
    titleConstraint.constant = -26.5;
    [self showView:chooseActiontWC.view];
122 123 124 125
    [chooseActiontWC showCancelButton:showCancel];
    [chooseActiontWC showAdvancedButton:showAdvanced];
    isCancelable = showCancel;
    withAdvanced = showAdvanced;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
126
    [chooseActiontWC updateFrame];
127 128 129
    [self showView:chooseActiontWC.view];
}

130
- (void)showChooseWithCancelButton:(BOOL)showCancel
131
{
132
    [self.windowHeader setStringValue: NSLocalizedString(@"Welcome to Jami",
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
133 134 135 136
                                                         @"Welcome title")];
    [ringImage setHidden: NO];
    titleConstraint.constant = -26.5;
    [self showView:chooseActiontWC.view];
137 138
    [chooseActiontWC showCancelButton:showCancel];
    isCancelable = showCancel;
139 140
}

141
- (void)showNewAccountVC
142
{
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
143 144 145 146 147
    [self.windowHeader setStringValue: NSLocalizedString(@"Create a new account",
                                                         @"Welcome title")];
    [chooseActiontWC showCancelButton: isCancelable];
    [ringImage setHidden: YES];
    titleConstraint.constant = 0;
148 149
    [self showView: newAccountWC.view];
    [newAccountWC show];
150 151
}

152
- (void)showLinkAccountVC
153
{
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
154 155 156 157 158
    [self.windowHeader setStringValue: NSLocalizedString(@"Link to an account",
                                                         @"link account title")];
    [ringImage setHidden: YES];
    titleConstraint.constant = 0;
    [chooseActiontWC showCancelButton: isCancelable];
159 160
    [self showView: linkAccountWC.view];
    [linkAccountWC show];
161 162
}

163 164
- (void)showSIPAccountVC
{
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
165 166 167 168
    [self.windowHeader setStringValue: NSLocalizedString(@"Add a SIP account",
                                                         @"Welcome title")];
    [ringImage setHidden: YES];
    titleConstraint.constant = 0;
169
    [self showView: addSIPAccountVC.view];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
170
    [chooseActiontWC showAdvancedButton: NO];
171 172 173
    [addSIPAccountVC show];
}

174 175 176
# pragma NSWindowDelegate methods

- (void)windowWillClose:(NSNotification *)notification
177
{
178 179 180 181 182 183
    if (!isCancelable){
        AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
        if ([appDelegate checkForRingAccount]) {
            [appDelegate showMainWindow];
        }
    }
184 185
}

186
#pragma - WizardChooseDelegate methods
187

188
- (void)didCompleteWithAction:(WizardAction)action
189
{
190
    if (action == WIZARD_ACTION_LINK) {
191
        [self showLinkAccountVC];
192
    } else if (action == WIZARD_ACTION_NEW) {
193
        [self showNewAccountVC];
194
    } else if (action == WIZARD_ACTION_ADVANCED) {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
195
        [self showView:chooseActiontWC.view];
196 197
    } else if (action == WIZARD_ACTION_SIP_ACCOUNT) {
        [self showSIPAccountVC];
198 199
    } else {
        [self.window close];
200
        [NSApp endSheet:self.window];
201
        [[NSApplication sharedApplication] removeWindowsItem:self.window];
202
    }
203 204
}

205
#pragma - WizardCreateAccountDelegate methods
206

207
- (void)didCreateAccountWithSuccess:(BOOL)success
208
{
209 210
    if (success) {
        [self.window close];
211
        [NSApp endSheet:self.window];
212
        [[NSApplication sharedApplication] removeWindowsItem:self.window];
213 214 215 216 217
        if (!isCancelable){
            AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
            [appDelegate showMainWindow];
        }
    } else {
218
        [self showChooseWithCancelButton: isCancelable andAdvanced: withAdvanced];
Alexandre Lision's avatar
Alexandre Lision committed
219
    }
220 221
}

222
#pragma - WizardLinkAccountDelegate methods
223

224
- (void)didLinkAccountWithSuccess:(BOOL)success
225
{
226 227
    if (success) {
        [self.window close];
228
        [NSApp endSheet:self.window];
229
        [[NSApplication sharedApplication] removeWindowsItem:self.window];
230 231 232 233 234
        if (!isCancelable){
            AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
            [appDelegate showMainWindow];
        }
    } else {
235
        [self showChooseWithCancelButton: isCancelable andAdvanced: withAdvanced];
236
    }
237 238 239
}

@end