Time: Style fixes to NetworkTimeView

This commit is contained in:
John Scipione
2014-03-26 17:21:00 -04:00
parent 39a1e74b08
commit 14d7522197
2 changed files with 60 additions and 46 deletions
+38 -24
View File
@@ -7,6 +7,7 @@
* Axel Dörfler <[email protected]> * Axel Dörfler <[email protected]>
*/ */
#include "NetworkTimeView.h" #include "NetworkTimeView.h"
#include <ctype.h> #include <ctype.h>
@@ -173,6 +174,7 @@ Settings::SettingsChanged()
char* oldBytes = new (std::nothrow) char[oldSize]; char* oldBytes = new (std::nothrow) char[oldSize];
if (oldBytes == NULL) if (oldBytes == NULL)
return true; return true;
fOldMessage.Flatten(oldBytes, oldSize); fOldMessage.Flatten(oldBytes, oldSize);
char* newBytes = new (std::nothrow) char[newSize]; char* newBytes = new (std::nothrow) char[newSize];
if (newBytes == NULL) { if (newBytes == NULL) {
@@ -239,10 +241,12 @@ int32
Settings::_GetStringByValue(const char* name, const char* value) Settings::_GetStringByValue(const char* name, const char* value)
{ {
const char* string; const char* string;
for (int32 index = 0; fMessage.FindString( for (int32 index = 0; fMessage.FindString(name, index, &string) == B_OK;
name, index, &string) == B_OK; index++) index++) {
if (strcmp(string, value) == 0) if (strcmp(string, value) == 0)
return index; return index;
}
return B_ERROR; return B_ERROR;
} }
@@ -253,7 +257,9 @@ Settings::_GetPath(BPath& path)
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
if (status != B_OK) if (status != B_OK)
return status; return status;
path.Append("networktime settings"); path.Append("networktime settings");
return B_OK; return B_OK;
} }
@@ -275,28 +281,32 @@ NetworkTimeView::MessageReceived(BMessage* message)
switch (message->what) { switch (message->what) {
case kMsgSetDefaultServer: case kMsgSetDefaultServer:
{ {
int32 sel = fServerListView->CurrentSelection(); int32 currentSelection = fServerListView->CurrentSelection();
if (sel < 0) if (currentSelection < 0)
fServerListView->Select(fSettings.GetDefaultServer()); fServerListView->Select(fSettings.GetDefaultServer());
else { else {
fSettings.SetDefaultServer(sel); fSettings.SetDefaultServer(currentSelection);
Looper()->PostMessage(new BMessage(kMsgChange)); Looper()->PostMessage(new BMessage(kMsgChange));
} }
break; break;
} }
case kMsgServerEdited: case kMsgServerEdited:
{ {
rgb_color defaultColor = ui_color(B_CONTROL_TEXT_COLOR); rgb_color defaultColor = ui_color(B_CONTROL_TEXT_COLOR);
rgb_color red = {255, 0, 0}; rgb_color red = {255, 0, 0};
int32 length = fServerTextControl->TextView()->TextLength(); int32 length = fServerTextControl->TextView()->TextLength();
if (_IsValidServerName(fServerTextControl->TextView()->Text())) if (_IsValidServerName(fServerTextControl->TextView()->Text())) {
fServerTextControl->TextView()->SetFontAndColor(0, length, NULL, 0, &defaultColor); fServerTextControl->TextView()->SetFontAndColor(0, length, NULL,
else 0, &defaultColor);
fServerTextControl->TextView()->SetFontAndColor(0, length, NULL, 0, &red); } else {
fServerTextControl->TextView()->SetFontAndColor(0, length, NULL,
0, &red);
}
break; break;
} }
case kMsgAddServer: case kMsgAddServer:
if (!_IsValidServerName(fServerTextControl->TextView()->Text())) if (!_IsValidServerName(fServerTextControl->TextView()->Text()))
break; break;
@@ -333,14 +343,14 @@ NetworkTimeView::MessageReceived(BMessage* message)
break; break;
case kMsgSynchronizeAtBoot: case kMsgSynchronizeAtBoot:
fSettings.SetSynchronizeAtBoot( fSettings.SetSynchronizeAtBoot(fSynchronizeAtBootCheckBox->Value());
fSynchronizeAtBootCheckBox->Value());
Looper()->PostMessage(new BMessage(kMsgChange)); Looper()->PostMessage(new BMessage(kMsgChange));
break; break;
case kMsgStopSynchronization: case kMsgStopSynchronization:
if (fUpdateThread >= B_OK) if (fUpdateThread >= B_OK)
kill_thread(fUpdateThread); kill_thread(fUpdateThread);
_DoneSynchronizing(); _DoneSynchronizing();
break; break;
@@ -362,24 +372,25 @@ NetworkTimeView::MessageReceived(BMessage* message)
status_t status; status_t status;
if (message->FindInt32("status", (int32 *)&status) == B_OK) { if (message->FindInt32("status", (int32 *)&status) == B_OK) {
if (status == B_OK) return; if (status == B_OK)
return;
const char* errorString; const char* errorString;
message->FindString("error string", &errorString); message->FindString("error string", &errorString);
char buffer[256]; char buffer[256];
int32 errorCode; int32 errorCode;
if (message->FindInt32("error code", &errorCode) if (message->FindInt32("error code", &errorCode) == B_OK) {
== B_OK)
snprintf(buffer, sizeof(buffer), snprintf(buffer, sizeof(buffer),
B_TRANSLATE("The following error occured " B_TRANSLATE("The following error occured "
"while synchronizing:\r\n%s: %s"), "while synchronizing:\r\n%s: %s"),
errorString, strerror(errorCode)); errorString, strerror(errorCode));
else } else {
snprintf(buffer, sizeof(buffer), snprintf(buffer, sizeof(buffer),
B_TRANSLATE("The following error occured " B_TRANSLATE("The following error occured "
"while synchronizing:\r\n%s"), "while synchronizing:\r\n%s"),
errorString); errorString);
}
BAlert* alert = new BAlert(B_TRANSLATE("Time"), buffer, BAlert* alert = new BAlert(B_TRANSLATE("Time"), buffer,
B_TRANSLATE("OK")); B_TRANSLATE("OK"));
@@ -391,8 +402,7 @@ NetworkTimeView::MessageReceived(BMessage* message)
case kMsgRevert: case kMsgRevert:
fSettings.Revert(); fSettings.Revert();
fTryAllServersCheckBox->SetValue( fTryAllServersCheckBox->SetValue(fSettings.GetTryAllServers());
fSettings.GetTryAllServers());
fSynchronizeAtBootCheckBox->SetValue( fSynchronizeAtBootCheckBox->SetValue(
fSettings.GetSynchronizeAtBoot()); fSettings.GetSynchronizeAtBoot());
_UpdateServerList(); _UpdateServerList();
@@ -425,7 +435,8 @@ NetworkTimeView::CheckCanRevert()
void void
NetworkTimeView::_InitView() NetworkTimeView::_InitView()
{ {
fServerTextControl = new BTextControl(NULL, NULL, new BMessage(kMsgAddServer)); fServerTextControl = new BTextControl(NULL, NULL,
new BMessage(kMsgAddServer));
fServerTextControl->SetModificationMessage(new BMessage(kMsgServerEdited)); fServerTextControl->SetModificationMessage(new BMessage(kMsgServerEdited));
fAddButton = new BButton("add", B_TRANSLATE("Add"), fAddButton = new BButton("add", B_TRANSLATE("Add"),
@@ -483,7 +494,8 @@ NetworkTimeView::_InitView()
void void
NetworkTimeView::_UpdateServerList() NetworkTimeView::_UpdateServerList()
{ {
while (fServerListView->RemoveItem((int32)0) != NULL); while (fServerListView->RemoveItem((int32)0) != NULL)
;
const char* server; const char* server;
int32 index = 0; int32 index = 0;
@@ -538,6 +550,7 @@ update_time(const Settings& settings, const char** errorString,
for (int32 index = 0; ; index++) { for (int32 index = 0; ; index++) {
if (index == defaultServer) if (index == defaultServer)
index++; index++;
server = settings.GetServer(index); server = settings.GetServer(index);
if (server == NULL) if (server == NULL)
break; break;
@@ -568,9 +581,10 @@ update_thread(void* params)
result.AddString("error string", errorString); result.AddString("error string", errorString);
if (errorCode != 0) if (errorCode != 0)
result.AddInt32("error code", errorCode); result.AddInt32("error code", errorCode);
messenger->SendMessage(&result);
messenger->SendMessage(&result);
delete messenger; delete messenger;
return B_OK; return B_OK;
} }
@@ -583,6 +597,6 @@ update_time(const Settings& settings, BMessenger* messenger,
params->AddItem((void*)&settings); params->AddItem((void*)&settings);
params->AddItem((void*)messenger); params->AddItem((void*)messenger);
*thread = spawn_thread(update_thread, "ntpUpdate", 64, params); *thread = spawn_thread(update_thread, "ntpUpdate", 64, params);
return resume_thread(*thread); return resume_thread(*thread);
} }
+10 -10
View File
@@ -83,16 +83,16 @@ private:
class NetworkTimeView : public BGroupView { class NetworkTimeView : public BGroupView {
public: public:
NetworkTimeView(const char* name); NetworkTimeView(const char* name);
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
bool CheckCanRevert(); bool CheckCanRevert();
private: private:
void _InitView(); void _InitView();
void _UpdateServerList(); void _UpdateServerList();
void _DoneSynchronizing(); void _DoneSynchronizing();
bool _IsValidServerName(const char * serverName); bool _IsValidServerName(const char * serverName);
Settings fSettings; Settings fSettings;
@@ -102,13 +102,13 @@ private:
BButton* fRemoveButton; BButton* fRemoveButton;
BButton* fResetButton; BButton* fResetButton;
BListView* fServerListView; BListView* fServerListView;
BCheckBox* fTryAllServersCheckBox; BCheckBox* fTryAllServersCheckBox;
BCheckBox* fSynchronizeAtBootCheckBox; BCheckBox* fSynchronizeAtBootCheckBox;
BButton* fSynchronizeButton; BButton* fSynchronizeButton;
thread_id fUpdateThread; thread_id fUpdateThread;
}; };
#endif
#endif // NETWORK_TIME_VIEW_H