1 // convert.h - Convert 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/>
20 #include "vcard/vcard.h"
21 #include "vcard/vcard34conv.h"
23 ConvertResult ConvertContact(wxString InputFormat, wxString OutputFormat,
24 wxString InputFilename, wxString OutputFilename){
26 std::vector<wxString> ValidFormats = {
31 bool ValidInput = false;
32 bool ValidOutput = false;
34 // Check that the input format is valid.
36 for (long l = 0; l < ValidFormats.size(); l++){
38 if (InputFormat == ValidFormats[l]){
45 if (ValidInput == false){
47 return CONVERTRESULT_INVALIDINPUTFORMAT;
51 // Check that the output format is valid.
53 for (long l = 0; l < ValidFormats.size(); l++){
55 if (OutputFormat == ValidFormats[l]){
62 if (ValidOutput == false){
64 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
68 // Check that the input and output formats
71 if (InputFormat == OutputFormat){
73 return CONVERTRESULT_FORMATSSAME;
77 // Check that the input filename is not empty.
79 if (InputFilename.empty()){
81 return CONVERTRESULT_INPUTFILEEMPTY;
85 // Check that the input file given exists.
87 if (!wxFileExists(InputFilename)){
89 return CONVERTRESULT_INPUTFILEMISSING;
95 if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
97 int InputFileErrNo = InputFile.GetLastError();
99 return CONVERTRESULT_INPUTFILEERROR;
103 // Check that the output file can be opened.
105 bool OutputPipe = false;
108 if (!OutputFilename.IsEmpty()){
110 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
111 return CONVERTRESULT_OUTPUTFILEERROR;
120 // Check that the file has the correct file format.
125 if (InputFormat == "vCard4"){
127 // Read from the file.
129 wxString InputFileData;
130 InputFile.ReadAll(&InputFileData, wxConvAuto());
134 vCard4Format.LoadString(InputFileData);
136 if (vCard4Format.Get("VERSION") != "4.0"){
137 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
140 } else if (InputFormat == "vCard3"){
142 // Read from the file.
144 wxString InputFileData;
145 InputFile.ReadAll(&InputFileData, wxConvAuto());
149 InputvCard.LoadString(InputFileData);
151 if (InputvCard.Get("VERSION") != "3.0"){
152 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
155 vCard34Conv vCard34ConvObj;
159 vCard34ConvObj.ConvertToV4(&InputFileData, &vCard4Format);
161 FinalData = vCard4Format.WriteString();
165 // Convert the vCard into the required format and
166 // get the string value.
168 if (OutputFormat == "vCard4"){
170 // Do nothing as the vCard is already in the vCard 4.0
173 } else if (OutputFormat == "vCard3"){
175 // Convert the vCard to vCard 3.0.
177 vCard34Conv vCard34ConvObj;
181 vCard34ConvObj.ConvertToV3(InputFilename, &FinalData);
185 // Check if we are outputting to console or to file.
187 if (OutputPipe == false){
189 // We are outputting to a file.
191 OutputFile.Write(FinalData, wxConvUTF8);
195 // Write out the data to the console.
198 std::cout << FinalData << std::endl;
202 return CONVERTRESULT_OK;