blob: a30a69fd980626e749d5021a95409100bc1bcb5e [file] [log] [blame]
Loïc Siretfcb4ca62016-09-21 17:12:09 -04001/*
2 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 * Author: Loïc Siret <loic.siret@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#import "RingWizardLinkAccountVC.h"
21//Cocoa
22#import <AddressBook/AddressBook.h>
23#import <Quartz/Quartz.h>
24
25//Qt
26#import <QUrl>
27#import <QPixmap>
28
29//LRC
30#import <accountmodel.h>
31#import <protocolmodel.h>
32#import <profilemodel.h>
33#import <QItemSelectionModel>
34#import <account.h>
35#import <certificate.h>
36#import <profilemodel.h>
37#import <profile.h>
38#import <person.h>
39
40#import "Constants.h"
41
42@interface RingWizardLinkAccountVC ()
43
44@end
45
46@implementation RingWizardLinkAccountVC {
47 __unsafe_unretained IBOutlet NSView* initialContainer;
48 __unsafe_unretained IBOutlet NSTextField* pinField;
49 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
50 __unsafe_unretained IBOutlet NSTextField* pinLabel;
51 __unsafe_unretained IBOutlet NSTextField* passwordLabel;
52 __unsafe_unretained IBOutlet NSButton* createButton;
53
54 __unsafe_unretained IBOutlet NSView* loadingContainer;
55 __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
56
57 __unsafe_unretained IBOutlet NSView* errorContainer;
58
59 Account* accountToCreate;
60 NSTimer* errorTimer;
61 QMetaObject::Connection stateChanged;
62}
63
64- (void)show
65{
66 [initialContainer setHidden:NO];
67 [loadingContainer setHidden:YES];
68 [errorContainer setHidden:YES];
69}
70
71- (void)showError
72{
73 [initialContainer setHidden:YES];
74 [loadingContainer setHidden:YES];
75 [errorContainer setHidden:NO];
76}
77- (void)showLoading
78{
79 [initialContainer setHidden:YES];
80 [loadingContainer setHidden:NO];
81 [progressBar startAnimation:nil];
82 [errorContainer setHidden:YES];
83}
84
85- (IBAction)importRingAccount:(id)sender
86{
87 [self showLoading];
88 if (auto profile = ProfileModel::instance().selectedProfile()) {
89 profile->person()->setFormattedName([NSFullUserName() UTF8String]);
90 profile->save();
91 }
92 accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING);
93 accountToCreate->setArchivePin(QString::fromNSString(self.pinValue));
94 accountToCreate->setArchivePassword(QString::fromNSString(self.passwordValue));
95
96 [self setCallback];
97
98 [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
99 [self registerDefaultPreferences];
100}
101
102- (IBAction)dismissViewWithError:(id)sender
103{
104 [self.delegate didLinkAccountWithSuccess:NO];
105}
106
107- (IBAction)back:(id)sender
108{
109 [self show];
110}
111
112/**
113 * Set default values for preferences
114 */
115- (void)registerDefaultPreferences
116{
117 // enable AutoStartup
118 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
119 if (loginItemsRef == nil) return;
120 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
121 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
122 if (itemRef) CFRelease(itemRef);
123
124 // enable Notifications
125 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications];
126}
127
128- (void)saveAccount
129{
130 accountToCreate->setUpnpEnabled(YES); // Always active upnp
131 accountToCreate << Account::EditAction::SAVE;
132}
133
134- (void)setCallback
135{
136 errorTimer = [NSTimer scheduledTimerWithTimeInterval:30
137 target:self
138 selector:@selector(didLinkFailed) userInfo:nil
139 repeats:NO];
140
141 stateChanged = QObject::connect(&AccountModel::instance(),
142 &AccountModel::accountStateChanged,
143 [=](Account *account, const Account::RegistrationState state) {
144 switch(state){
145 case Account::RegistrationState::READY:
146 case Account::RegistrationState::TRYING:
147 case Account::RegistrationState::UNREGISTERED:{
148 accountToCreate<< Account::EditAction::RELOAD;
149 QObject::disconnect(stateChanged);
150 [errorTimer invalidate];
151 [self.delegate didLinkAccountWithSuccess:YES];
152 break;
153 }
154 case Account::RegistrationState::ERROR:
155 QObject::disconnect(stateChanged);
156 [errorTimer invalidate];
157 [self showError];
158 break;
159 case Account::RegistrationState::INITIALIZING:
160 case Account::RegistrationState::COUNT__:{
161 //DO Nothing
162 break;
163 }
164 }
165 });
166}
167
168
169- (void)didLinkFailed
170{
171 [self showError];
172}
173
174@end