blob: 44eed7871cab002904c9f7e9eaca6a38a9c9b918 [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"
Alexandre Lision849514f2016-10-25 14:07:51 -040041#import "views/NSImage+Extensions.h"
Loïc Siretfcb4ca62016-09-21 17:12:09 -040042
43@interface RingWizardLinkAccountVC ()
44
45@end
46
47@implementation RingWizardLinkAccountVC {
48 __unsafe_unretained IBOutlet NSView* initialContainer;
Alexandre Lision9c1ec622016-11-14 21:40:59 -050049 __unsafe_unretained IBOutlet NSView* firstStepContainer;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040050
51 __unsafe_unretained IBOutlet NSView* loadingContainer;
52 __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
53
54 __unsafe_unretained IBOutlet NSView* errorContainer;
55
56 Account* accountToCreate;
57 NSTimer* errorTimer;
58 QMetaObject::Connection stateChanged;
59}
60
Alexandre Lision9c1ec622016-11-14 21:40:59 -050061- (IBAction)goToStepTwo:(id)sender
62{
63 [self disconnectCallback];
64 [firstStepContainer setHidden:YES];
65 [initialContainer setHidden:NO];
66 [loadingContainer setHidden:YES];
67 [errorContainer setHidden:YES];
68}
69
70- (IBAction)goToStepOne:(id)sender
71{
72 [firstStepContainer setHidden:NO];
73 [initialContainer setHidden:YES];
74 [loadingContainer setHidden:YES];
75 [errorContainer setHidden:YES];
76}
77
Loïc Siretfcb4ca62016-09-21 17:12:09 -040078- (void)show
79{
Alexandre Lision9c1ec622016-11-14 21:40:59 -050080 [firstStepContainer setHidden:NO];
81 [initialContainer setHidden:YES];
Loïc Siretfcb4ca62016-09-21 17:12:09 -040082 [loadingContainer setHidden:YES];
83 [errorContainer setHidden:YES];
84}
85
86- (void)showError
87{
88 [initialContainer setHidden:YES];
89 [loadingContainer setHidden:YES];
90 [errorContainer setHidden:NO];
91}
92- (void)showLoading
93{
94 [initialContainer setHidden:YES];
95 [loadingContainer setHidden:NO];
96 [progressBar startAnimation:nil];
97 [errorContainer setHidden:YES];
98}
99
100- (IBAction)importRingAccount:(id)sender
101{
102 [self showLoading];
103 if (auto profile = ProfileModel::instance().selectedProfile()) {
104 profile->person()->setFormattedName([NSFullUserName() UTF8String]);
Alexandre Lision849514f2016-10-25 14:07:51 -0400105 auto defaultAvatar = [NSImage imageResize:[NSImage imageNamed:@"default_user_icon"] newSize:{100,100}];
106 QPixmap pixMap;
107 pixMap.loadFromData(QByteArray::fromNSData([defaultAvatar TIFFRepresentation]));
108 profile->person()->setPhoto(QVariant(pixMap));
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400109 profile->save();
110 }
111 accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING);
112 accountToCreate->setArchivePin(QString::fromNSString(self.pinValue));
113 accountToCreate->setArchivePassword(QString::fromNSString(self.passwordValue));
114
115 [self setCallback];
116
117 [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
118 [self registerDefaultPreferences];
119}
120
121- (IBAction)dismissViewWithError:(id)sender
122{
123 [self.delegate didLinkAccountWithSuccess:NO];
124}
125
126- (IBAction)back:(id)sender
127{
Alexandre Lision9c1ec622016-11-14 21:40:59 -0500128 [self deleteAccount];
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400129 [self show];
130}
131
132/**
133 * Set default values for preferences
134 */
135- (void)registerDefaultPreferences
136{
137 // enable AutoStartup
138 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
139 if (loginItemsRef == nil) return;
140 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
141 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
142 if (itemRef) CFRelease(itemRef);
143
144 // enable Notifications
145 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications];
146}
147
148- (void)saveAccount
149{
150 accountToCreate->setUpnpEnabled(YES); // Always active upnp
151 accountToCreate << Account::EditAction::SAVE;
152}
153
Alexandre Lision9c1ec622016-11-14 21:40:59 -0500154- (void)deleteAccount
155{
156 if(auto account = AccountModel::instance().getById(accountToCreate->id())) {
157 AccountModel::instance().remove(accountToCreate);
158 AccountModel::instance().save();
159 }
160}
161
162- (void)disconnectCallback
163{
164 [errorTimer invalidate];
165 QObject::disconnect(stateChanged);
166}
167
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400168- (void)setCallback
169{
170 errorTimer = [NSTimer scheduledTimerWithTimeInterval:30
171 target:self
172 selector:@selector(didLinkFailed) userInfo:nil
173 repeats:NO];
174
175 stateChanged = QObject::connect(&AccountModel::instance(),
176 &AccountModel::accountStateChanged,
177 [=](Account *account, const Account::RegistrationState state) {
178 switch(state){
179 case Account::RegistrationState::READY:
180 case Account::RegistrationState::TRYING:
181 case Account::RegistrationState::UNREGISTERED:{
182 accountToCreate<< Account::EditAction::RELOAD;
183 QObject::disconnect(stateChanged);
184 [errorTimer invalidate];
185 [self.delegate didLinkAccountWithSuccess:YES];
186 break;
187 }
188 case Account::RegistrationState::ERROR:
189 QObject::disconnect(stateChanged);
190 [errorTimer invalidate];
191 [self showError];
192 break;
193 case Account::RegistrationState::INITIALIZING:
194 case Account::RegistrationState::COUNT__:{
195 //DO Nothing
196 break;
197 }
198 }
199 });
200}
201
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400202- (void)didLinkFailed
203{
204 [self showError];
205}
206
207@end