Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / SocketChat / SocketChat / project / src / MainFrm.cpp
1 //\r
2 // Open Service Platform\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\r
5 // Licensed under the Flora License, Version 1.0 (the License);\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://www.tizenopensource.org/license\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an AS IS BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 //\r
17 \r
18 #include "SocketChat.h"\r
19 #include "MainFrm.h"\r
20 using namespace Osp::App;\r
21 using namespace Osp::Base;\r
22 using namespace Osp::Ui;\r
23 using namespace Osp::Ui::Controls;\r
24 using namespace Osp::Net;\r
25 using namespace Osp::System;\r
26 MainFrm::MainFrm(void)\r
27 {\r
28         pChatForm = NULL;\r
29         __pip_clientip = NULL;\r
30         __pButtonServer = NULL;\r
31         __pButtonTcp    = NULL;\r
32         __pEditIPAddress= NULL;\r
33         __pEditPort             = NULL;\r
34         b_protocol              = NULL;\r
35         b_server                = NULL;\r
36         iport                   = NULL;\r
37 }\r
38 \r
39 MainFrm::~MainFrm(void)\r
40 {\r
41 }\r
42 \r
43 bool\r
44 MainFrm::Initialize()\r
45 {\r
46         // Construct an XML form\r
47         Construct(L"IDF_MAINFRM");\r
48 \r
49         return true;\r
50 }\r
51 \r
52 result\r
53 MainFrm::OnInitializing(void)\r
54 {\r
55         result r = E_SUCCESS;\r
56 \r
57         // Get a button via resource ID\r
58         __pButtonServer = static_cast <CheckButton *>(GetControl(L"IDC_CHECKServer"));\r
59 \r
60         if (__pButtonServer != null)\r
61         {\r
62                 __pButtonServer->SetActionId(IDC_CHECKServer_SEL,IDC_CHECKServer_UNSEL);\r
63                 __pButtonServer->AddActionEventListener(*this);\r
64         }\r
65 \r
66         __pButtonTcp = static_cast <CheckButton *>(GetControl(L"IDC_CHECKTCP"));\r
67         __pButtonUdp = static_cast <CheckButton *>(GetControl(L"IDC_CHECKUDP"));\r
68 \r
69         __pEditIPAddress = static_cast <EditField *>(GetControl(L"IDC_EDITIPaddress"));\r
70         if(__pEditIPAddress == null)\r
71         {\r
72                 r = E_FAILURE;\r
73         }\r
74 \r
75         __pEditPort = static_cast <EditField *>(GetControl(L"IDC_EDITPort"));\r
76         Footer* pFooter = GetFooter();\r
77         if (pFooter)\r
78         {\r
79                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);\r
80 \r
81 \r
82                 footerItem_Left.Construct(ID_START);\r
83                 footerItem_Left.SetText(L"Connect");\r
84                 pFooter->AddItem(footerItem_Left);\r
85                 pFooter->AddActionEventListener(*this);\r
86         }\r
87         return r;\r
88 }\r
89 void\r
90 MainFrm::SetDefaultValues()\r
91 {\r
92         if (__pButtonTcp != null)\r
93         {\r
94                 __pButtonTcp->SetSelected(true);\r
95         }\r
96 }\r
97 result\r
98 MainFrm::OnTerminating(void)\r
99 {\r
100         result r = E_SUCCESS;\r
101 \r
102         AppLog("MainFrm::OnTerminating");\r
103 \r
104         return r;\r
105 }\r
106 \r
107 void\r
108 MainFrm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)\r
109 {\r
110         switch(actionId)\r
111         {\r
112 \r
113                 case IDC_CHECKServer_UNSEL:\r
114                 {\r
115                         AppLog("Server Button unselected");\r
116                         __pEditIPAddress->SetEnabled(true);\r
117                         __pEditIPAddress->RequestRedraw(true);\r
118                         Footer* pFooter = GetFooter();\r
119                         pFooter->RemoveItemAt(0);\r
120                         footerItem_Left.SetText(L"Connect");\r
121                         pFooter->InsertItemAt(0,footerItem_Left);\r
122                         Draw();\r
123 \r
124 \r
125                 }\r
126                 break;\r
127                 case IDC_CHECKServer_SEL:\r
128                 {\r
129                         AppLog("Server button selected");\r
130 \r
131                         __pEditIPAddress->SetEnabled(false);\r
132                         __pEditIPAddress->RequestRedraw(true);\r
133                         this->SetFocus();\r
134                         Footer* pFooter = GetFooter();\r
135                         pFooter->RemoveItemAt(0);\r
136                         footerItem_Left.SetText(L"Listen");\r
137                         pFooter->InsertItemAt(0,footerItem_Left);\r
138                         // Draw the form\r
139                         Draw();\r
140                 }\r
141                 break;\r
142         case ID_START:\r
143                 {\r
144                         AppLog("Start Button is clicked! \n");\r
145                         StartChat();\r
146                 }\r
147                 break;\r
148         case ID_BUTTON_CLOSE:\r
149                 {\r
150                         AppLog("Back Button pressed, close application now");\r
151                         Application::GetInstance()->Terminate();\r
152                 }\r
153                 break;\r
154         default:\r
155         {\r
156                 AppLog("default");\r
157         }\r
158                 break;\r
159         }\r
160 }\r
161 \r
162 \r
163 void\r
164 MainFrm::StartChat()\r
165 {\r
166         String port;\r
167         // get protocol type\r
168         b_protocol = __pButtonTcp->IsSelected();\r
169 \r
170         //get connection type\r
171         b_server = __pButtonServer->IsSelected();\r
172 \r
173         if(b_server== false)\r
174         {\r
175                 //get ipaddress and port\r
176                 String ipaddress;\r
177                 ipaddress = __pEditIPAddress->GetText();\r
178 \r
179 \r
180                 if(ipaddress.IsEmpty() == true)\r
181                 {\r
182                         int result;\r
183                         MessageBox *pMsgBox = new MessageBox() ;\r
184                         pMsgBox->Construct(L"IP Address", L"Please enter IP address", MSGBOX_STYLE_OK) ;\r
185                         pMsgBox->ShowAndWait(result) ;\r
186                         delete pMsgBox ;\r
187                         return;\r
188                 }\r
189                 else\r
190                 {\r
191                         result r = E_SUCCESS;\r
192                         __pip_clientip = new Ip4Address(ipaddress);\r
193 \r
194                         r = GetLastResult();\r
195                         if(r!=E_SUCCESS || __pip_clientip==NULL)\r
196                         {\r
197                                 int result;\r
198                                 MessageBox *pMsgBox = new MessageBox() ;\r
199                                 pMsgBox->Construct(L"IP Address", L"Invalid IP address", MSGBOX_STYLE_OK) ;\r
200                                 pMsgBox->ShowAndWait(result) ;\r
201                                 delete pMsgBox ;\r
202                                 return;\r
203                         }\r
204 \r
205                 }\r
206         }\r
207         port = __pEditPort->GetText();\r
208         if(port.IsEmpty()==true)\r
209         {\r
210                 int result;\r
211                 MessageBox *pMsgBox = new MessageBox() ;\r
212                 pMsgBox->Construct(L"Port", L"Enter Valid User Port Number", MSGBOX_STYLE_OK) ;\r
213                 pMsgBox->ShowAndWait(result) ;\r
214                 delete pMsgBox ;\r
215                 return;\r
216         }\r
217         else\r
218         {\r
219                 result res=E_SUCCESS;\r
220                 int result;\r
221                 iport = -1;\r
222                 res =Osp::Base::Integer::Decode(port,iport);\r
223                 AppLog("Port No is %d",iport);\r
224                 if(iport<1024 || iport>49151 || res != E_SUCCESS)\r
225                 {\r
226                         MessageBox *pMsgBox = new MessageBox() ;\r
227                         pMsgBox->Construct(L"Port", L"Enter Valid User Port Number", MSGBOX_STYLE_OK) ;\r
228                         pMsgBox->ShowAndWait(result) ;\r
229                         delete pMsgBox ;\r
230                         return;\r
231 \r
232                 }\r
233         }\r
234         if(pChatForm!=NULL)\r
235         {\r
236                 delete pChatForm;\r
237                 pChatForm = NULL;\r
238         }\r
239 \r
240         pChatForm = new ChatFrm(__pip_clientip,iport,b_server,b_protocol, this);\r
241         if(pChatForm==NULL)\r
242         {\r
243                 int result;\r
244                 MessageBox *pMsgBox = new MessageBox() ;\r
245                 pMsgBox->Construct(L"Error", L"Error in allocating Memory", MSGBOX_STYLE_OK) ;\r
246                 pMsgBox->ShowAndWait(result) ;\r
247                 delete pMsgBox ;\r
248                 if(__pip_clientip!=NULL)\r
249                 {\r
250                         delete __pip_clientip;\r
251                         __pip_clientip=NULL;\r
252 \r
253                 }\r
254                 return;\r
255         }\r
256         if(__pip_clientip!=NULL)\r
257         {\r
258                 delete __pip_clientip;\r
259                 __pip_clientip=NULL;\r
260         }\r
261         pChatForm->Initialize();\r
262 \r
263         // Add the form to the frame\r
264         Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();\r
265         pFrame->AddControl(*pChatForm);\r
266         pFrame->SetCurrentForm(*pChatForm);\r
267         pFrame->RequestRedraw();\r
268         pChatForm->EnableControl(false);\r
269         pChatForm->StartChat();\r
270 \r
271 \r
272 }\r
273 void\r
274 MainFrm::StopChat()\r
275 {\r
276         Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();\r
277         if(pFrame!=NULL && pChatForm !=NULL)\r
278         {\r
279                 pFrame->SetCurrentForm(*this);\r
280                 Draw();\r
281                 AppLog("Removing Control");\r
282                 pFrame->RemoveControl(*pChatForm);\r
283 \r
284                 pChatForm = NULL;               \r
285 \r
286                 AppLog("request redraw");\r
287                 pFrame->RequestRedraw();\r
288         }\r
289 }\r
290 void MainFrm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)\r
291 {\r
292         AppLog("User Event Received");\r
293         StopChat();\r
294 \r
295         //SwitchToForm(requestId, pArgs);\r
296 }\r