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 return CONVERTRESULT_INPUTFILEERROR;
101 // Check that the output file can be opened.
103 bool OutputPipe = false;
106 if (!OutputFilename.IsEmpty()){
108 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
109 return CONVERTRESULT_OUTPUTFILEERROR;
118 // Check that the file has the correct file format.
123 if (InputFormat == "vCard4"){
125 // Read from the file.
127 wxString InputFileData;
128 InputFile.ReadAll(&InputFileData, wxConvAuto());
132 vCard4Format.LoadString(InputFileData);
134 if (vCard4Format.Get("VERSION") != "4.0"){
135 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
138 } else if (InputFormat == "vCard3"){
140 // Read from the file.
142 wxString InputFileData;
143 InputFile.ReadAll(&InputFileData, wxConvAuto());
147 InputvCard.LoadString(InputFileData);
149 if (InputvCard.Get("VERSION") != "3.0"){
150 return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
153 vCard34Conv vCard34ConvObj;
157 vCard34ConvObj.ConvertToV4(&InputFileData, &vCard4Format);
159 FinalData = vCard4Format.WriteString();
163 // Convert the vCard into the required format and
164 // get the string value.
166 if (OutputFormat == "vCard4"){
168 // Do nothing as the vCard is already in the vCard 4.0
171 } else if (OutputFormat == "vCard3"){
173 // Convert the vCard to vCard 3.0.
175 vCard34Conv vCard34ConvObj;
179 vCard34ConvObj.ConvertToV3(InputFilename, &FinalData);
183 // Check if we are outputting to console or to file.
185 if (OutputPipe == false){
187 // We are outputting to a file.
189 OutputFile.Write(FinalData, wxConvUTF8);
193 // Write out the data to the console.
196 std::cout << FinalData.ToStdString() << std::endl;
200 return CONVERTRESULT_OK;