Migrate from 2.4 code repo
[platform/core/context/place-context-provider.git] / src / place_context_provider.cpp
1 /*
2  * Copyright (c) 2014 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 #include <string>
18 #include <map>
19 #include <glib.h>
20 #include <json.h>
21 #include <types_internal.h>
22 #include <context_mgr.h>
23 #include <place_context_provider.h>
24 #include "sub_provider_base.h"
25
26 #ifdef _MOBILE_
27 #include "place_geofence/place_geofence.h"
28 #endif
29
30 #define CREATE_SUB_PROVIDER(subject, subprovider) do { \
31         ctx::sub_provider_base *sub = new ctx::subprovider(); \
32         if (sub->is_supported(NULL)) { \
33                 subject_map[(subject)] = sub; \
34         } else { \
35                 _E("%s is not supported", subject); \
36                 delete sub; \
37         }} while (0)
38
39 typedef std::map<std::string, ctx::sub_provider_base*> subject_map_t;
40 static subject_map_t subject_map;
41
42 static void load_subjects()
43 {
44 #ifdef _MOBILE_
45         CREATE_SUB_PROVIDER(PLACE_SUBJ_GEOFENCE, place_geofence_detector);
46 #endif
47 }
48
49 static void unload_subjects()
50 {
51         for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) {
52                 delete it->second;
53         }
54
55         subject_map.clear();
56 }
57
58 EXTAPI ctx::place_context_provider::place_context_provider()
59 {
60 }
61
62 EXTAPI ctx::place_context_provider::~place_context_provider()
63 {
64         unload_subjects();
65 }
66
67 bool ctx::place_context_provider::init()
68 {
69         IF_FAIL_RETURN(subject_map.empty(), true);
70
71         try {
72                 load_subjects();
73         } catch (std::bad_alloc& ba) {
74                 _E("Memory allocation failed");
75                 return false;
76         }
77
78         for (subject_map_t::iterator it = subject_map.begin(); it != subject_map.end(); ++it) {
79                 context_manager::register_provider(it->first.c_str(), this);
80         }
81
82         _D("Done");
83         return true;
84 }
85
86 bool ctx::place_context_provider::is_supported(const char* subject, const char* zone)
87 {
88         subject_map_t::iterator it = subject_map.find(subject);
89
90         if (it != subject_map.end())
91                 return it->second->is_supported(zone);
92
93         return false;
94 }
95
96 int ctx::place_context_provider::subscribe(const char* subject, ctx::json option, ctx::json* request_result, const char* zone)
97 {
98         subject_map_t::iterator it = subject_map.find(subject);
99
100         if (it != subject_map.end())
101                 return it->second->subscribe(option, request_result, zone);
102
103         return ERR_NOT_SUPPORTED;
104 }
105
106 int ctx::place_context_provider::unsubscribe(const char* subject, ctx::json option, const char* zone)
107 {
108         subject_map_t::iterator it = subject_map.find(subject);
109
110         if (it != subject_map.end())
111                 return it->second->unsubscribe(option, zone);
112
113         return ERR_NOT_SUPPORTED;
114 }
115
116 int ctx::place_context_provider::read(const char* subject, ctx::json option, ctx::json* request_result, const char* zone)
117 {
118         subject_map_t::iterator it = subject_map.find(subject);
119
120         if (it != subject_map.end())
121                 return it->second->read(option, request_result, zone);
122
123         return ERR_NOT_SUPPORTED;
124 }
125
126 int ctx::place_context_provider::write(const char* subject, ctx::json data, ctx::json* request_result, const char* zone)
127 {
128         subject_map_t::iterator it = subject_map.find(subject);
129
130         if (it != subject_map.end())
131                 return it->second->write(data, request_result, zone);
132
133         return ERR_NOT_SUPPORTED;
134 }