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