Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Fixed potential crash with frmContactEditorKey in Edit mode.
[xestiaab/.git] / source / contacteditor / frmContactEditorKey.cpp
1 // frmContactEditorKey.cpp - frmContactEditorKey form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book 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
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmContactEditorKey.h"
20 #include <wx/tokenzr.h>
21 #include "../common/textprocessing.h"
22 #include "../common/base64.h"
24 frmContactEditorKey::frmContactEditorKey( wxWindow* parent )
25 :
26 frmContactEditorKeyADT( parent )
27 {
28         EditorMode = FALSE;
29         sliPriority->Disable();
30         
31         // Setup the control boxes and hide tabs unless they are
32         // needed.
33         
34         tabURL->Hide();
35         tabKey->Hide();
36         
37         cmbKeyMainType->Append(_("URL"));
38         cmbKeyMainType->Append(_("Key"));
39         
40         cmbType->Append("");
41         cmbType->Append(_("Home"));
42         cmbType->Append(_("Work"));
43         
44         cmbURL->Append(_("PGP Key"));
45         
46         cmbKeyType->Append(_("PGP Key"));
47         
48         cmbType->SetSelection(0);
50 }
52 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
53 {
54         if (chkUsePref->IsChecked()){
55                 sliPriority->Enable();
56         } else {
57                 sliPriority->Disable();
58         }
59 }
61 void frmContactEditorKey::SetEditorMode(bool EditMode)
62 {
63         // Set if the editor is adding or editing an address.
64         // FALSE = Add
65         // TRUE = Edit
66         
67         if (EditMode == FALSE){
68         
69                 EditorMode = FALSE;
70                 btnAction->SetLabel(_("Add"));
71                 this->SetTitle(_("Add Key"));
72         
73         } else if (EditMode == TRUE){
74         
75                 EditorMode = TRUE;
76                 btnAction->SetLabel(_("Modify"));
77                 this->SetTitle(_("Modify Key"));
78                 
79                 std::map<int,bool>::iterator booliter;
80                 std::map<int,int>::iterator intiter;
81                 std::map<int,wxString>::iterator striter;               
82                 wxString strValue;
83                 bool boolKeyData = FALSE;       
84                 
85                 // Get the key data type.
86                 
87                 booliter = KeyListKeyTypePtr->find(KeyListIndex);
88                 
89                 if (booliter->first == KeyListIndex){
90                 
91                         boolKeyData = booliter->second;
92                 
93                 }
94                 
95                 // Get the data depending on key data type.             
96                 
97                 if (boolKeyData == FALSE){
98                 
99                         // Setup the URL.
100                 
101                         cmbKeyMainType->SetSelection(0);
102                         
103                         striter = KeyListPtr->find(KeyListIndex);
104                         
105                         if (striter->first == KeyListIndex){
106                         
107                                 strValue = striter->second;
108                         
109                         }
110                         
111                         txtAddress->ChangeValue(strValue);
112                         strValue.Clear();
113                         
114                         striter = KeyListDataTypePtr->find(KeyListIndex);
115                         
116                         if (striter->first == KeyListIndex &&
117                                 striter != KeyListDataTypePtr->end()){
118                         
119                                 strValue = striter->second;
120                         
121                         }                       
122                         
123                         cmbURL->SetValue(strValue);
124                 
125                 } else if (boolKeyData == TRUE){
126                 
127                         // Setup the key.
128                 
129                         cmbKeyMainType->SetSelection(1);
130                         std::string base64dec;
131                         
132                         // Decode the base64 data.
133                         
134                         striter = KeyListPtr->find(KeyListIndex);
135                         
136                         if (striter->first == KeyListIndex){
137                         
138                                 strValue = striter->second;
139                         
140                         }
141                         
142                         std::string base64enc = std::string(strValue.mb_str());
143                         base64dec = base64_decode(base64enc);
144                         
145                         strValue.Clear();
146                         wxString strValue(base64dec.c_str(), wxConvUTF8);
147                         
148                         txtKeyData->SetValue(strValue);         
149                         
150                         // Get the key data type.
151                         
152                         strValue.Clear();
153                         
154                         striter = KeyListDataTypePtr->find(KeyListIndex);
155                         
156                         if (striter->first == KeyListIndex &&
157                                 striter != KeyListDataTypePtr->end()){
158                         
159                                 strValue = striter->second;
160                         
161                         }                       
162                         
163                         cmbKeyType->SetValue(strValue);                         
164                 
165                 }
166                 
167                 ProcessKeyType();
168                 
169                 // Get the type.
170                 
171                 striter = KeyListTypePtr->find(KeyListIndex);   
172                 
173                 if (striter->first == KeyListIndex &&
174                         striter != KeyListTypePtr->end()){
175                 
176                         strValue = striter->second;
177                 
178                 }               
179                 
180                 if (strValue == wxT("home")){
181                 
182                         cmbType->SetSelection(0);
183                 
184                 } else if (strValue == wxT("work")){
185                 
186                         cmbType->SetSelection(1);       
187                 
188                 } else {
189                 
190                         //cmbType->SetSelection(-1);
191                 
192                 }
193                 
194                 // Get the key priority.
195                 
196                 intiter = KeyListPrefPtr->find(KeyListIndex);
197                 
198                 if (intiter->first == KeyListIndex && intiter->second > 0 &&
199                         intiter != KeyListPrefPtr->end()){
200                 
201                         sliPriority->SetValue(intiter->second);
202                         sliPriority->Enable();
203                         chkUsePref->SetValue(TRUE);
204                 
205                 }
206                 
207         }
208                 
211 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
213         long ListCtrlIndex;
214         int intKeyType = 0;
215         
216         intKeyType = cmbKeyMainType->GetCurrentSelection();
217         
218         if (intKeyType == -1){
219         
220                 // No option selected so do nothing.
221         
222                 return;
223         
224         }
225         
226         if (EditorMode == FALSE){
228                 wxString strValue;
229                 wxListItem coldata;             
230                 
231                 coldata.SetId(KeyListIndex);
232                 coldata.SetData(KeyListIndex);
233                 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);            
234                 
235                 // Get Key data.
236                 
237                 if (intKeyType == 0){
238                 
239                         // URL is text.
240                                         
241                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
243                         coldata.SetText(txtAddress->GetValue());
244                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
245                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
246                 
247                 } else if (intKeyType == 1){
248                 
249                         // Encode the data in the text to base64.
250                         
251                         wxString wxSb64orig = txtKeyData->GetValue();
252                         std::string base64new = std::string(wxSb64orig.mb_str());
253                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
254                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
255                         
256                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
257                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
258                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
259                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
260                 
261                 }
262                 
263                 // Get Key type.
264                 
265                 strValue.Clear();
266                 strValue = cmbType->GetString(cmbType->GetSelection());
268                 if (strValue == _("Home")) {
269                 
270                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
271                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
272                 
273                 } else if (strValue == _("Work")) {
274                 
275                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
276                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);                    
277                 
278                 } else {
279                 
280                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
281                 
282                 }
283                 
284                 // Get Key priority.
285                 
286                 if (chkUsePref->IsChecked()){
287                 
288                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
289                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));                        
290                 
291                 } else {
292                 
293                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
294                 
295                 }
296                 
297                 if (chkUsePref->IsChecked()){
298                 
299                 }
300                 
301                 this->Close();
302                 
303         } else if (EditorMode == TRUE){
305                 long longSelected = -1; 
306                 wxString strValue;              
307                 
308                 longSelected = KeyListCtrlPtr->GetNextItem(longSelected, 
309                         wxLIST_NEXT_ALL,
310                         wxLIST_STATE_SELECTED);
311                         
312                 if (longSelected == -1){
313                         return;
314                 }
315                 
316                 // Update Key data.             
317                 
318                 if (intKeyType == 0){
319                 
320                         // URL is text.
321                                         
322                         KeyListPtr->erase(KeyListIndex);
323                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
325                         KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
326                         KeyListKeyTypePtr->erase(KeyListIndex);                 
327                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
328                 
329                 } else if (intKeyType == 1){
330                 
331                         // Encode the data in the text to base64.
332                         
333                         wxString wxSb64orig = txtKeyData->GetValue();
334                         std::string base64new = std::string(wxSb64orig.mb_str());
335                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
336                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
337                         
338                         KeyListPtr->erase(KeyListIndex);                        
339                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
340                         
341                         KeyListDataTypePtr->erase(KeyListIndex);
342                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
343                         
344                         KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
345                         
346                         KeyListKeyTypePtr->erase(KeyListIndex);
347                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
348                 
349                 }
350                 
351                 // Update Key type.
352                 
353                 strValue.Clear();
354                 strValue = cmbType->GetString(cmbType->GetSelection());
356                 KeyListTypePtr->erase(KeyListIndex);
358                 if (strValue == _("Home")) {
359                 
360                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
361                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
362                 
363                 } else if (strValue == _("Work")) {
364                 
365                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
366                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);                     
367                 
368                 } else {
369                 
370                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
371                 
372                 }
373                 
374                 // Update the key priority.
375                 
376                 KeyListPrefPtr->erase(KeyListIndex);            
377                 
378                 if (chkUsePref->IsChecked()){
379                 
380                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
381                         KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
382                 
383                 } else {
384                 
385                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
386                 
387                 }
388                 
389                 this->Close();  
390         
391         }
394 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
397         int intKeyType = 0;
398         
399         intKeyType = cmbKeyMainType->GetCurrentSelection();
400         
401         if (intKeyType == 0){
402         
403                 tabURL->Show();
404                 tabKey->Hide();
405         
406         } else if (intKeyType == 1){
407         
408                 tabURL->Hide();
409                 tabKey->Show();
410         
411         }
415 void frmContactEditorKey::ProcessKeyType()
418         int intKeyType = 0;
419         
420         intKeyType = cmbKeyMainType->GetCurrentSelection();
421         
422         if (intKeyType == 0){
423         
424                 tabURL->Show();
425                 tabKey->Hide();
426         
427         } else if (intKeyType == 1){
428         
429                 tabURL->Hide();
430                 tabKey->Show();
431         
432         }
436 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
439         wxString strValue;
441         strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
442         
443         if (strValue == _("PGP Key")){
444         
445                 cmbURL->SetValue(wxT("application/pgp-keys"));
446         
447         }
451 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
454         wxString strValue;
456         strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
457         
458         if (strValue == _("PGP Key")){
459         
460                 cmbKeyType->SetValue(wxT("application/pgp-keys"));
461         
462         }
466 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
468         this->Close();
471 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
472         std::map<int, wxString> *KeyListAltID,
473         std::map<int, wxString> *KeyListPID,
474         std::map<int, bool> *KeyListKeyType,    
475         std::map<int, wxString> *KeyListDataType,       
476         std::map<int, wxString> *KeyListType,
477         std::map<int, wxString> *KeyListTokens,
478         std::map<int, int> *KeyListPref,
479         wxListCtrl *KeyListCtrl,
480         int KeyIndex )
483         KeyListPtr = KeyList;
484         KeyListAltIDPtr = KeyListAltID;
485         KeyListPIDPtr = KeyListPID;
486         KeyListKeyTypePtr = KeyListKeyType;     
487         KeyListDataTypePtr = KeyListDataType;
488         KeyListTypePtr = KeyListType;
489         KeyListTokensPtr = KeyListTokens;
490         KeyListPrefPtr = KeyListPref;
491         KeyListCtrlPtr = KeyListCtrl;
492         KeyListIndex = KeyIndex;
493         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy