From 4f8aaf79576fa49836cf295f3cfd4fed8046c3af Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Fri, 18 Jun 2021 12:49:30 -0500 Subject: [PATCH] Add 'error' slot for templates --- application/ProtocolSettings.cpp | 8 ++++---- application/ProtocolSettings.h | 3 ++- application/ProtocolTemplate.cpp | 16 +++++++++++----- application/ProtocolTemplate.h | 4 +++- application/preferences/AccountDialog.cpp | 12 ++++++------ application/windows/TemplateWindow.cpp | 9 +++++---- application/windows/TemplateWindow.h | 1 + protocols/xmpp/JabberHandler.cpp | 12 +++++++++++- 8 files changed, 43 insertions(+), 22 deletions(-) diff --git a/application/ProtocolSettings.cpp b/application/ProtocolSettings.cpp index 84831ba..7caaed2 100644 --- a/application/ProtocolSettings.cpp +++ b/application/ProtocolSettings.cpp @@ -85,16 +85,16 @@ ProtocolSettings::Load(const char* account, BView* parent) status_t -ProtocolSettings::Save(const char* account, BView* parent) +ProtocolSettings::Save(const char* account, BView* parent, BString* errorText) { if (!parent) debugger("Couldn't save protocol's settings GUI on a NULL parent!"); BMessage settings; - status_t status = fTemplate.Save(parent, &settings); + status_t status = fTemplate.Save(parent, &settings, errorText); - if (!account || status != B_OK) - return B_BAD_VALUE; + if (status != B_OK) + return status; status_t ret = B_ERROR; diff --git a/application/ProtocolSettings.h b/application/ProtocolSettings.h index 83dbd71..64324db 100644 --- a/application/ProtocolSettings.h +++ b/application/ProtocolSettings.h @@ -25,7 +25,8 @@ public: BObjectList Accounts() const; status_t Load(const char* account, BView* parent); - status_t Save(const char* account, BView* parent); + status_t Save(const char* account, BView* parent, + BString* errorText = NULL); status_t Rename(const char* from, const char* to); status_t Delete(const char* account); diff --git a/application/ProtocolTemplate.cpp b/application/ProtocolTemplate.cpp index 552af32..5f0d06a 100644 --- a/application/ProtocolTemplate.cpp +++ b/application/ProtocolTemplate.cpp @@ -228,7 +228,7 @@ ProtocolTemplate::Load(BView* parent, BMessage* settings) status_t -ProtocolTemplate::Save(BView* parent, BMessage* settings) +ProtocolTemplate::Save(BView* parent, BMessage* settings, BString* errorText) { if (!parent) debugger("Couldn't save protocol's settings GUI on a NULL parent!"); @@ -236,6 +236,7 @@ ProtocolTemplate::Save(BView* parent, BMessage* settings) BMessage cur; for (int32 i = 0; fTemplate->FindMessage("setting", i, &cur) == B_OK; i++) { const char* name = cur.FindString("name"); + BString error = cur.FindString("error"); // Skip NULL names if (!name) @@ -251,7 +252,13 @@ ProtocolTemplate::Save(BView* parent, BMessage* settings) BTextControl* textControl = dynamic_cast(view); - if (textControl) { + + if (textControl && BString(textControl->Text()).IsEmpty() == true) { + if (error.IsEmpty() == false && errorText != NULL) + errorText->SetTo(error); + return B_BAD_VALUE; + } + else if (textControl) switch (type) { case B_STRING_TYPE: settings->AddString(name, textControl->Text()); @@ -260,9 +267,8 @@ ProtocolTemplate::Save(BView* parent, BMessage* settings) settings->AddInt32(name, atoi(textControl->Text())); break; default: - return B_ERROR; + return B_BAD_TYPE; } - } BMenuField* menuField = dynamic_cast(view); @@ -279,7 +285,7 @@ ProtocolTemplate::Save(BView* parent, BMessage* settings) settings->AddInt32(name, atoi(item->Label())); break; default: - return B_ERROR; + return B_BAD_TYPE; } } diff --git a/application/ProtocolTemplate.h b/application/ProtocolTemplate.h index c9e7c47..64544da 100644 --- a/application/ProtocolTemplate.h +++ b/application/ProtocolTemplate.h @@ -10,6 +10,7 @@ #include class BMessage; +class BString; class BView; class CayaProtocol; @@ -24,7 +25,8 @@ public: CayaProtocol* Protocol() const; status_t Load(BView* parent, BMessage* settings = NULL); - status_t Save(BView* parent, BMessage* settings); + status_t Save(BView* parent, BMessage* settings, + BString* errorText = NULL); private: CayaProtocol* fProtocol; diff --git a/application/preferences/AccountDialog.cpp b/application/preferences/AccountDialog.cpp index 4012af9..4e3fb15 100644 --- a/application/preferences/AccountDialog.cpp +++ b/application/preferences/AccountDialog.cpp @@ -98,7 +98,10 @@ AccountDialog::MessageReceived(BMessage* msg) } // Save account settings - if (fSettings->Save(fAccountName->Text(), fTop) == B_OK) { + BString error = "An error has occured saving the settings.\n" + "Check if your disk has enough space."; + + if (fSettings->Save(fAccountName->Text(), fTop, &error) == B_OK) { if (fTarget && (adding || renaming)) { BMessage* saveMsg = new BMessage(renaming ? kAccountRenamed : kAccountAdded); @@ -113,12 +116,9 @@ AccountDialog::MessageReceived(BMessage* msg) Close(); } else { - BAlert* alert = new BAlert("", "An error is occurred saving the settings.\n" - "Check if your disk has enough space.", "OK", NULL, NULL, B_WIDTH_AS_USUAL, - B_STOP_ALERT); + BAlert* alert = new BAlert("", error.String(), "OK", NULL, NULL, + B_WIDTH_AS_USUAL, B_STOP_ALERT); alert->Go(); - - Close(); } break; } diff --git a/application/windows/TemplateWindow.cpp b/application/windows/TemplateWindow.cpp index ce8d04f..146e926 100644 --- a/application/windows/TemplateWindow.cpp +++ b/application/windows/TemplateWindow.cpp @@ -62,12 +62,14 @@ TemplateWindow::MessageReceived(BMessage* msg) if (fTemplate == NULL || fTemplateView == NULL) break; + BString error = "Some items are empty. Please make sure to fill " + "out every item."; BMessage* settings = new BMessage(*fMessage); - status_t result = fTemplate->Save(fTemplateView, settings); + status_t result = fTemplate->Save(fTemplateView, settings, &error); if (result != B_OK) { - BAlert* alert = new BAlert("", "Invalid settings― make sure " - "each item is filled out.\n", "OK", NULL, NULL); + BAlert* alert = new BAlert("", error.String(), "OK", NULL, NULL, + B_WIDTH_AS_USUAL, B_WARNING_ALERT); alert->Go(); break; } @@ -75,7 +77,6 @@ TemplateWindow::MessageReceived(BMessage* msg) = fServer->GetProtocolLooper(fAccounts.ValueAt(fSelectedAcc)); if (looper == NULL) break; - looper->PostMessage(settings); Close(); break; diff --git a/application/windows/TemplateWindow.h b/application/windows/TemplateWindow.h index 49bb964..0b9225a 100644 --- a/application/windows/TemplateWindow.h +++ b/application/windows/TemplateWindow.h @@ -12,6 +12,7 @@ #include "ProtocolTemplate.h" #include "Server.h" +class BAlert; class BMenu; class BMenuField; class BTextControl; diff --git a/protocols/xmpp/JabberHandler.cpp b/protocols/xmpp/JabberHandler.cpp index 8783529..28c12b1 100644 --- a/protocols/xmpp/JabberHandler.cpp +++ b/protocols/xmpp/JabberHandler.cpp @@ -1142,12 +1142,16 @@ JabberHandler::_SettingsTemplate(const char* username, bool serverOption) BMessage usernameText; usernameText.AddString("name", "username"); usernameText.AddString("description", username); + usernameText.AddString("error", "You can't log into an account without a " + "username.\nPlease fill in your username for the given server."); usernameText.AddInt32("type", 'CSTR'); stemplate.AddMessage("setting", &usernameText); BMessage passwordText; passwordText.AddString("name", "password"); passwordText.AddString("description", "Password"); + passwordText.AddString("error", "You can't log into an account without a " + "password.\nPlease fill in your password for the given account."); passwordText.AddInt32("type", 'CSTR'); passwordText.AddBool("is_secret", true); stemplate.AddMessage("setting", &passwordText); @@ -1155,6 +1159,8 @@ JabberHandler::_SettingsTemplate(const char* username, bool serverOption) BMessage serverText; serverText.AddString("name", "server"); serverText.AddString("description", "Server"); + serverText.AddString("error", "You can't add an account without a server.\n" + "Please add a valid XMPP server."); serverText.AddInt32("type", 'CSTR'); if (serverOption == true) stemplate.AddMessage("setting", &serverText); @@ -1164,6 +1170,8 @@ JabberHandler::_SettingsTemplate(const char* username, bool serverOption) resourceText.AddString("description", "Resource"); resourceText.AddInt32("type", 'CSTR'); resourceText.AddString("default", "Caya"); + resourceText.AddString("error", "You can't add an account without a " + "resource.\nDon't worry― it can be whatever string you want."); stemplate.AddMessage("setting", &resourceText); return stemplate; @@ -1176,7 +1184,9 @@ JabberHandler::_RoomTemplate() BMessage stemplate('IMst'); BMessage roomIdentifier; roomIdentifier.AddString("name", "chat_id"); - roomIdentifier.AddString("description", "JID"); + roomIdentifier.AddString("description", "Room identifier"); + roomIdentifier.AddString("error", "You can't create a room without a JID!\n" + "Use the \"name@server\" format."); roomIdentifier.AddInt32("type", 'CSTR'); stemplate.AddMessage("setting", &roomIdentifier);