blob: ae715be7b62047686751f65516b0485c73879d58 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lisionbb5c2462015-07-30 12:55:37 -04002 * Copyright (C) 2015 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 Lision5855b6a2015-02-03 11:31:05 -050019#import "RingWindowController.h"
20
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040021//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050022#import <accountmodel.h>
23#import <callmodel.h>
24#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040025#import <call.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050026
Alexandre Lision745e4d62015-03-22 20:03:10 -040027#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040028#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040029#import "CurrentCallVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040030
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040031#import "PreferencesWC.h"
32#import "views/NSColor+RingTheme.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040033
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040034@implementation RingWindowController {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050035
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040036 __unsafe_unretained IBOutlet NSView *callView;
37 __unsafe_unretained IBOutlet NSTextField *ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040038
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040039 PreferencesWC *preferencesWC;
40 CurrentCallVC* currentVC;
41}
Alexandre Lision58cab672015-06-09 15:25:40 -040042
Alexandre Lisionc5148052015-03-04 15:10:35 -050043static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050044
Alexandre Lision5855b6a2015-02-03 11:31:05 -050045- (void)windowDidLoad {
46 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040047 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -040048
49 currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
50 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
51 [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
52
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040053 [callView addSubview:[currentVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040054
Alexandre Lision58cab672015-06-09 15:25:40 -040055 [currentVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040056
Alexandre Lision08abfac2015-09-22 12:20:51 -040057 // Fresh run, we need to make sure RingID appears
58 [self updateRingID];
59
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040060 // Update Ring ID label based on account model changes
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040061 QObject::connect(&AccountModel::instance(),
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040062 &AccountModel::dataChanged,
63 [=] {
64 [self updateRingID];
65 });
66}
67
68/**
69 * Implement the necessary logic to choose which Ring ID to display.
70 * This tries to choose the "best" ID to show
71 */
72- (void) updateRingID
73{
74 Account* registered = nullptr;
75 Account* enabled = nullptr;
76 Account* finalChoice = nullptr;
77
78 [ringIDLabel setStringValue:@""];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040079 auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040080 for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
81 Account* acc = ringList.value(i);
82 if (acc->isEnabled()) {
83 if(!enabled)
84 enabled = finalChoice = acc;
85 if (acc->registrationState() == Account::RegistrationState::READY) {
86 registered = enabled = finalChoice = acc;
87 }
88 } else {
89 if (!finalChoice)
90 finalChoice = acc;
91 }
92 }
93
Alexandre Lision7f8351b2015-08-20 11:43:37 -040094 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050095}
96
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050097- (IBAction)openPreferences:(id)sender
98{
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040099 preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"];
100 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500101}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500102
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500103@end