1 // frmContactEditorPicture.cpp - frmContactEditorPicture form.
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/>
19 #include "frmContactEditorPicture.h"
22 #include <wx/tokenzr.h>
24 #include <wx/mstream.h>
25 #include <wx/fs_mem.h>
26 #include <SFML/Audio.hpp>
28 #include "../common/textprocessing.h"
29 #include "../common/base64.h"
30 #include "../common/mime.h"
32 frmContactEditorPicture::frmContactEditorPicture( wxWindow* parent )
34 frmContactEditorPictureADT( parent )
37 sliPriority->Disable();
39 cmbType->Append(wxT(""));
40 cmbType->Append(_("Home"));
41 cmbType->Append(_("Work"));
44 void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
46 if (chkUsePref->IsChecked()){
47 sliPriority->Enable();
49 sliPriority->Disable();
53 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
57 if (EditorMode == FALSE){
59 // Load the file into memory.
62 wxString wxSPictureFilename;
63 wxString wxSPictureMIMEType;
67 wxSPictureFilename = fipPicture->GetPath();
70 #if wxABI_VERSION < 20900
71 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
73 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
77 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
79 len = PictureFile.Length();
81 unsigned char* PictureFileData = new unsigned char[len];
82 flen = PictureFile.Read(PictureFileData, len);
84 // Check to make sure it is an audio file.
86 wxMemoryInputStream istream(PictureFileData, flen);
89 //wxBitmap (bigimgs_contactpersonicon48i, -1);
93 /*const char *MIMEType;
94 magic_t MagicCookie = magic_open(MAGIC_MIME);
95 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
99 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
104 if (magic_load(MagicCookie, NULL) != 0) {
106 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
111 MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
112 wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
113 magic_close(MagicCookie);*/
115 wxSPictureMIMEType = GetMIME(wxSPictureFilename);
117 if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
119 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
125 // Covert the file into base64.
127 b64enc = base64_encode(PictureFileData, (int)len);
129 delete[] PictureFileData;
133 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
139 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
144 strValue = cmbType->GetString(cmbType->GetSelection());
146 // Setup Picture Type.
148 if (strValue == _("Home")) {
150 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
152 } else if (strValue == _("Work")) {
154 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
158 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
162 // Setup Organisation Priority.
164 if (chkUsePref->IsChecked()){
166 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
170 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
176 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
178 // Setup encoding type.
180 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
186 coldata.SetId(PictureListIndex);
187 coldata.SetData(PictureListIndex);
189 if (PictureLogo == TRUE){
191 coldata.SetText(_("Picture"));
195 coldata.SetText(_("Logo"));
199 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
201 if (strValue == _("Home")) {
203 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
205 } else if (strValue == _("Work")) {
207 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
215 if (chkUsePref->IsChecked()){
217 PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
223 } else if (EditorMode == TRUE) {
225 // Do not touch the audio file but update the settings
226 // associated with the audio file.
228 long longSelected = -1;
231 // Update Picture Type.
233 PictureListTypePtr->erase(PictureListIndex);
234 strValue = cmbType->GetString(cmbType->GetSelection());
236 if (strValue == _("Home")) {
238 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
240 } else if (strValue == _("Work")) {
242 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
246 // Update Category Priority.
248 PictureListPrefPtr->erase(PictureListIndex);
250 if (chkUsePref->IsChecked()){
252 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
256 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
262 longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
264 wxLIST_STATE_SELECTED);
266 if (longSelected == -1){
270 if (PictureLogo == TRUE){
271 PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
275 PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
279 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
281 if (chkUsePref->IsChecked()){
283 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
287 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
296 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
301 void frmContactEditorPicture::SetEditorMode(bool EditMode)
303 // Set if the editor is adding or editing an address.
307 if (EditMode == FALSE){
310 btnAction->SetLabel(_("Add"));
311 this->SetTitle(_("Add Picture"));
313 } else if (EditMode == TRUE){
316 btnAction->SetLabel(_("Modify"));
317 this->SetTitle(_("Modify Picture"));
318 fipPicture->Disable();
320 std::map<int,int>::iterator intiter;
321 std::map<int,wxString>::iterator striter;
324 // Load the data into the form. Get the website.
326 striter = PictureListTypePtr->find(PictureListIndex);
328 if (striter->first == PictureListIndex &&
329 striter != PictureListTypePtr->end()){
331 strValue = striter->second;
335 if (strValue == wxT("home")){
337 cmbType->SetSelection(1);
339 } else if (strValue == wxT("work")){
341 cmbType->SetSelection(2);
345 cmbType->SetSelection(0);
351 // Get the website priority.
353 intiter = PictureListPrefPtr->find(PictureListIndex);
355 if (intiter->first == PictureListIndex && intiter->second > 0 &&
356 intiter != PictureListPrefPtr->end()){
358 sliPriority->SetValue(intiter->second);
359 sliPriority->Enable();
360 chkUsePref->SetValue(TRUE);
368 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
369 std::map<int, wxString> *PictureListAltID,
370 std::map<int, wxString> *PictureListPID,
371 std::map<int, wxString> *PictureListType,
372 std::map<int, wxString> *PictureListPicEncType,
373 std::map<int, wxString> *PictureListPictureType,
374 std::map<int, wxString> *PictureListTokens,
375 std::map<int, wxString> *PictureListMediatype,
376 std::map<int, int> *PictureListPref,
377 wxListCtrl *PictureListCtrl,
382 PictureListPtr = PictureList;
383 PictureListAltIDPtr = PictureListAltID;
384 PictureListPIDPtr = PictureListPID;
385 PictureListPicEncTypePtr = PictureListPicEncType;
386 PictureListPictureTypePtr = PictureListPictureType;
387 PictureListTypePtr = PictureListType;
388 PictureListTokensPtr = PictureListTokens;
389 PictureListMediatypePtr = PictureListMediatype;
390 PictureListPrefPtr = PictureListPref;
391 PictureListCtrlPtr = PictureListCtrl;
392 PictureListIndex = PictureIndex;
393 PictureLogo = PictureLogoIn;