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>
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();
45 wxString FinalFilename;
46 wxArrayString SelectedFileList;
47 wxString SelectedFileDirectory;
50 int ImportErrorCount = 0;
52 std::map<int,wxString> *ResultData = new std::map<int,wxString>;
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.
61 wxFileDialog ImportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
62 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
64 if (ImportDlg.ShowModal() == wxID_CANCEL){
66 frmMainPtrInc->ResumeAllTimers();
71 // Get the list of filenames.
73 ImportDlg.GetPaths(SelectedFileList);
75 // Find which data type is being used.
77 ImportType = ImportDlg.GetFilterIndex();
79 std::map<int, ImportDataContact> ContactData;
83 ContactData = ImportVCard4(&SelectedFileList);
91 ContactData = ImportVCard3(&SelectedFileList);
95 // Check how many contacts are in the set. If zero, throw an error
98 if (ContactData.size() == 0){
100 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
106 // Show a form to let the user select the
107 // contacts needed to import and which account
110 frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
112 frmIC->SetupList(&ContactData);
114 frmIC->GetResults(&ContactData);
115 AccountName = frmIC->GetAccount();
116 AccountType = frmIC->GetAccountType();
118 if (frmIC->GetDialogResult() == FALSE){
120 // User decided not to import. Clean up the
125 frmMainPtrInc->ResumeAllTimers();
133 // Import the contacts into the selected account.
138 // Setup the directory for writing into.
140 wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
142 wxString GeneratedFilename;
143 wxString OutputFilename;
144 wxString UIDValueFinal;
146 bool UIDFilterOK = FALSE;
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("]"));
159 BadCharRex.Compile(BadCharListFinal);
161 for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
162 iter != ContactData.end(); iter++){
164 if (iter->second.ContactSelected == TRUE){
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
172 // Filter UID incase writing outside the
173 // directory is attempted.
175 #if defined(__HAIKU__)
179 #elif defined(__WIN32__)
181 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
182 while (UIDValueCheck.HasMoreTokens()){
183 UIDValueCheck.GetNextToken();
189 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
190 while (UIDValueCheck.HasMoreTokens()){
191 UIDValueCheck.GetNextToken();
197 if (FilterCount == 1){
201 if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
203 bool NewFileExists = TRUE;
205 while (NewFileExists){
207 // Generate the UID and check if it exists.
209 wxString CheckFilename;
211 UIDToken = GenerateUUID();
213 CheckFilename.Append(AccountDirFinal);
214 CheckFilename.Append(UIDToken);
215 CheckFilename.Append(wxT(".vcf"));
217 if (!wxFileExists(CheckFilename)){
219 NewFileExists = FALSE;
227 UIDToken = iter->second.UIDValue;
231 // Check if the file exists in the directory already.
233 OutputFilename.Append(AccountDirFinal);
234 OutputFilename.Append(wxT("/"));
235 OutputFilename.Append(UIDToken);
236 OutputFilename.Append(wxT(".vcf"));
238 if(wxFileExists(OutputFilename)){
240 // File already exists so mark as an error.
242 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
245 GeneratedFilename.Clear();
246 OutputFilename.Clear();
247 UIDValueFinal.Clear();
254 // Write to the file.
258 #if wxABI_VERSION < 20900
259 if(!OutputFile.Open(OutputFilename, wxT("w"))){
261 if(!OutputFile.Open(OutputFilename, wxT("w"))){
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."))));
267 OutputFile.Write(iter->second.ContactData, wxConvAuto());
271 ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
275 // Check if there is a UID value set. If there is not a UID
276 // value set then generate a new one.
282 GeneratedFilename.Clear();
283 OutputFilename.Clear();
284 UIDValueFinal.Clear();
293 // Check if the import error count is more than 0. If it is, then
294 // display the results dialog.
296 wxCommandEvent imprres(IMPORT_RESULTSSHOW);
297 imprres.SetClientData(ResultData);
298 imprres.SetInt(ImportCount);
299 imprres.SetExtraLong(ImportErrorCount);
300 wxPostEvent(frmMainPtrInc, imprres);
302 // Syncronise the account which the contacts got imported to.
303 // (If the account is not a local account).
305 if (AccountType != wxT("Local") && AccountType != wxT("local")){
307 wxString AccNamePostEvent;
309 AccNamePostEvent.Clear();
310 AccNamePostEvent.Append(AccountName);
311 AccNamePostEvent.Trim();
313 wxCommandEvent accevent(SYNCACCOUNT);
314 accevent.SetString(AccNamePostEvent);
316 wxPostEvent(frmMainPtrInc, accevent);
320 // Resume the timers.
322 frmMainPtrInc->ResumeAllTimers();