Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnSuggestionItemProvider.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file    PhnSuggestionItemProvider.cpp
19  * @brief   Suggestion list item provider class
20  */
21 #include <FApp.h>
22 #include <FSystem.h>
23 #include <FMedia.h>
24 #include <FSocial.h>
25 #include "PhnAppUtility.h"
26 #include "PhnCommonUtils.h"
27 #include "PhnDialContactInfo.h"
28 #include "PhnDialPresentationModel.h"
29 #include "PhnSuggestionItemProvider.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Media;
35 using namespace Tizen::System;
36 using namespace Tizen::Social;
37 using namespace Tizen::Base::Collection;
38
39 SuggestionItemProvider::SuggestionItemProvider(
40                 DialPresentationModel& pDialPresentationModel) :
41                 __dialPresentationModel(pDialPresentationModel)
42 {
43 }
44
45 SuggestionItemProvider::~SuggestionItemProvider(void)
46 {
47 }
48
49 ListItemBase*
50 SuggestionItemProvider::CreateItem(int index, int itemWidth)
51 {
52         CustomItem* pItem = null;
53         //Check if any suggestion exist at 'index+1'.
54         DialContactInfo* pDialInfo = __dialPresentationModel.GetSuggestionAtIndex(index+1);
55         if(pDialInfo == null)
56         {
57                 return pItem;
58         }
59         //Construct List view item
60         pItem = new (std::nothrow) CustomItem();
61         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
62         pItem->Construct(Dimension(itemWidth, 128), style);
63
64         //create enriched text - Display name with Keyword
65         String* keyWord = pDialInfo->GetSearchKey();
66         EnrichedText* pEnrichedName = ConstructEnrichedTextN(pDialInfo->GetDisplayName(), *keyWord , FONT_SUGGESTION_NAME, BUTTON_STATUS_NORMAL);
67         pItem->AddElement(Rectangle(16, 10, 445, 60), 202, *pEnrichedName);
68         delete pEnrichedName;
69         pEnrichedName = null;
70
71         //create text element - Phone number with Keyword
72         String phoneNumber(L"Mobile ");
73         phoneNumber.Append(pDialInfo->GetPhoneNumber());
74         EnrichedText* pEnrichedMobile = ConstructEnrichedTextN(phoneNumber, *keyWord, FONT_SUGGESTION_MOBILE, BUTTON_STATUS_NORMAL);
75         pItem->AddElement(Rectangle(16, 70, 445, 48), 204, *pEnrichedMobile);
76         delete pEnrichedMobile;
77         pEnrichedMobile =null;
78
79         //Fetch contact's thumbnail
80         Bitmap* pContactBitmap = pDialInfo->GetThumbnailN();
81         //show default TN
82         if(pContactBitmap == null)
83         {
84                 pContactBitmap = AppUtility::GetBitmapFromResourcesN(L"C01-1_icon_contacts.png");
85         }
86         pItem->AddElement(Rectangle(445, 16, 96, 96), 206, *pContactBitmap);
87         delete pContactBitmap;
88
89         return pItem;
90 }
91
92 EnrichedText*
93 SuggestionItemProvider::ConstructEnrichedTextN(String& text, String& keyword, int textFontStyle, int buttonStatus)
94 {
95         //get text font
96         Font* fontName = DialUtil::GetTextFontN(textFontStyle);
97         //get text color
98         Color* textColor = DialUtil::GetTextColorN(textFontStyle, buttonStatus);
99         //highlighted text Color
100         Color* highlightedTxtColor = DialUtil::GetTextColorN(FONT_HIGHLIGHT_SEARCH_KEYWORD,buttonStatus);
101
102         EnrichedText* pEnrichedName = new (std::nothrow) EnrichedText();
103         pEnrichedName->Construct(Dimension(400, 60));
104
105         //Text is divided in 3 parts - pre-text, highlighted keyword, post-text.
106         String preTxt(L"");
107         String highlightTxt(L"");
108         String postTxt(L"");
109         if(keyword.IsEmpty() == false)
110         {
111                 int searchIndex;
112                 result r = text.IndexOf(keyword,0,searchIndex);
113                 if(r == E_SUCCESS)
114                 {
115                         if(searchIndex > 0)
116                         {
117                                 text.SubString(0,searchIndex,preTxt);
118                         }
119                         text.SubString(searchIndex,keyword.GetLength(),highlightTxt);
120                         text.SubString((searchIndex+keyword.GetLength()),postTxt);
121                 }
122                 else
123                 {
124                         preTxt = text;
125                 }
126         }
127
128         TextElement* pTextName = null;
129         if(preTxt.IsEmpty() == false)
130         {
131                 //create text element
132                 pTextName = new (std::nothrow) TextElement();
133                 pTextName->Construct(preTxt);
134                 pTextName->SetFont(*fontName);
135                 pTextName->SetTextColor(*textColor);
136                 //ownership of 'pTextName' transferred to 'pEnrichedName'
137                 pEnrichedName->Add(*pTextName);
138         }
139
140         if(highlightTxt.IsEmpty() == false)
141         {
142                 //create text element
143                 pTextName = new (std::nothrow) TextElement();
144                 pTextName->Construct(highlightTxt);
145                 pTextName->SetFont(*fontName);
146                 // Highlighted search text
147                 pTextName->SetTextColor(*highlightedTxtColor);
148                 pEnrichedName->Add(*pTextName);
149         }
150
151         if(postTxt.IsEmpty() == false)
152         {
153                 //create text element
154                 pTextName = new (std::nothrow) TextElement();
155                 pTextName->Construct(postTxt);
156                 pTextName->SetFont(*fontName);
157                 pTextName->SetTextColor(*textColor);
158                 pEnrichedName->Add(*pTextName);
159         }
160
161         delete fontName;
162         delete textColor;
163         delete highlightedTxtColor;
164
165         return pEnrichedName;
166 }
167
168 bool
169 SuggestionItemProvider::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
170 {
171         if (pItem != null)
172         {
173                 delete pItem;
174                 pItem = null;
175         }
176         return true;
177 }
178
179 int
180 SuggestionItemProvider::GetItemCount(void)
181 {
182         return __dialPresentationModel.GetNumberOfSuggestions() - 1;
183 }