1 #include <wx/filedlg.h>
2 #include <wx/filename.h>
4 #include <wx/tokenzr.h>
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 struct ImportResultData{
26 frmMainPtrInc->PauseAllTimers();
30 long ContactsExported = 0;
31 long ContactsCollected = 0;
34 wxString FinalFilename;
35 wxArrayString SelectedFileList;
36 wxString SelectedFileDirectory;
38 int ImportErrorCount = 0;
40 std::map<int,wxString> ResultData;
42 FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)"));
43 FileTypes.Append(wxT("|*.vcf|"));
44 FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)"));
45 FileTypes.Append(wxT("|*.vcf"));
47 // Open up the dialog to export file data.
49 wxFileDialog ExportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
50 FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
52 if (ExportDlg.ShowModal() == wxID_CANCEL){
54 frmMainPtrInc->ResumeAllTimers();
59 // Get the list of filenames.
61 ExportDlg.GetPaths(SelectedFileList);
63 // Find which data type is being used.
65 ExportType = ExportDlg.GetFilterIndex();
67 std::map<int, ImportDataContact> ContactData;
71 ContactData = ImportVCard4(&SelectedFileList);
73 //ImportVCard4(DirMode, FinalFilename, FileListInc, &ExportCount);
81 ContactData = ImportVCard3(&SelectedFileList);
83 //ImportVCard3(DirMode, FinalFilename, FileListInc, &ExportCount);
87 // Check how many contacts are in the set. If zero, throw an error
90 if (ContactData.size() == 0){
92 wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import"));
98 // Show a form to let the user select the
99 // contacts needed to import and which account
102 frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc);
104 frmIC->SetupList(&ContactData);
106 frmIC->GetResults(&ContactData);
107 AccountName = frmIC->GetAccount();
109 // Import the contacts into the selected account.
114 // Setup the directory for writing into.
116 wxString AccountDirFinal = GetAccountDir(AccountName, FALSE);
118 wxString GeneratedFilename;
119 wxString OutputFilename;
120 wxString UIDValueFinal;
122 bool UIDFilterOK = FALSE;
125 GeneratedFilename.Clear();
126 OutputFilename.Clear();
128 static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE);
129 wxString BadCharListFinal;
130 BadCharListFinal.Append(wxT("["));
131 BadCharListFinal.Append(BadCharList);
132 BadCharListFinal.Append(wxT("]"));
135 BadCharRex.Compile(BadCharListFinal);
137 for(std::map<int,ImportDataContact>::iterator iter = ContactData.begin();
138 iter != ContactData.end(); iter++){
140 if (iter->second.ContactSelected == TRUE){
142 // Workout the file name.
143 // Get the UID value and check if it is empty.
144 // If it isn't empty, check it can be used for a valid
145 // filename other wise generate a file name for the purposes
148 // Filter UID incase writing outside the
149 // directory is attempted.
151 #if defined(__HAIKU__)
153 //preffilename = wxT("noo");
155 #elif defined(__WIN32__)
157 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\"));
158 while (UIDValueCheck.HasMoreTokens()){
159 UIDValueCheck.GetNextToken();
165 wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/"));
166 while (UIDValueCheck.HasMoreTokens()){
167 UIDValueCheck.GetNextToken();
173 if (FilterCount == 1){
177 if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){
179 bool NewFileExists = TRUE;
181 while (NewFileExists){
183 // Generate the UID and check if it exists.
185 wxString CheckFilename;
187 UIDToken = GenerateUUID();
189 CheckFilename.Append(AccountDirFinal);
190 CheckFilename.Append(UIDToken);
191 CheckFilename.Append(wxT(".vcf"));
193 if (!wxFileExists(CheckFilename)){
195 NewFileExists = FALSE;
203 UIDToken = iter->second.UIDValue;
209 /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){
215 // Check if the file exists in the directory already.
217 OutputFilename.Append(AccountDirFinal);
218 OutputFilename.Append(wxT("/"));
219 OutputFilename.Append(UIDToken);
220 OutputFilename.Append(wxT(".vcf"));
222 if(wxFileExists(OutputFilename)){
224 // File already exists so mark as an error.
226 ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): "), iter->second.FriendlyName, UIDToken)));
229 GeneratedFilename.Clear();
230 OutputFilename.Clear();
231 UIDValueFinal.Clear();
238 // Write to the file.
242 #if wxABI_VERSION < 20900
243 OutputFile.Open(OutputFilename, wxT("w"));
245 OutputFile.Open(OutputFilename, wxT("w"));
248 OutputFile.Write(iter->second.ContactData, wxConvAuto());
252 ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."))));
256 // Check if there is a UID value set. If there is not a UID
257 // value set then generate a new one.
263 GeneratedFilename.Clear();
264 OutputFilename.Clear();
265 UIDValueFinal.Clear();
274 // Check if the import error count is more than 0. If it is, then
275 // display the results dialog.
277 frmImportResults *frmIR = new frmImportResults(frmMainPtrInc);
278 frmIR->LoadData(&ResultData);
287 // Syncronise the account which the contacts got imported to.
289 wxString *AccNamePostEventPtr = new wxString;
291 wxCommandEvent accevent(SYNCACCOUNT);
292 accevent.SetClientData(AccNamePostEventPtr);
294 wxPostEvent(frmMainPtrInc, accevent);
298 // Resume the timers.
300 frmMainPtrInc->ResumeAllTimers();