1 // frmContactEditorKey.cpp - frmContactEditorKey form.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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 )
26 frmContactEditorKeyADT( parent )
32 priorityCtrl = new XABPriorityCtrl(tabGeneral);
33 szrGeneral->Add(priorityCtrl, 1, wxEXPAND, 5);
35 // Setup the control boxes and hide tabs unless they are
41 cmbKeyMainType->Append(_("URL"));
42 cmbKeyMainType->Append(_("Key"));
45 cmbType->Append(_("Home"));
46 cmbType->Append(_("Work"));
48 cmbURL->Append(_("PGP Key"));
50 cmbKeyType->Append(_("PGP Key"));
52 cmbType->SetSelection(0);
56 void frmContactEditorKey::SetEditorMode(bool EditMode)
59 // Set the editor mode.
61 // Set if the editor is adding or editing an address.
65 if (EditMode == FALSE){
68 btnAction->SetLabel(_("Add"));
69 this->SetTitle(_("Add Key"));
71 } else if (EditMode == TRUE){
74 btnAction->SetLabel(_("Modify"));
75 this->SetTitle(_("Modify Key"));
77 std::map<int,bool>::iterator booliter;
78 std::map<int,int>::iterator intiter;
79 std::map<int,wxString>::iterator striter;
81 bool boolKeyData = FALSE;
83 // Get the key data type.
85 booliter = KeyListKeyTypePtr->find(KeyListIndex);
87 if (booliter->first == KeyListIndex){
89 boolKeyData = booliter->second;
93 // Get the data depending on key data type.
95 if (boolKeyData == FALSE){
99 cmbKeyMainType->SetSelection(0);
101 striter = KeyListPtr->find(KeyListIndex);
103 if (striter->first == KeyListIndex){
105 strValue = striter->second;
109 txtAddress->ChangeValue(strValue);
112 striter = KeyListDataTypePtr->find(KeyListIndex);
114 if (striter->first == KeyListIndex &&
115 striter != KeyListDataTypePtr->end()){
117 strValue = striter->second;
121 cmbURL->SetValue(strValue);
123 } else if (boolKeyData == TRUE){
127 cmbKeyMainType->SetSelection(1);
128 std::string base64dec;
130 // Decode the base64 data.
132 striter = KeyListPtr->find(KeyListIndex);
134 if (striter->first == KeyListIndex){
136 strValue = striter->second;
140 std::string base64enc = std::string(strValue.mb_str());
141 base64dec = base64_decode(base64enc);
144 wxString strValue(base64dec.c_str(), wxConvUTF8);
146 txtKeyData->SetValue(strValue);
148 // Get the key data type.
152 striter = KeyListDataTypePtr->find(KeyListIndex);
154 if (striter->first == KeyListIndex &&
155 striter != KeyListDataTypePtr->end()){
157 strValue = striter->second;
161 cmbKeyType->SetValue(strValue);
169 striter = KeyListTypePtr->find(KeyListIndex);
171 if (striter->first == KeyListIndex &&
172 striter != KeyListTypePtr->end()){
174 strValue = striter->second;
178 if (strValue == wxT("home")){
180 cmbType->SetSelection(0);
182 } else if (strValue == wxT("work")){
184 cmbType->SetSelection(1);
188 //cmbType->SetSelection(-1);
192 // Get the key priority.
194 intiter = KeyListPrefPtr->find(KeyListIndex);
196 if (intiter->first == KeyListIndex && intiter->second > 0 &&
197 intiter != KeyListPrefPtr->end()){
199 priorityCtrl->SetValue(intiter->second);
200 priorityCtrl->EnablePriority(true);
208 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
216 intKeyType = cmbKeyMainType->GetCurrentSelection();
218 if (intKeyType == -1){
220 // No option selected so do nothing.
226 if (EditorMode == FALSE){
231 coldata.SetId(KeyListIndex);
232 coldata.SetData(KeyListIndex);
233 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
237 if (intKeyType == 0){
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));
247 } else if (intKeyType == 1){
249 // Encode the data in the text to base64.
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);
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));
266 strValue = cmbType->GetString(cmbType->GetSelection());
268 if (strValue == _("Home")) {
270 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
271 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
273 } else if (strValue == _("Work")) {
275 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
276 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
280 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
286 if (priorityCtrl->IsPriorityChecked()){
288 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
289 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
293 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
299 } else if (EditorMode == TRUE){
301 long longSelected = -1;
304 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
306 wxLIST_STATE_SELECTED);
308 if (longSelected == -1){
314 if (intKeyType == 0){
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));
325 } else if (intKeyType == 1){
327 // Encode the data in the text to base64.
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);
334 KeyListPtr->erase(KeyListIndex);
335 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
337 KeyListDataTypePtr->erase(KeyListIndex);
338 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
340 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
342 KeyListKeyTypePtr->erase(KeyListIndex);
343 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
350 strValue = cmbType->GetString(cmbType->GetSelection());
352 KeyListTypePtr->erase(KeyListIndex);
354 if (strValue == _("Home")) {
356 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
357 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
359 } else if (strValue == _("Work")) {
361 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
362 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
366 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
370 // Update the key priority.
372 KeyListPrefPtr->erase(KeyListIndex);
374 if (priorityCtrl->IsPriorityChecked()){
376 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
377 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
381 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
390 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
393 // Process the key type (via wxCommandEvent).
399 void frmContactEditorKey::ProcessKeyType()
402 // Process the key type.
406 intKeyType = cmbKeyMainType->GetCurrentSelection();
408 if (intKeyType == 0){
413 } else if (intKeyType == 1){
422 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
425 // Convert the URL type.
429 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
431 if (strValue == _("PGP Key")){
433 cmbURL->SetValue(wxT("application/pgp-keys"));
439 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
442 // Convert the key type.
446 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
448 if (strValue == _("PGP Key")){
450 cmbKeyType->SetValue(wxT("application/pgp-keys"));
456 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
459 // Close this window.
465 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
466 std::map<int, wxString> *KeyListAltID,
467 std::map<int, wxString> *KeyListPID,
468 std::map<int, bool> *KeyListKeyType,
469 std::map<int, wxString> *KeyListDataType,
470 std::map<int, wxString> *KeyListType,
471 std::map<int, wxString> *KeyListTokens,
472 std::map<int, int> *KeyListPref,
473 wxListCtrl *KeyListCtrl,
477 // Setup the pointers.
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;