Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor: Stop XAB crashing when pressing Modify/Delete
[xestiaab/.git] / source / contacteditor / frmContactEditor-Sound.cpp
index 094a7cb..369238c 100644 (file)
+// frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
+//
+// (c) 2012-2016 Xestia Software Development.
+//
+// This file is part of Xestia Address Book.
+//
+// Xestia Address Book is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Address Book is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
+
 #include "frmContactEditor.h"
-#include "frmContactEditorSound.h"
-#include "../common/base64.h"
-#include <SFML/Audio.hpp>
-#include <thread>
 
 void frmContactEditor::AddSound( wxCommandEvent& event )
 {
-    int intResult = 0;
-    
-    frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
-    frameCESound->SetEditorMode(FALSE);
-    intResult = GetLastInt(&SoundsList);
-    frameCESound->SetupPointers(&SoundsList,
-                                &SoundsListAltID,
-                                &SoundsListPID,
-                                &SoundsListType,
-                                &SoundsListAudioType,
-                                &SoundsListAudioEncType,
-                                &SoundsListTokens,
-                                &SoundsListMediatype,
-                                &SoundsListPref,
-                                lboSounds,
-                                (intValueSeek));
-    frameCESound->ShowModal();
-    delete frameCESound;
-    frameCESound = NULL;
+
+       // Bring up a form to add sound.
+           
+       frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
+       frameCESound->SetEditorMode(FALSE);
+       frameCESound->SetupPointers(&ContactEditorData.SoundsList,
+               &ContactEditorData.SoundsListAltID,
+                &ContactEditorData.SoundsListPID,
+               &ContactEditorData.SoundsListType,
+                &ContactEditorData.SoundsListAudioType,
+                &ContactEditorData.SoundsListAudioEncType,
+                &ContactEditorData.SoundsListTokens,
+                &ContactEditorData.SoundsListMediatype,
+                &ContactEditorData.SoundsListPref,
+                lboSounds,
+                (intValueSeek));
+       frameCESound->ShowModal();
+       delete frameCESound;
+       frameCESound = NULL;
+       
 }
 
 void frmContactEditor::ModifySound( wxCommandEvent& event )
 {
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    if (!GetSelectedItem(lboSounds,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
-    
-    frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
-    frameCESound->SetupPointers(&SoundsList,
-                                &SoundsListAltID,
-                                &SoundsListPID,
-                                &SoundsListType,
-                                &SoundsListAudioType,
-                                &SoundsListAudioEncType,
-                                &SoundsListTokens,
-                                &SoundsListMediatype,
-                                &SoundsListPref,
-                                lboSounds,
-                                intSelectedData);
-    frameCESound->SetEditorMode(TRUE);
-    frameCESound->ShowModal();
-    delete frameCESound;
-    frameCESound = NULL;
+       
+       // Bring up a form to modify sound.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
+               &longSelected,
+                &intSelectedData)){
+               return;
+       }
+    
+       frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
+       frameCESound->SetupPointers(&ContactEditorData.SoundsList,
+               &ContactEditorData.SoundsListAltID,
+               &ContactEditorData.SoundsListPID,
+               &ContactEditorData.SoundsListType,
+               &ContactEditorData.SoundsListAudioType,
+               &ContactEditorData.SoundsListAudioEncType,
+               &ContactEditorData.SoundsListTokens,
+               &ContactEditorData.SoundsListMediatype,
+               &ContactEditorData.SoundsListPref,
+               lboSounds,
+                intSelectedData);
+       frameCESound->SetEditorMode(TRUE);
+       frameCESound->ShowModal();
+       delete frameCESound;
+       frameCESound = NULL;
+       
 }
 
 void frmContactEditor::DeleteSound( wxCommandEvent& event )
 {
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    if (!GetSelectedItem(lboSounds,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
-    
-    lboSounds->DeleteItem(longSelected);
-    
-    DeleteMapData(intSelectedData, &SoundsList, &SoundsListAltID,
-                  &SoundsListPID, &SoundsListType, &SoundsListAudioType,
-                  &SoundsListAudioEncType, &SoundsListTokens, &SoundsListMediatype,
-                  &SoundsListPref);
-    
+       
+       // Bring up a form to delete sound.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
+               &longSelected,
+               &intSelectedData)){
+               return;
+       }
+    
+       lboSounds->DeleteItem(longSelected);
+    
+       DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
+               &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
+               &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
+               &ContactEditorData.SoundsListPref);
+
 }
 
 void frmContactEditor::PlaySoundDetach()
 {
     
-    if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
+       // Play the sound and detach.
+       
+       if (!AudioStreamPlaybackPointer->openFromMemory(base64dec.c_str(), base64declen)){
         
-        // Can't load file. We must flee...
-        // Also show an error message too.
+               // Can't load file. We must flee...
+               // Also show an error message too.
         
-        wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
+               wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
         
-        return;
+               return;
         
-    }
-    
-    btnStop->Enable();
-    
-    //AudioStreamPlayback.setbuffer(AudioBuffer);
-    AudioStreamPlayback.play();
+       }
     
