Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Initial import of code already done for Xestia Address Book
[xestiaab/.git] / source / import / import.cpp
1 #include <wx/filedlg.h>
2 #include <wx/filename.h>
3 #include <wx/regex.h>
4 #include <wx/tokenzr.h>
5 #include <wx/ffile.h>
6 #include <map>
8 #include "import-vcard3.h"
9 #include "import-vcard4.h"
10 #include "import-struct.h"
11 #include "../frmMain.h"
12 #include "frmImportContacts.h"
13 #include "frmImportResults.h"
14 #include "../common/uuid.h"
15 #include "../common/dirs.h"
17 void ImportRun(frmMain *frmMainPtrInc){
19         // Bring up a dialog selecting one or multiple 
20         // files for processing based on type.
22         struct ImportResultData{
23                 
24         };
26         frmMainPtrInc->PauseAllTimers();
28         wxString FileTypes;
29         
30         long ContactsExported = 0;
31         long ContactsCollected = 0;
32         int ExportType = 0;
33         int ExportCount = 0;
34         wxString FinalFilename;
35         wxArrayString SelectedFileList;
36         wxString SelectedFileDirectory;
37         wxString AccountName;
38         int ImportErrorCount = 0;
39         
40         std::map<int,wxString> ResultData;
41         
42         FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)"));
43         FileTypes.Append(wxT("|*.vcf|"));
44         FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)"));
45         FileTypes.Append(wxT("|*.vcf"));
47         // Open up the dialog to export file data.
48         
49         wxFileDialog ExportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
50                 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
51         
52         if (ExportDlg.ShowModal() == wxID_CANCEL){
53         
54                 frmMainPtrInc->ResumeAllTimers();
55                 return;
56         
57         }
59         // Get the list of filenames.
61         ExportDlg.GetPaths(SelectedFileList);
62         
63         // Find which data type is being used.
64                 
65         ExportType = ExportDlg.GetFilterIndex();
67         std::map<int, ImportDataContact> ContactData;
69         if (ExportType == 0){
70         
71                 ContactData = ImportVCard4(&SelectedFileList);
72         
73                 //ImportVCard4(DirMode, FinalFilename, FileListInc, &ExportCount);
74         
75         }
76         
77         // vCard 3.0 export
78         
79         if (ExportType == 1){
80         
81                 ContactData = ImportVCard3(&SelectedFileList);
82         
83                 //ImportVCard3(DirMode, FinalFilename, FileListInc, &ExportCount);
85         }
87         // Check how many contacts are in the set. If zero, throw an error
88         // message and exit.
89         
90         if (ContactData.size() == 0){
91         
92                 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
93         
94                 return;
95         
96         }
98         // Show a form to let the user select the
99         // contacts needed to import and which account
100         // they go to.
102         frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
103         
104         frmIC->SetupList(&ContactData);
105         frmIC->ShowModal();
106         frmIC->GetResults(&ContactData);
107         AccountName = frmIC->GetAccount();
109         // Import the contacts into the selected account.
111         int ImportCount = 0;
112         int ImportSeek = 0;
113         
114         // Setup the directory for writing into.
115         
116         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
117         
118         wxString GeneratedFilename;
119         wxString OutputFilename;
120         wxString UIDValueFinal;
121         wxString UIDToken;
122         bool UIDFilterOK = FALSE;
123         int FilterCount = 0;
124         
125         GeneratedFilename.Clear();
126         OutputFilename.Clear();
128         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
129         wxString BadCharListFinal;
130         BadCharListFinal.Append(wxT("["));
131         BadCharListFinal.Append(BadCharList);
132         BadCharListFinal.Append(wxT("]"));
133         
134         wxRegEx BadCharRex;
135         BadCharRex.Compile(BadCharListFinal);
136         
137         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
138         iter != ContactData.end(); iter++){
139         
140                 if (iter->second.ContactSelected == TRUE){
141                 
142                         // Workout the file name.
143                         // Get the UID value and check if it is empty.
144                         // If it isn't empty, check it can be used for a valid
145                         // filename other wise generate a file name for the purposes
146                         // of storage.
147                         
148                         // Filter UID incase writing outside the
149                         // directory is attempted.
150                         
151 #if defined(__HAIKU__)
152     
153     //preffilename = wxT("noo");
155 #elif defined(__WIN32__)
157                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
158                         while (UIDValueCheck.HasMoreTokens()){
159                                 UIDValueCheck.GetNextToken();
160                                 FilterCount++;
161                         }
163 #else
165                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
166                         while (UIDValueCheck.HasMoreTokens()){
167                                 UIDValueCheck.GetNextToken();
168                                 FilterCount++;
169                         }
170         
171 #endif
172                         
173                         if (FilterCount == 1){
174                                 UIDFilterOK = TRUE;
175                         }
176                         
177                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
178                         
179                                 bool NewFileExists = TRUE;
180                                 
181                                 while (NewFileExists){
182                                 
183                                         // Generate the UID and check if it exists.
184                                         
185                                         wxString CheckFilename;
186                                         
187                                         UIDToken = GenerateUUID();
188                                         
189                                         CheckFilename.Append(AccountDirFinal);
190                                         CheckFilename.Append(UIDToken);
191                                         CheckFilename.Append(wxT(".vcf"));
192                                 
193                                         if (!wxFileExists(CheckFilename)){
194                                         
195                                                 NewFileExists = FALSE;
196                                         
197                                         }
198                                 
199                                 }
200                         
201                         } else {
202                         
203                                 UIDToken = iter->second.UIDValue;
204                         
205                         }
206                         
207                         //GenerateUUID();
208                         
209                         /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){
210                         
211                         
212                         
213                         }*/
214                 
215                         // Check if the file exists in the directory already.
216                 
217                         OutputFilename.Append(AccountDirFinal);
218                         OutputFilename.Append(wxT("/"));
219                         OutputFilename.Append(UIDToken);
220                         OutputFilename.Append(wxT(".vcf"));
221                 
222                         if(wxFileExists(OutputFilename)){
223                         
224                                 // File already exists so mark as an error.
225                                 
226                                 ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): "), iter->second.FriendlyName, UIDToken)));
227                                 ImportErrorCount++;
228                                 ImportSeek++;
229                                 GeneratedFilename.Clear();
230                                 OutputFilename.Clear();
231                                 UIDValueFinal.Clear();
232                                 UIDFilterOK = FALSE;
233                                 FilterCount = 0;
234                                 continue;
235                         
236                         } else {
237                         
238                                 // Write to the file.
239                                 
240                                 wxFFile OutputFile;
241                         
242 #if wxABI_VERSION < 20900
243                                 OutputFile.Open(OutputFilename, wxT("w"));
244 #else
245                                 OutputFile.Open(OutputFilename, wxT("w"));
246 #endif
247                         
248                                 OutputFile.Write(iter->second.ContactData, wxConvAuto());
249                         
250                                 OutputFile.Close();
251                                 
252                                 ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."))));
253                         
254                         }
255                         
256                         // Check if there is a UID value set. If there is not a UID
257                         // value set then generate a new one.
258                 
259                         ImportCount++;
260                 
261                         ImportSeek++;
262                 
263                         GeneratedFilename.Clear();
264                         OutputFilename.Clear();
265                         UIDValueFinal.Clear();
266                         UIDToken.Clear();
267                         UIDFilterOK = FALSE;
268                         FilterCount = 0;
269                 
270                 }
271         
272         }
274         // Check if the import error count is more than 0. If it is, then
275         // display the results dialog.
276         
277         frmImportResults *frmIR = new frmImportResults(frmMainPtrInc);
278         frmIR->LoadData(&ResultData);
279         frmIR->ShowModal();
280                 
281         delete frmIR;
282         frmIR = NULL;
284         delete frmIC;
285         frmIC = NULL;
286         
287         // Syncronise the account which the contacts got imported to.
288         
289         wxString *AccNamePostEventPtr = new wxString;
290         
291         wxCommandEvent accevent(SYNCACCOUNT);
292         accevent.SetClientData(AccNamePostEventPtr);
293         
294         wxPostEvent(frmMainPtrInc, accevent);
295         
296         //wxPostEvent
297         
298         // Resume the timers.
299         
300         frmMainPtrInc->ResumeAllTimers();
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