Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmPreferences: Removed std::cout printing contact background colour
[xestiaab/.git] / source / contacteditor / frmContactEditorKey.cpp
1 // frmContactEditorKey.cpp - frmContactEditorKey form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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 )
25 :
26 frmContactEditorKeyADT( parent )
27 {
28         
29         // Setup the window.
30         
31         EditorMode = FALSE;
32         sliPriority->Disable();
33         
34         // Setup the control boxes and hide tabs unless they are
35         // needed.
36         
37         tabURL->Hide();
38         tabKey->Hide();
39         
40         cmbKeyMainType->Append(_("URL"));
41         cmbKeyMainType->Append(_("Key"));
42         
43         cmbType->Append("");
44         cmbType->Append(_("Home"));
45         cmbType->Append(_("Work"));
46         
47         cmbURL->Append(_("PGP Key"));
48         
49         cmbKeyType->Append(_("PGP Key"));
50         
51         cmbType->SetSelection(0);
53 }
55 void frmContactEditorKey::EnablePriority( wxCommandEvent& event )
56 {
57         
58         // Enable/disable the priority setting.
59         
60         if (chkUsePref->IsChecked()){
61                 sliPriority->Enable();
62         } else {
63                 sliPriority->Disable();
64         }
65         
66 }
68 void frmContactEditorKey::SetEditorMode(bool EditMode)
69 {
70         
71         // Set the editor mode.
72         
73         // Set if the editor is adding or editing an address.
74         // FALSE = Add
75         // TRUE = Edit
76         
77         if (EditMode == FALSE){
78         
79                 EditorMode = FALSE;
80                 btnAction->SetLabel(_("Add"));
81                 this->SetTitle(_("Add Key"));
82         
83         } else if (EditMode == TRUE){
84         
85                 EditorMode = TRUE;
86                 btnAction->SetLabel(_("Modify"));
87                 this->SetTitle(_("Modify Key"));
88                 
89                 std::map<int,bool>::iterator booliter;
90                 std::map<int,int>::iterator intiter;
91                 std::map<int,wxString>::iterator striter;               
92                 wxString strValue;
93                 bool boolKeyData = FALSE;       
94                 
95                 // Get the key data type.
96                 
97                 booliter = KeyListKeyTypePtr->find(KeyListIndex);
98                 
99                 if (booliter->first == KeyListIndex){
100                 
101                         boolKeyData = booliter->second;
102                 
103                 }
104                 
105                 // Get the data depending on key data type.             
106                 
107                 if (boolKeyData == FALSE){
108                 
109                         // Setup the URL.
110                 
111                         cmbKeyMainType->SetSelection(0);
112                         
113                         striter = KeyListPtr->find(KeyListIndex);
114                         
115                         if (striter->first == KeyListIndex){
116                         
117                                 strValue = striter->second;
118                         
119                         }
120                         
121                         txtAddress->ChangeValue(strValue);
122                         strValue.Clear();
123                         
124                         striter = KeyListDataTypePtr->find(KeyListIndex);
125                         
126                         if (striter->first == KeyListIndex &&
127                                 striter != KeyListDataTypePtr->end()){
128                         
129                                 strValue = striter->second;
130                         
131                         }                       
132                         
133                         cmbURL->SetValue(strValue);
134                 
135                 } else if (boolKeyData == TRUE){
136                 
137                         // Setup the key.
138                 
139                         cmbKeyMainType->SetSelection(1);
140                         std::string base64dec;
141                         
142                         // Decode the base64 data.
143                         
144                         striter = KeyListPtr->find(KeyListIndex);
145                         
146                         if (striter->first == KeyListIndex){
147                         
148                                 strValue = striter->second;
149                         
150                         }
151                         
152                         std::string base64enc = std::string(strValue.mb_str());
153                         base64dec = base64_decode(base64enc);
154                         
155                         strValue.Clear();
156                         wxString strValue(base64dec.c_str(), wxConvUTF8);
157                         
158                         txtKeyData->SetValue(strValue);         
159                         
160                         // Get the key data type.
161                         
162                         strValue.Clear();
163                         
164                         striter = KeyListDataTypePtr->find(KeyListIndex);
165                         
166                         if (striter->first == KeyListIndex &&
167                                 striter != KeyListDataTypePtr->end()){
168                         
169                                 strValue = striter->second;
170                         
171                         }                       
172                         
173                         cmbKeyType->SetValue(strValue);                         
174                 
175                 }
176                 
177                 ProcessKeyType();
178                 
179                 // Get the type.
180                 
181                 striter = KeyListTypePtr->find(KeyListIndex);   
182                 
183                 if (striter->first == KeyListIndex &&
184                         striter != KeyListTypePtr->end()){
185                 
186                         strValue = striter->second;
187                 
188                 }               
189                 
190                 if (strValue == wxT("home")){
191                 
192                         cmbType->SetSelection(0);
193                 
194                 } else if (strValue == wxT("work")){
195                 
196                         cmbType->SetSelection(1);       
197                 
198                 } else {
199                 
200                         //cmbType->SetSelection(-1);
201                 
202                 }
203                 
204                 // Get the key priority.
205                 
206                 intiter = KeyListPrefPtr->find(KeyListIndex);
207                 
208                 if (intiter->first == KeyListIndex && intiter->second > 0 &&
209                         intiter != KeyListPrefPtr->end()){
210                 
211                         sliPriority->SetValue(intiter->second);
212                         sliPriority->Enable();
213                         chkUsePref->SetValue(TRUE);
214                 
215                 }
216                 
217         }
218                 
221 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
223         
224         // Process action.
225         
226         long ListCtrlIndex;
227         int intKeyType = 0;
228         
229         intKeyType = cmbKeyMainType->GetCurrentSelection();
230         
231         if (intKeyType == -1){
232         
233                 // No option selected so do nothing.
234         
235                 return;
236         
237         }
238         
239         if (EditorMode == FALSE){
241                 wxString strValue;
242                 wxListItem coldata;             
243                 
244                 coldata.SetId(KeyListIndex);
245                 coldata.SetData(KeyListIndex);
246                 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);            
247                 
248                 // Get Key data.
249                 
250                 if (intKeyType == 0){
251                 
252                         // URL is text.
253                                         
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));
259                 
260                 } else if (intKeyType == 1){
261                 
262                         // Encode the data in the text to base64.
263                         
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);
268                         
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));                  
273                 
274                 }
275                 
276                 // Get Key type.
277                 
278                 strValue.Clear();
279                 strValue = cmbType->GetString(cmbType->GetSelection());
281                 if (strValue == _("Home")) {
282                 
283                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
284                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
285                 
286                 } else if (strValue == _("Work")) {
287                 
288                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
289                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);                    
290                 
291                 } else {
292                 
293                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
294                 
295                 }
296                 
297                 // Get Key priority.
298                 
299                 if (chkUsePref->IsChecked()){
300                 
301                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
302                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));                        
303                 
304                 } else {
305                 
306                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
307                 
308                 }
309                 
310                 if (chkUsePref->IsChecked()){
311                 
312                 }
313                 
314                 this->Close();
315                 
316         } else if (EditorMode == TRUE){
318                 long longSelected = -1; 
319                 wxString strValue;              
320                 
321                 longSelected = KeyListCtrlPtr->GetNextItem(longSelected, 
322                         wxLIST_NEXT_ALL,
323                         wxLIST_STATE_SELECTED);
324                         
325                 if (longSelected == -1){
326                         return;
327                 }
328                 
329                 // Update Key data.             
330                 
331                 if (intKeyType == 0){
332                 
333                         // URL is text.
334                                         
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));
341                 
342                 } else if (intKeyType == 1){
343                 
344                         // Encode the data in the text to base64.
345                         
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);
350                         
351                         KeyListPtr->erase(KeyListIndex);                        
352                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
353                         
354                         KeyListDataTypePtr->erase(KeyListIndex);
355                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
356                         
357                         KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
358                         
359                         KeyListKeyTypePtr->erase(KeyListIndex);
360                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
361                 
362                 }
363                 
364                 // Update Key type.
365                 
366                 strValue.Clear();
367                 strValue = cmbType->GetString(cmbType->GetSelection());
369                 KeyListTypePtr->erase(KeyListIndex);
371                 if (strValue == _("Home")) {
372                 
373                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
374                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
375                 
376                 } else if (strValue == _("Work")) {
377                 
378                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
379                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);                     
380                 
381                 } else {
382                 
383                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
384                 
385                 }
386                 
387                 // Update the key priority.
388                 
389                 KeyListPrefPtr->erase(KeyListIndex);            
390                 
391                 if (chkUsePref->IsChecked()){
392                 
393                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, sliPriority->GetValue()));
394                         KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
395                 
396                 } else {
397                 
398                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
399                 
400                 }
401                 
402                 this->Close();  
403         
404         }
407 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
410         // Process the key type (via wxCommandEvent).
412         ProcessKeyType();
413         
416 void frmContactEditorKey::ProcessKeyType()
419         // Process the key type.
420         
421         int intKeyType = 0;
422         
423         intKeyType = cmbKeyMainType->GetCurrentSelection();
424         
425         if (intKeyType == 0){
426         
427                 tabURL->Show();
428                 tabKey->Hide();
429         
430         } else if (intKeyType == 1){
431         
432                 tabURL->Hide();
433                 tabKey->Show();
434         
435         }
439 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
442         // Convert the URL type.
443         
444         wxString strValue;
446         strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
447         
448         if (strValue == _("PGP Key")){
449         
450                 cmbURL->SetValue(wxT("application/pgp-keys"));
451         
452         }
456 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
459         // Convert the key type.
460         
461         wxString strValue;
463         strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
464         
465         if (strValue == _("PGP Key")){
466         
467                 cmbKeyType->SetValue(wxT("application/pgp-keys"));
468         
469         }
473 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
475         
476         // Close this window.
477         
478         this->Close();
479         
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,
491         int KeyIndex )
494         // Setup the pointers.
495         
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;
506         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy