Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Removed commented code on Import/Export subroutines that's no longer required.
[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);
70         }
71         
72         // vCard 3.0 export
73         
74         if (ExportType == 1){
75         
76                 ContactData = ImportVCard3(&SelectedFileList);
78         }
80         // Check how many contacts are in the set. If zero, throw an error
81         // message and exit.
82         
83         if (ContactData.size() == 0){
84         
85                 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
86         
87                 return;
88         
89         }
91         // Show a form to let the user select the
92         // contacts needed to import and which account
93         // they go to.
95         frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
96         
97         frmIC->SetupList(&ContactData);
98         frmIC->ShowModal();
99         frmIC->GetResults(&ContactData);
100         AccountName = frmIC->GetAccount();
101         AccountType = frmIC->GetAccountType();
103         if (frmIC->GetDialogResult() == FALSE){
104                 
105                 // User decided not to import. Clean up the
106                 // dialog.
107                 
108                 delete frmIC;
109                 frmIC = NULL;
110                 frmMainPtrInc->ResumeAllTimers();
111                 return;
112                 
113         }
115         delete frmIC;
116         frmIC = NULL;
118         // Import the contacts into the selected account.
120         int ImportCount = 0;
121         int ImportSeek = 0;
122         
123         // Setup the directory for writing into.
124         
125         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
126         
127         wxString GeneratedFilename;
128         wxString OutputFilename;
129         wxString UIDValueFinal;
130         wxString UIDToken;
131         bool UIDFilterOK = FALSE;
132         int FilterCount = 0;
133         
134         GeneratedFilename.Clear();
135         OutputFilename.Clear();
137         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
138         wxString BadCharListFinal;
139         BadCharListFinal.Append(wxT("["));
140         BadCharListFinal.Append(BadCharList);
141         BadCharListFinal.Append(wxT("]"));
142         
143         wxRegEx BadCharRex;
144         BadCharRex.Compile(BadCharListFinal);
145         
146         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
147         iter != ContactData.end(); iter++){
148         
149                 if (iter->second.ContactSelected == TRUE){
150                 
151                         // Workout the file name.               void CloseWindow( wxCommandEvent& event );
152                         // Get the UID value and check if it is empty.
153                         // If it isn't empty, check it can be used for a valid
154                         // filename other wise generate a file name for the purposes
155                         // of storage.
156                         
157                         // Filter UID incase writing outside the
158                         // directory is attempted.
159                         
160 #if defined(__HAIKU__)
161     
164 #elif defined(__WIN32__)
166                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
167                         while (UIDValueCheck.HasMoreTokens()){
168                                 UIDValueCheck.GetNextToken();
169                                 FilterCount++;
170                         }
172 #else
174                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
175                         while (UIDValueCheck.HasMoreTokens()){
176                                 UIDValueCheck.GetNextToken();
177                                 FilterCount++;
178                         }
179         
180 #endif
181                         
182                         if (FilterCount == 1){
183                                 UIDFilterOK = TRUE;
184                         }
185                         
186                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
187                         
188                                 bool NewFileExists = TRUE;
189                                 
190                                 while (NewFileExists){
191                                 
192                                         // Generate the UID and check if it exists.
193                                         
194                                         wxString CheckFilename;
195                                         
196                                         UIDToken = GenerateUUID();
197                                         
198                                         CheckFilename.Append(AccountDirFinal);
199                                         CheckFilename.Append(UIDToken);
200                                         CheckFilename.Append(wxT(".vcf"));
201                                 
202                                         if (!wxFileExists(CheckFilename)){
203                                         
204                                                 NewFileExists = FALSE;
205                                         
206                                         }
207                                 
208                                 }
209                         
210                         } else {
211                         
212                                 UIDToken = iter->second.UIDValue;
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 AccNamePostEvent;
293         
294                 AccNamePostEvent.Clear();
295                 AccNamePostEvent.Append(AccountName);
296                 AccNamePostEvent.Trim();
297         
298                 wxCommandEvent accevent(SYNCACCOUNT);
299                 accevent.SetString(AccNamePostEvent);
300         
301                 wxPostEvent(frmMainPtrInc, accevent);
302         
303         }
304         
305         // Resume the timers.
306         
307         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