blob: 7435098d2d12cd9da1a4f4def67a6c8dfb05a7c6 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * 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 Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050019#import "GeneralPrefsVC.h"
20
Alexandre Lision261f1b92016-04-04 12:35:34 -040021#import <Quartz/Quartz.h>
22
23//Qt
24#import <QSize>
25#import <QtMacExtras/qmacfunctions.h>
26#import <QPixmap>
27
28//LRC
Alexandre Lisione4041492015-03-20 18:20:43 -040029#import <categorizedhistorymodel.h>
Alexandre Lision261f1b92016-04-04 12:35:34 -040030#import <profilemodel.h>
31#import <profile.h>
32#import <person.h>
33#import <globalinstances.h>
Kateryna Kostiuk9aef8492017-06-22 16:58:36 -040034#import <media/recordingmodel.h>
Kateryna Kostiuk67735232018-05-10 15:05:32 -040035#import <api/datatransfermodel.h>
Alexandre Lisione4041492015-03-20 18:20:43 -040036
Alexandre Lision3d4143a2015-06-10 14:27:49 -040037#if ENABLE_SPARKLE
38#import <Sparkle/Sparkle.h>
39#endif
40
Alexandre Lisionc65310c2015-04-23 16:44:23 -040041#import "Constants.h"
Alexandre Lision261f1b92016-04-04 12:35:34 -040042#import "views/NSImage+Extensions.h"
43#import "delegates/ImageManipulationDelegate.h"
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040044#import "utils.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040045
Alexandre Lision261f1b92016-04-04 12:35:34 -040046@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 Kostiuk87ae2bf2018-05-04 13:46:17 -040057 __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040058 __unsafe_unretained IBOutlet NSButton *downloadFolder;
Alexandre Lision261f1b92016-04-04 12:35:34 -040059}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050060@end
61
Alexandre Lision4de68ce2015-04-24 18:22:49 -040062@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040063
Kateryna Kostiuk67735232018-05-10 15:05:32 -040064@synthesize dataTransferModel;
65
66
67-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::DataTransferModel*) dataTransferModel
68{
69 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
70 {
71 self.dataTransferModel = dataTransferModel;
72 }
73 return self;
74}
75
Alexandre Lisione4041492015-03-20 18:20:43 -040076- (void)loadView
77{
78 [super loadView];
Alexandre Lisionc65310c2015-04-23 16:44:23 -040079 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040080
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020081 [startUpButton setState:[self isLaunchAtStartup]];
82
Alexandre Lisionaa03df42016-01-26 09:39:25 -050083 int historyLimit = CategorizedHistoryModel::instance().historyLimit();
84 [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
85 [historyStepper setIntValue:historyLimit];
86
87 BOOL limited = CategorizedHistoryModel::instance().isHistoryLimited();
88 [historySwitch setState:limited];
89 [historyStepper setEnabled:limited];
90 [historyTextField setEnabled:limited];
Alexandre Lision3d4143a2015-06-10 14:27:49 -040091#if ENABLE_SPARKLE
92 [sparkleContainer setHidden:NO];
93 SUUpdater *updater = [SUUpdater sharedUpdater];
94 [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
95
96 [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
97 [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil];
98#else
99 [sparkleContainer setHidden:YES];
100#endif
101
Alexandre Lision261f1b92016-04-04 12:35:34 -0400102 [photoView setWantsLayer: YES];
103 photoView.layer.cornerRadius = photoView.frame.size.width / 2;
104 photoView.layer.masksToBounds = YES;
Kateryna Kostiuk87ae2bf2018-05-04 13:46:17 -0400105 [addProfilePhotoImage setWantsLayer: YES];
106 [addProfilePhotoImage setHidden:NO];
107 [photoView setBordered:YES];
Alexandre Lision261f1b92016-04-04 12:35:34 -0400108
109 if (auto pro = ProfileModel::instance().selectedProfile()) {
Alexandre Lision261f1b92016-04-04 12:35:34 -0400110 [profileNameField setStringValue:pro->person()->formattedName().toNSString()];
Kateryna Kostiuk87ae2bf2018-05-04 13:46:17 -0400111 if (pro->person() && pro->person()->photo().isValid()) {
112 auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140});
113 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
114 [addProfilePhotoImage setHidden:YES];
115 [photoView setBordered:NO];
116 }
Alexandre Lision261f1b92016-04-04 12:35:34 -0400117 }
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400118 if (dataTransferModel) {
119 downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
120 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400121}
122
Alexandre Lision81c97212015-06-17 15:51:53 -0400123- (void) dealloc
124{
125 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
Alexandre Lision81c97212015-06-17 15:51:53 -0400126}
127
Alexandre Lisione4041492015-03-20 18:20:43 -0400128- (IBAction)clearHistory:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400129 CategorizedHistoryModel::instance().clearAllCollections();
Kateryna Kostiuk9aef8492017-06-22 16:58:36 -0400130 Media::RecordingModel::instance().clearAllCollections();
Alexandre Lisione4041492015-03-20 18:20:43 -0400131 [historyChangedLabel setHidden:NO];
132}
133
Alexandre Lision261f1b92016-04-04 12:35:34 -0400134- (IBAction)toggleHistory:(NSButton*)sender {
Alexandre Lisionaa03df42016-01-26 09:39:25 -0500135 CategorizedHistoryModel::instance().setHistoryLimited([sender state]);
136 int historyLimit = CategorizedHistoryModel::instance().historyLimit();
137 [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
138 [historyStepper setIntValue:historyLimit];
139 [historyChangedLabel setHidden:NO];
140 [historyStepper setEnabled:[sender state]];
141 [historyTextField setEnabled:[sender state]];
142}
143
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400144- (IBAction)changeDownloadFolder:(id)sender {
145
146 NSOpenPanel *panel = [NSOpenPanel openPanel];
147 [panel setAllowsMultipleSelection:NO];
148 [panel setCanChooseDirectories:YES];
149 [panel setCanChooseFiles:NO];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400150 panel.delegate = self;
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400151 if ([panel runModal] != NSFileHandlingPanelOKButton) return;
152 if ([[panel URLs] lastObject] == nil) return;
153 NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"];
154 dataTransferModel->downloadDirectory = std::string([path UTF8String]);
155 downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
156 [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder];
157}
158
Alexandre Lisione4041492015-03-20 18:20:43 -0400159// KVO handler
160-(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
161 change:(NSDictionary *)aChange context:(void *)aContext
162{
Alexandre Lisionaa03df42016-01-26 09:39:25 -0500163 if ([aKeyPath isEqualToString:Preferences::HistoryLimit]) {
164 CategorizedHistoryModel::instance().setHistoryLimit([[aChange objectForKey: NSKeyValueChangeNewKey] integerValue]);
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400165 [historyChangedLabel setHidden:NO];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400166 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400167}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500168
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200169#pragma mark - Startup API
170
171// MIT license by Brian Dunagan
172- (BOOL)isLaunchAtStartup {
173 // See if the app is currently in LoginItems.
174 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
175 // Store away that boolean.
176 BOOL isInList = itemRef != nil;
177 // Release the reference if it exists.
178 if (itemRef != nil) CFRelease(itemRef);
179
180 return isInList;
181}
182
183- (IBAction)toggleLaunchAtStartup:(id)sender {
184 // Toggle the state.
185 BOOL shouldBeToggled = ![self isLaunchAtStartup];
186 // Get the LoginItems list.
187 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
188 if (loginItemsRef == nil) return;
189 if (shouldBeToggled) {
190 // Add the app to the LoginItems list.
Alexandre Lision81c97212015-06-17 15:51:53 -0400191 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200192 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
193 if (itemRef) CFRelease(itemRef);
194 }
195 else {
196 // Remove the app from the LoginItems list.
197 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
198 LSSharedFileListItemRemove(loginItemsRef,itemRef);
199 if (itemRef != nil) CFRelease(itemRef);
200 }
201 CFRelease(loginItemsRef);
202}
203
204- (LSSharedFileListItemRef)itemRefInLoginItems {
205 LSSharedFileListItemRef itemRef = nil;
Alexandre Lision81c97212015-06-17 15:51:53 -0400206 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200207
208 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400209 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200210 // Get the LoginItems list.
211 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
212 if (loginItemsRef == nil) return nil;
213 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400214 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200215 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
216 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400217 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
218 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200219 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400220 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200221 // Save the LoginItem reference.
222 itemRef = currentItemRef;
223 }
224 }
225 }
226 // Retain the LoginItem reference.
227 if (itemRef != nil) CFRetain(itemRef);
228 // Release the LoginItems lists.
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200229 CFRelease(loginItemsRef);
230
231 return itemRef;
232}
233
Alexandre Lision261f1b92016-04-04 12:35:34 -0400234#pragma mark - Profile Photo edition
235
236- (IBAction) editPhoto:(id)sender {
237 auto pictureTaker = [IKPictureTaker pictureTaker];
238 [pictureTaker beginPictureTakerSheetForWindow:self.view.window
239 withDelegate:self
240 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
241 contextInfo:nil];
242}
243
244- (void) pictureTakerDidEnd:(IKPictureTaker *) picker
245 returnCode:(NSInteger) code
246 contextInfo:(void*) contextInfo
247{
248 if (auto outputImage = [picker outputImage]) {
249 [photoView setImage:outputImage];
Kateryna Kostiuk87ae2bf2018-05-04 13:46:17 -0400250 [addProfilePhotoImage setHidden:YES];
251 [photoView setBordered:NO];
252 if (auto pro = ProfileModel::instance().selectedProfile()) {
253 QPixmap p;
254 auto smallImg = [NSImage imageResize:outputImage newSize:{100,100}];
255 if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) {
256 pro->person()->setPhoto(QVariant(p));
257 }
258 pro->save();
Alexandre Lision261f1b92016-04-04 12:35:34 -0400259 }
Kateryna Kostiuk87ae2bf2018-05-04 13:46:17 -0400260 } else if (!photoView.image){
261 [addProfilePhotoImage setHidden:NO];
262 [photoView setBordered:YES];
Alexandre Lision261f1b92016-04-04 12:35:34 -0400263 }
264}
265
266#pragma mark - NSTextFieldDelegate methods
267
268-(void)controlTextDidChange:(NSNotification *)notif
269{
270 if (auto pro = ProfileModel::instance().selectedProfile()) {
271 pro->person()->setFormattedName(profileNameField.stringValue.UTF8String);
272 pro->save();
273 }
274}
275
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400276#pragma mark - NSOpenSavePanelDelegate delegate methods
277
278- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
279 if(!appSandboxed()) {
280 return YES;
281 }
282 return isUrlAccessibleFromSandbox(url);
283}
284
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500285@end