Fixed the messagebox display issue when user enters invalid recipient
[apps/native/preloaded/Email.git] / src / EmRecipientField.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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        EmRecipientField.cpp
19  * @brief       Provides the implementation for the custom control, which is used for viewing sender/recipient addresses.
20  */
21
22 #include "EmEmailApp.h"
23 #include "EmRecipientField.h"
24 #include "EmTypes.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Controls;
32
33 RecipientField::RecipientField(void)
34         : __mode(READ_WRITE_MODE)
35         , __isWindowDeactived(false)
36         , __showMessageBox(0)
37         , __width(W_RECIPIENT_AREA)
38         , __expandedHeight(H_RECIPIENT_AREA)
39         , __alreadyValidated(false)
40         , __hasFocus(false)
41         , __sendUserEvent(true)
42         , __pRecipientList(null)
43         , __pAddContactButton(null)
44         , __pCountButton(null)
45         , __pRecipientArea(null)
46         , __pTokenChangedEventListener(null)
47 {
48         // Empty Implementation
49 }
50
51 RecipientField::~RecipientField(void)
52 {
53         AppLogDebug("ENTER");
54
55         if (__pRecipientList != null)
56         {
57                 __pRecipientList->RemoveAll(true);
58                 delete __pRecipientList;
59         }
60
61         Frame* pFrame = EmailApp::GetInstance()->GetFrame(IDS_EMAIL_TEXT);
62         pFrame->RemoveWindowEventListener(*this);
63
64         AppLogDebug("EXIT");
65 }
66
67 result
68 RecipientField::Construct(const FloatRectangle& bounds, MODE mode)
69 {
70         AppLogDebug("Email::ENTER");
71         result r = E_SUCCESS;
72         Bitmap* pContactBitmap = null;
73         Bitmap* pContactCircleBitmap = null;
74         Bitmap* pContactCirclePressedBitmap = null;
75         AppResource* pAppResource = UiApp::GetInstance()->GetAppResource();
76
77         __mode = mode;
78
79         Color bgColor = Color(0,0,0,0);
80         RelativeLayout relLayout;
81         relLayout.Construct();
82
83         Panel::Construct(relLayout, bounds, GROUP_STYLE_NONE);
84
85         Frame* pFrame = EmailApp::GetInstance()->GetFrame(IDS_EMAIL_TEXT);
86         pFrame->AddWindowEventListener(*this);
87
88         if (pAppResource)
89         {
90                 pContactBitmap = pAppResource->GetBitmapN(IDB_ADDCONTACT, BITMAP_PIXEL_FORMAT_ARGB8888);
91                 pContactCircleBitmap = pAppResource->GetBitmapN(IDB_CIRCLE, BITMAP_PIXEL_FORMAT_ARGB8888);
92                 pContactCirclePressedBitmap = pAppResource->GetBitmapN(IDB_ADD_CONTACT_CIRCLE_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888);
93         }
94
95         __pRecipientArea = new (std::nothrow) ExpandableEditArea();
96
97         if (__mode == READ_WRITE_MODE)
98         {
99                 __pRecipientArea->Construct(FloatRectangle(X_COMMON_CUSTOM_ITEM, Y_RECIPIENT_AREA, bounds.width-(2*X_COMMON_CUSTOM_ITEM)-W_ADD_CONTACT_BUTTON-GENERIC_OFFSET_16, H_RECIPIENT_AREA), EXPANDABLE_EDIT_AREA_STYLE_TOKEN, EXPANDABLE_EDIT_AREA_TITLE_STYLE_INNER, 5);
100                 __pRecipientArea->SetAutoShrinkModeEnabled(true);
101                 __pRecipientArea->AddFocusEventListener(*this);
102                 __pRecipientArea->AddExpandableEditAreaEventListener(*this);
103                 __pRecipientArea->SetTokenFilter(this);
104                 __pRecipientArea->SetMinimumSize(Dimension(W_CONTRACTED_RECIPIENT_AREA, H_RECIPIENT_AREA));
105                 __pRecipientArea->SetKeypadEnabled(true);
106                 __pRecipientArea->SetKeypadStyle(KEYPAD_STYLE_EMAIL);
107                 __pRecipientArea->SetLowerCaseModeEnabled(true);
108                 __pRecipientArea->SetTokenEditModeEnabled(true);
109         }
110         else
111         {
112                 __pRecipientArea->Construct(FloatRectangle(X_COMMON_CUSTOM_ITEM, Y_RECIPIENT_AREA, GetWidth()-(2*X_COMMON_CUSTOM_ITEM), H_RECIPIENT_AREA), EXPANDABLE_EDIT_AREA_STYLE_TOKEN, EXPANDABLE_EDIT_AREA_TITLE_STYLE_INNER, COUNT_LINE);
113                 __pRecipientArea->SetAutoShrinkModeEnabled(false);
114                 __pRecipientArea->AddExpandableEditAreaEventListener(*this);
115                 __pRecipientArea->HideKeypad();
116                 __pRecipientArea->SetKeypadEnabled(false);
117                 __pRecipientArea->SetFocusable(false);
118                 __pRecipientArea->SetEnabled(false);
119                 __pRecipientArea->SetTokenEditModeEnabled(false);
120         }
121
122         __pRecipientArea->SetColor(EDIT_STATUS_NORMAL, bgColor);
123         //__pRecipientArea->SetColor(EDIT_STATUS_HIGHLIGHTED, bgColor);
124         __pRecipientArea->SetColor(EDIT_STATUS_PRESSED, bgColor);
125         __pRecipientArea->SetColor(EDIT_STATUS_DISABLED, bgColor);
126         __pRecipientArea->AddKeyEventListener(*this);
127
128         AddControl(__pRecipientArea);
129         SetControlAlwaysOnTop(*__pRecipientArea, true);
130
131         __pAddContactButton = new (std::nothrow) Button();
132         __pAddContactButton->Construct(Rectangle(bounds.width-X_COMMON_CUSTOM_ITEM-W_ADDCONTACT_BUTTON, Y_ADD_CONTACT_BUTTON, W_ADD_CONTACT_BUTTON, H_ADD_CONTACT_BUTTON), L"");
133
134         if (__mode == READ_WRITE_MODE)
135         {
136                 __pAddContactButton->AddFocusEventListener(*this);
137         }
138
139         __pAddContactButton->SetColor(BUTTON_STATUS_NORMAL, Color::GetColor(COLOR_ID_WHITE));
140         __pAddContactButton->SetNormalBitmap(Point(14, 14), *pContactBitmap);
141         __pAddContactButton->SetNormalBackgroundBitmap(*pContactCircleBitmap);
142         __pAddContactButton->SetPressedBackgroundBitmap(*pContactCirclePressedBitmap);
143         __pAddContactButton->SetActionId(IDA_TOFIELDPANEL_ADD_CONTACT_BUTTON);
144         __pAddContactButton->SetShowState(false);
145         AddControl(__pAddContactButton);
146
147         __pCountButton = new (std::nothrow) Button();
148         __pCountButton->Construct(Rectangle(__pAddContactButton->GetX()-GENERIC_OFFSET_16-W_COUNT_LABEL, Y_COUNT_LABEL, W_COUNT_LABEL, H_COUNT_LABEL), L"");
149         __pCountButton->SetTextColor(Color::GetColor(COLOR_ID_WHITE));
150         __pCountButton->SetDisabledTextColor(Color::GetColor(COLOR_ID_WHITE));
151         __pCountButton->SetHighlightedTextColor(Color::GetColor(COLOR_ID_WHITE));
152         __pCountButton->SetNormalBackgroundBitmap(*pContactCircleBitmap);
153         __pCountButton->SetPressedBackgroundBitmap(*pContactCircleBitmap);
154         __pCountButton->SetTextSize(26);
155         __pCountButton->SetShowState(false);
156         AddControl(__pCountButton);
157
158         relLayout.SetRelation(*__pAddContactButton, this, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
159         relLayout.SetMargin(*__pAddContactButton, 16, 12, 24, 24);
160
161         relLayout.SetRelation(*__pCountButton, this, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
162         relLayout.SetMargin(*__pCountButton, 16, 12, 24, 24);
163
164         __pRecipientList = new (std::nothrow) ArrayList();
165         __pRecipientList->Construct();
166
167         delete pContactBitmap;
168         delete pContactCircleBitmap;
169         delete pContactCirclePressedBitmap;
170
171         AppLogDebug("Email::EXIT: r = %s", GetErrorMessage(r));
172         return r;
173 }
174
175 void
176 RecipientField::AddNewToken(const String& emailIds, bool showMsgBox)
177 {
178         AppLogDebug("ENTER, emailIds:(%ls)", emailIds.GetPointer());
179
180         int count = 0;
181         bool alreadyExists=false, invalidAddress=false;
182         result r = E_SUCCESS;
183         HashMap* pDuplicateMap = null;
184         String token;
185         String emailAddress;
186         Utility::StringTokenizer addressTokenizer(emailIds, IDS_DELIM_SEMICOLON);
187
188         pDuplicateMap = new (std::nothrow) HashMap();
189         TryCatch(pDuplicateMap != null, ,"Failed to allocate memory for duplicate map");
190
191         r = pDuplicateMap->Construct();
192         TryCatch(r == E_SUCCESS, delete pDuplicateMap, "Failed to construct duplicate map");
193
194         count = __pRecipientList->GetCount();
195         for (int i=0; i<count; i++)
196         {
197                 emailAddress.Clear();
198                 emailAddress.Append(*static_cast<String*>(__pRecipientList->GetAt(i)));
199
200                 pDuplicateMap->Add(new (std::nothrow) String(emailAddress), new (std::nothrow) Integer(1));
201         }
202
203         while (addressTokenizer.HasMoreTokens())
204         {
205                 addressTokenizer.GetNextToken(token);
206
207                 if (__mode == READ_MODE)
208                 {
209                         __alreadyValidated = true;
210
211                         __pRecipientArea->AppendToken(token);
212                         __pRecipientList->Add(new (std::nothrow) String(token));
213
214                         __alreadyValidated = false;
215                 }
216                 else
217                 {
218                         if (Utils::ValidateEmailAddress(token) == true)
219                         {
220                                 r = pDuplicateMap->Add(new (std::nothrow) String(token), new (std::nothrow) Integer(1));
221
222                                 if (r == E_OBJ_ALREADY_EXIST)
223                                 {
224                                         alreadyExists = true;
225                                 }
226                                 else if (r == E_SUCCESS)
227                                 {
228                                         __alreadyValidated = true;
229
230                                         __pRecipientArea->AppendToken(token);
231                                         __pRecipientList->Add(new (std::nothrow) String(token));
232
233                                         __alreadyValidated = false;
234                                 }
235                         }
236                         else
237                         {
238                                 invalidAddress = true;
239                         }
240                 }
241         }
242
243         if (showMsgBox == true)
244         {
245                 if (invalidAddress == true)
246                 {
247                         int modRes;
248                         MessageBox msgBox;
249                         msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
250                         msgBox.ShowAndWait(modRes);
251                 }
252
253                 if (alreadyExists == true)
254                 {
255                         FloatDimension convertedSize = FloatDimension(__pRecipientArea->GetWidthF(), H_RECIPIENT_AREA * 1.0);
256                         r = __pRecipientArea->SetSize(convertedSize);
257
258                         int modRes;
259                         MessageBox msgBox;
260                         msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
261                         msgBox.ShowAndWait(modRes);
262                 }
263         }
264
265         count = __pRecipientArea->GetTokenCount();
266         token.Clear();
267         token.Append(count);
268         __pCountButton->SetText(token);
269
270         pDuplicateMap->RemoveAll(true);
271         delete pDuplicateMap;
272
273         Invalidate(true);
274
275 CATCH:
276         AppLogDebug("EXIT");
277         return;
278 }
279
280 int
281 RecipientField::GetAddressFieldHeight(void)
282 {
283         AppLogDebug("returning height:(%d)", __pRecipientArea->GetHeight());
284         return __pRecipientArea->GetHeight();
285 }
286
287 int
288 RecipientField::GetAddressFieldWidth(void)
289 {
290         AppLogDebug("returning width:(%d)", __pRecipientArea->GetWidth());
291         return __pRecipientArea->GetWidth();
292 }
293
294 int
295 RecipientField::GetTokenCount(void)
296 {
297         AppLogDebug("returning token count:(%d)", __pRecipientArea->GetTokenCount());
298         return __pRecipientArea->GetTokenCount();
299 }
300
301 void
302 RecipientField::HideKeypad(void)
303 {
304         __pRecipientArea->HideKeypad();
305 }
306
307 bool
308 RecipientField::IsDuplicate(const String& emailAddress)
309 {
310         AppLogDebug("ENTER, got emailAddress:(%ls)", emailAddress.GetPointer());
311
312         bool retVal = false;
313         String* pTmpAddress;
314
315         for (int i=0; i<__pRecipientList->GetCount(); i++)
316         {
317                 pTmpAddress = dynamic_cast<String*>(__pRecipientList->GetAt(i));
318
319                 AppLogDebug("Got pTmpAddress:(%x)", pTmpAddress);
320                 AppLogDebugIf(pTmpAddress != null, "pTmpAddress:(%ls)", pTmpAddress->GetPointer());
321
322                 if (pTmpAddress != null && pTmpAddress->Equals(emailAddress, false) == true)
323                 {
324                         retVal = true;
325                         break;
326                 }
327         }
328
329         AppLogDebug("EXIT, returning:(%d)", retVal);
330         return retVal;
331 }
332
333 void
334 RecipientField::SetExpandableEditAreaTitle(const String& titleText)
335 {
336         AppLogDebug("ENTER");
337
338         __pRecipientArea->SetTitleText(titleText);
339
340         AppLogDebug("EXIT");
341 }
342
343 void
344 RecipientField::SetTokenChangedEventListener(ITokenChangedEventListener& listener)
345 {
346         __pTokenChangedEventListener = &listener;
347 }
348
349 void
350 RecipientField::SetAddContactButtonEventListener(IActionEventListener& listener)
351 {
352         __pAddContactButton->AddActionEventListener(listener);
353 }
354
355 void
356 RecipientField::SetKeyPadEventListener(Tizen::Ui::IKeypadEventListener& listener)
357 {
358         __pRecipientArea->AddKeypadEventListener(listener);
359 }
360
361 result
362 RecipientField::SetFocus(void)
363 {
364         return __pRecipientArea->SetFocus();
365 }
366
367 ArrayList*
368 RecipientField::GetRecipientList(void)
369 {
370         return __pRecipientList;
371 }
372
373 ExpandableEditArea*
374 RecipientField::GetRecipientArea(void)
375 {
376         return __pRecipientArea;
377 }
378
379 void
380 RecipientField::OnBoundsChanged(const Rectangle& oldRect, const Rectangle& newRect)
381 {
382         AppLogDebug("ENTER newrect bounds (%d) (%d) (%d) (%d)", newRect.x, newRect.y, newRect.width, newRect.height);
383
384         result r = E_SUCCESS;
385
386         if (__pRecipientArea != null)
387         {
388                 float width = (newRect.width*1.0)-(2.0*X_COMMON_CUSTOM_ITEM)-W_ADD_CONTACT_BUTTON-GENERIC_OFFSET_16;
389                 if (width < 0)
390                 {
391                         width = 1.0;
392                 }
393
394                 if (__hasFocus == true || __mode == READ_MODE)
395                 {
396 //                      FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(width, __pRecipientArea->GetHeightF()));
397                         FloatDimension convertedSize = FloatDimension(width, H_RECIPIENT_AREA * 1.0);
398                         r = __pRecipientArea->SetSize(convertedSize);
399                         AppLogDebug("Result of SetSize:(%s)", GetErrorMessage(r));
400                 }
401                 else
402                 {
403 //                      FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(width, H_RECIPIENT_AREA*1.0));
404                         FloatDimension convertedSize = FloatDimension(width, H_RECIPIENT_AREA*1.0);
405                         r = __pRecipientArea->SetSize(convertedSize);
406                         AppLogDebug("Result of SetSize:(%s)", GetErrorMessage(r));
407                 }
408
409                 AppLogDebug("recipient area bounds:(%f, %f, %f, %f)", __pRecipientArea->GetBoundsF().x, __pRecipientArea->GetBoundsF().y, __pRecipientArea->GetBoundsF().width, __pRecipientArea->GetBoundsF().height);
410
411                 __pRecipientArea->Invalidate(true);
412         }
413
414         AppLogDebug("EXIT");
415 }
416
417 void
418 RecipientField::OnExpandableEditAreaLineAdded(ExpandableEditArea& source, int newLineCount)
419 {
420         AppLogDebug("ENTER");
421
422         result r = E_SUCCESS;
423
424         if (__hasFocus == true)
425         {
426                 AppLogDebug("Has focus");
427                 __expandedHeight = __pRecipientArea->GetHeightF();
428
429                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), __expandedHeight+(2.0*Y_RECIPIENT_AREA)));
430                 r = SetSize(convertedSize);
431         }
432         else if (__hasFocus == false && __mode == READ_WRITE_MODE)
433         {
434                 AppLogDebug("No focus");
435                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), H_RECIPIENT_AREA+(2.0*Y_RECIPIENT_AREA)));
436                 r = SetSize(convertedSize);
437         }
438         else if (__mode == READ_MODE)
439         {
440                 AppLogDebug("Read mode");
441                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), __pRecipientArea->GetHeight()+(2.0*Y_RECIPIENT_AREA)));
442                 r = SetSize(convertedSize);
443         }
444
445         AppLogDebug("Result of setsize:(%s)", GetErrorMessage(r));
446         AppLogDebug("To Field Panel dimensions:(%f,%f)", GetWidthF(), GetHeightF());
447
448         if (__pTokenChangedEventListener)
449         {
450                 __pTokenChangedEventListener->LineCountChanged(*this);
451         }
452
453         AppLogDebug("EXIT");
454         return;
455 }
456
457 void
458 RecipientField::OnExpandableEditAreaLineRemoved(ExpandableEditArea& source, int newLineCount)
459 {
460         AppLogDebug("ENTER");
461
462         if (__pRecipientArea->HasFocus() == true)
463         {
464                 AppLogDebug("Has focus");
465                 __expandedHeight = __pRecipientArea->GetHeightF();
466
467                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), __expandedHeight+(2.0*Y_RECIPIENT_AREA)));
468                 SetSize(convertedSize);
469         }
470         else
471         {
472                 AppLogDebug("No focus");
473                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), H_RECIPIENT_AREA+(2.0*Y_RECIPIENT_AREA)));
474                 SetSize(convertedSize);
475         }
476
477         AppLogDebug("To Field Panel dimensions:(%f,%f)", __pRecipientArea->GetWidthF(), __pRecipientArea->GetHeightF());
478
479         if (__pTokenChangedEventListener)
480         {
481                 __pTokenChangedEventListener->LineCountChanged(*this);
482         }
483
484         AppLogDebug("EXIT");
485         return;
486 }
487
488 void
489 RecipientField::AdjustHeight(void)
490 {
491         AppLogDebug("ENTER");
492
493         // We insert and remove a char so that the control automatically resizes and gives us the correct expanded height after resizing.
494         // OnEEALineAdded is called automatically..
495         __pRecipientArea->InsertCharacterAt(0, ' ');
496         __pRecipientArea->DeleteCharacterAt(0);
497
498         AppLogDebug("EXIT");
499         return;
500 }
501
502 void
503 RecipientField::OnFocusGained(const Control& source)
504 {
505         AppLogDebug("ENTER");
506
507         __hasFocus = true;
508
509         if (source.GetHashCode() == __pRecipientArea->GetHashCode())
510         {
511 //              AdjustHeight();
512
513                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), __pRecipientArea->GetHeightF()+(2.0*Y_RECIPIENT_AREA)));
514                 SetSize(convertedSize);
515
516                 AppLogDebug("recipient area bounds:(%f, %f, %f, %f)", __pRecipientArea->GetBoundsF().x, __pRecipientArea->GetBoundsF().y, __pRecipientArea->GetBoundsF().width, __pRecipientArea->GetBoundsF().height);
517
518                 __pAddContactButton->SetShowState(true);
519                 __pCountButton->SetShowState(false);
520
521                 Invalidate(true);
522
523                 if (__pTokenChangedEventListener)
524                 {
525                         __pTokenChangedEventListener->LineCountChanged(*this);
526                 }
527         }
528         else if (source.GetHashCode() == __pAddContactButton->GetHashCode())
529         {
530                 __pAddContactButton->SetShowState(true);
531                 __pCountButton->SetShowState(false);
532         }
533
534         AppLogDebug("EXIT");
535         return;
536 }
537
538 void
539 RecipientField::OnFocusLost(const Control& source)
540 {
541         AppLogDebug("ENTER");
542         String countText;
543
544         __hasFocus = false;
545
546         if (source.GetHashCode() == __pRecipientArea->GetHashCode())
547         {
548                 FloatDimension convertedSize = CoordinateSystem::AlignToDevice(FloatDimension(GetWidthF(), H_RECIPIENT_AREA+(2.0*Y_RECIPIENT_AREA)));
549                 SetSize(convertedSize);
550
551                 AppLogDebug("recipient area bounds:(%f, %f, %f, %f)", __pRecipientArea->GetBoundsF().x, __pRecipientArea->GetBoundsF().y, __pRecipientArea->GetBoundsF().width, __pRecipientArea->GetBoundsF().height);
552
553                 __pAddContactButton->SetShowState(false);
554
555                 SendUserEvent(IDA_UPDATE_TOKEN_COUNT, null);
556         }
557         else if (source.GetHashCode() == __pAddContactButton->GetHashCode())
558         {
559                 __pAddContactButton->SetShowState(false);
560
561                 if (__pRecipientArea->GetTokenCount() > 1)
562                 {
563                         __pCountButton->SetShowState(true);
564                 }
565                 else
566                 {
567                         __pCountButton->SetShowState(false);
568                 }
569
570                 Invalidate(true);
571         }
572
573         AppLogDebug("EXIT");
574         return;
575 }
576
577 void
578 RecipientField::SetMode(MODE newMode)
579 {
580         AppLogDebug("ENTER");
581         __mode = newMode;
582         AppLogDebug("EXIT");
583         return;
584 }
585
586 void
587 RecipientField::SetUserEventFlagValue(bool value)
588 {
589         AppLogDebug("ENTER");
590
591         __sendUserEvent = value;
592
593         AppLogDebug("EXIT");
594 }
595
596 bool
597 RecipientField::ReplaceToken(const String& token, String& replacement)
598 {
599         AppLogDebug("ENTER");
600         AppLogDebug("Got token: (%ls)", token.GetPointer());
601
602         replacement.Clear();
603         replacement.Append(token);
604         replacement.Trim();
605
606         if (replacement.IsEmpty() == false)
607         {
608                 if (__alreadyValidated == false)
609                 {
610                         int selectedTokenIndex = __pRecipientArea->GetSelectedTokenIndex();
611                         AppLogDebug("selectedTokenIndex: (%d)", selectedTokenIndex);
612
613                         if (selectedTokenIndex == -1)
614                         {
615                                 if (Utils::ValidateEmailAddress(replacement) == false)
616                                 {
617                                         replacement.Clear();
618                                         __pRecipientArea->Clear();
619                                         __pRecipientArea->HideKeypad();
620
621                                         if (__sendUserEvent == false)
622                                         {
623                                                 int modRes;
624                                                 MessageBox msgBox;
625                                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
626                                                 msgBox.ShowAndWait(modRes);
627                                                 __sendUserEvent = true;
628                                         }
629                                         else
630                                         {
631                                                 SendUserEvent(IDA_SHOW_INVALID_ADDRESS_MESSAGE_BOX, null);
632                                         }
633
634 //                                      __pRecipientArea->SetFocus();
635                                 }
636                                 else if (IsDuplicate(replacement) == true)
637                                 {
638                                         replacement.Clear();
639                                         __pRecipientArea->Clear();
640                                         __pRecipientArea->HideKeypad();
641
642                                         if (__sendUserEvent == false)
643                                         {
644                                                 int modRes;
645                                                 MessageBox msgBox;
646                                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
647                                                 msgBox.ShowAndWait(modRes);
648                                                 __sendUserEvent = true;
649                                         }
650                                         else
651                                         {
652                                                 SendUserEvent(IDA_SHOW_DUPLICATE_ADDRESS_MESSAGE_BOX, null);
653                                         }
654
655 //                                      __pRecipientArea->SetFocus();
656                                 }
657                                 else
658                                 {
659                                         __pRecipientList->Add(new (std::nothrow) String(replacement));
660                                 }
661                         }
662                         else
663                         {
664                                 __pRecipientList->RemoveAt(selectedTokenIndex);
665
666                                 if (Utils::ValidateEmailAddress(replacement) == false)
667                                 {
668                                         replacement.Clear();
669                                         __pRecipientArea->Clear();
670                                         __pRecipientArea->HideKeypad();
671
672                                         if (__sendUserEvent == false)
673                                         {
674                                                 int modRes;
675                                                 MessageBox msgBox;
676                                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
677                                                 msgBox.ShowAndWait(modRes);
678                                                 __sendUserEvent = true;
679                                         }
680                                         else
681                                         {
682                                                 SendUserEvent(IDA_SHOW_INVALID_ADDRESS_MESSAGE_BOX, null);
683                                         }
684
685 //                                      __pRecipientArea->SetFocus();
686                                 }
687                                 else if (IsDuplicate(replacement) == true)
688                                 {
689                                         replacement.Clear();
690                                         __pRecipientArea->Clear();
691                                         __pRecipientArea->HideKeypad();
692
693                                         if (__sendUserEvent == false)
694                                         {
695                                                 int modRes;
696                                                 MessageBox msgBox;
697                                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
698                                                 msgBox.ShowAndWait(modRes);
699                                                 __sendUserEvent = true;
700                                         }
701                                         else
702                                         {
703                                                 SendUserEvent(IDA_SHOW_DUPLICATE_ADDRESS_MESSAGE_BOX, null);
704                                         }
705
706 //                                      __pRecipientArea->SetFocus();
707                                 }
708                                 else
709                                 {
710                                         __pRecipientList->InsertAt(new (std::nothrow) String(replacement), selectedTokenIndex);
711                                 }
712                         }
713                 }
714         }
715         else
716         {
717                 if (__alreadyValidated == false)
718                 {
719                         int selectedTokenIndex = __pRecipientArea->GetSelectedTokenIndex();
720                         AppLogDebug("selectedTokenIndex: (%d)", selectedTokenIndex);
721
722                         if (selectedTokenIndex != -1)
723                         {
724                                 __pRecipientList->RemoveAt(selectedTokenIndex);
725                         }
726                 }
727         }
728
729         AppLogDebug("Got replacement: (%ls)", replacement.GetPointer());
730         AppLogDebug("EXIT");
731         return true;
732 }
733
734 void
735 RecipientField::OnKeyPressed(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
736 {
737         AppLogDebug("ENTER, keyCode:(%d)", keyCode);
738
739         int modRes;
740
741         if ((keyCode == KEY_BACKSPACE || keyCode == KEY_DELETE) && source.GetHashCode() == __pRecipientArea->GetHashCode())
742         {
743                 int index = __pRecipientArea->GetSelectedTokenIndex();
744
745                 if (index == -1)
746                 {
747                         AppLogDebug("No token selected");
748                         index = __pRecipientArea->GetTokenCount()-1;
749                 }
750
751                 AppLogDebug("Got index:(%d)", index);
752
753                 if (__pRecipientArea->GetText().IsEmpty() == true &&  index >= 0) // User deleted a token
754                 {
755                         AppLogDebug("Removing token at index:(%d)", index);
756                         __pRecipientList->RemoveAt(index, true);
757                 }
758         }
759         else if (keyCode == KEY_INVALID)
760         {
761                 String token = __pRecipientArea->GetText();
762                 if (token.IsEmpty() == false)
763                 {
764                         int selectedTokenIndex = __pRecipientArea->GetSelectedTokenIndex();
765
766                         if (Utils::ValidateEmailAddress(token) == false)
767                         {
768                                 __pRecipientArea->Clear();
769
770                                 if (selectedTokenIndex != -1)
771                                 {
772                                         __pRecipientList->RemoveAt(selectedTokenIndex, true);
773                                         __pRecipientArea->RemoveTokenAt(selectedTokenIndex);
774                                 }
775
776                                 MessageBox msgBox;
777                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
778                                 msgBox.ShowAndWait(modRes);
779                         }
780                         else if (IsDuplicate(token) == true)
781                         {
782                                 __pRecipientArea->Clear();
783
784                                 if (selectedTokenIndex != -1)
785                                 {
786                                         __pRecipientList->RemoveAt(selectedTokenIndex, true);
787                                         __pRecipientArea->RemoveTokenAt(selectedTokenIndex);
788                                 }
789
790                                 MessageBox msgBox;
791                                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
792                                 msgBox.ShowAndWait(modRes);
793                         }
794                 }
795         }
796
797 #ifdef _DEBUG
798         AppLogDebug("Recipient list so far:");
799         for (int i=0; i<__pRecipientList->GetCount(); i++)
800         {
801                 AppLogDebug("%ls,",static_cast<String*>(__pRecipientList->GetAt(i))->GetPointer());
802         }
803 #endif // _DEBUG
804
805         AppLogDebug("EXIT");
806 }
807
808 void
809 RecipientField::OnKeyReleased(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
810 {
811         // Empty Implementation
812 }
813
814 void
815 RecipientField::OnKeyLongPressed(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
816 {
817         // Empty Implementation
818 }
819
820 void
821 RecipientField::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
822 {
823         AppLogDebug("ENTER");
824
825         if (requestId == IDA_UPDATE_TOKEN_COUNT)
826         {
827                 if (__pRecipientArea->GetTokenCount() > 1)
828                 {
829                         int count = __pRecipientArea->GetTokenCount();
830                         AppLogDebug("recipient count %d, str:(%ls)", count, Integer::ToString(count).GetPointer());
831                         __pCountButton->SetText(Integer::ToString(count));
832                         __pCountButton->SetShowState(true);
833                         __pCountButton->Invalidate(true);
834
835                         if (__pAddContactButton->GetShowState() == true)
836                         {
837                                 __pCountButton->SetShowState(false);
838                         }
839                 }
840                 else
841                 {
842                         int count = __pRecipientArea->GetTokenCount();
843                         AppLogDebug("recipient count %d, str:(%ls)", count, Integer::ToString(count).GetPointer());
844                         __pCountButton->SetText(Integer::ToString(count));
845                         __pCountButton->SetShowState(true);
846                         __pCountButton->Invalidate(true);
847                         __pCountButton->SetShowState(false);
848                 }
849
850                 Invalidate(true);
851
852                 if (__pTokenChangedEventListener)
853                 {
854                         __pTokenChangedEventListener->LineCountChanged(*this);
855                 }
856         }
857         else if (requestId == IDA_SHOW_INVALID_ADDRESS_MESSAGE_BOX)
858         {
859                 if (__isWindowDeactived == true)
860                 {
861                         __showMessageBox = IDA_SHOW_INVALID_ADDRESS_MESSAGE_BOX;
862                 }
863                 else
864                 {
865                         int modRes;
866                         MessageBox msgBox;
867                         msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
868                         msgBox.ShowAndWait(modRes);
869                 }
870         }
871         else if (requestId == IDA_SHOW_DUPLICATE_ADDRESS_MESSAGE_BOX)
872         {
873                 if (__isWindowDeactived == true)
874                 {
875                         __showMessageBox = IDA_SHOW_DUPLICATE_ADDRESS_MESSAGE_BOX;
876                 }
877                 else
878                 {
879                         int modRes;
880                         MessageBox msgBox;
881                         msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
882                         msgBox.ShowAndWait(modRes);
883                 }
884         }
885
886         AppLogDebug("EXIT");
887 }
888
889 void
890 RecipientField::OnWindowActivated (const Tizen::Ui::Window &source)
891 {
892         AppLogDebug("ENTER");
893
894         __isWindowDeactived = false;
895
896         if (__showMessageBox == IDA_SHOW_INVALID_ADDRESS_MESSAGE_BOX)
897         {
898                 __showMessageBox = 0;
899                 int modRes;
900                 MessageBox msgBox;
901                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_EMAIL_POP_INVALID_EMAIL_ADDRESS"), MSGBOX_STYLE_OK, 300000);
902                 msgBox.ShowAndWait(modRes);
903         }
904         else if (__showMessageBox == IDA_SHOW_DUPLICATE_ADDRESS_MESSAGE_BOX)
905         {
906                 __showMessageBox = 0;
907                 int modRes;
908                 MessageBox msgBox;
909                 msgBox.Construct(Utils::GetResourceString(L"IDS_COM_POP_ERROR"), Utils::GetResourceString(L"IDS_MSGC_BODY_DUPLICATED_RECIPIENT"), MSGBOX_STYLE_OK, 300000);
910                 msgBox.ShowAndWait(modRes);
911         }
912
913         AppLogDebug("EXIT");
914 }
915
916 void
917 RecipientField::OnWindowDeactivated (const Tizen::Ui::Window &source)
918 {
919         AppLogDebug("ENTER");
920
921         __isWindowDeactived = true;
922
923         AppLogDebug("EXIT");
924 }
925