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