1 #include "frmContactEditorKey.h"
2 #include <wx/tokenzr.h>
3 #include "../common/textprocessing.h"
4 #include "../common/base64.h"
6 frmContactEditorKey::frmContactEditorKey( wxWindow* parent )
8 frmContactEditorKeyADT( parent )
11 sliPriority->Disable();
13 // Setup the control boxes and hide tabs unless they are
19 cmbKeyMainType->Append(_("URL"));
20 cmbKeyMainType->Append(_("Key"));
22 cmbType->Append(_("Home"));
23 cmbType->Append(_("Work"));
25 cmbURL->Append(_("PGP Key"));
27 cmbKeyType->Append(_("PGP Key"));
31 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
33 if (chkUsePref->IsChecked()){
34 sliPriority->Enable();
36 sliPriority->Disable();
40 void frmContactEditorKey::SetEditorMode(bool EditMode)
42 // Set if the editor is adding or editing an address.
46 if (EditMode == FALSE){
49 btnAction->SetLabel(_("Add"));
50 this->SetTitle(_("Add Key"));
52 } else if (EditMode == TRUE){
55 btnAction->SetLabel(_("Modify"));
56 this->SetTitle(_("Modify Key"));
58 std::map<int,bool>::iterator booliter;
59 std::map<int,int>::iterator intiter;
60 std::map<int,wxString>::iterator striter;
62 bool boolKeyData = FALSE;
64 // Get the key data type.
66 booliter = KeyListKeyTypePtr->find(KeyListIndex);
68 if (booliter->first == KeyListIndex){
70 boolKeyData = booliter->second;
74 // Get the data depending on key data type.
76 if (boolKeyData == FALSE){
80 cmbKeyMainType->SetSelection(0);
82 striter = KeyListPtr->find(KeyListIndex);
84 if (striter->first == KeyListIndex){
86 strValue = striter->second;
90 txtAddress->ChangeValue(strValue);
93 striter = KeyListDataTypePtr->find(KeyListIndex);
95 if (striter->first == KeyListIndex){
97 strValue = striter->second;
101 cmbURL->SetValue(strValue);
103 } else if (boolKeyData == TRUE){
107 cmbKeyMainType->SetSelection(1);
108 std::string base64dec;
110 // Decode the base64 data.
112 striter = KeyListPtr->find(KeyListIndex);
114 if (striter->first == KeyListIndex){
116 strValue = striter->second;
120 std::string base64enc = std::string(strValue.mb_str());
121 base64dec = base64_decode(base64enc);
124 wxString strValue(base64dec.c_str(), wxConvUTF8);
126 txtKeyData->SetValue(strValue);
128 // Get the key data type.
132 striter = KeyListDataTypePtr->find(KeyListIndex);
134 if (striter->first == KeyListIndex){
136 strValue = striter->second;
140 cmbKeyType->SetValue(strValue);
148 striter = KeyListTypePtr->find(KeyListIndex);
150 if (striter->first == KeyListIndex){
152 strValue = striter->second;
156 if (strValue == wxT("home")){
158 cmbType->SetSelection(0);
160 } else if (strValue == wxT("work")){
162 cmbType->SetSelection(1);
166 //cmbType->SetSelection(-1);
170 // Get the key priority.
172 intiter = KeyListPrefPtr->find(KeyListIndex);
174 if (intiter->first == KeyListIndex && intiter->second > 0){
176 sliPriority->SetValue(intiter->second);
177 sliPriority->Enable();
178 chkUsePref->SetValue(TRUE);
186 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
191 intKeyType = cmbKeyMainType->GetCurrentSelection();
193 if (intKeyType == -1){
195 // No option selected so do nothing.
201 if (EditorMode == FALSE){
206 coldata.SetId(KeyListIndex);
207 coldata.SetData(KeyListIndex);
208 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
212 if (intKeyType == 0){
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));
222 } else if (intKeyType == 1){
224 // Encode the data in the text to base64.
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);
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));
241 strValue = cmbType->GetString(cmbType->GetSelection());
243 if (strValue == _("Home")) {
245 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
246 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
248 } else if (strValue == _("Work")) {
250 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
251 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
255 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
261 if (chkUsePref->IsChecked()){
263 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
264 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
268 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
272 if (chkUsePref->IsChecked()){
278 } else if (EditorMode == TRUE){
280 long longSelected = -1;
283 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
285 wxLIST_STATE_SELECTED);
287 if (longSelected == -1){
293 if (intKeyType == 0){
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));
304 } else if (intKeyType == 1){
306 // Encode the data in the text to base64.
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);
313 KeyListPtr->erase(KeyListIndex);
314 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
316 KeyListDataTypePtr->erase(KeyListIndex);
317 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
319 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
321 KeyListKeyTypePtr->erase(KeyListIndex);
322 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
329 strValue = cmbType->GetString(cmbType->GetSelection());
331 KeyListTypePtr->erase(KeyListIndex);
333 if (strValue == _("Home")) {
335 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
336 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
338 } else if (strValue == _("Work")) {
340 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
341 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
345 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
349 // Update the key priority.
351 KeyListPrefPtr->erase(KeyListIndex);
353 if (chkUsePref->IsChecked()){
355 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
356 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
360 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
369 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
374 intKeyType = cmbKeyMainType->GetCurrentSelection();
376 if (intKeyType == 0){
381 } else if (intKeyType == 1){
390 void frmContactEditorKey::ProcessKeyType()
395 intKeyType = cmbKeyMainType->GetCurrentSelection();
397 if (intKeyType == 0){
402 } else if (intKeyType == 1){
411 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
416 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
418 if (strValue == _("PGP Key")){
420 cmbURL->SetValue(wxT("application/pgp-keys"));
426 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
431 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
433 if (strValue == _("PGP Key")){
435 cmbKeyType->SetValue(wxT("application/pgp-keys"));
441 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
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,
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;