Bug fix: TIVI-1996.
[profile/ivi/ico-uxf-homescreen.git] / lib / common / CicoSound.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoSound.h
13  *
14  *  @brief  This file is implimention of CicoSound class
15  */
16 //==========================================================================
17 #include <string>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <ico_log.h>
25 #include "CicoResourceConfig.h"
26 #include "CicoGKeyFileConfig.h"
27
28 #include "CicoSound.h"
29
30
31 CicoSound *CicoSound::m_myInstance = NULL;
32
33 /*--------------------------------------------------------------------------*/
34 /**
35  *  @brief  default constructor
36  */
37 /*--------------------------------------------------------------------------*/
38 CicoSound::CicoSound()
39     : m_initialized(false),
40       m_command(ICO_SOUND_DEFAULT_COMMAND),
41       m_operationSFile(ICO_SOUND_DEFAULT_OPERATION),
42       m_successSFile(ICO_SOUND_DEFAULT_SUCCESS),
43       m_failureSFile(ICO_SOUND_DEFAULT_FAILURE)
44 {
45 }
46
47 /*--------------------------------------------------------------------------*/
48 /**
49  *  @brief  destructor
50  */
51 /*--------------------------------------------------------------------------*/
52 CicoSound::~CicoSound()
53 {
54 }
55
56 /*--------------------------------------------------------------------------*/
57 /**
58  *  @brief  get instance
59  *
60  *  @return CicoSound instance
61  */
62 /*--------------------------------------------------------------------------*/
63 CicoSound *
64 CicoSound::GetInstance(void)
65 {
66     if (NULL == m_myInstance) {
67         m_myInstance = new CicoSound();
68     }
69
70     return m_myInstance;
71 }
72
73 //--------------------------------------------------------------------------
74 /**
75  * @brief   Initialize
76  *          initialize to sound a operation
77  *
78  * @param[in]   homescreen_config   configuration instance
79  * @return      result
80  * @retval      true    success
81  * @retval      false   error
82  */
83 //--------------------------------------------------------------------------
84 bool
85 CicoSound::Initialize(CicoGKeyFileConfig *hsConfig)
86 {
87     if (true == m_initialized) {
88         return true;
89     }
90
91     m_command = hsConfig->ConfigGetString(ICO_CONFIG_SOUND,
92                                           ICO_CONFIG_COMMAND,
93                                           ICO_SOUND_DEFAULT_COMMAND);
94
95     m_operationSFile = hsConfig->ConfigGetString(ICO_CONFIG_SOUND,
96                                                  ICO_CONFIG_OPERATION,
97                                                  ICO_SOUND_DEFAULT_OPERATION);
98
99     m_successSFile = hsConfig->ConfigGetString(ICO_CONFIG_SOUND,
100                                                ICO_CONFIG_SUCCESS,
101                                                ICO_SOUND_DEFAULT_SUCCESS);
102
103     m_failureSFile = hsConfig->ConfigGetString(ICO_CONFIG_SOUND,
104                                                ICO_CONFIG_FAILURE,
105                                                ICO_SOUND_DEFAULT_FAILURE);
106
107     ICO_DBG("CicoSound(command=%s operation=%s success=%s failure=%s",
108             m_command.c_str(), m_operationSFile.c_str(),
109             m_successSFile.c_str(), m_failureSFile.c_str())
110
111     m_initialized = true;
112
113     return true;
114 }
115
116 /*--------------------------------------------------------------------------*/
117 /**
118  * @brief   CicoSound::PlayOperationSound
119  *          play the given sound file(.wav).
120  *
121  * @return      result
122  * @retval      true    success
123  * @retval      false   error
124  */
125 /*--------------------------------------------------------------------------*/
126 bool
127 CicoSound::PlayOperationSound(void)
128 {
129     return PlaySound(m_operationSFile);
130 }
131
132 /*--------------------------------------------------------------------------*/
133 /**
134  * @brief   CicoSound::PlaySuccessSound
135  *
136  * @return      result
137  * @retval      true    success
138  * @retval      false   error
139  */
140 /*--------------------------------------------------------------------------*/
141 bool
142 CicoSound::PlaySuccessSound(void)
143 {
144     return PlaySound(m_successSFile);
145 }
146
147 /*--------------------------------------------------------------------------*/
148 /**
149  * @brief   CicoSound::PlaySuccessSound
150  *
151  * @return      result
152  * @retval      true    success
153  * @retval      false   error
154  */
155 /*--------------------------------------------------------------------------*/
156 bool
157 CicoSound::PlayFailureSound(void)
158 {
159     return PlaySound(m_failureSFile);
160 }
161
162 /*--------------------------------------------------------------------------*/
163 /**
164  * @brief   CicoSound::PlaySound
165  *          play the given sound file(.wav).
166  *
167  * @return      result
168  * @retval      true    success
169  * @retval      false   error
170  */
171 /*--------------------------------------------------------------------------*/
172 bool
173 CicoSound::PlaySound(std::string & soundFile)
174 {
175     int pid;
176     char command[256];
177  
178     if (false == m_initialized) {
179         return false;
180     }
181
182     if (true == m_operationSFile.empty()) {
183         ICO_WRN("CicoSound::PlaySound: Leave(error file name is empty)");
184         return false;
185     }
186
187     if (m_operationSFile.at(0) != '/') {
188         ICO_WRN("CicoSound::PlaySound: Leave(file name not absolute path");
189         return false;
190     }
191
192     /* given by full file path */
193     snprintf(command, sizeof(command), "%s %s",
194              m_command.c_str(), soundFile.c_str());
195
196     ICO_DBG("PlaySound:system(%s)", command);
197     pid = fork();
198     if (!pid) {
199         system(command);
200         exit(0);
201     }
202
203     return true;
204 }
205 // vim: set expandtab ts=4 sw=4: