1 // frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
3 // (c) 2012-2016 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 "frmContactEditor.h"
21 void frmContactEditor::AddSound( wxCommandEvent& event )
24 // Bring up a form to add sound.
26 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
27 frameCESound->SetEditorMode(FALSE);
28 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
29 &ContactEditorData.SoundsListAltID,
30 &ContactEditorData.SoundsListPID,
31 &ContactEditorData.SoundsListType,
32 &ContactEditorData.SoundsListAudioType,
33 &ContactEditorData.SoundsListAudioEncType,
34 &ContactEditorData.SoundsListTokens,
35 &ContactEditorData.SoundsListMediatype,
36 &ContactEditorData.SoundsListPref,
39 frameCESound->ShowModal();
45 void frmContactEditor::ModifySound( wxCommandEvent& event )
48 // Bring up a form to modify sound.
50 long longSelected = -1;
51 int intSelectedData = 0;
53 if (!GetSelectedItem(lboSounds,
59 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
60 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
61 &ContactEditorData.SoundsListAltID,
62 &ContactEditorData.SoundsListPID,
63 &ContactEditorData.SoundsListType,
64 &ContactEditorData.SoundsListAudioType,
65 &ContactEditorData.SoundsListAudioEncType,
66 &ContactEditorData.SoundsListTokens,
67 &ContactEditorData.SoundsListMediatype,
68 &ContactEditorData.SoundsListPref,
71 frameCESound->SetEditorMode(TRUE);
72 frameCESound->ShowModal();
78 void frmContactEditor::DeleteSound( wxCommandEvent& event )
81 // Bring up a form to delete sound.
83 long longSelected = -1;
84 int intSelectedData = 0;
86 if (!GetSelectedItem(lboSounds,
92 lboSounds->DeleteItem(longSelected);
94 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
95 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
96 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
97 &ContactEditorData.SoundsListPref);
101 void frmContactEditor::PlaySoundDetach()
104 // Play the sound and detach.
106 if (!AudioStreamPlaybackPointer->openFromMemory(base64dec.c_str(), base64declen)){
108 // Can't load file. We must flee...
109 // Also show an error message too.
111 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
119 AudioStreamPlaybackPointer->play();
121 while (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
123 // We poll this every 100ms in order to conserve CPU usage.
134 void frmContactEditor::PlaySound( wxCommandEvent& event )
139 long longSelected = -1;
140 int intSelectedData = 0;
142 if (AudioStreamPlaybackPointer == nullptr){
144 AudioStreamPlaybackPointer = new AudioStream;
148 // Check if sound is still being played and if it is, stop it.
150 if (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
152 AudioStreamPlaybackPointer->stop();
156 if (!GetSelectedItem(lboSounds,
164 std::map<int, std::string>::iterator initier;
166 initier = ContactEditorData.SoundsList.find(intSelectedData);
168 base64dec = base64_decode(initier->second);
169 base64declen = base64dec.size();
171 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
172 AudioPlaybackThread.detach();
178 void frmContactEditor::StopSound( wxCommandEvent& event )
183 if (AudioStreamPlaybackPointer != nullptr && AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
185 AudioStreamPlaybackPointer->stop();
196 void frmContactEditor::SaveSound( wxCommandEvent &event )
201 long longSelected = -1;
202 int intSelectedData = 0;
204 if (!GetSelectedItem(lboSounds,
214 wxString FinalFilename;
216 std::map<int,std::string>::iterator initier;
217 std::map<int,wxString>::iterator soundtypeiter;
219 initier = ContactEditorData.SoundsList.find(intSelectedData);
220 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
222 // Open up the dialog to save the picture.
224 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
225 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
227 if (ExportDlg.ShowModal() == wxID_CANCEL){
233 // Write out the picture to the file.
237 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
239 std::string base64dec;
241 initier = ContactEditorData.SoundsList.find(intSelectedData);
242 base64dec = base64_decode(initier->second);
244 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
250 wxMessageBox(_("An error occured whilst saving the sound. Check that you have free space and permissions to write the picture at this location."), _("Sound save error"), wxOK, this);