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();
79 if (AudioFile.Open(wxSAudioFilename, wxFile::read)){
82 len = AudioFile.Length();
84 unsigned char* AudioFileData = new unsigned char[len];
85 flen = AudioFile.Read(AudioFileData, (size_t)len);
87 wxSAudioMIMEType = GetMIME(wxSAudioFilename);
89 // Check to make sure it is an audio file.
91 if (!AudioBuffer.loadFromMemory(AudioFileData, (size_t)len)){
93 // Can't load file. We must flee...
94 // Also show an error message too.
96 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
102 // Covert the file into base64.
104 b64enc = base64_encode(AudioFileData, (int)len);
106 delete[] AudioFileData;
110 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
116 SoundListPtr->insert(std::make_pair(SoundListIndex, b64enc));
120 strValue = cmbType->GetString(cmbType->GetSelection());
124 if (strValue == _("Home")) {
126 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
128 } else if (strValue == _("Work")) {
130 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
134 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("")));
138 // Setup Organisation Priority.
140 if (chkUsePref->IsChecked()){
142 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
146 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
152 SoundListAudioTypePtr->insert(std::make_pair(SoundListIndex, wxSAudioMIMEType));
154 // Setup Encoding Type.
156 SoundListAudioEncTypePtr->insert(std::make_pair(SoundListIndex, wxT("base64")));
162 coldata.SetId(SoundListIndex);
163 coldata.SetData(SoundListIndex);
164 coldata.SetText(_("Sound"));
165 ListCtrlIndex = SoundListCtrlPtr->InsertItem(coldata);
167 if (strValue == _("Home")) {
169 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
171 } else if (strValue == _("Work")) {
173 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
181 if (chkUsePref->IsChecked()){
183 SoundListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
189 } else if (EditorMode == TRUE) {
191 // Do not touch the audio file but update the settings
192 // associated with the audio file.
194 long longSelected = -1;
197 // Update Category Type.
199 SoundListTypePtr->erase(SoundListIndex);
200 strValue = cmbType->GetString(cmbType->GetSelection());
202 if (strValue == _("Home")) {
204 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
206 } else if (strValue == _("Work")) {
208 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
212 // Update Category Priority.
214 SoundListPrefPtr->erase(SoundListIndex);
216 if (chkUsePref->IsChecked()){
218 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
222 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
228 longSelected = SoundListCtrlPtr->GetNextItem(longSelected,
230 wxLIST_STATE_SELECTED);
232 if (longSelected == -1){
236 SoundListCtrlPtr->SetItem(longSelected, 0, wxT("Sound"));
237 SoundListCtrlPtr->SetItem(longSelected, 1, strValue);
239 if (chkUsePref->IsChecked()){
241 SoundListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
245 SoundListCtrlPtr->SetItem(longSelected, 2, wxT(""));
254 void frmContactEditorSound::CloseWindow( wxCommandEvent& event )
257 // Close this window.
263 void frmContactEditorSound::SetEditorMode(bool EditMode)
266 // Set the editor mode.
268 // Set if the editor is adding or editing an address.
272 if (EditMode == FALSE){
275 btnAction->SetLabel(_("Add"));
276 this->SetTitle(_("Add Sound"));
278 } else if (EditMode == TRUE){
281 btnAction->SetLabel(_("Modify"));
282 this->SetTitle(_("Modify Sound"));
285 std::map<int,int>::iterator intiter;
286 std::map<int,wxString>::iterator striter;
289 // Load the data into the form. Get the website.
291 striter = SoundListTypePtr->find(SoundListIndex);
293 if (striter->first == SoundListIndex){
295 strValue = striter->second;
299 if (strValue == wxT("home")){
301 cmbType->SetSelection(1);
303 } else if (strValue == wxT("work")){
305 cmbType->SetSelection(2);
309 cmbType->SetSelection(0);
313 // Get the website priority.
315 intiter = SoundListPrefPtr->find(SoundListIndex);
317 if (intiter->first == SoundListIndex && intiter->second > 0 &&
318 intiter != SoundListPrefPtr->end()){
320 sliPriority->SetValue(intiter->second);
321 sliPriority->Enable();
322 chkUsePref->SetValue(TRUE);
332 void frmContactEditorSound::SetupPointers(std::map<int, std::string> *SoundList,
333 std::map<int, wxString> *SoundListAltID,
334 std::map<int, wxString> *SoundListPID,
335 std::map<int, wxString> *SoundListType,
336 std::map<int, wxString> *SoundListAudioType,
337 std::map<int, wxString> *SoundListAudioEncType,
338 std::map<int, wxString> *SoundListTokens,
339 std::map<int, wxString> *SoundListMediatype,
340 std::map<int, int> *SoundListPref,
341 wxListCtrl *SoundListCtrl,
345 // Setup the pointers.
347 SoundListPtr = SoundList;
348 SoundListAltIDPtr = SoundListAltID;
349 SoundListPIDPtr = SoundListPID;
350 SoundListTypePtr = SoundListType;
351 SoundListAudioTypePtr = SoundListAudioType;
352 SoundListAudioEncTypePtr = SoundListAudioEncType;
353 SoundListTokensPtr = SoundListTokens;
354 SoundListMediatypePtr = SoundListMediatype;
355 SoundListPrefPtr = SoundListPref;
356 SoundListCtrlPtr = SoundListCtrl;
357 SoundListIndex = SoundIndex;