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 &&
117 striter != KeyListDataTypePtr->end()){
119 strValue = striter->second;
123 cmbURL->SetValue(strValue);
125 } else if (boolKeyData == TRUE){
129 cmbKeyMainType->SetSelection(1);
130 std::string base64dec;
132 // Decode the base64 data.
134 striter = KeyListPtr->find(KeyListIndex);
136 if (striter->first == KeyListIndex){
138 strValue = striter->second;
142 std::string base64enc = std::string(strValue.mb_str());
143 base64dec = base64_decode(base64enc);
146 wxString strValue(base64dec.c_str(), wxConvUTF8);
148 txtKeyData->SetValue(strValue);
150 // Get the key data type.
154 striter = KeyListDataTypePtr->find(KeyListIndex);
156 if (striter->first == KeyListIndex &&
157 striter != KeyListDataTypePtr->end()){
159 strValue = striter->second;
163 cmbKeyType->SetValue(strValue);
171 striter = KeyListTypePtr->find(KeyListIndex);
173 if (striter->first == KeyListIndex &&
174 striter != KeyListTypePtr->end()){
176 strValue = striter->second;
180 if (strValue == wxT("home")){
182 cmbType->SetSelection(0);
184 } else if (strValue == wxT("work")){
186 cmbType->SetSelection(1);
190 //cmbType->SetSelection(-1);
194 // Get the key priority.
196 intiter = KeyListPrefPtr->find(KeyListIndex);
198 if (intiter->first == KeyListIndex && intiter->second > 0 &&
199 intiter != KeyListPrefPtr->end()){
201 sliPriority->SetValue(intiter->second);
202 sliPriority->Enable();
203 chkUsePref->SetValue(TRUE);
211 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 (chkUsePref->IsChecked()){
288 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
289 KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
293 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
297 if (chkUsePref->IsChecked()){
303 } else if (EditorMode == TRUE){
305 long longSelected = -1;
308 longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
310 wxLIST_STATE_SELECTED);
312 if (longSelected == -1){
318 if (intKeyType == 0){
322 KeyListPtr->erase(KeyListIndex);
323 KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
325 KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
326 KeyListKeyTypePtr->erase(KeyListIndex);
327 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
329 } else if (intKeyType == 1){
331 // Encode the data in the text to base64.
333 wxString wxSb64orig = txtKeyData->GetValue();
334 std::string base64new = std::string(wxSb64orig.mb_str());
335 std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
336 wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
338 KeyListPtr->erase(KeyListIndex);
339 KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
341 KeyListDataTypePtr->erase(KeyListIndex);
342 KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
344 KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
346 KeyListKeyTypePtr->erase(KeyListIndex);
347 KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
354 strValue = cmbType->GetString(cmbType->GetSelection());
356 KeyListTypePtr->erase(KeyListIndex);
358 if (strValue == _("Home")) {
360 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
361 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
363 } else if (strValue == _("Work")) {
365 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
366 KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
370 KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
374 // Update the key priority.
376 KeyListPrefPtr->erase(KeyListIndex);
378 if (chkUsePref->IsChecked()){
380 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
381 KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
385 KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
394 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
399 intKeyType = cmbKeyMainType->GetCurrentSelection();
401 if (intKeyType == 0){
406 } else if (intKeyType == 1){
415 void frmContactEditorKey::ProcessKeyType()
420 intKeyType = cmbKeyMainType->GetCurrentSelection();
422 if (intKeyType == 0){
427 } else if (intKeyType == 1){
436 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
441 strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
443 if (strValue == _("PGP Key")){
445 cmbURL->SetValue(wxT("application/pgp-keys"));
451 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
456 strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
458 if (strValue == _("PGP Key")){
460 cmbKeyType->SetValue(wxT("application/pgp-keys"));
466 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
471 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
472 std::map<int, wxString> *KeyListAltID,
473 std::map<int, wxString> *KeyListPID,
474 std::map<int, bool> *KeyListKeyType,
475 std::map<int, wxString> *KeyListDataType,
476 std::map<int, wxString> *KeyListType,
477 std::map<int, wxString> *KeyListTokens,
478 std::map<int, int> *KeyListPref,
479 wxListCtrl *KeyListCtrl,
483 KeyListPtr = KeyList;
484 KeyListAltIDPtr = KeyListAltID;
485 KeyListPIDPtr = KeyListPID;
486 KeyListKeyTypePtr = KeyListKeyType;
487 KeyListDataTypePtr = KeyListDataType;
488 KeyListTypePtr = KeyListType;
489 KeyListTokensPtr = KeyListTokens;
490 KeyListPrefPtr = KeyListPref;
491 KeyListCtrlPtr = KeyListCtrl;
492 KeyListIndex = KeyIndex;