Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 19 | #import "GeneralPrefsVC.h" |
| 20 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 21 | #import <Quartz/Quartz.h> |
| 22 | |
| 23 | //Qt |
| 24 | #import <QSize> |
| 25 | #import <QtMacExtras/qmacfunctions.h> |
| 26 | #import <QPixmap> |
| 27 | |
| 28 | //LRC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 29 | #import <categorizedhistorymodel.h> |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 30 | #import <profilemodel.h> |
| 31 | #import <profile.h> |
| 32 | #import <person.h> |
| 33 | #import <globalinstances.h> |
Kateryna Kostiuk | 9aef849 | 2017-06-22 16:58:36 -0400 | [diff] [blame] | 34 | #import <media/recordingmodel.h> |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 35 | #import <api/datatransfermodel.h> |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 36 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 37 | #if ENABLE_SPARKLE |
| 38 | #import <Sparkle/Sparkle.h> |
| 39 | #endif |
| 40 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 41 | #import "Constants.h" |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 42 | #import "views/NSImage+Extensions.h" |
| 43 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 44 | #import "utils.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 45 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 46 | @interface GeneralPrefsVC () { |
| 47 | __unsafe_unretained IBOutlet NSTextField* historyChangedLabel; |
| 48 | __unsafe_unretained IBOutlet NSButton* startUpButton; |
| 49 | __unsafe_unretained IBOutlet NSButton* toggleAutomaticUpdateCheck; |
| 50 | __unsafe_unretained IBOutlet NSPopUpButton* checkIntervalPopUp; |
| 51 | __unsafe_unretained IBOutlet NSView* sparkleContainer; |
| 52 | __unsafe_unretained IBOutlet NSTextField* historyTextField; |
| 53 | __unsafe_unretained IBOutlet NSStepper* historyStepper; |
| 54 | __unsafe_unretained IBOutlet NSButton* historySwitch; |
| 55 | __unsafe_unretained IBOutlet NSButton* photoView; |
| 56 | __unsafe_unretained IBOutlet NSTextField* profileNameField; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet NSButton *downloadFolder; |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 59 | __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 60 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 61 | @end |
| 62 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 63 | @implementation GeneralPrefsVC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 64 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 65 | @synthesize dataTransferModel; |
| 66 | |
| 67 | |
| 68 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::DataTransferModel*) dataTransferModel |
| 69 | { |
| 70 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 71 | { |
| 72 | self.dataTransferModel = dataTransferModel; |
| 73 | } |
| 74 | return self; |
| 75 | } |
| 76 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 77 | - (void)loadView |
| 78 | { |
| 79 | [super loadView]; |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 80 | [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 81 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 82 | [startUpButton setState:[self isLaunchAtStartup]]; |
| 83 | |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 84 | int historyLimit = CategorizedHistoryModel::instance().historyLimit(); |
| 85 | [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]]; |
| 86 | [historyStepper setIntValue:historyLimit]; |
| 87 | |
| 88 | BOOL limited = CategorizedHistoryModel::instance().isHistoryLimited(); |
| 89 | [historySwitch setState:limited]; |
| 90 | [historyStepper setEnabled:limited]; |
| 91 | [historyTextField setEnabled:limited]; |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 92 | #if ENABLE_SPARKLE |
| 93 | [sparkleContainer setHidden:NO]; |
| 94 | SUUpdater *updater = [SUUpdater sharedUpdater]; |
| 95 | [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 96 | |
| 97 | [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 98 | [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil]; |
| 99 | #else |
| 100 | [sparkleContainer setHidden:YES]; |
| 101 | #endif |
| 102 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 103 | [photoView setWantsLayer: YES]; |
| 104 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 105 | photoView.layer.masksToBounds = YES; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 106 | [addProfilePhotoImage setWantsLayer: YES]; |
| 107 | [addProfilePhotoImage setHidden:NO]; |
| 108 | [photoView setBordered:YES]; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 109 | |
| 110 | if (auto pro = ProfileModel::instance().selectedProfile()) { |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 111 | [profileNameField setStringValue:pro->person()->formattedName().toNSString()]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 112 | if (pro->person() && pro->person()->photo().isValid()) { |
| 113 | auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140}); |
| 114 | [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))]; |
| 115 | [addProfilePhotoImage setHidden:YES]; |
| 116 | [photoView setBordered:NO]; |
| 117 | } |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 118 | } |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 119 | if (appSandboxed()) { |
| 120 | [downloadFolder setHidden:YES]; |
| 121 | [downloadFolder setEnabled:NO]; |
| 122 | [downloadFolderLabel setHidden: YES]; |
| 123 | return; |
| 124 | } |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 125 | if (dataTransferModel) { |
| 126 | downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent]; |
| 127 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 130 | - (void) dealloc |
| 131 | { |
| 132 | [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 135 | - (IBAction)clearHistory:(id)sender { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 136 | CategorizedHistoryModel::instance().clearAllCollections(); |
Kateryna Kostiuk | 9aef849 | 2017-06-22 16:58:36 -0400 | [diff] [blame] | 137 | Media::RecordingModel::instance().clearAllCollections(); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 138 | [historyChangedLabel setHidden:NO]; |
| 139 | } |
| 140 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 141 | - (IBAction)toggleHistory:(NSButton*)sender { |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 142 | CategorizedHistoryModel::instance().setHistoryLimited([sender state]); |
| 143 | int historyLimit = CategorizedHistoryModel::instance().historyLimit(); |
| 144 | [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]]; |
| 145 | [historyStepper setIntValue:historyLimit]; |
| 146 | [historyChangedLabel setHidden:NO]; |
| 147 | [historyStepper setEnabled:[sender state]]; |
| 148 | [historyTextField setEnabled:[sender state]]; |
| 149 | } |
| 150 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 151 | - (IBAction)changeDownloadFolder:(id)sender { |
| 152 | |
| 153 | NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 154 | [panel setAllowsMultipleSelection:NO]; |
| 155 | [panel setCanChooseDirectories:YES]; |
| 156 | [panel setCanChooseFiles:NO]; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 157 | panel.delegate = self; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 158 | if ([panel runModal] != NSFileHandlingPanelOKButton) return; |
| 159 | if ([[panel URLs] lastObject] == nil) return; |
| 160 | NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"]; |
| 161 | dataTransferModel->downloadDirectory = std::string([path UTF8String]); |
| 162 | downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent]; |
| 163 | [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder]; |
| 164 | } |
| 165 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 166 | // KVO handler |
| 167 | -(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject |
| 168 | change:(NSDictionary *)aChange context:(void *)aContext |
| 169 | { |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 170 | if ([aKeyPath isEqualToString:Preferences::HistoryLimit]) { |
| 171 | CategorizedHistoryModel::instance().setHistoryLimit([[aChange objectForKey: NSKeyValueChangeNewKey] integerValue]); |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 172 | [historyChangedLabel setHidden:NO]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 173 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 174 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 175 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 176 | #pragma mark - Startup API |
| 177 | |
| 178 | // MIT license by Brian Dunagan |
| 179 | - (BOOL)isLaunchAtStartup { |
| 180 | // See if the app is currently in LoginItems. |
| 181 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 182 | // Store away that boolean. |
| 183 | BOOL isInList = itemRef != nil; |
| 184 | // Release the reference if it exists. |
| 185 | if (itemRef != nil) CFRelease(itemRef); |
| 186 | |
| 187 | return isInList; |
| 188 | } |
| 189 | |
| 190 | - (IBAction)toggleLaunchAtStartup:(id)sender { |
| 191 | // Toggle the state. |
| 192 | BOOL shouldBeToggled = ![self isLaunchAtStartup]; |
| 193 | // Get the LoginItems list. |
| 194 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 195 | if (loginItemsRef == nil) return; |
| 196 | if (shouldBeToggled) { |
| 197 | // Add the app to the LoginItems list. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 198 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 199 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 200 | if (itemRef) CFRelease(itemRef); |
| 201 | } |
| 202 | else { |
| 203 | // Remove the app from the LoginItems list. |
| 204 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 205 | LSSharedFileListItemRemove(loginItemsRef,itemRef); |
| 206 | if (itemRef != nil) CFRelease(itemRef); |
| 207 | } |
| 208 | CFRelease(loginItemsRef); |
| 209 | } |
| 210 | |
| 211 | - (LSSharedFileListItemRef)itemRefInLoginItems { |
| 212 | LSSharedFileListItemRef itemRef = nil; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 213 | CFURLRef itemUrl = nil; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 214 | |
| 215 | // Get the app's URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 216 | auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 217 | // Get the LoginItems list. |
| 218 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 219 | if (loginItemsRef == nil) return nil; |
| 220 | // Iterate over the LoginItems. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 221 | NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 222 | for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { |
| 223 | // Get the current LoginItem and resolve its URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 224 | LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; |
| 225 | if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 226 | // Compare the URLs for the current LoginItem and the app. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 227 | if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 228 | // Save the LoginItem reference. |
| 229 | itemRef = currentItemRef; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | // Retain the LoginItem reference. |
| 234 | if (itemRef != nil) CFRetain(itemRef); |
| 235 | // Release the LoginItems lists. |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 236 | CFRelease(loginItemsRef); |
| 237 | |
| 238 | return itemRef; |
| 239 | } |
| 240 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 241 | #pragma mark - Profile Photo edition |
| 242 | |
| 243 | - (IBAction) editPhoto:(id)sender { |
| 244 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
| 245 | [pictureTaker beginPictureTakerSheetForWindow:self.view.window |
| 246 | withDelegate:self |
| 247 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 248 | contextInfo:nil]; |
| 249 | } |
| 250 | |
| 251 | - (void) pictureTakerDidEnd:(IKPictureTaker *) picker |
| 252 | returnCode:(NSInteger) code |
| 253 | contextInfo:(void*) contextInfo |
| 254 | { |
| 255 | if (auto outputImage = [picker outputImage]) { |
| 256 | [photoView setImage:outputImage]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 257 | [addProfilePhotoImage setHidden:YES]; |
| 258 | [photoView setBordered:NO]; |
| 259 | if (auto pro = ProfileModel::instance().selectedProfile()) { |
| 260 | QPixmap p; |
| 261 | auto smallImg = [NSImage imageResize:outputImage newSize:{100,100}]; |
| 262 | if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) { |
| 263 | pro->person()->setPhoto(QVariant(p)); |
| 264 | } |
| 265 | pro->save(); |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 266 | } |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 267 | } else if (!photoView.image){ |
| 268 | [addProfilePhotoImage setHidden:NO]; |
| 269 | [photoView setBordered:YES]; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | #pragma mark - NSTextFieldDelegate methods |
| 274 | |
| 275 | -(void)controlTextDidChange:(NSNotification *)notif |
| 276 | { |
| 277 | if (auto pro = ProfileModel::instance().selectedProfile()) { |
| 278 | pro->person()->setFormattedName(profileNameField.stringValue.UTF8String); |
| 279 | pro->save(); |
| 280 | } |
| 281 | } |
| 282 | |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 283 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 284 | |
| 285 | - (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url { |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 286 | return YES; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 289 | @end |