-    /*timespec n1, n2;
-     
-     n1.tv_sec = 0;
-     n1.tv_nsec = 100000000L;*/
+       btnStop->Enable();
+
+       AudioStreamPlaybackPointer->play();
     
-    //while (TimeA != TimeB){
-    while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
+       while (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
         
-        // We poll this every 100ms in order to conserve CPU usage.
+               // We poll this every 100ms in order to conserve CPU usage.
         
-        SleepFor(100000000);
+               SleepFor(10000000);
         
-    }
-    
-    btnStop->Disable();
+       }
+       
+       btnStop->Disable();
+       btnPlay->Enable();
     
 }
 
 void frmContactEditor::PlaySound( wxCommandEvent& event )
 {
     
-    //sf::Music *AudioPlayback = new sf::Music;
-    //AudioStream *AudioStreamPlayback = new AudioStream;
-    //sf::SoundBuffer *AudioBuffer = new sf::SoundBuffer;
-    
-    //sf::Sound *AudioPlayback = new sf::Sound;
-    
-    //delete(AudioPlayback);
+       // Play the sound.
     
-    //delete(AudioPlayback);
-    //delete(AudioStreamPlayback);
-    //delete(AudioBuffer);
+       long longSelected = -1;
+       int intSelectedData = 0;
     
-    //AudioPlayback = NULL;
-    //AudioStreamPlayback = NULL;
-    //AudioBuffer = NULL;
+       if (AudioStreamPlaybackPointer == nullptr){
+               
+               AudioStreamPlaybackPointer = new AudioStream;
+               
+       }
+       
+       // Check if sound is still being played and if it is, stop it.
     
-    //return;
-    
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    // Check if sound is still being played and if it is, stop it.
-    
-    if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
+       if (AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
         
-        AudioStreamPlayback.stop();
+               AudioStreamPlaybackPointer->stop();
         
-    }
+       }
     
-    if (!GetSelectedItem(lboSounds,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
-    
-    std::map<int, std::string>::iterator initier;
+       if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
+               &longSelected,
+               &intSelectedData)){
+               return;
+       }
+
+       btnPlay->Disable();
+       
+       std::map<int, std::string>::iterator initier;
     
-    initier = SoundsList.find(intSelectedData);
+       initier = ContactEditorData.SoundsList.find(intSelectedData);
     
-    base64dec = base64_decode(initier->second);
-    base64declen = base64dec.size();
+       base64dec = base64_decode(initier->second);
+       base64declen = base64dec.size();
     
-    std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
-    AudioPlaybackThread.detach();
+       std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
+       AudioPlaybackThread.detach();
     
-    return;
+       return;
     
 }
 
 void frmContactEditor::StopSound( wxCommandEvent& event )
 {
-    
-    if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
-        
-        AudioStreamPlayback.stop();
+       
+       // Stop the sound.
+       
+       if (AudioStreamPlaybackPointer != nullptr && AudioStreamPlaybackPointer->getStatus() == sf::SoundSource::Playing){
         
-    }
+               AudioStreamPlaybackPointer->stop();
+               
+       }
     
-    btnStop->Disable();
-    
-    return;
+       btnStop->Disable();
+       btnPlay->Enable();
+
+       return;
     
 }
 
+void frmContactEditor::SaveSound( wxCommandEvent &event )
+{
+
+       // Save the sound.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboSounds->GetItemCount() == 0 || !GetSelectedItem(lboSounds,
+                         &longSelected,
+                         &intSelectedData)){
+               
+               return;
+               
+       }
+       
+       wxString FileTypes;
+       
+       wxString FinalFilename;
+       
+       std::map<int,std::string>::iterator initier;
+       std::map<int,wxString>::iterator soundtypeiter;
+    
+       initier = ContactEditorData.SoundsList.find(intSelectedData);
+       soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
+       
+       // Open up the dialog to save the picture.
+       
+       wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
+               FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
+       
+       if (ExportDlg.ShowModal() == wxID_CANCEL){
+       
+               return;
+       
+       }
+       
+       // Write out the picture to the file.
+       
+       wxFile SoundFile;
+       
+       if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
+       
+               std::string base64dec;
+               
+               initier = ContactEditorData.SoundsList.find(intSelectedData);
+               base64dec = base64_decode(initier->second);
+               
+               SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
+
+               SoundFile.Close();
+       
+       } else {
+       
+               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);
+               return;
+       
+       }
+       
+}
\ No newline at end of file
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