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 )
40 priorityCtrl = new XABPriorityCtrl(tabGeneral);
41 szrGeneral->Add(priorityCtrl, 1, wxEXPAND, 5);
43 cmbType->Append(wxT(""));
44 cmbType->Append(_("Home"));
45 cmbType->Append(_("Work"));
49 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
56 if (EditorMode == FALSE){
58 // Load the file into memory.
61 wxString wxSPictureFilename;
62 wxString wxSPictureMIMEType;
66 wxSPictureFilename = fipPicture->GetPath();
69 #if wxABI_VERSION < 20900
70 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
72 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
76 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
78 len = PictureFile.Length();
80 unsigned char* PictureFileData = new unsigned char[len];
81 flen = PictureFile.Read(PictureFileData, len);
83 // Check to make sure it is an audio file.
85 wxMemoryInputStream istream(PictureFileData, flen);
88 //wxBitmap (bigimgs_contactpersonicon48i, -1);
92 /*const char *MIMEType;
93 magic_t MagicCookie = magic_open(MAGIC_MIME);
94 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
98 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
103 if (magic_load(MagicCookie, NULL) != 0) {
105 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
110 MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
111 wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
112 magic_close(MagicCookie);*/
114 wxSPictureMIMEType = GetMIME(wxSPictureFilename);
116 if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
118 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
124 // Covert the file into base64.
126 b64enc = base64_encode(PictureFileData, (int)len);
128 delete[] PictureFileData;
132 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
138 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
143 strValue = cmbType->GetString(cmbType->GetSelection());
145 // Setup Picture Type.
147 if (strValue == _("Home")) {
149 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
151 } else if (strValue == _("Work")) {
153 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
157 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
161 // Setup Organisation Priority.
163 if (priorityCtrl->IsPriorityChecked()){
165 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, priorityCtrl->GetValue()));
169 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
175 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
177 // Setup encoding type.
179 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
185 coldata.SetId(PictureListIndex);
186 coldata.SetData(PictureListIndex);
188 if (PictureLogo == TRUE){
190 coldata.SetText(_("Picture"));
194 coldata.SetText(_("Logo"));
198 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
200 if (strValue == _("Home")) {
202 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
204 } else if (strValue == _("Work")) {
206 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
214 if (priorityCtrl->IsPriorityChecked()){
216 PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
222 } else if (EditorMode == TRUE) {
224 // Do not touch the audio file but update the settings
225 // associated with the audio file.
227 long longSelected = -1;
230 // Update Picture Type.
232 PictureListTypePtr->erase(PictureListIndex);
233 strValue = cmbType->GetString(cmbType->GetSelection());
235 if (strValue == _("Home")) {
237 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
239 } else if (strValue == _("Work")) {
241 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
245 // Update Category Priority.
247 PictureListPrefPtr->erase(PictureListIndex);
249 if (priorityCtrl->IsPriorityChecked()){
251 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, priorityCtrl->GetValue()));
255 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
261 longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
263 wxLIST_STATE_SELECTED);
265 if (longSelected == -1){
269 if (PictureLogo == TRUE){
270 PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
274 PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
278 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
280 if (priorityCtrl->IsPriorityChecked()){
282 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), priorityCtrl->GetValue()));
286 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
295 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
298 // Close this window.
304 void frmContactEditorPicture::SetEditorMode(bool EditMode)
307 // Set the editor mode.
309 // Set if the editor is adding or editing an address.
313 if (EditMode == FALSE){
316 btnAction->SetLabel(_("Add"));
317 this->SetTitle(_("Add Picture"));
319 } else if (EditMode == TRUE){
322 btnAction->SetLabel(_("Modify"));
323 this->SetTitle(_("Modify Picture"));
324 fipPicture->Disable();
326 std::map<int,int>::iterator intiter;
327 std::map<int,wxString>::iterator striter;
330 // Load the data into the form. Get the website.
332 striter = PictureListTypePtr->find(PictureListIndex);
334 if (striter->first == PictureListIndex &&
335 striter != PictureListTypePtr->end()){
337 strValue = striter->second;
341 if (strValue == wxT("home")){
343 cmbType->SetSelection(1);
345 } else if (strValue == wxT("work")){
347 cmbType->SetSelection(2);
351 cmbType->SetSelection(0);
357 // Get the website priority.
359 intiter = PictureListPrefPtr->find(PictureListIndex);
361 if (intiter->first == PictureListIndex && intiter->second > 0 &&
362 intiter != PictureListPrefPtr->end()){
364 priorityCtrl->SetValue(intiter->second);
365 priorityCtrl->EnablePriority(true);
373 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
374 std::map<int, wxString> *PictureListAltID,
375 std::map<int, wxString> *PictureListPID,
376 std::map<int, wxString> *PictureListType,
377 std::map<int, wxString> *PictureListPicEncType,
378 std::map<int, wxString> *PictureListPictureType,
379 std::map<int, wxString> *PictureListTokens,
380 std::map<int, wxString> *PictureListMediatype,
381 std::map<int, int> *PictureListPref,
382 wxListCtrl *PictureListCtrl,
387 // Setup the pointers.
389 PictureListPtr = PictureList;
390 PictureListAltIDPtr = PictureListAltID;
391 PictureListPIDPtr = PictureListPID;
392 PictureListPicEncTypePtr = PictureListPicEncType;
393 PictureListPictureTypePtr = PictureListPictureType;
394 PictureListTypePtr = PictureListType;
395 PictureListTokensPtr = PictureListTokens;
396 PictureListMediatypePtr = PictureListMediatype;
397 PictureListPrefPtr = PictureListPref;
398 PictureListCtrlPtr = PictureListCtrl;
399 PictureListIndex = PictureIndex;
400 PictureLogo = PictureLogoIn;