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