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 )
37 sliPriority->Disable();
39 cmbType->Append(wxT(""));
40 cmbType->Append(_("Home"));
41 cmbType->Append(_("Work"));
45 void frmContactEditorSound::EnablePriority( wxCommandEvent& event )
48 // Enable/disable the priority setting.
50 if (chkUsePref->IsChecked()){
51 sliPriority->Enable();
53 sliPriority->Disable();
58 void frmContactEditorSound::ProcessData( wxCommandEvent& event )
61 // Process audio data.
65 if (EditorMode == FALSE){
67 // Load the file into memory.
70 wxString wxSAudioString;
71 wxString wxSAudioFilename;
72 wxString wxSAudioMIMEType;
73 sf::SoundBuffer AudioBuffer;
77 wxSAudioFilename = fipSound->GetPath();
80 #if wxABI_VERSION < 20900
81 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
83 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
87 if (AudioFile.Open(wxSAudioFilename, wxFile::read)){
89 len = AudioFile.Length();
91 unsigned char* AudioFileData = new unsigned char[len];
92 flen = AudioFile.Read(AudioFileData, (size_t)len);
96 /*const char *MIMEType;
97 magic_t MagicCookie = magic_open(MAGIC_MIME);
98 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
102 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
107 if (magic_load(MagicCookie, NULL) != 0) {
109 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
114 MIMEType = magic_file(MagicCookie, wxSAudioFilename.mb_str());
115 wxSAudioMIMEType = wxString::FromUTF8(MIMEType);
116 magic_close(MagicCookie);*/
118 wxSAudioMIMEType = GetMIME(wxSAudioFilename);
120 // Check to make sure it is an audio file.
122 if (!AudioBuffer.loadFromMemory(AudioFileData, (size_t)len)){
124 // Can't load file. We must flee...
125 // Also show an error message too.
127 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
133 // Covert the file into base64.
135 b64enc = base64_encode(AudioFileData, (int)len);
137 delete[] AudioFileData;
141 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
147 SoundListPtr->insert(std::make_pair(SoundListIndex, b64enc));
151 strValue = cmbType->GetString(cmbType->GetSelection());
155 if (strValue == _("Home")) {
157 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
159 } else if (strValue == _("Work")) {
161 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
165 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("")));
169 // Setup Organisation Priority.
171 if (chkUsePref->IsChecked()){
173 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
177 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
183 SoundListAudioTypePtr->insert(std::make_pair(SoundListIndex, wxSAudioMIMEType));
185 // Setup Encoding Type.
187 SoundListAudioEncTypePtr->insert(std::make_pair(SoundListIndex, wxT("base64")));
193 coldata.SetId(SoundListIndex);
194 coldata.SetData(SoundListIndex);
195 coldata.SetText(_("Sound"));
196 ListCtrlIndex = SoundListCtrlPtr->InsertItem(coldata);
198 if (strValue == _("Home")) {
200 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
202 } else if (strValue == _("Work")) {
204 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
212 if (chkUsePref->IsChecked()){
214 SoundListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
220 } else if (EditorMode == TRUE) {
222 // Do not touch the audio file but update the settings
223 // associated with the audio file.
227 long longSelected = -1;
230 // Update Category Type.
232 SoundListTypePtr->erase(SoundListIndex);
233 strValue = cmbType->GetString(cmbType->GetSelection());
235 if (strValue == _("Home")) {
237 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
239 } else if (strValue == _("Work")) {
241 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
245 // Update Category Priority.
247 SoundListPrefPtr->erase(SoundListIndex);
249 if (chkUsePref->IsChecked()){
251 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
255 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
261 longSelected = SoundListCtrlPtr->GetNextItem(longSelected,
263 wxLIST_STATE_SELECTED);
265 if (longSelected == -1){
269 SoundListCtrlPtr->SetItem(longSelected, 0, wxT("Sound"));
270 SoundListCtrlPtr->SetItem(longSelected, 1, strValue);
272 if (chkUsePref->IsChecked()){
274 SoundListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
278 SoundListCtrlPtr->SetItem(longSelected, 2, wxT(""));
287 void frmContactEditorSound::CloseWindow( wxCommandEvent& event )
290 // Close this window.
296 void frmContactEditorSound::SetEditorMode(bool EditMode)
299 // Set the editor mode.
301 // Set if the editor is adding or editing an address.
305 if (EditMode == FALSE){
308 btnAction->SetLabel(_("Add"));
309 this->SetTitle(_("Add Sound"));
311 } else if (EditMode == TRUE){
314 btnAction->SetLabel(_("Modify"));
315 this->SetTitle(_("Modify Sound"));
318 std::map<int,int>::iterator intiter;
319 std::map<int,wxString>::iterator striter;
322 // Load the data into the form. Get the website.
324 striter = SoundListTypePtr->find(SoundListIndex);
326 if (striter->first == SoundListIndex){
328 strValue = striter->second;
332 if (strValue == wxT("home")){
334 cmbType->SetSelection(1);
336 } else if (strValue == wxT("work")){
338 cmbType->SetSelection(2);
342 cmbType->SetSelection(0);
348 // Get the website priority.
350 intiter = SoundListPrefPtr->find(SoundListIndex);
352 if (intiter->first == SoundListIndex && intiter->second > 0 &&
353 intiter != SoundListPrefPtr->end()){
355 sliPriority->SetValue(intiter->second);
356 sliPriority->Enable();
357 chkUsePref->SetValue(TRUE);
367 void frmContactEditorSound::SetupPointers(std::map<int, std::string> *SoundList,
368 std::map<int, wxString> *SoundListAltID,
369 std::map<int, wxString> *SoundListPID,
370 std::map<int, wxString> *SoundListType,
371 std::map<int, wxString> *SoundListAudioType,
372 std::map<int, wxString> *SoundListAudioEncType,
373 std::map<int, wxString> *SoundListTokens,
374 std::map<int, wxString> *SoundListMediatype,
375 std::map<int, int> *SoundListPref,
376 wxListCtrl *SoundListCtrl,
380 // Setup the pointers.
382 SoundListPtr = SoundList;
383 SoundListAltIDPtr = SoundListAltID;
384 SoundListPIDPtr = SoundListPID;
385 SoundListTypePtr = SoundListType;
386 SoundListAudioTypePtr = SoundListAudioType;
387 SoundListAudioEncTypePtr = SoundListAudioEncType;
388 SoundListTokensPtr = SoundListTokens;
389 SoundListMediatypePtr = SoundListMediatype;
390 SoundListPrefPtr = SoundListPref;
391 SoundListCtrlPtr = SoundListCtrl;
392 SoundListIndex = SoundIndex;