blob: b3b03dd6ee92404837d2598c8e3df0b66355b6f8 [file] [log] [blame]
Alexandre Lision745e4d62015-03-22 20:03:10 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30#import "RingWizardWC.h"
31
32#import <accountmodel.h>
33#import <protocolmodel.h>
34#import <QItemSelectionModel>
35#import <account.h>
36
37#import "AppDelegate.h"
38
39
40@interface RingWizardWC ()
41@property (assign) IBOutlet NSButton *goToAppButton;
42@property (assign) IBOutlet NSTextField *nickname;
43@property (assign) IBOutlet NSProgressIndicator *progressBar;
44@property (assign) IBOutlet NSTextField *indicationLabel;
45@property (assign) IBOutlet NSButton *createButton;
46@end
47
48@implementation RingWizardWC
49@synthesize goToAppButton;
50@synthesize nickname;
51@synthesize progressBar;
52@synthesize indicationLabel;
53@synthesize createButton;
54
55- (void)windowDidLoad {
56 [super windowDidLoad];
57
58 [self.window makeKeyAndOrderFront:nil];
59 [self.window setLevel:NSStatusWindowLevel];
60 [self.window makeMainWindow];
61 [self checkForRingAccount];
62}
63
64- (void) checkForRingAccount
65{
66 for (int i = 0 ; i < AccountModel::instance()->rowCount() ; ++i) {
67 QModelIndex idx = AccountModel::instance()->index(i);
68 Account* acc = AccountModel::instance()->getAccountByModelIndex(idx);
69 if(acc->protocol() == Account::Protocol::RING) {
70 [indicationLabel setStringValue:@"Ring is already ready to work"];
71 [self displayHash:acc->username().toNSString()];
72 }
73 }
74}
75
76- (void) displayHash:(NSString* ) hash
77{
78 [nickname setFrameSize:NSMakeSize(400, nickname.frame.size.height)];
79 [nickname setStringValue:hash];
80 [nickname setEditable:NO];
81 [nickname setHidden:NO];
82
83 [goToAppButton setHidden:NO];
84
85 NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
86
87 [createButton setTitle:@"Share by mail"];
88 //[createButton setImage:emailSharingService.image];
89 [createButton setAlternateImage:emailSharingService.alternateImage];
90 [createButton setAction:@selector(shareByEmail)];
91}
92
93- (IBAction)createRingAccount:(id)sender {
94
95 [nickname setHidden:YES];
96 [progressBar setHidden:NO];
97 [createButton setEnabled:NO];
98 [indicationLabel setStringValue:@"Just a moment..."];
99
100 QModelIndex qIdx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
101
102 [self setCallback];
103 [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
104
Alexandre Lision41981972015-06-04 13:27:33 -0400105 [self registerAutoStartup];
106}
107
108/**
109 * Enable launch at startup by default
110 */
111- (void) registerAutoStartup
112{
113 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
114 if (loginItemsRef == nil) return;
115 CFURLRef appUrl = (CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
116 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
117 if (itemRef) CFRelease(itemRef);
Alexandre Lision745e4d62015-03-22 20:03:10 -0400118}
119
120- (void) saveAccount
121{
122 NSString* newAccName = @"My Ring";
123 Account* newAcc = AccountModel::instance()->add([newAccName UTF8String], Account::Protocol::RING);
124 newAcc->setAlias([[nickname stringValue] UTF8String]);
Alexandre Lisione3843ab2015-04-23 16:40:42 -0400125 newAcc->setUpnpEnabled(YES); // Always active upnp
Alexandre Lision745e4d62015-03-22 20:03:10 -0400126 newAcc << Account::EditAction::SAVE;
127}
128
129- (void) setCallback
130{
131 QObject::connect(AccountModel::instance(),
132 &AccountModel::accountStateChanged,
133 [=](Account *account, const Account::RegistrationState state) {
134 NSLog(@"Account created!");
135 [progressBar setHidden:YES];
136 [createButton setEnabled:YES];
137 [indicationLabel setStringValue:@"This is your number, share it with your friends!"];
138 [self displayHash:account->username().toNSString()];
139 });
140}
141
142- (IBAction)goToApp:(id)sender {
143 [self.window close];
144 AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
145 [appDelegate showMainWindow];
146}
147
148- (void) shareByEmail
149{
150 /*
151 Create the array of items to share.
152 Start with just the content of the text view. If there's an image, add that too.
153 */
154 NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:[nickname stringValue], nil];
155 NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
156
157 /*
158 Perform the service using the array of items.
159 */
160 [emailSharingService performWithItems:shareItems];
161}
162
163
164# pragma NSWindowDelegate methods
165
166- (BOOL)windowShouldClose:(id)sender
167{
168 NSLog(@"windowShouldClose");
169 return YES;
170}
171
172- (void)windowDidBecomeKey:(NSNotification *)notification
173{
174 NSLog(@"windowDidBecomeKey");
175}
176
177- (void)windowDidResignKey:(NSNotification *)notification
178{
179 NSLog(@"windowDidResignKey");
180}
181
182- (void)windowDidBecomeMain:(NSNotification *)notification
183{
184 NSLog(@"windowDidBecomeMain");
185}
186
187- (void)windowDidResignMain:(NSNotification *)notification
188{
189 NSLog(@"windowDidResignMain");
190 [self.window close];
191}
192
193- (void)windowWillClose:(NSNotification *)notification
194{
195 //NSLog(@"windowWillClose");
196 AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
197 [appDelegate showMainWindow];
198}
199
200@end