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