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