/* * Copyright 2024, Santiago Lema * All rights reserved. Distributed under the terms of the MIT license. */ #include "MainWindow.h" #include #include #include #include #include #include #include #include #include // #include #include using namespace BPrivate::Network; #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 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) { BMenuBar *menuBar = _BuildMenu(); BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menuBar).AddGlue().End(); _inputField = new BTextControl(B_TRANSLATE("Your question: "), "What is the matrix ?", new BMessage(kQuestionChanged)); _progress = new BStatusBar("prog"); _progress->SetMaxValue(100); _progress->SetTo(0); _progress->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); _answerView = new BTextView("answer"); BStringView* header = new BStringView("biglabel", "Let's Be Dumber!"); BFont font; header->GetFont(&font); font.SetSize(20); header->SetFont(&font); // Info view, only one line high _infoView = new BTextView("info"); _infoView->SetText("Ask your question.."); _infoView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); _infoView->MakeEditable(false); _infoView->MakeSelectable(false); _infoView->SetWordWrap(false); float lineHeight = _infoView->LineHeight(0); _infoView->SetExplicitMinSize(BSize(B_SIZE_UNSET, lineHeight)); _infoView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, lineHeight)); float askH = lineHeight * 2; _inputField->SetExplicitMinSize(BSize(B_SIZE_UNSET, askH)); _inputField->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, askH * 6)); BButton *sendButton = new BButton("send", B_TRANSLATE("ask"), new BMessage(kSendPrompt), B_WILL_DRAW | B_NAVIGABLE); BLayoutBuilder::Group<>(this, B_VERTICAL, 0) .Add(header) .AddGroup(B_HORIZONTAL, 0, 1) .Add(_inputField) .AddGroup(B_HORIZONTAL, 0, 1) .Add(sendButton) .End() .End() .Add(_progress) .Add(_infoView) .Add(_answerView) .SetInsets(5, 5, 5, 5) .End(); } MainWindow::~MainWindow() {} void MainWindow::MessageReceived(BMessage *message) { switch (message->what) { // case kMsgNewFile: { // fSaveMenuItem->SetEnabled(false); // printf("New\n"); // } break; // case kMsgOpenFile: { // fSaveMenuItem->SetEnabled(true); // printf("Open\n"); // } break; // case kMsgSaveFile: { // printf("Save\n"); // } break; case kQuestionChanged: { printf("Question Changed\n"); _progress->SetTo(2); // sendQuery(); } break; case kSendPrompt: { printf("Button Pressed\n"); sendQuery(); } break; case UrlEvent::HostNameResolved: { printf("Host name resolved\n"); auto name = message->GetString(UrlEventData::HostName); message->PrintToStream(); _infoView->SetText("Hostname resolve..."); _infoView->SetText(name); _progress->SetTo(5); } break; case UrlEvent::ConnectionOpened: { printf("ConnectionOpened\n"); _progress->SetTo(10); _infoView->SetText("connection opened..."); } break; case UrlEvent::ResponseStarted: { printf("ResponseStarted\n"); _progress->SetTo(14); _infoView->SetText("ResponseStarted..."); } break; case UrlEvent::HttpRedirect: { printf("HttpRedirect\n"); _progress->SetTo(16); _infoView->SetText("HttpRedirect..."); } break; case UrlEvent::RequestCompleted: { printf("RequestCompleted\n"); auto identifier = message->GetInt32(UrlEventData::Id, -1); if (resultOne->Identity() == identifier) { // The following call will not block, because we have been notified // that the request is done. auto body = resultOne->Body(); //if (body) // _DisplayHttpResult(body.value()); //else // _DisplayError(body.error()); } _infoView->SetText("Completed"); _progress->SetMaxValue(100); _progress->SetTo(100); } break; case UrlEvent::HttpStatus: { printf("HttpStatus\n"); _infoView->SetText("HttpStatus received"); _progress->SetTo(20); } break; case UrlEvent::BytesWritten: { _infoView->SetText("Some bytes written.."); // auto identifier = message->GetInt32(UrlEventData::Id, -1); // if (fResult.Identifier() == identifier) { //off_t numBytes = message->GetInt64(UrlEventData::NumBytes, 0); //_progress->SetTo(numBytes); //} } break; case UrlEvent::DownloadProgress: { auto identifier = message->GetInt32(UrlEventData::Id, -1); if (resultOne->Identity() == identifier) { off_t nn = message->GetInt64(UrlEventData::NumBytes, 0); off_t totalBytes = message->GetInt64(UrlEventData::TotalBytes, 0); _progress->SetMaxValue(totalBytes); _progress->SetTo(nn); _infoView->SetText("Download Progress.."); } } break; default: { message->PrintToStream(); BWindow::MessageReceived(message); // call the parent handler for other messages // _infoView->SetText(message->FindMessage()); break; } }//end switch } //end function void MainWindow::sendQuery() { //if (resultOne!=nullptr) // _sharedSession.Cancel(resultOne->Identity()); _progress->SetMaxValue(100); _progress->SetTo(0); auto url = BUrl("https://www.link-u.com/ip/"); BHttpRequest request = BHttpRequest(url); // requestOne = &request; // request.SetTimeout() // request.SetMethod(BHttpMethod::Get); printf("Sending Prompt to server: %s\n", url.UrlString().String()); BHttpResult res = _sharedSession.Execute(std::move(request), nullptr, this); printf("Result has identity: %d\n", res.Identity()); resultOne = &res; } BMenuBar *MainWindow::_BuildMenu() { BMenuBar *menuBar = new BMenuBar("menubar"); BMenu *menu; BMenuItem *item; // menu 'File' menu = new BMenu(B_TRANSLATE("File")); // item = new BMenuItem(B_TRANSLATE("New"), new BMessage(kMsgNewFile), 'N'); // menu->AddItem(item); // item = new BMenuItem(B_TRANSLATE("Open" B_UTF8_ELLIPSIS), // new BMessage(kMsgOpenFile), 'O'); // menu->AddItem(item); // fSaveMenuItem = // new BMenuItem(B_TRANSLATE("Save"), new BMessage(kMsgSaveFile), 'S'); // fSaveMenuItem->SetEnabled(false); // menu->AddItem(fSaveMenuItem); // menu->AddSeparatorItem(); item = new BMenuItem(B_TRANSLATE("About" B_UTF8_ELLIPSIS), new BMessage(B_ABOUT_REQUESTED)); item->SetTarget(be_app); menu->AddItem(item); item = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(B_QUIT_REQUESTED), 'Q'); menu->AddItem(item); menuBar->AddItem(menu); return menuBar; }