diff --git a/App.cpp b/App.cpp index d033be2..4b7b73e 100644 --- a/App.cpp +++ b/App.cpp @@ -38,6 +38,8 @@ void App::AboutRequested() { about->Show(); } + + int main() { App *app = new App(); app->Run(); diff --git a/App.h b/App.h index 7147f2a..2760df9 100644 --- a/App.h +++ b/App.h @@ -21,6 +21,13 @@ public: MainWindow* mainWindow = nullptr; + void ReadyToRun() + { + printf("ready to Run"); + // mainWindow->checkValidKey(); + + } + private: }; diff --git a/Conversation.cpp b/Conversation.cpp index 4973bcd..b2b7463 100644 --- a/Conversation.cpp +++ b/Conversation.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -178,6 +179,9 @@ void Conversation::ask(const std::string& prompt) { } + + + BString Conversation::ReadOpenAIKey() { // /boot/home/config/openai_key @@ -187,13 +191,16 @@ BString Conversation::ReadOpenAIKey() { if (find_directory(B_USER_SETTINGS_DIRECTORY, &configPath) != B_OK) return "error: couldn't find config directory"; - configPath.Append("openai_key"); + configPath.Append("openai_keyxxx"); BFile file(configPath.Path(), B_READ_ONLY); printf("full path:%s\n", configPath.Path()); if (file.InitCheck() != B_OK) + { + validKey=false; return "error: couldn't open key file "; + } off_t size; file.GetSize(&size); @@ -204,6 +211,8 @@ BString Conversation::ReadOpenAIKey() { BString result(buffer); delete[] buffer; + + validKey=true; return result; } diff --git a/Conversation.h b/Conversation.h index 0e637a4..7e8fd8b 100644 --- a/Conversation.h +++ b/Conversation.h @@ -47,12 +47,16 @@ using namespace BPrivate::Network; class Conversation : public BHandler { public: + bool validKey = false; + Conversation(BHandler* replyTo); virtual ~Conversation() ; virtual void ask(const std::string& prompt); virtual void MessageReceived(BMessage *msg); private: + + void sendReply(BMessage message); BHandler* replyTarget; BString ReadOpenAIKey(); diff --git a/DumBer b/DumBer index d19bf4a..8ac83e7 100755 Binary files a/DumBer and b/DumBer differ diff --git a/MainWindow.cpp b/MainWindow.cpp index e3a93db..1bbe758 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -19,6 +19,7 @@ static int progressAnim = 0; #include #include +#include #include #include "Conversation.h" @@ -26,15 +27,6 @@ static int progressAnim = 0; #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "Window" -static const uint32 kMsgNewFile = 'fnew'; -static const uint32 kMsgOpenFile = 'fopn'; -static const uint32 kMsgSaveFile = 'fsav'; - -static const uint32 kPulse = 'plse'; - -static const uint32 kSendPrompt = 'kspt'; -static const uint32 kQuestionChanged = 'kqch'; - MainWindow::MainWindow() : BWindow(BRect(100, 100, 600, 400), B_TRANSLATE("BeDumb"), B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) { @@ -118,22 +110,59 @@ MainWindow::MainWindow() .SetInsets(5, 5, 5, 5) .End(); + + - + //Just to animate progress BMessageRunner* runner = new BMessageRunner( this, // target BHandler new BMessage(kPulse), 50000 // interval in μs (0 ms) ); + PostMessage(kCheckKey); + + } + +void MainWindow::checkValidKey() { + + if (!_conversation->validKey) + { + _infoView->SetText("MISSING API KEY"); + ShowMissingKeyAlertAndQuit(); + return; + } + else + _infoView->SetText("API Key loaded."); + +} + +void MainWindow::ShowMissingKeyAlertAndQuit() +{ + +BAlert* alert = new BAlert("Missing key file!", "Create a file named 'openai_key' containing a valid OpenAI Token on one line in \n\n/boot/home/config/settings/openai_key .\n\nThen relaunch the app.\n\nBe aware that this is not a safe storage so don't use valuable keys.", "Oh, no", "Sigh", "Just give up", B_WIDTH_AS_USUAL,B_WARNING_ALERT); + +alert->SetType(B_INFO_ALERT); + +uint32 result = alert->Go(); +PostMessage(B_QUIT_REQUESTED); + + +} + + MainWindow::~MainWindow() {} void MainWindow::MessageReceived(BMessage *message) { switch (message->what) { + case kCheckKey: { + checkValidKey(); + } + break; case kPulse: { @@ -141,8 +170,9 @@ void MainWindow::MessageReceived(BMessage *message) { _progress->SetTo(progressAnim); progressAnim++; } - break; } + break; + // case kMsgNewFile: { // fSaveMenuItem->SetEnabled(false); // printf("New\n"); @@ -182,7 +212,7 @@ void MainWindow::MessageReceived(BMessage *message) { const char* text; if (message->FindString("text", &text) == B_OK) { - printf("Received text: %s\n", text); + // printf("Received text: %s\n", text); // Do something with text (e.g., set it to a BTextView) _answerView->SetText(text); } else { diff --git a/MainWindow.h b/MainWindow.h index 0703ea6..1928377 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -11,8 +11,23 @@ #include #include #include +#include + #include "Conversation.h" + + +static const uint32 kCheckKey = 'chkk'; +static const uint32 kMsgNewFile = 'fnew'; +static const uint32 kMsgOpenFile = 'fopn'; +static const uint32 kMsgSaveFile = 'fsav'; + +static const uint32 kPulse = 'plse'; + +static const uint32 kSendPrompt = 'kspt'; +static const uint32 kQuestionChanged = 'kqch'; + + @@ -25,8 +40,13 @@ public: Conversation* _conversation = new Conversation(this); + void checkValidKey(); + + + private: + void ShowMissingKeyAlertAndQuit(); BMenuBar *_BuildMenu(); BTextView * _answerView;