Fix : Search APIs don't return exception despite the content in DB doesn't match...
[platform/framework/native/content.git] / src / FCnt_DownloadRequestImpl.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        FCnt_DownloadRequestImpl.cpp
19  * @brief       This is the implementation file for the _DownloadRequestImpl class.
20  *
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseLog.h>
26 #include <FBaseSysLog.h>
27 #include <FCntDownloadRequest.h>
28
29 #include "FCnt_DownloadRequestImpl.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace std;
34
35 namespace Tizen { namespace Content
36 {
37
38 _DownloadRequestImpl::_DownloadRequestImpl(void)
39         : __notification(true)
40         , __networkType(DOWNLOAD_NETWORK_ALL)
41 {
42         __notifyExtraData.Construct();
43         __requestHeader.Construct();
44 }
45
46 _DownloadRequestImpl::_DownloadRequestImpl(const String& url)
47         : __url(url)
48         , __notification(true)
49         , __networkType(DOWNLOAD_NETWORK_ALL)
50 {
51         __notifyExtraData.Construct();
52         __requestHeader.Construct();
53 }
54
55 _DownloadRequestImpl::_DownloadRequestImpl(const String& url, const String& dirPath)
56         : __url(url)
57         , __dirPath(dirPath)
58         , __notification(true)
59         , __networkType(DOWNLOAD_NETWORK_ALL)
60 {
61         __notifyExtraData.Construct();
62         __requestHeader.Construct();
63 }
64
65 _DownloadRequestImpl::_DownloadRequestImpl(const _DownloadRequestImpl& rhs)
66         : __url(rhs.__url)
67         , __dirPath(rhs.__dirPath)
68         , __fileName(rhs.__fileName)
69         , __notification(rhs.__notification)
70         , __networkType(rhs.__networkType)
71 {
72         __notifyExtraData.Construct(rhs.__notifyExtraData);
73         __requestHeader.Construct(rhs.__requestHeader);
74 }
75
76 _DownloadRequestImpl::~_DownloadRequestImpl(void)
77 {
78 }
79
80
81 _DownloadRequestImpl&
82 _DownloadRequestImpl::operator =(const _DownloadRequestImpl& rhs)
83 {
84         __url = rhs.__url;
85         __dirPath = rhs.__dirPath;
86         __fileName = rhs.__fileName;
87         __notification = rhs.__notification;
88         __networkType = rhs.__networkType;
89         __notifyExtraData.RemoveAll();
90         __notifyExtraData.Construct(rhs.__notifyExtraData);
91         __requestHeader.RemoveAll();
92         __requestHeader.Construct(rhs.__requestHeader);
93
94         return *this;
95 }
96
97 bool
98 _DownloadRequestImpl::operator ==(const _DownloadRequestImpl& rhs) const
99 {
100         if (&rhs == this)
101         {
102                 return true;
103         }
104
105         if (__url == rhs.__url
106                 && __dirPath == rhs.__dirPath
107                 && __fileName == rhs.__fileName)
108         {
109                 return true;
110         }
111
112         return false;
113 }
114
115 bool
116 _DownloadRequestImpl::Equals(const Object& obj) const
117 {
118         const _DownloadRequestImpl* pOther = dynamic_cast< const _DownloadRequestImpl* >(&obj);
119         if (pOther == null)
120         {
121                 return false;
122         }
123
124         return (*this == *pOther);
125 }
126
127 int
128 _DownloadRequestImpl::GetHashCode(void) const
129 {
130         String hashCode;
131
132         hashCode.Append(__url);
133         hashCode.Append(__dirPath);
134         hashCode.Append(__fileName);
135
136         return hashCode.GetHashCode();
137 }
138
139 void
140 _DownloadRequestImpl::SetDirectoryPath(const String& dirPath)
141 {
142         __dirPath = dirPath;
143 }
144
145 void
146 _DownloadRequestImpl::SetFileName(const String& fileName)
147 {
148         __fileName = fileName;
149 }
150
151 String
152 _DownloadRequestImpl::GetUrl(void) const
153 {
154         return __url;
155 }
156
157 String
158 _DownloadRequestImpl::GetDirectoryPath(void) const
159 {
160         return __dirPath;
161 }
162
163 String
164 _DownloadRequestImpl::GetFileName(void) const
165 {
166         return __fileName;
167 }
168
169 void
170 _DownloadRequestImpl::SetNotification(bool enable)
171 {
172         __notification = enable;
173 }
174
175 result
176 _DownloadRequestImpl::SetNotificationExtraData(const IMap* pExtraData)
177 {
178         String* pKey = null;
179         String* pValue = null;
180         result r = E_SUCCESS;
181
182         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(pExtraData)->GetMapEnumeratorN());
183         while (pMapEnum->MoveNext() == E_SUCCESS)
184         {
185                 pKey = dynamic_cast< String* >(pMapEnum->GetKey());
186                 SysTryReturnResult(NID_CNT, pKey != null, E_INVALID_ARG, "The object is not a String class.");
187
188                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
189                 SysTryReturnResult(NID_CNT, pValue != null, E_INVALID_ARG, "The object is not a String class.");
190
191                 r = __notifyExtraData.Add(new String(*pKey), new String(*pValue));
192         }
193         return r;
194 }
195
196 void
197 _DownloadRequestImpl::SetNetworkType(DownloadNetworkType type)
198 {
199         __networkType = type;
200 }
201
202 bool
203 _DownloadRequestImpl::IsNotificationEnabled(void) const
204 {
205         return __notification;
206 }
207
208 const IMap*
209 _DownloadRequestImpl::GetNotificationExtraData(void) const
210 {
211         return &__notifyExtraData;
212 }
213
214 DownloadNetworkType
215 _DownloadRequestImpl::GetNetworkType(void) const
216 {
217         return __networkType;
218 }
219
220 result
221 _DownloadRequestImpl::AddRequestHeader(const String& field, const String& value)
222 {
223         SysTryReturnResult(NID_CNT, !field.IsEmpty(), E_INVALID_ARG, "The field is empty.");
224         SysTryReturnResult(NID_CNT, !__requestHeader.ContainsKey(field), E_INVALID_ARG, "The field already exist.");
225         return __requestHeader.Add(new String(field), new String(value));
226 }
227
228 result
229 _DownloadRequestImpl::SetRequestHeader(const String& field, const String& value)
230 {
231         SysTryReturnResult(NID_CNT, !field.IsEmpty(), E_INVALID_ARG, "The field is empty.");
232         SysTryReturnResult(NID_CNT, __requestHeader.ContainsKey(field), E_INVALID_ARG, "The field does not exist.");
233         return __requestHeader.SetValue(field, new String(value));
234 }
235
236 result
237 _DownloadRequestImpl::RemoveRequestHeader(const String& field)
238 {
239         SysTryReturnResult(NID_CNT, !field.IsEmpty(), E_INVALID_ARG, "The field is empty.");
240         SysTryReturnResult(NID_CNT, __requestHeader.ContainsKey(field), E_INVALID_ARG, "The field does not exist.");
241         return __requestHeader.Remove(field);
242 }
243
244 String*
245 _DownloadRequestImpl::GetRequestHeaderN(const String& field)
246 {
247         String* pValue = null;
248         String* pKey = null;
249         IMapEnumerator* pMapEnum = null;
250
251         pMapEnum = __requestHeader.GetMapEnumeratorN();
252         while (pMapEnum->MoveNext() == E_SUCCESS)
253         {
254                 pKey = static_cast< String* > (pMapEnum->GetKey());
255                 if (*pKey == field)
256                 {
257                         pValue = new String (*(static_cast< String* > (pMapEnum->GetValue())));
258                         SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
259                         break;
260                 }
261         }
262
263         delete pMapEnum;
264         return pValue; 
265 }
266
267 _DownloadRequestImpl*
268 _DownloadRequestImpl::GetInstance(const DownloadRequest* pRequest)
269 {
270         return pRequest->__pDownloadRequestImpl;
271 }
272
273 IMap*
274 _DownloadRequestImpl::GetRequestHeader(void)
275 {
276         return &__requestHeader;
277 }
278
279 } } // Tizen::Content