Fixed Nabi Issues N_SE-56966,56735
[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.1 (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                 AppLog("width of editfield edithomepage %d",__pUrlEditField->GetWidth());
85         }
86
87         return r;
88 }
89
90 result
91 EditHomePageForm::OnTerminating(void)
92 {
93         result r = E_SUCCESS;
94         return r;
95 }
96
97 void
98 EditHomePageForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
99 {
100         if (__pUrlEditField != null)
101         {
102                 String favURL = SettingPresentationModel::GetInstance()->GetFavoriteUrl();
103                 AppLogDebug("EditHomePageForm::OnSceneActivatedN favURL %ls",favURL.GetPointer());
104                 __pUrlEditField->SetText(SettingPresentationModel::GetInstance()->GetFavoriteUrl());
105                 __pUrlEditField->SetFocus();
106                 __pUrlEditField->SetCursorPosition(__pUrlEditField->GetTextLength());
107                 __pUrlEditField->ShowKeypad();
108                 __pUrlEditField->Invalidate(true);
109         }
110
111         Invalidate(true);
112
113         return;
114 }
115
116 void
117 EditHomePageForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
118 {
119
120 }
121
122 void
123 EditHomePageForm::OnFormBackRequested(Form& source)
124 {
125         ArrayList* pArgList = null;
126         pArgList = new(std::nothrow) ArrayList();
127         pArgList->Construct();
128         pArgList->Add(*new(std::nothrow) String(CommonUtil::GetString(L"IDS_BR_SK_CANCEL")));
129         SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
130
131 }
132
133 void
134 EditHomePageForm::OnActionPerformed(const Control& source, int actionId)
135 {
136         ArrayList* pArgList = null;
137         pArgList = new(std::nothrow) ArrayList();
138         pArgList->Construct();
139         switch(actionId)
140         {
141         case IDA_BUTTON_DONE:
142         {
143                 pArgList->Add(*new(std::nothrow) String(CommonUtil::GetString(L"IDS_BR_SK_DONE")));
144                 String urlText = __pUrlEditField->GetText();
145                 urlText.Trim();
146                 AppLogDebug("set url text is %ls",urlText.GetPointer());
147
148                 bool ret = false;
149                 bool flag = false;
150                 String firstPattern(L"((ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)");//[\w\d:#@%/;$()~_?\+-=\\\.&]*
151                 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)
152                 RegularExpression firstRegex;
153                 RegularExpression secondRegex;
154
155                 firstRegex.Construct(firstPattern, REGEX_CASELESS);
156                 secondRegex.Construct(secondPattern, REGEX_CASELESS);
157
158 //              Uri uri;
159 //              uri.SetUri(urlText);
160 //              urlText = uri.GetEncodedString();
161
162                 // Match
163                 ret = firstRegex.Match(urlText, false); // This returns true value
164                 flag = secondRegex.Match(urlText, false);  // Checks whether URL typed is of type  abc.com (i.e without protocol in front of it)
165
166
167                 if (ret == true)
168                 {
169                         //valid Url
170                         SettingPresentationModel::GetInstance()->SetHomepage(urlText);
171                         SettingPresentationModel::GetInstance()->SetFavoriteValue(urlText);
172                         result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
173                         TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
174                 }
175                 else
176                 {
177                         if (flag == true)
178                         {
179                                 String validUrl;
180                                 if((urlText.Contains("http://") == true) || (urlText.Contains("https://") == true))
181                                 {
182                                         validUrl.Append(urlText);
183                                 }
184                                 else
185                                 {
186                                         validUrl = L"http://";
187                                         validUrl.Append(urlText);
188                                 }
189
190                                 // save Url
191                                 SettingPresentationModel::GetInstance()->SetHomepage(validUrl);
192                                 SettingPresentationModel::GetInstance()->SetFavoriteValue(validUrl);
193                                 result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
194                                 TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
195                         }
196                         else
197                         {
198                                 // show pop up , not a valid URL
199                                 String msg = CommonUtil::GetString(L"IDS_COM_POP_INVALID_URL");
200                                 CreateMessage(msg);
201                         }
202                 }
203
204                 break;
205         }
206         case IDA_BUTTON_CANCEL:
207         {
208                 pArgList->Add(*new(std::nothrow) String(CommonUtil::GetString(L"IDS_BR_SK_CANCEL")));
209                 result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(), pArgList);
210                 TryCatch(!IsFailed(r), "EditHomePageForm::OnActionPerformed Failed to GoBackward %s",GetErrorMessage(r));
211                 break;
212         }
213         default:
214                 break;
215         }
216
217         CATCH:
218         pArgList->RemoveAll(true);
219         delete pArgList;
220         return;
221 }
222
223 void
224 EditHomePageForm::OnTextValueChanged(const Control& source)
225 {
226         String title ;
227         Header* pHeader = GetHeader();
228
229         if (pHeader == NULL)
230         {
231                 return;
232         }
233
234         if (__pUrlEditField == NULL)
235         {
236                 return;
237         }
238
239         title = __pUrlEditField->GetText();
240         title.Trim();
241
242         // To disable done button when folder title edit-field is empty
243         if (__pUrlEditField && title.IsEmpty() == true)
244         {
245                 pHeader->SetButtonEnabled(BUTTON_POSITION_RIGHT, false);
246         }
247         else
248         {
249                 pHeader->SetButtonEnabled(BUTTON_POSITION_RIGHT, true);
250         }
251         pHeader->Invalidate(true);
252         return;
253 }
254
255 void
256 EditHomePageForm::OnTextValueChangeCanceled(const Control& source)
257 {
258         return;
259 }
260
261 void
262 EditHomePageForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
263 {
264         return;
265 }
266
267 void
268 EditHomePageForm::CreateMessage(String& message)
269 {
270
271         if (__pMsgBox != null)
272         {
273                 delete __pMsgBox;
274                 __pMsgBox = null;
275                 __modalMsgBoxResult = 0;
276         }
277
278         __pMsgBox = new(std::nothrow) MessageBox;
279         if (__pMsgBox != null)
280         {
281                 __pMsgBox->Construct(L"", message, MSGBOX_STYLE_OK, 3000);
282         }
283
284         if (__pMsgBox != null)
285         {
286                 __pMsgBox->ShowAndWait(__modalMsgBoxResult);
287                 if (__modalMsgBoxResult == MSGBOX_RESULT_OK)
288                 {
289                         if (__pMsgBox != null)
290                         {
291                                 delete __pMsgBox;
292                                 __pMsgBox = null;
293                         }
294                 }
295                 __modalMsgBoxResult = 0;
296         }
297
298         return;
299 }