Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor: Stop XAB crashing when pressing Modify/Delete
[xestiaab/.git] / source / contacteditor / frmContactEditorAddress.cpp
1 // frmContactEditorAddress.cpp - frmContactEditorAddress 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 "frmContactEditorAddress.h"
20 #include <map>
21 #include <wx/tokenzr.h>
23 #include "../common/textprocessing.h"
25 frmContactEditorAddress::frmContactEditorAddress( wxWindow* parent )
26 :
27 frmContactEditorAddressADT( parent )
28 {
29         
30         // Setup the window.
31         
32         EditorMode = FALSE;
33         sliPriority->Disable();
34         
35 }
37 void frmContactEditorAddress::InitAddressEditor( wxInitDialogEvent& event )
38 {
39         // Do nothing with this subroutine at this time.
40 }
42 void frmContactEditorAddress::EnablePriority( wxCommandEvent& event )
43 {
44         
45         // Enable/disable the priority setting.
46         
47         if (chkUsePref->IsChecked()){
48                 sliPriority->Enable();
49         } else {
50                 sliPriority->Disable();
51         }
52         
53 }
55 void frmContactEditorAddress::ProcessData( wxCommandEvent& event )
56 {
58         // Process data.
59         
60         // Check what edit mode we are in and go from there.
61         
62         long ListCtrlIndex;
63         
64         if (EditorMode == FALSE){
65         
66                 // Add the address to the list (maps).
67                 
68                 AddressListPtr->erase(AddressListIndex);                
69                 AddressListPtr->insert(std::make_pair(AddressListIndex, txtAddress->GetValue()));
71                 AddressListTownPtr->erase(AddressListIndex);
72                 AddressListTownPtr->insert(std::make_pair(AddressListIndex, txtTownCity->GetValue()));
74                 AddressListCountyPtr->erase(AddressListIndex);
75                 AddressListCountyPtr->insert(std::make_pair(AddressListIndex, txtCounty->GetValue()));
77                 AddressListPostCodePtr->erase(AddressListIndex);
78                 AddressListPostCodePtr->insert(std::make_pair(AddressListIndex, txtPostCode->GetValue()));
80                 AddressListCountryPtr->erase(AddressListIndex);
81                 AddressListCountryPtr->insert(std::make_pair(AddressListIndex, txtCountry->GetValue()));
82                 
83                 // Add the label to the list (maps).
84                 
85                 wxString LabelData = txtLabel->GetValue();
86                 LabelData.Prepend("\"");
87                 LabelData.Append("\"");
88                 AddressListLabelPtr->insert(std::make_pair(AddressListIndex, LabelData));
89                 
90                 // Add the language to the list (maps).
91                 
92                 AddressListLangPtr->insert(std::make_pair(AddressListIndex, cmbLanguage->GetValue()));
93                 
94                 // No AltIDs at present.
95                 
96                 // No PIDs at present.
97                 
98                 // There are no tokens for a new address to add so this can be
99                 // ignored for the time being.
100                 
101                 // Add the Geopositioning to the list (maps).
102                 
103                 AddressListGeoPtr->insert(std::make_pair(AddressListIndex, txtGeo->GetValue()));
104                 
105                 // Add the Timezone to the list (maps).
107                 AddressListTimezonePtr->insert(std::make_pair(AddressListIndex, txtTimezone->GetValue()));
108                 
109                 if (EditSectionType == CE_GENERAL){
110                 
111                         AddressListTypePtr->insert(std::make_pair(AddressListIndex, wxT("")));
112                 
113                 } else if (EditSectionType == CE_HOME) {
114                 
115                         AddressListTypePtr->insert(std::make_pair(AddressListIndex, wxT("home")));
116                 
117                 } else if (EditSectionType == CE_WORK) {
118                 
119                         AddressListTypePtr->insert(std::make_pair(AddressListIndex, wxT("work")));
120                 
121                 }
123                 // Add the mediatype to the list (maps).
124                 
125                 AddressListMediatypePtr->insert(std::make_pair(AddressListIndex, wxT("text/plain")));
126                 
127                 if (chkUsePref->IsChecked()){
128                 
129                         AddressListPrefPtr->insert(std::make_pair(AddressListIndex, sliPriority->GetValue()));
130                 
131                 } else {
132                 
133                         AddressListPrefPtr->insert(std::make_pair(AddressListIndex, 0));                
134                 
135                 }
136                 
137                 // Add to the form.
138                 
139                 wxListItem coldata;
140                 
141                 coldata.SetId(AddressListIndex);
142                 coldata.SetData(AddressListIndex);              
143                 coldata.SetText(txtAddress->GetValue());
144                 ListCtrlIndex = AddressListCtrlPtr->InsertItem(coldata);
145                 
146                 AddressListCtrlPtr->SetItem(ListCtrlIndex, 1, txtTownCity->GetValue());
147                 AddressListCtrlPtr->SetItem(ListCtrlIndex, 2, txtCounty->GetValue());
148                 AddressListCtrlPtr->SetItem(ListCtrlIndex, 3, txtPostCode->GetValue());
149                 
150                 if (chkUsePref->IsChecked()){
151                 
152                         AddressListCtrlPtr->SetItem(ListCtrlIndex, 4, wxString::Format(wxT("%i"), sliPriority->GetValue()));
153                 
154                 }
155                 
156                 this->Close();
157         
158         } else if (EditorMode == TRUE){
159         
160                 // Edit the address in the list.
161                 
162                 wxString strAddress;
163                 wxString strValue;
164                 long longSelected = -1;
165                 
166                 AddressListPtr->erase(AddressListIndex);                
167                 AddressListPtr->insert(std::make_pair(AddressListIndex, txtAddress->GetValue()));
169                 AddressListTownPtr->erase(AddressListIndex);
170                 AddressListTownPtr->insert(std::make_pair(AddressListIndex, txtTownCity->GetValue()));
172                 AddressListCountyPtr->erase(AddressListIndex);
173                 AddressListCountyPtr->insert(std::make_pair(AddressListIndex, txtCounty->GetValue()));
175                 AddressListPostCodePtr->erase(AddressListIndex);
176                 AddressListPostCodePtr->insert(std::make_pair(AddressListIndex, txtPostCode->GetValue()));
178                 AddressListCountryPtr->erase(AddressListIndex);
179                 AddressListCountryPtr->insert(std::make_pair(AddressListIndex, txtCountry->GetValue()));
180                 
181                 // Add the label to the list (maps).
182                 
183                 AddressListLabelPtr->erase(AddressListIndex);
184                 
185                 wxString LabelData = txtLabel->GetValue();
186                 LabelData.Prepend("\"");
187                 LabelData.Append("\"");
188                 AddressListLabelPtr->insert(std::make_pair(AddressListIndex, LabelData));
189                 
190                 // Add the language to the list (maps).
191                 
192                 AddressListLangPtr->erase(AddressListIndex);
193                 AddressListLangPtr->insert(std::make_pair(AddressListIndex, cmbLanguage->GetValue()));
194                 
195                 //AddressListAltIDPtr = &AddressListAltID;
196                 
197                 // No AltIDs at present.
198                 
199                 // No PIDs at present.
200                 
201                 // There are no tokens for a new address to add so this can be
202                 // ignored for the time being.
203                 
204                 // Add the Geopositioning to the list (maps).
205                 
206                 AddressListGeoPtr->erase(AddressListIndex);
207                 AddressListGeoPtr->insert(std::make_pair(AddressListIndex, txtGeo->GetValue()));
208                 
209                 // Add the Timezone to the list (maps).
210                 
211                 AddressListTimezonePtr->erase(AddressListIndex);
212                 AddressListTimezonePtr->insert(std::make_pair(AddressListIndex, txtTimezone->GetValue()));
213                 
214                 // Add the mediatype to the list (maps).
215                 
216                 AddressListMediatypePtr->erase(AddressListIndex);
217                 AddressListMediatypePtr->insert(std::make_pair(AddressListIndex, wxT("text/plain")));
218                 
219                 AddressListPrefPtr->erase(AddressListIndex);
220                 
221                 if (chkUsePref->IsChecked()){
222                 
223                         AddressListPrefPtr->insert(std::make_pair(AddressListIndex, sliPriority->GetValue()));
224                 
225                 } else {
226                 
227                         AddressListPrefPtr->insert(std::make_pair(AddressListIndex, 0));                
228                 
229                 }
230                 
231                 // Update the form with the new details.
232                 
233                 longSelected = AddressListCtrlPtr->GetNextItem(longSelected, 
234                         wxLIST_NEXT_ALL,
235                         wxLIST_STATE_SELECTED);
236     
237                 if (longSelected == -1){
238                         return;
239                 }
241                 AddressListCtrlPtr->SetItem(longSelected, 0, txtAddress->GetValue());           
242                 AddressListCtrlPtr->SetItem(longSelected, 1, txtTownCity->GetValue());
243                 AddressListCtrlPtr->SetItem(longSelected, 2, txtCounty->GetValue());
244                 AddressListCtrlPtr->SetItem(longSelected, 3, txtPostCode->GetValue());
245                 
246                 if (chkUsePref->IsChecked()){
247                 
248                         AddressListCtrlPtr->SetItem(longSelected, 4, wxString::Format(wxT("%i"), sliPriority->GetValue()));
249                 
250                 } else {
251                 
252                         AddressListCtrlPtr->SetItem(longSelected, 4, wxT(""));
253                 
254                 }
255                 
256                 this->Close();
257         
258         }
262 void frmContactEditorAddress::CloseWindow( wxCommandEvent& event )
264         
265         // Close the window.
266         
267         this->Close();
268         
271 void frmContactEditorAddress::CopyAddress( wxCommandEvent& event )
274         // Copy the address.
275         
276         wxString MailingLabel;
277         bool AddComma = FALSE;
278         
279         if (!txtAddress->IsEmpty()){
280         
281                 MailingLabel.Append(txtAddress->GetValue());
282                 AddComma = TRUE;
283         
284         }
285         
286         if (!txtTownCity->IsEmpty()){
287         
288                 if(AddComma == TRUE){
289                 
290                         MailingLabel.Append(wxT(",\n"));
291                         AddComma = FALSE;
292                 
293                 }
294                 
295                 MailingLabel.Append(txtTownCity->GetValue());
296                 AddComma = TRUE;
297         
298         }
299         
300         if (!txtCounty->IsEmpty()){
301         
302                 if(AddComma == TRUE){
303                 
304                         MailingLabel.Append(wxT(",\n"));
305                         AddComma = FALSE;
306                 
307                 }
308                 
309                 MailingLabel.Append(txtCounty->GetValue());
310                 AddComma = TRUE;
311         
312         }
313         
314         if (!txtPostCode->IsEmpty()){
315         
316                 if(AddComma == TRUE){
317                 
318                         MailingLabel.Append(wxT(",\n"));
319                         AddComma = FALSE;
320                 
321                 }
322                 
323                 MailingLabel.Append(txtPostCode->GetValue());
324                 AddComma = TRUE;
325         
326         }
327         
328         if (!txtCountry->IsEmpty()){
329         
330                 if(AddComma == TRUE){
331                 
332                         MailingLabel.Append(wxT(",\n"));
333                         AddComma = FALSE;
334                 
335                 }
336                 
337                 MailingLabel.Append(txtCountry->GetValue());
338                 AddComma = TRUE;
339         
340         }       
341         
342         txtLabel->SetValue(MailingLabel);
346 void frmContactEditorAddress::SetEditorMode( bool EditMode, SectionType SectType )
348         
349         // Set the editor mode.
350         
351         // Set if the editor is adding or editing an address.
352         // FALSE = Add
353         // TRUE = Edit
354         
355         if (EditMode == FALSE){
356         
357                 EditorMode = FALSE;
358                 btnAction->SetLabel(_("Add"));
359                 this->SetTitle(_("Add Address"));
360         
361         } else if (EditMode == TRUE){
362         
363                 EditorMode = TRUE;
364                 btnAction->SetLabel(_("Modify"));
365                 this->SetTitle(_("Modify Address"));
366                 
367                 wxString strValue;
368                 
369                 std::map<int,int>::iterator intiter;
370                 std::map<int,wxString>::iterator striter;               
371                 
372                 // Load the data into the form.
373                 
374                 striter = AddressListPtr->find(AddressListIndex);
375                  
376                 if (striter->first == AddressListIndex &&
377                   striter != AddressListPtr->end()){
378                 
379                         txtAddress->SetValue(striter->second);
380                 
381                 }
382                 
383                 striter = AddressListTownPtr->find(AddressListIndex);
385                 if (striter->first == AddressListIndex &&
386                   striter != AddressListTownPtr->end()){
387                 
388                         txtTownCity->SetValue(striter->second);
389                 
390                 }
391                 
392                 striter = AddressListCountyPtr->find(AddressListIndex);
394                 if (striter->first == AddressListIndex &&
395                   striter != AddressListCountyPtr->end()){
396                 
397                         txtCounty->SetValue(striter->second);
398                 
399                 }
401                 striter = AddressListPostCodePtr->find(AddressListIndex);
403                 if (striter->first == AddressListIndex &&
404                   striter != AddressListPostCodePtr->end()){
405                 
406                         txtPostCode->SetValue(striter->second);
407                 
408                 }
410                 striter = AddressListCountryPtr->find(AddressListIndex);
412                 if (striter->first == AddressListIndex &&
413                   striter != AddressListCountryPtr->end()){
414                 
415                         txtCountry->SetValue(striter->second);
416                 
417                 }
418                 
419                 // Setup the Slider.
420                 
421                 intiter = AddressListPrefPtr->find(AddressListIndex);
422                 
423                 if (intiter->first == AddressListIndex && intiter->second > 0 && intiter != AddressListPrefPtr->end() &&
424                   intiter != AddressListPrefPtr->end()){
425                 
426                         sliPriority->SetValue(intiter->second);
427                         sliPriority->Enable();
428                         chkUsePref->SetValue(TRUE);
429                 
430                 }
431                 
432                 // Setup the Label.
433                 
434                 striter = AddressListLabelPtr->find(AddressListIndex);
435                  
436                 if (striter->first == AddressListIndex && striter != AddressListLabelPtr->end() &&
437                   striter != AddressListLabelPtr->end()){
438                 
439                         strValue = striter->second;
440                         if (strValue.Left(1) == "\"" && strValue.Right(1) == "\""){
441                                 strValue.erase(0, 1);
442                                 strValue.RemoveLast();
443                         }
444                 
445                 }
447                 txtLabel->SetValue(strValue);
448                 
449                 // Setup the Language, Geographic position and timezone.
450                 
451                 striter = AddressListLangPtr->find(AddressListIndex);
452                  
453                 if (striter->first == AddressListIndex && striter != AddressListLangPtr->end()){
454                 
455                         strValue = striter->second;
457                 }       
458                 
459                 cmbLanguage->SetValue(strValue);
460                 
461                 striter = AddressListGeoPtr->find(AddressListIndex);
462                  
463                 if (striter->first == AddressListIndex && striter != AddressListGeoPtr->end()){
464                 
465                         strValue = striter->second;
466                 
467                 }
468                 
469                 txtGeo->SetValue(strValue);
470                 
471                 striter = AddressListTimezonePtr->find(AddressListIndex);
472                  
473                 if (striter->first == AddressListIndex && striter != AddressListTimezonePtr->end()){
474                 
475                         strValue = striter->second;
476                 
477                 }
478                 
479                 txtTimezone->SetValue(strValue);        
480         
481         }
482         
483         EditSectionType = SectType;
486 void frmContactEditorAddress::SetupPointers( std::map<int, wxString> *AddressList,
487         std::map<int, wxString> *AddressListTown,
488         std::map<int, wxString> *AddressListCounty,
489         std::map<int, wxString> *AddressListPostCode,
490         std::map<int, wxString> *AddressListCountry,
491         std::map<int, wxString> *AddressListLabel,
492         std::map<int, wxString> *AddressListLang,               
493         std::map<int, wxString> *AddressListAltID,
494         std::map<int, wxString> *AddressListPID,
495         std::map<int, wxString> *AddressListTokens,
496         std::map<int, wxString> *AddressListGeo,
497         std::map<int, wxString> *AddressListTimezone,
498         std::map<int, wxString> *AddressListType,
499         std::map<int, wxString> *AddressListMediatype,
500         std::map<int, int> *AddressListPref,
501         wxListCtrl *AddressListCtrl, 
502         int AddressIndex )
505         // Setup the pointers.
506         
507         AddressListPtr = AddressList;
508         AddressListTownPtr = AddressListTown;
509         AddressListCountyPtr = AddressListCounty;
510         AddressListPostCodePtr = AddressListPostCode;
511         AddressListCountryPtr = AddressListCountry;
512         AddressListLabelPtr = AddressListLabel;
513         AddressListLangPtr = AddressListLang;
514         AddressListAltIDPtr = AddressListAltID;
515         AddressListPIDPtr = AddressListPID;
516         AddressListTokensPtr = AddressListTokens;
517         AddressListGeoPtr = AddressListGeo;
518         AddressListTimezonePtr = AddressListTimezone;
519         AddressListTypePtr = AddressListType;
520         AddressListMediatypePtr = AddressListMediatype;
521         AddressListPrefPtr = AddressListPref;
522         AddressListCtrlPtr = AddressListCtrl;
523         AddressListIndex = AddressIndex;
524         
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