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.
31 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
32 frameCEPicture->SetEditorMode(FALSE);
33 intResult = GetLastInt(&ContactEditorData.PicturesList);
34 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
35 &ContactEditorData.PicturesListAltID,
36 &ContactEditorData.PicturesListPID,
37 &ContactEditorData.PicturesListType,
38 &ContactEditorData.PicturesListPicEncType,
39 &ContactEditorData.PicturesListPictureType,
40 &ContactEditorData.PicturesListTokens,
41 &ContactEditorData.PicturesListMediatype,
42 &ContactEditorData.PicturesListPref,
46 frameCEPicture->ShowModal();
47 delete frameCEPicture;
48 frameCEPicture = NULL;
52 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
55 // Bring up window for modifying a picture.
57 long longSelected = -1;
58 int intSelectedData = 0;
60 if (!GetSelectedItem(lboPictures,
66 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
67 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
68 &ContactEditorData.PicturesListAltID,
69 &ContactEditorData.PicturesListPID,
70 &ContactEditorData.PicturesListType,
71 &ContactEditorData.PicturesListPicEncType,
72 &ContactEditorData.PicturesListPictureType,
73 &ContactEditorData.PicturesListTokens,
74 &ContactEditorData.PicturesListMediatype,
75 &ContactEditorData.PicturesListPref,
79 frameCEPicture->SetEditorMode(TRUE);
80 frameCEPicture->ShowModal();
81 delete frameCEPicture;
82 frameCEPicture = NULL;
86 void frmContactEditor::DeletePicture( wxCommandEvent& event )
89 // Bring up window for deleting a picture.
91 long longSelected = -1;
92 int intSelectedData = 0;
94 if (!GetSelectedItem(lboPictures,
100 lboPictures->DeleteItem(longSelected);
102 DeleteMapData(intSelectedData,
103 &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
104 &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
105 &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
107 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
108 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
109 PictureImage = (wxBitmap)misc_emptyimage_png;
111 imgPicture->SetBitmap(PictureImage);
115 void frmContactEditor::LoadPicture( wxListEvent& event )
120 long longSelected = -1;
121 int intSelectedData = 0;
123 if (!GetSelectedItem(lboPictures,
129 std::string base64dec;
131 std::map<int,std::string>::iterator initier;
132 std::map<int,wxString>::iterator pictypeiter;
134 initier = ContactEditorData.PicturesList.find(intSelectedData);
135 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
137 base64dec = base64_decode(initier->second);
139 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
142 if (pictypeiter->second == wxT("image/jpeg")){
144 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
150 } else if (pictypeiter->second == wxT("image/png")){
152 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
158 } else if (pictypeiter->second == wxT("image/gif")){
160 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
172 PictureImage = (wxBitmap)photo;
173 imgPicture->SetBitmap(PictureImage);
177 void frmContactEditor::SavePicture( wxCommandEvent &event )
180 // Save the picture to a file.
182 long longSelected = -1;
183 int intSelectedData = 0;
185 if (!GetSelectedItem(lboPictures,
195 wxString FinalFilename;
197 std::map<int,std::string>::iterator initier;
198 std::map<int,wxString>::iterator pictypeiter;
200 initier = ContactEditorData.PicturesList.find(intSelectedData);
201 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
203 if (pictypeiter->second == wxT("image/jpeg")){
205 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
207 } else if (pictypeiter->second == wxT("image/png")){
209 FileTypes.Append("PNG Image (*.png)|*.png");
211 } else if (pictypeiter->second == wxT("image/gif")){
213 FileTypes.Append("GIF Image (*.gif)|*.gif");
217 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
222 // Open up the dialog to save the picture.
224 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
225 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
227 if (ExportDlg.ShowModal() == wxID_CANCEL){
233 // Write out the picture to the file.
237 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
239 std::string base64dec;
241 initier = ContactEditorData.PicturesList.find(intSelectedData);
242 base64dec = base64_decode(initier->second);
244 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
250 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);