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("SaveHistory history url is %ls",history.GetHistoryUrl().GetPointer());
360
361                 if(pHistoryItem != null && pHistoryItem->GetHistoryUrl().CompareTo(history.GetHistoryUrl()) == 0)
362                 {
363                         history.SetHistoryId(pHistoryItem->GetHistoryId());
364                         AppLog("HistoryPresentationModel::saveHistory pHistoryItem history is %ls",pHistoryItem->GetHistoryId().GetPointer());
365                         break;
366                 }
367         }
368
369         pMostVisitedSites = new(std::nothrow) ArrayList();
370         pMostVisitedSites->Construct();
371         String historyTitle = history.GetHistoryTitle();
372         historyTitle.Replace(L"'", L"''");
373
374         String historyUrl = history.GetHistoryUrl();
375         historyUrl.Replace(L"'", L"''");
376
377         PresentationModelBase::GetCurrentDateTime(date);
378         GetMostVisitedSites(*pMostVisitedSites);
379         GetUrlCount(historyUrl, urlCount);
380         for (i = 0; i < pMostVisitedSites->GetCount(); i++)
381         {
382                 History* pHistory = static_cast< History* >(pMostVisitedSites->GetAt(i));
383                 if (pHistory != null && pHistory->GetHistoryUrl().CompareTo(historyUrl) == 0)
384                 {
385                         AppLog("HistoryPresentationModel::saveHistory isalreadyexist true");
386                         isAlreadyExist = true;
387                         break;
388                 }
389                 if ( pHistory != null &&  urlCount >= pHistory->GetVisitedCount())
390                 {
391                         isEligibleForMostVisitedSites = true;
392                 }
393                 else
394                 {
395                         isEligibleForMostVisitedSites = false;
396                 }
397         }
398
399         if (pMostVisitedSites->GetCount() < 9 || (isAlreadyExist == false && isEligibleForMostVisitedSites == true && history.GetThumbnail() != null))
400         {
401                 AppLog("HistoryPresentationModel::SaveHistory coming here");
402
403                 thumbnailPath = GenerateFileName();
404                 AppLog("generating file path and url %ls, %ls",thumbnailPath.GetPointer(),historyUrl.GetPointer());
405                 Image* pImage = new(std::nothrow) Image();
406                 pImage->Construct();
407                 result r = pImage->EncodeToFile(*history.GetThumbnail(), IMG_FORMAT_JPG, thumbnailPath, true);
408                 delete pImage;
409                 if(IsFailed(r))
410                 {
411                         AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));
412                         return r;
413                 }
414         }
415         delete pMostVisitedSites;
416         pMostVisitedSites = NULL ;
417
418         if (history.GetHistoryId() == "")
419         {
420                 AppLog("HistoryPresentationModel::saveHistory GetHistoryId is blank");
421
422                 columnNames.Append(L"ADDRESS, ");
423                 columnNames.Append(L"TITLE, ");
424                 columnNames.Append(L"VISITDATE, ");
425                 columnNames.Append(L"FAVICON, ");
426                 columnNames.Append(L"FAVICON_W, ");
427                 columnNames.Append(L"FAVICON_H");
428
429                 query.Append(L"INSERT INTO ");
430                 query.Append(historyTable);
431                 query.Append("(");
432                 query.Append(columnNames);
433                 query.Append(") ");
434                 query.Append("VALUES");
435                 query.Append(" (?,?,?,?,?,?)");
436         }
437         else
438         {
439                 query.Append(L"UPDATE ");
440                 query.Append(historyTable);
441                 query.Append(" SET ADDRESS = ");
442                 query.Append("'");
443                 query.Append(historyUrl);
444                 query.Append("'");
445                 query.Append(", TITLE = ");
446                 query.Append("'");
447                 query.Append(historyTitle);
448                 query.Append("'");
449                 query.Append(", VISITED_TIME = ");
450                 query.Append("DateTime(");
451                 query.Append("'");
452                 query.Append(GetStringFromDate(date));
453                 query.Append("')");
454                 query.Append("'");
455                 query.Append(" WHERE ID = ");
456                 query.Append(history.GetHistoryId());
457         }
458
459         AppLog("HistoryPresentationModel::SaveHistory query is %S",query.GetPointer());
460
461         r = PresentationModelBase::ExecuteHistoryQuery(query, resultCount, history);
462         if (r == E_SUCCESS)
463         {
464                 AppLog("HistoryPresentationModel::SaveHistory result is success");
465         }
466         else
467         {
468                 AppLog("HistoryPresentationModel::SaveHistory result is failure");
469         }
470         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveHistory query failed  %s",GetErrorMessage(r));
471
472         r = PresentationModelBase::CommitDb();
473
474         r = GetLastInsertedId(historyTable, rowId);
475         if (IsFailed(r))
476         {
477                 AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));
478                 return r;
479         }
480         if (rowId < 0)
481                 return E_FAILURE;
482
483         historyId.Append(rowId);
484         if (historyId.GetLength() < 0)
485         {
486                 return E_FAILURE;
487         }
488         //Set the generated ID to History
489         history.SetHistoryId(historyId);
490
491         if(__pDataList)
492         {
493                 delete __pDataList;
494                 __pDataList = null;
495         }
496
497         CATCH: return r;
498
499 }
500
501 result
502 HistoryPresentationModel::DeleteMultipleHistory(ArrayList* pHistoryIdList)
503 {
504         result r = E_FAILURE;
505         String query;
506         String historyTable = HISTORY_DATA_TABLE;
507         int resultCount = -1;
508
509         if (pHistoryIdList == null)
510         {
511                 return E_FAILURE;
512         }
513
514         query.Append(L"DELETE FROM ");
515         query.Append(historyTable);
516         query.Append(" WHERE");
517         for(int index = 0; index <= pHistoryIdList->GetCount()-2; index++)
518         {
519                 query.Append(" ID = ");
520                 String* hisId = static_cast<String*>(pHistoryIdList->GetAt(index));
521                 query.Append(*hisId);
522                 query.Append(" OR ");
523         }
524         query.Append(" ID = ");
525         String* hisId = static_cast<String*>(pHistoryIdList->GetAt(pHistoryIdList->GetCount()-1));
526         if(hisId == null)
527         {
528                 return E_FAILURE;
529         }
530         query.Append(*hisId);
531
532         r = PresentationModelBase::ExecuteQuery(query, resultCount);
533         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
534
535         r = PresentationModelBase::CommitDb();
536
537         if(__pDataList)
538         {
539                 delete __pDataList;
540                 __pDataList = null;
541         }
542
543         CATCH: return r;
544 }
545
546 result
547 HistoryPresentationModel::DeleteHistory(const int historyId)
548 {
549         result r = E_FAILURE;
550         String query;
551         String historyTable = HISTORY_DATA_TABLE;
552         int resultCount = -1;
553
554         if (historyId < 1)
555         {
556                 return E_INVALID_ARG;
557         }
558
559         query.Append(L"DELETE FROM ");
560         query.Append(historyTable);
561         query.Append(" WHERE ID = ");
562         query.Append(historyId);
563
564         r = PresentationModelBase::ExecuteQuery(query, resultCount);
565         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
566
567         r = PresentationModelBase::CommitDb();
568
569         if(__pDataList)
570         {
571                 delete __pDataList;
572                 __pDataList = null;
573         }
574
575         CATCH: return r;
576 }
577
578 result
579 HistoryPresentationModel::DeleteHistory(String& historyUrl)
580 {
581         result r = E_FAILURE;
582         String query;
583         String historyTable = HISTORY_DATA_TABLE;
584         int resultCount = -1;
585
586         query.Append(L"DELETE FROM ");
587         query.Append(historyTable);
588         query.Append(" WHERE ADDRESS = '");
589         query.Append(historyUrl);
590         query.Append("'");
591
592         r = PresentationModelBase::ExecuteQuery(query, resultCount);
593         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
594
595         r = PresentationModelBase::CommitDb();
596
597         if(__pDataList)
598         {
599                 delete __pDataList;
600                 __pDataList = null;
601         }
602
603         CATCH: return r;
604 }
605
606 result
607 HistoryPresentationModel::ClearHistory(void)
608 {
609         result r = E_FAILURE;
610         String query;
611         String historyTable = HISTORY_DATA_TABLE;
612         int resultCount = -1;
613
614         query.Append(L"DELETE FROM ");
615         query.Append(historyTable);
616
617         r = PresentationModelBase::ExecuteQuery(query, resultCount);
618         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::ClearHistory GetLastInsertedId failed %s",GetErrorMessage(r));
619
620         r = PresentationModelBase::CommitDb();
621
622     CATCH: return r;
623 }
624
625 result
626 HistoryPresentationModel::GetHistoryCount(int& historyCount)
627 {
628         int count = -1;
629         int intVal = -1;
630         String query;
631         String historyTable(HISTORY_DATA_TABLE);
632         result r = E_FAILURE;
633         bool nextRowPresent = false;
634
635         query.Append(L"SELECT COUNT(ID) FROM ");
636         query.Append(historyTable);
637
638         r = PresentationModelBase::ExecuteQuery(query, count);
639         AppLog("the count is %d", count);
640         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
641
642         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
643         if (IsFailed(r) != null)
644                 return r;
645         if (nextRowPresent == true)
646         {
647                 r = PresentationModelBase::GetColumn(0, intVal);
648                 if (IsFailed(r) != null)
649                 {
650                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
651                         return r;
652                 }
653         }
654         historyCount = intVal;
655
656     CATCH: return r;
657 }
658
659 result
660 HistoryPresentationModel::GetHistoryCountWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime, int& historyCount)
661 {
662         int count = 0;
663         result r = E_SUCCESS;
664
665         if(__pDataList != null)
666         {
667                 delete __pDataList;
668                 __pDataList = null;
669         }
670
671         //if(__pDataList == null)
672         //{
673                 __pDataList = new(std::nothrow) ArrayList();
674                 __pDataList->Construct();
675
676                 r = GetHistoryCount(count);
677                 if(IsFailed(r))
678                 {
679                         return r;
680                 }
681                 if(count > 0)
682                 {
683                         r = GetHistory(0, count, *__pDataList);
684                         if (IsFailed(r))
685                         {
686                                 return r;
687                         }
688                 }
689         //}
690         //else
691         //{
692                 count = __pDataList->GetCount();
693         //}
694
695         for (int counter = 0 ; counter < count; counter++)
696         {
697                 History *pHistory = dynamic_cast<History*>(__pDataList->GetAt(counter));
698                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
699                 {
700                         historyCount++;
701                 }
702         }
703         return r;
704 }
705
706 result
707 HistoryPresentationModel::GetHistory(const int startIndex,const int limit, ArrayList& pHistoryList)
708 {
709
710         int count = -1;
711         String query;
712         String historyTable(HISTORY_DATA_TABLE);
713         result r = E_FAILURE;
714
715         query.Append(L"SELECT * from ");
716         query.Append(historyTable);
717         query.Append(" ORDER BY VISITDATE DESC");
718
719
720         //Append LIMIT
721         if (limit > 0)
722         {
723                 query.Append(" LIMIT ");
724                 query.Append(limit);
725
726                 //Append OFFESET
727                 if (startIndex >= 0)
728                 {
729                         query.Append(" OFFSET ");
730                         query.Append(startIndex);
731                 }
732         }
733
734         r = PresentationModelBase::ExecuteQuery(query, count);
735         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistory query failed %s",GetErrorMessage(r));
736
737         r = CreateHistoryList(count, pHistoryList);
738         if (IsFailed(r))
739         {
740                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
741         }
742         CATCH: return r;
743 }
744
745 result
746 HistoryPresentationModel::CreateHistoryList(int historyCount, ArrayList& historyList, int maxCount)
747 {
748
749         History* pHistory = null;
750         int intVal = -1;
751         String stringVal;
752         DateTime dateVal;
753         result r = E_FAILURE;
754         String historyId;
755         String faviconId;
756         String bookmarkId;
757         int visitedCount;
758         DateTime visitedTime;
759         String iconPath;
760         String thumbnailPath;
761         bool nextRowPresent = false;
762         Image image;
763         Bitmap* pThumnailBitmap = null;
764         ByteBuffer *pFavIconBuffer;
765
766         image.Construct();
767         if (historyCount < 1)
768         {
769                 return E_SUCCESS;
770         }
771
772         for (int Count = 0; (maxCount == -1 && Count < historyCount ) || (maxCount != -1 &&  Count < maxCount && Count < historyCount); Count++)
773         {
774                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
775                 if (IsFailed(r))
776                 {
777                         return E_SUCCESS;
778                 }
779                 if (nextRowPresent == true)
780                 {
781                         pHistory = new(std::nothrow) History;
782                         if (pHistory)
783                         {
784                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
785                                 {
786
787                                         switch (columnCount)
788                                         {
789                                         case HISTORY_ID:
790                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
791                                                 if (!IsFailed(r))
792                                                 {
793                                                         if (intVal > 0)
794                                                         {
795                                                                 historyId.Clear();
796                                                                 historyId.Append(intVal);
797                                                                 if (historyId.GetLength() > 0)
798                                                                 {
799                                                                         pHistory->SetHistoryId(historyId);
800                                                                 }
801                                                         }
802
803                                                 }
804                                                 break;
805                                         case HISTORY_URL:
806                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
807                                                 if (!IsFailed(r))
808                                                 {
809                                                         pHistory->SetHistoryUrl(stringVal);
810                                                 }
811                                                 break;
812                                         case HISTORY_TITLE:
813                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
814                                                 if (!IsFailed(r))
815                                                 {
816                                                         pHistory->SetHistoryTitle(stringVal);
817                                                 }
818                                                 break;
819
820                                         case HISTORY_VISITED_TIME:
821                                                 r = PresentationModelBase::GetColumn(columnCount, dateVal);
822                                                 if (!IsFailed(r))
823                                                 {
824                                                         visitedTime = dateVal;
825                                                         pHistory->SetVisitedTime(visitedTime);
826                                                 }
827                                                 break;
828
829                                         case HISTORY_FAVICON:
830                                         {
831                                                 pFavIconBuffer = new ByteBuffer();
832                                                 r = PresentationModelBase::GetColumn(columnCount, *pFavIconBuffer);
833                                                 if (!IsFailed(r))
834                                                 {
835                                                         pHistory->SetFavIconBuffer(*pFavIconBuffer);
836                                                 }
837                                         }
838                                         break;
839                                         case HISTORY_FAVICON_LENGTH:
840                                         {
841
842                                         }
843                                         break;
844                                         case HISTORY_FAVICON_W:
845                                         {
846                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
847                                                 if (!IsFailed(r))
848                                                 {
849                                                         if (intVal > 0)
850                                                         {
851                                                                 pHistory->SetFavIconWidth(intVal);
852                                                         }
853                                                 }
854                                         }
855                                         break;
856                                         case HISTORY_FAVICON_H:
857                                         {
858                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
859                                                 if (!IsFailed(r))
860                                                 {
861                                                         if (intVal > 0)
862                                                         {
863                                                                 pHistory->SetFavIconHeight(intVal);
864                                                         }
865                                                 }
866                                         }
867                                         break;
868
869                                         case HISTORY_VISITED_COUNT:
870                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
871                                                 if (!IsFailed(r))
872                                                 {
873                                                         if (intVal > 0)
874                                                         {
875                                                                 pHistory->SetVisitedCount(intVal);
876
877                                                         }
878                                                 }
879                                                 break;
880
881                                         default:
882                                                 break;
883                                         }
884
885                                 }
886                                 r = historyList.Add(*pHistory);
887
888                                 if (IsFailed(r))
889                                 {
890                                         delete pHistory;
891                                         return r;
892                                 }
893                         }
894                 }
895         }
896         return E_SUCCESS;
897 }
898
899 result
900 HistoryPresentationModel::GetSearchHistory(const int startIndex,const int limit, ArrayList& pHistoryList, String& text)
901 {
902         text.Replace(L"'", L"''");
903
904         int count = -1;
905         String query;
906         String historyTable(HISTORY_DATA_TABLE);
907         result r = E_FAILURE;
908
909         query.Append(L"SELECT ");
910         query.Append(historyTable);
911         query.Append(L".* ");
912         query.Append(L"FROM ");
913         query.Append(historyTable);
914         query.Append(L" WHERE (");
915         query.Append(historyTable);
916         query.Append(L".TITLE LIKE '%");
917         query.Append(text);
918         query.Append("%')");
919         query.Append(" OR ");
920         query.Append(L"(");
921         query.Append(historyTable);
922         query.Append(L".ADDRESS LIKE '%");
923         query.Append(text);
924         query.Append("%')");
925         query.Append(" ORDER BY VISITDATE DESC");
926
927         //Append LIMIT
928         if (limit > 0)
929         {
930                 query.Append(" LIMIT ");
931                 query.Append(limit);
932
933                 //Append OFFESET
934                 if (startIndex >= 0)
935                 {
936                         query.Append(" OFFSET ");
937                         query.Append(startIndex);
938                 }
939         }
940
941         r = HistoryPresentationModel::ExecuteQuery(query, count);
942         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));
943
944         r = CreateHistoryList(count, pHistoryList);
945         if (IsFailed(r))
946         {
947                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
948         }
949         CATCH: return r;
950 }
951
952 result
953 HistoryPresentationModel::GetSearchHistoryCount(int& historyCount, String& text)
954 {
955         int count = -1;
956         int intVal = -1;
957         String query;
958         String historyTable(HISTORY_DATA_TABLE);
959         result r = E_FAILURE;
960         bool nextRowPresent = false;
961
962         text.Replace(L"'", L"''");
963         query.Append(L"SELECT COUNT(ID) FROM ");
964         query.Append(historyTable);
965         query.Append(L" WHERE (TITLE LIKE '%");
966         query.Append(text);
967         query.Append("%')");
968         query.Append(" OR ");
969         query.Append(L"(ADDRESS LIKE '%");
970         query.Append(text);
971         query.Append("%')");
972
973         r = HistoryPresentationModel::ExecuteQuery(query, count);
974         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistoryCount query failed %s",GetErrorMessage(r));
975
976         r = HistoryPresentationModel::DbIsNextRowPresent(nextRowPresent);
977         if (IsFailed(r) != null)
978                 return r;
979         if (nextRowPresent == true)
980         {
981                 r = HistoryPresentationModel::GetColumn(0, intVal);
982                 if (IsFailed(r) != null)
983                 {
984                         AppLogDebug("Error:HistoryPresentationModel::DbGetColumn:%s", GetErrorMessage(r));
985                         return r;
986                 }
987         }
988         historyCount = intVal;
989
990         CATCH: return r;
991 }
992
993 result
994 HistoryPresentationModel::GetHistoryWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime,const int startIndex,const int limit, ArrayList& pHistoryList)
995 {
996         int count = 0;
997         result r = E_SUCCESS;
998         if(__pDataList == null)
999         {
1000                 __pDataList = new(std::nothrow) ArrayList();
1001                 __pDataList->Construct();
1002
1003                 r = GetHistoryCount(count);
1004                 if(IsFailed(r))
1005                 {
1006                         return r;
1007                 }
1008                 if(count > 0)
1009                 {
1010                         r = GetHistory(0, count, *__pDataList);
1011                         if(IsFailed(r))
1012                         {
1013                                 return r;
1014                         }
1015                 }
1016         }
1017         else
1018         {
1019                 count = __pDataList->GetCount();
1020         }
1021         int itemsFoundIndex = -1;
1022         for (int counter = 0 ; counter < count; counter++)
1023         {
1024                 History *pHistory = dynamic_cast<History*>(__pDataList->GetAt(counter));
1025                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
1026                 {
1027                         itemsFoundIndex++;
1028                         if(itemsFoundIndex >= startIndex && itemsFoundIndex < (limit + startIndex))
1029                         {
1030                                 pHistoryList.Add(*pHistory);
1031                         }
1032                         else if(itemsFoundIndex > (limit + startIndex))
1033                         {
1034                                 break;
1035                         }
1036                 }
1037         }
1038         return r;
1039 }
1040
1041 result
1042 HistoryPresentationModel::GetMostVisitedSites(ArrayList& pHistoryList)
1043 {
1044
1045         int count = -1;
1046         String query;
1047         String historyTable(HISTORY_DATA_TABLE);
1048         result r = E_FAILURE;
1049
1050
1051         //query = L"SELECT *, COUNT(URL) AS NOS FROM HistoryData GROUP BY URL ORDER BY NOS DESC";
1052         query = L"SELECT History.*, COUNT(History.ADDRESS) AS NOS FROM History WHERE History.ADDRESS != '' GROUP BY History.ADDRESS ORDER BY NOS DESC";
1053
1054         r = HistoryPresentationModel::ExecuteQuery(query, count);
1055         AppLog("the count is %d",count);
1056         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));
1057
1058         r = CreateHistoryList(count, pHistoryList, 9);
1059         if (IsFailed(r))
1060         {
1061                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
1062         }
1063
1064         CATCH: return r;
1065 }
1066
1067 result
1068 HistoryPresentationModel::GetUrlCount(const String& Url, int& historyCount)
1069 {
1070         int count = -1;
1071         int intVal = -1;
1072         String query;
1073         String historyTable(HISTORY_DATA_TABLE);
1074         result r = E_FAILURE;
1075         bool nextRowPresent = false;
1076
1077         query.Append(L"SELECT COUNT(ADDRESS) FROM ");
1078         query.Append(historyTable);
1079         query.Append(L" WHERE ADDRESS = ");
1080         query.Append("'");
1081         query.Append(Url);
1082         query.Append("'");
1083
1084         r = PresentationModelBase::ExecuteQuery(query, count);
1085         AppLog("the count is %d", count);
1086         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
1087
1088         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
1089         if (IsFailed(r))
1090                 return r;
1091         if (nextRowPresent == true)
1092         {
1093                 r = PresentationModelBase::GetColumn(0, intVal);
1094                 if (IsFailed(r))
1095                 {
1096                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
1097                         return r;
1098                 }
1099         }
1100         historyCount = intVal;
1101
1102     CATCH: return r;
1103 }
1104
1105 String
1106 HistoryPresentationModel::GenerateFileName(void)
1107 {
1108         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
1109         if ( pAppRegistry == NULL )
1110         {
1111                 return NULL ;
1112         }
1113         String keyCount("ThumbnailCount");
1114         String fileName = UiApp::GetInstance()->GetAppRootPath() + "/data/Thumbnail/thumbnails";
1115         result r = E_SUCCESS;
1116         int keyValue = 1;
1117         r = pAppRegistry->Get(keyCount, keyValue);
1118
1119         if (r == E_KEY_NOT_FOUND)
1120         {
1121                 keyValue = 1;
1122                 pAppRegistry->Add(keyCount, keyValue);
1123         }
1124         else
1125         {
1126                 keyValue++;
1127                 pAppRegistry->Set(keyCount, keyValue);
1128         }
1129
1130         fileName.Append(keyValue);
1131         fileName.Append(".jpg");
1132         pAppRegistry->Save();
1133
1134         return fileName;
1135 }
1136
1137 String
1138 HistoryPresentationModel::GetStringFromDate(DateTime& date)
1139 {
1140         String dateFormat = L"";
1141         dateFormat.Append(date.GetYear());
1142         dateFormat.Append('-');
1143         if(date.GetMonth() < 10)
1144         {
1145                 dateFormat.Append('0');
1146         }
1147         dateFormat.Append(date.GetMonth());
1148         dateFormat.Append('-');
1149         if(date.GetDay() < 10)
1150         {
1151                 dateFormat.Append('0');
1152         }
1153         dateFormat.Append(date.GetDay());
1154         dateFormat.Append(' ');
1155         if(date.GetHour() < 10)
1156         {
1157                 dateFormat.Append('0');
1158         }
1159         dateFormat.Append(date.GetHour());
1160         dateFormat.Append(':');
1161         if(date.GetMinute() < 10)
1162         {
1163                 dateFormat.Append('0');
1164         }
1165         dateFormat.Append(date.GetMinute());
1166         dateFormat.Append(':');
1167         if(date.GetSecond() < 10)
1168         {
1169                 dateFormat.Append('0');
1170         }
1171         dateFormat.Append(date.GetSecond());
1172         return dateFormat;
1173 }