Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Removed unused variables from import/import.cpp
[xestiaab/.git] / source / import / import.cpp
1 // import.cpp - Import subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include <wx/filedlg.h>
20 #include <wx/filename.h>
21 #include <wx/regex.h>
22 #include <wx/tokenzr.h>
23 #include <wx/ffile.h>
24 #include <map>
26 #include "import-vcard3.h"
27 #include "import-vcard4.h"
28 #include "import-struct.h"
29 #include "../frmMain.h"
30 #include "frmImportContacts.h"
31 #include "frmImportResults.h"
32 #include "../common/uuid.h"
33 #include "../common/dirs.h"
35 void ImportRun(frmMain *frmMainPtrInc){
37         // Bring up a dialog selecting one or multiple 
38         // files for processing based on type.
40         frmMainPtrInc->PauseAllTimers();
42         wxString FileTypes;
43         
44         int ImportType = 0;
45         wxString FinalFilename;
46         wxArrayString SelectedFileList;
47         wxString SelectedFileDirectory;
48         wxString AccountName;
49         wxString AccountType;
50         int ImportErrorCount = 0;
51         
52         std::map<int,wxString> *ResultData = new std::map<int,wxString>;
53         
54         FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)"));
55         FileTypes.Append(wxT("|*.vcf|"));
56         FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)"));
57         FileTypes.Append(wxT("|*.vcf"));
59         // Open up the dialog to import file data.
60         
61         wxFileDialog ImportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
62                 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
63         
64         if (ImportDlg.ShowModal() == wxID_CANCEL){
65         
66                 frmMainPtrInc->ResumeAllTimers();
67                 return;
68         
69         }
71         // Get the list of filenames.
73         ImportDlg.GetPaths(SelectedFileList);
74         
75         // Find which data type is being used.
76                 
77         ImportType = ImportDlg.GetFilterIndex();
79         std::map<int, ImportDataContact> ContactData;
81         if (ImportType == 0){
82         
83                 ContactData = ImportVCard4(&SelectedFileList);
85         }
86         
87         // vCard 3.0 import
88         
89         if (ImportType == 1){
90         
91                 ContactData = ImportVCard3(&SelectedFileList);
93         }
95         // Check how many contacts are in the set. If zero, throw an error
96         // message and exit.
97         
98         if (ContactData.size() == 0){
99         
100                 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
101         
102                 return;
103         
104         }
106         // Show a form to let the user select the
107         // contacts needed to import and which account
108         // they go to.
110         frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
111         
112         frmIC->SetupList(&ContactData);
113         frmIC->ShowModal();
114         frmIC->GetResults(&ContactData);
115         AccountName = frmIC->GetAccount();
116         AccountType = frmIC->GetAccountType();
118         if (frmIC->GetDialogResult() == FALSE){
119                 
120                 // User decided not to import. Clean up the
121                 // dialog.
122                 
123                 delete frmIC;
124                 frmIC = NULL;
125                 frmMainPtrInc->ResumeAllTimers();
126                 return;
127                 
128         }
130         delete frmIC;
131         frmIC = NULL;
133         // Import the contacts into the selected account.
135         int ImportCount = 0;
136         int ImportSeek = 0;
137         
138         // Setup the directory for writing into.
139         
140         wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
141         
142         wxString GeneratedFilename;
143         wxString OutputFilename;
144         wxString UIDValueFinal;
145         wxString UIDToken;
146         bool UIDFilterOK = FALSE;
147         int FilterCount = 0;
148         
149         GeneratedFilename.Clear();
150         OutputFilename.Clear();
152         static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
153         wxString BadCharListFinal;
154         BadCharListFinal.Append(wxT("["));
155         BadCharListFinal.Append(BadCharList);
156         BadCharListFinal.Append(wxT("]"));
157         
158         wxRegEx BadCharRex;
159         BadCharRex.Compile(BadCharListFinal);
160         
161         for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
162         iter != ContactData.end(); iter++){
163         
164                 if (iter->second.ContactSelected == TRUE){
165                 
166                         // Workout the file name.
167                         // Get the UID value and check if it is empty.
168                         // If it isn't empty, check it can be used for a valid
169                         // filename other wise generate a file name for the purposes
170                         // of storage.
171                         
172                         // Filter UID incase writing outside the
173                         // directory is attempted.
174                         
175 #if defined(__HAIKU__)
176     
179 #elif defined(__WIN32__)
181                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
182                         while (UIDValueCheck.HasMoreTokens()){
183                                 UIDValueCheck.GetNextToken();
184                                 FilterCount++;
185                         }
187 #else
189                         wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
190                         while (UIDValueCheck.HasMoreTokens()){
191                                 UIDValueCheck.GetNextToken();
192                                 FilterCount++;
193                         }
194         
195 #endif
196                         
197                         if (FilterCount == 1){
198                                 UIDFilterOK = TRUE;
199                         }
200                         
201                         if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
202                         
203                                 bool NewFileExists = TRUE;
204                                 
205                                 while (NewFileExists){
206                                 
207                                         // Generate the UID and check if it exists.
208                                         
209                                         wxString CheckFilename;
210                                         
211                                         UIDToken = GenerateUUID();
212                                         
213                                         CheckFilename.Append(AccountDirFinal);
214                                         CheckFilename.Append(UIDToken);
215                                         CheckFilename.Append(wxT(".vcf"));
216                                 
217                                         if (!wxFileExists(CheckFilename)){
218                                         
219                                                 NewFileExists = FALSE;
220                                         
221                                         }
222                                 
223                                 }
224                         
225                         } else {
226                         
227                                 UIDToken = iter->second.UIDValue;
228                         
229                         }
230                 
231                         // Check if the file exists in the directory already.
232                 
233                         OutputFilename.Append(AccountDirFinal);
234                         OutputFilename.Append(wxT("/"));
235                         OutputFilename.Append(UIDToken);
236                         OutputFilename.Append(wxT(".vcf"));
237                 
238                         if(wxFileExists(OutputFilename)){
239                         
240                                 // File already exists so mark as an error.
241                                 
242                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
243                                 ImportErrorCount++;
244                                 ImportSeek++;
245                                 GeneratedFilename.Clear();
246                                 OutputFilename.Clear();
247                                 UIDValueFinal.Clear();
248                                 UIDFilterOK = FALSE;
249                                 FilterCount = 0;
250                                 continue;
251                         
252                         } else {
253                         
254                                 // Write to the file.
255                                 
256                                 wxFFile OutputFile;
257                         
258 #if wxABI_VERSION < 20900
259                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
260 #else
261                                 if(!OutputFile.Open(OutputFilename, wxT("w"))){
262 #endif
263                                         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."))));
264                         
265                                 }
266                         
267                                 OutputFile.Write(iter->second.ContactData, wxConvAuto());
268                         
269                                 OutputFile.Close();
270                                 
271                                 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
272                         
273                         }
274                         
275                         // Check if there is a UID value set. If there is not a UID
276                         // value set then generate a new one.
277                 
278                         ImportCount++;
279                 
280                         ImportSeek++;
281                 
282                         GeneratedFilename.Clear();
283                         OutputFilename.Clear();
284                         UIDValueFinal.Clear();
285                         UIDToken.Clear();
286                         UIDFilterOK = FALSE;
287                         FilterCount = 0;
288                 
289                 }
290         
291         }
293         // Check if the import error count is more than 0. If it is, then
294         // display the results dialog.
295         
296         wxCommandEvent imprres(IMPORT_RESULTSSHOW);
297         imprres.SetClientData(ResultData);
298         imprres.SetInt(ImportCount);
299         imprres.SetExtraLong(ImportErrorCount);
300         wxPostEvent(frmMainPtrInc, imprres);
301                 
302         // Syncronise the account which the contacts got imported to.
303         // (If the account is not a local account).
304         
305         if (AccountType != wxT("Local") && AccountType != wxT("local")){
306         
307                 wxString AccNamePostEvent;
308         
309                 AccNamePostEvent.Clear();
310                 AccNamePostEvent.Append(AccountName);
311                 AccNamePostEvent.Trim();
312         
313                 wxCommandEvent accevent(SYNCACCOUNT);
314                 accevent.SetString(AccNamePostEvent);
315         
316                 wxPostEvent(frmMainPtrInc, accevent);
317         
318         }
319         
320         // Resume the timers.
321         
322         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