1 // frmContactEditor-Picture.cpp - frmContactEditor Picture tab 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 "frmContactEditor.h"
21 #include "frmContactEditorPicture.h"
22 #include "../common/base64.h"
24 void frmContactEditor::AddPicture( wxCommandEvent& event )
28 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
29 frameCEPicture->SetEditorMode(FALSE);
30 intResult = GetLastInt(&PicturesList);
31 frameCEPicture->SetupPointers(&PicturesList,
35 &PicturesListPicEncType,
36 &PicturesListPictureType,
38 &PicturesListMediatype,
42 frameCEPicture->ShowModal();
43 delete frameCEPicture;
44 frameCEPicture = NULL;
47 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
49 long longSelected = -1;
50 int intSelectedData = 0;
52 if (!GetSelectedItem(lboPictures,
58 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
59 frameCEPicture->SetupPointers(&PicturesList,
63 &PicturesListPicEncType,
64 &PicturesListPictureType,
66 &PicturesListMediatype,
70 frameCEPicture->SetEditorMode(TRUE);
71 frameCEPicture->ShowModal();
72 delete frameCEPicture;
73 frameCEPicture = NULL;
77 void frmContactEditor::DeletePicture( wxCommandEvent& event )
79 long longSelected = -1;
80 int intSelectedData = 0;
82 if (!GetSelectedItem(lboPictures,
88 lboPictures->DeleteItem(longSelected);
90 DeleteMapData(intSelectedData,
91 &PicturesList, &PicturesListAltID, &PicturesListPID, &PicturesListType,
92 &PicturesListPicEncType, &PicturesListPictureType, &PicturesListTokens,
93 &PicturesListMediatype, &PicturesListPref);
95 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
96 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
97 PictureImage = (wxBitmap)misc_emptyimage_png;
99 imgPicture->SetBitmap(PictureImage);
102 void frmContactEditor::LoadPicture( wxListEvent& event )
105 long longSelected = -1;
106 int intSelectedData = 0;
108 if (!GetSelectedItem(lboPictures,
114 std::string base64dec;
116 std::map<int,std::string>::iterator initier;
117 std::map<int,wxString>::iterator pictypeiter;
119 initier = ContactEditorData.PicturesList.find(intSelectedData);
120 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
122 base64dec = base64_decode(initier->second);
124 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
126 //wxMemoryInputStream istream(base64dec, (size_t)base64dec.size());
128 if (pictypeiter->second == wxT("image/jpeg")){
130 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
136 } else if (pictypeiter->second == wxT("image/png")){
138 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
144 } else if (pictypeiter->second == wxT("image/gif")){
146 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
158 PictureImage = (wxBitmap)photo;
159 imgPicture->SetBitmap(PictureImage);
163 void frmContactEditor::SavePicture( wxCommandEvent &event )
166 long longSelected = -1;
167 int intSelectedData = 0;
169 if (!GetSelectedItem(lboPictures,
179 wxString FinalFilename;
181 std::map<int,std::string>::iterator initier;
182 std::map<int,wxString>::iterator pictypeiter;
184 initier = ContactEditorData.PicturesList.find(intSelectedData);
185 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
187 if (pictypeiter->second == wxT("image/jpeg")){
189 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
191 } else if (pictypeiter->second == wxT("image/png")){
193 FileTypes.Append("PNG Image (*.png)|*.png");
195 } else if (pictypeiter->second == wxT("image/gif")){
197 FileTypes.Append("GIF Image (*.gif)|*.gif");
201 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
206 // Open up the dialog to save the picture.
208 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
209 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
211 if (ExportDlg.ShowModal() == wxID_CANCEL){
217 // Write out the picture to the file.
221 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
223 std::string base64dec;
225 initier = ContactEditorData.PicturesList.find(intSelectedData);
226 base64dec = base64_decode(initier->second);
228 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
234 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);