Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor(*): Call Layout on szrGeneral after adding priorty control
[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         priorityCtrl = new XABPriorityCtrl(tabGeneral);
33         szrGeneral->Add(priorityCtrl, 1, wxEXPAND, 5);
34         szrGeneral->Layout();
35         
36         // Setup the control boxes and hide tabs unless they are
37         // needed.
38         
39         tabURL->Hide();
40         tabKey->Hide();
41         
42         cmbKeyMainType->Append(_("URL"));
43         cmbKeyMainType->Append(_("Key"));
44         
45         cmbType->Append("");
46         cmbType->Append(_("Home"));
47         cmbType->Append(_("Work"));
48         
49         cmbURL->Append(_("PGP Key"));
50         
51         cmbKeyType->Append(_("PGP Key"));
52         
53         cmbType->SetSelection(0);
55 }
57 void frmContactEditorKey::SetEditorMode(bool EditMode)
58 {
59         
60         // Set the editor mode.
61         
62         // Set if the editor is adding or editing an address.
63         // FALSE = Add
64         // TRUE = Edit
65         
66         if (EditMode == FALSE){
67         
68                 EditorMode = FALSE;
69                 btnAction->SetLabel(_("Add"));
70                 this->SetTitle(_("Add Key"));
71         
72         } else if (EditMode == TRUE){
73         
74                 EditorMode = TRUE;
75                 btnAction->SetLabel(_("Modify"));
76                 this->SetTitle(_("Modify Key"));
77                 
78                 std::map<int,bool>::iterator booliter;
79                 std::map<int,int>::iterator intiter;
80                 std::map<int,wxString>::iterator striter;               
81                 wxString strValue;
82                 bool boolKeyData = FALSE;       
83                 
84                 // Get the key data type.
85                 
86                 booliter = KeyListKeyTypePtr->find(KeyListIndex);
87                 
88                 if (booliter->first == KeyListIndex){
89                 
90                         boolKeyData = booliter->second;
91                 
92                 }
93                 
94                 // Get the data depending on key data type.             
95                 
96                 if (boolKeyData == FALSE){
97                 
98                         // Setup the URL.
99                 
100                         cmbKeyMainType->SetSelection(0);
101                         
102                         striter = KeyListPtr->find(KeyListIndex);
103                         
104                         if (striter->first == KeyListIndex){
105                         
106                                 strValue = striter->second;
107                         
108                         }
109                         
110                         txtAddress->ChangeValue(strValue);
111                         strValue.Clear();
112                         
113                         striter = KeyListDataTypePtr->find(KeyListIndex);
114                         
115                         if (striter->first == KeyListIndex &&
116                                 striter != KeyListDataTypePtr->end()){
117                         
118                                 strValue = striter->second;
119                         
120                         }                       
121                         
122                         cmbURL->SetValue(strValue);
123                 
124                 } else if (boolKeyData == TRUE){
125                 
126                         // Setup the key.
127                 
128                         cmbKeyMainType->SetSelection(1);
129                         std::string base64dec;
130                         
131                         // Decode the base64 data.
132                         
133                         striter = KeyListPtr->find(KeyListIndex);
134                         
135                         if (striter->first == KeyListIndex){
136                         
137                                 strValue = striter->second;
138                         
139                         }
140                         
141                         std::string base64enc = std::string(strValue.mb_str());
142                         base64dec = base64_decode(base64enc);
143                         
144                         strValue.Clear();
145                         wxString strValue(base64dec.c_str(), wxConvUTF8);
146                         
147                         txtKeyData->SetValue(strValue);         
148                         
149                         // Get the key data type.
150                         
151                         strValue.Clear();
152                         
153                         striter = KeyListDataTypePtr->find(KeyListIndex);
154                         
155                         if (striter->first == KeyListIndex &&
156                                 striter != KeyListDataTypePtr->end()){
157                         
158                                 strValue = striter->second;
159                         
160                         }                       
161                         
162                         cmbKeyType->SetValue(strValue);                         
163                 
164                 }
165                 
166                 ProcessKeyType();
167                 
168                 // Get the type.
169                 
170                 striter = KeyListTypePtr->find(KeyListIndex);   
171                 
172                 if (striter->first == KeyListIndex &&
173                         striter != KeyListTypePtr->end()){
174                 
175                         strValue = striter->second;
176                 
177                 }               
178                 
179                 if (strValue == wxT("home")){
180                 
181                         cmbType->SetSelection(0);
182                 
183                 } else if (strValue == wxT("work")){
184                 
185                         cmbType->SetSelection(1);       
186                 
187                 } else {
188                 
189                         //cmbType->SetSelection(-1);
190                 
191                 }
192                 
193                 // Get the key priority.
194                 
195                 intiter = KeyListPrefPtr->find(KeyListIndex);
196                 
197                 if (intiter->first == KeyListIndex && intiter->second > 0 &&
198                         intiter != KeyListPrefPtr->end()){
199                 
200                         priorityCtrl->SetValue(intiter->second);
201                         priorityCtrl->EnablePriority(true);
202                 
203                 }
204                 
205         }
206                 
209 void frmContactEditorKey::ProcessAction( wxCommandEvent& event )
211         
212         // Process action.
213         
214         long ListCtrlIndex;
215         int intKeyType = 0;
216         
217         intKeyType = cmbKeyMainType->GetCurrentSelection();
218         
219         if (intKeyType == -1){
220         
221                 // No option selected so do nothing.
222         
223                 return;
224         
225         }
226         
227         if (EditorMode == FALSE){
229                 wxString strValue;
230                 wxListItem coldata;             
231                 
232                 coldata.SetId(KeyListIndex);
233                 coldata.SetData(KeyListIndex);
234                 ListCtrlIndex = KeyListCtrlPtr->InsertItem(coldata);            
235                 
236                 // Get Key data.
237                 
238                 if (intKeyType == 0){
239                 
240                         // URL is text.
241                                         
242                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
244                         coldata.SetText(txtAddress->GetValue());
245                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, strValue);
246                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
247                 
248                 } else if (intKeyType == 1){
249                 
250                         // Encode the data in the text to base64.
251                         
252                         wxString wxSb64orig = txtKeyData->GetValue();
253                         std::string base64new = std::string(wxSb64orig.mb_str());
254                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
255                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
256                         
257                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
258                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
259                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 0, _("PGP Key"));
260                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
261                 
262                 }
263                 
264                 // Get Key type.
265                 
266                 strValue.Clear();
267                 strValue = cmbType->GetString(cmbType->GetSelection());
269                 if (strValue == _("Home")) {
270                 
271                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
272                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);
273                 
274                 } else if (strValue == _("Work")) {
275                 
276                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
277                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 1, strValue);                    
278                 
279                 } else {
280                 
281                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
282                 
283                 }
284                 
285                 // Get Key priority.
286                 
287                 if (priorityCtrl->IsPriorityChecked()){
288                 
289                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
290                         KeyListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));                       
291                 
292                 } else {
293                 
294                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
295                 
296                 }
297                 
298                 this->Close();
299                 
300         } else if (EditorMode == TRUE){
302                 long longSelected = -1; 
303                 wxString strValue;              
304                 
305                 longSelected = KeyListCtrlPtr->GetNextItem(longSelected, 
306                         wxLIST_NEXT_ALL,
307                         wxLIST_STATE_SELECTED);
308                         
309                 if (longSelected == -1){
310                         return;
311                 }
312                 
313                 // Update Key data.             
314                 
315                 if (intKeyType == 0){
316                 
317                         // URL is text.
318                                         
319                         KeyListPtr->erase(KeyListIndex);
320                         KeyListPtr->insert(std::make_pair(KeyListIndex, txtAddress->GetValue()));
322                         KeyListCtrlPtr->SetItem(longSelected, 0, strValue);
323                         KeyListKeyTypePtr->erase(KeyListIndex);                 
324                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, FALSE));
325                 
326                 } else if (intKeyType == 1){
327                 
328                         // Encode the data in the text to base64.
329                         
330                         wxString wxSb64orig = txtKeyData->GetValue();
331                         std::string base64new = std::string(wxSb64orig.mb_str());
332                         std::string base64enc = base64_encode(reinterpret_cast<const unsigned char*>(base64new.c_str()), (int)base64new.length());
333                         wxString wxSb64key(base64enc.c_str(), wxConvUTF8);
334                         
335                         KeyListPtr->erase(KeyListIndex);                        
336                         KeyListPtr->insert(std::make_pair(KeyListIndex, wxSb64key));
337                         
338                         KeyListDataTypePtr->erase(KeyListIndex);
339                         KeyListDataTypePtr->insert(std::make_pair(KeyListIndex, cmbKeyType->GetValue()));
340                         
341                         KeyListCtrlPtr->SetItem(longSelected, 0, _("PGP Key"));
342                         
343                         KeyListKeyTypePtr->erase(KeyListIndex);
344                         KeyListKeyTypePtr->insert(std::make_pair(KeyListIndex, TRUE));                  
345                 
346                 }
347                 
348                 // Update Key type.
349                 
350                 strValue.Clear();
351                 strValue = cmbType->GetString(cmbType->GetSelection());
353                 KeyListTypePtr->erase(KeyListIndex);
355                 if (strValue == _("Home")) {
356                 
357                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("home")));
358                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);
359                 
360                 } else if (strValue == _("Work")) {
361                 
362                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("work")));
363                         KeyListCtrlPtr->SetItem(longSelected, 1, strValue);                     
364                 
365                 } else {
366                 
367                         KeyListTypePtr->insert(std::make_pair(KeyListIndex, wxT("")));                  
368                 
369                 }
370                 
371                 // Update the key priority.
372                 
373                 KeyListPrefPtr->erase(KeyListIndex);            
374                 
375                 if (priorityCtrl->IsPriorityChecked()){
376                 
377                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, priorityCtrl->GetValue()));
378                         KeyListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
379                 
380                 } else {
381                 
382                         KeyListPrefPtr->insert(std::make_pair(KeyListIndex, 0));
383                 
384                 }
385                 
386                 this->Close();  
387         
388         }
391 void frmContactEditorKey::ProcessKeyType( wxCommandEvent& event )
394         // Process the key type (via wxCommandEvent).
396         ProcessKeyType();
397         
400 void frmContactEditorKey::ProcessKeyType()
403         // Process the key type.
404         
405         int intKeyType = 0;
406         
407         intKeyType = cmbKeyMainType->GetCurrentSelection();
408         
409         if (intKeyType == 0){
410         
411                 tabURL->Show();
412                 tabKey->Hide();
413         
414         } else if (intKeyType == 1){
415         
416                 tabURL->Hide();
417                 tabKey->Show();
418         
419         }
423 void frmContactEditorKey::ConvertURLType( wxCommandEvent& event )
426         // Convert the URL type.
427         
428         wxString strValue;
430         strValue = cmbURL->GetString(cmbURL->GetCurrentSelection());
431         
432         if (strValue == _("PGP Key")){
433         
434                 cmbURL->SetValue(wxT("application/pgp-keys"));
435         
436         }
440 void frmContactEditorKey::ConvertKeyType( wxCommandEvent& event )
443         // Convert the key type.
444         
445         wxString strValue;
447         strValue = cmbKeyType->GetString(cmbKeyType->GetCurrentSelection());
448         
449         if (strValue == _("PGP Key")){
450         
451                 cmbKeyType->SetValue(wxT("application/pgp-keys"));
452         
453         }
457 void frmContactEditorKey::CloseWindow( wxCommandEvent& event )
459         
460         // Close this window.
461         
462         this->Close();
463         
466 void frmContactEditorKey::SetupPointers(std::map<int, wxString> *KeyList,
467         std::map<int, wxString> *KeyListAltID,
468         std::map<int, wxString> *KeyListPID,
469         std::map<int, bool> *KeyListKeyType,    
470         std::map<int, wxString> *KeyListDataType,       
471         std::map<int, wxString> *KeyListType,
472         std::map<int, wxString> *KeyListTokens,
473         std::map<int, int> *KeyListPref,
474         wxListCtrl *KeyListCtrl,
475         int KeyIndex )
478         // Setup the pointers.
479         
480         KeyListPtr = KeyList;
481         KeyListAltIDPtr = KeyListAltID;
482         KeyListPIDPtr = KeyListPID;
483         KeyListKeyTypePtr = KeyListKeyType;     
484         KeyListDataTypePtr = KeyListDataType;
485         KeyListTypePtr = KeyListType;
486         KeyListTokensPtr = KeyListTokens;
487         KeyListPrefPtr = KeyListPref;
488         KeyListCtrlPtr = KeyListCtrl;
489         KeyListIndex = KeyIndex;
490         
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