Initialize Tizen 2.3
[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.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
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                 __pHistoryPresentationModel->UnInitialize();
74                 delete __pHistoryPresentationModel;
75                 __pHistoryPresentationModel = null;
76         }
77 }
78
79 HistoryPresentationModel::HistoryPresentationModel(void)
80 {
81         __pDataList = null;
82 }
83
84 HistoryPresentationModel::~HistoryPresentationModel(void)
85 {
86
87 }
88
89 HistoryPresentationModel::HistoryPresentationModel(const HistoryPresentationModel& historyModelObj)
90 {
91
92 }
93
94 HistoryPresentationModel& HistoryPresentationModel::operator=(const HistoryPresentationModel& historyModelObj)
95 {
96         return *this;
97 }
98
99 result
100 HistoryPresentationModel::Construct(void)
101 {
102         result r = E_SUCCESS;
103         DB_FILE_PATH = L"/opt/usr/dbspace/.browser-history.db";
104         r = PresentationModelBase::Initialize();
105         TryCatch( !IsFailed(r),,"Failed to initialize HistoryPresentationModel Model %s",GetErrorMessage(r));
106
107         CATCH: return r;
108
109 }
110
111 result
112 HistoryPresentationModel::SaveHistory(History& history)
113 {
114         AppLog("HistoryPresentationModel::saveHistory");
115         result r = E_FAILURE;
116         String historyTable(HISTORY_DATA_TABLE);
117         String query;
118         String columnNames;
119         String attachment;
120         int resultCount = -1;
121         int rowId = -1;
122         DateTime date;
123         String historyId;
124         int urlCount = 0;
125         ArrayList* pMostVisitedSites = null;
126         bool isAlreadyExist = false;
127         bool isEligibleForMostVisitedSites = true;
128         int count;
129         String thumbnailPath;
130         int todaysCount = 0;
131         DateTime todayStart;
132         DateTime todayEnd;
133         SystemTime::GetCurrentTime(todayStart);
134         SystemTime::GetCurrentTime(todayEnd);
135         todayStart.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 0,0,0);
136         todayEnd.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 23,59,59);
137
138         ArrayList* pTodaysList = new(std::nothrow) ArrayList();
139         pTodaysList->Construct();
140         GetHistoryCountWithTimeRange(todayStart, todayEnd, todaysCount);
141         GetHistoryWithTimeRange(todayStart, todayEnd, 0, todaysCount, *pTodaysList);
142
143         for (int count = 0; count < todaysCount; count++)
144         {
145                 History *pHistoryItem  = dynamic_cast<History*>(pTodaysList->GetAt(count));
146
147                 if (pHistoryItem != null && pHistoryItem->GetHistoryUrl().CompareTo(history.GetHistoryUrl()) == 0)
148                 {
149                         AppLog("SaveHistory pHistoryItem url is %ls",pHistoryItem->GetHistoryUrl().GetPointer());
150                         AppLog("SaveHistory history url is %ls",history.GetHistoryUrl().GetPointer());
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 void HistoryPresentationModel::UpdateHistoryFavIcon(History& history, Tizen::Graphics::Bitmap& favIconImage)
325 {
326         result r = E_FAILURE;
327         String historyTable(HISTORY_DATA_TABLE);
328         String query;
329         String columnNames;
330         int resultCount = -1;
331
332         Image* pImage = null;
333         pImage = new Image();
334         pImage->Construct();
335
336         Tizen::Base::ByteBuffer* pFavBuffer = pImage->EncodeToBufferN(favIconImage, IMG_FORMAT_PNG);
337         // added because conversion may fail
338         if (GetLastResult() != E_SUCCESS)
339         {
340                 delete pImage;
341                 return;
342         }
343         delete pFavBuffer;
344         history.SetFavIconBitmap(favIconImage);
345
346         delete pImage;
347
348         query.Append(L"UPDATE ");
349         query.Append(historyTable);
350         query.Append(" SET FAVICON = ");
351         query.Append("?");
352         query.Append(", FAVICON_W = ");
353         query.Append("?");
354         query.Append(", FAVICON_H = ");
355         query.Append("?");
356         query.Append(" WHERE ID = ");
357         query.Append("?");
358
359         AppLog("query is %ls", query.GetPointer());
360
361         r = PresentationModelBase::ExecuteUpdateHistoryFavIconQuery(query, resultCount, history);
362         if (r == E_SUCCESS)
363         {
364                 AppLog("HistoryPresentationModel::SaveHistory result is success");
365         }
366         else
367         {
368                 AppLog("HistoryPresentationModel::SaveHistory result is failure");
369         }
370         r = PresentationModelBase::CommitDb();
371
372 }
373
374 result
375 HistoryPresentationModel::SaveTempHistory(History& history)
376 {
377         AppLog("HistoryPresentationModel::saveHistory");
378         result r = E_FAILURE;
379         String historyTable(HISTORY_DATA_TABLE);
380         String query;
381         String columnNames;
382         String attachment;
383         int resultCount = -1;
384         int rowId = -1;
385         DateTime date;
386         String historyId;
387         int urlCount = 0;
388         ArrayList* pMostVisitedSites = null;
389         bool isAlreadyExist = false;
390         bool isEligibleForMostVisitedSites = true;
391         int i;
392         String thumbnailPath;
393         int todaysCount = 0;
394         DateTime todayStart;
395         DateTime todayEnd;
396         SystemTime::GetCurrentTime(todayStart);
397         SystemTime::GetCurrentTime(todayEnd);
398         todayStart.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 0,0,0);
399         todayEnd.SetValue(todayStart.GetYear(), todayStart.GetMonth(), todayStart.GetDay(), 23,59,59);
400
401         ArrayList* pTodaysList = new(std::nothrow) ArrayList();
402         pTodaysList->Construct();
403         GetHistoryCountWithTimeRange(todayStart, todayEnd, todaysCount);
404         GetHistoryWithTimeRange(todayStart, todayEnd, 0, todaysCount, *pTodaysList);
405
406         for(int count = 0; count < todaysCount; count++)
407         {
408                 History *pHistoryItem  = dynamic_cast<History*>(pTodaysList->GetAt(count));
409                 AppLog("SaveHistory history url is %ls",history.GetHistoryUrl().GetPointer());
410
411                 if(pHistoryItem != null && pHistoryItem->GetHistoryUrl().CompareTo(history.GetHistoryUrl()) == 0)
412                 {
413                         history.SetHistoryId(pHistoryItem->GetHistoryId());
414                         AppLog("HistoryPresentationModel::saveHistory pHistoryItem history is %ls",pHistoryItem->GetHistoryId().GetPointer());
415                         break;
416                 }
417         }
418
419         if (pTodaysList)
420         {
421                 pTodaysList->RemoveAll();
422                 delete pTodaysList;
423         }
424
425         pMostVisitedSites = new(std::nothrow) ArrayList();
426         pMostVisitedSites->Construct();
427         String historyTitle = history.GetHistoryTitle();
428         historyTitle.Replace(L"'", L"''");
429
430         String historyUrl = history.GetHistoryUrl();
431         historyUrl.Replace(L"'", L"''");
432
433         PresentationModelBase::GetCurrentDateTime(date);
434         GetMostVisitedSites(*pMostVisitedSites);
435         GetUrlCount(historyUrl, urlCount);
436         for (i = 0; i < pMostVisitedSites->GetCount(); i++)
437         {
438                 History* pHistory = static_cast< History* >(pMostVisitedSites->GetAt(i));
439                 if (pHistory != null && pHistory->GetHistoryUrl().CompareTo(historyUrl) == 0)
440                 {
441                         AppLog("HistoryPresentationModel::saveHistory isalreadyexist true");
442                         isAlreadyExist = true;
443                         break;
444                 }
445                 if ( pHistory != null &&  urlCount >= pHistory->GetVisitedCount())
446                 {
447                         isEligibleForMostVisitedSites = true;
448                 }
449                 else
450                 {
451                         isEligibleForMostVisitedSites = false;
452                 }
453         }
454
455         if (pMostVisitedSites->GetCount() < 9 || (isAlreadyExist == false && isEligibleForMostVisitedSites == true && history.GetThumbnail() != null))
456         {
457                 AppLog("HistoryPresentationModel::SaveHistory coming here");
458
459                 thumbnailPath = GenerateFileName();
460                 AppLog("generating file path and url %ls, %ls",thumbnailPath.GetPointer(),historyUrl.GetPointer());
461                 Image* pImage = new(std::nothrow) Image();
462                 pImage->Construct();
463                 result r = pImage->EncodeToFile(*history.GetThumbnail(), IMG_FORMAT_JPG, thumbnailPath, true);
464                 delete pImage;
465                 if(IsFailed(r))
466                 {
467                         AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));
468                         return r;
469                 }
470         }
471         delete pMostVisitedSites;
472         pMostVisitedSites = NULL ;
473
474         if (history.GetHistoryId() == "")
475         {
476                 AppLog("HistoryPresentationModel::saveHistory GetHistoryId is blank");
477
478                 columnNames.Append(L"ADDRESS, ");
479                 columnNames.Append(L"TITLE, ");
480                 columnNames.Append(L"VISITDATE, ");
481                 columnNames.Append(L"FAVICON, ");
482                 columnNames.Append(L"FAVICON_W, ");
483                 columnNames.Append(L"FAVICON_H");
484
485                 query.Append(L"INSERT INTO ");
486                 query.Append(historyTable);
487                 query.Append("(");
488                 query.Append(columnNames);
489                 query.Append(") ");
490                 query.Append("VALUES");
491                 query.Append(" (?,?,?,?,?,?)");
492         }
493         else
494         {
495                 query.Append(L"UPDATE ");
496                 query.Append(historyTable);
497                 query.Append(" SET ADDRESS = ");
498                 query.Append("'");
499                 query.Append(historyUrl);
500                 query.Append("'");
501                 query.Append(", TITLE = ");
502                 query.Append("'");
503                 query.Append(historyTitle);
504                 query.Append("'");
505                 query.Append(", VISITED_TIME = ");
506                 query.Append("DateTime(");
507                 query.Append("'");
508                 query.Append(GetStringFromDate(date));
509                 query.Append("')");
510                 query.Append("'");
511                 query.Append(" WHERE ID = ");
512                 query.Append(history.GetHistoryId());
513         }
514
515         AppLog("HistoryPresentationModel::SaveHistory query is %S",query.GetPointer());
516
517         r = PresentationModelBase::ExecuteHistoryQuery(query, resultCount, history);
518         if (r == E_SUCCESS)
519         {
520                 AppLog("HistoryPresentationModel::SaveHistory result is success");
521         }
522         else
523         {
524                 AppLog("HistoryPresentationModel::SaveHistory result is failure");
525         }
526         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveHistory query failed  %s",GetErrorMessage(r));
527
528         r = PresentationModelBase::CommitDb();
529
530         r = GetLastInsertedId(historyTable, rowId);
531         if (IsFailed(r))
532         {
533                 AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));
534                 return r;
535         }
536         if (rowId < 0)
537                 return E_FAILURE;
538
539         historyId.Append(rowId);
540         if (historyId.GetLength() < 0)
541         {
542                 return E_FAILURE;
543         }
544         //Set the generated ID to History
545         history.SetHistoryId(historyId);
546
547         if(__pDataList)
548         {
549                 delete __pDataList;
550                 __pDataList = null;
551         }
552
553         CATCH: return r;
554
555 }
556
557 result
558 HistoryPresentationModel::DeleteMultipleHistory(ArrayList* pHistoryIdList)
559 {
560         result r = E_FAILURE;
561         String query;
562         String historyTable = HISTORY_DATA_TABLE;
563         int resultCount = -1;
564
565         if (pHistoryIdList == null)
566         {
567                 return E_FAILURE;
568         }
569
570         query.Append(L"DELETE FROM ");
571         query.Append(historyTable);
572         query.Append(" WHERE");
573         for(int index = 0; index <= pHistoryIdList->GetCount()-2; index++)
574         {
575                 query.Append(" ID = ");
576                 String* hisId = static_cast<String*>(pHistoryIdList->GetAt(index));
577                 query.Append(*hisId);
578                 query.Append(" OR ");
579         }
580         query.Append(" ID = ");
581         String* hisId = static_cast<String*>(pHistoryIdList->GetAt(pHistoryIdList->GetCount()-1));
582         if(hisId == null)
583         {
584                 return E_FAILURE;
585         }
586         query.Append(*hisId);
587
588         r = PresentationModelBase::ExecuteQuery(query, resultCount);
589         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
590
591         r = PresentationModelBase::CommitDb();
592
593         if(__pDataList)
594         {
595                 delete __pDataList;
596                 __pDataList = null;
597         }
598
599         CATCH: return r;
600 }
601
602 result
603 HistoryPresentationModel::DeleteHistory(const int historyId)
604 {
605         result r = E_FAILURE;
606         String query;
607         String historyTable = HISTORY_DATA_TABLE;
608         int resultCount = -1;
609
610         if (historyId < 1)
611         {
612                 return E_INVALID_ARG;
613         }
614
615         query.Append(L"DELETE FROM ");
616         query.Append(historyTable);
617         query.Append(" WHERE ID = ");
618         query.Append(historyId);
619
620         r = PresentationModelBase::ExecuteQuery(query, resultCount);
621         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
622
623         r = PresentationModelBase::CommitDb();
624
625         if(__pDataList)
626         {
627                 delete __pDataList;
628                 __pDataList = null;
629         }
630
631         CATCH: return r;
632 }
633
634 result
635 HistoryPresentationModel::DeleteHistory(String& historyUrl)
636 {
637         result r = E_FAILURE;
638         String query;
639         String historyTable = HISTORY_DATA_TABLE;
640         int resultCount = -1;
641
642         query.Append(L"DELETE FROM ");
643         query.Append(historyTable);
644         query.Append(" WHERE ADDRESS = '");
645         query.Append(historyUrl);
646         query.Append("'");
647
648         r = PresentationModelBase::ExecuteQuery(query, resultCount);
649         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
650
651         r = PresentationModelBase::CommitDb();
652
653         if(__pDataList)
654         {
655                 delete __pDataList;
656                 __pDataList = null;
657         }
658
659         CATCH: return r;
660 }
661
662 result
663 HistoryPresentationModel::ClearHistory(void)
664 {
665         result r = E_FAILURE;
666         String query;
667         String historyTable = HISTORY_DATA_TABLE;
668         int resultCount = -1;
669
670         query.Append(L"DELETE FROM ");
671         query.Append(historyTable);
672
673         r = PresentationModelBase::ExecuteQuery(query, resultCount);
674         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::ClearHistory GetLastInsertedId failed %s",GetErrorMessage(r));
675
676         r = PresentationModelBase::CommitDb();
677
678     CATCH: return r;
679 }
680
681 result
682 HistoryPresentationModel::GetHistoryCount(int& historyCount)
683 {
684         int count = -1;
685         int intVal = -1;
686         String query;
687         String historyTable(HISTORY_DATA_TABLE);
688         result r = E_FAILURE;
689         bool nextRowPresent = false;
690
691         query.Append(L"SELECT COUNT(ID) FROM ");
692         query.Append(historyTable);
693
694         r = PresentationModelBase::ExecuteQuery(query, count);
695         AppLog("the count is %d", count);
696         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
697
698         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
699         if (IsFailed(r) != null)
700                 return r;
701         if (nextRowPresent == true)
702         {
703                 r = PresentationModelBase::GetColumn(0, intVal);
704                 if (IsFailed(r) != null)
705                 {
706                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
707                         return r;
708                 }
709         }
710         historyCount = intVal;
711
712     CATCH: return r;
713 }
714
715 result
716 HistoryPresentationModel::GetHistoryCountWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime, int& historyCount)
717 {
718         int count = 0;
719         result r = E_SUCCESS;
720
721         if(__pDataList != null)
722         {
723                 delete __pDataList;
724                 __pDataList = null;
725         }
726
727         //if(__pDataList == null)
728         //{
729                 __pDataList = new(std::nothrow) ArrayList();
730                 __pDataList->Construct();
731
732                 r = GetHistoryCount(count);
733                 if(IsFailed(r))
734                 {
735                         return r;
736                 }
737                 if(count > 0)
738                 {
739                         r = GetHistory(0, count, *__pDataList);
740                         if (IsFailed(r))
741                         {
742                                 delete __pDataList;
743                                 __pDataList = null;
744                                 return r;
745                         }
746                 }
747         //}
748         //else
749         //{
750                 count = __pDataList->GetCount();
751         //}
752
753         for (int counter = 0 ; counter < count; counter++)
754         {
755                 History *pHistory = dynamic_cast<History*>(__pDataList->GetAt(counter));
756
757                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
758                 {
759                         historyCount++;
760                 }
761         }
762         return r;
763 }
764
765 result
766 HistoryPresentationModel::GetHistory(const int startIndex,const int limit, ArrayList& pHistoryList)
767 {
768
769         int count = -1;
770         String query;
771         String historyTable(HISTORY_DATA_TABLE);
772         result r = E_FAILURE;
773
774         query.Append(L"SELECT * from ");
775         query.Append(historyTable);
776         query.Append(" ORDER BY VISITDATE DESC");
777
778
779         //Append LIMIT
780         if (limit > 0)
781         {
782                 query.Append(" LIMIT ");
783                 query.Append(limit);
784
785                 //Append OFFESET
786                 if (startIndex >= 0)
787                 {
788                         query.Append(" OFFSET ");
789                         query.Append(startIndex);
790                 }
791         }
792
793         r = PresentationModelBase::ExecuteQuery(query, count);
794         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistory query failed %s",GetErrorMessage(r));
795
796         r = CreateHistoryList(count, pHistoryList);
797         if (IsFailed(r))
798         {
799                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
800         }
801         CATCH: return r;
802 }
803
804 result
805 HistoryPresentationModel::CreateHistoryList(int historyCount, ArrayList& historyList, int maxCount)
806 {
807
808         History* pHistory = null;
809         int intVal = -1;
810         String stringVal;
811         DateTime dateVal;
812         result r = E_FAILURE;
813         String historyId;
814         String faviconId;
815         String bookmarkId;
816         int visitedCount;
817         DateTime visitedTime;
818         String iconPath;
819         String thumbnailPath;
820         bool nextRowPresent = false;
821         Image image;
822         Bitmap* pThumnailBitmap = null;
823         ByteBuffer *pFavIconBuffer;
824
825         image.Construct();
826         if (historyCount < 1)
827         {
828                 return E_SUCCESS;
829         }
830
831         for (int Count = 0; (maxCount == -1 && Count < historyCount ) || (maxCount != -1 &&  Count < maxCount && Count < historyCount); Count++)
832         {
833                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
834                 if (IsFailed(r))
835                 {
836                         return E_SUCCESS;
837                 }
838                 if (nextRowPresent == true)
839                 {
840                         pHistory = new(std::nothrow) History;
841                         if (pHistory)
842                         {
843                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
844                                 {
845
846                                         switch (columnCount)
847                                         {
848                                         case HISTORY_ID:
849                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
850                                                 if (!IsFailed(r))
851                                                 {
852                                                         if (intVal > 0)
853                                                         {
854                                                                 historyId.Clear();
855                                                                 historyId.Append(intVal);
856                                                                 if (historyId.GetLength() > 0)
857                                                                 {
858                                                                         pHistory->SetHistoryId(historyId);
859                                                                 }
860                                                         }
861
862                                                 }
863                                                 break;
864                                         case HISTORY_URL:
865                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
866                                                 if (!IsFailed(r))
867                                                 {
868                                                         pHistory->SetHistoryUrl(stringVal);
869                                                 }
870                                                 break;
871                                         case HISTORY_TITLE:
872                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
873                                                 if (!IsFailed(r))
874                                                 {
875                                                         pHistory->SetHistoryTitle(stringVal);
876                                                 }
877                                                 break;
878
879                                         case HISTORY_VISITED_TIME:
880                                                 r = PresentationModelBase::GetColumn(columnCount, dateVal);
881                                                 if (!IsFailed(r))
882                                                 {
883                                                         visitedTime = dateVal;
884                                                         pHistory->SetVisitedTime(visitedTime);
885                                                 }
886                                                 break;
887
888                                         case HISTORY_FAVICON:
889                                         {
890                                                 pFavIconBuffer = new ByteBuffer();
891                                                 r = PresentationModelBase::GetColumn(columnCount, *pFavIconBuffer);
892                                                 if (!IsFailed(r) && pFavIconBuffer->GetCapacity() > 0)
893                                                 {
894                                                         pHistory->SetFavIconBuffer(*pFavIconBuffer);
895                                                 }
896                                         }
897                                         break;
898                                         case HISTORY_FAVICON_LENGTH:
899                                         {
900
901                                         }
902                                         break;
903                                         case HISTORY_FAVICON_W:
904                                         {
905                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
906                                                 if (!IsFailed(r))
907                                                 {
908                                                         if (intVal > 0)
909                                                         {
910                                                                 pHistory->SetFavIconWidth(intVal);
911                                                         }
912                                                 }
913                                         }
914                                         break;
915                                         case HISTORY_FAVICON_H:
916                                         {
917                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
918                                                 if (!IsFailed(r))
919                                                 {
920                                                         if (intVal > 0)
921                                                         {
922                                                                 pHistory->SetFavIconHeight(intVal);
923                                                         }
924                                                 }
925                                         }
926                                         break;
927
928                                         case HISTORY_VISITED_COUNT:
929                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
930                                                 if (!IsFailed(r))
931                                                 {
932                                                         if (intVal > 0)
933                                                         {
934                                                                 pHistory->SetVisitedCount(intVal);
935
936                                                         }
937                                                 }
938                                                 break;
939
940                                         default:
941                                                 break;
942                                         }
943
944                                 }
945                                 r = historyList.Add(*pHistory);
946
947                                 if (IsFailed(r))
948                                 {
949                                         delete pHistory;
950                                         return r;
951                                 }
952                         }
953                 }
954         }
955         return E_SUCCESS;
956 }
957
958 result
959 HistoryPresentationModel::GetSearchHistory(const int startIndex,const int limit, ArrayList& pHistoryList, String& text)
960 {
961         DateTime endTime;
962         SystemTime::GetCurrentTime(endTime);
963
964         text.Replace(L"'", L"''");
965
966         int count = -1;
967         String query;
968         String historyTable(HISTORY_DATA_TABLE);
969         result r = E_FAILURE;
970
971         query.Append(L"SELECT ");
972         query.Append(historyTable);
973         query.Append(L".* ");
974         query.Append(L"FROM ");
975         query.Append(historyTable);
976         query.Append(L" WHERE (");
977         query.Append(historyTable);
978         query.Append(L".TITLE LIKE '%");
979         query.Append(text);
980         query.Append("%' ESCAPE '/')");
981         query.Append(" OR ");
982         query.Append(L"(");
983         query.Append(historyTable);
984         query.Append(L".ADDRESS LIKE '%");
985         query.Append(text);
986         query.Append("%' ESCAPE '/')");
987         query.Append(" ORDER BY VISITDATE DESC");
988
989
990         r = HistoryPresentationModel::ExecuteQuery(query, count);
991 //      TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));
992
993         ArrayList pAllSearchList;
994
995         r = CreateHistoryList(count, pAllSearchList);
996
997         int itemsFoundIndex = -1;
998         for (int counter = 0 ; counter < count; counter++)
999         {
1000                 History *pHistory = dynamic_cast<History*>(pAllSearchList.GetAt(counter));
1001                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
1002                 {
1003                         itemsFoundIndex++;
1004                         if(itemsFoundIndex >= startIndex && itemsFoundIndex < (limit + startIndex))
1005                         {
1006                                 pHistoryList.Add(*pHistory);
1007                         }
1008                         else if(itemsFoundIndex > (limit + startIndex))
1009                         {
1010                                 break;
1011                         }
1012                 }
1013         }
1014
1015
1016         if (IsFailed(r))
1017         {
1018                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
1019         }
1020         CATCH: return r;
1021 }
1022
1023 result
1024 HistoryPresentationModel::GetSearchHistoryCount(int& historyCount, String& text)
1025 {
1026         DateTime endTime;
1027         SystemTime::GetCurrentTime(endTime);
1028         text.Replace(L"'", L"''");
1029
1030         int count = -1;
1031         String query;
1032         String historyTable(HISTORY_DATA_TABLE);
1033         result r = E_FAILURE;
1034
1035         query.Append(L"SELECT ");
1036         query.Append(historyTable);
1037         query.Append(L".* ");
1038         query.Append(L"FROM ");
1039         query.Append(historyTable);
1040         query.Append(L" WHERE (");
1041         query.Append(historyTable);
1042         query.Append(L".TITLE LIKE '%");
1043         query.Append(text);
1044         query.Append("%'ESCAPE '/')");
1045         query.Append(" OR ");
1046         query.Append(L"(");
1047         query.Append(historyTable);
1048         query.Append(L".ADDRESS LIKE '%");
1049         query.Append(text);
1050         query.Append("%' ESCAPE '/')");
1051         query.Append(" ORDER BY VISITDATE DESC");
1052
1053         r = HistoryPresentationModel::ExecuteQuery(query, count);
1054 //      TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));
1055
1056         ArrayList pAllSearchList;
1057
1058         r = CreateHistoryList(count, pAllSearchList);
1059
1060         historyCount = 0;
1061         for (int counter = 0 ; counter < count; counter++)
1062         {
1063                 History *pHistory = dynamic_cast<History*>(pAllSearchList.GetAt(counter));
1064                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
1065                 {
1066                         historyCount++;
1067                 }
1068         }
1069
1070
1071         if (IsFailed(r))
1072         {
1073                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
1074         }
1075         CATCH: return r;
1076 }
1077
1078 result
1079 HistoryPresentationModel::GetHistoryWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime,const int startIndex,const int limit, ArrayList& pHistoryList)
1080 {
1081         int count = 0;
1082         result r = E_SUCCESS;
1083         if(__pDataList == null)
1084         {
1085                 __pDataList = new(std::nothrow) ArrayList();
1086                 __pDataList->Construct();
1087
1088                 r = GetHistoryCount(count);
1089                 if(IsFailed(r))
1090                 {
1091                         return r;
1092                 }
1093                 if(count > 0)
1094                 {
1095                         r = GetHistory(0, count, *__pDataList);
1096                         if(IsFailed(r))
1097                         {
1098                                 delete __pDataList;
1099                                 __pDataList = null;
1100                                 return r;
1101                         }
1102                 }
1103         }
1104         else
1105         {
1106                 count = __pDataList->GetCount();
1107         }
1108         int itemsFoundIndex = -1;
1109         for (int counter = 0 ; counter < count; counter++)
1110         {
1111                 History *pHistory = dynamic_cast<History*>(__pDataList->GetAt(counter));
1112                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
1113                 {
1114                         itemsFoundIndex++;
1115                         if(itemsFoundIndex >= startIndex && itemsFoundIndex < (limit + startIndex))
1116                         {
1117                                 pHistoryList.Add(*pHistory);
1118                         }
1119                         else if(itemsFoundIndex > (limit + startIndex))
1120                         {
1121                                 break;
1122                         }
1123                 }
1124         }
1125         return r;
1126 }
1127
1128 result
1129 HistoryPresentationModel::GetMostVisitedSites(ArrayList& pHistoryList)
1130 {
1131
1132         int count = -1;
1133         String query;
1134         String historyTable(HISTORY_DATA_TABLE);
1135         result r = E_FAILURE;
1136
1137
1138         //query = L"SELECT *, COUNT(URL) AS NOS FROM HistoryData GROUP BY URL ORDER BY NOS DESC";
1139         query = L"SELECT History.*, COUNT(History.ADDRESS) AS NOS FROM History WHERE History.ADDRESS != '' GROUP BY History.ADDRESS ORDER BY NOS DESC";
1140
1141         r = HistoryPresentationModel::ExecuteQuery(query, count);
1142         AppLog("the count is %d",count);
1143         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));
1144
1145         r = CreateHistoryList(count, pHistoryList, 9);
1146         if (IsFailed(r))
1147         {
1148                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
1149         }
1150
1151         CATCH: return r;
1152 }
1153
1154 result
1155 HistoryPresentationModel::GetUrlCount(const String& Url, int& historyCount)
1156 {
1157         int count = -1;
1158         int intVal = -1;
1159         String query;
1160         String historyTable(HISTORY_DATA_TABLE);
1161         result r = E_FAILURE;
1162         bool nextRowPresent = false;
1163
1164         query.Append(L"SELECT COUNT(ADDRESS) FROM ");
1165         query.Append(historyTable);
1166         query.Append(L" WHERE ADDRESS = ");
1167         query.Append("'");
1168         query.Append(Url);
1169         query.Append("'");
1170
1171         r = PresentationModelBase::ExecuteQuery(query, count);
1172         AppLog("the count is %d", count);
1173         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
1174
1175         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
1176         if (IsFailed(r))
1177                 return r;
1178         if (nextRowPresent == true)
1179         {
1180                 r = PresentationModelBase::GetColumn(0, intVal);
1181                 if (IsFailed(r))
1182                 {
1183                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
1184                         return r;
1185                 }
1186         }
1187         historyCount = intVal;
1188
1189     CATCH: return r;
1190 }
1191
1192 String
1193 HistoryPresentationModel::GenerateFileName(void)
1194 {
1195         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
1196         if ( pAppRegistry == NULL )
1197         {
1198                 return NULL ;
1199         }
1200         String keyCount("ThumbnailCount");
1201         String fileName = UiApp::GetInstance()->GetAppRootPath() + "/data/Thumbnail";
1202         result r = E_SUCCESS;
1203         int keyValue = 1;
1204
1205         fileName.Append(L"/thumbnails");
1206         r = pAppRegistry->Get(keyCount, keyValue);
1207
1208         if (r == E_KEY_NOT_FOUND)
1209         {
1210                 keyValue = 1;
1211                 pAppRegistry->Add(keyCount, keyValue);
1212         }
1213         else
1214         {
1215                 keyValue++;
1216                 pAppRegistry->Set(keyCount, keyValue);
1217         }
1218
1219         fileName.Append(keyValue);
1220         fileName.Append(".jpg");
1221         pAppRegistry->Save();
1222
1223         return fileName;
1224 }
1225
1226 String
1227 HistoryPresentationModel::GetStringFromDate(DateTime& date)
1228 {
1229         String dateFormat = L"";
1230         dateFormat.Append(date.GetYear());
1231         dateFormat.Append('-');
1232         if(date.GetMonth() < 10)
1233         {
1234                 dateFormat.Append('0');
1235         }
1236         dateFormat.Append(date.GetMonth());
1237         dateFormat.Append('-');
1238         if(date.GetDay() < 10)
1239         {
1240                 dateFormat.Append('0');
1241         }
1242         dateFormat.Append(date.GetDay());
1243         dateFormat.Append(' ');
1244         if(date.GetHour() < 10)
1245         {
1246                 dateFormat.Append('0');
1247         }
1248         dateFormat.Append(date.GetHour());
1249         dateFormat.Append(':');
1250         if(date.GetMinute() < 10)
1251         {
1252                 dateFormat.Append('0');
1253         }
1254         dateFormat.Append(date.GetMinute());
1255         dateFormat.Append(':');
1256         if(date.GetSecond() < 10)
1257         {
1258                 dateFormat.Append('0');
1259         }
1260         dateFormat.Append(date.GetSecond());
1261         return dateFormat;
1262 }