Fixed Nabi issues N_SE-39262, N_SE-39349, N_SE-38797
[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                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
747                 {
748                         historyCount++;
749                 }
750         }
751         return r;
752 }
753
754 result
755 HistoryPresentationModel::GetHistory(const int startIndex,const int limit, ArrayList& pHistoryList)
756 {
757
758         int count = -1;
759         String query;
760         String historyTable(HISTORY_DATA_TABLE);
761         result r = E_FAILURE;
762
763         query.Append(L"SELECT * from ");
764         query.Append(historyTable);
765         query.Append(" ORDER BY VISITDATE DESC");
766
767
768         //Append LIMIT
769         if (limit > 0)
770         {
771                 query.Append(" LIMIT ");
772                 query.Append(limit);
773
774                 //Append OFFESET
775                 if (startIndex >= 0)
776                 {
777                         query.Append(" OFFSET ");
778                         query.Append(startIndex);
779                 }
780         }
781
782         r = PresentationModelBase::ExecuteQuery(query, count);
783         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistory query failed %s",GetErrorMessage(r));
784
785         r = CreateHistoryList(count, pHistoryList);
786         if (IsFailed(r))
787         {
788                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
789         }
790         CATCH: return r;
791 }
792
793 result
794 HistoryPresentationModel::CreateHistoryList(int historyCount, ArrayList& historyList, int maxCount)
795 {
796
797         History* pHistory = null;
798         int intVal = -1;
799         String stringVal;
800         DateTime dateVal;
801         result r = E_FAILURE;
802         String historyId;
803         String faviconId;
804         String bookmarkId;
805         int visitedCount;
806         DateTime visitedTime;
807         String iconPath;
808         String thumbnailPath;
809         bool nextRowPresent = false;
810         Image image;
811         Bitmap* pThumnailBitmap = null;
812         ByteBuffer *pFavIconBuffer;
813
814         image.Construct();
815         if (historyCount < 1)
816         {
817                 return E_SUCCESS;
818         }
819
820         for (int Count = 0; (maxCount == -1 && Count < historyCount ) || (maxCount != -1 &&  Count < maxCount && Count < historyCount); Count++)
821         {
822                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
823                 if (IsFailed(r))
824                 {
825                         return E_SUCCESS;
826                 }
827                 if (nextRowPresent == true)
828                 {
829                         pHistory = new(std::nothrow) History;
830                         if (pHistory)
831                         {
832                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
833                                 {
834
835                                         switch (columnCount)
836                                         {
837                                         case HISTORY_ID:
838                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
839                                                 if (!IsFailed(r))
840                                                 {
841                                                         if (intVal > 0)
842                                                         {
843                                                                 historyId.Clear();
844                                                                 historyId.Append(intVal);
845                                                                 if (historyId.GetLength() > 0)
846                                                                 {
847                                                                         pHistory->SetHistoryId(historyId);
848                                                                 }
849                                                         }
850
851                                                 }
852                                                 break;
853                                         case HISTORY_URL:
854                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
855                                                 if (!IsFailed(r))
856                                                 {
857                                                         pHistory->SetHistoryUrl(stringVal);
858                                                 }
859                                                 break;
860                                         case HISTORY_TITLE:
861                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
862                                                 if (!IsFailed(r))
863                                                 {
864                                                         pHistory->SetHistoryTitle(stringVal);
865                                                 }
866                                                 break;
867
868                                         case HISTORY_VISITED_TIME:
869                                                 r = PresentationModelBase::GetColumn(columnCount, dateVal);
870                                                 if (!IsFailed(r))
871                                                 {
872                                                         visitedTime = dateVal;
873                                                         pHistory->SetVisitedTime(visitedTime);
874                                                 }
875                                                 break;
876
877                                         case HISTORY_FAVICON:
878                                         {
879                                                 pFavIconBuffer = new ByteBuffer();
880                                                 r = PresentationModelBase::GetColumn(columnCount, *pFavIconBuffer);
881                                                 if (!IsFailed(r))
882                                                 {
883                                                         pHistory->SetFavIconBuffer(*pFavIconBuffer);
884                                                 }
885                                         }
886                                         break;
887                                         case HISTORY_FAVICON_LENGTH:
888                                         {
889
890                                         }
891                                         break;
892                                         case HISTORY_FAVICON_W:
893                                         {
894                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
895                                                 if (!IsFailed(r))
896                                                 {
897                                                         if (intVal > 0)
898                                                         {
899                                                                 pHistory->SetFavIconWidth(intVal);
900                                                         }
901                                                 }
902                                         }
903                                         break;
904                                         case HISTORY_FAVICON_H:
905                                         {
906                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
907                                                 if (!IsFailed(r))
908                                                 {
909                                                         if (intVal > 0)
910                                                         {
911                                                                 pHistory->SetFavIconHeight(intVal);
912                                                         }
913                                                 }
914                                         }
915                                         break;
916
917                                         case HISTORY_VISITED_COUNT:
918                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
919                                                 if (!IsFailed(r))
920                                                 {
921                                                         if (intVal > 0)
922                                                         {
923                                                                 pHistory->SetVisitedCount(intVal);
924
925                                                         }
926                                                 }
927                                                 break;
928
929                                         default:
930                                                 break;
931                                         }
932
933                                 }
934                                 r = historyList.Add(*pHistory);
935
936                                 if (IsFailed(r))
937                                 {
938                                         delete pHistory;
939                                         return r;
940                                 }
941                         }
942                 }
943         }
944         return E_SUCCESS;
945 }
946
947 result
948 HistoryPresentationModel::GetSearchHistory(const int startIndex,const int limit, ArrayList& pHistoryList, String& text)
949 {
950         text.Replace(L"'", L"''");
951
952         int count = -1;
953         String query;
954         String historyTable(HISTORY_DATA_TABLE);
955         result r = E_FAILURE;
956
957         query.Append(L"SELECT ");
958         query.Append(historyTable);
959         query.Append(L".* ");
960         query.Append(L"FROM ");
961         query.Append(historyTable);
962         query.Append(L" WHERE (");
963         query.Append(historyTable);
964         query.Append(L".TITLE LIKE '%");
965         query.Append(text);
966         query.Append("%')");
967         query.Append(" OR ");
968         query.Append(L"(");
969         query.Append(historyTable);
970         query.Append(L".ADDRESS LIKE '%");
971         query.Append(text);
972         query.Append("%')");
973         query.Append(" ORDER BY VISITDATE DESC");
974
975         //Append LIMIT
976         if (limit > 0)
977         {
978                 query.Append(" LIMIT ");
979                 query.Append(limit);
980
981                 //Append OFFESET
982                 if (startIndex >= 0)
983                 {
984                         query.Append(" OFFSET ");
985                         query.Append(startIndex);
986                 }
987         }
988
989         r = HistoryPresentationModel::ExecuteQuery(query, count);
990         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));
991
992         r = CreateHistoryList(count, pHistoryList);
993         if (IsFailed(r))
994         {
995                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
996         }
997         CATCH: return r;
998 }
999
1000 result
1001 HistoryPresentationModel::GetSearchHistoryCount(int& historyCount, String& text)
1002 {
1003         int count = -1;
1004         int intVal = -1;
1005         String query;
1006         String historyTable(HISTORY_DATA_TABLE);
1007         result r = E_FAILURE;
1008         bool nextRowPresent = false;
1009
1010         text.Replace(L"'", L"''");
1011         query.Append(L"SELECT COUNT(ID) FROM ");
1012         query.Append(historyTable);
1013         query.Append(L" WHERE (TITLE LIKE '%");
1014         query.Append(text);
1015         query.Append("%')");
1016         query.Append(" OR ");
1017         query.Append(L"(ADDRESS LIKE '%");
1018         query.Append(text);
1019         query.Append("%')");
1020
1021         r = HistoryPresentationModel::ExecuteQuery(query, count);
1022         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistoryCount query failed %s",GetErrorMessage(r));
1023
1024         r = HistoryPresentationModel::DbIsNextRowPresent(nextRowPresent);
1025         if (IsFailed(r) != null)
1026                 return r;
1027         if (nextRowPresent == true)
1028         {
1029                 r = HistoryPresentationModel::GetColumn(0, intVal);
1030                 if (IsFailed(r) != null)
1031                 {
1032                         AppLogDebug("Error:HistoryPresentationModel::DbGetColumn:%s", GetErrorMessage(r));
1033                         return r;
1034                 }
1035         }
1036         historyCount = intVal;
1037
1038         CATCH: return r;
1039 }
1040
1041 result
1042 HistoryPresentationModel::GetHistoryWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime,const int startIndex,const int limit, ArrayList& pHistoryList)
1043 {
1044         int count = 0;
1045         result r = E_SUCCESS;
1046         if(__pDataList == null)
1047         {
1048                 __pDataList = new(std::nothrow) ArrayList();
1049                 __pDataList->Construct();
1050
1051                 r = GetHistoryCount(count);
1052                 if(IsFailed(r))
1053                 {
1054                         return r;
1055                 }
1056                 if(count > 0)
1057                 {
1058                         r = GetHistory(0, count, *__pDataList);
1059                         if(IsFailed(r))
1060                         {
1061                                 delete __pDataList;
1062                                 __pDataList = null;
1063                                 return r;
1064                         }
1065                 }
1066         }
1067         else
1068         {
1069                 count = __pDataList->GetCount();
1070         }
1071         int itemsFoundIndex = -1;
1072         for (int counter = 0 ; counter < count; counter++)
1073         {
1074                 History *pHistory = dynamic_cast<History*>(__pDataList->GetAt(counter));
1075                 if(pHistory != null && pHistory->GetVisitedTime().CompareTo(startTime) > 0 && pHistory->GetVisitedTime().CompareTo(endTime) < 0)
1076                 {
1077                         itemsFoundIndex++;
1078                         if(itemsFoundIndex >= startIndex && itemsFoundIndex < (limit + startIndex))
1079                         {
1080                                 pHistoryList.Add(*pHistory);
1081                         }
1082                         else if(itemsFoundIndex > (limit + startIndex))
1083                         {
1084                                 break;
1085                         }
1086                 }
1087         }
1088         return r;
1089 }
1090
1091 result
1092 HistoryPresentationModel::GetMostVisitedSites(ArrayList& pHistoryList)
1093 {
1094
1095         int count = -1;
1096         String query;
1097         String historyTable(HISTORY_DATA_TABLE);
1098         result r = E_FAILURE;
1099
1100
1101         //query = L"SELECT *, COUNT(URL) AS NOS FROM HistoryData GROUP BY URL ORDER BY NOS DESC";
1102         query = L"SELECT History.*, COUNT(History.ADDRESS) AS NOS FROM History WHERE History.ADDRESS != '' GROUP BY History.ADDRESS ORDER BY NOS DESC";
1103
1104         r = HistoryPresentationModel::ExecuteQuery(query, count);
1105         AppLog("the count is %d",count);
1106         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));
1107
1108         r = CreateHistoryList(count, pHistoryList, 9);
1109         if (IsFailed(r))
1110         {
1111                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
1112         }
1113
1114         CATCH: return r;
1115 }
1116
1117 result
1118 HistoryPresentationModel::GetUrlCount(const String& Url, int& historyCount)
1119 {
1120         int count = -1;
1121         int intVal = -1;
1122         String query;
1123         String historyTable(HISTORY_DATA_TABLE);
1124         result r = E_FAILURE;
1125         bool nextRowPresent = false;
1126
1127         query.Append(L"SELECT COUNT(ADDRESS) FROM ");
1128         query.Append(historyTable);
1129         query.Append(L" WHERE ADDRESS = ");
1130         query.Append("'");
1131         query.Append(Url);
1132         query.Append("'");
1133
1134         r = PresentationModelBase::ExecuteQuery(query, count);
1135         AppLog("the count is %d", count);
1136         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
1137
1138         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
1139         if (IsFailed(r))
1140                 return r;
1141         if (nextRowPresent == true)
1142         {
1143                 r = PresentationModelBase::GetColumn(0, intVal);
1144                 if (IsFailed(r))
1145                 {
1146                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
1147                         return r;
1148                 }
1149         }
1150         historyCount = intVal;
1151
1152     CATCH: return r;
1153 }
1154
1155 String
1156 HistoryPresentationModel::GenerateFileName(void)
1157 {
1158         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
1159         if ( pAppRegistry == NULL )
1160         {
1161                 return NULL ;
1162         }
1163         String keyCount("ThumbnailCount");
1164         String fileName = UiApp::GetInstance()->GetAppRootPath() + "/data/Thumbnail/thumbnails";
1165         result r = E_SUCCESS;
1166         int keyValue = 1;
1167         r = pAppRegistry->Get(keyCount, keyValue);
1168
1169         if (r == E_KEY_NOT_FOUND)
1170         {
1171                 keyValue = 1;
1172                 pAppRegistry->Add(keyCount, keyValue);
1173         }
1174         else
1175         {
1176                 keyValue++;
1177                 pAppRegistry->Set(keyCount, keyValue);
1178         }
1179
1180         fileName.Append(keyValue);
1181         fileName.Append(".jpg");
1182         pAppRegistry->Save();
1183
1184         return fileName;
1185 }
1186
1187 String
1188 HistoryPresentationModel::GetStringFromDate(DateTime& date)
1189 {
1190         String dateFormat = L"";
1191         dateFormat.Append(date.GetYear());
1192         dateFormat.Append('-');
1193         if(date.GetMonth() < 10)
1194         {
1195                 dateFormat.Append('0');
1196         }
1197         dateFormat.Append(date.GetMonth());
1198         dateFormat.Append('-');
1199         if(date.GetDay() < 10)
1200         {
1201                 dateFormat.Append('0');
1202         }
1203         dateFormat.Append(date.GetDay());
1204         dateFormat.Append(' ');
1205         if(date.GetHour() < 10)
1206         {
1207                 dateFormat.Append('0');
1208         }
1209         dateFormat.Append(date.GetHour());
1210         dateFormat.Append(':');
1211         if(date.GetMinute() < 10)
1212         {
1213                 dateFormat.Append('0');
1214         }
1215         dateFormat.Append(date.GetMinute());
1216         dateFormat.Append(':');
1217         if(date.GetSecond() < 10)
1218         {
1219                 dateFormat.Append('0');
1220         }
1221         dateFormat.Append(date.GetSecond());
1222         return dateFormat;
1223 }