clear last result to fix GetLastResult() error [N_SE-46568]
[platform/framework/native/shell.git] / src / FShell_AppWidgetProviderInfoImpl.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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        FShell_AppWidgetProviderInfoImpl.cpp
19  * @brief       This is the implementation for the _AppWidgetProviderInfoImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <livebox-service/livebox-service.h>
24 #include <FApp.h>
25 #include <FBase.h>
26 #include <FBaseSysLog.h>
27 #include <FBase_StringConverter.h>
28 #include <FApp_AppInfo.h>
29 #include <FApp_Aul.h>
30 #include <FAppPkg_PackageManagerImpl.h>
31 #include <FAppPkg_PackageInfoImpl.h>
32 #include <FShellAppWidgetProviderInfo.h>
33 #include <FShellAppWidgetSizeInfo.h>
34 #include "FShell_AppWidgetManagerImpl.h"
35 #include "FShell_AppWidgetProviderInfoImpl.h"
36 #include "FShell_AppWidgetSizeInfoImpl.h"
37
38 namespace Tizen { namespace Shell {
39
40 using namespace Tizen::App;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Graphics;
44
45
46 _AppWidgetProviderInfoImpl::_AppWidgetProviderInfoImpl(const Tizen::App::AppId& appId, const Tizen::Base::String& name)
47         :__appId(appId)
48         ,__name(name)
49         ,__pDisplayName(null)
50     ,__pConfigurationAppControlAppId(null)
51     ,__pIconPath(null)
52         ,__pProviderId(null)
53         ,__isDefault(false)
54 {
55         String providerId = _AppWidgetManagerImpl::MakeProviderName(appId, name);
56         __pProviderId = _StringConverter::CopyToCharArrayN(providerId);
57
58         SysSecureLog(NID_SHELL, "appId(%ls), name(%ls), providerId(%s)", appId.GetPointer(), name.GetPointer(), __pProviderId);
59 }
60
61 _AppWidgetProviderInfoImpl::~_AppWidgetProviderInfoImpl()
62 {
63         delete[] __pProviderId;
64         delete __pDisplayName;
65         delete __pConfigurationAppControlAppId;
66         delete __pIconPath;
67 }
68
69 Tizen::App::AppId
70 _AppWidgetProviderInfoImpl::GetAppId() const
71 {
72         return __appId;
73 }
74
75 Tizen::Base::String
76 _AppWidgetProviderInfoImpl::GetName() const
77 {
78         return __name;
79 }
80
81 Tizen::Base::String
82 _AppWidgetProviderInfoImpl::GetDisplayName()
83 {
84         if (__pDisplayName == null)
85         {
86                 std::unique_ptr<char> pDisplayName(livebox_service_i18n_name(__pProviderId, null));
87                 __pDisplayName = new(std::nothrow) String(pDisplayName.get());
88         }
89         return *__pDisplayName;
90 }
91
92 Tizen::Base::String
93 _AppWidgetProviderInfoImpl::GetIconPath()
94 {
95         if (__pIconPath == null)
96         {
97                 std::unique_ptr<char> pIconPath(livebox_service_i18n_icon(__pProviderId, null) );
98                 __pIconPath = new(std::nothrow) String(pIconPath.get());
99         }
100
101         return *__pIconPath;
102 }
103
104 Tizen::Base::String
105 _AppWidgetProviderInfoImpl::GetConfigurationAppControlAppId()
106 {
107         if (__pConfigurationAppControlAppId == null)
108         {
109                 std::unique_ptr<char> pSetUpAppId(livebox_service_setup_appid(__pProviderId));
110                 __pConfigurationAppControlAppId = new(std::nothrow) String(pSetUpAppId.get());
111         }
112         return *__pConfigurationAppControlAppId;
113 }
114
115 bool
116 _AppWidgetProviderInfoImpl::IsDefault() const
117 {
118         return __isDefault;
119 }
120
121 int
122 _AppWidgetProviderInfoImpl::GetPrivilegeLevel(const AppId& appId)
123 {
124
125         Package::_PackageManagerImpl* pPkg = Package::_PackageManagerImpl::GetInstance();
126         SysTryReturn(NID_APP, pPkg != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageManager instance.");
127
128         String pkgId = Package::_PackageManagerImpl::GetPackageIdByAppId(appId);
129         SysTryReturn(NID_APP, !pkgId.IsEmpty(), _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid appId(%ls).", appId.GetPointer() );
130
131         std::unique_ptr<Package::PackageInfo> pInfo (pPkg->GetPackageInfoN(pkgId) );
132         SysTryReturn(NID_APP, pInfo != null, _API_VISIBILITY_PUBLIC, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
133
134         const Package::_PackageInfoImpl* pPkgInfo = Package::_PackageInfoImpl::GetInstance(pInfo.get());
135         SysTryReturn(NID_APP, pPkgInfo != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageInfo instance.");
136
137         return pPkgInfo->GetApiVisibility();
138 }
139
140 bool
141 _AppWidgetProviderInfoImpl::IsValidSize(const AppId& appId, int w, int h)
142 {
143         if (IsInhouseApp(appId) == true)//TODO: remove this
144         {
145                 return true;
146         }
147
148 //      static const int UNIT_SIZE_BASE = 175;
149         static const int UNIT_SIZE_2 = 354;// UNIT_SIZE_BASE*n + 4*(n-1)
150
151         int privilegeLevel = GetPrivilegeLevel(appId);
152         if( privilegeLevel == _API_VISIBILITY_PUBLIC || privilegeLevel == _API_VISIBILITY_PARTNER)
153         {
154                  if( h > UNIT_SIZE_2 || w > UNIT_SIZE_2)
155                  {
156                          SysLog(NID_SHELL, "%dx%d size is invalid for %d privilege", privilegeLevel);
157                          return false;
158                  }
159         }
160         /*else if( size.height > UNIT_SIZE_4 || size.width > UNIT_SIZE_4)
161         {
162                 return false;
163         }*/
164         return true;
165 }
166
167 Tizen::Base::Collection::IList*
168 _AppWidgetProviderInfoImpl::GetSizeInfoListN() const
169 {
170         static const int MAX_SUPPORTED_SIZE_TYPE = NR_OF_SIZE_LIST;
171
172         int supportedSizeCount = MAX_SUPPORTED_SIZE_TYPE;
173         int widthArray[MAX_SUPPORTED_SIZE_TYPE];
174         int heightArray[MAX_SUPPORTED_SIZE_TYPE];
175
176         livebox_service_get_supported_sizes(__pProviderId, &supportedSizeCount , widthArray, heightArray);
177
178         SysTryReturn(NID_SHELL, supportedSizeCount > 0, null, E_SYSTEM, "[E_SYSTEM] There is no appwidget of provider(%s)",__pProviderId );
179
180         std::unique_ptr<ArrayList> pSizes(new (std::nothrow) ArrayList);
181         SysTryReturn(NID_SHELL, pSizes, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory for ArrayList." );
182
183         pSizes->Construct();
184         FloatDimension size(0, 0);
185         String previewImagePath;
186
187         for(int i = 0; i < supportedSizeCount; i++)
188         {
189                 size.SetSize(widthArray[i], heightArray[i]);
190                 previewImagePath  = livebox_service_preview(__pProviderId, livebox_service_size_type(static_cast<int>(size.width), static_cast<int>(size.height) ) );
191
192                 if( IsValidSize(__appId, size.width, size.height) == false )
193                 {
194                         continue;
195                 }
196
197                 int sizeType = livebox_service_size_type(size.width, size.height);
198                 SysAssertf(sizeType != LB_SIZE_TYPE_UNKNOWN, "Failed to invoke livebox_service_size_type(%d, %d)", size.width, size.height);
199                 bool useDecoration = livebox_service_need_frame(__pProviderId, sizeType);
200
201                 std::unique_ptr<AppWidgetSizeInfo> pAppWidgetSizeInfo(_AppWidgetSizeInfoImpl::CreateAppWidgetSizeInfo(size, previewImagePath, useDecoration) );
202                 SysTryReturn(NID_SHELL, pAppWidgetSizeInfo, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory for AppWidgetSizeInfo." );
203                 pSizes->Add( *pAppWidgetSizeInfo.release() );
204         }
205         return pSizes.release();
206 }
207
208 AppWidgetProviderInfo*
209 _AppWidgetProviderInfoImpl::GetAppWidgetProviderInfoN(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName)
210 {
211         std::unique_ptr<char[]> pProviderId(_StringConverter::CopyToCharArrayN(_AppWidgetManagerImpl::MakeProviderName(appId, providerName)));
212
213         bool isDefault = livebox_service_is_primary(pProviderId.get());
214         return GetAppWidgetProviderInfoN(appId, providerName, isDefault);
215 }
216
217 AppWidgetProviderInfo*
218 _AppWidgetProviderInfoImpl::GetAppWidgetProviderInfoN(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, bool isDefault)
219 {
220         SysTryReturn(NID_SHELL, Tizen::App::_Aul::IsInstalled(appId) == true, null, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
221         SysTryReturn(NID_SHELL, IsExist(appId, providerName) == true, null, E_OBJ_NOT_FOUND, "The provider(%ls.%ls) isn't found.", appId.GetPointer(), providerName.GetPointer() );
222
223         AppWidgetProviderInfo* pInfo = new (std::nothrow) AppWidgetProviderInfo(appId, providerName);
224         SysTryReturn(NID_SHELL, pInfo, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory for AppWidgetProviderInfo." );
225
226         _AppWidgetProviderInfoImpl::GetImpl(*pInfo)->__isDefault = isDefault;
227
228         return pInfo;
229 }
230
231 bool
232 _AppWidgetProviderInfoImpl::IsExist(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName)
233 {
234         std::unique_ptr<char[]> pProviderId(_StringConverter::CopyToCharArrayN(_AppWidgetManagerImpl::MakeProviderName(appId, providerName)));
235         std::unique_ptr<char> pPackageId( livebox_service_appid(pProviderId.get()) );
236         SysTryReturn(NID_SHELL, pPackageId.get(), false, E_OBJ_NOT_FOUND, "The provider(%s) isn't found.", pProviderId.get() );
237
238         return true;
239 }
240
241 _AppWidgetProviderInfoImpl*
242 _AppWidgetProviderInfoImpl::GetImpl(const AppWidgetProviderInfo& appwidgetProviderInfo)
243 {
244         return static_cast<_AppWidgetProviderInfoImpl*>(appwidgetProviderInfo.__pAppWidgetProviderInfoImpl);
245 }
246
247 bool
248 _AppWidgetProviderInfoImpl::IsInhouseApp(const Tizen::App::AppId& appId)
249 {
250         return (appId.StartsWith("com.samsung.", 0) == true);
251 }
252
253 } /* namespace AppWidget */
254 } /* namespace Samsung */