1 | /* |
---|
2 | * Estonian ID card plugin for web browsers |
---|
3 | * |
---|
4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
5 | * |
---|
6 | * This is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU Lesser General Public |
---|
8 | * License as published by the Free Software Foundation; either |
---|
9 | * version 2.1 of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This software is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with this library; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #import "CertificateSelection.h" |
---|
23 | |
---|
24 | #import "esteid_log.h" |
---|
25 | #import "esteid_map.h" |
---|
26 | #import "esteid_certinfo.h" |
---|
27 | #import "l10n.h" |
---|
28 | |
---|
29 | @implementation CertificateSelection |
---|
30 | |
---|
31 | @synthesize certificateSelectionPanel, certificateSelection, okButton, cancelButton, warningLabel; |
---|
32 | |
---|
33 | - (void)dealloc |
---|
34 | { |
---|
35 | [certificateSelection release]; |
---|
36 | [certificateSelectionPanel release]; |
---|
37 | [certificates release]; |
---|
38 | [okButton release]; |
---|
39 | [cancelButton release]; |
---|
40 | [super dealloc]; |
---|
41 | } |
---|
42 | |
---|
43 | - (void)awakeFromNib |
---|
44 | { |
---|
45 | LOG_LOCATION; |
---|
46 | [certificateSelection setTarget:self]; |
---|
47 | [certificateSelection setDoubleAction:@selector(okClicked:)]; |
---|
48 | [certificateSelection reloadData]; |
---|
49 | |
---|
50 | NSTableColumn* cn = [certificateSelection tableColumnWithIdentifier:@"CN"]; |
---|
51 | NSTableHeaderCell* header = [cn headerCell]; |
---|
52 | [header setStringValue: [NSString stringWithUTF8String: l10n("Certificate")]]; |
---|
53 | cn = [certificateSelection tableColumnWithIdentifier:@"type"]; |
---|
54 | header = [cn headerCell]; |
---|
55 | [header setStringValue: [NSString stringWithUTF8String: l10n("Type")]]; |
---|
56 | cn = [certificateSelection tableColumnWithIdentifier:@"validTo"]; |
---|
57 | header = [cn headerCell]; |
---|
58 | [header setStringValue: [NSString stringWithUTF8String: l10n("Valid to")]]; |
---|
59 | |
---|
60 | [certificateSelectionPanel setTitle:[NSString stringWithUTF8String:l10n("Select certificate")]]; |
---|
61 | |
---|
62 | [okButton setTitle:[NSString stringWithUTF8String:l10n("Select")]]; |
---|
63 | [cancelButton setTitle:[NSString stringWithUTF8String:l10n("Cancel")]]; |
---|
64 | |
---|
65 | [warningLabel setTitleWithMnemonic:[NSString stringWithUTF8String: l10n("By selecting a certificate I accept that my name and personal ID code will be sent to service provider.")]]; |
---|
66 | |
---|
67 | if ([certificateSelection numberOfRows] > 0) { |
---|
68 | EstEID_log("Selecting first row in certificates list"); |
---|
69 | [certificateSelection selectRow:0 byExtendingSelection:FALSE]; |
---|
70 | [okButton setEnabled: YES]; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | - (id)init |
---|
75 | { |
---|
76 | |
---|
77 | if ((self = [super init])) { |
---|
78 | EstEID_Certs *certs = EstEID_loadCerts(); |
---|
79 | EstEID_log("%i certificates found", certs->count); |
---|
80 | certificates = [NSMutableArray new]; |
---|
81 | |
---|
82 | for(int i = 0; i < certs->count; i++) { |
---|
83 | NSMutableDictionary* certificateData = [NSMutableDictionary new]; |
---|
84 | EstEID_Map cert = certs->certs[i]; |
---|
85 | if (!EstEID_mapGet(cert, "usageNonRepudiation")) continue; |
---|
86 | |
---|
87 | [certificateData setObject:[NSString stringWithCString:EstEID_mapGet(cert, "commonName") encoding:NSUTF8StringEncoding] forKey:@"CN"]; |
---|
88 | NSString* validTo = [NSString stringWithCString:EstEID_mapGet(cert, "validTo") encoding:NSUTF8StringEncoding]; |
---|
89 | NSArray* list = [validTo componentsSeparatedByString:@" "]; |
---|
90 | [certificateData setObject:[list objectAtIndex:0] forKey:@"validTo"]; |
---|
91 | [certificateData setObject:[NSString stringWithCString:EstEID_mapGet(cert, "certHash") encoding:NSUTF8StringEncoding] forKey:@"id"]; |
---|
92 | [certificateData setObject:[NSString stringWithCString:EstEID_mapGet(cert, "organizationName") encoding:NSUTF8StringEncoding] forKey:@"type"]; |
---|
93 | [certificates addObject:certificateData]; |
---|
94 | [certificateData release]; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | return self; |
---|
99 | } |
---|
100 | |
---|
101 | - (int)numberOfRowsInTableView:(NSTableView*)pTableView |
---|
102 | { |
---|
103 | return [certificates count]; |
---|
104 | } |
---|
105 | |
---|
106 | - (id)tableView:(NSTableView*)pTableView objectValueForTableColumn:(NSTableColumn*)pTableColumn row:(int)pRowIndex |
---|
107 | { |
---|
108 | NSString* data = [[NSString alloc] autorelease]; |
---|
109 | NSDictionary* current = [NSDictionary dictionaryWithDictionary:[certificates objectAtIndex:pRowIndex]]; |
---|
110 | if ([[pTableColumn identifier] isEqualToString:@"CN"]) { |
---|
111 | data = [current objectForKey:@"CN"]; |
---|
112 | } |
---|
113 | if ([[pTableColumn identifier] isEqualToString:@"type"]) { |
---|
114 | data = [current objectForKey:@"type"]; |
---|
115 | } |
---|
116 | if ([[pTableColumn identifier] isEqualToString:@"validTo"]) { |
---|
117 | data = [current objectForKey:@"validTo"]; |
---|
118 | } |
---|
119 | return data; |
---|
120 | } |
---|
121 | |
---|
122 | - (BOOL)tableView:(NSTableView*)pTableView shouldSelectRow:(int)row |
---|
123 | { |
---|
124 | EstEID_log("Selected row %i, enabled?: %i, button: %p", row, [okButton isEnabled], okButton); |
---|
125 | [okButton setEnabled: YES]; |
---|
126 | EstEID_log("enabled?: %i", [okButton isEnabled]); |
---|
127 | return YES; |
---|
128 | } |
---|
129 | |
---|
130 | - (IBAction)okClicked:(id)pId |
---|
131 | { |
---|
132 | cancelled = FALSE; |
---|
133 | [NSApp stopModal]; |
---|
134 | } |
---|
135 | |
---|
136 | - (IBAction)cancelClicked:(id)pId |
---|
137 | { |
---|
138 | cancelled = TRUE; |
---|
139 | [NSApp abortModal]; |
---|
140 | } |
---|
141 | |
---|
142 | - (IBAction)enableOkButton:(id)pId |
---|
143 | { |
---|
144 | EstEID_log("button: %p", okButton); |
---|
145 | [okButton setEnabled:([certificateSelection selectedRow] != -1)]; |
---|
146 | } |
---|
147 | |
---|
148 | - (NSString *)showForWindow:(NSWindow *)window |
---|
149 | { |
---|
150 | LOG_LOCATION; |
---|
151 | [NSBundle loadNibNamed: @"CertificateSelection" owner: self]; |
---|
152 | [NSApp beginSheet: certificateSelectionPanel modalForWindow: window modalDelegate: nil didEndSelector: nil contextInfo: nil]; |
---|
153 | [NSApp runModalForWindow: certificateSelectionPanel]; |
---|
154 | int selectedCertificateIndex = [certificateSelection selectedRow]; |
---|
155 | [NSApp endSheet: certificateSelectionPanel]; |
---|
156 | [certificateSelectionPanel orderOut: self]; |
---|
157 | |
---|
158 | BOOL isCancelledOrNotSelected = (cancelled || (selectedCertificateIndex == -1)); |
---|
159 | EstEID_log("Certificate selection returns with index %i, cert id is %s", |
---|
160 | selectedCertificateIndex, |
---|
161 | isCancelledOrNotSelected ? "N/A" : [[[certificates objectAtIndex:selectedCertificateIndex] objectForKey:@"id"] UTF8String]); |
---|
162 | |
---|
163 | return (isCancelledOrNotSelected) ? @"" : [[certificates objectAtIndex:selectedCertificateIndex] objectForKey:@"id"]; |
---|
164 | } |
---|
165 | |
---|
166 | @end |
---|