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