From 3e3d0effdc47debfd583da23fbb4000e453d9bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 28 Sep 2014 00:29:50 +0200 Subject: [PATCH] HaikuDepot: Limit language selection for account creation For the time being, there is a limited amount of supported languages when creating accounts, reflect this in the UI and use a BMenuField. Creating accounts with other languages fails, the error reporting was not ideal and could be improved. The server responded with "object not found", which was reported in the alert, but the response actually contains the object which was not found (NaturalLanguageCode). --- src/apps/haikudepot/ui/UserLoginWindow.cpp | 45 ++++++++++++++++++++-- src/apps/haikudepot/ui/UserLoginWindow.h | 4 +- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/apps/haikudepot/ui/UserLoginWindow.cpp b/src/apps/haikudepot/ui/UserLoginWindow.cpp index bb47b532fb..a26ac966a3 100644 --- a/src/apps/haikudepot/ui/UserLoginWindow.cpp +++ b/src/apps/haikudepot/ui/UserLoginWindow.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -31,6 +33,7 @@ enum { MSG_SEND = 'send', MSG_TAB_SELECTED = 'tbsl', MSG_CAPTCHA_OBTAINED = 'cpob', + MSG_LANGUAGE_SELECTED = 'lngs', }; @@ -59,12 +62,26 @@ private: }; +static void +add_languages_to_menu(const StringList& languages, BMenu* menu) +{ + for (int i = 0; i < languages.CountItems(); i++) { + const BString& language = languages.ItemAtFast(i); + BMessage* message = new BMessage(MSG_LANGUAGE_SELECTED); + message->AddString("code", language); + BMenuItem* item = new BMenuItem(language, message); + menu->AddItem(item); + } +} + + UserLoginWindow::UserLoginWindow(BWindow* parent, BRect frame, Model& model) : BWindow(frame, B_TRANSLATE_SYSTEM_NAME("Log in"), B_FLOATING_WINDOW_LOOK, B_FLOATING_SUBSET_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_RESIZABLE | B_NOT_ZOOMABLE), + fPreferredLanguage(model.PreferredLanguage()), fModel(model), fMode(NONE), fWorkerThread(-1) @@ -73,14 +90,30 @@ UserLoginWindow::UserLoginWindow(BWindow* parent, BRect frame, Model& model) fUsernameField = new BTextControl(B_TRANSLATE("User name:"), "", NULL); fPasswordField = new BTextControl(B_TRANSLATE("Pass phrase:"), "", NULL); + fPasswordField->TextView()->HideTyping(true); fNewUsernameField = new BTextControl(B_TRANSLATE("User name:"), "", NULL); fNewPasswordField = new BTextControl(B_TRANSLATE("Pass phrase:"), "", NULL); + fNewPasswordField->TextView()->HideTyping(true); fRepeatPasswordField = new BTextControl(B_TRANSLATE("Repeat pass phrase:"), "", NULL); - fLanguageCodeField = new BTextControl(B_TRANSLATE("Language code:"), - model.PreferredLanguage(), NULL); + fRepeatPasswordField->TextView()->HideTyping(true); + + // Construct languages popup + BPopUpMenu* languagesMenu = new BPopUpMenu(B_TRANSLATE("Language")); + fLanguageCodeField = new BMenuField("language", + B_TRANSLATE("Preferred language:"), languagesMenu); + + add_languages_to_menu(fModel.SupportedLanguages(), languagesMenu); + languagesMenu->SetTargetForItems(this); + + BMenuItem* defaultItem = languagesMenu->ItemAt( + fModel.SupportedLanguages().IndexOf(fPreferredLanguage)); + if (defaultItem != NULL) + defaultItem->SetMarked(true); + + fEmailField = new BTextControl(B_TRANSLATE("Email address:"), "", NULL); fCaptchaView = new BitmapView("captcha view"); fCaptchaResultField = new BTextControl("", "", NULL); @@ -104,7 +137,7 @@ UserLoginWindow::UserLoginWindow(BWindow* parent, BRect frame, Model& model) .AddTextControl(fNewPasswordField, 0, 1) .AddTextControl(fRepeatPasswordField, 0, 2) .AddTextControl(fEmailField, 0, 3) - .AddTextControl(fLanguageCodeField, 0, 4) + .AddMenuField(fLanguageCodeField, 0, 4) .Add(fCaptchaView, 0, 5) .Add(fCaptchaResultField, 1, 5) @@ -190,6 +223,10 @@ UserLoginWindow::MessageReceived(BMessage* message) } break; + case MSG_LANGUAGE_SELECTED: + message->FindString("code", &fPreferredLanguage); + break; + default: BWindow::MessageReceived(message); break; @@ -448,7 +485,7 @@ UserLoginWindow::_CreateAccountThread() BString email(fEmailField->Text()); BString captchaToken(fCaptchaToken); BString captchaResponse(fCaptchaResultField->Text()); - BString languageCode(fLanguageCodeField->Text()); + BString languageCode(fPreferredLanguage); Unlock(); diff --git a/src/apps/haikudepot/ui/UserLoginWindow.h b/src/apps/haikudepot/ui/UserLoginWindow.h index a1788816c9..66f0e53b19 100644 --- a/src/apps/haikudepot/ui/UserLoginWindow.h +++ b/src/apps/haikudepot/ui/UserLoginWindow.h @@ -12,6 +12,7 @@ class BButton; +class BMenuField; class BTabView; class BTextControl; class BitmapView; @@ -63,7 +64,7 @@ private: BTextControl* fNewPasswordField; BTextControl* fRepeatPasswordField; BTextControl* fEmailField; - BTextControl* fLanguageCodeField; + BMenuField* fLanguageCodeField; BitmapView* fCaptchaView; BTextControl* fCaptchaResultField; @@ -72,6 +73,7 @@ private: BString fCaptchaToken; BitmapRef fCaptchaImage; + BString fPreferredLanguage; Model& fModel;