blob: 57ed1d939710bd3fc845a23cff16943ecccf2565 [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
105}
106
107- (void) saveAccount
108{
109 NSString* newAccName = @"My Ring";
110 Account* newAcc = AccountModel::instance()->add([newAccName UTF8String], Account::Protocol::RING);
111 newAcc->setAlias([[nickname stringValue] UTF8String]);
112 newAcc << Account::EditAction::SAVE;
113}
114
115- (void) setCallback
116{
117 QObject::connect(AccountModel::instance(),
118 &AccountModel::accountStateChanged,
119 [=](Account *account, const Account::RegistrationState state) {
120 NSLog(@"Account created!");
121 [progressBar setHidden:YES];
122 [createButton setEnabled:YES];
123 [indicationLabel setStringValue:@"This is your number, share it with your friends!"];
124 [self displayHash:account->username().toNSString()];
125 });
126}
127
128- (IBAction)goToApp:(id)sender {
129 [self.window close];
130 AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
131 [appDelegate showMainWindow];
132}
133
134- (void) shareByEmail
135{
136 /*
137 Create the array of items to share.
138 Start with just the content of the text view. If there's an image, add that too.
139 */
140 NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:[nickname stringValue], nil];
141 NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
142
143 /*
144 Perform the service using the array of items.
145 */
146 [emailSharingService performWithItems:shareItems];
147}
148
149
150# pragma NSWindowDelegate methods
151
152- (BOOL)windowShouldClose:(id)sender
153{
154 NSLog(@"windowShouldClose");
155 return YES;
156}
157
158- (void)windowDidBecomeKey:(NSNotification *)notification
159{
160 NSLog(@"windowDidBecomeKey");
161}
162
163- (void)windowDidResignKey:(NSNotification *)notification
164{
165 NSLog(@"windowDidResignKey");
166}
167
168- (void)windowDidBecomeMain:(NSNotification *)notification
169{
170 NSLog(@"windowDidBecomeMain");
171}
172
173- (void)windowDidResignMain:(NSNotification *)notification
174{
175 NSLog(@"windowDidResignMain");
176 [self.window close];
177}
178
179- (void)windowWillClose:(NSNotification *)notification
180{
181 //NSLog(@"windowWillClose");
182 AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
183 [appDelegate showMainWindow];
184}
185
186@end