Merge "Remove dead code in Locales." into tizen_2.2
[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 __pMountEvent;
125         delete __pFormatEvent;
126 }
127
128 result
129 _MmcStorageManagerImpl::Construct(void)
130 {
131         unique_ptr<_MountEvent> pMountEvent(new (std::nothrow) _MountEvent);
132         SysTryReturnResult(NID_IO, pMountEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
133
134         unique_ptr<_FormatEvent> pFormatEvent(new (std::nothrow) _FormatEvent);
135         SysTryReturnResult(NID_IO, pFormatEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
136
137         __pMountEvent = pMountEvent.release();
138         __pFormatEvent = pFormatEvent.release();
139
140         return E_SUCCESS;
141 }
142
143 _MmcStorageManagerImpl*
144 _MmcStorageManagerImpl::GetInstance(void)
145 {
146         static _MmcStorageManagerImpl* pImpl = null;
147         if (pImpl == null)
148         {
149                 unique_ptr<_MmcStorageManagerImpl> pMmcStorageManagerImpl(new (std::nothrow) _MmcStorageManagerImpl);
150                 SysTryReturn(NID_IO, pMmcStorageManagerImpl != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
151
152                 result r = pMmcStorageManagerImpl->Construct();
153                 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
154
155                 pImpl = pMmcStorageManagerImpl.release();
156         }
157
158         return pImpl;
159 }
160
161 result
162 _MmcStorageManagerImpl::Mount(void)
163 {
164         if (__pMmcStorageManagerProxy == null)
165         {
166                 __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
167                 SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
168                 __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
169         }
170         return __pMmcStorageManagerProxy->Mount();
171 }
172
173 result
174 _MmcStorageManagerImpl::Unmount(void)
175 {
176         if (__pMmcStorageManagerProxy == null)
177         {
178                 __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
179                 SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
180                 __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
181         }
182         return __pMmcStorageManagerProxy->Unmount();
183 }
184
185 result
186 _MmcStorageManagerImpl::Format(void)
187 {
188         if (__pMmcStorageManagerProxy == null)
189         {
190                 __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
191                 SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
192                 __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
193         }
194         return __pMmcStorageManagerProxy->Format();
195 }
196
197 result
198 _MmcStorageManagerImpl::AddMmcStorageMountListener(IMmcStorageMountListener& listener)
199 {
200         return __pMountEvent->AddListener(listener);
201 }
202
203 result
204 _MmcStorageManagerImpl::RemoveMmcStorageMountListener(IMmcStorageMountListener& listener)
205 {
206         return __pMountEvent->RemoveListener(listener);
207 }
208
209 result
210 _MmcStorageManagerImpl::AddMmcStorageFormatListener(IMmcStorageFormatListener& listener)
211 {
212         return __pFormatEvent->AddListener(listener);
213 }
214
215 result
216 _MmcStorageManagerImpl::RemoveMmcStorageFormatListener(IMmcStorageFormatListener& listener)
217 {
218         return __pFormatEvent->RemoveListener(listener);
219 }
220
221 void
222 _MmcStorageManagerImpl::OnMmcMountResponseReceived(result r)
223 {
224         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(MOUNT_TYPE, r);
225         if(pEventArg != null)
226         {
227                 __pMountEvent->Fire(*pEventArg);
228         }
229 }
230
231 void
232 _MmcStorageManagerImpl::OnMmcUnmountResponseReceived(result r)
233 {
234         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(UNMOUNT_TYPE, r);
235         if(pEventArg != null)
236         {
237                 __pMountEvent->Fire(*pEventArg);
238         }
239 }
240
241 void
242 _MmcStorageManagerImpl::OnMmcFormatResponseReceived(result r)
243 {
244         _FormatEventArg* pEventArg= new (std::nothrow) _FormatEventArg(r);
245         if(pEventArg != null)
246         {
247                 __pFormatEvent->Fire(*pEventArg);
248         }
249 }
250
251 }} // Tizen::Io
252