Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Initial import of code already done for Xestia Address Book
[xestiaab/.git] / source / contacteditor / frmContactEditorKey.cpp
1 #include "frmContactEditorKey.h"
2 #include <wx/tokenzr.h>
3 #include "../common/textprocessing.h"
4 #include "../common/base64.h"
6 frmContactEditorKey::frmContactEditorKey( wxWindow* parent )
7 :
8 frmContactEditorKeyADT( parent )
9 {
10         EditorMode = FALSE;
11         sliPriority->Disable();
12         
13         // Setup the control boxes and hide tabs unless they are
14         // needed.
15         
16         tabURL->Hide();
17         tabKey->Hide();
18         
19         cmbKeyMainType->Append(_("URL"));
20         cmbKeyMainType->Append(_("Key"));
21         
22         cmbType->Append(_("Home"));
23         cmbType->Append(_("Work"));
24         
25         cmbURL->Append(_("PGP Key"));
26         
27         cmbKeyType->Append(_("PGP Key"));
29 }
31 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
32 {
33         if (chkUsePref->IsChecked()){
34                 sliPriority->Enable();
35         } else {
36                 sliPriority->Disable();
37         }
38 }
40 void frmContactEditorKey::SetEditorMode(bool EditMode)
41 {
42         // Set if the editor is adding or editing an address.
43         // FALSE = Add
44         // TRUE = Edit
45         
46         if (EditMode == FALSE){
47         
48                 EditorMode = FALSE;
49                 btnAction->SetLabel(_("Add"));
50                 this->SetTitle(_("Add Key"));
51         
52         } else if (EditMode == TRUE){
53         
54                 EditorMode = TRUE;
55                 btnAction->SetLabel(_("Modify"));
56                 this->SetTitle(_("Modify Key"));
57                 
58                 std::map<int,bool>::iterator booliter;
59                 std::map<int,int>::iterator intiter;
60                 std::map<int,wxString>::iterator striter;               
61                 wxString strValue;
62                 bool boolKeyData = FALSE;       
63                 
64                 // Get the key data type.
65                 
66                 booliter = KeyListKeyTypePtr->find(KeyListIndex);
67                 
68                 if (booliter->first == KeyListIndex){
69                 
70                         boolKeyData = booliter->second;
71                 
72                 }
73                 
74                 // Get the data depending on key data type.             
75                 
76                 if (boolKeyData == FALSE){
77                 
78                         // Setup the URL.
79                 
80                         cmbKeyMainType->SetSelection(0);
81                         
82                         striter = KeyListPtr->find(KeyListIndex);
83                         
84                         if (striter->first == KeyListIndex){
85                         
86                                 strValue = striter->second;
87                         
88                         }
89                         
90                         txtAddress->ChangeValue(strValue);
91                         strValue.Clear();
92                         
93                         striter = KeyListDataTypePtr->find(KeyListIndex);
94                         
95                         if (striter->first == KeyListIndex){
96                         
97                                 strValue = striter->second;
98                         
99                         }                       
100                         
101                         cmbURL->SetValue(strValue);
102                 
103                 } else if (boolKeyData == TRUE){
104                 
105                         // Setup the key.
106                 
107                         cmbKeyMainType->SetSelection(1);
108                         std::string base64dec;
109                         
110                         // Decode the base64 data.
111                         
112                         striter = KeyListPtr->find(KeyListIndex);
113                         
114                         if (striter->first == KeyListIndex){
115                         
116                                 strValue = striter->second;
117                         
118                         }
119                         
120                         std::string base64enc = std::string(strValue.mb_str());
121                         base64dec = base64_decode(base64enc);
122                         
123                         strValue.Clear();
124                         wxString strValue(base64dec.c_str(), wxConvUTF8);
125                         
126                         txtKeyData->SetValue(strValue);         
127                         
128                         // Get the key data type.
129                         
130                         strValue.Clear();
131                         
132                         striter = KeyListDataTypePtr->find(KeyListIndex);
133                         
134                         if (striter->first == KeyListIndex){
135                         
136                                 strValue = striter->second;
137                         
138                         }                       
139                         
140                         cmbKeyType->SetValue(strValue);                         
141                 
142                 }
143                 
144                 ProcessKeyType();
145                 
146                 // Get the type.
147                 
148                 striter = KeyListTypePtr->find(KeyListIndex);   
149                 
150                 if (striter->first == KeyListIndex){
151                 
152                         strValue = striter->second;
153                 
154                 }               
155                 
156                 if (strValue == wxT("home")){
157                 
158                         cmbType->SetSelection(0);
159                 
160                 } else if (strValue == wxT("work")){
161                 
162                         cmbType->SetSelection(1);       
163                 
164                 } else {
165                 
166                         //cmbType->SetSelection(-1);
167                 
168                 }
169                 
170                 // Get the key priority.
171                 
172                 intiter = KeyListPrefPtr->find(KeyListIndex);
173                 
174                 if (intiter->first == KeyListIndex && intiter->second > 0){
175                 
176                         sliPriority->SetValue(intiter->second);
177                         sliPriority->Enable();
178                         chkUsePref->SetValue(TRUE);
179                 
180                 }
181                 
182         }
183                 
186 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
188         long ListCtrlIndex;
189         int intKeyType = 0;
190         
191         intKeyType = cmbKeyMainType->GetCurrentSelection();
192         
193         if (intKeyType == -1){
194         
195                 // No option selected so do nothing.
196         
197                 return;
198         
199         }
200         
201         if (EditorMode == FALSE){
203                 wxString strValue;
204                 wxListItem coldata;             
205                 
206                 coldata.SetId(KeyListIndex);
207                 coldata.SetData(KeyListIndex);
208                 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);            
209                 
210                 // Get Key data.
211                 
212                 if (intKeyType == 0){
213                 
214                         // URL is text.
215                                         
216                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
218                         coldata.SetText(txtAddress->GetValue());
219                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
220                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
221                 
222                 } else if (intKeyType == 1){
223                 
224                         // Encode the data in the text to base64.
225                         
226                         wxString wxSb64orig = txtKeyData->GetValue();
227                         std::string base64new = std::string(wxSb64orig.mb_str());
228                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), base64new.length());
229                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
230                         
231                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
232                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
233                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
234                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
235                 
236                 }
237                 
238                 // Get Key type.
239                 
240                 strValue.Clear();
241                 strValue = cmbType->GetString(cmbType->GetSelection());
243                 if (strValue == _("Home")) {
244                 
245                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
246                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
247                 
248                 } else if (strValue == _("Work")) {
249                 
250                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
251                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);                    
252                 
253                 } else {
254                 
255                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
256                 
257                 }
258                 
259                 // Get Key priority.
260                 
261                 if (chkUsePref->IsChecked()){
262                 
263                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
264                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));                        
265                 
266                 } else {
267                 
268                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
269                 
270                 }
271                 
272                 if (chkUsePref->IsChecked()){
273                 
274                 }
275                 
276                 this->Close();
277                 
278         } else if (EditorMode == TRUE){
280                 long longSelected = -1; 
281                 wxString strValue;              
282                 
283                 longSelected = KeyListCtrlPtr->GetNextItem(longSelected, 
284                         wxLIST_NEXT_ALL,
285                         wxLIST_STATE_SELECTED);
286                         
287                 if (longSelected == -1){
288                         return;
289                 }
290                 
291                 // Update Key data.             
292                 
293                 if (intKeyType == 0){
294                 
295                         // URL is text.
296                                         
297                         KeyListPtr->erase(KeyListIndex);
298                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
300                         KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
301                         KeyListKeyTypePtr->erase(KeyListIndex);                 
302                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
303                 
304                 } else if (intKeyType == 1){
305                 
306                         // Encode the data in the text to base64.
307                         
308                         wxString wxSb64orig = txtKeyData->GetValue();
309                         std::string base64new = std::string(wxSb64orig.mb_str());
310                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), base64new.length());
311                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
312                         
313                         KeyListPtr->erase(KeyListIndex);                        
314                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
315                         
316                         KeyListDataTypePtr->erase(KeyListIndex);
317                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
318                         
319                         KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
320                         
321                         KeyListKeyTypePtr->erase(KeyListIndex);
322                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
323                 
324                 }
325                 
326                 // Update Key type.
327                 
328                 strValue.Clear();
329                 strValue = cmbType->GetString(cmbType->GetSelection());
331                 KeyListTypePtr->erase(KeyListIndex);
333                 if (strValue == _("Home")) {
334                 
335                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
336                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
337                 
338                 } else if (strValue == _("Work")) {
339                 
340                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
341                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);                     
342                 
343                 } else {
344                 
345                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
346                 
347                 }
348                 
349                 // Update the key priority.
350                 
351                 KeyListPrefPtr->erase(KeyListIndex);            
352                 
353                 if (chkUsePref->IsChecked()){
354                 
355                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
356                         KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
357                 
358                 } else {
359                 
360                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
361                 
362                 }
363                 
364                 this->Close();  
365         
366         }
369 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
372         int intKeyType = 0;
373         
374         intKeyType = cmbKeyMainType->GetCurrentSelection();
375         
376         if (intKeyType == 0){
377         
378                 tabURL->Show();
379                 tabKey->Hide();
380         
381         } else if (intKeyType == 1){
382         
383                 tabURL->Hide();
384                 tabKey->Show();
385         
386         }
390 void frmContactEditorKey::ProcessKeyType()
393         int intKeyType = 0;
394         
395         intKeyType = cmbKeyMainType->GetCurrentSelection();
396         
397         if (intKeyType == 0){
398         
399                 tabURL->Show();
400                 tabKey->Hide();
401         
402         } else if (intKeyType == 1){
403         
404                 tabURL->Hide();
405                 tabKey->Show();
406         
407         }
411 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
414         wxString strValue;
416         strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
417         
418         if (strValue == _("PGP Key")){
419         
420                 cmbURL->SetValue(wxT("application/pgp-keys"));
421         
422         }
426 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
429         wxString strValue;
431         strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
432         
433         if (strValue == _("PGP Key")){
434         
435                 cmbKeyType->SetValue(wxT("application/pgp-keys"));
436         
437         }
441 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
443         this->Close();
446 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
447         std::map<int, wxString> *KeyListAltID,
448         std::map<int, wxString> *KeyListPID,
449         std::map<int, bool> *KeyListKeyType,    
450         std::map<int, wxString> *KeyListDataType,       
451         std::map<int, wxString> *KeyListType,
452         std::map<int, wxString> *KeyListTokens,
453         std::map<int, int> *KeyListPref,
454         wxListCtrl *KeyListCtrl,
455         int KeyIndex )
458         KeyListPtr = KeyList;
459         KeyListAltIDPtr = KeyListAltID;
460         KeyListPIDPtr = KeyListPID;
461         KeyListKeyTypePtr = KeyListKeyType;     
462         KeyListDataTypePtr = KeyListDataType;
463         KeyListTypePtr = KeyListType;
464         KeyListTokensPtr = KeyListTokens;
465         KeyListPrefPtr = KeyListPref;
466         KeyListCtrlPtr = KeyListCtrl;
467         KeyListIndex = KeyIndex;
468         
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