214 lines
No EOL
5 KiB
C++
214 lines
No EOL
5 KiB
C++
/*
|
|
* Copyright 2024, Santiago Lema <santiago@lema.org>
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
*/
|
|
|
|
#include "MainWindow.h"
|
|
|
|
#include <Url.h>
|
|
|
|
#include <Application.h>
|
|
#include <Button.h>
|
|
#include <Catalog.h>
|
|
#include <LayoutBuilder.h>
|
|
#include <Menu.h>
|
|
#include <MenuBar.h>
|
|
|
|
|
|
#include <View.h>
|
|
|
|
// #include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
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) {
|
|
|
|
auto sess = BHttpSession();
|
|
_sharedSession = sess;
|
|
|
|
BMenuBar *menuBar = _BuildMenu();
|
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menuBar).AddGlue().End();
|
|
|
|
_inputField = new BTextControl(B_TRANSLATE("question"), "",
|
|
new BMessage(kQuestionChanged));
|
|
|
|
_progress = new BStatusBar(BRect(0,0,100,10),"prog");
|
|
_progress->SetMaxValue(100);
|
|
_progress->SetTo(0);
|
|
|
|
_answerView =
|
|
new BTextView("answer", B_WILL_DRAW | B_FOLLOW_ALL_SIDES);
|
|
|
|
BButton *sendButton =
|
|
new BButton("send", B_TRANSLATE("ask"), new BMessage(kSendPrompt),
|
|
B_WILL_DRAW | B_NAVIGABLE);
|
|
|
|
// BLayoutItem * addMe = new BLayoutItem():
|
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
|
|
|
|
.AddGroup(B_HORIZONTAL, 0,1)
|
|
.Add(_inputField)
|
|
.AddGroup(B_VERTICAL,B_USE_DEFAULT_SPACING, 0.1f)
|
|
.Add(_progress)
|
|
.Add(sendButton)
|
|
.End()
|
|
.End()
|
|
|
|
|
|
|
|
.AddGroup(B_HORIZONTAL, 0)
|
|
.Add(_answerView)
|
|
.End()
|
|
|
|
.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");
|
|
|
|
sendQuery();
|
|
|
|
} break;
|
|
|
|
case kSendPrompt: {
|
|
|
|
printf("Button Pressed\n");
|
|
sendQuery();
|
|
|
|
} break;
|
|
|
|
case UrlEvent::HostNameResolved: {
|
|
printf("Host name resolved\n");
|
|
_inputField->SetText("Hostname resolve...");
|
|
_progress->SetTo(10);
|
|
|
|
} break;
|
|
|
|
case UrlEvent::ConnectionOpened: {
|
|
printf("ConnectionOpened\n");
|
|
_progress->SetTo(20);
|
|
_inputField->SetText("connection opened...");
|
|
} break;
|
|
|
|
case UrlEvent::RequestCompleted: {
|
|
|
|
printf("RequestCompleted\n");
|
|
_inputField->SetText("Completed");
|
|
_progress->SetMaxValue(100);
|
|
_progress->SetTo(100);
|
|
|
|
} break;
|
|
|
|
case UrlEvent::BytesWritten: {
|
|
// 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 (fResult.Identifier() == identifier) {
|
|
off_t nn = message->GetInt64(UrlEventData::NumBytes, 0);
|
|
off_t totalBytes = message->GetInt64(UrlEventData::TotalBytes, 0);
|
|
_progress->SetMaxValue(totalBytes);
|
|
_progress->SetTo(nn);
|
|
//}
|
|
} break;
|
|
|
|
default: {
|
|
BWindow::MessageReceived(message);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void MainWindow::sendQuery() {
|
|
|
|
printf("Sending Prompt to server...\n");
|
|
|
|
_progress->SetMaxValue(100);
|
|
_progress->SetTo(50);
|
|
|
|
auto url = BUrl("https://www.smallte.ch/");
|
|
auto request = BHttpRequest(url);
|
|
|
|
// Add a cookie to the session, this cookie will be used in window1 and
|
|
// window2
|
|
// BNetworkCookie cookie("key", "value", BUrl("https://example.com/"));
|
|
// session.AddCookie(std::move(cookie));
|
|
_sharedSession.Execute(std::move(request), nullptr, this);
|
|
|
|
}
|
|
|
|
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;
|
|
} |