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 sliPriority->Disable();
34 // Setup the control boxes and hide tabs unless they are
40 cmbKeyMainType->Append(_("URL"));
41 cmbKeyMainType->Append(_("Key"));
44 cmbType->Append(_("Home"));
45 cmbType->Append(_("Work"));
47 cmbURL->Append(_("PGP Key"));
49 cmbKeyType->Append(_("PGP Key"));
51 cmbType->SetSelection(0);
55 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
58 // Enable/disable the priority setting.
60 if (chkUsePref->IsChecked()){
61 sliPriority->Enable();
63 sliPriority->Disable();
68 void frmContactEditorKey::SetEditorMode(bool EditMode)
71 // Set the editor mode.
73 // Set if the editor is adding or editing an address.
77 if (EditMode == FALSE){
80 btnAction->SetLabel(_("Add"));
81 this->SetTitle(_("Add Key"));
83 } else if (EditMode == TRUE){
86 btnAction->SetLabel(_("Modify"));
87 this->SetTitle(_("Modify Key"));
89 std::map<int,bool>::iterator booliter;
90 std::map<int,int>::iterator intiter;
91 std::map<int,wxString>::iterator striter;
93 bool boolKeyData = FALSE;
95 // Get the key data type.
97 booliter = KeyListKeyTypePtr->find(KeyListIndex);
99 if (booliter->first == KeyListIndex){
101 boolKeyData = booliter->second;
105 // Get the data depending on key data type.
107 if (boolKeyData == FALSE){
111 cmbKeyMainType->SetSelection(0);
113 striter = KeyListPtr->find(KeyListIndex);
115 if (striter->first == KeyListIndex){
117 strValue = striter->second;
121 txtAddress->ChangeValue(strValue);
124 striter = KeyListDataTypePtr->find(KeyListIndex);
126 if (striter->first == KeyListIndex &&
127 striter != KeyListDataTypePtr->end()){
129 strValue = striter->second;
133 cmbURL->SetValue(strValue);
135 } else if (boolKeyData == TRUE){
139 cmbKeyMainType->SetSelection(1);
140 std::string base64dec;
142 // Decode the base64 data.
144 striter = KeyListPtr->find(KeyListIndex);
146 if (striter->first == KeyListIndex){
148 strValue = striter->second;
152 std::string base64enc = std::string(strValue.mb_str());
153 base64dec = base64_decode(base64enc);
156 wxString strValue(base64dec.c_str(), wxConvUTF8);
158 txtKeyData->SetValue(strValue);
160 // Get the key data type.
164 striter = KeyListDataTypePtr->find(KeyListIndex);
166 if (striter->first == KeyListIndex &&
167 striter != KeyListDataTypePtr->end()){
169 strValue = striter->second;
173 cmbKeyType->SetValue(strValue);
181 striter = KeyListTypePtr->find(KeyListIndex);
183 if (striter->first == KeyListIndex &&
184 striter != KeyListTypePtr->end()){
186 strValue = striter->second;
190 if (strValue == wxT("home")){
192 cmbType->SetSelection(0);
194 } else if (strValue == wxT("work")){
196 cmbType->SetSelection(1);
200 //cmbType->SetSelection(-1);
204 // Get the key priority.
206 intiter = KeyListPrefPtr->find(KeyListIndex);
208 if (intiter->first == KeyListIndex && intiter->second > 0 &&
209 intiter != KeyListPrefPtr->end()){
211 sliPriority->SetValue(intiter->second);
212 sliPriority->Enable();
213 chkUsePref->SetValue(TRUE);
221 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
229 intKeyType = cmbKeyMainType->GetCurrentSelection();
231 if (intKeyType == -1){
233 // No option selected so do nothing.
239 if (EditorMode == FALSE){
244 coldata.SetId(KeyListIndex);
245 coldata.SetData(KeyListIndex);
246 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
250 if (intKeyType == 0){
254 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
256 coldata.SetText(txtAddress->GetValue());
257 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
258 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
260 } else if (intKeyType == 1){
262 // Encode the data in the text to base64.
264 wxString wxSb64orig = txtKeyData->GetValue();
265 std::string base64new = std::string(wxSb64orig.mb_str());
266 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
267 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
269 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
270 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
271 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
272 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
279 strValue = cmbType->GetString(cmbType->GetSelection());
281 if (strValue == _("Home")) {
283 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
284 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
286 } else if (strValue == _("Work")) {
288 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
289 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
293 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
299 if (chkUsePref->IsChecked()){
301 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
302 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
306 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
310 if (chkUsePref->IsChecked()){
316 } else if (EditorMode == TRUE){
318 long longSelected = -1;
321 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
323 wxLIST_STATE_SELECTED);
325 if (longSelected == -1){
331 if (intKeyType == 0){
335 KeyListPtr->erase(KeyListIndex);
336 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
338 KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
339 KeyListKeyTypePtr->erase(KeyListIndex);
340 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
342 } else if (intKeyType == 1){
344 // Encode the data in the text to base64.
346 wxString wxSb64orig = txtKeyData->GetValue();
347 std::string base64new = std::string(wxSb64orig.mb_str());
348 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
349 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
351 KeyListPtr->erase(KeyListIndex);
352 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
354 KeyListDataTypePtr->erase(KeyListIndex);
355 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
357 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
359 KeyListKeyTypePtr->erase(KeyListIndex);
360 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
367 strValue = cmbType->GetString(cmbType->GetSelection());
369 KeyListTypePtr->erase(KeyListIndex);
371 if (strValue == _("Home")) {
373 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
374 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
376 } else if (strValue == _("Work")) {
378 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
379 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
383 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
387 // Update the key priority.
389 KeyListPrefPtr->erase(KeyListIndex);
391 if (chkUsePref->IsChecked()){
393 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
394 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
398 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
407 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
410 // Process the key type (via wxCommandEvent).
416 void frmContactEditorKey::ProcessKeyType()
419 // Process the key type.
423 intKeyType = cmbKeyMainType->GetCurrentSelection();
425 if (intKeyType == 0){
430 } else if (intKeyType == 1){
439 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
442 // Convert the URL type.
446 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
448 if (strValue == _("PGP Key")){
450 cmbURL->SetValue(wxT("application/pgp-keys"));
456 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
459 // Convert the key type.
463 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
465 if (strValue == _("PGP Key")){
467 cmbKeyType->SetValue(wxT("application/pgp-keys"));
473 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
476 // Close this window.
482 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
483 std::map<int, wxString> *KeyListAltID,
484 std::map<int, wxString> *KeyListPID,
485 std::map<int, bool> *KeyListKeyType,
486 std::map<int, wxString> *KeyListDataType,
487 std::map<int, wxString> *KeyListType,
488 std::map<int, wxString> *KeyListTokens,
489 std::map<int, int> *KeyListPref,
490 wxListCtrl *KeyListCtrl,
494 // Setup the pointers.
496 KeyListPtr = KeyList;
497 KeyListAltIDPtr = KeyListAltID;
498 KeyListPIDPtr = KeyListPID;
499 KeyListKeyTypePtr = KeyListKeyType;
500 KeyListDataTypePtr = KeyListDataType;
501 KeyListTypePtr = KeyListType;
502 KeyListTokensPtr = KeyListTokens;
503 KeyListPrefPtr = KeyListPref;
504 KeyListCtrlPtr = KeyListCtrl;
505 KeyListIndex = KeyIndex;