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 sliPriority->Disable();
42 cmbType->Append(wxT(""));
43 cmbType->Append(_("Home"));
44 cmbType->Append(_("Work"));
48 void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
51 // Enable/disable the priority.
53 if (chkUsePref->IsChecked()){
54 sliPriority->Enable();
56 sliPriority->Disable();
60 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
67 if (EditorMode == FALSE){
69 // Load the file into memory.
72 wxString wxSPictureFilename;
73 wxString wxSPictureMIMEType;
77 wxSPictureFilename = fipPicture->GetPath();
80 #if wxABI_VERSION < 20900
81 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
83 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
87 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
89 len = PictureFile.Length();
91 unsigned char* PictureFileData = new unsigned char[len];
92 flen = PictureFile.Read(PictureFileData, len);
94 // Check to make sure it is an audio file.
96 wxMemoryInputStream istream(PictureFileData, flen);
99 //wxBitmap (bigimgs_contactpersonicon48i, -1);
103 /*const char *MIMEType;
104 magic_t MagicCookie = magic_open(MAGIC_MIME);
105 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
109 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
114 if (magic_load(MagicCookie, NULL) != 0) {
116 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
121 MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
122 wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
123 magic_close(MagicCookie);*/
125 wxSPictureMIMEType = GetMIME(wxSPictureFilename);
127 if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
129 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
135 // Covert the file into base64.
137 b64enc = base64_encode(PictureFileData, (int)len);
139 delete[] PictureFileData;
143 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
149 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
154 strValue = cmbType->GetString(cmbType->GetSelection());
156 // Setup Picture Type.
158 if (strValue == _("Home")) {
160 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
162 } else if (strValue == _("Work")) {
164 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
168 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
172 // Setup Organisation Priority.
174 if (chkUsePref->IsChecked()){
176 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
180 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
186 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
188 // Setup encoding type.
190 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
196 coldata.SetId(PictureListIndex);
197 coldata.SetData(PictureListIndex);
199 if (PictureLogo == TRUE){
201 coldata.SetText(_("Picture"));
205 coldata.SetText(_("Logo"));
209 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
211 if (strValue == _("Home")) {
213 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
215 } else if (strValue == _("Work")) {
217 PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
225 if (chkUsePref->IsChecked()){
227 PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
233 } else if (EditorMode == TRUE) {
235 // Do not touch the audio file but update the settings
236 // associated with the audio file.
238 long longSelected = -1;
241 // Update Picture Type.
243 PictureListTypePtr->erase(PictureListIndex);
244 strValue = cmbType->GetString(cmbType->GetSelection());
246 if (strValue == _("Home")) {
248 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
250 } else if (strValue == _("Work")) {
252 PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
256 // Update Category Priority.
258 PictureListPrefPtr->erase(PictureListIndex);
260 if (chkUsePref->IsChecked()){
262 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
266 PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
272 longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
274 wxLIST_STATE_SELECTED);
276 if (longSelected == -1){
280 if (PictureLogo == TRUE){
281 PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
285 PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
289 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
291 if (chkUsePref->IsChecked()){
293 PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
297 PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
306 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
309 // Close this window.
315 void frmContactEditorPicture::SetEditorMode(bool EditMode)
318 // Set the editor mode.
320 // Set if the editor is adding or editing an address.
324 if (EditMode == FALSE){
327 btnAction->SetLabel(_("Add"));
328 this->SetTitle(_("Add Picture"));
330 } else if (EditMode == TRUE){
333 btnAction->SetLabel(_("Modify"));
334 this->SetTitle(_("Modify Picture"));
335 fipPicture->Disable();
337 std::map<int,int>::iterator intiter;
338 std::map<int,wxString>::iterator striter;
341 // Load the data into the form. Get the website.
343 striter = PictureListTypePtr->find(PictureListIndex);
345 if (striter->first == PictureListIndex &&
346 striter != PictureListTypePtr->end()){
348 strValue = striter->second;
352 if (strValue == wxT("home")){
354 cmbType->SetSelection(1);
356 } else if (strValue == wxT("work")){
358 cmbType->SetSelection(2);
362 cmbType->SetSelection(0);
368 // Get the website priority.
370 intiter = PictureListPrefPtr->find(PictureListIndex);
372 if (intiter->first == PictureListIndex && intiter->second > 0 &&
373 intiter != PictureListPrefPtr->end()){
375 sliPriority->SetValue(intiter->second);
376 sliPriority->Enable();
377 chkUsePref->SetValue(TRUE);
385 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,
386 std::map<int, wxString> *PictureListAltID,
387 std::map<int, wxString> *PictureListPID,
388 std::map<int, wxString> *PictureListType,
389 std::map<int, wxString> *PictureListPicEncType,
390 std::map<int, wxString> *PictureListPictureType,
391 std::map<int, wxString> *PictureListTokens,
392 std::map<int, wxString> *PictureListMediatype,
393 std::map<int, int> *PictureListPref,
394 wxListCtrl *PictureListCtrl,
399 // Setup the pointers.
401 PictureListPtr = PictureList;
402 PictureListAltIDPtr = PictureListAltID;
403 PictureListPIDPtr = PictureListPID;
404 PictureListPicEncTypePtr = PictureListPicEncType;
405 PictureListPictureTypePtr = PictureListPictureType;
406 PictureListTypePtr = PictureListType;
407 PictureListTokensPtr = PictureListTokens;
408 PictureListMediatypePtr = PictureListMediatype;
409 PictureListPrefPtr = PictureListPref;
410 PictureListCtrlPtr = PictureListCtrl;
411 PictureListIndex = PictureIndex;
412 PictureLogo = PictureLogoIn;