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
This commit is contained in:
leavengood 2011-06-11 14:01:03 +00:00
parent ea71d6eb4f
commit d2d65a82c8
2 changed files with 41 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "NetworkTimeView.h" #include "NetworkTimeView.h"
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -280,9 +281,26 @@ NetworkTimeView::MessageReceived(BMessage* message)
} }
break; 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: case kMsgAddServer:
if (!_IsValidServerName(fServerTextControl->TextView()->Text()))
break;
fSettings.AddServer(fServerTextControl->Text()); fSettings.AddServer(fServerTextControl->Text());
_UpdateServerList(); _UpdateServerList();
fServerTextControl->SetText("");
Looper()->PostMessage(new BMessage(kMsgChange)); Looper()->PostMessage(new BMessage(kMsgChange));
break; break;
@ -377,6 +395,7 @@ NetworkTimeView::MessageReceived(BMessage* message)
void void
NetworkTimeView::AttachedToWindow() NetworkTimeView::AttachedToWindow()
{ {
fServerTextControl->SetTarget(this);
fServerListView->SetTarget(this); fServerListView->SetTarget(this);
fAddButton->SetTarget(this); fAddButton->SetTarget(this);
fRemoveButton->SetTarget(this); fRemoveButton->SetTarget(this);
@ -393,10 +412,12 @@ NetworkTimeView::CheckCanRevert()
return fSettings.SettingsChanged(); return fSettings.SettingsChanged();
} }
void void
NetworkTimeView::_InitView() 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"), fAddButton = new BButton("add", B_TRANSLATE("Add"),
new BMessage(kMsgAddServer)); 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 status_t
update_time(const Settings& settings, const char** errorString, update_time(const Settings& settings, const char** errorString,
int32* errorCode) int32* errorCode)

View File

@ -25,6 +25,7 @@ class Settings;
static const uint32 kMsgNetworkTimeSettings = 'ntst'; static const uint32 kMsgNetworkTimeSettings = 'ntst';
static const uint32 kMsgSetDefaultServer = 'setd'; static const uint32 kMsgSetDefaultServer = 'setd';
static const uint32 kMsgServerEdited = 'nsed';
static const uint32 kMsgAddServer = 'asrv'; static const uint32 kMsgAddServer = 'asrv';
static const uint32 kMsgRemoveServer = 'rsrv'; static const uint32 kMsgRemoveServer = 'rsrv';
static const uint32 kMsgResetServerList = 'rstl'; static const uint32 kMsgResetServerList = 'rstl';
@ -92,6 +93,7 @@ private:
void _InitView(); void _InitView();
void _UpdateServerList(); void _UpdateServerList();
void _DoneSynchronizing(); void _DoneSynchronizing();
bool _IsValidServerName(const char * serverName);
Settings fSettings; Settings fSettings;