sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIo_MmcStorageManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_MmcStorageManagerImpl.cpp
20  * @brief       This is the implementation file for _MmcStorageManagerImpl class.
21  */
22
23 #include <limits.h>
24 #include <unique_ptr.h>
25 #include <new>
26
27 #include <FBaseSysLog.h>
28 #include <FBase_NativeError.h>
29 #include <FIo_MmcStorageManagerImpl.h>
30
31 #include "FIo_MmcStorageManagerProxy.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35
36 namespace Tizen { namespace Io
37 {
38
39 _MmcStorageManagerImpl::_MmcStorageManagerImpl(void)
40         : __pMmcStorageManagerProxy(null)
41 {
42 }
43
44 _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
45 {
46 }
47
48 result
49 _MmcStorageManagerImpl::Construct(void)
50 {
51         result r = E_SUCCESS;
52
53         std::unique_ptr<_MmcStorageManagerProxy> pMmcStorageManagerProxy(new (std::nothrow) _MmcStorageManagerProxy);
54         SysTryReturnResult(NID_IO, pMmcStorageManagerProxy != null, E_OUT_OF_MEMORY, "The memory is insufficient");
55
56         r = pMmcStorageManagerProxy->Construct(this);
57         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] __pMmcStorageManagerProxy->Construct() failed.", GetErrorMessage(r));
58
59         __pMmcStorageManagerProxy = pMmcStorageManagerProxy.release();
60
61         return E_SUCCESS;
62 }
63
64 _MmcStorageManagerImpl*
65 _MmcStorageManagerImpl::GetInstance(void)
66 {
67         static _MmcStorageManagerImpl* pImpl = null;
68         if (pImpl == null)
69         {
70                 std::unique_ptr<_MmcStorageManagerImpl> p(new (std::nothrow) _MmcStorageManagerImpl);
71                 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
72
73                 result r = p->Construct();
74                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
75
76                 pImpl = p.release();
77         }
78
79         return pImpl;
80
81 }
82
83 result
84 _MmcStorageManagerImpl::Mount(void)
85 {
86         return __pMmcStorageManagerProxy->Mount();
87 }
88
89 result
90 _MmcStorageManagerImpl::Unmount(void)
91 {
92         return __pMmcStorageManagerProxy->Unmount();
93 }
94
95 result
96 _MmcStorageManagerImpl::Format(void)
97 {
98         return __pMmcStorageManagerProxy->Format();
99 }
100
101 result
102 _MmcStorageManagerImpl::AddMmcStorageMountListener(IMmcStorageMountListener& listener)
103 {
104         result r = __pMmcMountListener.Add(&listener);
105         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
106
107         return E_SUCCESS;
108 }
109
110 result
111 _MmcStorageManagerImpl::RemoveMmcStorageMountListener(IMmcStorageMountListener& listener)
112 {
113         result r = __pMmcMountListener.Remove(&listener);
114         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
115
116         return E_SUCCESS;
117 }
118
119 result
120 _MmcStorageManagerImpl::AddMmcStorageFormatListener(IMmcStorageFormatListener& listener)
121 {
122         result r = __pMmcFormatListener.Add(&listener);
123         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
124
125         return E_SUCCESS;
126 }
127
128 result
129 _MmcStorageManagerImpl::RemoveMmcStorageFormatListener(IMmcStorageFormatListener& listener)
130 {
131         result r = E_SUCCESS;
132
133         r = __pMmcFormatListener.Remove(&listener);
134         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
135
136         return E_SUCCESS;
137 }
138
139 result
140 _MmcStorageManagerImpl::OnMmcMountResponseReceived(result r)
141 {
142         std::unique_ptr< IEnumeratorT< IMmcStorageMountListener* > >
143                 pEnum(__pMmcMountListener.GetEnumeratorN());
144         if (pEnum.get())
145         {
146                 while (pEnum->MoveNext() == E_SUCCESS)
147                 {
148                         IMmcStorageMountListener* pListener = null;
149                         pEnum->GetCurrent(pListener);
150                         if (pListener != null)
151                         {
152                                 SysLog(NID_IO, "Final Mount() response sent to Application : [%s]", GetErrorMessage(r));
153                                 pListener->OnMmcStorageMounted(r);
154                         }
155                 }
156         }
157
158         return E_SUCCESS;
159 }
160
161 result
162 _MmcStorageManagerImpl::OnMmcUnmountResponseReceived(result r)
163 {
164         std::unique_ptr< IEnumeratorT< IMmcStorageMountListener* > >
165                 pEnum(__pMmcMountListener.GetEnumeratorN());
166         if (pEnum.get())
167         {
168                 while (pEnum->MoveNext() == E_SUCCESS)
169                 {
170                         IMmcStorageMountListener* pListener = null;
171                         pEnum->GetCurrent(pListener);
172                         if (pListener != null)
173                         {
174                                 SysLog(NID_IO, "Final Unmount() response sent to Application : [%s]", GetErrorMessage(r));
175                                 pListener->OnMmcStorageUnmounted(r);
176                         }
177                 }
178         }
179
180         return E_SUCCESS;
181 }
182
183 result
184 _MmcStorageManagerImpl::OnMmcFormatResponseReceived(result r)
185 {
186         std::unique_ptr<IEnumeratorT< IMmcStorageFormatListener* > >
187                 pEnum(__pMmcFormatListener.GetEnumeratorN());
188         if (pEnum.get())
189         {
190                 while (pEnum->MoveNext() == E_SUCCESS)
191                 {
192                         IMmcStorageFormatListener* pListener = null;
193                         pEnum->GetCurrent(pListener);
194                         if (pListener != null)
195                         {
196                                 SysLog(NID_IO, "Final Format() response sent to Application : [%s]", GetErrorMessage(r));
197                                 pListener->OnMmcStorageFormatted(r);
198                         }
199                 }
200         }
201
202         return E_SUCCESS;
203 }
204
205 }} // Tizen::Io
206