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;
50 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
53 // Bring up window for modifying a picture.
55 long longSelected = -1;
56 int intSelectedData = 0;
58 if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
64 frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
65 frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
66 &ContactEditorData.PicturesListAltID,
67 &ContactEditorData.PicturesListPID,
68 &ContactEditorData.PicturesListType,
69 &ContactEditorData.PicturesListPicEncType,
70 &ContactEditorData.PicturesListPictureType,
71 &ContactEditorData.PicturesListTokens,
72 &ContactEditorData.PicturesListMediatype,
73 &ContactEditorData.PicturesListPref,
77 frameCEPicture->SetEditorMode(TRUE);
78 frameCEPicture->ShowModal();
79 delete frameCEPicture;
80 frameCEPicture = NULL;
84 void frmContactEditor::DeletePicture( wxCommandEvent& event )
87 // Bring up window for deleting a picture.
89 long longSelected = -1;
90 int intSelectedData = 0;
92 if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
98 lboPictures->DeleteItem(longSelected);
100 DeleteMapData(intSelectedData,
101 &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
102 &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
103 &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
105 wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
106 wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
107 PictureImage = (wxBitmap)misc_emptyimage_png;
109 imgPicture->SetBitmap(PictureImage);
113 void frmContactEditor::LoadPicture( wxListEvent& event )
118 long longSelected = -1;
119 int intSelectedData = 0;
121 if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
127 std::string base64dec;
129 std::map<int,std::string>::iterator initier;
130 std::map<int,wxString>::iterator pictypeiter;
132 initier = ContactEditorData.PicturesList.find(intSelectedData);
133 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
135 base64dec = base64_decode(initier->second);
137 wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
140 if (pictypeiter->second == wxT("image/jpeg")){
142 if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
148 } else if (pictypeiter->second == wxT("image/png")){
150 if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
156 } else if (pictypeiter->second == wxT("image/gif")){
158 if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
170 PictureImage = (wxBitmap)photo;
171 imgPicture->SetBitmap(PictureImage);
175 void frmContactEditor::SavePicture( wxCommandEvent &event )
178 // Save the picture to a file.
180 long longSelected = -1;
181 int intSelectedData = 0;
183 if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
193 wxString FinalFilename;
195 std::map<int,std::string>::iterator initier;
196 std::map<int,wxString>::iterator pictypeiter;
198 initier = ContactEditorData.PicturesList.find(intSelectedData);
199 pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
201 if (pictypeiter->second == wxT("image/jpeg")){
203 FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
205 } else if (pictypeiter->second == wxT("image/png")){
207 FileTypes.Append("PNG Image (*.png)|*.png");
209 } else if (pictypeiter->second == wxT("image/gif")){
211 FileTypes.Append("GIF Image (*.gif)|*.gif");
215 wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
220 // Open up the dialog to save the picture.
222 wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
223 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
225 if (ExportDlg.ShowModal() == wxID_CANCEL){
231 // Write out the picture to the file.
235 if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
237 std::string base64dec;
239 initier = ContactEditorData.PicturesList.find(intSelectedData);
240 base64dec = base64_decode(initier->second);
242 PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
248 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);