blob: b88281ac4f6fd193049000b3a88cded921647619 [file] [log] [blame]
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -04001/*
2 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@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//Qt
21#import <QSize>
22#import <QtMacExtras/qmacfunctions.h>
23#import <QPixmap>
24
25//LRC
26#import <account.h>
27#import <person.h>
28#import <contactmethod.h>
29#import <availableAccountModel.h>
30#import <contactRequest.h>
31#import <globalinstances.h>
32#import <recentmodel.h>
33
34#import "Constants.h"
35#import "views/NSImage+Extensions.h"
36#import "delegates/ImageManipulationDelegate.h"
37#import "SendContactRequestWC.h"
38
39@interface SendContactRequestWC () {
40
41 __unsafe_unretained IBOutlet NSTextField* userName;
42 __unsafe_unretained IBOutlet NSTextField* ringID;
43 __unsafe_unretained IBOutlet NSTextField* infoLabel;
44 __unsafe_unretained IBOutlet NSImageView* photoView;
45
46}
47@end
48
49@implementation SendContactRequestWC
50
51NSString* const sendingErrorMsg = @"An error happened, contact request has not been sent";
52NSString* const findContactErrorMsg = @"Could not find contact to send request";
53
54- (void)windowDidLoad {
55 [super windowDidLoad];
56 self.hideButtons = false;
57 if(!self.contactMethod) {
58 [self findContactError];
59 return;
60 }
61 auto photo = GlobalInstances::pixmapManipulator().callPhoto(self.contactMethod, {100,100});
62 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
63 [userName setStringValue:self.contactMethod->bestName().toNSString()];
64 [ringID setStringValue:self.contactMethod->bestId().toNSString()];
65}
66
67-(IBAction) sendContactRequest:(id)sender
68{
69 if(self.contactMethod->account() == nullptr) {
70 self.contactMethod->setAccount([self chosenAccount]);
71 }
72
73 if(self.contactMethod->account() == nullptr) {
74 return;
75 }
76 if (self.contactMethod->account()->sendContactRequest(self.contactMethod)) {
77 [self close];
78 return;
79 } else {
80 [self sendRequestError];
81 }
82}
83
84- (IBAction) cancelPressed:(id)sender
85{
86 [self close];
87}
88
89-(Account* ) chosenAccount
90{
91 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
92 if(!index.isValid())
93 return nil;
94 return index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
95}
96
97-(void)sendRequestError
98{
99 self.hideButtons = true;
100 NSString* error = NSLocalizedString(sendingErrorMsg, @"Error shown to user");
101 [infoLabel setStringValue:sendingErrorMsg];
102}
103
104-(void) findContactError
105{
106 self.hideButtons = true;
107 NSString* error = NSLocalizedString(findContactErrorMsg, @"Error shown to user");
108 [infoLabel setStringValue:error];
109}
110
111@end