// frmContactEditorKey.cpp - frmContactEditorKey form.
//
// (c) 2012-2015 Xestia Software Development.
//
// This file is part of Xestia Address Book.
//
// Xestia Address Book is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, version 3 of the license.
//
// Xestia Address Book is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with Xestia Address Book. If not, see
#include "frmContactEditorKey.h"
#include
#include "../common/textprocessing.h"
#include "../common/base64.h"
frmContactEditorKey::frmContactEditorKey( wxWindow* parent )
:
frmContactEditorKeyADT( parent )
{
// Setup the window.
EditorMode = FALSE;
sliPriority->Disable();
// Setup the control boxes and hide tabs unless they are
// needed.
tabURL->Hide();
tabKey->Hide();
cmbKeyMainType->Append(_("URL"));
cmbKeyMainType->Append(_("Key"));
cmbType->Append("");
cmbType->Append(_("Home"));
cmbType->Append(_("Work"));
cmbURL->Append(_("PGP Key"));
cmbKeyType->Append(_("PGP Key"));
cmbType->SetSelection(0);
}
void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
{
// Enable/disable the priority setting.
if (chkUsePref->IsChecked()){
sliPriority->Enable();
} else {
sliPriority->Disable();
}
}
void frmContactEditorKey::SetEditorMode(bool EditMode)
{
// Set the editor mode.
// Set if the editor is adding or editing an address.
// FALSE = Add
// TRUE = Edit
if (EditMode == FALSE){
EditorMode = FALSE;
btnAction->SetLabel(_("Add"));
this->SetTitle(_("Add Key"));
} else if (EditMode == TRUE){
EditorMode = TRUE;
btnAction->SetLabel(_("Modify"));
this->SetTitle(_("Modify Key"));
std::map::iterator booliter;
std::map::iterator intiter;
std::map::iterator striter;
wxString strValue;
bool boolKeyData = FALSE;
// Get the key data type.
booliter = KeyListKeyTypePtr->find(KeyListIndex);
if (booliter->first == KeyListIndex){
boolKeyData = booliter->second;
}
// Get the data depending on key data type.
if (boolKeyData == FALSE){
// Setup the URL.
cmbKeyMainType->SetSelection(0);
striter = KeyListPtr->find(KeyListIndex);
if (striter->first == KeyListIndex){
strValue = striter->second;
}
txtAddress->ChangeValue(strValue);
strValue.Clear();
striter = KeyListDataTypePtr->find(KeyListIndex);
if (striter->first == KeyListIndex &&
striter != KeyListDataTypePtr->end()){
strValue = striter->second;
}
cmbURL->SetValue(strValue);
} else if (boolKeyData == TRUE){
// Setup the key.
cmbKeyMainType->SetSelection(1);
std::string base64dec;
// Decode the base64 data.
striter = KeyListPtr->find(KeyListIndex);
if (striter->first == KeyListIndex){
strValue = striter->second;
}
std::string base64enc = std::string(strValue.mb_str());
base64dec = base64_decode(base64enc);
strValue.Clear();
wxString strValue(base64dec.c_str(), wxConvUTF8);
txtKeyData->SetValue(strValue);
// Get the key data type.
strValue.Clear();
striter = KeyListDataTypePtr->find(KeyListIndex);
if (striter->first == KeyListIndex &&
striter != KeyListDataTypePtr->end()){
strValue = striter->second;
}
cmbKeyType->SetValue(strValue);
}
ProcessKeyType();
// Get the type.
striter = KeyListTypePtr->find(KeyListIndex);
if (striter->first == KeyListIndex &&
striter != KeyListTypePtr->end()){
strValue = striter->second;
}
if (strValue == wxT("home")){
cmbType->SetSelection(0);
} else if (strValue == wxT("work")){
cmbType->SetSelection(1);
} else {
//cmbType->SetSelection(-1);
}
// Get the key priority.
intiter = KeyListPrefPtr->find(KeyListIndex);
if (intiter->first == KeyListIndex && intiter->second > 0 &&
intiter != KeyListPrefPtr->end()){
sliPriority->SetValue(intiter->second);
sliPriority->Enable();
chkUsePref->SetValue(TRUE);
}
}
}
void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
{
// Process action.
long ListCtrlIndex;
int intKeyType = 0;
intKeyType = cmbKeyMainType->GetCurrentSelection();
if (intKeyType == -1){
// No option selected so do nothing.
return;
}
if (EditorMode == FALSE){
wxString strValue;
wxListItem coldata;
coldata.SetId(KeyListIndex);
coldata.SetData(KeyListIndex);
ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);
// Get Key data.
if (intKeyType == 0){
// URL is text.
KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
coldata.SetText(txtAddress->GetValue());
KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
} else if (intKeyType == 1){
// Encode the data in the text to base64.
wxString wxSb64orig = txtKeyData->GetValue();
std::string base64new = std::string(wxSb64orig.mb_str());
std::string base64enc = base64_encode(reinterpret_cast(base64new.c_str()), (int)base64new.length());
wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
}
// Get Key type.
strValue.Clear();
strValue = cmbType->GetString(cmbType->GetSelection());
if (strValue == _("Home")) {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
} else if (strValue == _("Work")) {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
} else {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
}
// Get Key priority.
if (chkUsePref->IsChecked()){
KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
} else {
KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
}
if (chkUsePref->IsChecked()){
}
this->Close();
} else if (EditorMode == TRUE){
long longSelected = -1;
wxString strValue;
longSelected = KeyListCtrlPtr->GetNextItem(longSelected,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
if (longSelected == -1){
return;
}
// Update Key data.
if (intKeyType == 0){
// URL is text.
KeyListPtr->erase(KeyListIndex);
KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
KeyListKeyTypePtr->erase(KeyListIndex);
KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
} else if (intKeyType == 1){
// Encode the data in the text to base64.
wxString wxSb64orig = txtKeyData->GetValue();
std::string base64new = std::string(wxSb64orig.mb_str());
std::string base64enc = base64_encode(reinterpret_cast(base64new.c_str()), (int)base64new.length());
wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
KeyListPtr->erase(KeyListIndex);
KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
KeyListDataTypePtr->erase(KeyListIndex);
KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
KeyListKeyTypePtr->erase(KeyListIndex);
KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));
}
// Update Key type.
strValue.Clear();
strValue = cmbType->GetString(cmbType->GetSelection());
KeyListTypePtr->erase(KeyListIndex);
if (strValue == _("Home")) {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
} else if (strValue == _("Work")) {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
} else {
KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));
}
// Update the key priority.
KeyListPrefPtr->erase(KeyListIndex);
if (chkUsePref->IsChecked()){
KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
} else {
KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
}
this->Close();
}
}
void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
{
// Process the key type (via wxCommandEvent).
ProcessKeyType();
}
void frmContactEditorKey::ProcessKeyType()
{
// Process the key type.
int intKeyType = 0;
intKeyType = cmbKeyMainType->GetCurrentSelection();
if (intKeyType == 0){
tabURL->Show();
tabKey->Hide();
} else if (intKeyType == 1){
tabURL->Hide();
tabKey->Show();
}
}
void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
{
// Convert the URL type.
wxString strValue;
strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
if (strValue == _("PGP Key")){
cmbURL->SetValue(wxT("application/pgp-keys"));
}
}
void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
{
// Convert the key type.
wxString strValue;
strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
if (strValue == _("PGP Key")){
cmbKeyType->SetValue(wxT("application/pgp-keys"));
}
}
void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
{
// Close this window.
this->Close();
}
void frmContactEditorKey::SetupPointers(std::map *KeyList,
std::map *KeyListAltID,
std::map *KeyListPID,
std::map *KeyListKeyType,
std::map *KeyListDataType,
std::map *KeyListType,
std::map *KeyListTokens,
std::map *KeyListPref,
wxListCtrl *KeyListCtrl,
int KeyIndex )
{
// Setup the pointers.
KeyListPtr = KeyList;
KeyListAltIDPtr = KeyListAltID;
KeyListPIDPtr = KeyListPID;
KeyListKeyTypePtr = KeyListKeyType;
KeyListDataTypePtr = KeyListDataType;
KeyListTypePtr = KeyListType;
KeyListTokensPtr = KeyListTokens;
KeyListPrefPtr = KeyListPref;
KeyListCtrlPtr = KeyListCtrl;
KeyListIndex = KeyIndex;
}