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