Fixing: 266274 - §qtn_comm_share_wrong_credentials§ Wrong spacing in Danish
[accounts-sso:libaccounts-ui.git] / lib / AccountsUI / credentialwidgetview.cpp
1 /*
2  * This file is part of accounts-ui
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  *
6  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
7  * Contact: Lucian Horga <ext-lucian.horga@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23
24
25 #include "basic-header-widget.h"
26 #include "credentialwidgetview.h"
27 #include "credentialwidget.h"
28 #include "credentialimagewidget.h"
29
30 //M
31 #include <MLayout>
32 #include <MLinearLayoutPolicy>
33 #include <MFlowLayoutPolicy>
34 #include <MGridLayoutPolicy>
35 #include <MLabel>
36 #include <MImageWidget>
37 #include <MTextEdit>
38 #include <MLocale>
39 #include <MContainer>
40 #include <MButton>
41 #include <MTextEdit>
42 #include <MDialog>
43 #include <MInputMethodState>
44
45 //Qt
46 #include <QDebug>
47 #include <QGraphicsWidget>
48 #include <QGraphicsLinearLayout>
49 #include <QGraphicsGridLayout>
50 #include <QImage>
51
52 #define ROW_SPACING 30
53 const int BinaryTextVariantSeparator = 0x9c;
54
55 class CredentialWidgetViewPrivate {
56
57 public:
58     CredentialWidgetViewPrivate(CredentialWidget *controller)
59         : informativeNoteLabel(0),
60           usernameLabel(0),
61           usernameTextEdit(0),
62           keychainButton(0),
63           usernameHeader(0),
64           passwordTextEdit(0),
65           passwordLabel(0),
66           authFailureOpenLabel(0),
67           authFailureLabel(0),
68           authFailureCloseLabel(0),
69           forgotPasswordLabel(0),
70           rememberPasswordLabel(0),
71           rememberPasswordSwitch(0),
72           captchaLabel(0),
73           captchaImage(0),
74           captchaRefreshButton(0),
75           captchaTextEdit(0),
76           captchaImageClicked(false),
77           signInButton(0),
78           nextButton(0),
79           cancelButton(0),
80           keyChainDialog(0),
81           identity(0),
82           registeredAttributeExtensionId(0),
83           usernameEmtpy(true),
84           passwordEmpty(true)
85     {
86         //layouts and policies init
87         mainLayout = new MLayout(controller);
88         mainLayout->setContentsMargins(0, 0, 0, 0);
89         portraitPolicy = new MGridLayoutPolicy(mainLayout);
90         landscapePolicy = new MGridLayoutPolicy(mainLayout);
91         portraitPolicy->setSpacing(0);
92         landscapePolicy->setSpacing(0);
93
94         passwordLayout = new MLayout(mainLayout);
95         passwordLayoutPolicy = new MLinearLayoutPolicy(passwordLayout, Qt::Horizontal);
96     }
97
98     ~CredentialWidgetViewPrivate()
99     {
100     }
101
102     /*
103      * these parts are common for all three different
104      * variants of accounts-ui dialogs
105      */
106     MLayout *mainLayout;
107
108
109     /*
110      * label for displaying informative texts
111      */
112     MLabel *informativeNoteLabel;
113
114     /*
115      * username specific items
116      */
117     MLabel *usernameLabel;
118     MTextEdit *usernameTextEdit;
119     MButton *keychainButton;
120     AccountsUI::BasicHeaderWidget *usernameHeader;
121
122     /*
123      * password specific items
124      */
125     MTextEdit *passwordTextEdit;
126     MLabel *passwordLabel;
127     MLabel *authFailureOpenLabel;
128     MLabel *authFailureLabel;
129     MLabel *authFailureCloseLabel;
130
131     /*
132      * forgot password specific items
133      */
134     MLabel *forgotPasswordLabel;
135
136     /*
137      * remember me switch specific items
138      */
139     MLabel *rememberPasswordLabel;
140     MButton *rememberPasswordSwitch;
141
142     /*
143      * captcha specific items
144      */
145     MLabel *captchaLabel;
146     CredentialImageWidget *captchaImage;
147     MButton *captchaRefreshButton;
148     MTextEdit *captchaTextEdit;
149     bool captchaImageClicked;
150
151     /*
152      * sign in specific button
153      */
154     MButton *signInButton;
155
156     /*
157      * button for going to the next page without authentication
158      */
159     MButton *nextButton;
160
161     /*
162      * button for cancelation
163      */
164     MButton *cancelButton;
165
166     /*
167      * layout policies
168      */
169     MGridLayoutPolicy *portraitPolicy;
170     MGridLayoutPolicy *landscapePolicy;
171     MLayout *passwordLayout;
172     MLinearLayoutPolicy *passwordLayoutPolicy;
173
174     MDialog *keyChainDialog;
175
176     SignOn::Identity *identity;
177     int registeredAttributeExtensionId;
178     bool usernameEmtpy;
179     bool passwordEmpty;
180
181 public:
182     void destroyAllWidgets();
183     void setVKB(MTextEdit *textEdit);
184 };
185
186 void CredentialWidgetViewPrivate::destroyAllWidgets()
187 {
188     if (informativeNoteLabel) {
189         delete informativeNoteLabel;
190         informativeNoteLabel = NULL;
191     }
192
193     if (usernameLabel) {
194         delete usernameLabel;
195         usernameLabel = NULL;
196     }
197
198     if (usernameTextEdit) {
199         delete usernameTextEdit;
200         usernameTextEdit = NULL;
201     }
202
203     if (keychainButton) {
204         delete keychainButton;
205         keychainButton = NULL;
206     }
207
208     if (usernameHeader) {
209         delete usernameHeader;
210         usernameHeader = NULL;
211     }
212
213     if (passwordLabel) {
214         delete passwordLabel;
215         passwordLabel = NULL;
216     }
217
218     if (authFailureOpenLabel) {
219         delete authFailureOpenLabel;
220         authFailureOpenLabel = NULL;
221     }
222
223     if (authFailureLabel) {
224         delete authFailureLabel;
225         authFailureLabel = NULL;
226     }
227
228     if (authFailureCloseLabel) {
229         delete authFailureCloseLabel;
230         authFailureCloseLabel = NULL;
231     }
232
233     if (passwordTextEdit) {
234         delete passwordTextEdit;
235         passwordTextEdit = NULL;
236     }
237
238     if (forgotPasswordLabel) {
239         delete forgotPasswordLabel;
240         forgotPasswordLabel = NULL;
241     }
242
243     if (rememberPasswordLabel) {
244         delete rememberPasswordLabel;
245         rememberPasswordLabel = NULL;
246     }
247
248     if (rememberPasswordSwitch) {
249         delete rememberPasswordSwitch;
250         rememberPasswordSwitch = NULL;
251     }
252
253     if (captchaLabel) {
254         delete captchaLabel;
255         captchaLabel = NULL;
256     }
257
258     if (captchaImage) {
259         delete captchaImage;
260         captchaImage = NULL;
261     }
262
263     if (captchaRefreshButton) {
264         delete captchaRefreshButton;
265         captchaRefreshButton = NULL;
266     }
267
268     if (captchaTextEdit) {
269         delete captchaTextEdit;
270         captchaTextEdit = NULL;
271     }
272
273     if (signInButton) {
274         delete signInButton;
275         signInButton = NULL;
276     }
277
278     if (nextButton) {
279         delete nextButton;
280         nextButton = NULL;
281     }
282
283     if (cancelButton) {
284         delete cancelButton;
285         cancelButton = NULL;
286     }
287
288     captchaImageClicked = false;
289 }
290
291
292 void CredentialWidgetViewPrivate::setVKB(MTextEdit *textEdit)
293 {
294     if (!textEdit->text().isEmpty()) {
295         MInputMethodState::instance()->setExtendedAttribute(registeredAttributeExtensionId,
296                                                             "/keys",
297                                                             "actionKey",
298                                                             "enabled",
299                                                             QVariant(true));
300         MInputMethodState::instance()->setExtendedAttribute(registeredAttributeExtensionId,
301                                                             "/keys",
302                                                             "actionKey",
303                                                             "highlighted",
304                                                             QVariant(true));
305     } else
306         MInputMethodState::instance()->setExtendedAttribute(registeredAttributeExtensionId,
307                                                             "/keys",
308                                                             "actionKey",
309                                                             "enabled",
310                                                             QVariant(false));
311
312 }
313
314 CredentialWidgetView::CredentialWidgetView(CredentialWidget *controller)
315     : MWidgetView(controller),
316       d_ptr(new CredentialWidgetViewPrivate(controller))
317 {
318     Q_D(CredentialWidgetView);
319     connect(this, SIGNAL(enterCredentialsDone()), controller, SIGNAL(doSignIn()));
320     d->registeredAttributeExtensionId
321             = MInputMethodState::instance()->registerAttributeExtension();
322 }
323
324 CredentialWidgetView::~CredentialWidgetView()
325 {
326 }
327
328 void CredentialWidgetView::usernameTextEditGainedFocus()
329 {
330     Q_D(CredentialWidgetView);
331
332     MInputMethodState::instance()->setExtendedAttribute(d->registeredAttributeExtensionId,
333                                                         "/keys",
334                                                         "actionKey",
335                                                         "label",
336                                                         qtTrId("qtn_comm_next"));
337     MInputMethodState::instance()->setExtendedAttribute(d->registeredAttributeExtensionId,
338                                                         "/keys",
339                                                         "actionKey",
340                                                         "enabled",
341                                                         QVariant(false));
342 }
343
344 void CredentialWidgetView::passwordTextEditGainedFocus()
345 {
346     Q_D(CredentialWidgetView);
347
348     MInputMethodState::instance()->setExtendedAttribute(d->registeredAttributeExtensionId,
349                                                         "/keys",
350                                                         "actionKey",
351                                                         "label",
352                                                         qtTrId("qtn_comm_ok"));
353     MInputMethodState::instance()->setExtendedAttribute(d->registeredAttributeExtensionId,
354                                                         "/keys",
355                                                         "actionKey",
356                                                         "enabled",
357                                                         QVariant(false));
358 }
359
360 void CredentialWidgetView::onUsernameTextChanged()
361 {
362     Q_D(CredentialWidgetView);
363     MTextEdit *textEdit = qobject_cast<MTextEdit *>(sender());
364     d->setVKB(textEdit);
365     if (textEdit->text().isEmpty() && d->passwordEmpty)
366         d->signInButton->setEnabled(false);
367     else
368         d->signInButton->setEnabled(true);
369     d->usernameEmtpy = textEdit->text().isEmpty();
370
371 }
372
373 void CredentialWidgetView::onPasswordTextChanged()
374 {
375     Q_D(CredentialWidgetView);
376     MTextEdit *textEdit = qobject_cast<MTextEdit *>(sender());
377     d->setVKB(textEdit);
378     if (textEdit->text().isEmpty() && d->usernameEmtpy)
379         d->signInButton->setEnabled(false);
380     else
381         d->signInButton->setEnabled(true);
382     d->passwordEmpty = textEdit->text().isEmpty();
383 }
384
385 void CredentialWidgetView::closeVKB()
386 {
387     Q_D(CredentialWidgetView);
388     MInputMethodState::closeSoftwareInputPanel();
389     QGraphicsItem *passwordItem = static_cast<QGraphicsItem *>(d->passwordTextEdit);
390     passwordItem->clearFocus();
391     emit enterCredentialsDone();
392 }
393
394 void CredentialWidgetView::passwordTextEditSetFocus()
395 {
396     Q_D(CredentialWidgetView);
397     QGraphicsItem *passwordItem = static_cast<QGraphicsItem *>(d->passwordTextEdit);
398     if (passwordItem != 0)
399         passwordItem->setFocus();
400 }
401
402 void CredentialWidgetView::launchKeychain()
403 {
404     //TODO
405 }
406
407 void CredentialWidgetView::itemPickedSlot(quint32 credentialsId)
408 {
409     Q_D(CredentialWidgetView);
410
411     //dismiss the keychain
412     d->keyChainDialog->done(0);
413
414     d->passwordTextEdit->blockSignals(true);
415     d->passwordTextEdit->blockSignals(false);
416
417     d->identity = SignOn::Identity::existingIdentity(credentialsId, this);
418     if (d->identity) {
419         connect(d->identity, SIGNAL(info(const SignOn::IdentityInfo&)),
420                 this, SLOT(infoReadySlot(const SignOn::IdentityInfo&)));
421         d->identity->queryInfo();
422     }
423 }
424
425 void CredentialWidgetView::infoReadySlot(const SignOn::IdentityInfo &info)
426 {
427     Q_D(CredentialWidgetView);
428
429     d->usernameTextEdit->blockSignals(true);
430     d->usernameTextEdit->setText(info.userName());
431     d->usernameTextEdit->setCursorPosition(0);
432     d->usernameTextEdit->blockSignals(false);
433 }
434
435 void CredentialWidgetView::recreateWidgets()
436 {
437     Q_D(CredentialWidgetView);
438
439     if (!d->mainLayout->isEmpty()) {
440         for (int i = d->mainLayout->count() - 1; i >= 0; i--)
441             d->mainLayout->removeAt(i);
442     }
443
444     d->destroyAllWidgets();
445
446     if (model()->loginDialogVisible()) {
447
448         if (!model()->informativeNoteText().isEmpty()) {
449             d->informativeNoteLabel = new MLabel(model()->informativeNoteText());
450             d->informativeNoteLabel->setStyleName("CommonBodyTextInverted");
451             d->informativeNoteLabel->setWordWrap(true);
452             d->informativeNoteLabel->setWrapMode(QTextOption::WordWrap);
453             d->informativeNoteLabel->setAlignment(Qt::AlignCenter);
454         }
455
456         if (!model()->usernameEditable()) {
457
458             d->usernameHeader = new AccountsUI::BasicHeaderWidget(AccountsUI::BasicHeaderWidget::IconWithTitleAndSubTitle);
459             d->usernameHeader->createLayout();
460             //Add icon here
461             if (!model()->serviceIcon().isEmpty()) {
462                 d->usernameHeader->setImage(model()->serviceIcon());
463             } else {
464                 //empty icon
465                 d->usernameHeader->setImage("icon-l-default-application");
466             }
467             //% "Username"
468             if (!model()->displayName().isEmpty() && model()->confirmPassword()) {
469                 d->usernameHeader->setTitle(model()->displayName());
470                 d->usernameHeader->setSubtitle(model()->username());
471             } else {
472                 d->usernameHeader->setTitle(model()->username());
473             }
474
475         } else {
476            //% "Username"
477            d->usernameLabel = new MLabel(qtTrId("qtn_acc_login_username"));
478            d->usernameLabel->setStyleName("CommonFieldLabelInverted");
479
480            d->usernameTextEdit = new MTextEdit();
481            d->usernameTextEdit->setStyleName("CommonSingleInputFieldLabeledInverted");
482            d->usernameTextEdit->setContentType(M::EmailContentType); // TO DO url type needs to be supported if we do OpenId
483            d->usernameTextEdit->setCursorPosition(0);
484            d->usernameTextEdit->attachToolbar(d->registeredAttributeExtensionId);
485            connect(d->usernameTextEdit, SIGNAL(returnPressed()),
486                    this, SLOT(passwordTextEditSetFocus()));
487         }
488
489         d->passwordTextEdit = new MTextEdit();
490         d->passwordTextEdit->setStyleName("CommonSingleInputFieldLabeledInverted");
491         d->passwordTextEdit->setEchoMode(MTextEditModel::Password);
492         d->passwordTextEdit->setCursorPosition(0);
493         d->passwordTextEdit->attachToolbar(d->registeredAttributeExtensionId);
494         connect(d->passwordTextEdit, SIGNAL(returnPressed()),
495                 this, SLOT(closeVKB()));
496         //% "Password"
497         d->passwordLabel = new MLabel(qtTrId("qtn_acc_login_password"));
498         d->passwordLabel->setStyleName("CommonFieldLabelInverted");
499         d->passwordLayoutPolicy->addItem(d->passwordLabel);
500
501         if (model()->authenticationFailed()) {
502             d->passwordTextEdit->setErrorHighlight(true);
503             d->authFailureOpenLabel = new MLabel(QLatin1String("("));
504             d->authFailureOpenLabel->setStyleName("WarningLabel");
505             //% "authentication failed"
506             d->authFailureLabel = new MLabel(qtTrId("qtn_acc_auth_failed_infobanner"));
507             d->authFailureLabel->setTextElide(true);
508             d->authFailureLabel->setStyleName("WarningLabel");
509             d->authFailureLabel->setWrapMode(QTextOption::WordWrap);
510             d->authFailureCloseLabel = new MLabel(QLatin1String(")"));
511             d->authFailureCloseLabel->setStyleName("WarningLabel");
512
513             d->passwordLayoutPolicy->addItem(d->authFailureOpenLabel);
514             d->passwordLayoutPolicy->addItem(d->authFailureLabel);
515             d->passwordLayoutPolicy->addItem(d->authFailureCloseLabel);
516         }
517
518         if (model()->focusOnPasswordField())
519             setFocusOnPasswordField();
520
521         if (model()->forgotPassword().isEmpty()) {
522             //% "Forgot my password"
523             QString link("<a href=\"%1\"> " + qtTrId("qtn_acc_forgot_password") + "! </a>");
524             link = link.left(link.indexOf(QChar(BinaryTextVariantSeparator)));
525             d->forgotPasswordLabel = new MLabel(link.arg(model()->forgotPasswordUrl()));
526         } else {
527             d->forgotPasswordLabel = new MLabel(model()->forgotPassword());
528             d->forgotPasswordLabel->setWordWrap(true);
529             d->forgotPasswordLabel->setWrapMode(QTextOption::WordWrap);
530         }
531         d->forgotPasswordLabel->setStyleName("CommonFieldLabelInverted");
532         d->forgotPasswordLabel->setTextFormat(Qt::RichText);
533         d->forgotPasswordLabel->setAlignment(Qt::AlignLeft);
534         d->forgotPasswordLabel->setStyleName("CommonFieldLabelInverted");
535         d->forgotPasswordLabel->setObjectName("wgForgotPasswordLabel");
536
537         //% "Remember my password"
538         d->rememberPasswordLabel = new MLabel(qtTrId("qtn_acc_login_remember_pw"));
539         d->rememberPasswordLabel->setStyleName("CommonFieldLabelInverted");
540
541         d->rememberPasswordSwitch = new MButton();
542         d->rememberPasswordSwitch->setObjectName("wgRememberPasswordSwitch");
543         d->rememberPasswordSwitch->setStyleName("CommonSwitchInverted");
544         d->rememberPasswordSwitch->setViewType(MButton::switchType);
545         d->rememberPasswordSwitch->setCheckable(true);
546
547         if (d->usernameTextEdit) {
548             d->usernameTextEdit->setReadOnly(!model()->usernameEditable());
549             d->usernameTextEdit->setText(model()->username());
550         }
551         d->passwordTextEdit->setText(model()->password());
552
553         connect(d->passwordTextEdit, SIGNAL(gainedFocus(Qt::FocusReason)),
554                 this, SLOT(passwordTextEditGainedFocus()));
555
556         connect(d->passwordTextEdit, SIGNAL(lostFocus(Qt::FocusReason)),
557                 this, SLOT(refreshPasswordInModel()));
558
559         connect(d->usernameTextEdit, SIGNAL(lostFocus(Qt::FocusReason)),
560                 this, SLOT(refreshUsernameInModel()));
561
562         connect(d->usernameTextEdit, SIGNAL(gainedFocus (Qt::FocusReason)),
563                 this, SLOT(usernameTextEditGainedFocus()));
564         connect(d->usernameTextEdit, SIGNAL(textChanged()),
565                 this, SLOT(onUsernameTextChanged()));
566         connect(d->passwordTextEdit, SIGNAL(textChanged()),
567                 this, SLOT(onPasswordTextChanged()));
568
569         connect(d->forgotPasswordLabel, SIGNAL(linkActivated(QString)),
570                 this, SLOT(forgotPasswordClicked(QString)));
571
572         if (model()->checkboxVisible()) {
573             d->rememberPasswordSwitch->setChecked(model()->checkboxPressed());
574             connect(d->rememberPasswordSwitch, SIGNAL(toggled(bool)),
575                     this, SLOT(refreshCheckboxStateInModel(bool)));
576         }
577     }
578
579     if (model()->captchaDialogVisible()) {
580         //% "Enter characters you see in the picture below"
581         d->captchaLabel = new MLabel(qtTrId("qtn_acc_login_captcha_info"));
582         d->captchaLabel->setStyleName("CommonFieldLabelInverted");
583
584         d->captchaImage = new CredentialImageWidget;
585         d->captchaImage->setStyleName("wgCaptchaImage");
586
587         //TODO: change after specs clarification
588         QSize size(365, 70);
589         d->captchaImage->setImage(model()->captcha().scaled(size, Qt::IgnoreAspectRatio));
590
591         d->captchaRefreshButton = new MButton();
592         d->captchaRefreshButton->setStyleName("CommonSingleButtonInverted");
593         d->captchaRefreshButton->setIconID("icon-m-toolbar-refresh");
594
595         d->captchaTextEdit = new MTextEdit();
596         d->captchaTextEdit->setStyleName("CommonSingleInputFieldLabeledInverted");
597         d->captchaTextEdit->setText(model()->captchaText());
598
599         connect(d->captchaImage, SIGNAL(widgetClicked()),
600                 this, SLOT(captchaImageClicked()));
601         connect(d->captchaTextEdit, SIGNAL(textChanged()),
602                 this, SLOT(refreshCaptchaTextInModel()));
603     }
604
605     if (model()->signInButtonVisible()) {
606         Q_ASSERT(d->signInButton == 0);
607         if (model()->confirmPassword()) {
608             //% "Continue"
609             d->signInButton = new MButton(qtTrId("qtn_comm_command_continue"));
610         } else {
611             //% "Sign In"
612             d->signInButton = new MButton(qtTrId("qtn_acc_auth_dial_button"));
613         }
614         d->signInButton->setStyleName("CommonSingleButtonInverted");
615         d->signInButton->setObjectName("wgSignInButton");
616
617         connect(d->signInButton, SIGNAL(clicked()),
618                         model(), SIGNAL(signInClicked()));
619     }
620
621     if (model()->nextButtonVisible()) {
622         Q_ASSERT(d->nextButton == 0);
623         //% "Next"
624         d->nextButton = new MButton(qtTrId("qtn_acc_next_button"));
625         d->nextButton->setStyleName("CommonSingleButtonInverted");
626
627         connect(d->nextButton, SIGNAL(clicked()),
628                         model(), SIGNAL(nextClicked()));
629     }
630
631     if (model()->cancelButtonVisible()) {
632         Q_ASSERT(d->cancelButton == 0);
633         //% "Cancel"
634         d->cancelButton = new MButton(qtTrId("qtn_comm_cancel"));
635         d->cancelButton->setStyleName("CommonSingleButtonInverted");
636
637         connect(d->cancelButton, SIGNAL(clicked()),
638                         model(), SIGNAL(cancelClicked()));
639     }
640
641     d->captchaImageClicked = false;
642     setEnabled(model()->enabled());
643     d->signInButton->setEnabled(false);
644
645 }
646
647 void CredentialWidgetView::setupModel()
648 {
649     MWidgetView::setupModel();
650
651     recreateWidgets();
652     updateMainLayout();
653 }
654
655 void CredentialWidgetView::applyStyle()
656 {
657     MWidgetView::applyStyle();
658 }
659
660 void CredentialWidgetView::updateData(const QList <const char *> &modifications)
661 {
662     Q_D(CredentialWidgetView);
663
664     MWidgetView::updateData(modifications);
665     const char *member;
666
667     foreach(member, modifications) {
668         if (member == CredentialWidgetModel::Username) {
669             if (d->usernameTextEdit)
670                 d->usernameTextEdit->setText(model()->username());
671         }
672         else if (member == CredentialWidgetModel::UsernameEditable) {
673             if (d->usernameTextEdit)
674                 d->usernameTextEdit->setReadOnly(model()->usernameEditable());
675         }
676         else if (member == CredentialWidgetModel::Password) {
677             if (d->passwordTextEdit)
678                 d->passwordTextEdit->setText(model()->password());
679         }
680         else if (member == CredentialWidgetModel::FocusOnPasswordField) {
681             if (d->passwordTextEdit)
682                 d->passwordTextEdit->setFocus(Qt::ActiveWindowFocusReason);
683         }
684         else if (member == CredentialWidgetModel::CheckboxVisible) {
685             if (d->rememberPasswordSwitch)
686                 d->rememberPasswordSwitch->show();
687         }
688         else if (member == CredentialWidgetModel::CheckboxPressed) {
689             if (d->rememberPasswordSwitch)
690                 d->rememberPasswordSwitch->setChecked(model()->checkboxPressed());
691         }
692         else if (member == CredentialWidgetModel::Captcha) {
693             if (d->captchaImage) {
694                 //TODO: cahnge after specs clarification
695                 QSize size(200, 80);
696                 d->captchaImage->setImage(model()->captcha().scaled(size, Qt::IgnoreAspectRatio));
697                 d->captchaImageClicked = false;
698             }
699         }
700         else if (member == CredentialWidgetModel::CaptchaText) {
701             if (d->captchaTextEdit)
702                 d->captchaTextEdit->setText(model()->captchaText());
703         }
704         else if (member == CredentialWidgetModel::FocusOnCaptchaTextField) {
705             if (d->captchaTextEdit)
706                 d->captchaTextEdit->setFocus(Qt::ActiveWindowFocusReason);
707         }
708         else if (member == CredentialWidgetModel::Enabled) {
709             setEnabled(model()->enabled());
710         }
711         else if (member == CredentialWidgetModel::InformativeNoteText) {
712             if (d->informativeNoteLabel)
713                 d->informativeNoteLabel->setText(model()->informativeNoteText());
714         }
715         else if (member == CredentialWidgetModel::UsernameDisplayString) {
716             d->usernameLabel->setText(qtTrId(model()->usernameDisplayString().toLatin1()));
717         }
718     }
719 }
720
721 void CredentialWidgetView::setEnabled(bool isWidgetEnabled)
722 {
723     Q_D(CredentialWidgetView);
724
725     if (d->informativeNoteLabel)
726         d->informativeNoteLabel->setEnabled(isWidgetEnabled);
727
728     if (d->usernameLabel)
729         d->usernameLabel->setEnabled(isWidgetEnabled);
730
731     if (d->usernameTextEdit)
732         d->usernameTextEdit->setEnabled(isWidgetEnabled);
733
734     if (d->keychainButton)
735         d->keychainButton->setEnabled(isWidgetEnabled);
736
737     if (d->usernameHeader)
738         d->usernameHeader->setEnabled(isWidgetEnabled);
739
740     if (d->passwordLabel)
741         d->passwordLabel->setEnabled(isWidgetEnabled);
742
743     if (d->authFailureOpenLabel)
744         d->authFailureOpenLabel->setEnabled(isWidgetEnabled);
745
746     if (d->authFailureLabel)
747         d->authFailureLabel->setEnabled(isWidgetEnabled);
748
749     if (d->authFailureCloseLabel)
750         d->authFailureCloseLabel->setEnabled(isWidgetEnabled);
751
752     if (d->passwordTextEdit)
753         d->passwordTextEdit->setEnabled(isWidgetEnabled);
754
755     if (d->forgotPasswordLabel)
756         d->forgotPasswordLabel->setEnabled(isWidgetEnabled);
757
758     if (d->rememberPasswordLabel)
759         d->rememberPasswordLabel->setEnabled(isWidgetEnabled);
760
761     if (d->rememberPasswordSwitch)
762         d->rememberPasswordSwitch->setEnabled(isWidgetEnabled);
763
764     if (d->captchaLabel)
765         d->captchaLabel->setEnabled(isWidgetEnabled);
766
767     if (d->captchaImage)
768         d->captchaImage->setEnabled(isWidgetEnabled);
769
770     if (d->captchaRefreshButton)
771         d->captchaRefreshButton->setEnabled(isWidgetEnabled);
772
773     if (d->captchaTextEdit)
774         d->captchaTextEdit->setEnabled(isWidgetEnabled);
775
776     if (d->signInButton)
777         d->signInButton->setEnabled(isWidgetEnabled);
778
779     if (d->nextButton)
780         d->nextButton->setEnabled(isWidgetEnabled);
781
782     if (d->cancelButton)
783         d->cancelButton->setEnabled(isWidgetEnabled);
784 }
785
786 void CredentialWidgetView::refreshUsernameInModel()
787 {
788     Q_D(CredentialWidgetView);
789     model()->blockSignals(true);
790     model()->setUsername(d->usernameTextEdit->text());
791     model()->blockSignals(false);
792 }
793
794 void CredentialWidgetView::refreshPasswordInModel()
795 {
796     Q_D(CredentialWidgetView);
797     model()->blockSignals(true);
798     model()->setPassword(d->passwordTextEdit->text());
799     model()->blockSignals(false);
800 }
801
802 void CredentialWidgetView::refreshCaptchaTextInModel()
803 {
804     Q_D(CredentialWidgetView);
805     model()->blockSignals(true);
806     model()->setCaptchaText(d->captchaTextEdit->text());
807     model()->blockSignals(false);
808 }
809
810 void CredentialWidgetView::refreshCheckboxStateInModel(bool checked)
811 {
812     model()->setCheckboxPressed(checked);
813 }
814
815 void CredentialWidgetView::captchaImageClicked()
816 {
817     Q_D(CredentialWidgetView);
818
819     if (!d->captchaImageClicked && d->captchaImage) {
820         d->captchaImageClicked = true;
821         model()->captchaClick();
822     }
823 }
824
825 void CredentialWidgetView::forgotPasswordClicked(QString link)
826 {
827     model()->forgotPasswordClick(link);
828 }
829
830 void CredentialWidgetView::configureWithCaptcha(int &portraitRow, int &landscapeRow)
831 {
832     Q_D(CredentialWidgetView);
833
834     int row = portraitRow;
835     //portrait mode
836     d->portraitPolicy->addItem(d->captchaLabel, row, 0, 1, 2);
837     row++;
838     d->portraitPolicy->addItem(d->captchaImage, row, 0, Qt::AlignLeft);
839     d->portraitPolicy->addItem(d->captchaRefreshButton, row, 1, Qt::AlignRight);
840     row++;
841     d->portraitPolicy->addItem(d->captchaTextEdit, row, 0, 1, 2);
842     row++;
843     portraitRow = row;
844
845     row = landscapeRow;
846     //lanscape mode
847     d->landscapePolicy->addItem(d->captchaLabel, row, 0, 1, 4);
848     row++;
849     d->landscapePolicy->addItem(d->captchaImage, row, 0, 1, 1, Qt::AlignLeft);
850     d->landscapePolicy->addItem(d->captchaRefreshButton, row, 3, 1, 1, Qt::AlignRight);
851     row++;
852     d->landscapePolicy->addItem(d->captchaTextEdit, row, 0, 1, 4);
853     row++;
854     landscapeRow = row;
855 }
856
857 void CredentialWidgetView::configureWithLogin(int &portraitRow, int &landscapeRow)
858 {
859     Q_D(CredentialWidgetView);
860
861     int row = portraitRow;
862     //portrait mode
863     if (!model()->usernameEditable()) {
864         d->portraitPolicy->addItem(d->usernameHeader, row , 0, Qt::AlignLeft);
865        row++;
866     } else {
867        d->portraitPolicy->addItem(d->usernameLabel, row, 0);
868        d->portraitPolicy->setRowSpacing(row, 0);
869        row++;
870        d->portraitPolicy->addItem(d->usernameTextEdit, row, 0, 1, 2);
871        row++;
872     }
873     d->portraitPolicy->addItem(d->passwordLayout, row, 0, 1, 2, Qt::AlignLeft);
874     d->portraitPolicy->setRowSpacing(row, 0);
875     row++;
876     d->portraitPolicy->addItem(d->passwordTextEdit, row, 0, 1, 2);
877     row++;
878
879     if (model()->forgotPasswordVisible()) {
880         d->portraitPolicy->addItem(d->forgotPasswordLabel, row, 0, 1, 2, Qt::AlignLeft);
881         d->landscapePolicy->setRowAlignment(row, Qt::AlignLeft);
882         row++;
883     }
884
885     if (model()->checkboxVisible()) {
886         d->portraitPolicy->addItem(d->rememberPasswordLabel, row, 0);
887         d->portraitPolicy->addItem(d->rememberPasswordSwitch, row, 1);
888         row++;
889     }
890     portraitRow = row;
891
892     row = landscapeRow;
893     //landscape mode
894     d->landscapePolicy->addItem(d->usernameLabel, row, 0);
895     row++;
896     d->landscapePolicy->addItem(d->usernameTextEdit, row, 0);
897     row--;
898     d->landscapePolicy->addItem(d->passwordLayout, row, 1);
899     row++;
900     d->landscapePolicy->addItem(d->passwordTextEdit, row, 1);
901     row++;
902
903     if (model()->forgotPasswordVisible()) {
904         d->landscapePolicy->addItem(d->forgotPasswordLabel, row, 0, 1, 2, Qt::AlignLeft);
905         d->landscapePolicy->setRowAlignment(row, Qt::AlignLeft);
906         row++;
907     }
908
909     if (model()->checkboxVisible()) {
910         d->landscapePolicy->addItem(d->rememberPasswordLabel, row, 0, 1, 3);
911         d->landscapePolicy->addItem(d->rememberPasswordSwitch, row, 3, 1, 1, Qt::AlignRight);
912         row++;
913     }
914
915     landscapeRow = row;
916 }
917
918 void CredentialWidgetView::configureWithButtons(int &portraitRow, int &landscapeRow)
919 {
920     Q_D(CredentialWidgetView);
921
922     int row = portraitRow;
923     //portrait mode
924     if (model()->signInButtonVisible()) {
925         d->portraitPolicy->addItem(d->signInButton, row, 0, 1, 2, Qt::AlignHCenter);
926         row++;
927     }
928     if (model()->cancelButtonVisible()) {
929         d->portraitPolicy->addItem(d->cancelButton, row, 0, 1, 2, Qt::AlignHCenter);
930         row++;
931     }
932     if (model()->nextButtonVisible()) {
933         d->portraitPolicy->addItem(d->nextButton, row, 0, 1, 2, Qt::AlignHCenter);
934         row++;
935     }
936     portraitRow = row;
937
938     row = landscapeRow;
939     //landscape mode
940     if (model()->signInButtonVisible()) {
941         d->landscapePolicy->addItem(d->signInButton, row, 0, 1, 2, Qt::AlignHCenter);
942         row++;
943     }
944     if (model()->cancelButtonVisible()) {
945         d->landscapePolicy->addItem(d->cancelButton, row, 0, 1, 2, Qt::AlignHCenter);
946         row++;
947     }
948     if (model()->nextButtonVisible()) {
949         d->landscapePolicy->addItem(d->nextButton, row, 0, 1, 2, Qt::AlignHCenter);
950         row++;
951     }
952     landscapeRow = row;
953 }
954
955 void CredentialWidgetView::configureWithInfoLabel(int &portraitRow, int &landscapeRow)
956 {
957     Q_D(CredentialWidgetView);
958
959     int row = portraitRow;
960     //portrait mode
961     d->portraitPolicy->addItem(d->informativeNoteLabel, row, 0, 1, 2);
962     row++;
963     portraitRow = row;
964
965     row = landscapeRow;
966     //landscape mode
967     d->landscapePolicy->addItem(d->informativeNoteLabel, row, 0, 1, 2);
968     d->landscapePolicy->setRowAlignment(row, Qt::AlignHCenter);
969     row++;
970     landscapeRow = row;
971 }
972
973 void CredentialWidgetView::updateMainLayout()
974 {
975     Q_D(CredentialWidgetView);
976
977     int portraitRow = 0;
978     int landscapeRow = 0;
979
980     configureWithInfoLabel(portraitRow, landscapeRow);
981     if (model()->loginDialogVisible())
982         configureWithLogin(portraitRow, landscapeRow);
983     if (model()->captchaDialogVisible())
984         configureWithCaptcha(portraitRow, landscapeRow);
985     configureWithButtons(portraitRow, landscapeRow);
986
987     d->landscapePolicy->setColumnStretchFactor(0, 1);
988     d->landscapePolicy->setColumnStretchFactor(1, 1);
989
990     d->portraitPolicy->setColumnStretchFactor(0, 99);
991     d->portraitPolicy->setColumnStretchFactor(1, 1);
992
993     d->mainLayout->setPortraitPolicy(d->portraitPolicy);
994
995     if(model()->forcedPortraitMode())
996         d->mainLayout->setLandscapePolicy(d->portraitPolicy);
997     else
998         d->mainLayout->setLandscapePolicy(d->landscapePolicy);
999 }
1000
1001 void CredentialWidgetView::setFocusOnUserNameField()
1002 {
1003     Q_D(CredentialWidgetView);
1004     if (d->usernameTextEdit)
1005         d->usernameTextEdit->setFocus(Qt::ActiveWindowFocusReason);
1006 }
1007
1008 void CredentialWidgetView::setFocusOnPasswordField()
1009 {
1010     Q_D(CredentialWidgetView);
1011     if (d->passwordTextEdit)
1012         d->passwordTextEdit->setFocus(Qt::ActiveWindowFocusReason);
1013 }
1014
1015 M_REGISTER_VIEW_NEW(CredentialWidgetView, CredentialWidget);
1016