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"));
41 cmbType->Append(_("Home"));
42 cmbType->Append(_("Work"));
44 cmbURL->Append(_("PGP Key"));
46 cmbKeyType->Append(_("PGP Key"));
48 cmbType->SetSelection(0);
52 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
54 if (chkUsePref->IsChecked()){
55 sliPriority->Enable();
57 sliPriority->Disable();
61 void frmContactEditorKey::SetEditorMode(bool EditMode)
63 // Set if the editor is adding or editing an address.
67 if (EditMode == FALSE){
70 btnAction->SetLabel(_("Add"));
71 this->SetTitle(_("Add Key"));
73 } else if (EditMode == TRUE){
76 btnAction->SetLabel(_("Modify"));
77 this->SetTitle(_("Modify Key"));
79 std::map<int,bool>::iterator booliter;
80 std::map<int,int>::iterator intiter;
81 std::map<int,wxString>::iterator striter;
83 bool boolKeyData = FALSE;
85 // Get the key data type.
87 booliter = KeyListKeyTypePtr->find(KeyListIndex);
89 if (booliter->first == KeyListIndex){
91 boolKeyData = booliter->second;
95 // Get the data depending on key data type.
97 if (boolKeyData == FALSE){
101 cmbKeyMainType->SetSelection(0);
103 striter = KeyListPtr->find(KeyListIndex);
105 if (striter->first == KeyListIndex){
107 strValue = striter->second;
111 txtAddress->ChangeValue(strValue);
114 striter = KeyListDataTypePtr->find(KeyListIndex);
116 if (striter->first == KeyListIndex){
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){
157 strValue = striter->second;
161 cmbKeyType->SetValue(strValue);
169 striter = KeyListTypePtr->find(KeyListIndex);
171 if (striter->first == KeyListIndex){
173 strValue = striter->second;
177 if (strValue == wxT("home")){
179 cmbType->SetSelection(0);
181 } else if (strValue == wxT("work")){
183 cmbType->SetSelection(1);
187 //cmbType->SetSelection(-1);
191 // Get the key priority.
193 intiter = KeyListPrefPtr->find(KeyListIndex);
195 if (intiter->first == KeyListIndex && intiter->second > 0){
197 sliPriority->SetValue(intiter->second);
198 sliPriority->Enable();
199 chkUsePref->SetValue(TRUE);
207 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
212 intKeyType = cmbKeyMainType->GetCurrentSelection();
214 if (intKeyType == -1){
216 // No option selected so do nothing.
222 if (EditorMode == FALSE){
227 coldata.SetId(KeyListIndex);
228 coldata.SetData(KeyListIndex);
229 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
233 if (intKeyType == 0){
237 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
239 coldata.SetText(txtAddress->GetValue());
240 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
241 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
243 } else if (intKeyType == 1){
245 // Encode the data in the text to base64.
247 wxString wxSb64orig = txtKeyData->GetValue();
248 std::string base64new = std::string(wxSb64orig.mb_str());
249 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
250 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
252 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
253 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
254 KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
255 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
262 strValue = cmbType->GetString(cmbType->GetSelection());
264 if (strValue == _("Home")) {
266 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
267 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
269 } else if (strValue == _("Work")) {
271 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
272 KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
276 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
282 if (chkUsePref->IsChecked()){
284 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
285 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
289 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
293 if (chkUsePref->IsChecked()){
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 (chkUsePref->IsChecked()){
376 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
377 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
381 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
390 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
395 intKeyType = cmbKeyMainType->GetCurrentSelection();
397 if (intKeyType == 0){
402 } else if (intKeyType == 1){
411 void frmContactEditorKey::ProcessKeyType()
416 intKeyType = cmbKeyMainType->GetCurrentSelection();
418 if (intKeyType == 0){
423 } else if (intKeyType == 1){
432 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
437 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
439 if (strValue == _("PGP Key")){
441 cmbURL->SetValue(wxT("application/pgp-keys"));
447 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
452 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
454 if (strValue == _("PGP Key")){
456 cmbKeyType->SetValue(wxT("application/pgp-keys"));
462 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
467 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
468 std::map<int, wxString> *KeyListAltID,
469 std::map<int, wxString> *KeyListPID,
470 std::map<int, bool> *KeyListKeyType,
471 std::map<int, wxString> *KeyListDataType,
472 std::map<int, wxString> *KeyListType,
473 std::map<int, wxString> *KeyListTokens,
474 std::map<int, int> *KeyListPref,
475 wxListCtrl *KeyListCtrl,
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;