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)){
81 len = AudioFile.Length();
83 unsigned char* AudioFileData = new unsigned char[len];
85 wxSAudioMIMEType = GetMIME(wxSAudioFilename);
87 // Check to make sure it is an audio file.
89 if (!AudioBuffer.loadFromMemory(AudioFileData, (size_t)len)){
91 // Can't load file. We must flee...
92 // Also show an error message too.
94 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
100 // Covert the file into base64.
102 b64enc = base64_encode(AudioFileData, (int)len);
104 delete[] AudioFileData;
108 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
114 SoundListPtr->insert(std::make_pair(SoundListIndex, b64enc));
118 strValue = cmbType->GetString(cmbType->GetSelection());
122 if (strValue == _("Home")) {
124 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
126 } else if (strValue == _("Work")) {
128 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
132 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("")));
136 // Setup Organisation Priority.
138 if (chkUsePref->IsChecked()){
140 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
144 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
150 SoundListAudioTypePtr->insert(std::make_pair(SoundListIndex, wxSAudioMIMEType));
152 // Setup Encoding Type.
154 SoundListAudioEncTypePtr->insert(std::make_pair(SoundListIndex, wxT("base64")));
160 coldata.SetId(SoundListIndex);
161 coldata.SetData(SoundListIndex);
162 coldata.SetText(_("Sound"));
163 ListCtrlIndex = SoundListCtrlPtr->InsertItem(coldata);
165 if (strValue == _("Home")) {
167 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
169 } else if (strValue == _("Work")) {
171 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
179 if (chkUsePref->IsChecked()){
181 SoundListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
187 } else if (EditorMode == TRUE) {
189 // Do not touch the audio file but update the settings
190 // associated with the audio file.
192 long longSelected = -1;
195 // Update Category Type.
197 SoundListTypePtr->erase(SoundListIndex);
198 strValue = cmbType->GetString(cmbType->GetSelection());
200 if (strValue == _("Home")) {
202 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
204 } else if (strValue == _("Work")) {
206 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
210 // Update Category Priority.
212 SoundListPrefPtr->erase(SoundListIndex);
214 if (chkUsePref->IsChecked()){
216 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
220 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
226 longSelected = SoundListCtrlPtr->GetNextItem(longSelected,
228 wxLIST_STATE_SELECTED);
230 if (longSelected == -1){
234 SoundListCtrlPtr->SetItem(longSelected, 0, wxT("Sound"));
235 SoundListCtrlPtr->SetItem(longSelected, 1, strValue);
237 if (chkUsePref->IsChecked()){
239 SoundListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
243 SoundListCtrlPtr->SetItem(longSelected, 2, wxT(""));
252 void frmContactEditorSound::CloseWindow( wxCommandEvent& event )
255 // Close this window.
261 void frmContactEditorSound::SetEditorMode(bool EditMode)
264 // Set the editor mode.
266 // Set if the editor is adding or editing an address.
270 if (EditMode == FALSE){
273 btnAction->SetLabel(_("Add"));
274 this->SetTitle(_("Add Sound"));
276 } else if (EditMode == TRUE){
279 btnAction->SetLabel(_("Modify"));
280 this->SetTitle(_("Modify Sound"));
283 std::map<int,int>::iterator intiter;
284 std::map<int,wxString>::iterator striter;
287 // Load the data into the form. Get the website.
289 striter = SoundListTypePtr->find(SoundListIndex);
291 if (striter->first == SoundListIndex){
293 strValue = striter->second;
297 if (strValue == wxT("home")){
299 cmbType->SetSelection(1);
301 } else if (strValue == wxT("work")){
303 cmbType->SetSelection(2);
307 cmbType->SetSelection(0);
311 // Get the website priority.
313 intiter = SoundListPrefPtr->find(SoundListIndex);
315 if (intiter->first == SoundListIndex && intiter->second > 0 &&
316 intiter != SoundListPrefPtr->end()){
318 sliPriority->SetValue(intiter->second);
319 sliPriority->Enable();
320 chkUsePref->SetValue(TRUE);
330 void frmContactEditorSound::SetupPointers(std::map<int, std::string> *SoundList,
331 std::map<int, wxString> *SoundListAltID,
332 std::map<int, wxString> *SoundListPID,
333 std::map<int, wxString> *SoundListType,
334 std::map<int, wxString> *SoundListAudioType,
335 std::map<int, wxString> *SoundListAudioEncType,
336 std::map<int, wxString> *SoundListTokens,
337 std::map<int, wxString> *SoundListMediatype,
338 std::map<int, int> *SoundListPref,
339 wxListCtrl *SoundListCtrl,
343 // Setup the pointers.
345 SoundListPtr = SoundList;
346 SoundListAltIDPtr = SoundListAltID;
347 SoundListPIDPtr = SoundListPID;
348 SoundListTypePtr = SoundListType;
349 SoundListAudioTypePtr = SoundListAudioType;
350 SoundListAudioEncTypePtr = SoundListAudioEncType;
351 SoundListTokensPtr = SoundListTokens;
352 SoundListMediatypePtr = SoundListMediatype;
353 SoundListPrefPtr = SoundListPref;
354 SoundListCtrlPtr = SoundListCtrl;
355 SoundListIndex = SoundIndex;