Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 1 | /* |
| 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 () |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 41 | @property (unsafe_unretained) IBOutlet NSButton *goToAppButton; |
| 42 | @property (unsafe_unretained) IBOutlet NSTextField *nickname; |
| 43 | @property (unsafe_unretained) IBOutlet NSProgressIndicator *progressBar; |
| 44 | @property (unsafe_unretained) IBOutlet NSTextField *indicationLabel; |
| 45 | @property (unsafe_unretained) IBOutlet NSButton *createButton; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 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 Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 105 | [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; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 115 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 116 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 117 | if (itemRef) CFRelease(itemRef); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 118 | } |
| 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 Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 125 | newAcc->setDisplayName([[nickname stringValue] UTF8String]); |
| 126 | |
Alexandre Lision | e3843ab | 2015-04-23 16:40:42 -0400 | [diff] [blame] | 127 | newAcc->setUpnpEnabled(YES); // Always active upnp |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 128 | newAcc << Account::EditAction::SAVE; |
| 129 | } |
| 130 | |
| 131 | - (void) setCallback |
| 132 | { |
| 133 | QObject::connect(AccountModel::instance(), |
| 134 | &AccountModel::accountStateChanged, |
| 135 | [=](Account *account, const Account::RegistrationState state) { |
| 136 | NSLog(@"Account created!"); |
| 137 | [progressBar setHidden:YES]; |
| 138 | [createButton setEnabled:YES]; |
| 139 | [indicationLabel setStringValue:@"This is your number, share it with your friends!"]; |
| 140 | [self displayHash:account->username().toNSString()]; |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | - (IBAction)goToApp:(id)sender { |
| 145 | [self.window close]; |
| 146 | AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
| 147 | [appDelegate showMainWindow]; |
| 148 | } |
| 149 | |
| 150 | - (void) shareByEmail |
| 151 | { |
| 152 | /* |
| 153 | Create the array of items to share. |
| 154 | Start with just the content of the text view. If there's an image, add that too. |
| 155 | */ |
| 156 | NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:[nickname stringValue], nil]; |
| 157 | NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail]; |
| 158 | |
| 159 | /* |
| 160 | Perform the service using the array of items. |
| 161 | */ |
| 162 | [emailSharingService performWithItems:shareItems]; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | # pragma NSWindowDelegate methods |
| 167 | |
| 168 | - (BOOL)windowShouldClose:(id)sender |
| 169 | { |
| 170 | NSLog(@"windowShouldClose"); |
| 171 | return YES; |
| 172 | } |
| 173 | |
| 174 | - (void)windowDidBecomeKey:(NSNotification *)notification |
| 175 | { |
| 176 | NSLog(@"windowDidBecomeKey"); |
| 177 | } |
| 178 | |
| 179 | - (void)windowDidResignKey:(NSNotification *)notification |
| 180 | { |
| 181 | NSLog(@"windowDidResignKey"); |
| 182 | } |
| 183 | |
| 184 | - (void)windowDidBecomeMain:(NSNotification *)notification |
| 185 | { |
| 186 | NSLog(@"windowDidBecomeMain"); |
| 187 | } |
| 188 | |
| 189 | - (void)windowDidResignMain:(NSNotification *)notification |
| 190 | { |
| 191 | NSLog(@"windowDidResignMain"); |
| 192 | [self.window close]; |
| 193 | } |
| 194 | |
| 195 | - (void)windowWillClose:(NSNotification *)notification |
| 196 | { |
| 197 | //NSLog(@"windowWillClose"); |
| 198 | AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
| 199 | [appDelegate showMainWindow]; |
| 200 | } |
| 201 | |
| 202 | @end |