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