merge with master
[apps/osp/Internet.git] / src / IntHistoryPresentationModel.cpp
1 //\r
2 \r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\r
5 // Licensed under the Flora License, Version 1.0 (the License);\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://floralicense.org/license/\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an AS IS BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 //\r
17 \r
18 \r
19 //!Internet\r
20 /*@file: IntHistoryPresentationModel.cpp\r
21  *@brief: Provides functionalities to handle/maintain Browser History.\r
22  */\r
23 \r
24 #include <cstdlib>\r
25 #include "IntHistoryData.h"\r
26 #include "IntHistoryPresentationModel.h"\r
27 #include "IntTypes.h"\r
28 \r
29 using namespace Tizen::App;\r
30 using namespace Tizen::Base;\r
31 using namespace Tizen::Base::Collection;\r
32 using namespace Tizen::Base::Utility;\r
33 using namespace Tizen::Graphics;\r
34 using namespace Tizen::Io;\r
35 using namespace Tizen::Locales;\r
36 using namespace Tizen::Media;\r
37 using namespace Tizen::System;\r
38 \r
39 extern const wchar_t* HISTORY_DATA_TABLE;\r
40 extern const wchar_t* BOOKMARK_URL_TABLE;\r
41 \r
42 HistoryPresentationModel* HistoryPresentationModel::__pHistoryPresentationModel = null;\r
43 \r
44 void HistoryPresentationModel::CreateInstance(void)\r
45 {\r
46         if (__pHistoryPresentationModel == null)\r
47                 __pHistoryPresentationModel = new(std::nothrow) HistoryPresentationModel();\r
48         result r = __pHistoryPresentationModel->Construct();\r
49         if (IsFailed(r))\r
50         {\r
51                 delete __pHistoryPresentationModel;\r
52                 __pHistoryPresentationModel = null;\r
53                 return;\r
54         }\r
55         std::atexit(DestroyInstance);\r
56 }\r
57 \r
58 \r
59 HistoryPresentationModel* HistoryPresentationModel::GetInstance(void)\r
60 {\r
61         if (__pHistoryPresentationModel == null)\r
62         {\r
63                 CreateInstance();\r
64         }\r
65         return __pHistoryPresentationModel;\r
66 \r
67 }\r
68 \r
69 void HistoryPresentationModel::DestroyInstance(void)\r
70 {\r
71         if (__pHistoryPresentationModel)\r
72         {\r
73                 delete __pHistoryPresentationModel;\r
74                 __pHistoryPresentationModel = null;\r
75         }\r
76 }\r
77 \r
78 HistoryPresentationModel::HistoryPresentationModel(void)\r
79 {\r
80 \r
81 }\r
82 \r
83 HistoryPresentationModel::~HistoryPresentationModel(void)\r
84 {\r
85 \r
86 }\r
87 \r
88 HistoryPresentationModel::HistoryPresentationModel(const HistoryPresentationModel& historyModelObj)\r
89 {\r
90 \r
91 }\r
92 \r
93 HistoryPresentationModel& HistoryPresentationModel::operator=(const HistoryPresentationModel& historyModelObj)\r
94 {\r
95         return *this;\r
96 }\r
97 \r
98 result\r
99 HistoryPresentationModel::Construct(void)\r
100 {\r
101         result r = E_SUCCESS;\r
102         DB_FILE_PATH = L"/opt/usr/dbspace/.browser-history.db";\r
103         r = PresentationModelBase::Initialize();\r
104         TryCatch( !IsFailed(r),,"Failed to initialize HistoryPresentationModel Model %s",GetErrorMessage(r));\r
105 \r
106         CATCH: return r;\r
107 \r
108 }\r
109 \r
110 result\r
111 HistoryPresentationModel::SaveHistory(History& history)\r
112 {\r
113         AppLog("HistoryPresentationModel::saveHistory");\r
114         result r = E_FAILURE;\r
115         String historyTable(HISTORY_DATA_TABLE);\r
116         String query;\r
117         String columnNames;\r
118         String attachment;\r
119         int resultCount = -1;\r
120         int rowId = -1;\r
121         DateTime date;\r
122         String historyId;\r
123         int urlCount = 0;\r
124         ArrayList* pMostVisitedSites = null;\r
125         bool isAlreadyExist = false;\r
126         bool isEligibleForMostVisitedSites = true;\r
127         int i;\r
128         String thumbnailPath;\r
129         int todaysCount = 0;\r
130         DateTime todayStart;\r
131         DateTime todayEnd;\r
132         SystemTime::GetCurrentTime(todayStart);\r
133         SystemTime::GetCurrentTime(todayEnd);\r
134         todayStart.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 0,0,0);\r
135         todayEnd.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 23,59,59);\r
136 \r
137         ArrayList* pTodaysList = new(std::nothrow) ArrayList();\r
138         pTodaysList->Construct();\r
139         GetHistoryCountWithTimeRange(todayStart, todayEnd, todaysCount);\r
140         GetHistoryWithTimeRange(todayStart, todayEnd, 0, todaysCount, *pTodaysList);\r
141 \r
142         for(int count = 0; count < todaysCount; count++)\r
143         {\r
144                 History *pHistoryItem  = dynamic_cast<History*>(pTodaysList->GetAt(count));\r
145                 //AppLog("HistoryPresentationModel::saveHistory pHistoryItem id is %ls",pHistoryItem->GetHistoryId().GetPointer());\r
146                 AppLog("SaveHistory pHistoryItem url is %ls",pHistoryItem->GetHistoryUrl().GetPointer());\r
147                 AppLog("SaveHistory history url is %ls",history.GetHistoryUrl().GetPointer());\r
148 \r
149                 if(pHistoryItem != null && pHistoryItem->GetHistoryUrl().CompareTo(history.GetHistoryUrl()) == 0)\r
150                 {\r
151                         history.SetHistoryId(pHistoryItem->GetHistoryId());\r
152                         AppLog("HistoryPresentationModel::saveHistory pHistoryItem history is %ls",pHistoryItem->GetHistoryId().GetPointer());\r
153                         break;\r
154                 }\r
155         }\r
156 \r
157         pMostVisitedSites = new(std::nothrow) ArrayList();\r
158         pMostVisitedSites->Construct();\r
159         String historyTitle = history.GetHistoryTitle();\r
160         historyTitle.Replace(L"'", L"''");\r
161 \r
162         String historyUrl = history.GetHistoryUrl();\r
163         historyUrl.Replace(L"'", L"''");\r
164 \r
165         PresentationModelBase::GetCurrentDateTime(date);\r
166         GetMostVisitedSites(*pMostVisitedSites);\r
167         GetUrlCount(historyUrl, urlCount);\r
168         for (i = 0; i < pMostVisitedSites->GetCount(); i++)\r
169         {\r
170                 History* pHistory = static_cast< History* >(pMostVisitedSites->GetAt(i));\r
171                 if (pHistory != null && pHistory->GetHistoryUrl().CompareTo(historyUrl) == 0)\r
172                 {\r
173                         AppLog("HistoryPresentationModel::saveHistory isalreadyexist true");\r
174                         isAlreadyExist = true;\r
175                         break;\r
176                 }\r
177                 if ( pHistory != null &&  urlCount >= pHistory->GetVisitedCount())\r
178                 {\r
179                         isEligibleForMostVisitedSites = true;\r
180                 }\r
181                 else\r
182                 {\r
183                         isEligibleForMostVisitedSites = false;\r
184                 }\r
185         }\r
186 \r
187         if (pMostVisitedSites->GetCount() < 9 || (isAlreadyExist == false && isEligibleForMostVisitedSites == true && history.GetThumbnail() != null))\r
188         {\r
189                 AppLog("HistoryPresentationModel::SaveHistory coming here");\r
190 \r
191                 thumbnailPath = GenerateFileName();\r
192                 AppLog("generating file path and url %ls, %ls",thumbnailPath.GetPointer(),historyUrl.GetPointer());\r
193                 Image* pImage = new(std::nothrow) Image();\r
194                 pImage->Construct();\r
195                 result r = pImage->EncodeToFile(*history.GetThumbnail(), IMG_FORMAT_JPG, thumbnailPath, true);\r
196                 delete pImage;\r
197                 if(IsFailed(r))\r
198                 {\r
199                         AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));\r
200                         return r;\r
201                 }\r
202         }\r
203         delete pMostVisitedSites;\r
204         pMostVisitedSites = NULL ;\r
205 \r
206         if (history.GetHistoryId() == "")\r
207         {\r
208                 AppLog("HistoryPresentationModel::saveHistory GetHistoryId is blank");\r
209                 /*\r
210                 columnNames.Append(L"TITLE, ");\r
211                 columnNames.Append(L"URL, ");\r
212                 columnNames.Append(L"VISITED_TIME, ");\r
213                 columnNames.Append(L"ICON_PATH, ");\r
214                 columnNames.Append(L"FAVICON_ID, ");\r
215                 columnNames.Append(L"THUMBNAIL_PATH");\r
216 \r
217                 query.Append(L"INSERT INTO ");\r
218                 query.Append(historyTable);\r
219                 query.Append("(");\r
220                 query.Append(columnNames);\r
221                 query.Append(") ");\r
222                 query.Append("VALUES(");\r
223                 query.Append("'");\r
224                 query.Append(historyTitle);\r
225                 query.Append("'");\r
226                 query.Append(", ");\r
227                 query.Append("'");\r
228                 query.Append(historyUrl);\r
229                 query.Append("', ");\r
230                 query.Append("DateTime(");\r
231                 query.Append("'");\r
232                 query.Append(GetStringFromDate(date));\r
233                 query.Append("'), ");\r
234                 query.Append("'");\r
235                 query.Append(history.GetHistoryIconPath());\r
236                 query.Append("'");\r
237                 query.Append(", ");\r
238                 query.Append("'");\r
239                 query.Append(history.GetFaviconId());\r
240                 query.Append("'");\r
241                 query.Append(", ");\r
242                 query.Append("'");\r
243                 query.Append(thumbnailPath);\r
244                 query.Append("'");\r
245                 query.Append(")");\r
246          */\r
247                 columnNames.Append(L"ADDRESS, ");\r
248                 columnNames.Append(L"TITLE, ");\r
249                 //columnNames.Append(L"COUNTER, ");\r
250                 columnNames.Append(L"VISITDATE");\r
251         //      columnNames.Append(L"FAVICON, ");\r
252         //      columnNames.Append(L"FAVICON_LENGTH, ");\r
253         //      columnNames.Append(L"FAVICON_W, ");\r
254         //      columnNames.Append(L"FAVICON_H, ");\r
255         //      columnNames.Append(L"SNAPSHOT, ");\r
256         //      columnNames.Append(L"SNAPSHOT_STRIDE, ");\r
257         //      columnNames.Append(L"SNAPSHOT_W, ");\r
258         //      columnNames.Append(L"SNAPSHOT_H");\r
259         //      columnNames.Append(L"ICON_PATH, ");\r
260         //      columnNames.Append(L"FAVICON_ID, ");\r
261         //      columnNames.Append(L"THUMBNAIL_PATH");\r
262 \r
263                 query.Append(L"INSERT INTO ");\r
264                 query.Append(historyTable);\r
265                 query.Append("(");\r
266                 query.Append(columnNames);\r
267                 query.Append(") ");\r
268                 query.Append("VALUES(");\r
269                 query.Append("'");\r
270                 query.Append(historyUrl);\r
271                 query.Append("'");\r
272                 query.Append(", ");\r
273                 query.Append("'");\r
274                 query.Append(historyTitle);\r
275                 query.Append("', ");\r
276                 query.Append("DateTime(");\r
277                 query.Append("'");\r
278                 query.Append(GetStringFromDate(date));\r
279                 query.Append("'))");\r
280 \r
281                 //query.Append("'), ");\r
282                 //query.Append("'");\r
283                 //query.Append(history.GetHistoryIconPath());\r
284                 //query.Append("'");\r
285                 //query.Append(", ");\r
286                 //query.Append("'");\r
287                 //query.Append(history.GetFaviconId());\r
288                 //query.Append("'");\r
289                 //query.Append(", ");\r
290                 //query.Append("'");\r
291                 //query.Append(thumbnailPath);\r
292                 //query.Append("'");\r
293                 //query.Append(")");\r
294         }\r
295         else\r
296         {\r
297                 /*query.Append(L"UPDATE ");\r
298                 query.Append(historyTable);\r
299                 query.Append(" SET TITLE = ");\r
300                 query.Append("'");\r
301                 query.Append(historyTitle);\r
302                 query.Append("'");\r
303                 query.Append(", URL = ");\r
304                 query.Append("'");\r
305                 query.Append(historyUrl);\r
306                 query.Append("'");\r
307                 query.Append(", VISITED_TIME = ");\r
308                 query.Append("DateTime(");\r
309                 query.Append("'");\r
310                 query.Append(GetStringFromDate(date));\r
311                 query.Append("')");\r
312                 query.Append(", ICON_PATH = ");\r
313                 query.Append("'");\r
314                 query.Append(history.GetHistoryIconPath());\r
315                 query.Append("'");\r
316                 query.Append(", ");\r
317                 query.Append("'");\r
318                 query.Append(history.GetFaviconId());\r
319                 query.Append("'");\r
320                 query.Append(", ");\r
321                 query.Append("'");\r
322                 query.Append(thumbnailPath);\r
323                 query.Append("'");\r
324                 query.Append(" WHERE ID = ");\r
325                 query.Append(history.GetHistoryId());*/\r
326                 query.Append(L"UPDATE ");\r
327                 query.Append(historyTable);\r
328                 query.Append(" SET ADDRESS = ");\r
329                 query.Append("'");\r
330                 query.Append(historyUrl);\r
331                 query.Append("'");\r
332                 query.Append(", TITLE = ");\r
333                 query.Append("'");\r
334                 query.Append(historyTitle);\r
335                 query.Append("'");\r
336                 query.Append(", VISITED_TIME = ");\r
337                 query.Append("DateTime(");\r
338                 query.Append("'");\r
339                 query.Append(GetStringFromDate(date));\r
340                 query.Append("')");\r
341                 /*query.Append(", ICON_PATH = ");\r
342                 query.Append("'");\r
343                 query.Append(history.GetHistoryIconPath());\r
344                 query.Append("'");\r
345                 query.Append(", ");\r
346                 query.Append("'");\r
347                 query.Append(history.GetFaviconId());\r
348                 query.Append("'");\r
349                 query.Append(", ");\r
350                 query.Append("'");\r
351                 query.Append(thumbnailPath);*/\r
352                 query.Append("'");\r
353                 query.Append(" WHERE ID = ");\r
354                 query.Append(history.GetHistoryId());\r
355 \r
356         }\r
357 \r
358         AppLog("HistoryPresentationModel::SaveHistory query is %S",query.GetPointer());\r
359 \r
360         r = PresentationModelBase::ExecuteQuery(query, resultCount);\r
361         if (r == E_SUCCESS)\r
362         {\r
363                 AppLog("HistoryPresentationModel::SaveHistory result is success");\r
364         }\r
365         else\r
366         {\r
367                 AppLog("HistoryPresentationModel::SaveHistory result is failure");\r
368         }\r
369         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveHistory query failed  %s",GetErrorMessage(r));\r
370 \r
371         r = PresentationModelBase::CommitDb();\r
372 \r
373         r = GetLastInsertedId(historyTable, rowId);\r
374         if (IsFailed(r))\r
375         {\r
376                 AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));\r
377                 return r;\r
378         }\r
379         if (rowId < 0)\r
380                 return E_FAILURE;\r
381 \r
382         historyId.Append(rowId);\r
383         if (historyId.GetLength() < 0)\r
384         {\r
385                 return E_FAILURE;\r
386         }\r
387         //Set the generated ID to History\r
388         history.SetHistoryId(historyId);\r
389 \r
390         CATCH: return r;\r
391 }\r
392 \r
393 result\r
394 HistoryPresentationModel::DeleteMultipleHistory(ArrayList* pHistoryIdList)\r
395 {\r
396         result r = E_FAILURE;\r
397         String query;\r
398         String historyTable = HISTORY_DATA_TABLE;\r
399         int resultCount = -1;\r
400 \r
401         if (pHistoryIdList == null)\r
402         {\r
403                 return E_FAILURE;\r
404         }\r
405 \r
406         query.Append(L"DELETE FROM ");\r
407         query.Append(historyTable);\r
408         query.Append(" WHERE");\r
409         for(int index = 0; index <= pHistoryIdList->GetCount()-2; index++)\r
410         {\r
411                 query.Append(" ID = ");\r
412                 String* hisId = static_cast<String*>(pHistoryIdList->GetAt(index));\r
413                 query.Append(*hisId);\r
414                 query.Append(" OR ");\r
415         }\r
416         query.Append(" ID = ");\r
417         String* hisId = static_cast<String*>(pHistoryIdList->GetAt(pHistoryIdList->GetCount()-1));\r
418         if(hisId == null)\r
419         {\r
420                 return E_FAILURE;\r
421         }\r
422         query.Append(*hisId);\r
423 \r
424         r = PresentationModelBase::ExecuteQuery(query, resultCount);\r
425         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));\r
426 \r
427         r = PresentationModelBase::CommitDb();\r
428 \r
429         CATCH: return r;\r
430 }\r
431 \r
432 result\r
433 HistoryPresentationModel::DeleteHistory(const int historyId)\r
434 {\r
435         result r = E_FAILURE;\r
436         String query;\r
437         String historyTable = HISTORY_DATA_TABLE;\r
438         int resultCount = -1;\r
439 \r
440         if (historyId < 1)\r
441         {\r
442                 return E_INVALID_ARG;\r
443         }\r
444 \r
445         query.Append(L"DELETE FROM ");\r
446         query.Append(historyTable);\r
447         query.Append(" WHERE ID = ");\r
448         query.Append(historyId);\r
449 \r
450         r = PresentationModelBase::ExecuteQuery(query, resultCount);\r
451         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));\r
452 \r
453         r = PresentationModelBase::CommitDb();\r
454 \r
455         CATCH: return r;\r
456 }\r
457 \r
458 result\r
459 HistoryPresentationModel::DeleteHistory(String& historyUrl)\r
460 {\r
461         result r = E_FAILURE;\r
462         String query;\r
463         String historyTable = HISTORY_DATA_TABLE;\r
464         int resultCount = -1;\r
465 \r
466         query.Append(L"DELETE FROM ");\r
467         query.Append(historyTable);\r
468         query.Append(" WHERE URL = '");\r
469         query.Append(historyUrl);\r
470         query.Append("'");\r
471 \r
472         r = PresentationModelBase::ExecuteQuery(query, resultCount);\r
473         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));\r
474 \r
475         r = PresentationModelBase::CommitDb();\r
476 \r
477         CATCH: return r;\r
478 }\r
479 \r
480 result\r
481 HistoryPresentationModel::ClearHistory(void)\r
482 {\r
483         result r = E_FAILURE;\r
484         String query;\r
485         String historyTable = HISTORY_DATA_TABLE;\r
486         int resultCount = -1;\r
487 \r
488         query.Append(L"DELETE FROM ");\r
489         query.Append(historyTable);\r
490 \r
491         r = PresentationModelBase::ExecuteQuery(query, resultCount);\r
492         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::ClearHistory GetLastInsertedId failed %s",GetErrorMessage(r));\r
493 \r
494         r = PresentationModelBase::CommitDb();\r
495 \r
496     CATCH: return r;\r
497 }\r
498 \r
499 result\r
500 HistoryPresentationModel::GetHistoryCount(int& historyCount)\r
501 {\r
502         int count = -1;\r
503         int intVal = -1;\r
504         String query;\r
505         String historyTable(HISTORY_DATA_TABLE);\r
506         result r = E_FAILURE;\r
507         bool nextRowPresent = false;\r
508 \r
509         query.Append(L"SELECT COUNT(ID) FROM ");\r
510         query.Append(historyTable);\r
511 \r
512         r = PresentationModelBase::ExecuteQuery(query, count);\r
513         AppLog("the count is %d", count);\r
514         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));\r
515 \r
516         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);\r
517         if (IsFailed(r) != null)\r
518                 return r;\r
519         if (nextRowPresent == true)\r
520         {\r
521                 r = PresentationModelBase::GetColumn(0, intVal);\r
522                 if (IsFailed(r) != null)\r
523                 {\r
524                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));\r
525                         return r;\r
526                 }\r
527         }\r
528         historyCount = intVal;\r
529 \r
530     CATCH: return r;\r
531 }\r
532 \r
533 result\r
534 HistoryPresentationModel::GetHistoryCountWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime, int& historyCount)\r
535 {\r
536 \r
537         int count = -1;\r
538         int intVal = -1;\r
539         String query;\r
540         String historyTable(HISTORY_DATA_TABLE);\r
541         result r = E_FAILURE;\r
542         bool nextRowPresent = false;\r
543 \r
544         query.Append(L"SELECT COUNT(ID) FROM ");\r
545         query.Append(historyTable);\r
546         query.Append(" WHERE VISITDATE >= ");\r
547         query.Append("DateTime(");\r
548         query.Append("'");\r
549         query.Append(GetStringFromDate(startTime));\r
550         query.Append("')");\r
551         query.Append(" AND VISITDATE <= ");\r
552         query.Append("DateTime(");\r
553         query.Append("'");\r
554         query.Append(GetStringFromDate(endTime));\r
555         query.Append("')");\r
556 \r
557         r = PresentationModelBase::ExecuteQuery(query, count);\r
558         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCountWithTimeRange query failed %s",GetErrorMessage(r));\r
559 \r
560         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);\r
561         if (IsFailed(r))\r
562                 return r;\r
563         if (nextRowPresent == true)\r
564         {\r
565                 r = PresentationModelBase::GetColumn(0, intVal);\r
566                 if (IsFailed(r))\r
567                 {\r
568                         AppLogDebug("Error:PresentationModelBase::DbGetColumn:%s", GetErrorMessage(r));\r
569                         return r;\r
570                 }\r
571         }\r
572         historyCount = intVal;\r
573 \r
574         CATCH: return r;\r
575 }\r
576 \r
577 result\r
578 HistoryPresentationModel::GetHistory(const int startIndex,const int limit, ArrayList& pHistoryList)\r
579 {\r
580 \r
581         int count = -1;\r
582         String query;\r
583         String historyTable(HISTORY_DATA_TABLE);\r
584         result r = E_FAILURE;\r
585 \r
586         query.Append(L"SELECT");\r
587         query.Append(historyTable);\r
588         query.Append(L".*, ");\r
589         query.Append(BOOKMARK_URL_TABLE);\r
590         query.Append(L".ID ");\r
591         query.Append(L"FROM ");\r
592         query.Append(historyTable);\r
593         query.Append(L", ");\r
594         query.Append(BOOKMARK_URL_TABLE);\r
595         query.Append(L" WHERE ");\r
596         query.Append(historyTable);\r
597         query.Append(L".ADDRESS ");\r
598         query.Append(L"IN");\r
599         query.Append(L"(");\r
600         query.Append(L"SELECT");\r
601         query.Append(historyTable);\r
602         query.Append(L".ADDRESS ");\r
603         query.Append(L"FROM ");\r
604         query.Append(BOOKMARK_URL_TABLE);\r
605         query.Append(L" LEFT JOIN ");\r
606         query.Append(historyTable);\r
607         query.Append(L"ON");\r
608         query.Append(BOOKMARK_URL_TABLE);\r
609         query.Append(L".ADDRESS = ");\r
610         query.Append(historyTable);\r
611         query.Append(L".ADDRESS ");\r
612         query.Append(" ORDER BY");\r
613         query.Append(historyTable);\r
614         query.Append(L".VISITDATE DESC");\r
615         query.Append(")");\r
616         query.Append(" GROUP BY ");\r
617         query.Append(historyTable);\r
618         query.Append(L".ADDRESS ");\r
619         query.Append(" ORDER BY");\r
620         query.Append(L"COUNT(");\r
621         query.Append(historyTable);\r
622         query.Append(L".ADDRESS) DESC ");\r
623 \r
624 \r
625         //Append LIMIT\r
626         if (limit > 0)\r
627         {\r
628                 query.Append(" LIMIT ");\r
629                 query.Append(limit);\r
630 \r
631                 //Append OFFESET\r
632                 if (startIndex >= 0)\r
633                 {\r
634                         query.Append(" OFFSET ");\r
635                         query.Append(startIndex);\r
636                 }\r
637         }\r
638 \r
639         r = PresentationModelBase::ExecuteQuery(query, count);\r
640         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistory query failed %s",GetErrorMessage(r));\r
641 \r
642         r = CreateHistoryList(count, pHistoryList);\r
643         if (IsFailed(r))\r
644         {\r
645                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));\r
646         }\r
647         CATCH: return r;\r
648 }\r
649 \r
650 result\r
651 HistoryPresentationModel::CreateHistoryList(int historyCount, ArrayList& historyList, int maxCount)\r
652 {\r
653 \r
654         History* pHistory = null;\r
655         int intVal = -1;\r
656         String stringVal;\r
657         DateTime dateVal;\r
658         result r = E_FAILURE;\r
659         String historyId;\r
660         String faviconId;\r
661         String bookmarkId;\r
662         int visitedCount;\r
663         DateTime visitedTime;\r
664         String iconPath;\r
665         String thumbnailPath;\r
666         bool nextRowPresent = false;\r
667         Image image;\r
668         Bitmap* pThumnailBitmap = null;\r
669 \r
670         image.Construct();\r
671         if (historyCount < 1)\r
672         {\r
673                 return E_SUCCESS;\r
674         }\r
675 \r
676         for (int Count = 0; (maxCount == -1 && Count < historyCount ) || (maxCount != -1 &&  Count < maxCount && Count < historyCount); Count++)\r
677         {\r
678                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);\r
679                 if (IsFailed(r))\r
680                 {\r
681                         return E_SUCCESS;\r
682                 }\r
683                 if (nextRowPresent == true)\r
684                 {\r
685                         pHistory = new(std::nothrow) History;\r
686                         if (pHistory)\r
687                         {\r
688                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)\r
689                                 {\r
690 \r
691                                         switch (columnCount)\r
692                                         {\r
693                                         case HISTORY_ID:\r
694                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);\r
695                                                 if (!IsFailed(r))\r
696                                                 {\r
697                                                         if (intVal > 0)\r
698                                                         {\r
699                                                                 historyId.Clear();\r
700                                                                 historyId.Append(intVal);\r
701                                                                 if (historyId.GetLength() > 0)\r
702                                                                 {\r
703                                                                         pHistory->SetHistoryId(historyId);\r
704                                                                 }\r
705                                                         }\r
706 \r
707                                                 }\r
708                                                 break;\r
709                                         case HISTORY_URL:\r
710                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);\r
711                                                 if (!IsFailed(r))\r
712                                                 {\r
713                                                         pHistory->SetHistoryUrl(stringVal);\r
714                                                 }\r
715                                                 break;\r
716                                         case HISTORY_TITLE:\r
717                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);\r
718                                                 if (!IsFailed(r))\r
719                                                 {\r
720                                                         pHistory->SetHistoryTitle(stringVal);\r
721                                                 }\r
722                                                 break;\r
723                                         case HISTORY_VISITED_TIME:\r
724                                                 r = PresentationModelBase::GetColumn(columnCount, dateVal);\r
725                                                 if (!IsFailed(r))\r
726                                                 {\r
727                                                         visitedTime = dateVal;\r
728                                                         pHistory->SetVisitedTime(visitedTime);\r
729                                                 }\r
730                                                 break;\r
731 \r
732                                                 /*case HISTORY_ICON_PATH:\r
733                                                         r = PresentationModelBase::GetColumn(columnCount, stringVal);\r
734                                                         if (!IsFailed(r))\r
735                                                         {\r
736                                                                 iconPath = stringVal;\r
737                                                                 pHistory->SetIconPath(iconPath);\r
738                                                         }\r
739                                                         break;\r
740 \r
741                                                 case HISTORY_FAVICONID:\r
742 \r
743                                                         r = PresentationModelBase::GetColumn(columnCount, intVal);\r
744                                                         if (!IsFailed(r))\r
745                                                         {\r
746                                                                 if (intVal >= 0)\r
747                                                                 {\r
748                                                                         faviconId.Clear();\r
749                                                                         faviconId.Append(intVal);\r
750                                                                         if (faviconId.GetLength() > 0)\r
751                                                                         {\r
752                                                                                 pHistory->SetFaviconId(faviconId);\r
753                                                                         }\r
754                                                                 }\r
755                                                         }\r
756                                                         break;\r
757                                                 case HISTORY_THUMBNAIL:\r
758                                                         r = PresentationModelBase::GetColumn(columnCount, stringVal);\r
759                                                         if (!IsFailed(r))\r
760                                                         {\r
761 \r
762                                                                 thumbnailPath = stringVal;\r
763                                                                 pThumnailBitmap = image.DecodeN(thumbnailPath, BITMAP_PIXEL_FORMAT_RGB565);\r
764                                                                 if (pThumnailBitmap != null)\r
765                                                                 {\r
766                                                                         pHistory->SetThumbnail(pThumnailBitmap);\r
767                                                                         pHistory->SetThumbnailPath(thumbnailPath);\r
768                                                                 }\r
769                                                         }\r
770                                                         break;*/\r
771                                 /*      case HISTORY_BOOKMARK_ID:\r
772                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);\r
773                                                 if (!IsFailed(r))\r
774                                                 {\r
775                                                         if (intVal > 0)\r
776                                                         {\r
777                                                                 bookmarkId.Clear();\r
778                                                                 bookmarkId.Append(intVal);\r
779                                                                 if (bookmarkId.GetLength() > 0)\r
780                                                                 {\r
781                                                                         pHistory->SetBookmarkId(bookmarkId);\r
782                                                                 }\r
783                                                         }\r
784 \r
785                                                 }\r
786                                                 break;\r
787                                                 */\r
788                                         case HISTORY_VISITED_COUNT:\r
789                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);\r
790                                                 if (!IsFailed(r))\r
791                                                 {\r
792                                                         if (intVal > 0)\r
793                                                         {\r
794                                                                 pHistory->SetVisitedCount(intVal);\r
795 \r
796                                                         }\r
797 \r
798                                                 }\r
799                                                 break;\r
800 \r
801                                         default:\r
802                                                 break;\r
803                                         }\r
804 \r
805                                 }\r
806                                 r = historyList.Add(*pHistory);\r
807 \r
808                                 if (IsFailed(r))\r
809                                 {\r
810                                         delete pHistory;\r
811                                         return r;\r
812                                 }\r
813                         }\r
814                 }\r
815         }\r
816         return E_SUCCESS;\r
817 }\r
818 \r
819 result\r
820 HistoryPresentationModel::GetSearchHistory(const int startIndex,const int limit, ArrayList& pHistoryList, String& text)\r
821 {\r
822         text.Replace(L"'", L"''");\r
823 \r
824         int count = -1;\r
825         String query;\r
826         String historyTable(HISTORY_DATA_TABLE);\r
827         result r = E_FAILURE;\r
828 \r
829         query.Append(L"SELECT ");\r
830         query.Append(historyTable);\r
831         query.Append(L".* ");\r
832         query.Append(L"FROM ");\r
833         query.Append(historyTable);\r
834         query.Append(L" WHERE (");\r
835         query.Append(historyTable);\r
836         query.Append(L".TITLE LIKE '%");\r
837         query.Append(text);\r
838         query.Append("%')");\r
839         query.Append(" OR ");\r
840         query.Append(L"(");\r
841         query.Append(historyTable);\r
842         query.Append(L".ADDRESS LIKE '%");\r
843         query.Append(text);\r
844         query.Append("%')");\r
845         query.Append(" ORDER BY VISITDATE DESC");\r
846 \r
847         //Append LIMIT\r
848         if (limit > 0)\r
849         {\r
850                 query.Append(" LIMIT ");\r
851                 query.Append(limit);\r
852 \r
853                 //Append OFFESET\r
854                 if (startIndex >= 0)\r
855                 {\r
856                         query.Append(" OFFSET ");\r
857                         query.Append(startIndex);\r
858                 }\r
859         }\r
860 \r
861         r = HistoryPresentationModel::ExecuteQuery(query, count);\r
862         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));\r
863 \r
864         r = CreateHistoryList(count, pHistoryList);\r
865         if (IsFailed(r))\r
866         {\r
867                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));\r
868         }\r
869         CATCH: return r;\r
870 }\r
871 \r
872 result\r
873 HistoryPresentationModel::GetSearchHistoryCount(int& historyCount, String& text)\r
874 {\r
875         int count = -1;\r
876         int intVal = -1;\r
877         String query;\r
878         String historyTable(HISTORY_DATA_TABLE);\r
879         result r = E_FAILURE;\r
880         bool nextRowPresent = false;\r
881 \r
882         text.Replace(L"'", L"''");\r
883         query.Append(L"SELECT COUNT(ID) FROM ");\r
884         query.Append(historyTable);\r
885         query.Append(L" WHERE (TITLE LIKE '%");\r
886         query.Append(text);\r
887         query.Append("%')");\r
888         query.Append(" OR ");\r
889         query.Append(L"(ADDRESS LIKE '%");\r
890         query.Append(text);\r
891         query.Append("%')");\r
892 \r
893         r = HistoryPresentationModel::ExecuteQuery(query, count);\r
894         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistoryCount query failed %s",GetErrorMessage(r));\r
895 \r
896         r = HistoryPresentationModel::DbIsNextRowPresent(nextRowPresent);\r
897         if (IsFailed(r) != null)\r
898                 return r;\r
899         if (nextRowPresent == true)\r
900         {\r
901                 r = HistoryPresentationModel::GetColumn(0, intVal);\r
902                 if (IsFailed(r) != null)\r
903                 {\r
904                         AppLogDebug("Error:HistoryPresentationModel::DbGetColumn:%s", GetErrorMessage(r));\r
905                         return r;\r
906                 }\r
907         }\r
908         historyCount = intVal;\r
909 \r
910         CATCH: return r;\r
911 }\r
912 \r
913 result\r
914 HistoryPresentationModel::GetHistoryWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime,const int startIndex,const int limit, ArrayList& pHistoryList)\r
915 {\r
916         int count = -1;\r
917         String query;\r
918         String historyTable(HISTORY_DATA_TABLE);\r
919         result r = E_FAILURE;\r
920 \r
921         query.Append(L"SELECT ");\r
922         //query.Append(historyTable);\r
923         query.Append(L"* ");\r
924         query.Append(L"FROM ");\r
925         query.Append(historyTable);\r
926         query.Append(" WHERE ");\r
927         //query.Append(historyTable);\r
928         query.Append(L"VISITDATE >= ");\r
929         query.Append("DateTime(");\r
930         query.Append("'");\r
931         query.Append(GetStringFromDate(startTime));\r
932         query.Append("')");\r
933         query.Append(" AND ");\r
934         //query.Append(historyTable);\r
935         query.Append(L"VISITDATE <= ");\r
936         query.Append("DateTime(");\r
937         query.Append("'");\r
938         query.Append(GetStringFromDate(endTime));\r
939         query.Append("')");\r
940         query.Append(" ORDER BY VISITDATE DESC");\r
941 \r
942         //Append LIMIT\r
943         if (limit > 0)\r
944         {\r
945                 query.Append(" LIMIT ");\r
946                 query.Append(limit);\r
947 \r
948                 //Append OFFESET\r
949                 if (startIndex >= 0)\r
950                 {\r
951                         query.Append(" OFFSET ");\r
952                         query.Append(startIndex);\r
953                 }\r
954         }\r
955 \r
956         r = HistoryPresentationModel::ExecuteQuery(query, count);\r
957         AppLog("the count is %d",count);\r
958         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));\r
959 \r
960         r = CreateHistoryList(count, pHistoryList);\r
961         if (IsFailed(r))\r
962         {\r
963                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));\r
964         }\r
965 \r
966         CATCH: return r;\r
967 }\r
968 \r
969 result\r
970 HistoryPresentationModel::GetMostVisitedSites(ArrayList& pHistoryList)\r
971 {\r
972 \r
973         int count = -1;\r
974         String query;\r
975         String historyTable(HISTORY_DATA_TABLE);\r
976         result r = E_FAILURE;\r
977 \r
978 \r
979         //query = L"SELECT *, COUNT(URL) AS NOS FROM HistoryData GROUP BY URL ORDER BY NOS DESC";\r
980         query = L"SELECT History.*, COUNT(History.ADDRESS) AS NOS FROM History WHERE History.ADDRESS != '' GROUP BY History.ADDRESS ORDER BY NOS DESC";\r
981 \r
982         r = HistoryPresentationModel::ExecuteQuery(query, count);\r
983         AppLog("the count is %d",count);\r
984         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));\r
985 \r
986         r = CreateHistoryList(count, pHistoryList, 9);\r
987         if (IsFailed(r))\r
988         {\r
989                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));\r
990         }\r
991 \r
992         CATCH: return r;\r
993 }\r
994 \r
995 result\r
996 HistoryPresentationModel::GetUrlCount(const String& Url, int& historyCount)\r
997 {\r
998         int count = -1;\r
999         int intVal = -1;\r
1000         String query;\r
1001         String historyTable(HISTORY_DATA_TABLE);\r
1002         result r = E_FAILURE;\r
1003         bool nextRowPresent = false;\r
1004 \r
1005         query.Append(L"SELECT COUNT(ADDRESS) FROM ");\r
1006         query.Append(historyTable);\r
1007         query.Append(L" WHERE ADDRESS = ");\r
1008         query.Append("'");\r
1009         query.Append(Url);\r
1010         query.Append("'");\r
1011 \r
1012         r = PresentationModelBase::ExecuteQuery(query, count);\r
1013         AppLog("the count is %d", count);\r
1014         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));\r
1015 \r
1016         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);\r
1017         if (IsFailed(r))\r
1018                 return r;\r
1019         if (nextRowPresent == true)\r
1020         {\r
1021                 r = PresentationModelBase::GetColumn(0, intVal);\r
1022                 if (IsFailed(r))\r
1023                 {\r
1024                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));\r
1025                         return r;\r
1026                 }\r
1027         }\r
1028         historyCount = intVal;\r
1029 \r
1030     CATCH: return r;\r
1031 }\r
1032 \r
1033 String\r
1034 HistoryPresentationModel::GenerateFileName(void)\r
1035 {\r
1036         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();\r
1037         if ( pAppRegistry == NULL )\r
1038         {\r
1039                 return NULL ;\r
1040         }\r
1041         String keyCount("ThumbnailCount");\r
1042         String fileName = UiApp::GetInstance()->GetAppRootPath() + "/data/Thumbnail/thumbnails";\r
1043         result r = E_SUCCESS;\r
1044         int keyValue = 1;\r
1045         r = pAppRegistry->Get(keyCount, keyValue);\r
1046 \r
1047         if (r == E_KEY_NOT_FOUND)\r
1048         {\r
1049                 keyValue = 1;\r
1050                 pAppRegistry->Add(keyCount, keyValue);\r
1051         }\r
1052         else\r
1053         {\r
1054                 keyValue++;\r
1055                 pAppRegistry->Set(keyCount, keyValue);\r
1056         }\r
1057 \r
1058         fileName.Append(keyValue);\r
1059         fileName.Append(".jpg");\r
1060         pAppRegistry->Save();\r
1061 \r
1062         return fileName;\r
1063 }\r
1064 \r
1065 String\r
1066 HistoryPresentationModel::GetStringFromDate(DateTime& date)\r
1067 {\r
1068         String dateFormat = L"";\r
1069         dateFormat.Append(date.GetYear());\r
1070         dateFormat.Append('-');\r
1071         if(date.GetMonth() < 10)\r
1072         {\r
1073                 dateFormat.Append('0');\r
1074         }\r
1075         dateFormat.Append(date.GetMonth());\r
1076         dateFormat.Append('-');\r
1077         if(date.GetDay() < 10)\r
1078         {\r
1079                 dateFormat.Append('0');\r
1080         }\r
1081         dateFormat.Append(date.GetDay());\r
1082         dateFormat.Append(' ');\r
1083         if(date.GetHour() < 10)\r
1084         {\r
1085                 dateFormat.Append('0');\r
1086         }\r
1087         dateFormat.Append(date.GetHour());\r
1088         dateFormat.Append(':');\r
1089         if(date.GetMinute() < 10)\r
1090         {\r
1091                 dateFormat.Append('0');\r
1092         }\r
1093         dateFormat.Append(date.GetMinute());\r
1094         dateFormat.Append(':');\r
1095         if(date.GetSecond() < 10)\r
1096         {\r
1097                 dateFormat.Append('0');\r
1098         }\r
1099         dateFormat.Append(date.GetSecond());\r
1100         return dateFormat;\r
1101 }\r