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         delete frmIC;
108         frmIC = NULL;
110         // Import the contacts into the selected account.
112         int ImportCount = 0;
113         int ImportSeek = 0;
114         
115         // Setup the directory for writing into.
116         
117         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
118         
119         wxString GeneratedFilename;
120         wxString OutputFilename;
121         wxString UIDValueFinal;
122         wxString UIDToken;
123         bool UIDFilterOK = FALSE;
124         int FilterCount = 0;
125         
126         GeneratedFilename.Clear();
127         OutputFilename.Clear();
129         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
130         wxString BadCharListFinal;
131         BadCharListFinal.Append(wxT("["));
132         BadCharListFinal.Append(BadCharList);
133         BadCharListFinal.Append(wxT("]"));
134         
135         wxRegEx BadCharRex;
136         BadCharRex.Compile(BadCharListFinal);
137         
138         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
139         iter != ContactData.end(); iter++){
140         
141                 if (iter->second.ContactSelected == TRUE){
142                 
143                         // Workout the file name.               void CloseWindow( wxCommandEvent& event );
144                         // Get the UID value and check if it is empty.
145                         // If it isn't empty, check it can be used for a valid
146                         // filename other wise generate a file name for the purposes
147                         // of storage.
148                         
149                         // Filter UID incase writing outside the
150                         // directory is attempted.
151                         
152 #if defined(__HAIKU__)
153     
154     //preffilename = wxT("noo");
156 #elif defined(__WIN32__)
158                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
159                         while (UIDValueCheck.HasMoreTokens()){
160                                 UIDValueCheck.GetNextToken();
161                                 FilterCount++;
162                         }
164 #else
166                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
167                         while (UIDValueCheck.HasMoreTokens()){
168                                 UIDValueCheck.GetNextToken();
169                                 FilterCount++;
170                         }
171         
172 #endif
173                         
174                         if (FilterCount == 1){
175                                 UIDFilterOK = TRUE;
176                         }
177                         
178                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
179                         
180                                 bool NewFileExists = TRUE;
181                                 
182                                 while (NewFileExists){
183                                 
184                                         // Generate the UID and check if it exists.
185                                         
186                                         wxString CheckFilename;
187                                         
188                                         UIDToken = GenerateUUID();
189                                         
190                                         CheckFilename.Append(AccountDirFinal);
191                                         CheckFilename.Append(UIDToken);
192                                         CheckFilename.Append(wxT(".vcf"));
193                                 
194                                         if (!wxFileExists(CheckFilename)){
195                                         
196                                                 NewFileExists = FALSE;
197                                         
198                                         }
199                                 
200                                 }
201                         
202                         } else {
203                         
204                                 UIDToken = iter->second.UIDValue;
205                         
206                         }
207                         
208                         //GenerateUUID();
209                         
210                         /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){
211                         
212                         
213                         
214                         }*/
215                 
216                         // Check if the file exists in the directory already.
217                 
218                         OutputFilename.Append(AccountDirFinal);
219                         OutputFilename.Append(wxT("/"));
220                         OutputFilename.Append(UIDToken);
221                         OutputFilename.Append(wxT(".vcf"));
222                 
223                         if(wxFileExists(OutputFilename)){
224                         
225                                 // File already exists so mark as an error.
226                                 
227                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
228                                 ImportErrorCount++;
229                                 ImportSeek++;
230                                 GeneratedFilename.Clear();
231                                 OutputFilename.Clear();
232                                 UIDValueFinal.Clear();
233                                 UIDFilterOK = FALSE;
234                                 FilterCount = 0;
235                                 continue;
236                         
237                         } else {
238                         
239                                 // Write to the file.
240                                 
241                                 wxFFile OutputFile;
242                         
243 #if wxABI_VERSION < 20900
244                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
245 #else
246                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
247 #endif
248                                         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."))));
249                         
250                                 }
251                         
252                                 OutputFile.Write(iter->second.ContactData, wxConvAuto());
253                         
254                                 OutputFile.Close();
255                                 
256                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
257                         
258                         }
259                         
260                         // Check if there is a UID value set. If there is not a UID
261                         // value set then generate a new one.
262                 
263                         ImportCount++;
264                 
265                         ImportSeek++;
266                 
267                         GeneratedFilename.Clear();
268                         OutputFilename.Clear();
269                         UIDValueFinal.Clear();
270                         UIDToken.Clear();
271                         UIDFilterOK = FALSE;
272                         FilterCount = 0;
273                 
274                 }
275         
276         }
278         // Check if the import error count is more than 0. If it is, then
279         // display the results dialog.
280         
281         wxCommandEvent imprres(IMPORT_RESULTSSHOW);
282         imprres.SetClientData(ResultData);
283         imprres.SetInt(ImportCount);
284         imprres.SetExtraLong(ImportErrorCount);
285         wxPostEvent(frmMainPtrInc, imprres);
286                 
287         // Syncronise the account which the contacts got imported to.
288         // (If the account is not a local account).
289         
290         if (AccountType != wxT("Local") && AccountType != wxT("local")){
291         
292                 wxString *AccNamePostEventPtr = new wxString;
293         
294                 wxCommandEvent accevent(SYNCACCOUNT);
295                 accevent.SetClientData(AccNamePostEventPtr);
296         
297                 wxPostEvent(frmMainPtrInc, accevent);
298         
299         }
300         
301         //wxPostEvent
302         
303         // Resume the timers.
304         
305         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