Compare commits
13 Commits
v0.7.2
..
a78f4536b2
| Author | SHA1 | Date | |
|---|---|---|---|
| a78f4536b2 | |||
| 1992b0e166 | |||
| 575101780d | |||
| 48b4f9cda8 | |||
| 5173a05a09 | |||
| fe9735fe58 | |||
| e4d67c2163 | |||
| c61a354be8 | |||
| 50ef8d9fda | |||
| 0a3bbffb52 | |||
| b352daabfb | |||
| b005631d20 | |||
| 74dc204f22 |
@@ -1,7 +1,7 @@
|
||||
# Compiler and flags
|
||||
CPPC = g++
|
||||
CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenu -lpanel -Wall -Wextra -Wno-write-strings
|
||||
DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenu -lpanel -Wall -Wextra -Wno-write-strings
|
||||
CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
|
||||
DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
|
||||
|
||||
|
||||
SRC_PATH := src
|
||||
@@ -29,7 +29,7 @@ $(BIN_PATH)/bakatui: $(OBJ_FILES)
|
||||
$(CPPC) $(CPPC_FLAGS) $^ -o $@
|
||||
|
||||
|
||||
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
|
||||
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp $(SRC_PATH)/%.h
|
||||
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#include <string_view>
|
||||
#ifndef VERSION
|
||||
|
||||
#define VERSION "0.7.2"
|
||||
#define VERSION "0.8"
|
||||
#define NAME "bakatui"
|
||||
|
||||
inline constexpr auto hash_djb2a(const std::string_view sv) {
|
||||
|
||||
+30
-2
@@ -3,6 +3,7 @@
|
||||
#include "main.h"
|
||||
#include "memory.h"
|
||||
#include "net.h"
|
||||
#include <algorithm>
|
||||
#include <codecvt>
|
||||
#include <csignal>
|
||||
#include <curses.h>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <iostream>
|
||||
#include <locale>
|
||||
#include <panel.h>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
@@ -199,7 +201,7 @@ std::wstring wrm_tr_le_whitespace(const std::wstring &s) {
|
||||
}
|
||||
|
||||
// Conversion utilities
|
||||
char *wchar_to_char(wchar_t *src) {
|
||||
char *wchar_to_char(const wchar_t *src) {
|
||||
if (!src)
|
||||
return nullptr;
|
||||
|
||||
@@ -210,7 +212,7 @@ char *wchar_to_char(wchar_t *src) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
wchar_t *char_to_wchar(char *src) {
|
||||
wchar_t *char_to_wchar(const char *src) {
|
||||
if (!src)
|
||||
return nullptr;
|
||||
|
||||
@@ -242,3 +244,29 @@ void move_panel_relative(PANEL *panel, int dy, int dx) {
|
||||
|
||||
move_panel(panel, new_y, new_x);
|
||||
}
|
||||
|
||||
std::string html_to_string(std::string html) {
|
||||
{ // fix new lines
|
||||
const std::string search = "<br />";
|
||||
const std::string replace = "\n";
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = html.find(search, pos)) != std::string::npos) {
|
||||
html.replace(pos, search.length(), replace);
|
||||
pos += replace.length();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::regex linkPattern("<a\\s+href=[\"'](.*?)[\"'](.*?)>(.*?)</a>");
|
||||
html = std::regex_replace(html, linkPattern,
|
||||
"\033]8;;$1\033\\$3\033]8;;\033\\");
|
||||
}
|
||||
|
||||
{
|
||||
std::regex tag("<[^>]*>");
|
||||
html = std::regex_replace(html, tag, "");
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
||||
+4
-2
@@ -27,10 +27,12 @@ void wprint_in_middle(WINDOW *win, int starty, int startx, int width,
|
||||
std::wstring wrm_tr_le_whitespace(const std::wstring &s);
|
||||
|
||||
// Conversion utilities
|
||||
char *wchar_to_char(wchar_t *src);
|
||||
wchar_t *char_to_wchar(char *src);
|
||||
char *wchar_to_char(const wchar_t *src);
|
||||
wchar_t *char_to_wchar(const char *src);
|
||||
|
||||
std::wstring string_to_wstring(const std::string &str);
|
||||
std::string wstring_to_string(const std::wstring &wstr);
|
||||
|
||||
std::string html_to_string(std::string html);
|
||||
|
||||
#endif
|
||||
+285
@@ -0,0 +1,285 @@
|
||||
#include "komens.h"
|
||||
#include "helper_funcs.h"
|
||||
#include "memory.h"
|
||||
#include "net.h"
|
||||
#include "types.h"
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
#include <cwchar>
|
||||
#include <menu.h>
|
||||
#include <ncurses.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define MAIN_WIN_PROPORTION 0.714f
|
||||
|
||||
#define MAIN_WIN_HIGHT (roundf(LINES * MAIN_WIN_PROPORTION))
|
||||
|
||||
#define DEFAULT_OFSET 4
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
std::vector<allocation> komens_allocated;
|
||||
|
||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
|
||||
json &resp_from_api);
|
||||
|
||||
void komens_page(koment_type type) {
|
||||
current_allocated = &komens_allocated;
|
||||
json resp_from_api;
|
||||
{
|
||||
/*std::ifstream f("test-data/komens.json");
|
||||
resp_from_api = json::parse(f);
|
||||
f.close();*/
|
||||
const char *types[] = {"/api/3/komens/messages/received",
|
||||
"/api/3/komens/messages/sent",
|
||||
"/api/3/komens/messages/noticeboard"};
|
||||
|
||||
const std::string endpoint = types[type];
|
||||
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, POST);
|
||||
}
|
||||
|
||||
/* Initialize curses */
|
||||
setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
/* Initialize all the colors */
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
init_pair(i, i, COLOR_BLACK);
|
||||
}
|
||||
|
||||
complete_menu komens_choise_menu;
|
||||
|
||||
size_t num_of_komens = resp_from_api["Messages"].size();
|
||||
komens_choise_menu.items = new ITEM *[num_of_komens + 1];
|
||||
komens_allocated.push_back(
|
||||
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
|
||||
|
||||
char **title_bufs = new char *[num_of_komens];
|
||||
char **name_bufs = new char *[num_of_komens];
|
||||
size_t max_item_lenght;
|
||||
{
|
||||
size_t max_title_lenght = 0;
|
||||
size_t max_name_lenght = 0;
|
||||
size_t tmp_lenght;
|
||||
char tmp_buf[1500];
|
||||
for (size_t i = 0; i < num_of_komens; i++) {
|
||||
wcstombs(tmp_buf,
|
||||
string_to_wstring(
|
||||
resp_from_api["Messages"][i]["Title"].get<std::string>())
|
||||
.c_str(),
|
||||
sizeof(tmp_buf));
|
||||
|
||||
tmp_lenght =
|
||||
resp_from_api["Messages"][i]["Title"].get<std::string>().length();
|
||||
|
||||
if (tmp_lenght > max_title_lenght) {
|
||||
max_title_lenght = tmp_lenght;
|
||||
}
|
||||
|
||||
title_bufs[i] = new char[strlen(tmp_buf) + 1];
|
||||
strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
||||
wcstombs(
|
||||
tmp_buf,
|
||||
string_to_wstring(
|
||||
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>())
|
||||
.c_str(),
|
||||
sizeof(tmp_buf));
|
||||
|
||||
tmp_lenght = resp_from_api["Messages"][i]["Sender"]["Name"]
|
||||
.get<std::string>()
|
||||
.length();
|
||||
|
||||
if (tmp_lenght > max_name_lenght) {
|
||||
max_name_lenght = tmp_lenght;
|
||||
}
|
||||
|
||||
name_bufs[i] = new char[strlen(tmp_buf) + 1];
|
||||
strlcpy(name_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
||||
|
||||
komens_choise_menu.items[i] = new_item(title_bufs[i], name_bufs[i]);
|
||||
}
|
||||
max_item_lenght = 3 + max_title_lenght + 1 + max_name_lenght;
|
||||
}
|
||||
komens_choise_menu.items[num_of_komens] = nullptr;
|
||||
|
||||
komens_choise_menu.menu = new_menu(komens_choise_menu.items);
|
||||
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
|
||||
|
||||
komens_choise_menu.win =
|
||||
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
|
||||
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu.win, 1});
|
||||
|
||||
set_menu_win(komens_choise_menu.menu, komens_choise_menu.win);
|
||||
set_menu_sub(komens_choise_menu.menu,
|
||||
derwin(komens_choise_menu.win, MAIN_WIN_HIGHT - 10,
|
||||
max_item_lenght, DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
||||
set_menu_format(komens_choise_menu.menu, MAIN_WIN_HIGHT - 5, 1);
|
||||
|
||||
set_menu_mark(komens_choise_menu.menu, " * ");
|
||||
|
||||
box(komens_choise_menu.win, 0, 0);
|
||||
|
||||
wprint_in_middle(komens_choise_menu.win, 1, 0, max_item_lenght, L"Komens",
|
||||
COLOR_PAIR(1));
|
||||
mvwaddch(komens_choise_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(komens_choise_menu.win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
||||
mvwaddch(komens_choise_menu.win, 2, max_item_lenght, ACS_RTEE);
|
||||
|
||||
post_menu(komens_choise_menu.menu);
|
||||
wrefresh(komens_choise_menu.win);
|
||||
|
||||
WINDOW *content_win =
|
||||
newwin(MAIN_WIN_HIGHT, COLS - max_item_lenght - DEFAULT_OFSET - 1,
|
||||
DEFAULT_OFSET, DEFAULT_OFSET + max_item_lenght + 1);
|
||||
komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
|
||||
|
||||
WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
|
||||
|
||||
insert_content(content_win, attachment_win,
|
||||
resp_from_api["Messages"][item_index(
|
||||
current_item(komens_choise_menu.menu))]);
|
||||
|
||||
attron(COLOR_PAIR(COLOR_BLUE));
|
||||
mvprintw(LINES - 2, 0,
|
||||
"Use PageUp and PageDown to scoll down or up a page of items");
|
||||
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(COLOR_BLUE));
|
||||
refresh();
|
||||
|
||||
int c;
|
||||
while ((c = getch()) != KEY_F(1)) {
|
||||
switch (c) {
|
||||
case KEY_DOWN:
|
||||
case KEY_NPAGE:
|
||||
case 'j':
|
||||
menu_driver(komens_choise_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
case 'k':
|
||||
menu_driver(komens_choise_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
default:
|
||||
if (c >= '1' && c <= '9') {
|
||||
size_t index = c - '0' - 1;
|
||||
if (index < resp_from_api["Messages"][item_index(
|
||||
current_item(komens_choise_menu.menu))]["Attachments"]
|
||||
.size()) {
|
||||
|
||||
std::string default_path =
|
||||
"~/Downloads/" +
|
||||
resp_from_api["Messages"][item_index(current_item(
|
||||
komens_choise_menu.menu))]["Attachments"][index]["Name"]
|
||||
.get<std::string>();
|
||||
char path[256];
|
||||
|
||||
// Create input prompt at bottom of screen
|
||||
move(LINES - 1, 0);
|
||||
clrtoeol();
|
||||
printw("Save path [%s]: ", default_path.c_str());
|
||||
echo();
|
||||
curs_set(1);
|
||||
getnstr(path, sizeof(path) - 1);
|
||||
if (strlen(path) == 0)
|
||||
strcpy(path, default_path.c_str());
|
||||
noecho();
|
||||
curs_set(0);
|
||||
move(LINES - 1, 0);
|
||||
clrtoeol();
|
||||
refresh();
|
||||
|
||||
// Download the attachment
|
||||
bakaapi::download_attachment(
|
||||
resp_from_api["Messages"][item_index(current_item(
|
||||
komens_choise_menu.menu))]["Attachments"][index]["Id"]
|
||||
.get<std::string>(),
|
||||
path);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
insert_content(content_win, attachment_win,
|
||||
resp_from_api["Messages"][item_index(
|
||||
current_item(komens_choise_menu.menu))]);
|
||||
wrefresh(komens_choise_menu.win);
|
||||
}
|
||||
delete_all(&komens_allocated);
|
||||
}
|
||||
|
||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
|
||||
json &message) {
|
||||
wclear(content_win);
|
||||
mvwprintw(content_win, 0, 0, "%s",
|
||||
html_to_string(message.at("Text")).c_str());
|
||||
wrefresh(content_win);
|
||||
if (!message.at("Attachments").empty()) {
|
||||
|
||||
size_t max_item_lenght = 0;
|
||||
{
|
||||
size_t max_name_lenght = 0;
|
||||
size_t max_size_lenght = 0;
|
||||
size_t tmp_lenght;
|
||||
|
||||
for (size_t j = 0; j < message.at("Attachments").size(); j++) {
|
||||
tmp_lenght =
|
||||
message.at("Attachments")[j]["Name"].get<std::string>().length();
|
||||
if (tmp_lenght > max_name_lenght) {
|
||||
max_name_lenght = tmp_lenght;
|
||||
}
|
||||
|
||||
tmp_lenght =
|
||||
std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
|
||||
.length();
|
||||
if (tmp_lenght > max_size_lenght) {
|
||||
max_size_lenght = tmp_lenght;
|
||||
}
|
||||
}
|
||||
|
||||
max_item_lenght = 3 + max_name_lenght + 1 + max_size_lenght;
|
||||
}
|
||||
|
||||
mvwin(attachment_win, MAIN_WIN_HIGHT + DEFAULT_OFSET + 1,
|
||||
COLS - max_item_lenght - 2);
|
||||
wresize(attachment_win, LINES - (MAIN_WIN_HIGHT + DEFAULT_OFSET + 1),
|
||||
max_item_lenght + 2);
|
||||
|
||||
wborder(attachment_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
|
||||
print_in_middle(attachment_win, 0, 0, max_item_lenght + 2, "Attachments",
|
||||
COLOR_PAIR(COLOR_RED));
|
||||
for (size_t j = 0; j < message.at("Attachments").size(); j++) {
|
||||
|
||||
mvwprintw(
|
||||
attachment_win, j + 1, 2, "%s %s",
|
||||
message.at("Attachments")[j]["Name"].get<std::string>().c_str(),
|
||||
std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
|
||||
.c_str());
|
||||
|
||||
wattron(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
||||
mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1);
|
||||
wattroff(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
||||
}
|
||||
{ // remove duplicating spaces
|
||||
unsigned short attachment_win_top, attachment_win_left,
|
||||
attachment_win_height, attachment_win_width;
|
||||
getbegyx(attachment_win, attachment_win_top, attachment_win_left);
|
||||
getmaxyx(attachment_win, attachment_win_height, attachment_win_width);
|
||||
|
||||
mvvline(attachment_win_top, attachment_win_left - 1, ' ',
|
||||
attachment_win_height);
|
||||
}
|
||||
refresh();
|
||||
|
||||
wrefresh(attachment_win);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef _ba_ko_hg_
|
||||
#define _ba_ko_hg_
|
||||
enum koment_type {
|
||||
RECEIVED,
|
||||
SEND,
|
||||
NOTICEBOARD,
|
||||
};
|
||||
void komens_page(koment_type type);
|
||||
#endif
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "komens_menu.h"
|
||||
#include "helper_funcs.h"
|
||||
#include "komens.h"
|
||||
#include "net.h"
|
||||
#include "types.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
void komens_menu() {
|
||||
wchar_t *choices[] = {
|
||||
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
|
||||
};
|
||||
|
||||
complete_menu komens_menu;
|
||||
|
||||
int c;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
komens_menu.items[i] =
|
||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||
|
||||
/* Crate menu */
|
||||
komens_menu.menu = new_menu(komens_menu.items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
komens_menu.win = newwin(12, 40, 4, 4);
|
||||
keypad(komens_menu.win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_menu_win(komens_menu.menu, komens_menu.win);
|
||||
set_menu_sub(komens_menu.menu, derwin(komens_menu.win, 8, 38, 3, 1));
|
||||
set_menu_format(komens_menu.menu, 7, 1);
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(komens_menu.menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(komens_menu.win, 0, 0);
|
||||
|
||||
wprint_in_middle(komens_menu.win, 1, 0, 40, L"Komens Menu", COLOR_PAIR(1));
|
||||
mvwaddch(komens_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(komens_menu.win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(komens_menu.win, 2, 39, ACS_RTEE);
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(komens_menu.menu);
|
||||
wrefresh(komens_menu.win);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 2, 0,
|
||||
"Use PageUp and PageDown to scoll down or up a page of items");
|
||||
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
|
||||
attroff(COLOR_PAIR(2));
|
||||
refresh();
|
||||
|
||||
while ((c = getch()) != KEY_F(1)) {
|
||||
switch (c) {
|
||||
case KEY_DOWN:
|
||||
case KEY_NPAGE:
|
||||
case 'j':
|
||||
menu_driver(komens_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
case 'k':
|
||||
menu_driver(komens_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: // ENTER
|
||||
clear();
|
||||
if (item_index(current_item(komens_menu.menu)) == n_choices - 1) {
|
||||
goto close_menu;
|
||||
}
|
||||
komens_page(
|
||||
static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
|
||||
pos_menu_cursor(komens_menu.menu);
|
||||
refresh();
|
||||
wrefresh(komens_menu.win);
|
||||
break;
|
||||
}
|
||||
wrefresh(komens_menu.win);
|
||||
}
|
||||
close_menu:
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(komens_menu.menu);
|
||||
free_menu(komens_menu.menu);
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
free_item(komens_menu.items[i]);
|
||||
endwin();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef _ba_km_hg_
|
||||
#define _ba_km_hg_
|
||||
|
||||
void komens_menu();
|
||||
|
||||
#endif
|
||||
+21
-8
@@ -1,8 +1,10 @@
|
||||
#include "main.h"
|
||||
#include "color.h"
|
||||
#include "flags.h"
|
||||
#include "helper_funcs.h"
|
||||
#include "main_menu.h"
|
||||
#include "net.h"
|
||||
#include "types.h"
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <curl/curl.h>
|
||||
@@ -12,8 +14,6 @@
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include "flags.h"
|
||||
#include "types.h"
|
||||
|
||||
std::string baka_api_url;
|
||||
|
||||
@@ -37,12 +37,25 @@ int main(int argc, char **argv) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hVvLS:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h': PrintHelp(); break;
|
||||
case 'V': PrintVersion(); break;
|
||||
case 'v': config.verbose = true; break;
|
||||
case 'L': DeleteLogin(savedir_path); break;
|
||||
case 'S': config.ignoressl = true; break;
|
||||
default: std::cerr << RED"[ERROR]" << RESET" invalid option: " << (char)optopt << "\ntry: -h\n"; safe_exit(EINVAL);
|
||||
case 'h':
|
||||
PrintHelp();
|
||||
break;
|
||||
case 'V':
|
||||
PrintVersion();
|
||||
break;
|
||||
case 'v':
|
||||
config.verbose = true;
|
||||
break;
|
||||
case 'L':
|
||||
DeleteLogin(savedir_path);
|
||||
break;
|
||||
case 'S':
|
||||
config.ignoressl = true;
|
||||
break;
|
||||
default:
|
||||
std::cerr << RED "[ERROR]" << RESET " invalid option: " << (char)optopt
|
||||
<< "\ntry: -h\n";
|
||||
safe_exit(EINVAL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+43
-43
@@ -1,28 +1,25 @@
|
||||
#include "helper_funcs.h"
|
||||
#include "komens_menu.h"
|
||||
#include "marks.h"
|
||||
#include "net.h"
|
||||
#include "timetable.h"
|
||||
#include <cstddef>
|
||||
#include "types.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#include "marks.h"
|
||||
|
||||
#define CTRLD 4
|
||||
|
||||
wchar_t *choices[] = {
|
||||
void main_menu() {
|
||||
wchar_t *choices[] = {
|
||||
L"login", L"Marks", L"timetable", L"Komens",
|
||||
L"Homework", L"Absence", L"Exit", (wchar_t *)NULL,
|
||||
};
|
||||
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, nullptr,
|
||||
L"Homework", L"Absence", L"Exit", nullptr,
|
||||
};
|
||||
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu,
|
||||
nullptr, nullptr, nullptr, nullptr};
|
||||
|
||||
void main_menu() {
|
||||
ITEM **my_items;
|
||||
complete_menu main_menu;
|
||||
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
@@ -37,37 +34,37 @@ void main_menu() {
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
my_items[i] =
|
||||
main_menu.items[i] =
|
||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
main_menu.menu = new_menu((ITEM **)main_menu.items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(12, 40, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
main_menu.win = newwin(12, 40, 4, 4);
|
||||
keypad(main_menu.win, TRUE);
|
||||
|
||||
/* Set main window and sub window */
|
||||
set_menu_win(my_menu, my_menu_win);
|
||||
set_menu_sub(my_menu, derwin(my_menu_win, 8, 38, 3, 1));
|
||||
set_menu_format(my_menu, 7, 1);
|
||||
set_menu_win(main_menu.menu, main_menu.win);
|
||||
set_menu_sub(main_menu.menu, derwin(main_menu.win, 8, 38, 3, 1));
|
||||
set_menu_format(main_menu.menu, 7, 1);
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(my_menu, " * ");
|
||||
set_menu_mark(main_menu.menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
box(main_menu.win, 0, 0);
|
||||
|
||||
wprint_in_middle(my_menu_win, 1, 0, 40, L"Main Menu", COLOR_PAIR(1));
|
||||
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
|
||||
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
|
||||
wprint_in_middle(main_menu.win, 1, 0, 40, L"Main Menu", COLOR_PAIR(1));
|
||||
mvwaddch(main_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(main_menu.win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(main_menu.win, 2, 39, ACS_RTEE);
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
post_menu(main_menu.menu);
|
||||
wrefresh(main_menu.win);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 2, 0,
|
||||
@@ -79,32 +76,35 @@ void main_menu() {
|
||||
while ((c = getch()) != KEY_F(1)) {
|
||||
switch (c) {
|
||||
case KEY_DOWN:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case KEY_NPAGE:
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
case 'j':
|
||||
menu_driver(main_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
case 'k':
|
||||
menu_driver(main_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: // ENTER
|
||||
clear();
|
||||
choicesFuncs[item_index(current_item(my_menu))]();
|
||||
pos_menu_cursor(my_menu);
|
||||
if (item_index(current_item(main_menu.menu)) == n_choices - 1) {
|
||||
goto close_menu;
|
||||
}
|
||||
choicesFuncs[item_index(current_item(main_menu.menu))]();
|
||||
pos_menu_cursor(main_menu.menu);
|
||||
refresh();
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(main_menu.win);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(main_menu.win);
|
||||
}
|
||||
close_menu:
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
unpost_menu(main_menu.menu);
|
||||
free_menu(main_menu.menu);
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
free_item(main_menu.items[i]);
|
||||
endwin();
|
||||
}
|
||||
+1
-1
@@ -40,7 +40,7 @@ void marks_page() {
|
||||
json resp_from_api;
|
||||
{
|
||||
std::string endpoint = "api/3/marks";
|
||||
resp_from_api = bakaapi::get_data_from_endpoint(endpoint);
|
||||
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, GET);
|
||||
}
|
||||
size_t size_my_wins = resp_from_api["Subjects"].size();
|
||||
WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins];
|
||||
|
||||
@@ -26,6 +26,14 @@ void delete_all(std::vector<allocation> *allocated) {
|
||||
delete[] panels;
|
||||
break;
|
||||
}
|
||||
case ITEM_ARRAY: {
|
||||
ITEM **items = static_cast<ITEM **>(allocated->at(i).ptr);
|
||||
for (std::size_t j = 0; j < allocated->at(i).size; j++) {
|
||||
free_item(items[j]);
|
||||
}
|
||||
delete[] items;
|
||||
break;
|
||||
}
|
||||
case GENERIC_ARRAY:
|
||||
delete[] static_cast<char *>(allocated->at(i).ptr);
|
||||
break;
|
||||
@@ -35,6 +43,9 @@ void delete_all(std::vector<allocation> *allocated) {
|
||||
case PANEL_TYPE:
|
||||
del_panel(static_cast<PANEL *>(allocated->at(i).ptr));
|
||||
break;
|
||||
case MENU_TYPE:
|
||||
free_menu(static_cast<MENU *>(allocated->at(i).ptr));
|
||||
break;
|
||||
case GENERIC_TYPE:
|
||||
delete static_cast<char *>(allocated->at(i).ptr);
|
||||
break;
|
||||
|
||||
@@ -3,13 +3,16 @@
|
||||
#define _ba_me_hg_
|
||||
|
||||
#include <cstddef>
|
||||
#include <menu.h>
|
||||
#include <vector>
|
||||
enum AllocationType {
|
||||
WINDOW_ARRAY,
|
||||
PANEL_ARRAY,
|
||||
ITEM_ARRAY,
|
||||
GENERIC_ARRAY,
|
||||
WINDOW_TYPE,
|
||||
PANEL_TYPE,
|
||||
MENU_TYPE,
|
||||
GENERIC_TYPE
|
||||
};
|
||||
|
||||
|
||||
+99
-16
@@ -11,35 +11,63 @@
|
||||
#include <curl/curl.h>
|
||||
#include <curses.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
#define ROUND_UP(x, y) ((((x) + (y) - 1)) & ~((y) - 1))
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
// metods
|
||||
enum metod {
|
||||
GET,
|
||||
POST,
|
||||
};
|
||||
|
||||
std::string access_token;
|
||||
|
||||
CURL *curl = curl_easy_init();
|
||||
|
||||
// Callback function to write data into a std::string
|
||||
size_t WriteCallback(void *contents, size_t size, size_t nmemb,
|
||||
std::string *userp) {
|
||||
size_t WriteCallback_to_string(void *contents, size_t size, size_t nmemb,
|
||||
void *userp) {
|
||||
size_t totalSize = size * nmemb;
|
||||
userp->append((char *)contents, totalSize);
|
||||
static_cast<std::string *>(userp)->append((char *)contents, totalSize);
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
|
||||
std::string req_data) {
|
||||
size_t WriteCallback_to_file(void *contents, size_t size, size_t nmemb,
|
||||
void *userp) {
|
||||
const size_t sector_size = 4096;
|
||||
size_t total = size * nmemb;
|
||||
void *aligned_buf = aligned_alloc(sector_size, ROUND_UP(total, sector_size));
|
||||
|
||||
// Zero out the buffer before copying
|
||||
memset(aligned_buf, 0, ROUND_UP(total, sector_size));
|
||||
memcpy(aligned_buf, contents, total);
|
||||
|
||||
// Cast userp to fstream*
|
||||
std::fstream *fileStream = static_cast<std::fstream *>(userp);
|
||||
|
||||
// Write to the file using fstream
|
||||
fileStream->write(static_cast<char *>(aligned_buf), total);
|
||||
|
||||
// Check if write was successful
|
||||
if (fileStream->fail()) {
|
||||
free(aligned_buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(aligned_buf);
|
||||
return total;
|
||||
}
|
||||
|
||||
std::tuple<std::string, int> send_curl_request(
|
||||
std::string endpoint, metod type, std::string req_data,
|
||||
size_t (*WriteCallback_function)(void *, size_t, size_t,
|
||||
void *) = WriteCallback_to_string,
|
||||
std::fstream *fileStream = nullptr) {
|
||||
std::string response;
|
||||
std::string url = baka_api_url + endpoint;
|
||||
if (type == GET) {
|
||||
@@ -48,15 +76,19 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
|
||||
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback_function);
|
||||
if (fileStream) {
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fileStream);
|
||||
} else {
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||
|
||||
}
|
||||
if (config.ignoressl) {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate");
|
||||
|
||||
struct curl_slist *headers = NULL;
|
||||
headers = curl_slist_append(
|
||||
@@ -83,7 +115,15 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
|
||||
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
|
||||
safe_exit(20);
|
||||
}
|
||||
curl_easy_perform(curl); // Perform the request
|
||||
CURLcode curl_return_code = curl_easy_perform(curl);
|
||||
if (curl_return_code != CURLE_OK) {
|
||||
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
|
||||
<< curl_easy_strerror(curl_return_code) << "\n";
|
||||
safe_exit(21);
|
||||
}
|
||||
if (fileStream) {
|
||||
fileStream->close();
|
||||
}
|
||||
|
||||
int http_code = 0;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||
@@ -159,7 +199,7 @@ void is_access_token_empty() {
|
||||
}
|
||||
|
||||
// supports all endpoints that only require access_token
|
||||
json get_data_from_endpoint(std::string &endpoint,
|
||||
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
||||
std::string additional_data) {
|
||||
is_access_token_empty();
|
||||
access_token_refreshed:
|
||||
@@ -169,7 +209,7 @@ access_token_refreshed:
|
||||
req_data.append(std::format("&{}", additional_data));
|
||||
}
|
||||
|
||||
auto [response, http_code] = send_curl_request(endpoint, GET, req_data);
|
||||
auto [response, http_code] = send_curl_request(endpoint, metod, req_data);
|
||||
|
||||
if (http_code != 200) {
|
||||
refresh_access_token();
|
||||
@@ -178,4 +218,47 @@ access_token_refreshed:
|
||||
|
||||
return json::parse(response);
|
||||
}
|
||||
|
||||
int download_attachment(std::string id, std::string path) {
|
||||
if (config.verbose) {
|
||||
std::clog << "downloading attachment please wait...\n";
|
||||
}
|
||||
if (path[0] == '~') {
|
||||
path = std::string(std::getenv("HOME")) + path.substr(1);
|
||||
}
|
||||
|
||||
std::string savedir_path = std::filesystem::path(path).parent_path().string();
|
||||
if (!std::filesystem::exists(savedir_path)) {
|
||||
if (!std::filesystem::create_directories(savedir_path)) {
|
||||
std::cerr << RED "[ERROR] " RESET
|
||||
<< "Failed to create directory: " << savedir_path << "\n";
|
||||
safe_exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
std::fstream fileStream(path, std::ios::out | std::ios::binary);
|
||||
if (!fileStream.is_open()) {
|
||||
std::cerr << RED "[ERROR] " RESET << "Failed to open file for writing\n";
|
||||
safe_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
is_access_token_empty();
|
||||
access_token_refreshed:
|
||||
std::string req_data =
|
||||
std::format("Authorization=Bearer&access_token={}", access_token);
|
||||
|
||||
auto [response, http_code] =
|
||||
send_curl_request(std::format("/api/3/komens/attachment/{}", id), GET,
|
||||
req_data, WriteCallback_to_file, &fileStream);
|
||||
if (http_code != 200) {
|
||||
if (config.verbose) {
|
||||
std::clog << BLUE "[LOG] " RESET << "download failed " << http_code
|
||||
<< " is non 200 response\n";
|
||||
}
|
||||
refresh_access_token();
|
||||
goto access_token_refreshed;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace bakaapi
|
||||
@@ -5,12 +5,20 @@
|
||||
#ifndef _ba_ne_hg_
|
||||
#define _ba_ne_hg_
|
||||
|
||||
// metods
|
||||
enum metod {
|
||||
GET,
|
||||
POST,
|
||||
};
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
extern CURL *curl;
|
||||
namespace bakaapi {
|
||||
void login(std::string username, std::string password);
|
||||
void refresh_access_token();
|
||||
json get_data_from_endpoint(std::string &endpoint, std::string data = "");
|
||||
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
||||
std::string additional_data = "");
|
||||
int download_attachment(std::string id, std::string path);
|
||||
} // namespace bakaapi
|
||||
#endif
|
||||
+2
-1
@@ -100,7 +100,8 @@ reload_for_new_week:
|
||||
std::string date_string = "date=" + date_stringstream.str();
|
||||
std::string endpoint = "api/3/timetable/actual";
|
||||
|
||||
json resp_from_api = bakaapi::get_data_from_endpoint(endpoint, date_string);
|
||||
json resp_from_api =
|
||||
bakaapi::get_data_from_endpoint(endpoint, GET, date_string);
|
||||
|
||||
// this may be unnecessary but i dont have enaugh data to test it
|
||||
// it sorts the hours by start time
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define _ba_ty_hg_
|
||||
|
||||
#include <istream>
|
||||
#include <menu.h>
|
||||
struct Config {
|
||||
bool verbose = false;
|
||||
bool ignoressl = false;
|
||||
@@ -144,4 +145,10 @@ struct SelectorType {
|
||||
: x(xArg, min_limit_x, max_limit_x), y(yArg, min_limit_y, max_limit_y) {}
|
||||
};
|
||||
|
||||
struct complete_menu {
|
||||
WINDOW *win;
|
||||
ITEM **items;
|
||||
MENU *menu;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user