Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor: Anniversary for contact set incorrectly to the Birthday value when...
[xestiaab/.git] / source / contacteditor / frmContactEditor-Save.cpp
1 // frmContactEditor-Save.cpp - frmContactEditor save contact subroutines.
2 //
3 // (c) 2012-2016 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 "frmContactEditor.h"
20 #include "../enums.h"
21 #include "../version.h"
22 #include "../vcard/vcard.h"
23 #include "../vcard/vcard34conv.h"
24 #include "../common/textprocessing.h"
25 #include "../common/preferences.h"
26 #include "../common/uuid.h"
27 #include "../common/dirs.h"
29 void frmContactEditor::SaveContact( wxCommandEvent& event )
30 {
31     
32         // Do not save if the account is an unsupported account.
33         
34         if (boolUnsupportedAccount == true){
35         
36                 wxMessageBox(_("Cannot make changes to a contact from an unsupported account type."), _("Unsupported account"), wxICON_ERROR);
37                 saveSuccess = false;
38                 return;
39                 
40         }
41         
42         // Check if Display As combo box has a value in it.
43         // Do not go any further if there is no value.
44     
45         wxString cmbDisplayAsValue = cmbDisplayAs->GetValue();
46     
47         if (cmbDisplayAsValue.IsEmpty()){
48         
49                 wxMessageBox(_("Display As value cannot be left blank."), _("Display As value empty"), wxICON_ERROR);
50                 saveSuccess = false;
51                 return;
52         
53         }
54     
55         // Check if a value has been entered either in the Title,
56         // Forename, Surname, Other Names and Suffix text boxes.
57         
58         if (txtTitle->IsEmpty() && txtForename->IsEmpty() && txtSurname->IsEmpty() 
59                 && txtOtherNames->IsEmpty() && txtSuffix->IsEmpty())
60         {
62                 wxMessageBox(_("A value must be as minimum eithered in either the Title, Forename, Surname, Other Names or Suffix text boxes."), _("No name information entered"), wxICON_ERROR);
63                 saveSuccess = false;
64                 return;
65                 
66         }
67     
68         // Save the updated contact data.
69     
70         //vCard ContactData;
71         wxString FilenameFinal;
72         bool ReplaceContact = FALSE;
73     
74         if (StartupEditMode == TRUE){
75     
76                 if (cmbType->GetCurrentSelection() == 1 ||
77                     cmbType->GetCurrentSelection() == 3 ||
78                     cmbType->GetCurrentSelection() == 4){
79         
80                         if (IsGroup == TRUE){
81             
82                                 // Mark contact for replacing.
83             
84                                 ReplaceContact = TRUE;
85                                 IsGroup = FALSE;
86             
87                         }
88         
89                 } else if (cmbType->GetCurrentSelection() == 2){
90         
91                         if (IsGroup == FALSE){
92             
93                                 // Mark contact for replacing.
94             
95                                 ReplaceContact = TRUE;
96                                 IsGroup = TRUE;
97             
98                         }
99         
100                 }
101     
102                 if (ReplaceContact == TRUE){
103         
104                         wxString wxDelSplitFilename;
105                         wxString wxDelFinalFilename;
106                         wxString wxSDelDataURL;
107                         wxStringTokenizer wSTDelFilename(wxSContactFilename, wxT("/"));
108                         
109                         while(wSTDelFilename.HasMoreTokens()){
110             
111                                 wxDelSplitFilename = wSTDelFilename.GetNextToken();
112             
113                         }
114         
115                         wxSDelDataURL = wxDelSplitFilename;
116         
117                         // Delete the contact from the server as it will be useless in certain
118                         // address book clients.
119         
120                         ActMgrPtr->AddTask(2, cmbDisplayAs->GetValue(), wxSContactAccount, wxSDelDataURL, wxDelSplitFilename, wxSContactFilename, wxT(""));
121         
122                         // Clear the filename so the trigger to recreate the UID is done.
123         
124                         wxSContactFilename.Clear();
125                         EditMode = FALSE;
126         
127                 }
128     
129         }
130     
131         if (wxSContactFilename.IsEmpty()){
132         
133                 // Generate a random UUID.
134         
135                 ContactEditorData.UIDToken = GenerateUUID();
136                 ContactEditorData.UIDToken = ContactEditorData.UIDToken.MakeUpper();
137         
138                 // Setup the filename.
139         
140                 FilenameFinal = GetAccountDir(wxSContactAccount, FALSE);
141         
142 #if defined(__HAIKU__)
143                 
144 #elif defined(__WIN32__)
145         
146                 FilenameFinal = GetAccountDir(wxSContactAccount, FALSE);
147                 FilenameFinal.Append(ContactEditorData.UIDToken);
148                 FilenameFinal.Append(wxT(".vcf"));
149                 wxSContactFilename = FilenameFinal;
150         
151 #else
152         
153                 FilenameFinal = GetAccountDir(wxSContactAccount, FALSE);
154                 FilenameFinal.Append(ContactEditorData.UIDToken);
155                 FilenameFinal.Append(wxT(".vcf"));
156                 wxSContactFilename = FilenameFinal;
157         
158 #endif
159         
160         } else {
161         
162                 if (ContactEditorData.UIDToken.IsEmpty()){
163             
164                         // UID Token is empty. (Shouldn't be).
165                         // Generate a new UID Token.
166             
167                         ContactEditorData.UIDToken = GenerateUUID();
168                         ContactEditorData.UIDToken = ContactEditorData.UIDToken.MakeUpper();
169             
170                 }
171         
172                 FilenameFinal = wxSContactFilename;
173         
174         }
175     
176         // Setup the data and write it into the account folder.
177     
178         // Begin preperations to write the contact to a file.
179     
180         bool FNFirst = TRUE;
181         std::map<int,int>::iterator intiter;
182         std::map<int,wxString>::iterator striter;
183     
184         // Set some values for saving.
185         
186         ContactEditorData.NameForename = txtForename->GetValue();
187         ContactEditorData.NameSurname = txtSurname->GetValue();
188         ContactEditorData.NameOtherNames = txtOtherNames->GetValue();
189         ContactEditorData.NameTitle = txtTitle->GetValue();
190         ContactEditorData.NameSuffix = txtSuffix->GetValue();
191         
192         ContactEditorData.GenderDetails = txtGenderDescription->GetValue();
193         
194         // Process Birthday: Get Day, Month & Year.
195         
196         wxDateTime BirthdayDate;
197         wxDateTime::Month BirthdayMonth;
198         int BirthdayDay;
199         int BirthdayYear;
200         wxString DataBeforeT;
201         wxString DataAfterT;
202         wxString FinalBirthdayString;
203         bool ProcessDataAfterT = FALSE;
204         
205         BirthdayDate = dapBirthday->GetValue();
206         
207         if (!txtBirthday->IsEmpty()){
208         
209                 // Birthday value is text so use this.
210                 
211                 ContactEditorData.Birthday = txtBirthday->GetValue();
212                 
213         } else if (BirthdayDate.IsValid()){
214             
215                 BirthdayDay = BirthdayDate.GetDay();
216                 BirthdayMonth = BirthdayDate.GetMonth();
217                 BirthdayYear = BirthdayDate.GetYear();
218             
219                 // Look for T and replace data before this.
220             
221                 wxStringTokenizer wSTDate(ContactEditorData.Birthday, wxT("T"));
222             
223                 while (wSTDate.HasMoreTokens()){
224                 
225                         if (ProcessDataAfterT == FALSE){
226                     
227                                 DataBeforeT = wSTDate.GetNextToken();
228                                 ProcessDataAfterT = TRUE;
229                     
230                         } else {
231                     
232                                 DataAfterT = wSTDate.GetNextToken();
233                                 break;
234                     
235                         }
236                 
237                 }
238             
239                 // If there is not T then replace altogether.
240             
241                 wxString FinalBirthdayDay;
242                 wxString FinalBirthdayMonth;
243                 wxString FinalBirthdayYear;
244             
245                 if (BirthdayDay < 10){
246                 
247                         FinalBirthdayDay = wxT("0") + wxString::Format(wxT("%i"), BirthdayDay);
248                 
249                 } else {
250                 
251                         FinalBirthdayDay = wxString::Format(wxT("%i"), BirthdayDay);
252                 
253                 }
254             
255                 if (((int)BirthdayMonth + 1) < 10){
256                 
257                         FinalBirthdayMonth = wxT("0") + wxString::Format(wxT("%i"), ((int)BirthdayMonth + 1));
258                 
259                 } else {
260                 
261                         FinalBirthdayMonth = wxString::Format(wxT("%i"), ((int)BirthdayMonth + 1));
262                 
263                 }
264             
265                 if (BirthdayYear == 0){
266                 
267                         FinalBirthdayYear = wxT("--");
268                 
269                 } else {
270                 
271                         FinalBirthdayYear = wxString::Format(wxT("%i"), BirthdayYear);
272                 
273                 }
274             
275                 if (!DataAfterT.IsEmpty()){
276                 
277                         FinalBirthdayString = FinalBirthdayYear + FinalBirthdayMonth + FinalBirthdayDay + wxT("T") + DataAfterT;
278                 
279                 } else {
280                 
281                         FinalBirthdayString = FinalBirthdayYear + FinalBirthdayMonth + FinalBirthdayDay;
282                 
283                 }
284                 
285                 if(!FinalBirthdayString.IsEmpty()){
286                         
287                         ContactEditorData.Birthday = FinalBirthdayString;
288                         
289                 }
290                 
291         }
292         
293         // Process Anniversary: Get Day, Month & Year.
294         
295         wxDateTime AnniversaryDate;
296         wxDateTime::Month AnniversaryMonth;
297         int AnniversaryDay;
298         int AnniversaryYear;
299         DataBeforeT = "";
300         DataAfterT = "";
301         wxString FinalAnniversaryString;
302         ProcessDataAfterT = FALSE;
303         
304         AnniversaryDate = dapAnniversary->GetValue();
305         
306         if (!txtAnniversary->IsEmpty()){
307         
308                 // Birthday value is text so use this.
309                 
310                 ContactEditorData.Anniversary = txtAnniversary->GetValue();
311                 
312         } else if (AnniversaryDate.IsValid()){
313             
314                 AnniversaryDay = AnniversaryDate.GetDay();
315                 AnniversaryMonth = AnniversaryDate.GetMonth();
316                 AnniversaryYear = AnniversaryDate.GetYear();
317             
318                 // Look for T and replace data before this.
319             
320                 wxStringTokenizer wSTDate(ContactEditorData.Anniversary, wxT("T"));
321             
322                 while (wSTDate.HasMoreTokens()){
323                 
324                         if (ProcessDataAfterT == FALSE){
325                     
326                                 DataBeforeT = wSTDate.GetNextToken();
327                                 ProcessDataAfterT = TRUE;
328                     
329                         } else {
330                     
331                                 DataAfterT = wSTDate.GetNextToken();
332                                 break;
333                     
334                         }
335                 
336                 }
337             
338                 // If there is not T then replace altogether.
339             
340                 wxString FinalAnniversaryDay;
341                 wxString FinalAnniversaryMonth;
342                 wxString FinalAnniversaryYear;
343            
344                 if (AnniversaryDay < 10){
345                 
346                         FinalAnniversaryDay = wxT("0") + wxString::Format(wxT("%i"), AnniversaryDay);
347                 
348                 } else {
349                 
350                         FinalAnniversaryDay = wxString::Format(wxT("%i"), AnniversaryDay);
351                 
352                 }
353             
354                 if (((int)AnniversaryMonth + 1) < 10){
355                 
356                         FinalAnniversaryMonth = wxT("0") + wxString::Format(wxT("%i"), ((int)AnniversaryMonth + 1));
357                 
358                 } else {
359                 
360                         FinalAnniversaryMonth = wxString::Format(wxT("%i"), ((int)AnniversaryMonth + 1));
361                 
362                 }
363         
364                 if (AnniversaryYear == 0){
365                 
366                         FinalAnniversaryYear = wxT("--");
367                 
368                 } else {
369                 
370                         FinalAnniversaryYear = wxString::Format(wxT("%i"), AnniversaryYear);
371                 
372                 }
373             
374                 if (!DataAfterT.IsEmpty()){
375                 
376                         FinalAnniversaryString = FinalAnniversaryYear + FinalAnniversaryMonth + FinalAnniversaryDay + wxT("T") + DataAfterT;
377                 
378                 } else {
379                 
380                         FinalAnniversaryString = FinalAnniversaryYear + FinalAnniversaryMonth + FinalAnniversaryDay;
381                 
382                 }
383                 
384                 if(!FinalAnniversaryString.IsEmpty()){
385                         
386                         ContactEditorData.Anniversary = FinalAnniversaryString;
387                         
388                 }
389         
390         }
391         
392         // Process full name.
393         
394         if (ContactEditorData.FullNamesList.size() > 0){
395                 
396                 ContactEditorData.FullNamesList[0] = cmbDisplayAs->GetValue();
397                 
398         } else {
399                 
400                 ContactEditorData.FullNamesList.insert(std::make_pair(0, cmbDisplayAs->GetValue()));
401                 ContactEditorData.FullNamesListAltID.insert(std::make_pair(0, wxT("")));
402                 ContactEditorData.FullNamesListPID.insert(std::make_pair(0, wxT("")));
403                 ContactEditorData.FullNamesListType.insert(std::make_pair(0, wxT("")));
404                 ContactEditorData.FullNamesListLanguage.insert(std::make_pair(0, wxT("")));
405                 ContactEditorData.FullNamesListPref.insert(std::make_pair(0, 0));
406                 ContactEditorData.FullNamesListTokens.insert(std::make_pair(0, wxT("")));
407                 
408         }
409     
410         FMTimer.Stop();
411         ContactEditorData.SaveFile(FilenameFinal);
412     
413         vCard34Conv ConvFileFun;
414     
415         wxString wxSData;
416     
417         ConvFileFun.ConvertToV3(FilenameFinal, &wxSData);
418     
419         wxString AccountDirPrefix;
420         wxString AccountDir;
421         wxString PrefDir;
422     
423 #if defined(__HAIKU__)
425 #elif defined(__APPLE__)
426     
427         PrefDir = GetUserPrefDir();
428     
429         wxStringTokenizer wSTFilename(wxSContactFilename, wxT("/"));
430     
431 #elif defined(__WIN32__)
432     
433         PrefDir = GetUserPrefDir();
434     
435         wxStringTokenizer wSTFilename(wxSContactFilename, wxT("\\"));
436     
437 #else
438     
439         PrefDir = GetUserPrefDir();
440     
441         wxStringTokenizer wSTFilename(wxSContactFilename, wxT("/"));
442     
443 #endif
445         XABPreferences PrefData(PrefDir);
446     
447         wxString AccountType;
448     
449         for (int i = 0; i < PrefData.accounts.GetCount(); i++){
450         
451                 AccountDir = PrefData.accounts.GetAccountDirectory(i) + wxT(".carddav");
452         
453                 if (AccountDir == wxSContactAccount){
454             
455                         AccountDirPrefix = PrefData.accounts.GetAccountDirPrefix(i);
456                         AccountDirPrefix.Trim();
457                         AccountType = PrefData.accounts.GetAccountType(i);
458                         break;
459             
460                 }
461         
462         }
463     
464         wxString wxSplitFilename;
465         wxString wxSDataURL;
466     
467         while(wSTFilename.HasMoreTokens()){
468         
469                 wxSplitFilename = wSTFilename.GetNextToken();
470         
471         }
472     
473         wxSDataURL = wxSplitFilename;
474     
475         // Find out if the filename exists in the table.
476     
477         if (AccountType == wxT("CardDAV") || AccountType == wxT("carddav")){
478         
479                 wxString ETagResult;
480                 wxString ETagOriginal;
481         
482                 ETagDB *ETagDBPtr = NULL;
483         
484                 ETagDBPtr = ETagTmrPtr->GetPointer(wxSContactAccount);
485         
486                 wxString wxSETag = ETagDBPtr->GetETag(wxSplitFilename);
487                 wxString wxSETagOrig = ETagDBPtr->GetETagOriginal(wxSplitFilename);
489                 if (wxSETagOrig.IsEmpty()){
490             
491                         // Generate the ETag.
492             
493                         wxSETagOrig = wxString::Format(wxT("%X%X%X%X"), rand() % 1024, rand() % 1024, rand() % 1024, rand() % 1024);
494             
495                 }
496         
497                 if (wxSETag.IsEmpty()){
498             
499                         // Update empty ETag.
500             
501                         wxSETag = wxSETagOrig;
502                         ETagDBPtr->AddETag(wxSplitFilename, wxSETag, "");
503             
504                 } else {
505             
506                         // Don't change original ETag.
507             
508                         wxSETag = wxString::Format(wxT("%X%X%X%X"), rand() % 1024, rand() % 1024, rand() % 1024, rand() % 1024);
509                         ETagDBPtr->UpdateETag(wxSplitFilename, wxSETag);
510             
511                 }
512         
513                 if (EditMode == FALSE){
515                         ActMgrPtr->AddTask(0, cmbDisplayAs->GetValue(), wxSContactAccount, wxSDataURL, wxSplitFilename, FilenameFinal, wxSData);
516                         EditMode = TRUE;
517                         FMTimer.SetFilename(FilenameFinal);
518                         FMTimer.UpdateTimestamp();
519                         FMTimer.Start(10000, FALSE);
520             
521                         wxCommandEvent reloadevent(RELOADCONTACTLIST);
522                         reloadevent.SetString(wxSContactAccount);
523                         wxPostEvent(this->GetParent(), reloadevent);
524             
525                 } else {
526             
527                         ActMgrPtr->AddTask(1, cmbDisplayAs->GetValue(), wxSContactAccount, wxSDataURL, wxSplitFilename, FilenameFinal, wxSData);
528                         FMTimer.UpdateTimestamp();
529                         FMTimer.Start(10000, FALSE);
530             
531                 }
532         
533         }
534     
535         // Send a notification to update the main window
536         // with the new details.
537     
538         UCNotif *ucd;
539         ucd = new UCNotif;
540     
541         // TODO: Workout nickname settings by priority and
542         // type.
543     
544         vCardName NameData;
545         
546         NameData.Forename = txtForename->GetValue();
547         NameData.Surname = txtSurname->GetValue();
548         NameData.OtherNames = txtOtherNames->GetValue();
549         NameData.Title = txtTitle->GetValue();
550         NameData.Suffix = txtSuffix->GetValue();
551         
552         if (this->GetParent() != nullptr)
553         {
554                 ucd->ContactAccount = wxSContactAccount;
555                 ucd->ContactFilename = FilenameFinal;
556                 ucd->ContactName = cmbDisplayAs->GetValue();
557                 ucd->ContactNameArray = NameData;
558     
559                 for (std::map<int,wxString>::iterator gniter = ContactEditorData.GeneralNicknamesList.begin();
560                         gniter != ContactEditorData.GeneralNicknamesList.end(); gniter++){
561         
562                         ucd->ContactNickname = gniter->second;
563                         break;
564         
565                 }
566         
567                 wxCommandEvent event2(CE_UPDATECONTACTLIST);
568                 event2.SetClientData(ucd);
569                 wxPostEvent(MainPtr, event2);
570         }
571         
572         saveSuccess = true;     
573     
576 void frmContactEditor::SaveCloseContact( wxCommandEvent& event )
578     
579         // Save the updated contact data and close the form.
580     
581         wxCommandEvent NullEvent;
582         this->SaveContact(NullEvent);
583         if (saveSuccess)
584         {
585                 this->Close();
586         }
587     
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