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();
46 void frmContactEditor::ModifySound( wxCommandEvent& event )
49 // Bring up a form to modify sound.
51 long longSelected = -1;
52 int intSelectedData = 0;
54 if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
60 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
61 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
62 &ContactEditorData.SoundsListAltID,
63 &ContactEditorData.SoundsListPID,
64 &ContactEditorData.SoundsListType,
65 &ContactEditorData.SoundsListAudioType,
66 &ContactEditorData.SoundsListAudioEncType,
67 &ContactEditorData.SoundsListTokens,
68 &ContactEditorData.SoundsListMediatype,
69 &ContactEditorData.SoundsListPref,
72 frameCESound->SetEditorMode(TRUE);
73 frameCESound->ShowModal();
79 void frmContactEditor::DeleteSound( wxCommandEvent& event )
82 // Bring up a form to delete sound.
84 long longSelected = -1;
85 int intSelectedData = 0;
87 if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
93 lboSounds->DeleteItem(longSelected);
95 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
96 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
97 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
98 &ContactEditorData.SoundsListPref);
102 void frmContactEditor::PlaySoundDetach()
105 // Play the sound and detach.
107 if (!AudioStreamPlaybackPointer->openFromMemory(base64dec.c_str(), base64declen)){
109 // Can't load file. We must flee...
110 // Also show an error message too.
112 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
120 AudioStreamPlaybackPointer->play();
122 while (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
124 // We poll this every 100ms in order to conserve CPU usage.
135 void frmContactEditor::PlaySound( wxCommandEvent& event )
140 long longSelected = -1;
141 int intSelectedData = 0;
143 if (AudioStreamPlaybackPointer == nullptr){
145 AudioStreamPlaybackPointer = new AudioStream;
149 // Check if sound is still being played and if it is, stop it.
151 if (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
153 AudioStreamPlaybackPointer->stop();
157 if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
165 std::map<int, std::string>::iterator initier;
167 initier = ContactEditorData.SoundsList.find(intSelectedData);
169 base64dec = base64_decode(initier->second);
170 base64declen = base64dec.size();
172 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
173 AudioPlaybackThread.detach();
179 void frmContactEditor::StopSound( wxCommandEvent& event )
184 if (AudioStreamPlaybackPointer != nullptr && AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
186 AudioStreamPlaybackPointer->stop();
197 void frmContactEditor::SaveSound( wxCommandEvent &event )
202 long longSelected = -1;
203 int intSelectedData = 0;
205 if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
215 wxString FinalFilename;
217 std::map<int,std::string>::iterator initier;
218 std::map<int,wxString>::iterator soundtypeiter;
220 initier = ContactEditorData.SoundsList.find(intSelectedData);
221 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
223 // Open up the dialog to save the picture.
225 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
226 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
228 if (ExportDlg.ShowModal() == wxID_CANCEL){
234 // Write out the picture to the file.
238 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
240 std::string base64dec;
242 initier = ContactEditorData.SoundsList.find(intSelectedData);
243 base64dec = base64_decode(initier->second);
245 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
251 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);