Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Import: Added import.h to import.cpp
[xestiaab/.git] / source / import / import.cpp
index 416a39f..efd701a 100644 (file)
@@ -1,3 +1,21 @@
+// import.cpp - Import subroutines.
+//
+// (c) 2012-2015 Xestia Software Development.
+//
+// This file is part of Xestia Address Book.
+//
+// Xestia Address Book is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Address Book is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
+
 #include <wx/filedlg.h>
 #include <wx/filename.h>
 #include <wx/regex.h>
@@ -5,6 +23,7 @@
 #include <wx/ffile.h>
 #include <map>
 
+#include "import.h"
 #include "import-vcard3.h"
 #include "import-vcard4.h"
 #include "import-struct.h"
@@ -19,37 +38,31 @@ 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;
+       int ImportType = 0;
        wxString FinalFilename;
        wxArrayString SelectedFileList;
        wxString SelectedFileDirectory;
        wxString AccountName;
+       wxString AccountType;
        int ImportErrorCount = 0;
        
-       std::map<int,wxString> ResultData;
+       std::map<int,wxString> *ResultData = new std::map<int,wxString>;
        
        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.
+       // Open up the dialog to import file data.
        
-       wxFileDialog ExportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
+       wxFileDialog ImportDlg(frmMainPtrInc, wxT("Import Contacts"), wxT(""), wxT(""),
                FileTypes, wxFD_OPEN|wxFD_MULTIPLE);
        
-       if (ExportDlg.ShowModal() == wxID_CANCEL){
+       if (ImportDlg.ShowModal() == wxID_CANCEL){
        
                frmMainPtrInc->ResumeAllTimers();
                return;
@@ -58,29 +71,25 @@ void ImportRun(frmMain *frmMainPtrInc){
 
        // Get the list of filenames.
 
-       ExportDlg.GetPaths(SelectedFileList);
+       ImportDlg.GetPaths(SelectedFileList);
        
        // Find which data type is being used.
                
-       ExportType = ExportDlg.GetFilterIndex();
+       ImportType = ImportDlg.GetFilterIndex();
 
        std::map<int, ImportDataContact> ContactData;
 
-       if (ExportType == 0){
+       if (ImportType == 0){
        
                ContactData = ImportVCard4(&SelectedFileList);
-       
-               //ImportVCard4(DirMode, FinalFilename, FileListInc, &ExportCount);
-       
+
        }
        
-       // vCard 3.0 export
+       // vCard 3.0 import
        
-       if (ExportType == 1){
+       if (ImportType == 1){
        
                ContactData = ImportVCard3(&SelectedFileList);
-       
-               //ImportVCard3(DirMode, FinalFilename, FileListInc, &ExportCount);
 
        }
 
@@ -105,6 +114,22 @@ void ImportRun(frmMain *frmMainPtrInc){
        frmIC->ShowModal();
        frmIC->GetResults(&ContactData);
        AccountName = frmIC->GetAccount();
+       AccountType = frmIC->GetAccountType();
+
+       if (frmIC->GetDialogResult() == FALSE){
+               
+               // User decided not to import. Clean up the
+               // dialog.
+               
+               delete frmIC;
+               frmIC = NULL;
+               frmMainPtrInc->ResumeAllTimers();
+               return;
+               
+       }
+
+       delete frmIC;
+       frmIC = NULL;
 
        // Import the contacts into the selected account.
 
@@ -150,7 +175,7 @@ void ImportRun(frmMain *frmMainPtrInc){
                        
 #if defined(__HAIKU__)
     
-    //preffilename = wxT("noo");
+
 
 #elif defined(__WIN32__)
 
@@ -203,14 +228,6 @@ void ImportRun(frmMain *frmMainPtrInc){
                                UIDToken = iter->second.UIDValue;
                        
                        }
-                       
-                       //GenerateUUID();
-                       
-                       /*if (iter->second.ContactSelected.UIDValue.IsEmpty()){
-                       
-                       
-                       
-                       }*/
                
                        // Check if the file exists in the directory already.
                
@@ -223,7 +240,7 @@ void ImportRun(frmMain *frmMainPtrInc){
                        
                                // 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)));
+                               ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Unable to import '%s'(%s): %s"), iter->second.FriendlyName, UIDToken, _("Contact already exists."))));
                                ImportErrorCount++;
                                ImportSeek++;
                                GeneratedFilename.Clear();
@@ -240,16 +257,19 @@ void ImportRun(frmMain *frmMainPtrInc){
                                wxFFile OutputFile;
                        
 #if wxABI_VERSION < 20900
-                               OutputFile.Open(OutputFilename, wxT("w"));
+                               if(!OutputFile.Open(OutputFilename, wxT("w"))){
 #else
-                               OutputFile.Open(OutputFilename, wxT("w"));
+                               if(!OutputFile.Open(OutputFilename, wxT("w"))){
 #endif
+                                       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."))));
+                       
+                               }
                        
                                OutputFile.Write(iter->second.ContactData, wxConvAuto());
                        
                                OutputFile.Close();
                                
-                               ResultData.insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."))));
+                               ResultData->insert(std::make_pair(ImportSeek, wxString::Format(_("Contact '%s'(%s) was successfully imported."), iter->second.FriendlyName, UIDToken)));
                        
                        }
                        
@@ -274,26 +294,29 @@ void ImportRun(frmMain *frmMainPtrInc){
        // 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();
+       wxCommandEvent imprres(IMPORT_RESULTSSHOW);
+       imprres.SetClientData(ResultData);
+       imprres.SetInt(ImportCount);
+       imprres.SetExtraLong(ImportErrorCount);
+       wxPostEvent(frmMainPtrInc, imprres);
                
-       delete frmIR;
-       frmIR = NULL;
-
-       delete frmIC;
-       frmIC = NULL;
-       
        // Syncronise the account which the contacts got imported to.
+       // (If the account is not a local account).
+       
+       if (AccountType != wxT("Local") && AccountType != wxT("local")){
        
-       wxString *AccNamePostEventPtr = new wxString;
+               wxString AccNamePostEvent;
        
-       wxCommandEvent accevent(SYNCACCOUNT);
-       accevent.SetClientData(AccNamePostEventPtr);
+               AccNamePostEvent.Clear();
+               AccNamePostEvent.Append(AccountName);
+               AccNamePostEvent.Trim();
        
-       wxPostEvent(frmMainPtrInc, accevent);
+               wxCommandEvent accevent(SYNCACCOUNT);
+               accevent.SetString(AccNamePostEvent);
        
-       //wxPostEvent
+               wxPostEvent(frmMainPtrInc, accevent);
+       
+       }
        
        // Resume the timers.
        
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