1 // import.cpp - Import subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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>
22 #include <wx/tokenzr.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();
46 wxString FinalFilename;
47 wxArrayString SelectedFileList;
48 wxString SelectedFileDirectory;
51 int ImportErrorCount = 0;
53 std::map<int,wxString> *ResultData = new std::map<int,wxString>;
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.
62 wxFileDialog ImportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
63 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
65 if (ImportDlg.ShowModal() == wxID_CANCEL){
67 frmMainPtrInc->ResumeAllTimers();
72 // Get the list of filenames.
74 ImportDlg.GetPaths(SelectedFileList);
76 // Find which data type is being used.
78 ImportType = ImportDlg.GetFilterIndex();
80 std::map<int, ImportDataContact> ContactData;
84 ContactData = ImportVCard4(&SelectedFileList);
92 ContactData = ImportVCard3(&SelectedFileList);
96 // Check how many contacts are in the set. If zero, throw an error
99 if (ContactData.size() == 0){
101 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
107 // Show a form to let the user select the
108 // contacts needed to import and which account
111 frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
113 frmIC->SetupList(&ContactData);
115 frmIC->GetResults(&ContactData);
116 AccountName = frmIC->GetAccount();
117 AccountType = frmIC->GetAccountType();
119 if (frmIC->GetDialogResult() == FALSE){
121 // User decided not to import. Clean up the
126 frmMainPtrInc->ResumeAllTimers();
134 // Import the contacts into the selected account.
139 // Setup the directory for writing into.
141 wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
143 wxString GeneratedFilename;
144 wxString OutputFilename;
145 wxString UIDValueFinal;
147 bool UIDFilterOK = FALSE;
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("]"));
160 BadCharRex.Compile(BadCharListFinal);
162 for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
163 iter != ContactData.end(); iter++){
165 if (iter->second.ContactSelected == TRUE){
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
173 // Filter UID incase writing outside the
174 // directory is attempted.
176 #if defined(__HAIKU__)
180 #elif defined(__WIN32__)
182 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
183 while (UIDValueCheck.HasMoreTokens()){
184 UIDValueCheck.GetNextToken();
190 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
191 while (UIDValueCheck.HasMoreTokens()){
192 UIDValueCheck.GetNextToken();
198 if (FilterCount == 1){
202 if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
204 bool NewFileExists = TRUE;
206 while (NewFileExists){
208 // Generate the UID and check if it exists.
210 wxString CheckFilename;
212 UIDToken = GenerateUUID();
214 CheckFilename.Append(AccountDirFinal);
215 CheckFilename.Append(UIDToken);
216 CheckFilename.Append(wxT(".vcf"));
218 if (!wxFileExists(CheckFilename)){
220 NewFileExists = FALSE;
228 UIDToken = iter->second.UIDValue;
232 // Check if the file exists in the directory already.
234 OutputFilename.Append(AccountDirFinal);
235 OutputFilename.Append(wxT("/"));
236 OutputFilename.Append(UIDToken);
237 OutputFilename.Append(wxT(".vcf"));
239 if(wxFileExists(OutputFilename)){
241 // File already exists so mark as an error.
243 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
246 GeneratedFilename.Clear();
247 OutputFilename.Clear();
248 UIDValueFinal.Clear();
255 // Write to the file.
259 #if wxABI_VERSION < 20900
260 if(!OutputFile.Open(OutputFilename, wxT("w"))){
262 if(!OutputFile.Open(OutputFilename, wxT("w"))){
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."))));
268 OutputFile.Write(iter->second.ContactData, wxConvAuto());
272 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
276 // Check if there is a UID value set. If there is not a UID
277 // value set then generate a new one.
283 GeneratedFilename.Clear();
284 OutputFilename.Clear();
285 UIDValueFinal.Clear();
294 // Check if the import error count is more than 0. If it is, then
295 // display the results dialog.
297 wxCommandEvent imprres(IMPORT_RESULTSSHOW);
298 imprres.SetClientData(ResultData);
299 imprres.SetInt(ImportCount);
300 imprres.SetExtraLong(ImportErrorCount);
301 wxPostEvent(frmMainPtrInc, imprres);
303 // Syncronise the account which the contacts got imported to.
304 // (If the account is not a local account).
306 if (AccountType != wxT("Local") && AccountType != wxT("local")){
308 wxString AccNamePostEvent;
310 AccNamePostEvent.Clear();
311 AccNamePostEvent.Append(AccountName);
312 AccNamePostEvent.Trim();
314 wxCommandEvent accevent(SYNCACCOUNT);
315 accevent.SetString(AccNamePostEvent);
317 wxPostEvent(frmMainPtrInc, accevent);
321 // Resume the timers.
323 frmMainPtrInc->ResumeAllTimers();