1 #include "frmContactEditorPicture.h"
4 #include <wx/tokenzr.h>
6 #include <wx/mstream.h>
8 #include <SFML/Audio.hpp>
10 #include "../common/textprocessing.h"
11 #include "../common/base64.h"
12 #include "../common/mime.h"
15 frmContactEditorPicture::frmContactEditorPicture( wxWindow* parent )
17 frmContactEditorPictureADT( parent )
20 sliPriority->Disable();
22 cmbType->Append(wxT(""));
23 cmbType->Append(_("Home"));
24 cmbType->Append(_("Work"));
27 void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
29 if (chkUsePref->IsChecked()){
30 sliPriority->Enable();
32 sliPriority->Disable();
36 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
40 if (EditorMode == FALSE){
42 // Load the file into memory.
45 wxString wxSPictureFilename;
46 wxString wxSPictureMIMEType;
50 wxSPictureFilename = fipPicture->GetPath();
53 #if wxABI_VERSION < 20900
54 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
56 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
60 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
62 len = PictureFile.Length();
64 unsigned char* PictureFileData = new unsigned char[len];
65 flen = PictureFile.Read(PictureFileData, len);
67 // Check to make sure it is an audio file.
69 wxMemoryInputStream istream(PictureFileData, flen);
72 //wxBitmap (bigimgs_contactpersonicon48i, -1);
76 /*const char *MIMEType;
77 magic_t MagicCookie = magic_open(MAGIC_MIME);
78 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
82 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
87 if (magic_load(MagicCookie, NULL) != 0) {
89 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
94 MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
95 wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
96 magic_close(MagicCookie);*/
98 wxSPictureMIMEType = GetMIME(wxSPictureFilename);
100 if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
102 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
108 // Covert the file into base64.
110 b64enc = base64_encode(PictureFileData, (int)len);
112 delete[] PictureFileData;
116 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
122 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
127 strValue = cmbType->GetString(cmbType->GetSelection());
129 // Setup Picture Type.
131 if (strValue == _("Home")) {
133 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
135 } else if (strValue == _("Work")) {
137 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
141 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
145 // Setup Organisation Priority.
147 if (chkUsePref->IsChecked()){
149 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
153 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
159 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
161 // Setup encoding type.
163 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
169 coldata.SetId(PictureListIndex);
170 coldata.SetData(PictureListIndex);
171 coldata.SetText(_("Picture"));
172 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
174 if (strValue == _("Home")) {
176 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
178 } else if (strValue == _("Work")) {
180 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
188 if (chkUsePref->IsChecked()){
190 PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
196 } else if (EditorMode == TRUE) {
198 // Do not touch the audio file but update the settings
199 // associated with the audio file.
201 long longSelected = -1;
204 // Update Picture Type.
206 PictureListTypePtr->erase(PictureListIndex);
207 strValue = cmbType->GetString(cmbType->GetSelection());
209 if (strValue == _("Home")) {
211 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
213 } else if (strValue == _("Work")) {
215 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
219 // Update Category Priority.
221 PictureListPrefPtr->erase(PictureListIndex);
223 if (chkUsePref->IsChecked()){
225 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
229 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
235 longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
237 wxLIST_STATE_SELECTED);
239 if (longSelected == -1){
243 PictureListCtrlPtr->SetItem(longSelected, 0, wxT("Picture"));
244 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
246 if (chkUsePref->IsChecked()){
248 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
252 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
261 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
266 void frmContactEditorPicture::SetEditorMode(bool EditMode)
268 // Set if the editor is adding or editing an address.
272 if (EditMode == FALSE){
275 btnAction->SetLabel(_("Add"));
276 this->SetTitle(_("Add Picture"));
278 } else if (EditMode == TRUE){
281 btnAction->SetLabel(_("Modify"));
282 this->SetTitle(_("Modify Picture"));
283 fipPicture->Disable();
285 std::map<int,int>::iterator intiter;
286 std::map<int,wxString>::iterator striter;
289 // Load the data into the form. Get the website.
291 striter = PictureListTypePtr->find(PictureListIndex);
293 if (striter->first == PictureListIndex){
295 strValue = striter->second;
299 if (strValue == wxT("home")){
301 cmbType->SetSelection(1);
303 } else if (strValue == wxT("work")){
305 cmbType->SetSelection(2);
309 cmbType->SetSelection(0);
315 // Get the website priority.
317 intiter = PictureListPrefPtr->find(PictureListIndex);
319 if (intiter->first == PictureListIndex && intiter->second > 0){
321 sliPriority->SetValue(intiter->second);
322 sliPriority->Enable();
323 chkUsePref->SetValue(TRUE);
331 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
332 std::map<int, wxString> *PictureListAltID,
333 std::map<int, wxString> *PictureListPID,
334 std::map<int, wxString> *PictureListType,
335 std::map<int, wxString> *PictureListPicEncType,
336 std::map<int, wxString> *PictureListPictureType,
337 std::map<int, wxString> *PictureListTokens,
338 std::map<int, wxString> *PictureListMediatype,
339 std::map<int, int> *PictureListPref,
340 wxListCtrl *PictureListCtrl,
344 PictureListPtr = PictureList;
345 PictureListAltIDPtr = PictureListAltID;
346 PictureListPIDPtr = PictureListPID;
347 PictureListPicEncTypePtr = PictureListPicEncType;
348 PictureListPictureTypePtr = PictureListPictureType;
349 PictureListTypePtr = PictureListType;
350 PictureListTokensPtr = PictureListTokens;
351 PictureListMediatypePtr = PictureListMediatype;
352 PictureListPrefPtr = PictureListPref;
353 PictureListCtrlPtr = PictureListCtrl;
354 PictureListIndex = PictureIndex;