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