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];
84 flen = AudioFile.Read(AudioFileData, (size_t)len);
86 wxSAudioMIMEType = GetMIME(wxSAudioFilename);
88 // Check to make sure it is an audio file.
90 if (!AudioBuffer.loadFromMemory(AudioFileData, (size_t)len)){
92 // Can't load file. We must flee...
93 // Also show an error message too.
95 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
101 // Covert the file into base64.
103 b64enc = base64_encode(AudioFileData, (int)len);
105 delete[] AudioFileData;
109 wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
115 SoundListPtr->insert(std::make_pair(SoundListIndex, b64enc));
119 strValue = cmbType->GetString(cmbType->GetSelection());
123 if (strValue == _("Home")) {
125 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
127 } else if (strValue == _("Work")) {
129 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
133 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("")));
137 // Setup Organisation Priority.
139 if (chkUsePref->IsChecked()){
141 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
145 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
151 SoundListAudioTypePtr->insert(std::make_pair(SoundListIndex, wxSAudioMIMEType));
153 // Setup Encoding Type.
155 SoundListAudioEncTypePtr->insert(std::make_pair(SoundListIndex, wxT("base64")));
161 coldata.SetId(SoundListIndex);
162 coldata.SetData(SoundListIndex);
163 coldata.SetText(_("Sound"));
164 ListCtrlIndex = SoundListCtrlPtr->InsertItem(coldata);
166 if (strValue == _("Home")) {
168 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
170 } else if (strValue == _("Work")) {
172 SoundListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
180 if (chkUsePref->IsChecked()){
182 SoundListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
188 } else if (EditorMode == TRUE) {
190 // Do not touch the audio file but update the settings
191 // associated with the audio file.
193 long longSelected = -1;
196 // Update Category Type.
198 SoundListTypePtr->erase(SoundListIndex);
199 strValue = cmbType->GetString(cmbType->GetSelection());
201 if (strValue == _("Home")) {
203 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("home")));
205 } else if (strValue == _("Work")) {
207 SoundListTypePtr->insert(std::make_pair(SoundListIndex, wxT("work")));
211 // Update Category Priority.
213 SoundListPrefPtr->erase(SoundListIndex);
215 if (chkUsePref->IsChecked()){
217 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, sliPriority->GetValue()));
221 SoundListPrefPtr->insert(std::make_pair(SoundListIndex, 0));
227 longSelected = SoundListCtrlPtr->GetNextItem(longSelected,
229 wxLIST_STATE_SELECTED);
231 if (longSelected == -1){
235 SoundListCtrlPtr->SetItem(longSelected, 0, wxT("Sound"));
236 SoundListCtrlPtr->SetItem(longSelected, 1, strValue);
238 if (chkUsePref->IsChecked()){
240 SoundListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
244 SoundListCtrlPtr->SetItem(longSelected, 2, wxT(""));
253 void frmContactEditorSound::CloseWindow( wxCommandEvent& event )
256 // Close this window.
262 void frmContactEditorSound::SetEditorMode(bool EditMode)
265 // Set the editor mode.
267 // Set if the editor is adding or editing an address.
271 if (EditMode == FALSE){
274 btnAction->SetLabel(_("Add"));
275 this->SetTitle(_("Add Sound"));
277 } else if (EditMode == TRUE){
280 btnAction->SetLabel(_("Modify"));
281 this->SetTitle(_("Modify Sound"));
284 std::map<int,int>::iterator intiter;
285 std::map<int,wxString>::iterator striter;
288 // Load the data into the form. Get the website.
290 striter = SoundListTypePtr->find(SoundListIndex);
292 if (striter->first == SoundListIndex){
294 strValue = striter->second;
298 if (strValue == wxT("home")){
300 cmbType->SetSelection(1);
302 } else if (strValue == wxT("work")){
304 cmbType->SetSelection(2);
308 cmbType->SetSelection(0);
312 // Get the website priority.
314 intiter = SoundListPrefPtr->find(SoundListIndex);
316 if (intiter->first == SoundListIndex && intiter->second > 0 &&
317 intiter != SoundListPrefPtr->end()){
319 sliPriority->SetValue(intiter->second);
320 sliPriority->Enable();
321 chkUsePref->SetValue(TRUE);
331 void frmContactEditorSound::SetupPointers(std::map<int, std::string> *SoundList,
332 std::map<int, wxString> *SoundListAltID,
333 std::map<int, wxString> *SoundListPID,
334 std::map<int, wxString> *SoundListType,
335 std::map<int, wxString> *SoundListAudioType,
336 std::map<int, wxString> *SoundListAudioEncType,
337 std::map<int, wxString> *SoundListTokens,
338 std::map<int, wxString> *SoundListMediatype,
339 std::map<int, int> *SoundListPref,
340 wxListCtrl *SoundListCtrl,
344 // Setup the pointers.
346 SoundListPtr = SoundList;
347 SoundListAltIDPtr = SoundListAltID;
348 SoundListPIDPtr = SoundListPID;
349 SoundListTypePtr = SoundListType;
350 SoundListAudioTypePtr = SoundListAudioType;
351 SoundListAudioEncTypePtr = SoundListAudioEncType;
352 SoundListTokensPtr = SoundListTokens;
353 SoundListMediatypePtr = SoundListMediatype;
354 SoundListPrefPtr = SoundListPref;
355 SoundListCtrlPtr = SoundListCtrl;
356 SoundListIndex = SoundIndex;