Tizen 2.0 Release
[framework/osp/locations.git] / src / FLoc_LocationImpl.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 #include <new>
19 #include <unique_ptr.h>
20 #include <FBaseColArrayList.h>
21 #include <FBaseSysLog.h>
22 #include <FBaseDouble.h>
23 #include <FBaseDouble.h>
24 #include <FBaseUtilMath.h>
25 #include <FLocCoordinates.h>
26 #include <FLocLocation.h>
27 #include "FLoc_LocationImpl.h"
28 #include "FLoc_Types.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Base::Utility;
33 using namespace std;
34
35 namespace Tizen { namespace Locations
36 {
37
38 _LocationImpl::_LocationImpl(void)
39         : __speed(Tizen::Locations::NaN)
40         , __course(Tizen::Locations::NaN)
41         , __horizontalAccuracy(Tizen::Locations::NaN)
42         , __verticalAccuracy(Tizen::Locations::NaN)
43         , __timestamp(0)
44         , __locationMethod("")
45         , __satelliteInformation("")
46         , __isLocationValid(false)
47         , __isDenied(false)
48 {
49 }
50
51 _LocationImpl::_LocationImpl(const _LocationImpl& rhs)
52         : __coordinate(rhs.__coordinate)
53         , __speed(rhs.__speed)
54         , __course(rhs.__course)
55         , __horizontalAccuracy(rhs.__horizontalAccuracy)
56         , __verticalAccuracy(rhs.__verticalAccuracy)
57         , __timestamp(rhs.__timestamp)
58         , __locationMethod(rhs.__locationMethod)
59         , __satelliteInformation(rhs.__satelliteInformation)
60         , __isLocationValid(rhs.__isLocationValid)
61         , __isDenied(rhs.__isDenied)
62 {
63 }
64
65 _LocationImpl::~_LocationImpl(void)
66 {
67 }
68
69 bool
70 _LocationImpl::Equals(const _LocationImpl& rhs) const
71 {
72         return *this == rhs;
73 }
74
75 int
76 _LocationImpl::GetHashCode(void) const
77 {
78         int hashCode = 0;
79
80         hashCode += __coordinate.GetHashCode();
81         hashCode += Double::GetHashCode(__speed) * 37;
82         hashCode += Double::GetHashCode(__course) * 37;
83         hashCode += Double::GetHashCode(__horizontalAccuracy) * 37;
84         hashCode += Double::GetHashCode(__verticalAccuracy) * 37;
85         hashCode += __locationMethod.GetHashCode();
86         hashCode += __satelliteInformation.GetHashCode();
87         hashCode *= (__isLocationValid ? 37 : 17);
88
89         if (hashCode < 0)
90         {
91                 hashCode *= (-1);
92         }
93
94         return hashCode;
95 }
96
97 DateTime
98 _LocationImpl::GetTimestamp(void) const
99 {
100         TimeSpan timespan(__timestamp);
101         DateTime dateTime;
102
103         dateTime.SetValue(1970, 1, 1);
104         dateTime.Add(timespan);
105
106         SysLog(NID_LOC, "The location timeStamp is (%ls)", dateTime.ToString().GetPointer());
107
108         return dateTime;
109 }
110
111 Tizen::Base::String
112 _LocationImpl::GetExtraInfo(const Tizen::Base::String& key) const
113 {
114         String reqValue = L"";
115
116         if (key.Equals(L"location_method", true))
117         {
118                 reqValue = __locationMethod;
119         }
120         else if (key.Equals(L"satellite", true))
121         {
122                 reqValue = __satelliteInformation;
123         }
124
125         SysLog(NID_LOC, "Requested information is '%ls' for the key '%ls'.", reqValue.GetPointer(), key.GetPointer());
126
127         return reqValue;
128 }
129
130 _LocationImpl*
131 _LocationImpl::GetInstance(Location& obj)
132 {
133         return obj.__pImpl;
134 }
135
136 const _LocationImpl*
137 _LocationImpl::GetInstance(const Location& obj)
138 {
139         return obj.__pImpl;
140 }
141
142 Location*
143 _LocationImpl::GetLocationInstanceN(void)
144 {
145         Location* pLoc = new (std::nothrow) Location();
146         SysTryReturn(NID_LOC, pLoc != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
147         return pLoc;
148 }
149
150 Location
151 _LocationImpl::GetLocationInstance(void)
152 {
153         Location invalidLoc;
154
155         return invalidLoc;
156 }
157
158 result
159 _LocationImpl::SetCoordinates(const Coordinates& coordinate)
160 {
161         result r = E_SUCCESS;
162
163         r = __coordinate.Set(coordinate.GetLatitude(), coordinate.GetLongitude(), coordinate.GetAltitude());
164         SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Setting the latitude/longitude value failed.", GetErrorMessage(r));
165
166         return E_SUCCESS;
167 }
168
169 void
170 _LocationImpl::SetExtraInfo(const Tizen::Base::String& key, const Tizen::Base::String& value)
171 {
172         if (key.Equals(L"location_method", true))
173         {
174                 __locationMethod = value;
175         }
176         else if (key.Equals(L"satellite", true))
177         {
178                 __satelliteInformation = value;
179         }
180 }
181
182 _LocationImpl&
183 _LocationImpl::operator =(const _LocationImpl& rhs)
184 {
185         if (this == &rhs)
186         {
187                 return *this;
188         }
189
190         __coordinate = rhs.__coordinate;
191         __speed = rhs.__speed;
192         __course = rhs.__course;
193         __horizontalAccuracy = rhs.__horizontalAccuracy;
194         __verticalAccuracy = rhs.__verticalAccuracy;
195         __timestamp = rhs.__timestamp;
196         __locationMethod = rhs.__locationMethod;
197         __satelliteInformation = rhs.__satelliteInformation;
198         __isLocationValid = rhs.__isLocationValid;
199
200         return *this;
201 }
202
203 bool
204 _LocationImpl::operator ==(const _LocationImpl& rhs) const
205 {
206
207         if (!__coordinate.Equals(rhs.__coordinate))
208         {
209                 return false;
210         }
211
212         if (__timestamp != rhs.__timestamp)
213         {
214                 return false;
215         }
216
217         if (__locationMethod != rhs.__locationMethod)
218         {
219                 return false;
220         }
221
222         if (!__satelliteInformation.Equals(rhs.__satelliteInformation))
223         {
224                 return false;
225         }
226
227         if (__isLocationValid != rhs.__isLocationValid)
228         {
229                 return false;
230         }
231
232         bool speedCheck = false;
233         bool courseCheck = false;
234         bool horAccCheck = false;
235         bool verAccCheck = false;
236
237         if (Double::IsNaN(__speed) || Double::IsNaN(rhs.__speed))
238         {
239                 if (Double::IsNaN(__speed) && Double::IsNaN(rhs.__speed))
240                 {
241                         speedCheck = true;
242                 }
243         }
244         else if (Double::Compare(__speed, rhs.__speed) == 0)
245         {
246                 speedCheck = true;
247         }
248
249         if (Double::IsNaN(__course) || Double::IsNaN(rhs.__course))
250         {
251                 if (Double::IsNaN(__course) && Double::IsNaN(rhs.__course))
252                 {
253                         courseCheck = true;
254                 }
255         }
256         else if (Double::Compare(__course, rhs.__course) == 0)
257         {
258                 courseCheck = true;
259         }
260
261         if (Double::IsNaN(__horizontalAccuracy) || Double::IsNaN(rhs.__horizontalAccuracy))
262         {
263                 if (Double::IsNaN(__horizontalAccuracy) && Double::IsNaN(rhs.__horizontalAccuracy))
264                 {
265                         horAccCheck = true;
266                 }
267         }
268         else if (Double::Compare(__horizontalAccuracy, rhs.__horizontalAccuracy) == 0)
269         {
270                 horAccCheck = true;
271         }
272
273         if (Double::IsNaN(__verticalAccuracy) || Double::IsNaN(rhs.__verticalAccuracy))
274         {
275                 if (Double::IsNaN(__verticalAccuracy) && Double::IsNaN(rhs.__verticalAccuracy))
276                 {
277                         verAccCheck = true;
278                 }
279         }
280         else if (Double::Compare(__verticalAccuracy, rhs.__verticalAccuracy) == 0)
281         {
282                 verAccCheck = true;
283         }
284
285         return (speedCheck && courseCheck && horAccCheck && verAccCheck);
286 }
287
288 }}