sync with master
[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 <unique_ptr.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBaseRtIEventArg.h>
27 #include <FBase_NativeError.h>
28 #include <FIoIMmcStorageMountListener.h>
29 #include <FIoIMmcStorageFormatListener.h>
30
31 #include "FIo_MmcStorageManagerImpl.h"
32 #include "FIo_MmcStorageManagerProxy.h"
33
34 using namespace std;
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39
40 namespace Tizen { namespace Io
41 {
42
43 static const int MOUNT_TYPE = 0;
44 static const int UNMOUNT_TYPE = 1;
45
46 class _MountEventArg
47         : public IEventArg
48 {
49 public:
50         _MountEventArg(int type, result r)
51                 : __type(type)
52                 , __r(r)
53         {
54         }
55         int __type;
56         result __r;
57 };
58
59 class _FormatEventArg
60         : public IEventArg
61 {
62 public:
63         _FormatEventArg(result r)
64                 : __r(r)
65         {
66         }
67         result __r;
68 };
69
70 class _MountEvent
71         : public Event
72 {
73 protected:
74         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
75         {
76                 const _MountEventArg* pArg = dynamic_cast<const _MountEventArg*>(&arg);
77                 if (pArg != null)
78                 {
79                         IMmcStorageMountListener* pListener = dynamic_cast<IMmcStorageMountListener*> (&listener);
80                         if (pListener != null)
81                         {
82                                 if (pArg->__type == MOUNT_TYPE) // Mount
83                                 {
84                                         pListener->OnMmcStorageMounted(pArg->__r);
85                                 }
86                                 else    // Unmount
87                                 {
88                                         pListener->OnMmcStorageUnmounted(pArg->__r);
89                                 }
90                         }
91                 }
92         }
93
94 };
95
96 class _FormatEvent
97         : public Event
98 {
99 protected:
100         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
101         {
102                 IMmcStorageFormatListener* pListener = dynamic_cast<IMmcStorageFormatListener*> (&listener);
103                 if (pListener != null)
104                 {
105                         const _FormatEventArg* pArg = dynamic_cast<const _FormatEventArg*>(&arg);
106                         if (pArg != null)
107                         {
108                                 pListener->OnMmcStorageFormatted(pArg->__r);
109                         }
110
111                 }
112         }
113
114 };
115
116 _MmcStorageManagerImpl::_MmcStorageManagerImpl(void)
117         : __pMmcStorageManagerProxy(null)
118         , __pMountEvent(null)
119         , __pFormatEvent(null)
120 {
121 }
122
123 _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
124 {
125         delete __pMountEvent;
126         delete __pFormatEvent;
127 }
128
129 result
130 _MmcStorageManagerImpl::Construct(void)
131 {
132         result r = E_SUCCESS;
133
134         unique_ptr<_MmcStorageManagerProxy> pMmcStorageManagerProxy(new (std::nothrow) _MmcStorageManagerProxy);
135         SysTryReturnResult(NID_IO, pMmcStorageManagerProxy != null, E_OUT_OF_MEMORY, "The memory is insufficient");
136
137         r = pMmcStorageManagerProxy->Construct(this);
138         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "__pMmcStorageManagerProxy->Construct() failed.");
139
140         unique_ptr<_MountEvent> pMountEvent(new (std::nothrow) _MountEvent);
141         SysTryReturnResult(NID_IO, pMountEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
142
143         unique_ptr<_FormatEvent> pFormatEvent(new (std::nothrow) _FormatEvent);
144         SysTryReturnResult(NID_IO, pFormatEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
145
146         __pMmcStorageManagerProxy = pMmcStorageManagerProxy.release();
147
148         __pMountEvent = pMountEvent.release();
149         __pFormatEvent = pFormatEvent.release();
150
151         return E_SUCCESS;
152 }
153
154 _MmcStorageManagerImpl*
155 _MmcStorageManagerImpl::GetInstance(void)
156 {
157         static _MmcStorageManagerImpl* pImpl = null;
158         if (pImpl == null)
159         {
160                 unique_ptr<_MmcStorageManagerImpl> p(new (std::nothrow) _MmcStorageManagerImpl);
161                 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
162
163                 result r = p->Construct();
164                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
165
166                 pImpl = p.release();
167         }
168
169         return pImpl;
170
171 }
172
173 result
174 _MmcStorageManagerImpl::Mount(void)
175 {
176         return __pMmcStorageManagerProxy->Mount();
177 }
178
179 result
180 _MmcStorageManagerImpl::Unmount(void)
181 {
182         return __pMmcStorageManagerProxy->Unmount();
183 }
184
185 result
186 _MmcStorageManagerImpl::Format(void)
187 {
188         return __pMmcStorageManagerProxy->Format();
189 }
190
191 result
192 _MmcStorageManagerImpl::AddMmcStorageMountListener(IMmcStorageMountListener& listener)
193 {
194         return __pMountEvent->AddListener(listener);
195 }
196
197 result
198 _MmcStorageManagerImpl::RemoveMmcStorageMountListener(IMmcStorageMountListener& listener)
199 {
200         return __pMountEvent->RemoveListener(listener);
201 }
202
203 result
204 _MmcStorageManagerImpl::AddMmcStorageFormatListener(IMmcStorageFormatListener& listener)
205 {
206         return __pFormatEvent->AddListener(listener);
207 }
208
209 result
210 _MmcStorageManagerImpl::RemoveMmcStorageFormatListener(IMmcStorageFormatListener& listener)
211 {
212         return __pFormatEvent->RemoveListener(listener);
213 }
214
215 void
216 _MmcStorageManagerImpl::OnMmcMountResponseReceived(result r)
217 {
218         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(MOUNT_TYPE, r);
219         if(pEventArg != null)
220         {
221                 __pMountEvent->Fire(*pEventArg);
222         }
223 }
224
225 void
226 _MmcStorageManagerImpl::OnMmcUnmountResponseReceived(result r)
227 {
228         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(UNMOUNT_TYPE, r);
229         if(pEventArg != null)
230         {
231                 __pMountEvent->Fire(*pEventArg);
232         }
233 }
234
235 void
236 _MmcStorageManagerImpl::OnMmcFormatResponseReceived(result r)
237 {
238         _FormatEventArg* pEventArg= new (std::nothrow) _FormatEventArg(r);
239         if(pEventArg != null)
240         {
241                 __pFormatEvent->Fire(*pEventArg);
242         }
243 }
244
245 }} // Tizen::Io
246