Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Improved importing support.
[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         frmMainPtrInc->PauseAllTimers();
24         wxString FileTypes;
25         
26         long ContactsExported = 0;
27         long ContactsCollected = 0;
28         int ExportType = 0;
29         int ExportCount = 0;
30         wxString FinalFilename;
31         wxArrayString SelectedFileList;
32         wxString SelectedFileDirectory;
33         wxString AccountName;
34         wxString AccountType;
35         int ImportErrorCount = 0;
36         
37         std::map<int,wxString> *ResultData = new std::map<int,wxString>;
38         
39         FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)"));
40         FileTypes.Append(wxT("|*.vcf|"));
41         FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)"));
42         FileTypes.Append(wxT("|*.vcf"));
44         // Open up the dialog to export file data.
45         
46         wxFileDialog ExportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
47                 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
48         
49         if (ExportDlg.ShowModal() == wxID_CANCEL){
50         
51                 frmMainPtrInc->ResumeAllTimers();
52                 return;
53         
54         }
56         // Get the list of filenames.
58         ExportDlg.GetPaths(SelectedFileList);
59         
60         // Find which data type is being used.
61                 
62         ExportType = ExportDlg.GetFilterIndex();
64         std::map<int, ImportDataContact> ContactData;
66         if (ExportType == 0){
67         
68                 ContactData = ImportVCard4(&SelectedFileList);
69         
70                 //ImportVCard4(DirMode, FinalFilename, FileListInc, &ExportCount);
71         
72         }
73         
74         // vCard 3.0 export
75         
76         if (ExportType == 1){
77         
78                 ContactData = ImportVCard3(&SelectedFileList);
79         
80                 //ImportVCard3(DirMode, FinalFilename, FileListInc, &ExportCount);
82         }
84         // Check how many contacts are in the set. If zero, throw an error
85         // message and exit.
86         
87         if (ContactData.size() == 0){
88         
89                 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
90         
91                 return;
92         
93         }
95         // Show a form to let the user select the
96         // contacts needed to import and which account
97         // they go to.
99         frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
100         
101         frmIC->SetupList(&ContactData);
102         frmIC->ShowModal();
103         frmIC->GetResults(&ContactData);
104         AccountName = frmIC->GetAccount();
105         AccountType = frmIC->GetAccountType();
107         if (frmIC->GetDialogResult() == FALSE){
108                 
109                 // User decided not to import. Clean up the
110                 // dialog.
111                 
112                 delete frmIC;
113                 frmIC = NULL;
114                 frmMainPtrInc->ResumeAllTimers();
115                 return;
116                 
117         }
119         delete frmIC;
120         frmIC = NULL;
122         // Import the contacts into the selected account.
124         int ImportCount = 0;
125         int ImportSeek = 0;
126         
127         // Setup the directory for writing into.
128         
129         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
130         
131         wxString GeneratedFilename;
132         wxString OutputFilename;
133         wxString UIDValueFinal;
134         wxString UIDToken;
135         bool UIDFilterOK = FALSE;
136         int FilterCount = 0;
137         
138         GeneratedFilename.Clear();
139         OutputFilename.Clear();
141         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
142         wxString BadCharListFinal;
143         BadCharListFinal.Append(wxT("["));
144         BadCharListFinal.Append(BadCharList);
145         BadCharListFinal.Append(wxT("]"));
146         
147         wxRegEx BadCharRex;
148         BadCharRex.Compile(BadCharListFinal);
149         
150         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
151         iter != ContactData.end(); iter++){
152         
153                 if (iter->second.ContactSelected == TRUE){
154                 
155                         // Workout the file name.               void CloseWindow( wxCommandEvent& event );
156                         // Get the UID value and check if it is empty.
157                         // If it isn't empty, check it can be used for a valid
158                         // filename other wise generate a file name for the purposes
159                         // of storage.
160                         
161                         // Filter UID incase writing outside the
162                         // directory is attempted.
163                         
164 #if defined(__HAIKU__)
165     
166     //preffilename = wxT("noo");
168 #elif defined(__WIN32__)
170                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
171                         while (UIDValueCheck.HasMoreTokens()){
172                                 UIDValueCheck.GetNextToken();
173                                 FilterCount++;
174                         }
176 #else
178                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
179                         while (UIDValueCheck.HasMoreTokens()){
180                                 UIDValueCheck.GetNextToken();
181                                 FilterCount++;
182                         }
183         
184 #endif
185                         
186                         if (FilterCount == 1){
187                                 UIDFilterOK = TRUE;
188                         }
189                         
190                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
191                         
192                                 bool NewFileExists = TRUE;
193                                 
194                                 while (NewFileExists){
195                                 
196                                         // Generate the UID and check if it exists.
197                                         
198                                         wxString CheckFilename;
199                                         
200                                         UIDToken = GenerateUUID();
201                                         
202                                         CheckFilename.Append(AccountDirFinal);
203                                         CheckFilename.Append(UIDToken);
204                                         CheckFilename.Append(wxT(".vcf"));
205                                 
206                                         if (!wxFileExists(CheckFilename)){
207                                         
208                                                 NewFileExists = FALSE;
209                                         
210                                         }
211                                 
212                                 }
213                         
214                         } else {
215                         
216                                 UIDToken = iter->second.UIDValue;
217                         
218                         }
219                         
220                         //GenerateUUID();
221                         
222                         /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){
223                         
224                         
225                         
226                         }*/
227                 
228                         // Check if the file exists in the directory already.
229                 
230                         OutputFilename.Append(AccountDirFinal);
231                         OutputFilename.Append(wxT("/"));
232                         OutputFilename.Append(UIDToken);
233                         OutputFilename.Append(wxT(".vcf"));
234                 
235                         if(wxFileExists(OutputFilename)){
236                         
237                                 // File already exists so mark as an error.
238                                 
239                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
240                                 ImportErrorCount++;
241                                 ImportSeek++;
242                                 GeneratedFilename.Clear();
243                                 OutputFilename.Clear();
244                                 UIDValueFinal.Clear();
245                                 UIDFilterOK = FALSE;
246                                 FilterCount = 0;
247                                 continue;
248                         
249                         } else {
250                         
251                                 // Write to the file.
252                                 
253                                 wxFFile OutputFile;
254                         
255 #if wxABI_VERSION < 20900
256                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
257 #else
258                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
259 #endif
260                                         ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Error occured whilst trying to create this contact. Please check permissions and storage device."))));
261                         
262                                 }
263                         
264                                 OutputFile.Write(iter->second.ContactData, wxConvAuto());
265                         
266                                 OutputFile.Close();
267                                 
268                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
269                         
270                         }
271                         
272                         // Check if there is a UID value set. If there is not a UID
273                         // value set then generate a new one.
274                 
275                         ImportCount++;
276                 
277                         ImportSeek++;
278                 
279                         GeneratedFilename.Clear();
280                         OutputFilename.Clear();
281                         UIDValueFinal.Clear();
282                         UIDToken.Clear();
283                         UIDFilterOK = FALSE;
284                         FilterCount = 0;
285                 
286                 }
287         
288         }
290         // Check if the import error count is more than 0. If it is, then
291         // display the results dialog.
292         
293         wxCommandEvent imprres(IMPORT_RESULTSSHOW);
294         imprres.SetClientData(ResultData);
295         imprres.SetInt(ImportCount);
296         imprres.SetExtraLong(ImportErrorCount);
297         wxPostEvent(frmMainPtrInc, imprres);
298                 
299         // Syncronise the account which the contacts got imported to.
300         // (If the account is not a local account).
301         
302         if (AccountType != wxT("Local") && AccountType != wxT("local")){
303         
304                 wxString AccNamePostEvent;
305         
306                 AccNamePostEvent.Clear();
307                 AccNamePostEvent.Append(AccountName);
308                 AccNamePostEvent.Trim();
309         
310                 wxCommandEvent accevent(SYNCACCOUNT);
311                 accevent.SetString(AccNamePostEvent);
312         
313                 wxPostEvent(frmMainPtrInc, accevent);
314         
315         }
316         
317         //wxPostEvent
318         
319         // Resume the timers.
320         
321         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