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 )
28 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
29 frameCEPicture->SetEditorMode(FALSE);
30 intResult = GetLastInt(&ContactEditorData.PicturesList);
31 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
32 &ContactEditorData.PicturesListAltID,
33 &ContactEditorData.PicturesListPID,
34 &ContactEditorData.PicturesListType,
35 &ContactEditorData.PicturesListPicEncType,
36 &ContactEditorData.PicturesListPictureType,
37 &ContactEditorData.PicturesListTokens,
38 &ContactEditorData.PicturesListMediatype,
39 &ContactEditorData.PicturesListPref,
43 frameCEPicture->ShowModal();
44 delete frameCEPicture;
45 frameCEPicture = NULL;
48 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
50 long longSelected = -1;
51 int intSelectedData = 0;
53 if (!GetSelectedItem(lboPictures,
59 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
60 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
61 &ContactEditorData.PicturesListAltID,
62 &ContactEditorData.PicturesListPID,
63 &ContactEditorData.PicturesListType,
64 &ContactEditorData.PicturesListPicEncType,
65 &ContactEditorData.PicturesListPictureType,
66 &ContactEditorData.PicturesListTokens,
67 &ContactEditorData.PicturesListMediatype,
68 &ContactEditorData.PicturesListPref,
72 frameCEPicture->SetEditorMode(TRUE);
73 frameCEPicture->ShowModal();
74 delete frameCEPicture;
75 frameCEPicture = NULL;
79 void frmContactEditor::DeletePicture( wxCommandEvent& event )
81 long longSelected = -1;
82 int intSelectedData = 0;
84 if (!GetSelectedItem(lboPictures,
90 lboPictures->DeleteItem(longSelected);
92 DeleteMapData(intSelectedData,
93 &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
94 &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
95 &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
97 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
98 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
99 PictureImage = (wxBitmap)misc_emptyimage_png;
101 imgPicture->SetBitmap(PictureImage);
104 void frmContactEditor::LoadPicture( wxListEvent& event )
107 long longSelected = -1;
108 int intSelectedData = 0;
110 if (!GetSelectedItem(lboPictures,
116 std::string base64dec;
118 std::map<int,std::string>::iterator initier;
119 std::map<int,wxString>::iterator pictypeiter;
121 initier = ContactEditorData.PicturesList.find(intSelectedData);
122 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
124 base64dec = base64_decode(initier->second);
126 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
128 //wxMemoryInputStream istream(base64dec, (size_t)base64dec.size());
130 if (pictypeiter->second == wxT("image/jpeg")){
132 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
138 } else if (pictypeiter->second == wxT("image/png")){
140 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
146 } else if (pictypeiter->second == wxT("image/gif")){
148 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
160 PictureImage = (wxBitmap)photo;
161 imgPicture->SetBitmap(PictureImage);
165 void frmContactEditor::SavePicture( wxCommandEvent &event )
168 long longSelected = -1;
169 int intSelectedData = 0;
171 if (!GetSelectedItem(lboPictures,
181 wxString FinalFilename;
183 std::map<int,std::string>::iterator initier;
184 std::map<int,wxString>::iterator pictypeiter;
186 initier = ContactEditorData.PicturesList.find(intSelectedData);
187 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
189 if (pictypeiter->second == wxT("image/jpeg")){
191 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
193 } else if (pictypeiter->second == wxT("image/png")){
195 FileTypes.Append("PNG Image (*.png)|*.png");
197 } else if (pictypeiter->second == wxT("image/gif")){
199 FileTypes.Append("GIF Image (*.gif)|*.gif");
203 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
208 // Open up the dialog to save the picture.
210 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
211 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
213 if (ExportDlg.ShowModal() == wxID_CANCEL){
219 // Write out the picture to the file.
223 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
225 std::string base64dec;
227 initier = ContactEditorData.PicturesList.find(intSelectedData);
228 base64dec = base64_decode(initier->second);
230 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
236 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);