bug fix: TC-1152
[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(""),
42       m_successSFile(""),
43       m_failureSFile("")
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     return m_myInstance;
70 }
71
72 //--------------------------------------------------------------------------
73 /**
74  * @brief   Initialize
75  *          initialize to sound a operation
76  *
77  * @param[in]   homescreen_config   configuration instance
78  * @return      result
79  * @retval      true    success
80  * @retval      false   error
81  */
82 //--------------------------------------------------------------------------
83 bool
84 CicoSound::Initialize(CicoGKeyFileConfig *hsConfig)
85 {
86     if (true == m_initialized) {
87         return true;
88     }
89
90     m_command = hsConfig->ConfigGetString(ICO_CONFIG_SOUND,
91                                           ICO_CONFIG_COMMAND,
92                                           ICO_SOUND_DEFAULT_COMMAND);
93
94     m_operationSFile = hsConfig->ConfigGetFilePath(
95                                         ICO_CONFIG_SOUND, ICO_CONFIG_OPERATION,
96                                         ICO_SYC_CONFIGPATH_HOME_SOUND,
97                                         ICO_SYC_PACKAGE_HOMESCREEN
98                                           "/" ICO_SYC_CONFIGPATH_PACKAGE_SOUND,
99                                         ICO_SOUND_DEFAULT_OPERATION);
100
101     m_successSFile = hsConfig->ConfigGetFilePath(
102                                         ICO_CONFIG_SOUND, ICO_CONFIG_SUCCESS,
103                                         ICO_SYC_CONFIGPATH_HOME_SOUND,
104                                         ICO_SYC_PACKAGE_HOMESCREEN
105                                           "/" ICO_SYC_CONFIGPATH_PACKAGE_SOUND,
106                                         ICO_SOUND_DEFAULT_SUCCESS);
107
108     m_failureSFile = hsConfig->ConfigGetFilePath(
109                                         ICO_CONFIG_SOUND, ICO_CONFIG_FAILURE,
110                                         ICO_SYC_CONFIGPATH_HOME_SOUND,
111                                         ICO_SYC_PACKAGE_HOMESCREEN
112                                           "/" ICO_SYC_CONFIGPATH_PACKAGE_SOUND,
113                                         ICO_SOUND_DEFAULT_FAILURE);
114
115     ICO_DBG("CicoSound(command=%s operation=%s success=%s failure=%s",
116             m_command.c_str(), m_operationSFile.c_str(),
117             m_successSFile.c_str(), m_failureSFile.c_str())
118
119     m_initialized = true;
120
121     return true;
122 }
123
124 /*--------------------------------------------------------------------------*/
125 /**
126  * @brief   CicoSound::PlayOperationSound
127  *          play the given sound file(.wav).
128  *
129  * @return      result
130  * @retval      true    success
131  * @retval      false   error
132  */
133 /*--------------------------------------------------------------------------*/
134 bool
135 CicoSound::PlayOperationSound(void)
136 {
137     return PlaySound(m_operationSFile);
138 }
139
140 /*--------------------------------------------------------------------------*/
141 /**
142  * @brief   CicoSound::PlaySuccessSound
143  *
144  * @return      result
145  * @retval      true    success
146  * @retval      false   error
147  */
148 /*--------------------------------------------------------------------------*/
149 bool
150 CicoSound::PlaySuccessSound(void)
151 {
152     return PlaySound(m_successSFile);
153 }
154
155 /*--------------------------------------------------------------------------*/
156 /**
157  * @brief   CicoSound::PlaySuccessSound
158  *
159  * @return      result
160  * @retval      true    success
161  * @retval      false   error
162  */
163 /*--------------------------------------------------------------------------*/
164 bool
165 CicoSound::PlayFailureSound(void)
166 {
167     return PlaySound(m_failureSFile);
168 }
169
170 /*--------------------------------------------------------------------------*/
171 /**
172  * @brief   CicoSound::PlaySound
173  *          play the given sound file(.wav).
174  *
175  * @return      result
176  * @retval      true    success
177  * @retval      false   error
178  */
179 /*--------------------------------------------------------------------------*/
180 bool
181 CicoSound::PlaySound(std::string & soundFile)
182 {
183     int pid;
184     char command[256];
185
186     if (false == m_initialized) {
187         return false;
188     }
189
190     if (true == m_operationSFile.empty()) {
191         ICO_WRN("CicoSound::PlaySound: Leave(error file name is empty)");
192         return false;
193     }
194
195     if (m_operationSFile.at(0) != '/') {
196         ICO_WRN("CicoSound::PlaySound: Leave(file name not absolute path");
197         return false;
198     }
199
200     /* given by full file path */
201     snprintf(command, sizeof(command), "%s %s",
202              m_command.c_str(), soundFile.c_str());
203
204     ICO_DBG("PlaySound:system(%s)", command);
205     pid = fork();
206     if (!pid) {
207         system(command);
208         exit(0);
209     }
210
211     return true;
212 }
213 // vim: set expandtab ts=4 sw=4: