fix proxy not to set instance when it's failed to construct, check exception for...
[platform/framework/native/shell.git] / src / core / FShell_ShortcutManagerImpl.cpp
1 //
2 // Copyright (c) 2013 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 /**
18  * @file        FShell_ShortcutManagerImpl.cpp
19  * @brief       This is the implementation for the _ShortcutManagerImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <shortcut.h>
24 #include <FBaseSysLog.h>
25 #include <FBase.h>
26 #include <FBase_StringConverter.h>
27 #include <FApp_Aul.h>
28 #include <FApp.h>
29 #include "FShell_ShortcutManagerImpl.h"
30
31
32 namespace Tizen { namespace Shell
33 {
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::App;
38
39 request_cb_t __pAppWidgetRequestHandlerCallback = null;
40
41 _ShortcutManagerImpl::_ShortcutManagerImpl()
42         : __isListenerConstructed(false)
43 {
44 }
45
46 _ShortcutManagerImpl::~_ShortcutManagerImpl()
47 {
48 }
49
50
51 _ShortcutManagerImpl*
52 _ShortcutManagerImpl::GetInstance()
53 {
54         static _ShortcutManagerImpl* __pShortcutManagerImpl = null;
55         if( __pShortcutManagerImpl == null)
56         {
57                 __pShortcutManagerImpl = new (std::nothrow)_ShortcutManagerImpl;
58                 SysTryReturn(NID_SHELL, __pShortcutManagerImpl, null, E_OUT_OF_MEMORY, "Allocating new _AppWidgetManagerProxy failed.");
59
60                 result r = __pShortcutManagerImpl->Construct();
61                 SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
62         }
63         return __pShortcutManagerImpl;
64 }
65
66 int ShortcutRequestEventHandlerCB(const char* appId, const char* text, int type, const char* content_info, const char* icon, int pid, double period, bool allowDuplication, void *data)
67 {
68         SysTryReturn(NID_SHELL, appId, 0, E_SYSTEM, "[E_SYSTEM] appId is null!");
69         SysLog(NID_SHELL, "Tizen::Shell::ShortcutManager - Shortcut request received.");
70
71         _ShortcutManagerImpl* pShortcutManager = static_cast<_ShortcutManagerImpl*>(data);
72         SysTryReturn(NID_SHELL, pShortcutManager, 0, E_SYSTEM, "[E_SYSTEM] pShortcutManager is null!");
73
74         const ArrayListT<IShortcutRequestListener*>* pListenerList = pShortcutManager->GetShortcutRequestListenerList();
75         IShortcutRequestListener* pListener;
76
77         SysLog(NID_SHELL, "There is(are) %d request handler(s).", pListenerList->GetCount());
78         for(int i = 0; i < pListenerList->GetCount(); i ++)
79         {
80                 pListener = null;
81                 pListenerList->GetAt(i, pListener);
82                 if( pListener != null)
83                 {
84                         if( type == SHORTCUT_REMOVE)
85                         {
86                                 pListener->OnShortcutRemoveRequested(appId, text);
87                         }
88                         else
89                         {
90                                 pListener->OnShortcutAddRequested(appId, text, icon, content_info);
91                         }
92                 }
93         }
94
95         SysLog(NID_SHELL, "Successed.");
96         return 0;
97 }
98
99 // c - style callback for shortcut_set_request_cb
100 int ShortcutRequestEventReceiverCB(const char* appId, const char* text, int type, const char* content_info, const char* icon, int pid, double period, int allowDuplication, void *data)
101 {
102         SysTryReturn(NID_SHELL, appId, 0, E_SYSTEM, "[E_SYSTEM] providerId is null!");
103
104         if( type == SHORTCUT_PACKAGE
105                 || type == SHORTCUT_DATA
106                 || type == SHORTCUT_REMOVE)
107         {
108                 ShortcutRequestEventHandlerCB(appId, text, type, content_info, icon, pid, period, allowDuplication, data);
109         }
110         else
111         {
112                 if( __pAppWidgetRequestHandlerCallback != null)
113                 {
114                         SysLog(NID_SHELL, "Invoking appwidget request handler.");
115                         __pAppWidgetRequestHandlerCallback(appId, text, type, content_info, icon, pid, period, allowDuplication, data);
116                 }
117         }
118
119         SysLog(NID_SHELL, "Successed.");
120         return 0;
121 }
122
123 result
124 _ShortcutManagerImpl::Construct()
125 {
126         return E_SUCCESS;
127 }
128
129 result
130 _ShortcutManagerImpl::ConstructListener()
131 {
132         int ret = shortcut_set_request_cb(ShortcutRequestEventReceiverCB, this );
133         SysTryReturnResult(NID_SHELL, ret == 0, E_SYSTEM, "failed to shortcut_set_request_cb (%d)", ret);
134
135         __shortcutRequestListenerList.Construct();
136
137         __isListenerConstructed = true;
138         SysLog(NID_SHELL, "Successed.");
139
140         return E_SUCCESS;
141 }
142
143 result
144 _ShortcutManagerImpl::AddShortcut(const AppId& appId, const String& displayName, const String& iconFilePath, const String& uriData, bool allowDuplication)
145 {
146         SysTryReturnResult(NID_SHELL, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
147
148         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
149         std::unique_ptr<char[]> pIcon(_StringConverter::CopyToCharArrayN(iconFilePath));
150         std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
151         std::unique_ptr<char[]> pUriData(_StringConverter::CopyToCharArrayN(uriData));
152         int type = (uriData.IsEmpty())? LAUNCH_BY_PACKAGE : LAUNCH_BY_URI;
153
154         int ret = add_to_home_shortcut(pAppId.get(), pName.get(), type, pUriData.get(), pIcon.get(), allowDuplication, NULL, this);
155         SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_shortcut, but regarded as success.");
156         SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_shortcut.");
157         SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "Failed to add_to_home_shortcut");
158
159         SysLog(NID_SHELL, "Successed.");
160         return E_SUCCESS;
161 }
162
163 result
164 _ShortcutManagerImpl::RemoveShortcut(const Tizen::App::AppId& appId, const Tizen::Base::String& displayName)
165 {
166         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
167         std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(displayName));
168
169         int ret = add_to_home_remove_shortcut(pAppId.get(), pName.get(), "", null, this);
170         SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_COMM, E_SUCCESS, "It's failed to add_to_home_remove_shortcut, but regarded as success.");
171         SysTryReturnResult(NID_SHELL, ret != SHORTCUT_ERROR_MEMORY, E_OUT_OF_MEMORY, "It's failed to add_to_home_remove_shortcut.");
172         SysTryReturnResult(NID_SHELL, ret == SHORTCUT_SUCCESS, E_SYSTEM, "It's failed to add_to_home_remove_shortcut, with reason (%d)");
173
174         return E_SUCCESS;
175 }
176
177 result
178 _ShortcutManagerImpl::AddShortcutRequestListener(IShortcutRequestListener& listener)
179 {
180         SysTryReturnResult(NID_SHELL, !__shortcutRequestListenerList.Contains(&listener), E_OBJ_ALREADY_EXIST, "The listener is already added.");
181         SysLog(NID_SHELL, "(%x)", &listener);
182         if( __isListenerConstructed == false)
183         {
184                 ConstructListener();
185         }
186         return __shortcutRequestListenerList.Add(&listener);
187 }
188
189 result
190 _ShortcutManagerImpl::RemoveShortcutRequestListener(IShortcutRequestListener& listener)
191 {
192         SysLog(NID_SHELL, "(%x)", &listener);
193         return  __shortcutRequestListenerList.Remove(&listener);
194 }
195
196 const Tizen::Base::Collection::ArrayListT<IShortcutRequestListener*>*
197 _ShortcutManagerImpl::GetShortcutRequestListenerList(void) const
198 {
199         return &__shortcutRequestListenerList;
200 }
201
202 void
203 _ShortcutManagerImpl::SetAppWidgetRequestHandlerCallback(request_cb_t pAppWidgetRequestHandlerFn)
204 {
205         if( __isListenerConstructed == false)
206         {
207                 ConstructListener();
208         }
209         __pAppWidgetRequestHandlerCallback = pAppWidgetRequestHandlerFn;
210 }
211
212 }} // Tizen::Shell