Add DRAFT stubs for Vehicle plugin
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Application / Application.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef TIZENAPIS_PLATFORM_APPLICATION_H_
19 #define TIZENAPIS_PLATFORM_APPLICATION_H_
20
21 #include <map>
22 #include <set>
23 #include <list>
24 #include <Commons/Emitters.h>
25 #include <API/Application/IApplication.h>
26 #include <API/Application/ApplicationFactory.h>
27 #include <API/Application/EventListInstalledApplications.h>
28 #include <API/Application/EventGetApplication.h>
29 #include <API/Application/EventManageApplication.h>
30 #include <API/Application/EventInstalledApplicationChanged.h>
31 #include <API/Application/EventLaunchService.h>
32
33 //#include <app_manager.h>
34 #include <app_service.h>
35
36 #include "AppManagerWrapper.h"
37
38 using namespace TizenApis::Api::Application;
39
40 namespace TizenApis {
41 namespace Platform {
42 namespace Application {
43
44 class Application: public IApplication, public IAppManagerAppListChangedCallbacks {
45     friend class ApplicationFactory;
46
47 public:
48         Application();
49         virtual ~Application();
50         virtual void launch(const EventManageApplicationPtr& event);
51         virtual void kill(const EventManageApplicationPtr& event);
52         virtual void listApplications(const EventListInstalledApplicationsPtr& event);
53         virtual void getApplication(const EventGetApplicationPtr& event);
54         virtual long addApplicationInformationEventListener(const EventInstalledApplicationChangedEmitterPtr& emitter);
55         virtual void removeApplicationInformationEventListener(const long watchId);
56         virtual void launchService(const EventLaunchServicePtr& event);
57         virtual void getApplicationService(const EventLaunchServicePtr& event);
58         virtual void install(const EventApplicationInstallPtr& event);
59         virtual void uninstall(const EventApplicationInstallPtr& event);
60         virtual void update(const EventApplicationInstallPtr& event);
61
62         void launchServiceManualAnswer(service_h request, service_h reply, service_result_e result,
63                         Api::Application::EventLaunchServicePtr &event);
64         //void InstalledApplicationChanged(app_manger_event_type_e event_type,const char *package);
65         void callKillEventCallback(int pid);
66         void installAppAnswer(int req_id,
67                                                   const char* pkg_type,
68                                                   const char* pkg_name,
69                                                   const char* key,
70                                                   const char* val,
71                                                   const void* pmsg,
72                                                   int result,
73                                                   Api::Application::EventApplicationInstallPtr &event);
74
75 protected:
76         virtual void OnRequestReceived(const EventListInstalledApplicationsPtr& event);
77         virtual void OnRequestReceived(const EventManageApplicationPtr& event);
78         virtual void OnRequestReceived(const EventGetApplicationPtr& event);
79         virtual void OnRequestReceived(const EventLaunchServicePtr& event);     
80         virtual void OnRequestReceived(const EventApplicationInstallPtr& event);
81
82         // inherited from IAppManagerAppListChangedCallbacks
83         virtual void onAppManagerEventInstalled(AppManagerPackageInfoPtr &pkgInfo);
84         virtual void onAppManagerEventUninstalled(AppManagerPackageInfoPtr &pkgInfo);
85         virtual void onAppManagerEventUpdated(AppManagerPackageInfoPtr &pkgInfo);
86
87 private:
88         void initialize();
89         EventManageApplicationPtr getKillEvent(int pid);
90
91         DPL::Mutex m_initializationMutex;
92         bool m_initialized;
93
94         std::map<int, EventManageApplicationPtr> m_killEventMap;
95         mutable DPL::Mutex m_killMapLock;
96
97         typedef WrtDeviceApis::Commons::Emitters<TizenApis::Api::Application::EventInstalledApplicationChangedEmitter> EventInstalledApplicationChangedEmitters;
98         typedef std::map<long, TizenApis::Api::Application::EventInstalledApplicationChangedEmitter::IdType> WatchIdMap;
99
100         EventInstalledApplicationChangedEmitters m_installedApplicationsEmitters;
101         WatchIdMap      m_watchIdMap;
102 };
103
104 class LaunchServicePendingEvent
105 {
106 public:
107         LaunchServicePendingEvent(void *thisObject, const Api::Application::EventLaunchServicePtr &event) :
108                 m_thisObject(thisObject),
109                 m_event(event)
110         {
111         }
112
113         virtual ~LaunchServicePendingEvent()
114         {
115         }
116
117         void* getThisObject() const { return m_thisObject; }
118         Api::Application::EventLaunchServicePtr getEvent() const { return m_event; }
119
120 private:
121         void *m_thisObject;
122         Api::Application::EventLaunchServicePtr m_event;
123 };
124
125 class ApplicationInstallPendingEvent
126 {
127 public:
128         ApplicationInstallPendingEvent(void *thisObject, const Api::Application::EventApplicationInstallPtr &event) :
129                 m_thisObject(thisObject),
130                 m_event(event)
131         {
132         }
133
134         virtual ~ApplicationInstallPendingEvent()
135         {
136         }
137
138         void* getThisObject() const { return m_thisObject; }
139         Api::Application::EventApplicationInstallPtr getEvent() const { return m_event; }
140
141 private:
142         void *m_thisObject;
143         Api::Application::EventApplicationInstallPtr m_event;
144 };
145
146 template <class KeyType, class DataType>
147 class KeyMultiMap
148 {
149 public:
150         typedef unsigned int                                DataKeyType;
151         typedef DataType *                                  DataPtrType;
152         typedef std::pair<KeyType, DataPtrType>             KeyDataPairType;
153         typedef std::map<DataKeyType, KeyDataPairType>      DataMapType;
154         typedef std::set<DataKeyType>                       DataKeySetType;
155         typedef std::map<KeyType, DataKeySetType>           KeyMapType;
156         typedef std::list<DataPtrType>                      DataPtrListType;
157
158         KeyMultiMap() :
159                 m_keyAcc(0)
160         {
161         }
162
163         DataKeyType insert(const KeyType &key, const DataPtrType &dataPtr)
164         {
165                 DataKeyType dataKey = ++m_keyAcc;
166
167                 KeyDataPairType pair(key, dataPtr);
168                 m_dataMap.insert(std::pair<DataKeyType, KeyDataPairType>(dataKey, pair));
169
170                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
171                 if(keyMapIter == m_keyMap.end())
172                 {
173                         DataKeySetType newKeySet;
174                         m_keyMap.insert(std::pair<KeyType, DataKeySetType>(key, newKeySet));
175                         keyMapIter = m_keyMap.find(key);
176                 }
177
178                 DataKeySetType &dataKeySet = keyMapIter->second;
179
180                 dataKeySet.insert(dataKey);
181
182                 return dataKey;
183         }
184
185         DataPtrType getData(const DataKeyType &dataKey) const
186         {
187                 typename DataMapType::const_iterator dataMapIter = m_dataMap.find(dataKey);
188
189                 if(dataMapIter == m_dataMap.end())
190                         return static_cast<DataPtrType>(NULL);
191
192                 return dataMapIter->second.second;
193         }
194
195         DataPtrListType getDataPtrList(const KeyType &key)
196         {
197                 DataPtrListType dataPtrList;
198
199                 typename KeyMapType::const_iterator keyMapIter = m_keyMap.find(key);
200                 if(keyMapIter == m_keyMap.end())
201                         return dataPtrList;
202
203                 DataKeySetType keySet = keyMapIter->second;
204                 DataKeySetType::iterator keySetIter = keySet.begin();
205                 for(; keySetIter != keySet.end(); keySetIter++)
206                 {
207                         DataPtrType dataPtr = getData(*keySetIter);
208                         if(dataPtr == NULL)
209                         {
210                                 LogWarning("No data for " << *keySetIter);
211                                 break;
212                         }
213
214                         dataPtrList.push_back(dataPtr);
215                 }
216
217                 return dataPtrList;
218         }
219
220         void eraseData(const DataKeyType &dataKey)
221         {
222                 typename DataMapType::iterator dataKeyIter = m_dataMap.find(dataKey);
223
224                 if(dataKeyIter == m_dataMap.end())
225                 {
226                         LogDebug("No data for " << dataKey);
227                         return;
228                 }
229
230                 KeyType key = dataKeyIter->second.first;
231
232                 m_dataMap.erase(dataKeyIter);
233
234                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
235                 if(keyMapIter == m_keyMap.end())
236                 {
237                         LogWarning("No data for Key");
238                         return;
239                 }
240
241                 DataKeySetType &keySet = keyMapIter->second;
242                 DataKeySetType::iterator keySetIter = keySet.find(dataKey);
243                 if(keySetIter == keySet.end())
244                 {
245                         LogWarning("No data for " << dataKey);
246                         return;
247                 }
248
249                 keySet.erase(keySetIter);
250         }
251
252         void eraseKey(const KeyType &key)
253         {
254                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
255                 if(keyMapIter == m_keyMap.end())
256                 {
257                         LogDebug("No data to erase.");
258                         return;
259                 }
260
261                 DataKeySetType &keySet = keyMapIter->second;
262                 DataKeySetType::iterator keySetIter = keySet.begin();
263                 for(; keySetIter != keySet.end(); keySetIter++)
264                 {
265                         typename DataMapType::iterator dataKeyIter = m_dataMap.find(*keySetIter);
266                         if(dataKeyIter == m_dataMap.end())
267                         {
268                                 LogWarning("No data to erase.");
269                                 break;
270                         }
271
272                         m_dataMap.erase(dataKeyIter);
273                 }
274
275                 m_keyMap.erase(keyMapIter);
276         }
277
278 private:
279         DataKeyType m_keyAcc;
280
281         DataMapType m_dataMap;
282         KeyMapType m_keyMap;
283 };
284
285 }
286 }
287 }
288
289 #endif