Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor: Stop XAB crashing when pressing Modify/Delete
[xestiaab/.git] / source / contacteditor / frmContactEditor-Sound.cpp
1 // frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
2 //
3 // (c) 2012-2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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 )
22 {
24         // Bring up a form to add sound.
25             
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,
37                 lboSounds,
38                 (intValueSeek));
39         frameCESound->ShowModal();
40         delete frameCESound;
41         frameCESound = NULL;
42         
43 }
45 void frmContactEditor::ModifySound( wxCommandEvent& event )
46 {
47         
48         // Bring up a form to modify sound.
49         
50         long longSelected = -1;
51         int intSelectedData = 0;
52     
53         if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
54                 &longSelected,
55                 &intSelectedData)){
56                 return;
57         }
58     
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,
69                 lboSounds,
70                 intSelectedData);
71         frameCESound->SetEditorMode(TRUE);
72         frameCESound->ShowModal();
73         delete frameCESound;
74         frameCESound = NULL;
75         
76 }
78 void frmContactEditor::DeleteSound( wxCommandEvent& event )
79 {
80         
81         // Bring up a form to delete sound.
82         
83         long longSelected = -1;
84         int intSelectedData = 0;
85     
86         if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
87                 &longSelected,
88                 &intSelectedData)){
89                 return;
90         }
91     
92         lboSounds->DeleteItem(longSelected);
93     
94         DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
95                 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
96                 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
97                 &ContactEditorData.SoundsListPref);
99 }
101 void frmContactEditor::PlaySoundDetach()
103     
104         // Play the sound and detach.
105         
106         if (!AudioStreamPlaybackPointer->openFromMemory(base64dec.c_str(), base64declen)){
107         
108                 // Can't load file. We must flee...
109                 // Also show an error message too.
110         
111                 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
112         
113                 return;
114         
115         }
116     
117         btnStop->Enable();
119         AudioStreamPlaybackPointer->play();
120     
121         while (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
122         
123                 // We poll this every 100ms in order to conserve CPU usage.
124         
125                 SleepFor(10000000);
126         
127         }
128         
129         btnStop->Disable();
130         btnPlay->Enable();
131     
134 void frmContactEditor::PlaySound( wxCommandEvent& event )
136     
137         // Play the sound.
138     
139         long longSelected = -1;
140         int intSelectedData = 0;
141     
142         if (AudioStreamPlaybackPointer == nullptr){
143                 
144                 AudioStreamPlaybackPointer = new AudioStream;
145                 
146         }
147         
148         // Check if sound is still being played and if it is, stop it.
149     
150         if (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
151         
152                 AudioStreamPlaybackPointer->stop();
153         
154         }
155     
156         if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
157                 &longSelected,
158                 &intSelectedData)){
159                 return;
160         }
162         btnPlay->Disable();
163         
164         std::map<int, std::string>::iterator initier;
165     
166         initier = ContactEditorData.SoundsList.find(intSelectedData);
167     
168         base64dec = base64_decode(initier->second);
169         base64declen = base64dec.size();
170     
171         std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
172         AudioPlaybackThread.detach();
173     
174         return;
175     
178 void frmContactEditor::StopSound( wxCommandEvent& event )
180         
181         // Stop the sound.
182         
183         if (AudioStreamPlaybackPointer != nullptr && AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
184         
185                 AudioStreamPlaybackPointer->stop();
186                 
187         }
188     
189         btnStop->Disable();
190         btnPlay->Enable();
192         return;
193     
196 void frmContactEditor::SaveSound( wxCommandEvent &event )
199         // Save the sound.
200         
201         long longSelected = -1;
202         int intSelectedData = 0;
203     
204         if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
205                          &longSelected,
206                          &intSelectedData)){
207                 
208                 return;
209                 
210         }
211         
212         wxString FileTypes;
213         
214         wxString FinalFilename;
215         
216         std::map<int,std::string>::iterator initier;
217         std::map<int,wxString>::iterator soundtypeiter;
218     
219         initier = ContactEditorData.SoundsList.find(intSelectedData);
220         soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
221         
222         // Open up the dialog to save the picture.
223         
224         wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
225                 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
226         
227         if (ExportDlg.ShowModal() == wxID_CANCEL){
228         
229                 return;
230         
231         }
232         
233         // Write out the picture to the file.
234         
235         wxFile SoundFile;
236         
237         if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
238         
239                 std::string base64dec;
240                 
241                 initier = ContactEditorData.SoundsList.find(intSelectedData);
242                 base64dec = base64_decode(initier->second);
243                 
244                 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
246                 SoundFile.Close();
247         
248         } else {
249         
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);
251                 return;
252         
253         }
254         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy