3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 /*@file: IntFaviconManager.cpp
20 *@brief: Provides functionalities to handle/maintain Browser Favicon.
25 #include "IntFaviconManager.h"
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;
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;
39 Bitmap* FaviconManager::__pDefaultBitmap = null;
41 FaviconManager::FaviconManager(void)
46 FaviconManager::~FaviconManager(void)
51 FaviconManager::FaviconManager(const FaviconManager& settingModelObj)
56 FaviconManager& FaviconManager::operator=(const FaviconManager& settingModelObj)
62 FaviconManager::Construct(void)
65 r = PresentationModelBase::Initialize();
66 TryCatch( !IsFailed(r),,"FaviconManager::Initialize failed %s",GetErrorMessage(r));
72 FaviconManager::CreateInstance(void)
74 if (__pFaviconManagerInstance == null)
75 __pFaviconManagerInstance = new(std::nothrow) FaviconManager();
76 result r = __pFaviconManagerInstance->Construct();
79 delete __pFaviconManagerInstance;
80 __pFaviconManagerInstance = null;
83 std::atexit(DestroyInstance);
87 void FaviconManager::DestroyInstance(void)
89 if (__pFaviconManagerInstance)
91 delete __pFaviconManagerInstance;
92 __pFaviconManagerInstance = null;
98 FaviconManager::GetInstance(void)
100 if (__pFaviconManagerInstance == null)
104 return __pFaviconManagerInstance;
110 FaviconManager::GenerateFileName(void)
112 AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
113 String keyCount("FileCount");
114 String fileName = UiApp::GetInstance()->GetAppRootPath() + "data/favicons/";
115 result r = E_SUCCESS;
117 if ( pAppRegistry == NULL )
121 r = pAppRegistry->Get(keyCount, keyValue);
123 if (r == E_KEY_NOT_FOUND)
126 pAppRegistry->Add(keyCount, keyValue);
131 pAppRegistry->Set(keyCount, keyValue);
134 fileName.Append(keyValue);
135 pAppRegistry->Save();
141 FaviconManager::SaveBitmapToFile(Bitmap& bitmap, String& filePath)
148 r = image.EncodeToFile(bitmap, Tizen::Media::IMG_FORMAT_PNG, filePath, true);
150 TryCatch( !IsFailed(r),,"FaviconManager::SaveBitmapToFile %s",GetErrorMessage(r));
156 FaviconManager::SaveFavicon(Tizen::Graphics::Bitmap& bitmap, String& url)
158 AppLogDebug("FaviconManager::SaveFavicon");
160 result r = E_FAILURE;
161 String faviconTable = FAVICON_DATA_TABLE;
165 int resultCount = -1;
171 bool nextRowPresent = false;
173 columnNames.Append(L"URL, ");
174 columnNames.Append(L"FILE_PATH");
176 //strQuery.Append(L"SELECT FILE_PATH FROM strFaviconTable WHERE URL= 'strURL'");
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 ");
185 r = PresentationModelBase::ExecuteQuery(query, resultCount);
186 AppLogDebug("FaviconManager: nResultCount %d",resultCount);
190 filePath = GenerateFileName();
191 if (url.EndsWith(L".gif"))
193 filePath.Append(L".gif");
195 else if (url.EndsWith(L".ico"))
197 filePath.Append(L".ico");
199 else if (url.EndsWith(L".png"))
201 filePath.Append(L".png");
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);
209 query.Append(columnNames);
211 query.Append("VALUES(");
217 query.Append(filePath);
220 r = PresentationModelBase::ExecuteQuery(query, resultCount);
221 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon query failed %s",GetErrorMessage(r));
223 r = PresentationModelBase::CommitDb();
224 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon CommitDb failed %s",GetErrorMessage(r));
226 r = PresentationModelBase::GetLastInsertedId(faviconTable, faviconID);
227 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetLastInsertedId failed %s",GetErrorMessage(r));
229 faviconId.Append(faviconID);
231 if (faviconId.GetLength() < 0)
233 AppLogDebug("FaviconManager::length is less than 0");
239 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
240 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon DbIsNextRowPresent failed %s",GetErrorMessage(r));
242 if (nextRowPresent == true)
244 r = PresentationModelBase::GetColumn(0, faviconID);
245 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetColumn failed %s",GetErrorMessage(r));
247 faviconId.Append(faviconID);
249 if (faviconId.GetLength() < 0)
251 AppLogDebug("FaviconManager::length is less than 0");
253 r = PresentationModelBase::GetColumn(1, filePath);
254 TryCatch( r == E_SUCCESS,,"FaviconManager::SaveFavicon GetColumn failed %s",GetErrorMessage(r));
259 SaveBitmapToFile(bitmap,filePath);
260 AppLogDebug("FaviconManager::SaveFavicon: %ls, %ls , %ls", url.GetPointer(),filePath.GetPointer(), faviconId.GetPointer());
262 CATCH: return faviconId;
266 FaviconManager::GetFaviconN(Tizen::Base::String& favicon_Id)
271 String table = FAVICON_DATA_TABLE;
272 result r = E_FAILURE;
273 bool nextRowPresent = false;
276 Bitmap* pFaviconBitmap = null;
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 ");
283 query.Append(L" WHERE ID= ");
285 query.Append(favicon_Id);
288 r = PresentationModelBase::ExecuteQuery(query, count);
289 TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon ExecuteQuery failed %s",GetErrorMessage(r));
291 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
292 TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon DbIsNextRowPresent failed %s",GetErrorMessage(r));
294 if (nextRowPresent == true)
296 r = PresentationModelBase::GetColumn(0, fileName);
297 TryCatch( r == E_SUCCESS,,"FaviconManager::GetFavicon GetColumn failed %s",GetErrorMessage(r));
301 if (fileName.EndsWith(L".gif"))
303 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_ARGB8888);
305 else if (fileName.EndsWith(L".ico"))
307 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_ARGB8888);
309 else if (fileName.EndsWith(L".png"))
311 pFaviconBitmap = image.DecodeN(fileName, BITMAP_PIXEL_FORMAT_R8G8B8A8);
314 return pFaviconBitmap;
320 FaviconManager::GetDefaultFaviconN()
322 AppResource* pAppResource = Application::GetInstance()->GetAppResource();
324 if ( pAppResource == NULL )
329 __pDefaultBitmap = pAppResource->GetBitmapN(IDB_ICON_DEFAULT_FAVICON);
330 return __pDefaultBitmap;