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