Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CURLWrapper module and more options for time zone service. #23

Merged
merged 19 commits into from
Jun 6, 2024
Merged
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM devkitpro/devkitppc

COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:20240505 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libcurlwrapper:20240505 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libnotifications:20240426 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:20240505 /artifacts $DEVKITPRO

RUN git config --global --add safe.directory /project

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ LDFLAGS = -g \
-Wl,-Map,$(notdir $*.map) \
$(CXXFLAGS)

LIBS := -lnotifications -lwups -lwut
LIBS := -lcurlwrapper -lnotifications -lwups -lwut

#-------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level
# containing include and lib
#-------------------------------------------------------------------------------
LIBDIRS := $(WUMS_ROOT) $(WUPS_ROOT) $(WUT_ROOT)
LIBDIRS := $(WUMS_ROOT) $(WUPS_ROOT) $(WUT_ROOT) $(PORTLIBS)

#-------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
Expand Down
4 changes: 4 additions & 0 deletions include/cfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace cfg {
extern const char* sync;
extern const char* threads;
extern const char* tolerance;
extern const char* tz_service;
extern const char* utc_offset;
}

Expand All @@ -29,6 +30,7 @@ namespace cfg {
extern const char* sync;
extern const char* threads;
extern const char* tolerance;
extern const char* tz_service;
extern const char* utc_offset;
}

Expand All @@ -40,6 +42,7 @@ namespace cfg {
extern const bool sync;
extern const int threads;
extern const std::chrono::milliseconds tolerance;
extern const int tz_service;
}


Expand All @@ -50,6 +53,7 @@ namespace cfg {
extern bool sync;
extern int threads;
extern std::chrono::milliseconds tolerance;
extern int tz_service;
extern std::chrono::minutes utc_offset;


Expand Down
80 changes: 80 additions & 0 deletions include/curl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT

#ifndef CURL_HPP
#define CURL_HPP

#include <memory>
#include <stdexcept> // runtime_error
#include <string>

#include <curl/curl.h>


namespace curl {

struct error : std::runtime_error {

CURLcode code;

error(CURLcode code);

};


struct global {

global();

~global();

};


class handle {

CURL* h;
std::unique_ptr<char[]> error_buffer;

void init();

// static int sockopt_callback(handle* h, curl_socket_t fd, curlsocktype purpose);

static
std::size_t
write_callback(char* buffer, std::size_t size, std::size_t nmemb, void* ctx);

protected:

virtual std::size_t on_recv(const char* buffer, std::size_t size);


public:

std::string result;


handle();

handle(const handle& other);

~handle();


void setopt(CURLoption option, bool arg);
void setopt(CURLoption option, const std::string& arg);


// convenience setters

void set_followlocation(bool enable);
void set_url(const std::string& url);
void set_useragent(const std::string& agent);

void perform();

};

} // namespace curl


#endif
35 changes: 31 additions & 4 deletions include/timezone_query_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,46 @@

#include <memory>

#include "wupsxx/text_item.hpp"
#include "wupsxx/item.hpp"
#include "wupsxx/var_watch.hpp"


struct timezone_query_item : wups::config::text_item {
class timezone_query_item : public wups::config::item {

timezone_query_item();
// We store the geolocation option as an integer, no point in parsing any complex
// string since we need specific implementations for each service.

wups::config::var_watch<int> variable;
const int default_value;
std::string text;

public:

timezone_query_item(const std::string& key,
const std::string& label,
int& variable,
int default_value);

static
std::unique_ptr<timezone_query_item> create();
std::unique_ptr<timezone_query_item> create(const std::string& key,
const std::string& label,
int& variable,
int default_value);


int get_display(char* buf, std::size_t size) const override;

int get_selected_display(char* buf, std::size_t size) const override;

void restore() override;

void on_input(WUPSConfigSimplePadData input,
WUPS_CONFIG_SIMPLE_INPUT repeat) override;

private:

void on_changed();

void run();

};
Expand Down
10 changes: 9 additions & 1 deletion include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ namespace utils {
};


int
get_num_tz_services();


const char*
get_tz_service_name(int idx);


std::pair<std::string,
std::chrono::minutes>
fetch_timezone();
fetch_timezone(int idx);

} // namespace utils

Expand Down
6 changes: 6 additions & 0 deletions source/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cfg.hpp"

#include "logging.hpp"
#include "nintendo_glyphs.h"
#include "time_utils.hpp"
#include "utils.hpp"
#include "wupsxx/storage.hpp"
Expand All @@ -26,6 +27,7 @@ namespace cfg {
const char* sync = "sync";
const char* threads = "threads";
const char* tolerance = "tolerance";
const char* tz_service = "tz_service";
const char* utc_offset = "utc_offset";
}

Expand All @@ -38,6 +40,7 @@ namespace cfg {
const char* sync = "Syncing Enabled";
const char* threads = "Background Threads";
const char* tolerance = "Tolerance";
const char* tz_service = "Detect Time Zone (press " NIN_GLYPH_BTN_A ")";
const char* utc_offset = "Time Offset (UTC)";
}

Expand All @@ -50,6 +53,7 @@ namespace cfg {
const bool sync = false;
const int threads = 4;
const milliseconds tolerance = 500ms;
const int tz_service = 0;
}


Expand All @@ -60,6 +64,7 @@ namespace cfg {
bool sync = defaults::sync;
int threads = defaults::threads;
milliseconds tolerance = defaults::tolerance;
int tz_service = defaults::tz_service;
minutes utc_offset = 0min;


Expand Down Expand Up @@ -87,6 +92,7 @@ namespace cfg {
load_or_init(key::sync, sync);
load_or_init(key::threads, threads);
load_or_init(key::tolerance, tolerance);
load_or_init(key::tz_service, tz_service);
load_or_init(key::utc_offset, utc_offset);
// logging::printf("Loaded settings.");
}
Expand Down
5 changes: 4 additions & 1 deletion source/config_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ make_config_screen()
cfg::label::utc_offset,
cfg::utc_offset));

cat.add(timezone_query_item::create());
cat.add(timezone_query_item::create(cfg::key::tz_service,
cfg::label::tz_service,
cfg::tz_service,
cfg::defaults::tz_service));

cat.add(bool_item::create(cfg::key::auto_tz,
cfg::label::auto_tz,
Expand Down
6 changes: 3 additions & 3 deletions source/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ namespace core {

if (cfg::auto_tz) {
try {
auto [name, offset] = utils::fetch_timezone();
auto [name, offset] = utils::fetch_timezone(cfg::tz_service);
if (offset != cfg::utc_offset) {
cfg::set_and_store_utc_offset(offset);
notify::info(notify::level::verbose,
"Updated timezone to " + name +
"Updated time zone to " + name +
"(" + time_utils::tz_offset_to_string(offset) + ")");
}
}
catch (std::exception& e) {
notify::error(notify::level::verbose,
"Failed to update timezone: "s + e.what());
"Failed to update time zone: "s + e.what());
}
}

Expand Down
Loading