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"
33 frmContactEditorPicture::frmContactEditorPicture( wxWindow* parent )
35 frmContactEditorPictureADT( parent )
38 sliPriority->Disable();
40 cmbType->Append(wxT(""));
41 cmbType->Append(_("Home"));
42 cmbType->Append(_("Work"));
45 void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
47 if (chkUsePref->IsChecked()){
48 sliPriority->Enable();
50 sliPriority->Disable();
54 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
58 if (EditorMode == FALSE){
60 // Load the file into memory.
63 wxString wxSPictureFilename;
64 wxString wxSPictureMIMEType;
68 wxSPictureFilename = fipPicture->GetPath();
71 #if wxABI_VERSION < 20900
72 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
74 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
78 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
80 len = PictureFile.Length();
82 unsigned char* PictureFileData = new unsigned char[len];
83 flen = PictureFile.Read(PictureFileData, len);
85 // Check to make sure it is an audio file.
87 wxMemoryInputStream istream(PictureFileData, flen);
90 //wxBitmap (bigimgs_contactpersonicon48i, -1);
94 /*const char *MIMEType;
95 magic_t MagicCookie = magic_open(MAGIC_MIME);
96 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
100 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
105 if (magic_load(MagicCookie, NULL) != 0) {
107 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
112 MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
113 wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
114 magic_close(MagicCookie);*/
116 wxSPictureMIMEType = GetMIME(wxSPictureFilename);
118 if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
120 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
126 // Covert the file into base64.
128 b64enc = base64_encode(PictureFileData, (int)len);
130 delete[] PictureFileData;
134 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
140 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
145 strValue = cmbType->GetString(cmbType->GetSelection());
147 // Setup Picture Type.
149 if (strValue == _("Home")) {
151 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
153 } else if (strValue == _("Work")) {
155 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
159 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
163 // Setup Organisation Priority.
165 if (chkUsePref->IsChecked()){
167 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
171 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
177 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
179 // Setup encoding type.
181 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
187 coldata.SetId(PictureListIndex);
188 coldata.SetData(PictureListIndex);
189 coldata.SetText(_("Picture"));
190 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
192 if (strValue == _("Home")) {
194 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
196 } else if (strValue == _("Work")) {
198 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
206 if (chkUsePref->IsChecked()){
208 PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
214 } else if (EditorMode == TRUE) {
216 // Do not touch the audio file but update the settings
217 // associated with the audio file.
219 long longSelected = -1;
222 // Update Picture Type.
224 PictureListTypePtr->erase(PictureListIndex);
225 strValue = cmbType->GetString(cmbType->GetSelection());
227 if (strValue == _("Home")) {
229 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
231 } else if (strValue == _("Work")) {
233 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
237 // Update Category Priority.
239 PictureListPrefPtr->erase(PictureListIndex);
241 if (chkUsePref->IsChecked()){
243 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
247 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
253 longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
255 wxLIST_STATE_SELECTED);
257 if (longSelected == -1){
261 PictureListCtrlPtr->SetItem(longSelected, 0, wxT("Picture"));
262 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
264 if (chkUsePref->IsChecked()){
266 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
270 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
279 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
284 void frmContactEditorPicture::SetEditorMode(bool EditMode)
286 // Set if the editor is adding or editing an address.
290 if (EditMode == FALSE){
293 btnAction->SetLabel(_("Add"));
294 this->SetTitle(_("Add Picture"));
296 } else if (EditMode == TRUE){
299 btnAction->SetLabel(_("Modify"));
300 this->SetTitle(_("Modify Picture"));
301 fipPicture->Disable();
303 std::map<int,int>::iterator intiter;
304 std::map<int,wxString>::iterator striter;
307 // Load the data into the form. Get the website.
309 striter = PictureListTypePtr->find(PictureListIndex);
311 if (striter->first == PictureListIndex){
313 strValue = striter->second;
317 if (strValue == wxT("home")){
319 cmbType->SetSelection(1);
321 } else if (strValue == wxT("work")){
323 cmbType->SetSelection(2);
327 cmbType->SetSelection(0);
333 // Get the website priority.
335 intiter = PictureListPrefPtr->find(PictureListIndex);
337 if (intiter->first == PictureListIndex && intiter->second > 0){
339 sliPriority->SetValue(intiter->second);
340 sliPriority->Enable();
341 chkUsePref->SetValue(TRUE);
349 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
350 std::map<int, wxString> *PictureListAltID,
351 std::map<int, wxString> *PictureListPID,
352 std::map<int, wxString> *PictureListType,
353 std::map<int, wxString> *PictureListPicEncType,
354 std::map<int, wxString> *PictureListPictureType,
355 std::map<int, wxString> *PictureListTokens,
356 std::map<int, wxString> *PictureListMediatype,
357 std::map<int, int> *PictureListPref,
358 wxListCtrl *PictureListCtrl,
362 PictureListPtr = PictureList;
363 PictureListAltIDPtr = PictureListAltID;
364 PictureListPIDPtr = PictureListPID;
365 PictureListPicEncTypePtr = PictureListPicEncType;
366 PictureListPictureTypePtr = PictureListPictureType;
367 PictureListTypePtr = PictureListType;
368 PictureListTokensPtr = PictureListTokens;
369 PictureListMediatypePtr = PictureListMediatype;
370 PictureListPrefPtr = PictureListPref;
371 PictureListCtrlPtr = PictureListCtrl;
372 PictureListIndex = PictureIndex;