Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / LocalContent / LocalContent / project / src / MainForm.cpp
1 //
2 // Tizen C++ SDK
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://www.tizenopensource.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 #include <FUi.h>
19 #include "MainForm.h"
20 #include "DetailForm.h"
21
22 using namespace Osp::App;
23 using namespace Osp::Base;
24 using namespace Osp::Base::Collection;
25 using namespace Osp::Ui;
26 using namespace Osp::Ui::Controls;
27 using namespace Osp::Content;
28 using namespace Osp::Graphics;
29
30 static const int MAX_LISTVIEW_ITEM_SIZE = 70;
31 static const int MAX_CONTENTTYPE_COUNT = 4;
32
33 MainForm::MainForm(void)
34         : __contentType(CONTENT_TYPE_OTHER)
35         , __pEFContentName(null)
36         , __pLblContentList(null)
37         , __pLblContentName(null)
38         , __pLblContentType(null)
39         , __pSearchResultForm(null)
40         , __pListView(null)
41         , __pSearchButton(null)
42 {
43 }
44
45 MainForm::~MainForm(void)
46 {
47
48 }
49
50 bool
51 MainForm::Initialize()
52 {
53         // Construct an XML form
54         Form::Construct(L"IDF_MAINFORM");
55
56         return true;
57 }
58
59 result
60 MainForm::OnInitializing(void)
61 {
62         result r = E_SUCCESS;
63
64         Footer* pFooter = GetFooter();
65         if(pFooter != null)
66         {
67                 r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT );
68                 TryReturn(E_SUCCESS == r, r, "pFooter->SetStyle failed by %s.", GetErrorMessage(r));
69                 r = pFooter->SetBackButton();
70                 TryReturn(E_SUCCESS == r, r, "pFooter->SetBackButton failed by %s.", GetErrorMessage(r));
71                 SetFormBackEventListener(this);
72         }
73
74         // ContentType Label
75         __pLblContentType = static_cast<Label *>(GetControl(L"IDC_LABEL1"));
76         TryReturn(__pLblContentType != null, r = E_FAILURE, "GetControl() failed by %s.", GetErrorMessage(r));
77
78         // ContentName Label
79         __pLblContentName = static_cast<Label *>(GetControl(L"IDC_LABEL2"));
80         TryReturn(__pLblContentName != null, r = E_FAILURE, "GetControl() failed by %s.", GetErrorMessage(r));
81
82         // ContentName EditField
83         __pEFContentName = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD1"));
84         TryReturn(__pEFContentName != null, r = E_FAILURE, "GetControl() failed by %s.", GetErrorMessage(r));
85
86         __pSearchButton = static_cast<Button *>(GetControl(L"IDC_BUTTON_SEARCH"));
87         TryReturn(__pSearchButton != null, r = E_FAILURE, "GetControl() failed by %s.", GetErrorMessage(r));
88
89         __pSearchButton->AddActionEventListener(*this);
90
91         r = init();
92
93         return r;
94 }
95
96 result
97 MainForm::OnTerminating(void)
98 {
99         result r = E_SUCCESS;
100
101         if (__pContentSearchImpl != null)
102         {
103                 delete __pContentSearchImpl;
104         }
105
106         return r;
107 }
108
109 result
110 MainForm::init(void)
111 {
112         result r = E_SUCCESS;
113         Rectangle rect;
114         Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
115
116         // initialize ListView for ContentType
117         __pListView = new (std::nothrow) ListView();
118         TryReturn(__pListView != null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] __pListView is null.");
119
120         rect = this->GetClientAreaBounds();
121         r = __pListView->Construct(Rectangle(rect.x, __pLblContentType->GetBounds().y + MAX_LISTVIEW_ITEM_SIZE, rect.width, __pLblContentName->GetBounds().y - __pLblContentType->GetBounds().y - MAX_LISTVIEW_ITEM_SIZE - 20), true, false);
122         TryCatch(E_SUCCESS == r, , "ListView::Construct() failed by %s.", GetErrorMessage(r));
123
124         r = __pListView->SetItemProvider(*this);
125         TryCatch(E_SUCCESS == r, , "ListView::SetItemProvider() failed by %s.", GetErrorMessage(r));
126
127         __pListView->AddListViewItemEventListener(*this);
128         r = this->AddControl(*__pListView);
129         TryCatch(E_SUCCESS == r, , "AddControl() failed by %s.", GetErrorMessage(r));
130
131         __pContentSearchImpl = new (std::nothrow) ContentSearchSampleImpl();
132         TryCatch(__pContentSearchImpl != null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] __pContentSearchImpl is null.");
133
134         // Create a SearchResultForm
135         __pSearchResultForm = new (std::nothrow) SearchResultForm;
136         TryCatch(__pSearchResultForm != null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] __pSearchResultForm is null.");
137
138         __pSearchResultForm->Initialize();
139
140         // Add the form to the frame
141         pFrame->AddControl(*__pSearchResultForm);
142
143         // Create a SearchResultForm
144         __pDetailForm = new (std::nothrow) DetailForm;
145         TryCatch(__pDetailForm != null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] __pDetailForm is null.");
146
147         __pDetailForm->Initialize();
148
149         // Add the form to the frame
150         pFrame->AddControl(*__pDetailForm);
151
152         return r;
153
154 CATCH:
155         delete __pListView;
156         __pListView = null;
157
158         delete __pContentSearchImpl;
159         __pContentSearchImpl = null;
160
161         delete __pSearchResultForm;
162         __pSearchResultForm = null;
163
164         delete __pDetailForm;
165         __pDetailForm = null;
166
167         return r;
168 }
169
170 void
171 MainForm::SetDefaultContentType(void)
172 {
173         __pListView->Draw();
174         CheckContentTypeList((int)CONTENT_TYPE_OTHER);
175 }
176
177 result
178 MainForm::SearchContent(void)
179 {
180         result r = E_SUCCESS;
181         String queryString;
182         String popStr;
183         Frame *pFrame = null;
184
185         // get query string
186         queryString = __pEFContentName->GetText();
187         // execute the query
188         r = __pContentSearchImpl->SearchContent(__contentType, queryString);
189         TryCatch(E_SUCCESS == r, , "Content Search failed by %s.", GetErrorMessage(r));
190
191         // display search result list
192         pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
193         pFrame->SetCurrentForm(*__pSearchResultForm);
194         __pSearchResultForm->DisplayResultList(__pContentSearchImpl);
195
196 CATCH:
197         // show error message
198         ShowErrorMsg(r);
199         return r;
200 }
201
202 result
203 MainForm::ReSearchContent(void)
204 {
205         result r = E_SUCCESS;
206         String popStr;
207         Frame *pFrame = null;
208
209         // execute the query
210         r = __pContentSearchImpl->RefreshSearchContent();
211         TryCatch(E_SUCCESS == r, , "Content Search failed by %s.", GetErrorMessage(r));
212
213         // display search result list
214         pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
215         pFrame->SetCurrentForm(*__pSearchResultForm);
216         __pSearchResultForm->DisplayResultList(__pContentSearchImpl);
217
218 CATCH:
219         // show error message
220         ShowErrorMsg(r);
221         return r;
222 }
223
224 void
225 MainForm::ShowErrorMsg(result r)
226 {
227         if (IsFailed(r))
228         {
229                 String popStr;
230                 MessageBox msgBox;
231                 int modalResult = 0;
232                 r = popStr.Format(128, L"Error [%s]", GetErrorMessage(r));
233                 TryReturn(r == E_SUCCESS, , "popStr.Format failed");
234                 r = msgBox.Construct("Message", popStr, MSGBOX_STYLE_NONE, 2000);
235                 if (!IsFailed(r))
236                 {
237                         r = msgBox.ShowAndWait(modalResult);
238                         TryReturn(r == E_SUCCESS, , "msgBox.ShowAndWait failed");
239                 }
240         }
241 }
242
243 void
244 MainForm::UncheckContentType(int index)
245 {
246         for (int i = CONTENT_TYPE_OTHER; i <= CONTENT_TYPE_ALL; i++)
247         {
248                 if (i == index)
249                 {
250                         continue;
251                 }
252
253                 if (__pListView->IsItemChecked(i))
254                 {
255                         __pListView->SetItemChecked(i, false);
256                         __pListView->RefreshList(i, LIST_REFRESH_TYPE_ITEM_MODIFY);
257                 }
258         }
259 }
260
261 void
262 MainForm::CheckContentTypeList(int prevCheck)
263 {
264         bool checkFlag = false;
265
266         for (int i = CONTENT_TYPE_OTHER; i <= CONTENT_TYPE_ALL; i++)
267         {
268                 if (__pListView->IsItemChecked(i))
269                 {
270                         checkFlag = true;
271                 }
272         }
273
274         if (checkFlag == false)
275         {
276                 __pListView->SetItemChecked(prevCheck, true);
277                 __pListView->RefreshList(prevCheck, LIST_REFRESH_TYPE_ITEM_MODIFY);
278         }
279 }
280
281 void
282 MainForm::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
283 {
284         result r = E_SUCCESS;
285
286         switch (requestId)
287         {
288         case ID_REFRESH_SEARCH:
289         {
290                 r = ReSearchContent();
291                 break;
292         }
293         case ID_DISPLAY_CONTENTINFO:
294         {
295                 Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
296                 Integer* pIndex = null;
297
298                 if (pArgs != null)
299                 {
300                         pIndex = (Integer*)(pArgs->GetAt(0));
301                         pFrame->SetCurrentForm(*__pDetailForm);
302                         __pDetailForm->LoadContentInfo(__pContentSearchImpl->GetSearchResultItem(pIndex->ToInt()));
303                         pFrame->Draw();
304                 }
305                 break;
306         }
307         }
308
309         if (pArgs)
310         {
311                 pArgs->RemoveAll(true);
312                 delete pArgs;
313         }
314 }
315
316
317 void
318 MainForm::OnActionPerformed(const Control& source, int actionId)
319 {
320         if (source.GetName() == L"IDC_BUTTON_SEARCH")
321         {
322                 SearchContent();
323         }
324 }
325
326 void
327 MainForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
328 {
329         if (status == LIST_ITEM_STATUS_CHECKED)
330         {
331                 __contentType = (ContentType)index;
332                 UncheckContentType(index);
333         }
334         else if (status == LIST_ITEM_STATUS_UNCHECKED)
335         {
336                 CheckContentTypeList(__contentType);
337         }
338 }
339
340 void
341 MainForm::OnListViewItemSwept(ListView &listView, int index, SweepDirection direction)
342 {
343 }
344
345 void
346 MainForm::OnListViewContextItemStateChanged(ListView &listView, int index, int elementId, ListContextItemStatus state)
347 {
348 }
349
350 int
351 MainForm::GetItemCount(void)
352 {
353     return MAX_CONTENTTYPE_COUNT;
354 }
355
356 ListItemBase*
357 MainForm::CreateItem(int index, int itemWidth)
358 {
359         String contentTypeName;
360         ListAnnexStyle style = LIST_ANNEX_STYLE_MARK;
361         CustomItem* pItem = new (std::nothrow) CustomItem();
362         TryReturn(pItem != null, pItem, "pItem is null.");
363
364         pItem->Construct(Dimension(itemWidth, MAX_LISTVIEW_ITEM_SIZE), style);
365
366         switch (index)
367         {
368         case CONTENT_TYPE_IMAGE:
369                 contentTypeName = L"Image";
370                 break;
371         case CONTENT_TYPE_AUDIO:
372                 contentTypeName = L"Audio";
373                 break;
374         case CONTENT_TYPE_VIDEO:
375                 contentTypeName = L"Video";
376                 break;
377         case CONTENT_TYPE_OTHER:
378                 contentTypeName = L"Other";
379                 break;
380         default:
381                 break;
382         }
383
384     pItem->AddElement(Rectangle(20, 0, itemWidth - 20, MAX_LISTVIEW_ITEM_SIZE), 100, contentTypeName);
385
386     return pItem;
387 }
388
389 bool
390 MainForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
391 {
392         delete pItem;
393         pItem = null;
394
395         return true;
396 }
397
398 void
399 MainForm::OnFormBackRequested(Form& source)
400 {
401         Application::GetInstance()->Terminate();
402 }