Fixed Nabi Issues N_SE-56966,56735
[apps/osp/Internet.git] / src / IntFaviconManager.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 //!Internet
19 /*@file: IntFaviconManager.cpp
20  *@brief: Provides functionalities to handle/maintain Browser Favicon.
21  */
22
23 #include <cstdlib>
24 #include "IntTypes.h"
25 #include "IntFaviconManager.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Utility;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::Io;
33 using namespace Tizen::Media;
34
35 const wchar_t* FAVICON_DATA_TABLE = L"FaviconData";
36 static const wchar_t* IDB_ICON_DEFAULT_FAVICON = L"I01_icon_default_favicon.png";
37 FaviconManager* FaviconManager::__pFaviconManagerInstance = null;
38
39 Bitmap* FaviconManager::__pDefaultBitmap = null;
40
41 FaviconManager::FaviconManager(void)
42 {
43
44 }
45
46 FaviconManager::~FaviconManager(void)
47 {
48
49 }
50
51 FaviconManager::FaviconManager(const FaviconManager& settingModelObj)
52 {
53
54 }
55
56 FaviconManager& FaviconManager::operator=(const FaviconManager& settingModelObj)
57 {
58         return *this;
59 }
60
61 result
62 FaviconManager::Construct(void)
63 {
64         result r = E_SUCCESS;
65         r = PresentationModelBase::Initialize();
66         TryCatch( !IsFailed(r),,"FaviconManager::Initialize failed %s",GetErrorMessage(r));
67
68         CATCH: return r;
69 }
70
71 void
72 FaviconManager::CreateInstance(void)
73 {
74         if (__pFaviconManagerInstance == null)
75                 __pFaviconManagerInstance = new(std::nothrow) FaviconManager();
76         result r = __pFaviconManagerInstance->Construct();
77         if (IsFailed(r))
78         {
79                 delete __pFaviconManagerInstance;
80                 __pFaviconManagerInstance = null;
81                 return;
82         }
83         std::atexit(DestroyInstance);
84 }
85
86 void FaviconManager::DestroyInstance(void)
87 {
88         if (__pFaviconManagerInstance)
89         {
90                 __pFaviconManagerInstance->UnInitialize();
91                 delete __pFaviconManagerInstance;
92                 __pFaviconManagerInstance = null;
93         }
94 }
95
96
97 FaviconManager*
98 FaviconManager::GetInstance(void)
99 {
100         if (__pFaviconManagerInstance == null)
101         {
102                 CreateInstance();
103         }
104         return __pFaviconManagerInstance;
105
106 }
107
108
109 String
110 FaviconManager::GenerateFileName(void)
111 {
112         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
113         String keyCount("FileCount");
114         String fileName = UiApp::GetInstance()->GetAppRootPath() + "data/favicons/";
115         result r = E_SUCCESS;
116         int keyValue = 1;
117         if ( pAppRegistry == NULL )
118         {
119                 return NULL;
120         }
121         r = pAppRegistry->Get(keyCount, keyValue);
122
123         if (r == E_KEY_NOT_FOUND)
124         {
125                 keyValue = 1;
126                 pAppRegistry->Add(keyCount, keyValue);
127         }
128         else
129         {
130                 keyValue++;
131                 pAppRegistry->Set(keyCount, keyValue);
132         }
133
134         fileName.Append(keyValue);
135         pAppRegistry->Save();
136
137         return fileName;
138 }
139
140 void
141 FaviconManager::SaveBitmapToFile(Bitmap& bitmap, String& filePath)
142 {
143
144         result r;
145         Image image;
146
147         image.Construct();
148         r = image.EncodeToFile(bitmap, Tizen::Media::IMG_FORMAT_PNG, filePath, true);
149
150         TryCatch( !IsFailed(r),,"FaviconManager::SaveBitmapToFile %s",GetErrorMessage(r));
151
152         CATCH: return ;
153 }
154
155 String
156 FaviconManager::SaveFavicon(Tizen::Graphics::Bitmap& bitmap, String& url)
157 {
158         AppLogDebug("FaviconManager::SaveFavicon");
159
160         result r = E_FAILURE;
161         String faviconTable = FAVICON_DATA_TABLE;
162         String query;
163         String columnNames;
164         String attachment;
165         int resultCount = -1;
166         int length = -1;
167         DateTime dt;
168         int faviconID = -1;
169         String faviconId;
170         String filePath ;
171         bool nextRowPresent = false;
172
173         columnNames.Append(L"URL, ");
174         columnNames.Append(L"FILE_PATH");
175
176         //strQuery.Append(L"SELECT FILE_PATH FROM strFaviconTable WHERE URL= 'strURL'");
177         query.Clear();
178         //strQuery.Format(MAX_DB_QUERY_SIZE, L"SELECT COUNT(ID) FROM %ls WHERE NOTEBOOK_ID = %ls", strTable.GetPointer(),notebookId.GetPointer());
179         query.Append(L"SELECT ID, FILE_PATH FROM ");
180         query.Append(faviconTable);
181         query.Append(" WHERE URL LIKE ");
182         query.Append("'");
183         query.Append(url);
184         query.Append("'");
185         r = PresentationModelBase::ExecuteQuery(query, resultCount);
186         AppLogDebug("FaviconManager: nResultCount %d",resultCount);
187
188         if (resultCount < 1)
189         {
190                 filePath = GenerateFileName();
191                 if (url.EndsWith(L".gif"))
192                 {
193                         filePath.Append(L".gif");
194                 }
195                 else if (url.EndsWith(L".ico"))
196                 {
197                         filePath.Append(L".ico");
198                 }
199                 else if (url.EndsWith(L".png"))
200                 {
201                         filePath.Append(L".png");
202                 }
203                 query.Clear();
204                 //@Append() is replaced with Format() for improving performance
205                 //strQuery.Format(MAX_DB_QUERY_SIZE, L"INSERT INTO %ls(%ls) VALUES('%ls', '%ls', '%ls', %ls, '%ls', '%ls', %d, %d, '%ls')", strNoteTable.GetPointer(),strColumnNames.GetPointer(), note.GetNoteTitle().GetPointer(), notePlaintext.GetPointer(), noteEncodedHtml.GetPointer(), note.GetNotebookId().GetPointer(), dt.ToString().GetPointer(), dt.ToString().GetPointer(), note.IsFavorite(), note.IsLocked(), strAttachment.GetPointer());
206                 query.Append(L"INSERT INTO ");
207                 query.Append(faviconTable);
208                 query.Append("(");
209                 query.Append(columnNames);
210                 query.Append(") ");
211                 query.Append("VALUES(");
212                 query.Append("'");
213                 query.Append(url);
214                 query.Append("'");
215                 query.Append(", ");
216                 query.Append("'");
217                 query.Append(filePath);
218                 query.Append("')");
219
220                 r = PresentationModelBase::ExecuteQuery(query, resultCount);
221                 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon query failed  %s",GetErrorMessage(r));
222
223                 r = PresentationModelBase::CommitDb();
224                 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon CommitDb failed  %s",GetErrorMessage(r));
225
226                 r = PresentationModelBase::GetLastInsertedId(faviconTable, faviconID);
227                 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetLastInsertedId failed  %s",GetErrorMessage(r));
228
229                 faviconId.Append(faviconID);
230
231                 if (faviconId.GetLength() < 0)
232                 {
233                         AppLogDebug("FaviconManager::length is less than 0");
234                 }
235
236         }
237         else
238         {
239                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
240                 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon DbIsNextRowPresent failed  %s",GetErrorMessage(r));
241
242                 if (nextRowPresent == true)
243                 {
244                         r = PresentationModelBase::GetColumn(0, faviconID);
245                         TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetColumn failed  %s",GetErrorMessage(r));
246
247                         faviconId.Append(faviconID);
248
249                         if (faviconId.GetLength() < 0)
250                         {
251                                 AppLogDebug("FaviconManager::length is less than 0");
252                         }
253                         r = PresentationModelBase::GetColumn(1, filePath);
254                         TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetColumn failed  %s",GetErrorMessage(r));
255                 }
256
257         }
258
259         SaveBitmapToFile(bitmap,filePath);
260         AppLogDebug("FaviconManager::SaveFavicon: %ls, %ls , %ls", url.GetPointer(),filePath.GetPointer(), faviconId.GetPointer());
261
262         CATCH: return faviconId;
263     }
264
265 Bitmap*
266 FaviconManager::GetFaviconN(Tizen::Base::String& favicon_Id)
267 {
268         int count = -1;
269         int length = -1;
270         String query;
271         String table = FAVICON_DATA_TABLE;
272         result r = E_FAILURE;
273         bool nextRowPresent = false;
274         String fileName ;
275         Image image;
276         Bitmap* pFaviconBitmap = null;
277
278         image.Construct();
279
280         //query.Format(MAX_DB_QUERY_SIZE, L"SELECT * FROM %ls WHERE NOTEBOOK_ID = %ls", strTable.GetPointer(),notebookId.GetPointer());
281         query.Append(L"SELECT FILE_PATH FROM ");
282         query.Append(table);
283         query.Append(L" WHERE ID= ");
284         query.Append("'");
285         query.Append(favicon_Id);
286         query.Append("'");
287
288         r = PresentationModelBase::ExecuteQuery(query, count);
289         TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon ExecuteQuery failed  %s",GetErrorMessage(r));
290
291         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
292         TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon DbIsNextRowPresent failed  %s",GetErrorMessage(r));
293
294         if (nextRowPresent == true)
295         {
296                 r = PresentationModelBase::GetColumn(0, fileName);
297                 TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon GetColumn failed  %s",GetErrorMessage(r));
298
299         }
300
301         if (fileName.EndsWith(L".gif"))
302         {
303                 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_ARGB8888);
304         }
305         else if (fileName.EndsWith(L".ico"))
306         {
307                 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_ARGB8888);
308         }
309         else if (fileName.EndsWith(L".png"))
310         {
311                 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_R8G8B8A8);
312         }
313
314         return pFaviconBitmap;
315
316         CATCH: return null;
317 }
318
319 Bitmap*
320 FaviconManager::GetDefaultFaviconN()
321 {
322         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
323
324         if ( pAppResource == NULL )
325         {
326                 return null;
327         }
328
329         __pDefaultBitmap = pAppResource->GetBitmapN(IDB_ICON_DEFAULT_FAVICON);
330         return __pDefaultBitmap;
331 }
332