Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Win32 Support: Implement dialog when an untrusted SSL connection is made.
[xestiaab/.git] / source / convert.cpp
1 // convert.h - Convert subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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/>
19 #include "convert.h"
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 = { 
27                 wxT("vCard3"),
28                 wxT("vCard4") 
29         };
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++){
37         
38                 if (InputFormat == ValidFormats[l]){
39                         ValidInput = true;
40                         break;
41                 }
42         
43         }
44         
45         if (ValidInput == false){
46         
47                 return CONVERTRESULT_INVALIDINPUTFORMAT;
48         
49         }
51         // Check that the output format is valid.
53         for (long l = 0; l < ValidFormats.size(); l++){
54         
55                 if (OutputFormat == ValidFormats[l]){
56                         ValidOutput = true;
57                         break;
58                 }
59         
60         }
61         
62         if (ValidOutput == false){
63         
64                 return CONVERTRESULT_INVALIDOUTPUTFORMAT;
65         
66         }
68         // Check that the input and output formats
69         // are not the same.
71         if (InputFormat == OutputFormat){
72         
73                 return CONVERTRESULT_FORMATSSAME;
74         
75         }
76         
77         // Check that the input filename is not empty.
78         
79         if (InputFilename.empty()){
80         
81                 return CONVERTRESULT_INPUTFILEEMPTY;
82         
83         }
84         
85         // Check that the input file given exists.
86         
87         if (!wxFileExists(InputFilename)){
88                 
89                 return CONVERTRESULT_INPUTFILEMISSING;
90                 
91         }
92         
93         wxFile InputFile;
94         
95         if (!InputFile.Open(InputFilename, wxFile::read, wxS_DEFAULT)){
96                                 
97                 return CONVERTRESULT_INPUTFILEERROR;
98                 
99         }
100         
101         // Check that the output file can be opened.
102         
103         bool OutputPipe = false;
104         wxFile OutputFile;
105         
106         if (!OutputFilename.IsEmpty()){
107         
108                 if (!OutputFile.Open(OutputFilename, wxFile::write, wxS_DEFAULT)){
109                         return CONVERTRESULT_OUTPUTFILEERROR;
110                 }
111         
112         } else {
113         
114                 OutputPipe = true;
115         
116         }
117         
118         // Check that the file has the correct file format.
119         
120         vCard vCard4Format;
121         wxString FinalData;
122         
123         if (InputFormat == "vCard4"){
124         
125                 // Read from the file.
126                 
127                 wxString InputFileData;
128                 InputFile.ReadAll(&InputFileData, wxConvAuto());
129         
130                 vCard vCard4Format;
131                 
132                 vCard4Format.LoadString(InputFileData);
133                 
134                 if (vCard4Format.Get("VERSION") != "4.0"){
135                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
136                 }
137         
138         } else if (InputFormat == "vCard3"){
139         
140                 // Read from the file.
141                 
142                 wxString InputFileData;
143                 InputFile.ReadAll(&InputFileData, wxConvAuto());
144         
145                 vCard InputvCard;
146                 
147                 InputvCard.LoadString(InputFileData);
148                 
149                 if (InputvCard.Get("VERSION") != "3.0"){
150                         return CONVERTRESULT_INPUTFILEINVALIDFORMAT;
151                 }
153                 vCard34Conv vCard34ConvObj;
154                 
155                 vCard vCard4Format;
156                 
157                 vCard34ConvObj.ConvertToV4(&InputFileData, &vCard4Format);
158                 
159                 FinalData = vCard4Format.WriteString();
160                 
161         }
162         
163         // Convert the vCard into the required format and
164         // get the string value.
165         
166         if (OutputFormat == "vCard4"){
167         
168                 // Do nothing as the vCard is already in the vCard 4.0
169                 // format.
170         
171         } else if (OutputFormat == "vCard3"){
172         
173                 // Convert the vCard to vCard 3.0.
174                 
175                 vCard34Conv vCard34ConvObj;
176                 
177                 vCard vCard3Format;
178                 
179                 vCard34ConvObj.ConvertToV3(InputFilename, &FinalData);
180         
181         }
182         
183         // Check if we are outputting to console or to file.
184         
185         if (OutputPipe == false){
186         
187                 // We are outputting to a file.
188                 
189                 OutputFile.Write(FinalData, wxConvUTF8);
190         
191         } else {
192         
193                 // Write out the data to the console.
194                 
195                 FinalData.Trim();
196                 std::cout << FinalData << std::endl;
197         
198         }
199         
200         return CONVERTRESULT_OK;
201         
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