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