Fixed Nabi Issues, Changes for tizen-service implementation and DB Changes
[apps/osp/Internet.git] / src / IntHistoryPresentationModel.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (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
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 i;
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 (i = 0; i < pMostVisitedSites->GetCount(); i++)
169         {
170                 History* pHistory = static_cast< History* >(pMostVisitedSites->GetAt(i));
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         if (history.GetHistoryId() == "")
207         {
208                 AppLog("HistoryPresentationModel::saveHistory GetHistoryId is blank");
209                 /*
210                 columnNames.Append(L"TITLE, ");
211                 columnNames.Append(L"URL, ");
212                 columnNames.Append(L"VISITED_TIME, ");
213                 columnNames.Append(L"ICON_PATH, ");
214                 columnNames.Append(L"FAVICON_ID, ");
215                 columnNames.Append(L"THUMBNAIL_PATH");
216
217                 query.Append(L"INSERT INTO ");
218                 query.Append(historyTable);
219                 query.Append("(");
220                 query.Append(columnNames);
221                 query.Append(") ");
222                 query.Append("VALUES(");
223                 query.Append("'");
224                 query.Append(historyTitle);
225                 query.Append("'");
226                 query.Append(", ");
227                 query.Append("'");
228                 query.Append(historyUrl);
229                 query.Append("', ");
230                 query.Append("DateTime(");
231                 query.Append("'");
232                 query.Append(GetStringFromDate(date));
233                 query.Append("'), ");
234                 query.Append("'");
235                 query.Append(history.GetHistoryIconPath());
236                 query.Append("'");
237                 query.Append(", ");
238                 query.Append("'");
239                 query.Append(history.GetFaviconId());
240                 query.Append("'");
241                 query.Append(", ");
242                 query.Append("'");
243                 query.Append(thumbnailPath);
244                 query.Append("'");
245                 query.Append(")");
246          */
247                 columnNames.Append(L"ADDRESS, ");
248                 columnNames.Append(L"TITLE, ");
249                 //columnNames.Append(L"COUNTER, ");
250                 columnNames.Append(L"VISITDATE");
251         //      columnNames.Append(L"FAVICON, ");
252         //      columnNames.Append(L"FAVICON_LENGTH, ");
253         //      columnNames.Append(L"FAVICON_W, ");
254         //      columnNames.Append(L"FAVICON_H, ");
255         //      columnNames.Append(L"SNAPSHOT, ");
256         //      columnNames.Append(L"SNAPSHOT_STRIDE, ");
257         //      columnNames.Append(L"SNAPSHOT_W, ");
258         //      columnNames.Append(L"SNAPSHOT_H");
259         //      columnNames.Append(L"ICON_PATH, ");
260         //      columnNames.Append(L"FAVICON_ID, ");
261         //      columnNames.Append(L"THUMBNAIL_PATH");
262
263                 query.Append(L"INSERT INTO ");
264                 query.Append(historyTable);
265                 query.Append("(");
266                 query.Append(columnNames);
267                 query.Append(") ");
268                 query.Append("VALUES(");
269                 query.Append("'");
270                 query.Append(historyUrl);
271                 query.Append("'");
272                 query.Append(", ");
273                 query.Append("'");
274                 query.Append(historyTitle);
275                 query.Append("', ");
276                 query.Append("DateTime(");
277                 query.Append("'");
278                 query.Append(GetStringFromDate(date));
279                 query.Append("'))");
280
281                 //query.Append("'), ");
282                 //query.Append("'");
283                 //query.Append(history.GetHistoryIconPath());
284                 //query.Append("'");
285                 //query.Append(", ");
286                 //query.Append("'");
287                 //query.Append(history.GetFaviconId());
288                 //query.Append("'");
289                 //query.Append(", ");
290                 //query.Append("'");
291                 //query.Append(thumbnailPath);
292                 //query.Append("'");
293                 //query.Append(")");
294         }
295         else
296         {
297                 /*query.Append(L"UPDATE ");
298                 query.Append(historyTable);
299                 query.Append(" SET TITLE = ");
300                 query.Append("'");
301                 query.Append(historyTitle);
302                 query.Append("'");
303                 query.Append(", URL = ");
304                 query.Append("'");
305                 query.Append(historyUrl);
306                 query.Append("'");
307                 query.Append(", VISITED_TIME = ");
308                 query.Append("DateTime(");
309                 query.Append("'");
310                 query.Append(GetStringFromDate(date));
311                 query.Append("')");
312                 query.Append(", ICON_PATH = ");
313                 query.Append("'");
314                 query.Append(history.GetHistoryIconPath());
315                 query.Append("'");
316                 query.Append(", ");
317                 query.Append("'");
318                 query.Append(history.GetFaviconId());
319                 query.Append("'");
320                 query.Append(", ");
321                 query.Append("'");
322                 query.Append(thumbnailPath);
323                 query.Append("'");
324                 query.Append(" WHERE ID = ");
325                 query.Append(history.GetHistoryId());*/
326                 query.Append(L"UPDATE ");
327                 query.Append(historyTable);
328                 query.Append(" SET ADDRESS = ");
329                 query.Append("'");
330                 query.Append(historyUrl);
331                 query.Append("'");
332                 query.Append(", TITLE = ");
333                 query.Append("'");
334                 query.Append(historyTitle);
335                 query.Append("'");
336                 query.Append(", VISITED_TIME = ");
337                 query.Append("DateTime(");
338                 query.Append("'");
339                 query.Append(GetStringFromDate(date));
340                 query.Append("')");
341                 /*query.Append(", ICON_PATH = ");
342                 query.Append("'");
343                 query.Append(history.GetHistoryIconPath());
344                 query.Append("'");
345                 query.Append(", ");
346                 query.Append("'");
347                 query.Append(history.GetFaviconId());
348                 query.Append("'");
349                 query.Append(", ");
350                 query.Append("'");
351                 query.Append(thumbnailPath);*/
352                 query.Append("'");
353                 query.Append(" WHERE ID = ");
354                 query.Append(history.GetHistoryId());
355
356         }
357
358         AppLog("HistoryPresentationModel::SaveHistory query is %S",query.GetPointer());
359
360         r = PresentationModelBase::ExecuteQuery(query, resultCount);
361         if (r == E_SUCCESS)
362         {
363                 AppLog("HistoryPresentationModel::SaveHistory result is success");
364         }
365         else
366         {
367                 AppLog("HistoryPresentationModel::SaveHistory result is failure");
368         }
369         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveHistory query failed  %s",GetErrorMessage(r));
370
371         r = PresentationModelBase::CommitDb();
372
373         r = GetLastInsertedId(historyTable, rowId);
374         if (IsFailed(r))
375         {
376                 AppLogDebug("PresentationModelBase::saveHistory -(%s)\n", GetErrorMessage(r));
377                 return r;
378         }
379         if (rowId < 0)
380                 return E_FAILURE;
381
382         historyId.Append(rowId);
383         if (historyId.GetLength() < 0)
384         {
385                 return E_FAILURE;
386         }
387         //Set the generated ID to History
388         history.SetHistoryId(historyId);
389
390         CATCH: return r;
391 }
392
393 result
394 HistoryPresentationModel::DeleteMultipleHistory(ArrayList* pHistoryIdList)
395 {
396         result r = E_FAILURE;
397         String query;
398         String historyTable = HISTORY_DATA_TABLE;
399         int resultCount = -1;
400
401         if (pHistoryIdList == null)
402         {
403                 return E_FAILURE;
404         }
405
406         query.Append(L"DELETE FROM ");
407         query.Append(historyTable);
408         query.Append(" WHERE");
409         for(int index = 0; index <= pHistoryIdList->GetCount()-2; index++)
410         {
411                 query.Append(" ID = ");
412                 String* hisId = static_cast<String*>(pHistoryIdList->GetAt(index));
413                 query.Append(*hisId);
414                 query.Append(" OR ");
415         }
416         query.Append(" ID = ");
417         String* hisId = static_cast<String*>(pHistoryIdList->GetAt(pHistoryIdList->GetCount()-1));
418         if(hisId == null)
419         {
420                 return E_FAILURE;
421         }
422         query.Append(*hisId);
423
424         r = PresentationModelBase::ExecuteQuery(query, resultCount);
425         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
426
427         r = PresentationModelBase::CommitDb();
428
429         CATCH: return r;
430 }
431
432 result
433 HistoryPresentationModel::DeleteHistory(const int historyId)
434 {
435         result r = E_FAILURE;
436         String query;
437         String historyTable = HISTORY_DATA_TABLE;
438         int resultCount = -1;
439
440         if (historyId < 1)
441         {
442                 return E_INVALID_ARG;
443         }
444
445         query.Append(L"DELETE FROM ");
446         query.Append(historyTable);
447         query.Append(" WHERE ID = ");
448         query.Append(historyId);
449
450         r = PresentationModelBase::ExecuteQuery(query, resultCount);
451         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
452
453         r = PresentationModelBase::CommitDb();
454
455         CATCH: return r;
456 }
457
458 result
459 HistoryPresentationModel::DeleteHistory(String& historyUrl)
460 {
461         result r = E_FAILURE;
462         String query;
463         String historyTable = HISTORY_DATA_TABLE;
464         int resultCount = -1;
465
466         query.Append(L"DELETE FROM ");
467         query.Append(historyTable);
468         query.Append(" WHERE ADDRESS = '");
469         query.Append(historyUrl);
470         query.Append("'");
471
472         r = PresentationModelBase::ExecuteQuery(query, resultCount);
473         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteHistory GetLastInsertedId failed %s",GetErrorMessage(r));
474
475         r = PresentationModelBase::CommitDb();
476
477         CATCH: return r;
478 }
479
480 result
481 HistoryPresentationModel::ClearHistory(void)
482 {
483         result r = E_FAILURE;
484         String query;
485         String historyTable = HISTORY_DATA_TABLE;
486         int resultCount = -1;
487
488         query.Append(L"DELETE FROM ");
489         query.Append(historyTable);
490
491         r = PresentationModelBase::ExecuteQuery(query, resultCount);
492         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::ClearHistory GetLastInsertedId failed %s",GetErrorMessage(r));
493
494         r = PresentationModelBase::CommitDb();
495
496     CATCH: return r;
497 }
498
499 result
500 HistoryPresentationModel::GetHistoryCount(int& historyCount)
501 {
502         int count = -1;
503         int intVal = -1;
504         String query;
505         String historyTable(HISTORY_DATA_TABLE);
506         result r = E_FAILURE;
507         bool nextRowPresent = false;
508
509         query.Append(L"SELECT COUNT(ID) FROM ");
510         query.Append(historyTable);
511
512         r = PresentationModelBase::ExecuteQuery(query, count);
513         AppLog("the count is %d", count);
514         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
515
516         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
517         if (IsFailed(r) != null)
518                 return r;
519         if (nextRowPresent == true)
520         {
521                 r = PresentationModelBase::GetColumn(0, intVal);
522                 if (IsFailed(r) != null)
523                 {
524                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
525                         return r;
526                 }
527         }
528         historyCount = intVal;
529
530     CATCH: return r;
531 }
532
533 result
534 HistoryPresentationModel::GetHistoryCountWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime, int& historyCount)
535 {
536
537         int count = -1;
538         int intVal = -1;
539         String query;
540         String historyTable(HISTORY_DATA_TABLE);
541         result r = E_FAILURE;
542         bool nextRowPresent = false;
543
544         query.Append(L"SELECT COUNT(ID) FROM ");
545         query.Append(historyTable);
546         query.Append(" WHERE VISITDATE >= ");
547         query.Append("DateTime(");
548         query.Append("'");
549         query.Append(GetStringFromDate(startTime));
550         query.Append("')");
551         query.Append(" AND VISITDATE <= ");
552         query.Append("DateTime(");
553         query.Append("'");
554         query.Append(GetStringFromDate(endTime));
555         query.Append("')");
556
557         r = PresentationModelBase::ExecuteQuery(query, count);
558         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCountWithTimeRange query failed %s",GetErrorMessage(r));
559
560         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
561         if (IsFailed(r))
562                 return r;
563         if (nextRowPresent == true)
564         {
565                 r = PresentationModelBase::GetColumn(0, intVal);
566                 if (IsFailed(r))
567                 {
568                         AppLogDebug("Error:PresentationModelBase::DbGetColumn:%s", GetErrorMessage(r));
569                         return r;
570                 }
571         }
572         historyCount = intVal;
573
574         CATCH: return r;
575 }
576
577 result
578 HistoryPresentationModel::GetHistory(const int startIndex,const int limit, ArrayList& pHistoryList)
579 {
580
581         int count = -1;
582         String query;
583         String historyTable(HISTORY_DATA_TABLE);
584         result r = E_FAILURE;
585
586         query.Append(L"SELECT");
587         query.Append(historyTable);
588         query.Append(L".*, ");
589         query.Append(BOOKMARK_URL_TABLE);
590         query.Append(L".ID ");
591         query.Append(L"FROM ");
592         query.Append(historyTable);
593         query.Append(L", ");
594         query.Append(BOOKMARK_URL_TABLE);
595         query.Append(L" WHERE ");
596         query.Append(historyTable);
597         query.Append(L".ADDRESS ");
598         query.Append(L"IN");
599         query.Append(L"(");
600         query.Append(L"SELECT");
601         query.Append(historyTable);
602         query.Append(L".ADDRESS ");
603         query.Append(L"FROM ");
604         query.Append(BOOKMARK_URL_TABLE);
605         query.Append(L" LEFT JOIN ");
606         query.Append(historyTable);
607         query.Append(L"ON");
608         query.Append(BOOKMARK_URL_TABLE);
609         query.Append(L".ADDRESS = ");
610         query.Append(historyTable);
611         query.Append(L".ADDRESS ");
612         query.Append(" ORDER BY");
613         query.Append(historyTable);
614         query.Append(L".VISITDATE DESC");
615         query.Append(")");
616         query.Append(" GROUP BY ");
617         query.Append(historyTable);
618         query.Append(L".ADDRESS ");
619         query.Append(" ORDER BY");
620         query.Append(L"COUNT(");
621         query.Append(historyTable);
622         query.Append(L".ADDRESS) DESC ");
623
624
625         //Append LIMIT
626         if (limit > 0)
627         {
628                 query.Append(" LIMIT ");
629                 query.Append(limit);
630
631                 //Append OFFESET
632                 if (startIndex >= 0)
633                 {
634                         query.Append(" OFFSET ");
635                         query.Append(startIndex);
636                 }
637         }
638
639         r = PresentationModelBase::ExecuteQuery(query, count);
640         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistory query failed %s",GetErrorMessage(r));
641
642         r = CreateHistoryList(count, pHistoryList);
643         if (IsFailed(r))
644         {
645                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
646         }
647         CATCH: return r;
648 }
649
650 result
651 HistoryPresentationModel::CreateHistoryList(int historyCount, ArrayList& historyList, int maxCount)
652 {
653
654         History* pHistory = null;
655         int intVal = -1;
656         String stringVal;
657         DateTime dateVal;
658         result r = E_FAILURE;
659         String historyId;
660         String faviconId;
661         String bookmarkId;
662         int visitedCount;
663         DateTime visitedTime;
664         String iconPath;
665         String thumbnailPath;
666         bool nextRowPresent = false;
667         Image image;
668         Bitmap* pThumnailBitmap = null;
669
670         image.Construct();
671         if (historyCount < 1)
672         {
673                 return E_SUCCESS;
674         }
675
676         for (int Count = 0; (maxCount == -1 && Count < historyCount ) || (maxCount != -1 &&  Count < maxCount && Count < historyCount); Count++)
677         {
678                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
679                 if (IsFailed(r))
680                 {
681                         return E_SUCCESS;
682                 }
683                 if (nextRowPresent == true)
684                 {
685                         pHistory = new(std::nothrow) History;
686                         if (pHistory)
687                         {
688                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
689                                 {
690
691                                         switch (columnCount)
692                                         {
693                                         case HISTORY_ID:
694                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
695                                                 if (!IsFailed(r))
696                                                 {
697                                                         if (intVal > 0)
698                                                         {
699                                                                 historyId.Clear();
700                                                                 historyId.Append(intVal);
701                                                                 if (historyId.GetLength() > 0)
702                                                                 {
703                                                                         pHistory->SetHistoryId(historyId);
704                                                                 }
705                                                         }
706
707                                                 }
708                                                 break;
709                                         case HISTORY_URL:
710                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
711                                                 if (!IsFailed(r))
712                                                 {
713                                                         pHistory->SetHistoryUrl(stringVal);
714                                                 }
715                                                 break;
716                                         case HISTORY_TITLE:
717                                                 r = PresentationModelBase::GetColumn(columnCount, stringVal);
718                                                 if (!IsFailed(r))
719                                                 {
720                                                         pHistory->SetHistoryTitle(stringVal);
721                                                 }
722                                                 break;
723                                         case HISTORY_VISITED_TIME:
724                                                 r = PresentationModelBase::GetColumn(columnCount, dateVal);
725                                                 if (!IsFailed(r))
726                                                 {
727                                                         visitedTime = dateVal;
728                                                         pHistory->SetVisitedTime(visitedTime);
729                                                 }
730                                                 break;
731
732                                                 /*case HISTORY_ICON_PATH:
733                                                         r = PresentationModelBase::GetColumn(columnCount, stringVal);
734                                                         if (!IsFailed(r))
735                                                         {
736                                                                 iconPath = stringVal;
737                                                                 pHistory->SetIconPath(iconPath);
738                                                         }
739                                                         break;
740
741                                                 case HISTORY_FAVICONID:
742
743                                                         r = PresentationModelBase::GetColumn(columnCount, intVal);
744                                                         if (!IsFailed(r))
745                                                         {
746                                                                 if (intVal >= 0)
747                                                                 {
748                                                                         faviconId.Clear();
749                                                                         faviconId.Append(intVal);
750                                                                         if (faviconId.GetLength() > 0)
751                                                                         {
752                                                                                 pHistory->SetFaviconId(faviconId);
753                                                                         }
754                                                                 }
755                                                         }
756                                                         break;
757                                                 case HISTORY_THUMBNAIL:
758                                                         r = PresentationModelBase::GetColumn(columnCount, stringVal);
759                                                         if (!IsFailed(r))
760                                                         {
761
762                                                                 thumbnailPath = stringVal;
763                                                                 pThumnailBitmap = image.DecodeN(thumbnailPath, BITMAP_PIXEL_FORMAT_RGB565);
764                                                                 if (pThumnailBitmap != null)
765                                                                 {
766                                                                         pHistory->SetThumbnail(pThumnailBitmap);
767                                                                         pHistory->SetThumbnailPath(thumbnailPath);
768                                                                 }
769                                                         }
770                                                         break;*/
771                                 /*      case HISTORY_BOOKMARK_ID:
772                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
773                                                 if (!IsFailed(r))
774                                                 {
775                                                         if (intVal > 0)
776                                                         {
777                                                                 bookmarkId.Clear();
778                                                                 bookmarkId.Append(intVal);
779                                                                 if (bookmarkId.GetLength() > 0)
780                                                                 {
781                                                                         pHistory->SetBookmarkId(bookmarkId);
782                                                                 }
783                                                         }
784
785                                                 }
786                                                 break;
787                                                 */
788                                         case HISTORY_VISITED_COUNT:
789                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
790                                                 if (!IsFailed(r))
791                                                 {
792                                                         if (intVal > 0)
793                                                         {
794                                                                 pHistory->SetVisitedCount(intVal);
795
796                                                         }
797
798                                                 }
799                                                 break;
800
801                                         default:
802                                                 break;
803                                         }
804
805                                 }
806                                 r = historyList.Add(*pHistory);
807
808                                 if (IsFailed(r))
809                                 {
810                                         delete pHistory;
811                                         return r;
812                                 }
813                         }
814                 }
815         }
816         return E_SUCCESS;
817 }
818
819 result
820 HistoryPresentationModel::GetSearchHistory(const int startIndex,const int limit, ArrayList& pHistoryList, String& text)
821 {
822         text.Replace(L"'", L"''");
823
824         int count = -1;
825         String query;
826         String historyTable(HISTORY_DATA_TABLE);
827         result r = E_FAILURE;
828
829         query.Append(L"SELECT ");
830         query.Append(historyTable);
831         query.Append(L".* ");
832         query.Append(L"FROM ");
833         query.Append(historyTable);
834         query.Append(L" WHERE (");
835         query.Append(historyTable);
836         query.Append(L".TITLE LIKE '%");
837         query.Append(text);
838         query.Append("%')");
839         query.Append(" OR ");
840         query.Append(L"(");
841         query.Append(historyTable);
842         query.Append(L".ADDRESS LIKE '%");
843         query.Append(text);
844         query.Append("%')");
845         query.Append(" ORDER BY VISITDATE DESC");
846
847         //Append LIMIT
848         if (limit > 0)
849         {
850                 query.Append(" LIMIT ");
851                 query.Append(limit);
852
853                 //Append OFFESET
854                 if (startIndex >= 0)
855                 {
856                         query.Append(" OFFSET ");
857                         query.Append(startIndex);
858                 }
859         }
860
861         r = HistoryPresentationModel::ExecuteQuery(query, count);
862         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistory query failed %s",GetErrorMessage(r));
863
864         r = CreateHistoryList(count, pHistoryList);
865         if (IsFailed(r))
866         {
867                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
868         }
869         CATCH: return r;
870 }
871
872 result
873 HistoryPresentationModel::GetSearchHistoryCount(int& historyCount, String& text)
874 {
875         int count = -1;
876         int intVal = -1;
877         String query;
878         String historyTable(HISTORY_DATA_TABLE);
879         result r = E_FAILURE;
880         bool nextRowPresent = false;
881
882         text.Replace(L"'", L"''");
883         query.Append(L"SELECT COUNT(ID) FROM ");
884         query.Append(historyTable);
885         query.Append(L" WHERE (TITLE LIKE '%");
886         query.Append(text);
887         query.Append("%')");
888         query.Append(" OR ");
889         query.Append(L"(ADDRESS LIKE '%");
890         query.Append(text);
891         query.Append("%')");
892
893         r = HistoryPresentationModel::ExecuteQuery(query, count);
894         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchHistoryCount query failed %s",GetErrorMessage(r));
895
896         r = HistoryPresentationModel::DbIsNextRowPresent(nextRowPresent);
897         if (IsFailed(r) != null)
898                 return r;
899         if (nextRowPresent == true)
900         {
901                 r = HistoryPresentationModel::GetColumn(0, intVal);
902                 if (IsFailed(r) != null)
903                 {
904                         AppLogDebug("Error:HistoryPresentationModel::DbGetColumn:%s", GetErrorMessage(r));
905                         return r;
906                 }
907         }
908         historyCount = intVal;
909
910         CATCH: return r;
911 }
912
913 result
914 HistoryPresentationModel::GetHistoryWithTimeRange(Tizen::Base::DateTime& startTime, Tizen::Base::DateTime& endTime,const int startIndex,const int limit, ArrayList& pHistoryList)
915 {
916         int count = -1;
917         String query;
918         String historyTable(HISTORY_DATA_TABLE);
919         result r = E_FAILURE;
920
921         query.Append(L"SELECT ");
922         //query.Append(historyTable);
923         query.Append(L"* ");
924         query.Append(L"FROM ");
925         query.Append(historyTable);
926         query.Append(" WHERE ");
927         //query.Append(historyTable);
928         query.Append(L"VISITDATE >= ");
929         query.Append("DateTime(");
930         query.Append("'");
931         query.Append(GetStringFromDate(startTime));
932         query.Append("')");
933         query.Append(" AND ");
934         //query.Append(historyTable);
935         query.Append(L"VISITDATE <= ");
936         query.Append("DateTime(");
937         query.Append("'");
938         query.Append(GetStringFromDate(endTime));
939         query.Append("')");
940         query.Append(" ORDER BY VISITDATE DESC");
941
942         //Append LIMIT
943         if (limit > 0)
944         {
945                 query.Append(" LIMIT ");
946                 query.Append(limit);
947
948                 //Append OFFESET
949                 if (startIndex >= 0)
950                 {
951                         query.Append(" OFFSET ");
952                         query.Append(startIndex);
953                 }
954         }
955
956         r = HistoryPresentationModel::ExecuteQuery(query, count);
957         AppLog("the count is %d",count);
958         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));
959
960         r = CreateHistoryList(count, pHistoryList);
961         if (IsFailed(r))
962         {
963                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
964         }
965
966         CATCH: return r;
967 }
968
969 result
970 HistoryPresentationModel::GetMostVisitedSites(ArrayList& pHistoryList)
971 {
972
973         int count = -1;
974         String query;
975         String historyTable(HISTORY_DATA_TABLE);
976         result r = E_FAILURE;
977
978
979         //query = L"SELECT *, COUNT(URL) AS NOS FROM HistoryData GROUP BY URL ORDER BY NOS DESC";
980         query = L"SELECT History.*, COUNT(History.ADDRESS) AS NOS FROM History WHERE History.ADDRESS != '' GROUP BY History.ADDRESS ORDER BY NOS DESC";
981
982         r = HistoryPresentationModel::ExecuteQuery(query, count);
983         AppLog("the count is %d",count);
984         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryWithTimeRange query failed %s",GetErrorMessage(r));
985
986         r = CreateHistoryList(count, pHistoryList, 9);
987         if (IsFailed(r))
988         {
989                 AppLogDebug("Error:CreateNoteList:failed:%s", GetErrorMessage(r));
990         }
991
992         CATCH: return r;
993 }
994
995 result
996 HistoryPresentationModel::GetUrlCount(const String& Url, int& historyCount)
997 {
998         int count = -1;
999         int intVal = -1;
1000         String query;
1001         String historyTable(HISTORY_DATA_TABLE);
1002         result r = E_FAILURE;
1003         bool nextRowPresent = false;
1004
1005         query.Append(L"SELECT COUNT(ADDRESS) FROM ");
1006         query.Append(historyTable);
1007         query.Append(L" WHERE ADDRESS = ");
1008         query.Append("'");
1009         query.Append(Url);
1010         query.Append("'");
1011
1012         r = PresentationModelBase::ExecuteQuery(query, count);
1013         AppLog("the count is %d", count);
1014         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetHistoryCount query failed %s",GetErrorMessage(r));
1015
1016         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
1017         if (IsFailed(r))
1018                 return r;
1019         if (nextRowPresent == true)
1020         {
1021                 r = PresentationModelBase::GetColumn(0, intVal);
1022                 if (IsFailed(r))
1023                 {
1024                         AppLogDebug("Error:PresentationModelBase::GetColumn:%s", GetErrorMessage(r));
1025                         return r;
1026                 }
1027         }
1028         historyCount = intVal;
1029
1030     CATCH: return r;
1031 }
1032
1033 String
1034 HistoryPresentationModel::GenerateFileName(void)
1035 {
1036         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
1037         if ( pAppRegistry == NULL )
1038         {
1039                 return NULL ;
1040         }
1041         String keyCount("ThumbnailCount");
1042         String fileName = UiApp::GetInstance()->GetAppRootPath() + "/data/Thumbnail/thumbnails";
1043         result r = E_SUCCESS;
1044         int keyValue = 1;
1045         r = pAppRegistry->Get(keyCount, keyValue);
1046
1047         if (r == E_KEY_NOT_FOUND)
1048         {
1049                 keyValue = 1;
1050                 pAppRegistry->Add(keyCount, keyValue);
1051         }
1052         else
1053         {
1054                 keyValue++;
1055                 pAppRegistry->Set(keyCount, keyValue);
1056         }
1057
1058         fileName.Append(keyValue);
1059         fileName.Append(".jpg");
1060         pAppRegistry->Save();
1061
1062         return fileName;
1063 }
1064
1065 String
1066 HistoryPresentationModel::GetStringFromDate(DateTime& date)
1067 {
1068         String dateFormat = L"";
1069         dateFormat.Append(date.GetYear());
1070         dateFormat.Append('-');
1071         if(date.GetMonth() < 10)
1072         {
1073                 dateFormat.Append('0');
1074         }
1075         dateFormat.Append(date.GetMonth());
1076         dateFormat.Append('-');
1077         if(date.GetDay() < 10)
1078         {
1079                 dateFormat.Append('0');
1080         }
1081         dateFormat.Append(date.GetDay());
1082         dateFormat.Append(' ');
1083         if(date.GetHour() < 10)
1084         {
1085                 dateFormat.Append('0');
1086         }
1087         dateFormat.Append(date.GetHour());
1088         dateFormat.Append(':');
1089         if(date.GetMinute() < 10)
1090         {
1091                 dateFormat.Append('0');
1092         }
1093         dateFormat.Append(date.GetMinute());
1094         dateFormat.Append(':');
1095         if(date.GetSecond() < 10)
1096         {
1097                 dateFormat.Append('0');
1098         }
1099         dateFormat.Append(date.GetSecond());
1100         return dateFormat;
1101 }