1 // frmContactEditorSound.cpp - frmContactEditorSound 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 "frmContactEditorSound.h"
21 #include <wx/tokenzr.h>
23 #include <SFML/Audio.hpp>
25 #include "../common/textprocessing.h"
26 #include "../common/base64.h"
27 #include "../common/mime.h"
29 frmContactEditorSound::frmContactEditorSound( wxWindow* parent )
31 frmContactEditorSoundADT( parent )
34 sliPriority->Disable();
36 cmbType->Append(wxT(""));
37 cmbType->Append(_("Home"));
38 cmbType->Append(_("Work"));
41 void frmContactEditorSound::EnablePriority( wxCommandEvent& event )
43 if (chkUsePref->IsChecked()){
44 sliPriority->Enable();
46 sliPriority->Disable();
50 void frmContactEditorSound::ProcessData( wxCommandEvent& event )
54 if (EditorMode == FALSE){
56 // Load the file into memory.
59 wxString wxSAudioString;
60 wxString wxSAudioFilename;
61 wxString wxSAudioMIMEType;
62 sf::SoundBuffer AudioBuffer;
66 wxSAudioFilename = fipSound->GetPath();
69 #if wxABI_VERSION < 20900
70 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
72 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
76 if (AudioFile.Open(wxSAudioFilename, wxFile::read)){
78 len = AudioFile.Length();
80 unsigned char* AudioFileData = new unsigned char[len];
81 flen = AudioFile.Read(AudioFileData, (size_t)len);
85 /*const char *MIMEType;
86 magic_t MagicCookie = magic_open(MAGIC_MIME);
87 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
91 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
96 if (magic_load(MagicCookie, NULL) != 0) {
98 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
103 MIMEType = magic_file(MagicCookie, wxSAudioFilename.mb_str());
104 wxSAudioMIMEType = wxString::FromUTF8(MIMEType);
105 magic_close(MagicCookie);*/
107 wxSAudioMIMEType = GetMIME(wxSAudioFilename);
109 // Check to make sure it is an audio file.
111 if (!AudioBuffer.loadFromMemory(AudioFileData, (size_t)len)){
113 // Can't load file. We must flee...
114 // Also show an error message too.
116 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
122 // Covert the file into base64.
124 b64enc = base64_encode(AudioFileData, (int)len);
126 delete[] AudioFileData;
130 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
136 SoundListPtr->insert(std::make_pair(SoundListIndex, b64enc));
140 strValue = cmbType->GetString(cmbType->GetSelection());
144 if (strValue == _("Home")) {
146 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
148 } else if (strValue == _("Work")) {
150 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
154 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("")));
158 // Setup Organisation Priority.
160 if (chkUsePref->IsChecked()){
162 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
166 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
172 SoundListAudioTypePtr->insert(std::make_pair(SoundListIndex, wxSAudioMIMEType));
174 // Setup Encoding Type.
176 SoundListAudioEncTypePtr->insert(std::make_pair(SoundListIndex, wxT("base64")));
182 coldata.SetId(SoundListIndex);
183 coldata.SetData(SoundListIndex);
184 coldata.SetText(_("Sound"));
185 ListCtrlIndex = SoundListCtrlPtr->InsertItem(coldata);
187 if (strValue == _("Home")) {
189 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
191 } else if (strValue == _("Work")) {
193 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
201 if (chkUsePref->IsChecked()){
203 SoundListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
209 } else if (EditorMode == TRUE) {
211 // Do not touch the audio file but update the settings
212 // associated with the audio file.
216 long longSelected = -1;
219 // Update Category Type.
221 SoundListTypePtr->erase(SoundListIndex);
222 strValue = cmbType->GetString(cmbType->GetSelection());
224 if (strValue == _("Home")) {
226 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
228 } else if (strValue == _("Work")) {
230 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
234 // Update Category Priority.
236 SoundListPrefPtr->erase(SoundListIndex);
238 if (chkUsePref->IsChecked()){
240 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
244 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
250 longSelected = SoundListCtrlPtr->GetNextItem(longSelected,
252 wxLIST_STATE_SELECTED);
254 if (longSelected == -1){
258 SoundListCtrlPtr->SetItem(longSelected, 0, wxT("Sound"));
259 SoundListCtrlPtr->SetItem(longSelected, 1, strValue);
261 if (chkUsePref->IsChecked()){
263 SoundListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
267 SoundListCtrlPtr->SetItem(longSelected, 2, wxT(""));
276 void frmContactEditorSound::CloseWindow( wxCommandEvent& event )
281 void frmContactEditorSound::SetEditorMode(bool EditMode)
283 // Set if the editor is adding or editing an address.
287 if (EditMode == FALSE){
290 btnAction->SetLabel(_("Add"));
291 this->SetTitle(_("Add Sound"));
293 } else if (EditMode == TRUE){
296 btnAction->SetLabel(_("Modify"));
297 this->SetTitle(_("Modify Sound"));
300 std::map<int,int>::iterator intiter;
301 std::map<int,wxString>::iterator striter;
304 // Load the data into the form. Get the website.
306 striter = SoundListTypePtr->find(SoundListIndex);
308 if (striter->first == SoundListIndex){
310 strValue = striter->second;
314 if (strValue == wxT("home")){
316 cmbType->SetSelection(1);
318 } else if (strValue == wxT("work")){
320 cmbType->SetSelection(2);
324 cmbType->SetSelection(0);
330 // Get the website priority.
332 intiter = SoundListPrefPtr->find(SoundListIndex);
334 if (intiter->first == SoundListIndex && intiter->second > 0){
336 sliPriority->SetValue(intiter->second);
337 sliPriority->Enable();
338 chkUsePref->SetValue(TRUE);
348 void frmContactEditorSound::SetupPointers(std::map<int, std::string> *SoundList,
349 std::map<int, wxString> *SoundListAltID,
350 std::map<int, wxString> *SoundListPID,
351 std::map<int, wxString> *SoundListType,
352 std::map<int, wxString> *SoundListAudioType,
353 std::map<int, wxString> *SoundListAudioEncType,
354 std::map<int, wxString> *SoundListTokens,
355 std::map<int, wxString> *SoundListMediatype,
356 std::map<int, int> *SoundListPref,
357 wxListCtrl *SoundListCtrl,
361 SoundListPtr = SoundList;
362 SoundListAltIDPtr = SoundListAltID;
363 SoundListPIDPtr = SoundListPID;
364 SoundListTypePtr = SoundListType;
365 SoundListAudioTypePtr = SoundListAudioType;
366 SoundListAudioEncTypePtr = SoundListAudioEncType;
367 SoundListTokensPtr = SoundListTokens;
368 SoundListMediatypePtr = SoundListMediatype;
369 SoundListPrefPtr = SoundListPref;
370 SoundListCtrlPtr = SoundListCtrl;
371 SoundListIndex = SoundIndex;