Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Renamed variables starting with Export to Import in import/import.cpp
[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 ContactsImported = 0;
27         long ContactsCollected = 0;
28         int ImportType = 0;
29         wxString FinalFilename;
30         wxArrayString SelectedFileList;
31         wxString SelectedFileDirectory;
32         wxString AccountName;
33         wxString AccountType;
34         int ImportErrorCount = 0;
35         
36         std::map<int,wxString> *ResultData = new std::map<int,wxString>;
37         
38         FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)"));
39         FileTypes.Append(wxT("|*.vcf|"));
40         FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)"));
41         FileTypes.Append(wxT("|*.vcf"));
43         // Open up the dialog to import file data.
44         
45         wxFileDialog ImportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
46                 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
47         
48         if (ImportDlg.ShowModal() == wxID_CANCEL){
49         
50                 frmMainPtrInc->ResumeAllTimers();
51                 return;
52         
53         }
55         // Get the list of filenames.
57         ImportDlg.GetPaths(SelectedFileList);
58         
59         // Find which data type is being used.
60                 
61         ImportType = ImportDlg.GetFilterIndex();
63         std::map<int, ImportDataContact> ContactData;
65         if (ImportType == 0){
66         
67                 ContactData = ImportVCard4(&SelectedFileList);
69         }
70         
71         // vCard 3.0 import
72         
73         if (ImportType == 1){
74         
75                 ContactData = ImportVCard3(&SelectedFileList);
77         }
79         // Check how many contacts are in the set. If zero, throw an error
80         // message and exit.
81         
82         if (ContactData.size() == 0){
83         
84                 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
85         
86                 return;
87         
88         }
90         // Show a form to let the user select the
91         // contacts needed to import and which account
92         // they go to.
94         frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
95         
96         frmIC->SetupList(&ContactData);
97         frmIC->ShowModal();
98         frmIC->GetResults(&ContactData);
99         AccountName = frmIC->GetAccount();
100         AccountType = frmIC->GetAccountType();
102         if (frmIC->GetDialogResult() == FALSE){
103                 
104                 // User decided not to import. Clean up the
105                 // dialog.
106                 
107                 delete frmIC;
108                 frmIC = NULL;
109                 frmMainPtrInc->ResumeAllTimers();
110                 return;
111                 
112         }
114         delete frmIC;
115         frmIC = NULL;
117         // Import       the contacts into the selected account.
119         int ImportCount = 0;
120         int ImportSeek = 0;
121         
122         // Setup the directory for writing into.
123         
124         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
125         
126         wxString GeneratedFilename;
127         wxString OutputFilename;
128         wxString UIDValueFinal;
129         wxString UIDToken;
130         bool UIDFilterOK = FALSE;
131         int FilterCount = 0;
132         
133         GeneratedFilename.Clear();
134         OutputFilename.Clear();
136         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
137         wxString BadCharListFinal;
138         BadCharListFinal.Append(wxT("["));
139         BadCharListFinal.Append(BadCharList);
140         BadCharListFinal.Append(wxT("]"));
141         
142         wxRegEx BadCharRex;
143         BadCharRex.Compile(BadCharListFinal);
144         
145         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
146         iter != ContactData.end(); iter++){
147         
148                 if (iter->second.ContactSelected == TRUE){
149                 
150                         // Workout the file name.               void CloseWindow( wxCommandEvent& event );
151                         // Get the UID value and check if it is empty.
152                         // If it isn't empty, check it can be used for a valid
153                         // filename other wise generate a file name for the purposes
154                         // of storage.
155                         
156                         // Filter UID incase writing outside the
157                         // directory is attempted.
158                         
159 #if defined(__HAIKU__)
160     
163 #elif defined(__WIN32__)
165                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
166                         while (UIDValueCheck.HasMoreTokens()){
167                                 UIDValueCheck.GetNextToken();
168                                 FilterCount++;
169                         }
171 #else
173                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
174                         while (UIDValueCheck.HasMoreTokens()){
175                                 UIDValueCheck.GetNextToken();
176                                 FilterCount++;
177                         }
178         
179 #endif
180                         
181                         if (FilterCount == 1){
182                                 UIDFilterOK = TRUE;
183                         }
184                         
185                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
186                         
187                                 bool NewFileExists = TRUE;
188                                 
189                                 while (NewFileExists){
190                                 
191                                         // Generate the UID and check if it exists.
192                                         
193                                         wxString CheckFilename;
194                                         
195                                         UIDToken = GenerateUUID();
196                                         
197                                         CheckFilename.Append(AccountDirFinal);
198                                         CheckFilename.Append(UIDToken);
199                                         CheckFilename.Append(wxT(".vcf"));
200                                 
201                                         if (!wxFileExists(CheckFilename)){
202                                         
203                                                 NewFileExists = FALSE;
204                                         
205                                         }
206                                 
207                                 }
208                         
209                         } else {
210                         
211                                 UIDToken = iter->second.UIDValue;
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): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
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                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
244 #else
245                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
246 #endif
247                                         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."))));
248                         
249                                 }
250                         
251                                 OutputFile.Write(iter->second.ContactData, wxConvAuto());
252                         
253                                 OutputFile.Close();
254                                 
255                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
256                         
257                         }
258                         
259                         // Check if there is a UID value set. If there is not a UID
260                         // value set then generate a new one.
261                 
262                         ImportCount++;
263                 
264                         ImportSeek++;
265                 
266                         GeneratedFilename.Clear();
267                         OutputFilename.Clear();
268                         UIDValueFinal.Clear();
269                         UIDToken.Clear();
270                         UIDFilterOK = FALSE;
271                         FilterCount = 0;
272                 
273                 }
274         
275         }
277         // Check if the import error count is more than 0. If it is, then
278         // display the results dialog.
279         
280         wxCommandEvent imprres(IMPORT_RESULTSSHOW);
281         imprres.SetClientData(ResultData);
282         imprres.SetInt(ImportCount);
283         imprres.SetExtraLong(ImportErrorCount);
284         wxPostEvent(frmMainPtrInc, imprres);
285                 
286         // Syncronise the account which the contacts got imported to.
287         // (If the account is not a local account).
288         
289         if (AccountType != wxT("Local") && AccountType != wxT("local")){
290         
291                 wxString AccNamePostEvent;
292         
293                 AccNamePostEvent.Clear();
294                 AccNamePostEvent.Append(AccountName);
295                 AccNamePostEvent.Trim();
296         
297                 wxCommandEvent accevent(SYNCACCOUNT);
298                 accevent.SetString(AccNamePostEvent);
299         
300                 wxPostEvent(frmMainPtrInc, accevent);
301         
302         }
303         
304         // Resume the timers.
305         
306         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