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