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 )
29 sliPriority->Disable();
31 // Setup the control boxes and hide tabs unless they are
37 cmbKeyMainType->Append(_("URL"));
38 cmbKeyMainType->Append(_("Key"));
40 cmbType->Append(_("Home"));
41 cmbType->Append(_("Work"));
43 cmbURL->Append(_("PGP Key"));
45 cmbKeyType->Append(_("PGP Key"));
49 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
51 if (chkUsePref->IsChecked()){
52 sliPriority->Enable();
54 sliPriority->Disable();
58 void frmContactEditorKey::SetEditorMode(bool EditMode)
60 // Set if the editor is adding or editing an address.
64 if (EditMode == FALSE){
67 btnAction->SetLabel(_("Add"));
68 this->SetTitle(_("Add Key"));
70 } else if (EditMode == TRUE){
73 btnAction->SetLabel(_("Modify"));
74 this->SetTitle(_("Modify Key"));
76 std::map<int,bool>::iterator booliter;
77 std::map<int,int>::iterator intiter;
78 std::map<int,wxString>::iterator striter;
80 bool boolKeyData = FALSE;
82 // Get the key data type.
84 booliter = KeyListKeyTypePtr->find(KeyListIndex);
86 if (booliter->first == KeyListIndex){
88 boolKeyData = booliter->second;
92 // Get the data depending on key data type.
94 if (boolKeyData == FALSE){
98 cmbKeyMainType->SetSelection(0);
100 striter = KeyListPtr->find(KeyListIndex);
102 if (striter->first == KeyListIndex){
104 strValue = striter->second;
108 txtAddress->ChangeValue(strValue);
111 striter = KeyListDataTypePtr->find(KeyListIndex);
113 if (striter->first == KeyListIndex){
115 strValue = striter->second;
119 cmbURL->SetValue(strValue);
121 } else if (boolKeyData == TRUE){
125 cmbKeyMainType->SetSelection(1);
126 std::string base64dec;
128 // Decode the base64 data.
130 striter = KeyListPtr->find(KeyListIndex);
132 if (striter->first == KeyListIndex){
134 strValue = striter->second;
138 std::string base64enc = std::string(strValue.mb_str());
139 base64dec = base64_decode(base64enc);
142 wxString strValue(base64dec.c_str(), wxConvUTF8);
144 txtKeyData->SetValue(strValue);
146 // Get the key data type.
150 striter = KeyListDataTypePtr->find(KeyListIndex);
152 if (striter->first == KeyListIndex){
154 strValue = striter->second;
158 cmbKeyType->SetValue(strValue);
166 striter = KeyListTypePtr->find(KeyListIndex);
168 if (striter->first == KeyListIndex){
170 strValue = striter->second;
174 if (strValue == wxT("home")){
176 cmbType->SetSelection(0);
178 } else if (strValue == wxT("work")){
180 cmbType->SetSelection(1);
184 //cmbType->SetSelection(-1);
188 // Get the key priority.
190 intiter = KeyListPrefPtr->find(KeyListIndex);
192 if (intiter->first == KeyListIndex && intiter->second > 0){
194 sliPriority->SetValue(intiter->second);
195 sliPriority->Enable();
196 chkUsePref->SetValue(TRUE);
204 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
209 intKeyType = cmbKeyMainType->GetCurrentSelection();
211 if (intKeyType == -1){
213 // No option selected so do nothing.
219 if (EditorMode == FALSE){
224 coldata.SetId(KeyListIndex);
225 coldata.SetData(KeyListIndex);
226 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
230 if (intKeyType == 0){
234 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
236 coldata.SetText(txtAddress->GetValue());
237 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
238 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
240 } else if (intKeyType == 1){
242 // Encode the data in the text to base64.
244 wxString wxSb64orig = txtKeyData->GetValue();
245 std::string base64new = std::string(wxSb64orig.mb_str());
246 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
247 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
249 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
250 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
251 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
252 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
259 strValue = cmbType->GetString(cmbType->GetSelection());
261 if (strValue == _("Home")) {
263 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
264 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
266 } else if (strValue == _("Work")) {
268 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
269 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
273 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
279 if (chkUsePref->IsChecked()){
281 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
282 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
286 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
290 if (chkUsePref->IsChecked()){
296 } else if (EditorMode == TRUE){
298 long longSelected = -1;
301 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
303 wxLIST_STATE_SELECTED);
305 if (longSelected == -1){
311 if (intKeyType == 0){
315 KeyListPtr->erase(KeyListIndex);
316 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
318 KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
319 KeyListKeyTypePtr->erase(KeyListIndex);
320 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
322 } else if (intKeyType == 1){
324 // Encode the data in the text to base64.
326 wxString wxSb64orig = txtKeyData->GetValue();
327 std::string base64new = std::string(wxSb64orig.mb_str());
328 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
329 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
331 KeyListPtr->erase(KeyListIndex);
332 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
334 KeyListDataTypePtr->erase(KeyListIndex);
335 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
337 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
339 KeyListKeyTypePtr->erase(KeyListIndex);
340 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
347 strValue = cmbType->GetString(cmbType->GetSelection());
349 KeyListTypePtr->erase(KeyListIndex);
351 if (strValue == _("Home")) {
353 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
354 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
356 } else if (strValue == _("Work")) {
358 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
359 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
363 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
367 // Update the key priority.
369 KeyListPrefPtr->erase(KeyListIndex);
371 if (chkUsePref->IsChecked()){
373 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
374 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
378 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
387 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
392 intKeyType = cmbKeyMainType->GetCurrentSelection();
394 if (intKeyType == 0){
399 } else if (intKeyType == 1){
408 void frmContactEditorKey::ProcessKeyType()
413 intKeyType = cmbKeyMainType->GetCurrentSelection();
415 if (intKeyType == 0){
420 } else if (intKeyType == 1){
429 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
434 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
436 if (strValue == _("PGP Key")){
438 cmbURL->SetValue(wxT("application/pgp-keys"));
444 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
449 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
451 if (strValue == _("PGP Key")){
453 cmbKeyType->SetValue(wxT("application/pgp-keys"));
459 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
464 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
465 std::map<int, wxString> *KeyListAltID,
466 std::map<int, wxString> *KeyListPID,
467 std::map<int, bool> *KeyListKeyType,
468 std::map<int, wxString> *KeyListDataType,
469 std::map<int, wxString> *KeyListType,
470 std::map<int, wxString> *KeyListTokens,
471 std::map<int, int> *KeyListPref,
472 wxListCtrl *KeyListCtrl,
476 KeyListPtr = KeyList;
477 KeyListAltIDPtr = KeyListAltID;
478 KeyListPIDPtr = KeyListPID;
479 KeyListKeyTypePtr = KeyListKeyType;
480 KeyListDataTypePtr = KeyListDataType;
481 KeyListTypePtr = KeyListType;
482 KeyListTokensPtr = KeyListTokens;
483 KeyListPrefPtr = KeyListPref;
484 KeyListCtrlPtr = KeyListCtrl;
485 KeyListIndex = KeyIndex;