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 )
27 // Bring up window for adding a picture.
29 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
30 frameCEPicture->SetEditorMode(FALSE);
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;
49 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
52 // Bring up window for modifying a picture.
54 long longSelected = -1;
55 int intSelectedData = 0;
57 if (!GetSelectedItem(lboPictures,
63 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
64 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
65 &ContactEditorData.PicturesListAltID,
66 &ContactEditorData.PicturesListPID,
67 &ContactEditorData.PicturesListType,
68 &ContactEditorData.PicturesListPicEncType,
69 &ContactEditorData.PicturesListPictureType,
70 &ContactEditorData.PicturesListTokens,
71 &ContactEditorData.PicturesListMediatype,
72 &ContactEditorData.PicturesListPref,
76 frameCEPicture->SetEditorMode(TRUE);
77 frameCEPicture->ShowModal();
78 delete frameCEPicture;
79 frameCEPicture = NULL;
83 void frmContactEditor::DeletePicture( wxCommandEvent& event )
86 // Bring up window for deleting a picture.
88 long longSelected = -1;
89 int intSelectedData = 0;
91 if (!GetSelectedItem(lboPictures,
97 lboPictures->DeleteItem(longSelected);
99 DeleteMapData(intSelectedData,
100 &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
101 &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
102 &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
104 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
105 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
106 PictureImage = (wxBitmap)misc_emptyimage_png;
108 imgPicture->SetBitmap(PictureImage);
112 void frmContactEditor::LoadPicture( wxListEvent& event )
117 long longSelected = -1;
118 int intSelectedData = 0;
120 if (!GetSelectedItem(lboPictures,
126 std::string base64dec;
128 std::map<int,std::string>::iterator initier;
129 std::map<int,wxString>::iterator pictypeiter;
131 initier = ContactEditorData.PicturesList.find(intSelectedData);
132 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
134 base64dec = base64_decode(initier->second);
136 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
139 if (pictypeiter->second == wxT("image/jpeg")){
141 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
147 } else if (pictypeiter->second == wxT("image/png")){
149 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
155 } else if (pictypeiter->second == wxT("image/gif")){
157 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
169 PictureImage = (wxBitmap)photo;
170 imgPicture->SetBitmap(PictureImage);
174 void frmContactEditor::SavePicture( wxCommandEvent &event )
177 // Save the picture to a file.
179 long longSelected = -1;
180 int intSelectedData = 0;
182 if (!GetSelectedItem(lboPictures,
192 wxString FinalFilename;
194 std::map<int,std::string>::iterator initier;
195 std::map<int,wxString>::iterator pictypeiter;
197 initier = ContactEditorData.PicturesList.find(intSelectedData);
198 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
200 if (pictypeiter->second == wxT("image/jpeg")){
202 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
204 } else if (pictypeiter->second == wxT("image/png")){
206 FileTypes.Append("PNG Image (*.png)|*.png");
208 } else if (pictypeiter->second == wxT("image/gif")){
210 FileTypes.Append("GIF Image (*.gif)|*.gif");
214 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
219 // Open up the dialog to save the picture.
221 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
222 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
224 if (ExportDlg.ShowModal() == wxID_CANCEL){
230 // Write out the picture to the file.
234 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
236 std::string base64dec;
238 initier = ContactEditorData.PicturesList.find(intSelectedData);
239 base64dec = base64_decode(initier->second);
241 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
247 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);