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);
36 // Setup the control boxes and hide tabs unless they are
42 cmbKeyMainType->Append(_("URL"));
43 cmbKeyMainType->Append(_("Key"));
46 cmbType->Append(_("Home"));
47 cmbType->Append(_("Work"));
49 cmbURL->Append(_("PGP Key"));
51 cmbKeyType->Append(_("PGP Key"));
53 cmbType->SetSelection(0);
57 void frmContactEditorKey::SetEditorMode(bool EditMode)
60 // Set the editor mode.
62 // Set if the editor is adding or editing an address.
66 if (EditMode == FALSE){
69 btnAction->SetLabel(_("Add"));
70 this->SetTitle(_("Add Key"));
72 } else if (EditMode == TRUE){
75 btnAction->SetLabel(_("Modify"));
76 this->SetTitle(_("Modify Key"));
78 std::map<int,bool>::iterator booliter;
79 std::map<int,int>::iterator intiter;
80 std::map<int,wxString>::iterator striter;
82 bool boolKeyData = FALSE;
84 // Get the key data type.
86 booliter = KeyListKeyTypePtr->find(KeyListIndex);
88 if (booliter->first == KeyListIndex){
90 boolKeyData = booliter->second;
94 // Get the data depending on key data type.
96 if (boolKeyData == FALSE){
100 cmbKeyMainType->SetSelection(0);
102 striter = KeyListPtr->find(KeyListIndex);
104 if (striter->first == KeyListIndex){
106 strValue = striter->second;
110 txtAddress->ChangeValue(strValue);
113 striter = KeyListDataTypePtr->find(KeyListIndex);
115 if (striter->first == KeyListIndex &&
116 striter != KeyListDataTypePtr->end()){
118 strValue = striter->second;
122 cmbURL->SetValue(strValue);
124 } else if (boolKeyData == TRUE){
128 cmbKeyMainType->SetSelection(1);
129 std::string base64dec;
131 // Decode the base64 data.
133 striter = KeyListPtr->find(KeyListIndex);
135 if (striter->first == KeyListIndex){
137 strValue = striter->second;
141 std::string base64enc = std::string(strValue.mb_str());
142 base64dec = base64_decode(base64enc);
145 wxString strValue(base64dec.c_str(), wxConvUTF8);
147 txtKeyData->SetValue(strValue);
149 // Get the key data type.
153 striter = KeyListDataTypePtr->find(KeyListIndex);
155 if (striter->first == KeyListIndex &&
156 striter != KeyListDataTypePtr->end()){
158 strValue = striter->second;
162 cmbKeyType->SetValue(strValue);
170 striter = KeyListTypePtr->find(KeyListIndex);
172 if (striter->first == KeyListIndex &&
173 striter != KeyListTypePtr->end()){
175 strValue = striter->second;
179 if (strValue == wxT("home")){
181 cmbType->SetSelection(0);
183 } else if (strValue == wxT("work")){
185 cmbType->SetSelection(1);
189 //cmbType->SetSelection(-1);
193 // Get the key priority.
195 intiter = KeyListPrefPtr->find(KeyListIndex);
197 if (intiter->first == KeyListIndex && intiter->second > 0 &&
198 intiter != KeyListPrefPtr->end()){
200 priorityCtrl->SetValue(intiter->second);
201 priorityCtrl->EnablePriority(true);
209 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
217 intKeyType = cmbKeyMainType->GetCurrentSelection();
219 if (intKeyType == -1){
221 // No option selected so do nothing.
227 if (EditorMode == FALSE){
232 coldata.SetId(KeyListIndex);
233 coldata.SetData(KeyListIndex);
234 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
238 if (intKeyType == 0){
242 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
244 coldata.SetText(txtAddress->GetValue());
245 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
246 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
248 } else if (intKeyType == 1){
250 // Encode the data in the text to base64.
252 wxString wxSb64orig = txtKeyData->GetValue();
253 std::string base64new = std::string(wxSb64orig.mb_str());
254 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
255 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
257 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
258 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
259 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
260 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
267 strValue = cmbType->GetString(cmbType->GetSelection());
269 if (strValue == _("Home")) {
271 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
272 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
274 } else if (strValue == _("Work")) {
276 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
277 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
281 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
287 if (priorityCtrl->IsPriorityChecked()){
289 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
290 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
294 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
300 } else if (EditorMode == TRUE){
302 long longSelected = -1;
305 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
307 wxLIST_STATE_SELECTED);
309 if (longSelected == -1){
315 if (intKeyType == 0){
319 KeyListPtr->erase(KeyListIndex);
320 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
322 KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
323 KeyListKeyTypePtr->erase(KeyListIndex);
324 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
326 } else if (intKeyType == 1){
328 // Encode the data in the text to base64.
330 wxString wxSb64orig = txtKeyData->GetValue();
331 std::string base64new = std::string(wxSb64orig.mb_str());
332 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
333 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
335 KeyListPtr->erase(KeyListIndex);
336 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
338 KeyListDataTypePtr->erase(KeyListIndex);
339 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
341 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
343 KeyListKeyTypePtr->erase(KeyListIndex);
344 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
351 strValue = cmbType->GetString(cmbType->GetSelection());
353 KeyListTypePtr->erase(KeyListIndex);
355 if (strValue == _("Home")) {
357 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
358 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
360 } else if (strValue == _("Work")) {
362 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
363 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
367 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
371 // Update the key priority.
373 KeyListPrefPtr->erase(KeyListIndex);
375 if (priorityCtrl->IsPriorityChecked()){
377 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
378 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
382 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
391 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
394 // Process the key type (via wxCommandEvent).
400 void frmContactEditorKey::ProcessKeyType()
403 // Process the key type.
407 intKeyType = cmbKeyMainType->GetCurrentSelection();
409 if (intKeyType == 0){
414 } else if (intKeyType == 1){
423 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
426 // Convert the URL type.
430 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
432 if (strValue == _("PGP Key")){
434 cmbURL->SetValue(wxT("application/pgp-keys"));
440 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
443 // Convert the key type.
447 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
449 if (strValue == _("PGP Key")){
451 cmbKeyType->SetValue(wxT("application/pgp-keys"));
457 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
460 // Close this window.
466 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
467 std::map<int, wxString> *KeyListAltID,
468 std::map<int, wxString> *KeyListPID,
469 std::map<int, bool> *KeyListKeyType,
470 std::map<int, wxString> *KeyListDataType,
471 std::map<int, wxString> *KeyListType,
472 std::map<int, wxString> *KeyListTokens,
473 std::map<int, int> *KeyListPref,
474 wxListCtrl *KeyListCtrl,
478 // Setup the pointers.
480 KeyListPtr = KeyList;
481 KeyListAltIDPtr = KeyListAltID;
482 KeyListPIDPtr = KeyListPID;
483 KeyListKeyTypePtr = KeyListKeyType;
484 KeyListDataTypePtr = KeyListDataType;
485 KeyListTypePtr = KeyListType;
486 KeyListTokensPtr = KeyListTokens;
487 KeyListPrefPtr = KeyListPref;
488 KeyListCtrlPtr = KeyListCtrl;
489 KeyListIndex = KeyIndex;