Tizen 2.1 base
[apps/osp/Internet.git] / src / IntEditHomePageForm.cpp
1 //
2
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://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 //!Internet EditHomePageForm class
19 /*@file:  EditHomePageForm.h
20  *@brief: Allow user to set any page as Home page.
21  *
22  */
23
24 #include <FAppUiApp.h>
25 #include <FUi.h>
26
27 #include "IntBookmarkData.h"
28 #include "IntCommonLib.h"
29 #include "IntEditHomePageForm.h"
30 #include "IntMultipleWindowPresentationModel.h"
31 #include "IntSceneRegister.h"
32 #include "IntSettingPresentationModel.h"
33
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Utility;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
43
44 const int EditHomePageForm::IDA_BUTTON_DONE = 101;
45 const int EditHomePageForm::IDA_BUTTON_CANCEL = 102;
46
47 EditHomePageForm::EditHomePageForm(void)
48 :__pUrlEditField(null)
49 , __pMsgBox(null)
50 , __modalMsgBoxResult(0)
51 {
52
53 }
54
55 EditHomePageForm::~EditHomePageForm(void)
56 {
57
58 }
59
60 bool
61 EditHomePageForm::Initialize(void)
62 {
63         Construct(L"IDL_EDIT_HOME_PAGE");
64         SetOrientation(ORIENTATION_AUTOMATIC);
65         return true;
66 }
67
68 result
69 EditHomePageForm::OnInitializing(void)
70 {
71         result r = E_SUCCESS;
72         SetFormBackEventListener(this);
73         SceneManager* pSceneManager = SceneManager::GetInstance();
74         AppAssert(pSceneManager);
75         pSceneManager->AddSceneEventListener(IDSCN_EDIT_HOMEPAGE_VIEW, *this);
76
77         GetHeader()->AddActionEventListener(*this);
78
79         __pUrlEditField = static_cast<EditField*>(GetControl(L"IDC_EDITFIELD"));
80         if (__pUrlEditField != null)
81         {
82                 __pUrlEditField->AddTextEventListener(*this);
83                 __pUrlEditField->SetOverlayKeypadCommandButtonVisible(false);
84         }
85         return r;
86 }
87
88 result
89 EditHomePageForm::OnTerminating(void)
90 {
91         result r = E_SUCCESS;
92         return r;
93 }
94
95 void
96 EditHomePageForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
97 {
98         if (__pUrlEditField != null)
99         {
100                 String favURL = SettingPresentationModel::GetInstance()->GetFavoriteUrl();
101                 AppLogDebug("EditHomePageForm::OnSceneActivatedN favURL %ls",favURL.GetPointer());
102                 __pUrlEditField->SetText(SettingPresentationModel::GetInstance()->GetFavoriteUrl());
103                 __pUrlEditField->SetFocus();
104                 __pUrlEditField->SetCursorPosition(__pUrlEditField->GetTextLength());
105                 __pUrlEditField->ShowKeypad();
106                 __pUrlEditField->Invalidate(true);
107         }
108
109         Invalidate(true);
110
111         return;
112 }
113
114 void
115 EditHomePageForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
116 {
117
118 }
119
120 void
121 EditHomePageForm::OnFormBackRequested(Form& source)
122 {
123
124 }
125
126 void
127 EditHomePageForm::OnActionPerformed(const Control& source, int actionId)
128 {
129         ArrayList* pArgList = null;
130         pArgList = new(std::nothrow) ArrayList();
131         switch(actionId)
132         {
133         case IDA_BUTTON_DONE:
134         {
135                 pArgList->Add(*new(std::nothrow) String(CommonUtil::GetString(L"IDS_BR_SK_DONE")));
136                 String urlText = __pUrlEditField->GetText();
137                 AppLogDebug("set url text is %ls",urlText.GetPointer());
138
139                 bool ret = false;
140                 bool flag = false;
141                 String firstPattern(L"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)");
142                 String secondPattern(L"^[A-Za-z0-9\.\+-:#@%/;$~?]+\\.[A-Za-z0-9\+-=:#@%/;$~()_?\\\.&]{2,}$");   // URL of type  abc.com (i.e without protocol in front of it)
143                 RegularExpression firstRegex;
144                 RegularExpression secondRegex;
145
146                 firstRegex.Construct(firstPattern, REGEX_CASELESS);
147                 secondRegex.Construct(secondPattern, REGEX_CASELESS);
148
149                 Uri uri;
150                 uri.SetUri(urlText);
151                 urlText = uri.GetEncodedString();
152
153                 // Match
154                 ret = firstRegex.Match(urlText, false); // This returns true value
155                 flag = secondRegex.Match(urlText, false);  // Checks whether URL typed is of type  abc.com (i.e without protocol in front of it)
156
157
158                 if (ret == true)
159                 {
160                         //valid Url
161                         SettingPresentationModel::GetInstance()->SetHomepage(urlText);
162                         SettingPresentationModel::GetInstance()->SetFavoriteValue(urlText);
163                         result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
164                         TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
165                 }
166                 else
167                 {
168                         if (flag == true)
169                         {
170                                 String validUrl;
171                                 validUrl = L"http://";
172                                 validUrl.Append(urlText);
173                                 // save Url
174                                 SettingPresentationModel::GetInstance()->SetHomepage(validUrl);
175                                 SettingPresentationModel::GetInstance()->SetFavoriteValue(validUrl);
176                                 result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
177                                 TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
178                         }
179                         else
180                         {
181                                 // show pop up , not a valid URL
182                                 String msg = CommonUtil::GetString(L"IDS_BR_POP_INVALIDADDRESS");
183                                 CreateMessage(msg);
184                         }
185                 }
186
187                 break;
188         }
189         case IDA_BUTTON_CANCEL:
190         {
191                 pArgList->Add(*new(std::nothrow) String(CommonUtil::GetString(L"IDS_BR_SK_CANCEL")));
192                 result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
193                 TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
194                 break;
195         }
196         default:
197                 break;
198         }
199
200         CATCH:
201         pArgList->RemoveAll(true);
202         delete pArgList;
203         return;
204 }
205
206 void
207 EditHomePageForm::OnTextValueChanged(const Control& source)
208 {
209         String title ;
210         Header* pHeader = GetHeader();
211
212         if (pHeader == NULL)
213         {
214                 return;
215         }
216
217         if (__pUrlEditField == NULL)
218         {
219                 return;
220         }
221
222         title = __pUrlEditField->GetText();
223         title.Trim();
224
225         // To disable done button when folder title edit-field is empty
226         if (__pUrlEditField && title.IsEmpty() == true)
227         {
228                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
229         }
230         else
231         {
232                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
233         }
234         pHeader->Invalidate(true);
235         return;
236 }
237
238 void
239 EditHomePageForm::OnTextValueChangeCanceled(const Control& source)
240 {
241         return;
242 }
243
244 void
245 EditHomePageForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
246 {
247         return;
248 }
249
250 void
251 EditHomePageForm::CreateMessage(String& message)
252 {
253
254         if (__pMsgBox != null)
255         {
256                 delete __pMsgBox;
257                 __pMsgBox = null;
258                 __modalMsgBoxResult = 0;
259         }
260
261         __pMsgBox = new(std::nothrow) MessageBox;
262         if (__pMsgBox != null)
263         {
264                 __pMsgBox->Construct(L"", message, MSGBOX_STYLE_OK, 3000);
265         }
266
267         if (__pMsgBox != null)
268         {
269                 __pMsgBox->ShowAndWait(__modalMsgBoxResult);
270                 if (__modalMsgBoxResult == MSGBOX_RESULT_OK)
271                 {
272                         if (__pMsgBox != null)
273                         {
274                                 delete __pMsgBox;
275                                 __pMsgBox = null;
276                         }
277                 }
278                 __modalMsgBoxResult = 0;
279         }
280
281         return;
282 }