Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added blank option (set as default) for type in frmContactEditorKey.
[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                         
118                                 strValue = striter->second;
119                         
120                         }                       
121                         
122                         cmbURL->SetValue(strValue);
123                 
124                 } else if (boolKeyData == TRUE){
125                 
126                         // Setup the key.
127                 
128                         cmbKeyMainType->SetSelection(1);
129                         std::string base64dec;
130                         
131                         // Decode the base64 data.
132                         
133                         striter = KeyListPtr->find(KeyListIndex);
134                         
135                         if (striter->first == KeyListIndex){
136                         
137                                 strValue = striter->second;
138                         
139                         }
140                         
141                         std::string base64enc = std::string(strValue.mb_str());
142                         base64dec = base64_decode(base64enc);
143                         
144                         strValue.Clear();
145                         wxString strValue(base64dec.c_str(), wxConvUTF8);
146                         
147                         txtKeyData->SetValue(strValue);         
148                         
149                         // Get the key data type.
150                         
151                         strValue.Clear();
152                         
153                         striter = KeyListDataTypePtr->find(KeyListIndex);
154                         
155                         if (striter->first == KeyListIndex){
156                         
157                                 strValue = striter->second;
158                         
159                         }                       
160                         
161                         cmbKeyType->SetValue(strValue);                         
162                 
163                 }
164                 
165                 ProcessKeyType();
166                 
167                 // Get the type.
168                 
169                 striter = KeyListTypePtr->find(KeyListIndex);   
170                 
171                 if (striter->first == KeyListIndex){
172                 
173                         strValue = striter->second;
174                 
175                 }               
176                 
177                 if (strValue == wxT("home")){
178                 
179                         cmbType->SetSelection(0);
180                 
181                 } else if (strValue == wxT("work")){
182                 
183                         cmbType->SetSelection(1);       
184                 
185                 } else {
186                 
187                         //cmbType->SetSelection(-1);
188                 
189                 }
190                 
191                 // Get the key priority.
192                 
193                 intiter = KeyListPrefPtr->find(KeyListIndex);
194                 
195                 if (intiter->first == KeyListIndex && intiter->second > 0){
196                 
197                         sliPriority->SetValue(intiter->second);
198                         sliPriority->Enable();
199                         chkUsePref->SetValue(TRUE);
200                 
201                 }
202                 
203         }
204                 
207 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
209         long ListCtrlIndex;
210         int intKeyType = 0;
211         
212         intKeyType = cmbKeyMainType->GetCurrentSelection();
213         
214         if (intKeyType == -1){
215         
216                 // No option selected so do nothing.
217         
218                 return;
219         
220         }
221         
222         if (EditorMode == FALSE){
224                 wxString strValue;
225                 wxListItem coldata;             
226                 
227                 coldata.SetId(KeyListIndex);
228                 coldata.SetData(KeyListIndex);
229                 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);            
230                 
231                 // Get Key data.
232                 
233                 if (intKeyType == 0){
234                 
235                         // URL is text.
236                                         
237                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
239                         coldata.SetText(txtAddress->GetValue());
240                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
241                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
242                 
243                 } else if (intKeyType == 1){
244                 
245                         // Encode the data in the text to base64.
246                         
247                         wxString wxSb64orig = txtKeyData->GetValue();
248                         std::string base64new = std::string(wxSb64orig.mb_str());
249                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
250                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
251                         
252                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
253                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
254                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
255                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
256                 
257                 }
258                 
259                 // Get Key type.
260                 
261                 strValue.Clear();
262                 strValue = cmbType->GetString(cmbType->GetSelection());
264                 if (strValue == _("Home")) {
265                 
266                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
267                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
268                 
269                 } else if (strValue == _("Work")) {
270                 
271                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
272                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);                    
273                 
274                 } else {
275                 
276                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
277                 
278                 }
279                 
280                 // Get Key priority.
281                 
282                 if (chkUsePref->IsChecked()){
283                 
284                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
285                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));                        
286                 
287                 } else {
288                 
289                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
290                 
291                 }
292                 
293                 if (chkUsePref->IsChecked()){
294                 
295                 }
296                 
297                 this->Close();
298                 
299         } else if (EditorMode == TRUE){
301                 long longSelected = -1; 
302                 wxString strValue;              
303                 
304                 longSelected = KeyListCtrlPtr->GetNextItem(longSelected, 
305                         wxLIST_NEXT_ALL,
306                         wxLIST_STATE_SELECTED);
307                         
308                 if (longSelected == -1){
309                         return;
310                 }
311                 
312                 // Update Key data.             
313                 
314                 if (intKeyType == 0){
315                 
316                         // URL is text.
317                                         
318                         KeyListPtr->erase(KeyListIndex);
319                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
321                         KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
322                         KeyListKeyTypePtr->erase(KeyListIndex);                 
323                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
324                 
325                 } else if (intKeyType == 1){
326                 
327                         // Encode the data in the text to base64.
328                         
329                         wxString wxSb64orig = txtKeyData->GetValue();
330                         std::string base64new = std::string(wxSb64orig.mb_str());
331                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
332                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
333                         
334                         KeyListPtr->erase(KeyListIndex);                        
335                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
336                         
337                         KeyListDataTypePtr->erase(KeyListIndex);
338                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
339                         
340                         KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
341                         
342                         KeyListKeyTypePtr->erase(KeyListIndex);
343                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
344                 
345                 }
346                 
347                 // Update Key type.
348                 
349                 strValue.Clear();
350                 strValue = cmbType->GetString(cmbType->GetSelection());
352                 KeyListTypePtr->erase(KeyListIndex);
354                 if (strValue == _("Home")) {
355                 
356                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
357                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
358                 
359                 } else if (strValue == _("Work")) {
360                 
361                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
362                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);                     
363                 
364                 } else {
365                 
366                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
367                 
368                 }
369                 
370                 // Update the key priority.
371                 
372                 KeyListPrefPtr->erase(KeyListIndex);            
373                 
374                 if (chkUsePref->IsChecked()){
375                 
376                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
377                         KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
378                 
379                 } else {
380                 
381                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
382                 
383                 }
384                 
385                 this->Close();  
386         
387         }
390 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
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::ProcessKeyType()
414         int intKeyType = 0;
415         
416         intKeyType = cmbKeyMainType->GetCurrentSelection();
417         
418         if (intKeyType == 0){
419         
420                 tabURL->Show();
421                 tabKey->Hide();
422         
423         } else if (intKeyType == 1){
424         
425                 tabURL->Hide();
426                 tabKey->Show();
427         
428         }
432 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
435         wxString strValue;
437         strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
438         
439         if (strValue == _("PGP Key")){
440         
441                 cmbURL->SetValue(wxT("application/pgp-keys"));
442         
443         }
447 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
450         wxString strValue;
452         strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
453         
454         if (strValue == _("PGP Key")){
455         
456                 cmbKeyType->SetValue(wxT("application/pgp-keys"));
457         
458         }
462 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
464         this->Close();
467 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
468         std::map<int, wxString> *KeyListAltID,
469         std::map<int, wxString> *KeyListPID,
470         std::map<int, bool> *KeyListKeyType,    
471         std::map<int, wxString> *KeyListDataType,       
472         std::map<int, wxString> *KeyListType,
473         std::map<int, wxString> *KeyListTokens,
474         std::map<int, int> *KeyListPref,
475         wxListCtrl *KeyListCtrl,
476         int KeyIndex )
479         KeyListPtr = KeyList;
480         KeyListAltIDPtr = KeyListAltID;
481         KeyListPIDPtr = KeyListPID;
482         KeyListKeyTypePtr = KeyListKeyType;     
483         KeyListDataTypePtr = KeyListDataType;
484         KeyListTypePtr = KeyListType;
485         KeyListTokensPtr = KeyListTokens;
486         KeyListPrefPtr = KeyListPref;
487         KeyListCtrlPtr = KeyListCtrl;
488         KeyListIndex = KeyIndex;
489         
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