apply Pre-ACR result, add _LiveboxPopupProviderImpl
[platform/framework/native/shell.git] / src / FShellLiveboxManager.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 /**
19  * @file        FShellLiveboxManager.cpp
20  * @brief       This is the implementation for the LiveboxManager class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <shortcut.h>
26 #include <livebox-service/livebox-service.h>
27
28 #include <FBaseSysLog.h>
29 #include <FBaseCol.h>
30 #include <FGraphics.h>
31 #include <FApp.h>
32 #include <FAppPkgPackageInfo.h>
33 #include <FApp_Types.h>
34 #include <FApp_AppInfo.h>
35 #include <FAppPkg_PackageManagerImpl.h>
36 #include <FBase_StringConverter.h>
37
38 #include <FShellLiveboxManager.h>
39 #include <FShellLiveboxProviderInfo.h>
40 #include "FShell_LiveboxManagerImpl.h"
41
42 using namespace std;
43 using namespace Tizen::App;
44 using namespace Tizen::App::Package;
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Graphics;
48
49
50 namespace Tizen { namespace Shell
51 {
52
53 LiveboxManager::LiveboxManager()
54         :__pLiveboxManagerImpl(null)
55 {
56
57 }
58
59 LiveboxManager::~LiveboxManager()
60 {
61 }
62
63 LiveboxManager*
64 LiveboxManager::GetInstance()
65 {
66         static LiveboxManager* pLiveboxManager = null;
67         if( pLiveboxManager == null)
68         {
69                 pLiveboxManager = new LiveboxManager;
70                 SysTryReturn(NID_APP, pLiveboxManager, null, E_OUT_OF_MEMORY, "E_OUT_OF_MEMORY" );
71
72                 result r = pLiveboxManager->Construct();
73 //              SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
74                 SysAssertf(!IsFailed(r), "Failed to construct LiveboxManager!!");
75                 SysLog(NID_APP, "LiveboxManager instance is created.");
76         }
77
78         return pLiveboxManager;
79 }
80
81 result
82 LiveboxManager::Construct()
83 {
84         SysAssert(__pLiveboxManagerImpl == null);
85
86         SysLog(NID_APP, "Enter.");
87         __pLiveboxManagerImpl = _LiveboxManagerImpl::GetInstance();
88         SysAssertf(__pLiveboxManagerImpl, "Failed to construct _LiveboxManagerImpl!!");
89
90         SysLog(NID_APP, "Exit.");
91
92         return E_SUCCESS;
93 }
94
95 result
96 LiveboxManager::SetLiveboxProviderFactory(ILiveboxProviderFactory& factory)
97 {
98         return __pLiveboxManagerImpl->SetLiveboxProviderFactory(factory);
99 }
100
101 result
102 LiveboxManager::SetLiveboxPopupProviderFactory(ILiveboxPopupProviderFactory& factory)
103 {
104         return __pLiveboxManagerImpl->SetLiveboxPopupProviderFactory(factory);
105 }
106
107 //result
108 //LiveboxManager::HandleRequest(const Tizen::Base::String& operationId, Tizen::Base::String* pCategory, Tizen::Base::Collection::IList* pArgs)
109 //{
110 //      return __pLiveboxManagerImpl->HandleRequest(operationId, pCategory, pArgs);
111 //}
112
113 Tizen::Base::Collection::IList*
114 LiveboxManager::GetLiveboxProviderInfosN()
115 {
116         return __pLiveboxManagerImpl->GetLiveboxProviderInfosN();
117 }
118
119 LiveboxProviderInfo*
120 LiveboxManager::GetLiveboxProviderInfoN(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName)
121 {
122         // TODO : set configurationAppcontrolAppId
123         return new LiveboxProviderInfo(appId, providerName);
124 }
125
126
127 /*
128 result
129 LiveboxManager::RequestUpdate()
130 {
131         Tizen::App::AppId appId(Tizen::App::_AppInfo::GetAppId());
132
133         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(appId));
134         int ret = livebox_service_trigger_update(pPackageId.get(), "user,created", "default");
135         SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM, "[E_SYSTEM] Failed to invoke livebox_service_trigger_update, ret (%d)", ret);
136
137         return E_SUCCESS;
138 }
139 */
140
141 result
142 LiveboxManager::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument)
143 {
144         __pLiveboxManagerImpl->RequestUpdate(appId, providerName);
145
146         return E_SUCCESS;
147 }
148
149 //void
150 //LiveboxManager::TerminateAppIfInvalidState()
151 //{
152 //      __pLiveboxManagerImpl->TerminateAppIfInvalidState();
153 //}
154
155
156 namespace
157 {
158 int AddLiveboxCallback(int ret, int pid, void *data)
159 {
160         if( ret != 0)
161         {
162                 SysLog(NID_APP, "Error.(%d)", ret);
163         }
164
165         return 0;
166 }
167 }
168
169
170 result
171 LiveboxManager::AddLivebox(const Tizen::App::AppId& targetAppId, const Tizen::Base::String& providerAppId, const Tizen::Base::String& providerName, const Tizen::Base::String& text, const Tizen::Base::String& userInfo)
172 {
173         SysTryReturnResult(NID_APP, providerAppId.IsEmpty()==false, E_INVALID_ARG, "The providerAppId is empty.");
174         SysTryReturnResult(NID_APP, providerAppId.GetLength() < NATIVE_APP_MAX_APPID_LENGTH, E_INVALID_ARG, "The providerAppId is too long (Maximum %d bytes).", NATIVE_APP_MAX_APPID_LENGTH);
175         SysTryReturnResult(NID_APP, _PackageManagerImpl::IsAppInstalled(providerAppId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", providerAppId.GetPointer());
176
177         //TODO :: check privilege
178         String providerId;
179         if( providerAppId.StartsWith(L"com.samsung", 0) == true )
180         {
181                 SysLog(NID_APP, "[E_INVALID_OPERATION] but allowed to test");
182                 //TODO:return E_INVALID_OPERATION
183                 providerId = providerAppId;
184         }
185         else
186         {
187                 String localPackageId = _PackageManagerImpl::GetPackageIdByAppId(Tizen::App::App::GetInstance()->GetAppId());
188                 String liveboxProviderPackageId = _PackageManagerImpl::GetPackageIdByAppId(providerAppId);
189                 if( localPackageId != liveboxProviderPackageId)
190                 {
191                         SysLog(NID_APP, "[E_INVALID_OPERATION] but allowed to test");
192                         //TODO:return E_INVALID_OPERATION
193                 }
194                 providerId = providerAppId + "." + providerName;
195         }
196
197         PackageInfo* pPackageInfo = null;
198         result r = E_SUCCESS;
199
200         pPackageInfo = _PackageManagerImpl::GetInstance()->GetPackageInfoN(providerAppId);
201         SysTryReturn(NID_APP, pPackageInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
202
203         String iconPath; //= packageInfo.GetAppIconPath();
204
205         delete pPackageInfo;
206
207         std::unique_ptr<char[]> pProviderId(_StringConverter::CopyToCharArrayN(providerId));
208         std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(text));
209         std::unique_ptr<char[]> pContent(_StringConverter::CopyToCharArrayN(userInfo));
210         std::unique_ptr<char[]> pIcon(_StringConverter::CopyToCharArrayN(iconPath));
211
212         int ret = add_to_home_livebox(pProviderId.get(), pName.get(), LAUNCH_BY_PACKAGE, pContent.get(), pIcon.get(), -1.0l, AddLiveboxCallback, this);
213         SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM, "[E_SYSTEM] failed to add_to_home_livebox (%d)", ret);
214
215         return E_SUCCESS;
216 }
217
218 // c style callback for shortcut_set_request_cb
219 int LiveboxRequestEventHandler(const char* providerId, const char* text, int type, const char* content_info, const char* icon, int pid, double period, void *data)
220 {
221         SysTryReturn(NID_APP, providerId, 0, E_SYSTEM, "[E_SYSTEM] providerId is null!");
222
223         LiveboxManager* pLiveboxManager = static_cast<LiveboxManager*>(data);
224         SysTryReturn(NID_APP, pLiveboxManager, 0, E_SYSTEM, "[E_SYSTEM] pLiveboxManager is null!");
225
226         String appId;
227         String providerName;
228         _LiveboxManagerImpl::ExtractAppIdAndProviderName(providerId, appId, providerName);
229
230         //OnLiveboxAddRequested(const Tizen::Base::AppId& appId, const const Tizen::Base::String& providerName, const Tizen::Base::String& userInfo, const const Tizen::Base::String& text, const Tizen::Base::String& iconPath)
231         pLiveboxManager->__pLiveboxRequestEventListener->OnLiveboxAddRequested(appId, providerName, content_info, text );
232
233         return 0;
234 }
235
236 result
237 LiveboxManager::SetLiveboxRequestEventHandler(ILiveboxRequestEventListener& listener)
238 {
239         int ret = shortcut_set_request_cb(LiveboxRequestEventHandler, this );
240         SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM, "failed to shortcut_set_request_cb (%d)", ret);
241
242         __pLiveboxRequestEventListener = &listener;
243         return E_SUCCESS;
244 }
245
246
247 }} // Tizen::Shell