1 // frmContactEditor-Picture.cpp - frmContactEditor Picture tab subroutines.
3 // (c) 2012-2016 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 "frmContactEditor.h"
21 #include "frmContactEditorPicture.h"
22 #include "../common/base64.h"
24 void frmContactEditor::AddPicture( wxCommandEvent& event )
29 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
30 frameCEPicture->SetEditorMode(FALSE);
31 intResult = GetLastInt(&ContactEditorData.PicturesList);
32 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
33 &ContactEditorData.PicturesListAltID,
34 &ContactEditorData.PicturesListPID,
35 &ContactEditorData.PicturesListType,
36 &ContactEditorData.PicturesListPicEncType,
37 &ContactEditorData.PicturesListPictureType,
38 &ContactEditorData.PicturesListTokens,
39 &ContactEditorData.PicturesListMediatype,
40 &ContactEditorData.PicturesListPref,
44 frameCEPicture->ShowModal();
45 delete frameCEPicture;
46 frameCEPicture = NULL;
50 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
53 long longSelected = -1;
54 int intSelectedData = 0;
56 if (!GetSelectedItem(lboPictures,
62 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
63 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
64 &ContactEditorData.PicturesListAltID,
65 &ContactEditorData.PicturesListPID,
66 &ContactEditorData.PicturesListType,
67 &ContactEditorData.PicturesListPicEncType,
68 &ContactEditorData.PicturesListPictureType,
69 &ContactEditorData.PicturesListTokens,
70 &ContactEditorData.PicturesListMediatype,
71 &ContactEditorData.PicturesListPref,
75 frameCEPicture->SetEditorMode(TRUE);
76 frameCEPicture->ShowModal();
77 delete frameCEPicture;
78 frameCEPicture = NULL;
82 void frmContactEditor::DeletePicture( wxCommandEvent& event )
85 long longSelected = -1;
86 int intSelectedData = 0;
88 if (!GetSelectedItem(lboPictures,
94 lboPictures->DeleteItem(longSelected);
96 DeleteMapData(intSelectedData,
97 &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
98 &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
99 &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
101 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
102 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
103 PictureImage = (wxBitmap)misc_emptyimage_png;
105 imgPicture->SetBitmap(PictureImage);
109 void frmContactEditor::LoadPicture( wxListEvent& event )
112 long longSelected = -1;
113 int intSelectedData = 0;
115 if (!GetSelectedItem(lboPictures,
121 std::string base64dec;
123 std::map<int,std::string>::iterator initier;
124 std::map<int,wxString>::iterator pictypeiter;
126 initier = ContactEditorData.PicturesList.find(intSelectedData);
127 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
129 base64dec = base64_decode(initier->second);
131 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
133 //wxMemoryInputStream istream(base64dec, (size_t)base64dec.size());
135 if (pictypeiter->second == wxT("image/jpeg")){
137 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
143 } else if (pictypeiter->second == wxT("image/png")){
145 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
151 } else if (pictypeiter->second == wxT("image/gif")){
153 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
165 PictureImage = (wxBitmap)photo;
166 imgPicture->SetBitmap(PictureImage);
170 void frmContactEditor::SavePicture( wxCommandEvent &event )
173 long longSelected = -1;
174 int intSelectedData = 0;
176 if (!GetSelectedItem(lboPictures,
186 wxString FinalFilename;
188 std::map<int,std::string>::iterator initier;
189 std::map<int,wxString>::iterator pictypeiter;
191 initier = ContactEditorData.PicturesList.find(intSelectedData);
192 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
194 if (pictypeiter->second == wxT("image/jpeg")){
196 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
198 } else if (pictypeiter->second == wxT("image/png")){
200 FileTypes.Append("PNG Image (*.png)|*.png");
202 } else if (pictypeiter->second == wxT("image/gif")){
204 FileTypes.Append("GIF Image (*.gif)|*.gif");
208 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
213 // Open up the dialog to save the picture.
215 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
216 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
218 if (ExportDlg.ShowModal() == wxID_CANCEL){
224 // Write out the picture to the file.
228 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
230 std::string base64dec;
232 initier = ContactEditorData.PicturesList.find(intSelectedData);
233 base64dec = base64_decode(initier->second);
235 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
241 wxMessageBox(_("An error occured whilst saving the picture. Check that you have free space and permissions to write the picture at this location."), _("Picture save error"), wxOK, this);