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