#include #include #include #include #include #include #include "import-vcard3.h" #include "import-vcard4.h" #include "import-struct.h" #include "../frmMain.h" #include "frmImportContacts.h" #include "frmImportResults.h" #include "../common/uuid.h" #include "../common/dirs.h" void ImportRun(frmMain *frmMainPtrInc){ // Bring up a dialog selecting one or multiple // files for processing based on type. struct ImportResultData{ }; frmMainPtrInc->PauseAllTimers(); wxString FileTypes; long ContactsExported = 0; long ContactsCollected = 0; int ExportType = 0; int ExportCount = 0; wxString FinalFilename; wxArrayString SelectedFileList; wxString SelectedFileDirectory; wxString AccountName; int ImportErrorCount = 0; std::map ResultData; FileTypes.Append(wxT("vCard 4.0 Contact(s) (*.vcf)")); FileTypes.Append(wxT("|*.vcf|")); FileTypes.Append(wxT("vCard 3.0 Contact(s) (*.vcf)")); FileTypes.Append(wxT("|*.vcf")); // Open up the dialog to export file data. wxFileDialog ExportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""), FileTypes, wxFD_OPEN|wxFD_MULTIPLE); if (ExportDlg.ShowModal() == wxID_CANCEL){ frmMainPtrInc->ResumeAllTimers(); return; } // Get the list of filenames. ExportDlg.GetPaths(SelectedFileList); // Find which data type is being used. ExportType = ExportDlg.GetFilterIndex(); std::map ContactData; if (ExportType == 0){ ContactData = ImportVCard4(&SelectedFileList); //ImportVCard4(DirMode, FinalFilename, FileListInc, &ExportCount); } // vCard 3.0 export if (ExportType == 1){ ContactData = ImportVCard3(&SelectedFileList); //ImportVCard3(DirMode, FinalFilename, FileListInc, &ExportCount); } // Check how many contacts are in the set. If zero, throw an error // message and exit. if (ContactData.size() == 0){ wxMessageBox(_("No valid contacts within the file(s) selected."), _("Unable to import")); return; } // Show a form to let the user select the // contacts needed to import and which account // they go to. frmImportContacts *frmIC = new frmImportContacts(frmMainPtrInc); frmIC->SetupList(&ContactData); frmIC->ShowModal(); frmIC->GetResults(&ContactData); AccountName = frmIC->GetAccount(); // Import the contacts into the selected account. int ImportCount = 0; int ImportSeek = 0; // Setup the directory for writing into. wxString AccountDirFinal = GetAccountDir(AccountName, FALSE); wxString GeneratedFilename; wxString OutputFilename; wxString UIDValueFinal; wxString UIDToken; bool UIDFilterOK = FALSE; int FilterCount = 0; GeneratedFilename.Clear(); OutputFilename.Clear(); static wxString BadCharList = wxFileName::GetForbiddenChars(wxPATH_NATIVE); wxString BadCharListFinal; BadCharListFinal.Append(wxT("[")); BadCharListFinal.Append(BadCharList); BadCharListFinal.Append(wxT("]")); wxRegEx BadCharRex; BadCharRex.Compile(BadCharListFinal); for(std::map::iterator iter = ContactData.begin(); iter != ContactData.end(); iter++){ if (iter->second.ContactSelected == TRUE){ // Workout the file name. // Get the UID value and check if it is empty. // If it isn't empty, check it can be used for a valid // filename other wise generate a file name for the purposes // of storage. // Filter UID incase writing outside the // directory is attempted. #if defined(__HAIKU__) //preffilename = wxT("noo"); #elif defined(__WIN32__) wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("\\")); while (UIDValueCheck.HasMoreTokens()){ UIDValueCheck.GetNextToken(); FilterCount++; } #else wxStringTokenizer UIDValueCheck(iter->second.UIDValue, wxT("/")); while (UIDValueCheck.HasMoreTokens()){ UIDValueCheck.GetNextToken(); FilterCount++; } #endif if (FilterCount == 1){ UIDFilterOK = TRUE; } if (BadCharRex.Matches(iter->second.UIDValue) && UIDFilterOK == TRUE){ bool NewFileExists = TRUE; while (NewFileExists){ // Generate the UID and check if it exists. wxString CheckFilename; UIDToken = GenerateUUID(); CheckFilename.Append(AccountDirFinal); CheckFilename.Append(UIDToken); CheckFilename.Append(wxT(".vcf")); if (!wxFileExists(CheckFilename)){ NewFileExists = FALSE; } } } else { UIDToken = iter->second.UIDValue; } //GenerateUUID(); /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){ }*/ // Check if the file exists in the directory already. OutputFilename.Append(AccountDirFinal); OutputFilename.Append(wxT("/")); OutputFilename.Append(UIDToken); OutputFilename.Append(wxT(".vcf")); if(wxFileExists(OutputFilename)){ // File already exists so mark as an error. ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): "), iter->second.FriendlyName, UIDToken))); ImportErrorCount++; ImportSeek++; GeneratedFilename.Clear(); OutputFilename.Clear(); UIDValueFinal.Clear(); UIDFilterOK = FALSE; FilterCount = 0; continue; } else { // Write to the file. wxFFile OutputFile; #if wxABI_VERSION < 20900 OutputFile.Open(OutputFilename, wxT("w")); #else OutputFile.Open(OutputFilename, wxT("w")); #endif OutputFile.Write(iter->second.ContactData, wxConvAuto()); OutputFile.Close(); ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported.")))); } // Check if there is a UID value set. If there is not a UID // value set then generate a new one. ImportCount++; ImportSeek++; GeneratedFilename.Clear(); OutputFilename.Clear(); UIDValueFinal.Clear(); UIDToken.Clear(); UIDFilterOK = FALSE; FilterCount = 0; } } // Check if the import error count is more than 0. If it is, then // display the results dialog. frmImportResults *frmIR = new frmImportResults(frmMainPtrInc); frmIR->LoadData(&ResultData); frmIR->ShowModal(); delete frmIR; frmIR = NULL; delete frmIC; frmIC = NULL; // Syncronise the account which the contacts got imported to. wxString *AccNamePostEventPtr = new wxString; wxCommandEvent accevent(SYNCACCOUNT); accevent.SetClientData(AccNamePostEventPtr); wxPostEvent(frmMainPtrInc, accevent); //wxPostEvent // Resume the timers. frmMainPtrInc->ResumeAllTimers(); }