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){
272 PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
276 PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
280 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
282 if (chkUsePref->IsChecked()){
284 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
288 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
297 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
302 void frmContactEditorPicture::SetEditorMode(bool EditMode)
304 // Set if the editor is adding or editing an address.
308 if (EditMode == FALSE){
311 btnAction->SetLabel(_("Add"));
312 this->SetTitle(_("Add Picture"));
314 } else if (EditMode == TRUE){
317 btnAction->SetLabel(_("Modify"));
318 this->SetTitle(_("Modify Picture"));
319 fipPicture->Disable();
321 std::map<int,int>::iterator intiter;
322 std::map<int,wxString>::iterator striter;
325 // Load the data into the form. Get the website.
327 striter = PictureListTypePtr->find(PictureListIndex);
329 if (striter->first == PictureListIndex){
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){
357 sliPriority->SetValue(intiter->second);
358 sliPriority->Enable();
359 chkUsePref->SetValue(TRUE);
367 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
368 std::map<int, wxString> *PictureListAltID,
369 std::map<int, wxString> *PictureListPID,
370 std::map<int, wxString> *PictureListType,
371 std::map<int, wxString> *PictureListPicEncType,
372 std::map<int, wxString> *PictureListPictureType,
373 std::map<int, wxString> *PictureListTokens,
374 std::map<int, wxString> *PictureListMediatype,
375 std::map<int, int> *PictureListPref,
376 wxListCtrl *PictureListCtrl,
381 PictureListPtr = PictureList;
382 PictureListAltIDPtr = PictureListAltID;
383 PictureListPIDPtr = PictureListPID;
384 PictureListPicEncTypePtr = PictureListPicEncType;
385 PictureListPictureTypePtr = PictureListPictureType;
386 PictureListTypePtr = PictureListType;
387 PictureListTokensPtr = PictureListTokens;
388 PictureListMediatypePtr = PictureListMediatype;
389 PictureListPrefPtr = PictureListPref;
390 PictureListCtrlPtr = PictureListCtrl;
391 PictureListIndex = PictureIndex;
392 PictureLogo = PictureLogoIn;