Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / io / FIo_MmcStorageManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FIo_MmcStorageManagerImpl.cpp
19  * @brief       This is the implementation file for _MmcStorageManagerImpl class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FBaseSysLog.h>
25 #include <FBaseRtIEventArg.h>
26 #include <FBase_NativeError.h>
27 #include <FIoIMmcStorageMountListener.h>
28 #include <FIoIMmcStorageFormatListener.h>
29
30 #include "FIo_MmcStorageManagerImpl.h"
31 #include "FIo_MmcStorageManagerProxy.h"
32
33 using namespace std;
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Base::Runtime;
38
39 namespace Tizen { namespace Io
40 {
41
42 static const int MOUNT_TYPE = 0;
43 static const int UNMOUNT_TYPE = 1;
44
45 class _MountEventArg
46         : public IEventArg
47 {
48 public:
49         _MountEventArg(int type, result r)
50                 : __type(type)
51                 , __r(r)
52         {
53         }
54         int __type;
55         result __r;
56 };
57
58 class _FormatEventArg
59         : public IEventArg
60 {
61 public:
62         _FormatEventArg(result r)
63                 : __r(r)
64         {
65         }
66         result __r;
67 };
68
69 class _MountEvent
70         : public Event
71 {
72 protected:
73         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
74         {
75                 const _MountEventArg* pArg = dynamic_cast<const _MountEventArg*>(&arg);
76                 if (pArg != null)
77                 {
78                         IMmcStorageMountListener* pListener = dynamic_cast<IMmcStorageMountListener*> (&listener);
79                         if (pListener != null)
80                         {
81                                 if (pArg->__type == MOUNT_TYPE) // Mount
82                                 {
83                                         pListener->OnMmcStorageMounted(pArg->__r);
84                                 }
85                                 else    // Unmount
86                                 {
87                                         pListener->OnMmcStorageUnmounted(pArg->__r);
88                                 }
89                         }
90                 }
91         }
92
93 };
94
95 class _FormatEvent
96         : public Event
97 {
98 protected:
99         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
100         {
101                 IMmcStorageFormatListener* pListener = dynamic_cast<IMmcStorageFormatListener*> (&listener);
102                 if (pListener != null)
103                 {
104                         const _FormatEventArg* pArg = dynamic_cast<const _FormatEventArg*>(&arg);
105                         if (pArg != null)
106                         {
107                                 pListener->OnMmcStorageFormatted(pArg->__r);
108                         }
109
110                 }
111         }
112
113 };
114
115 _MmcStorageManagerImpl::_MmcStorageManagerImpl(void)
116         : __pMmcStorageManagerProxy(null)
117         , __pMountEvent(null)
118         , __pFormatEvent(null)
119 {
120 }
121
122 _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
123 {
124         delete __pMmcStorageManagerProxy;
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