blob: a712e88bfe10a9ea2ca2c02ef275cdb3fddb1ad1 [file] [log] [blame]
Alexandre Lision45f1f542016-08-25 15:16:17 -04001/*
2 * Copyright (C) 2016 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
20#import "AccDevicesVC.h"
21
22//Qt
23#import <qitemselectionmodel.h>
24
25//LRC
26#import <accountmodel.h>
27#import <ringdevicemodel.h>
28#import <account.h>
29
30#import "QNSTreeController.h"
31#import "ExportPasswordWC.h"
32
33@interface AccDevicesVC () <ExportPasswordDelegate>
34
35@property QNSTreeController* devicesTreeController;
36@property ExportPasswordWC* passwordWC;
37
38@property (unsafe_unretained) IBOutlet NSOutlineView* deviceDetailsView;
39
40@end
41
42@implementation AccDevicesVC
43
44@synthesize passwordWC;
45
46NSInteger const TAG_NAME = 100;
47NSInteger const TAG_DEVICE_IDS = 200;
48
49- (void)awakeFromNib
50{
51 NSLog(@"INIT Devices VC");
52
53 QObject::connect(AccountModel::instance().selectionModel(),
54 &QItemSelectionModel::currentChanged,
55 [=](const QModelIndex &current, const QModelIndex &previous) {
56 if(!current.isValid())
57 return;
58 [self loadAccount];
59 });
60}
61
62
63- (void)loadAccount
64{
65 auto account = AccountModel::instance().selectedAccount();
66 self.devicesTreeController = [[QNSTreeController alloc] initWithQModel:(QAbstractItemModel*)account->ringDeviceModel()];
67 [self.devicesTreeController setAvoidsEmptySelection:NO];
68 [self.devicesTreeController setChildrenKeyPath:@"children"];
69
70 [self.deviceDetailsView bind:@"content" toObject:self.devicesTreeController withKeyPath:@"arrangedObjects" options:nil];
71 [self.deviceDetailsView bind:@"sortDescriptors" toObject:self.devicesTreeController withKeyPath:@"sortDescriptors" options:nil];
72 [self.deviceDetailsView bind:@"selectionIndexPaths" toObject:self.devicesTreeController withKeyPath:@"selectionIndexPaths" options:nil];
73}
74
75- (IBAction)startExportOnRing:(id)sender
76{
77 NSButton* btbAdd = (NSButton *) sender;
78
79 self.account = AccountModel::instance().selectedAccount();
80 [self showPasswordPrompt];
81}
82#pragma mark - Export methods
83
84- (void)showPasswordPrompt
85{
86 auto account = AccountModel::instance().selectedAccount();
87 passwordWC = [[ExportPasswordWC alloc] initWithDelegate:self actionCode:1];
88 [passwordWC setAccount: account];
89#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
90 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
91#else
92 [NSApp beginSheet: passwordWC.window
93 modalForWindow: self.view.window
94 modalDelegate: self
95 didEndSelector: nil
96 contextInfo: nil];
97#endif
98}
99
100
101#pragma mark - NSOutlineViewDelegate methods
102
103- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
104{
105 return YES;
106}
107
108- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
109{
110 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
111}
112
113- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
114{
115 NSTableView* result = [outlineView makeViewWithIdentifier:@"DeviceView" owner:self];
116
117 QModelIndex qIdx = [self.devicesTreeController toQIdx:((NSTreeNode*)item)];
118 if(!qIdx.isValid())
119 return result;
120
121 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
122 NSTextField* deviceIDLabel = [result viewWithTag:TAG_DEVICE_IDS];
123
124 auto account = AccountModel::instance().selectedAccount();
125
126 NSString* string = account->ringDeviceModel()->data(qIdx,Qt::DisplayRole).toString().toNSString();
127
128 [nameLabel setStringValue:account->alias().toNSString()];
129 [deviceIDLabel setStringValue:string];
130
131 return result;
132}
133
134@end