From d2d65a82c839e4596b5f303c3a9f18aec8450930 Mon Sep 17 00:00:00 2001 From: leavengood Date: Sat, 11 Jun 2011 14:01:03 +0000 Subject: [PATCH] Fix #7672 by applying humdinger's patch then adding more: The server name is checked while typing and if it is not valid the text is made red. Invalid entries cannot be added to the list. Also made the server add message be the invoking message so that the enter key works to add a server. After adding a valid server the text is cleared. Valid servers names are alpha-numeric with periods, dashes and underscores. Could need some things added but that should cover 99% of the cases. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42097 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/time/NetworkTimeView.cpp | 40 +++++++++++++++++++++++- src/preferences/time/NetworkTimeView.h | 2 ++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/preferences/time/NetworkTimeView.cpp b/src/preferences/time/NetworkTimeView.cpp index 445918ca6c..5a9b8aed9e 100644 --- a/src/preferences/time/NetworkTimeView.cpp +++ b/src/preferences/time/NetworkTimeView.cpp @@ -9,6 +9,7 @@ #include "NetworkTimeView.h" +#include #include #include @@ -280,9 +281,26 @@ NetworkTimeView::MessageReceived(BMessage* message) } break; } + case kMsgServerEdited: + { + rgb_color defaultColor = ui_color(B_CONTROL_TEXT_COLOR); + rgb_color red = {255, 0, 0}; + int32 length = fServerTextControl->TextView()->TextLength(); + + if (_IsValidServerName(fServerTextControl->TextView()->Text())) + fServerTextControl->TextView()->SetFontAndColor(0, length, NULL, 0, &defaultColor); + else + fServerTextControl->TextView()->SetFontAndColor(0, length, NULL, 0, &red); + + break; + } case kMsgAddServer: + if (!_IsValidServerName(fServerTextControl->TextView()->Text())) + break; + fSettings.AddServer(fServerTextControl->Text()); _UpdateServerList(); + fServerTextControl->SetText(""); Looper()->PostMessage(new BMessage(kMsgChange)); break; @@ -377,6 +395,7 @@ NetworkTimeView::MessageReceived(BMessage* message) void NetworkTimeView::AttachedToWindow() { + fServerTextControl->SetTarget(this); fServerListView->SetTarget(this); fAddButton->SetTarget(this); fRemoveButton->SetTarget(this); @@ -393,10 +412,12 @@ NetworkTimeView::CheckCanRevert() return fSettings.SettingsChanged(); } + void NetworkTimeView::_InitView() { - fServerTextControl = new BTextControl(NULL, NULL, NULL); + fServerTextControl = new BTextControl(NULL, NULL, new BMessage(kMsgAddServer)); + fServerTextControl->SetModificationMessage(new BMessage(kMsgServerEdited)); fAddButton = new BButton("add", B_TRANSLATE("Add"), new BMessage(kMsgAddServer)); @@ -475,6 +496,23 @@ NetworkTimeView::_DoneSynchronizing() } +bool +NetworkTimeView::_IsValidServerName(const char * serverName) +{ + if (serverName[0] == '\0') + return false; + + for (int32 i = 0; serverName[i] != '\0'; i++) { + char c = serverName[i]; + // Simple URL validation, no scheme should be present + if (!(isalnum(c) || c == '.' || c == '-' || c == '_')) + return false; + } + + return true; +} + + status_t update_time(const Settings& settings, const char** errorString, int32* errorCode) diff --git a/src/preferences/time/NetworkTimeView.h b/src/preferences/time/NetworkTimeView.h index 04950f350b..8636d3199b 100644 --- a/src/preferences/time/NetworkTimeView.h +++ b/src/preferences/time/NetworkTimeView.h @@ -25,6 +25,7 @@ class Settings; static const uint32 kMsgNetworkTimeSettings = 'ntst'; static const uint32 kMsgSetDefaultServer = 'setd'; +static const uint32 kMsgServerEdited = 'nsed'; static const uint32 kMsgAddServer = 'asrv'; static const uint32 kMsgRemoveServer = 'rsrv'; static const uint32 kMsgResetServerList = 'rstl'; @@ -92,6 +93,7 @@ private: void _InitView(); void _UpdateServerList(); void _DoneSynchronizing(); + bool _IsValidServerName(const char * serverName); Settings fSettings;