1 // frmContactEditor-LoadPhoto.cpp - frmContactEditor load photo 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/>
19 #include "frmContactEditor.h"
20 #include "../common/base64.h"
22 void frmContactEditor::LoadPhoto(wxString wxSPropertySeg1, wxString wxSPropertySeg2, int *PhotoCount){
24 size_t intPropertyLen = wxSPropertySeg1.Len();
25 std::map<int, int> SplitPoints;
26 std::map<int, int> SplitLength;
27 std::map<int, int>::iterator SLiter;
28 wxString PropertyData;
29 wxString PropertyName;
30 wxString PropertyValue;
31 wxString PropertyTokens;
32 bool FirstToken = TRUE;
33 int intSplitsFound = 0;
40 SplitValues(&wxSPropertySeg1, &SplitPoints, &SplitLength, intPrevValue);
44 // Look for type before continuing.
46 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
47 intiter != SplitPoints.end(); ++intiter){
49 SLiter = SplitLength.find(intiter->first);
51 PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
53 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
54 PropertyName = PropertyElement.GetNextToken();
55 PropertyValue = PropertyElement.GetNextToken();
57 intPrevValue = intiter->second;
59 if (PropertyName == wxT("TYPE")){
61 if (PropertyValue == wxT("work")){
65 } else if (PropertyValue == wxT("home")){
79 // Setup blank lines for later on.
81 PicturesList.insert(std::make_pair(intValueSeek, ""));
82 PicturesListType.insert(std::make_pair(intValueSeek, wxT("")));
83 PicturesListAltID.insert(std::make_pair(intValueSeek, wxT("")));
84 PicturesListPID.insert(std::make_pair(intValueSeek, wxT("")));
85 PicturesListPref.insert(std::make_pair(intValueSeek, 0));
86 PicturesListPicEncType.insert(std::make_pair(intValueSeek, wxT("")));
87 PicturesListPictureType.insert(std::make_pair(intValueSeek, wxT("")));
88 PicturesListTokens.insert(std::make_pair(intValueSeek, wxT("")));
89 PicturesListMediatype.insert(std::make_pair(intValueSeek, wxT("")));
93 for (std::map<int, int>::iterator intiter = SplitPoints.begin();
94 intiter != SplitPoints.end(); ++intiter){
96 SLiter = SplitLength.find(intiter->first);
98 PropertyData = wxSPropertySeg1.Mid(intPrevValue, (SLiter->second));
100 wxStringTokenizer PropertyElement (PropertyData, wxT("="));
101 PropertyName = PropertyElement.GetNextToken();
102 PropertyValue = PropertyElement.GetNextToken();
104 intPrevValue = intiter->second;
106 // Process properties.
108 size_t intPropertyValueLen = PropertyValue.Len();
110 if (PropertyValue.Mid((intPropertyValueLen - 1), 1) == wxT("\"")){
112 PropertyValue.Trim();
113 PropertyValue.RemoveLast();
117 if (PropertyValue.Mid(0, 1) == wxT("\"")){
119 PropertyValue.Remove(0, 1);
123 ProcessCaptureStrings(&PropertyValue);
125 if (PropertyName == wxT("ALTID")){
127 if (intType == 0){ PicturesListAltID.erase(intValueSeek); PicturesListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
128 else if (intType == 1){ PicturesListAltID.erase(intValueSeek); PicturesListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
129 else if (intType == 2){ PicturesListAltID.erase(intValueSeek); PicturesListAltID.insert(std::make_pair(intValueSeek, PropertyValue)); }
131 } else if (PropertyName == wxT("PID")){
133 if (intType == 0){ PicturesListPID.erase(intValueSeek); PicturesListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
134 else if (intType == 1){ PicturesListPID.erase(intValueSeek); PicturesListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
135 else if (intType == 2){ PicturesListPID.erase(intValueSeek); PicturesListPID.insert(std::make_pair(intValueSeek, PropertyValue)); }
137 } else if (PropertyName == wxT("PREF")){
139 intPref = wxAtoi(PropertyValue);
141 if (intType == 0){ PicturesListPref.erase(intValueSeek); PicturesListPref.insert(std::make_pair(intValueSeek, intPref)); }
142 else if (intType == 1){ PicturesListPref.erase(intValueSeek); PicturesListPref.insert(std::make_pair(intValueSeek, intPref)); }
143 else if (intType == 2){ PicturesListPref.erase(intValueSeek); PicturesListPref.insert(std::make_pair(intValueSeek, intPref)); }
145 } else if (PropertyName == wxT("MEDIATYPE")){
147 if (intType == 0){ PicturesListMediatype.erase(intValueSeek); PicturesListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
148 else if (intType == 1){ PicturesListMediatype.erase(intValueSeek); PicturesListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
149 else if (intType == 2){ PicturesListMediatype.erase(intValueSeek); PicturesListMediatype.insert(std::make_pair(intValueSeek, PropertyValue)); }
153 // Something else we don't know about so append
154 // to the tokens variable.
156 if (!PropertyName.IsEmpty() && !PropertyValue.IsEmpty() && PropertyName != wxT("TYPE")){
158 if (FirstToken == TRUE){
160 PropertyTokens.Append(PropertyName + wxT("=") + PropertyValue);
165 PropertyTokens.Append(wxT(";") + PropertyName + wxT("=") + PropertyValue);
175 intPropertyLen = wxSPropertySeg2.Len();
182 ProcessCaptureStrings(&wxSPropertySeg2);
184 for (int i = 0; i <= intPropertyLen; i++){
188 if (wxSPropertySeg2.Mid(i, 1) == wxT(";")){
191 SplitPoints.insert(std::make_pair(intSplitsFound, (i + 1)));
193 if (intSplitsFound == 6){
195 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
200 SplitLength.insert(std::make_pair(intSplitsFound, (intSplitSize - 1)));
210 wxString wxSPhotoURI;
211 wxString wxSPhotoMIME;
212 wxString wxSPhotoEncoding;
213 wxString wxSPhotoData;
214 std::string base64enc;
216 if (intSplitsFound == 0){
220 std::map<int, int>::iterator striter;
222 striter = SplitLength.find(1);
224 wxStringTokenizer wSTDataType(wxSPropertySeg2.Mid(0, striter->second), wxT(":"));
226 while (wSTDataType.HasMoreTokens() == TRUE){
228 wxSPhotoURI = wSTDataType.GetNextToken();
229 wxSPhotoMIME = wSTDataType.GetNextToken();
234 wxStringTokenizer wSTDataInfo(wxSPropertySeg2.Mid((striter->second + 1)), wxT(","));
236 while (wSTDataInfo.HasMoreTokens() == TRUE){
238 wxSPhotoEncoding = wSTDataInfo.GetNextToken();
239 wxSPhotoData = wSTDataInfo.GetNextToken();
240 base64enc = wxSPhotoData.mb_str();
247 // Add the data to the General/Home/Work address variables.
251 coldata.SetId(intValueSeek);
252 coldata.SetData(intValueSeek);
253 coldata.SetText(_("Picture"));
255 ListCtrlIndex = lboPictures->InsertItem(coldata);
257 if (intPref > 0 && intPref < 101){
259 lboPictures->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), intPref));
263 PicturesList.erase(intValueSeek);
264 PicturesListType.erase(intValueSeek);
265 PicturesListTokens.erase(intValueSeek);
266 PicturesListPictureType.erase(intValueSeek);
267 PicturesListPicEncType.erase(intValueSeek);
268 PicturesList.insert(std::make_pair(intValueSeek, base64enc));
269 PicturesListPictureType.insert(std::make_pair(intValueSeek, wxSPhotoMIME));
270 PicturesListPicEncType.insert(std::make_pair(intValueSeek, wxSPhotoEncoding));
274 PicturesListType.insert(std::make_pair(intValueSeek, wxT("")));
276 } else if (intType == 1){
278 PicturesListType.insert(std::make_pair(intValueSeek, wxT("home")));
279 lboPictures->SetItem(ListCtrlIndex, 1, _("Home"));
281 } else if (intType == 2){
283 PicturesListType.insert(std::make_pair(intValueSeek, wxT("work")));
284 lboPictures->SetItem(ListCtrlIndex, 1, _("Work"));
288 PicturesListTokens.insert(std::make_pair(intValueSeek, PropertyTokens));