summaryrefslogtreecommitdiff
path: root/src/util.cpp
blob: 09bb368a1ecb3ef64a231645db01992fbc042749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "util.hpp"

#include <array>
#include <cstring>
#include <filesystem>

#include <gtkmm.h>

void LaunchBrowser(const Glib::ustring &url) {
    GError *err = nullptr;
    if (!gtk_show_uri_on_window(nullptr, url.c_str(), GDK_CURRENT_TIME, &err))
        printf("failed to open uri: %s\n", err->message);
}

void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw, int clamph) {
    const auto frac = static_cast<float>(inw) / static_cast<float>(inh);

    outw = inw;
    outh = inh;

    if (outw > clampw) {
        outw = clampw;
        outh = clampw / frac;
    }

    if (outh > clamph) {
        outh = clamph;
        outw = clamph * frac;
    }
}

std::vector<uint8_t> ReadWholeFile(const std::string &path) {
    std::vector<uint8_t> ret;
    FILE *fp = std::fopen(path.c_str(), "rb");
    if (fp == nullptr)
        return ret;
    std::fseek(fp, 0, SEEK_END);
    int len = std::ftell(fp);
    std::rewind(fp);
    ret.resize(len);
    std::fread(ret.data(), 1, ret.size(), fp);
    std::fclose(fp);
    return ret;
}

std::string HumanReadableBytes(uint64_t bytes) {
    constexpr static const char *x[] = { "B", "KB", "MB", "GB", "TB" };
    int order = 0;
    while (bytes >= 1000 && order < 4) { // 4=len(x)-1
        order++;
        bytes /= 1000;
    }
    return std::to_string(bytes) + x[order];
}

int GetTimezoneOffset() {
    std::time_t secs;
    std::time(&secs);
    std::tm *tptr = std::localtime(&secs);
    std::time_t local_secs = std::mktime(tptr);
    tptr = std::gmtime(&secs);
    std::time_t gmt_secs = std::mktime(tptr);
    return static_cast<int>(local_secs - gmt_secs);
}

std::string FormatISO8601(const std::string &in, int extra_offset, const std::string &fmt) {
    int yr, mon, day, hr, min, sec, tzhr, tzmin;
    float milli;
    std::sscanf(in.c_str(), "%d-%d-%dT%d:%d:%d%f+%d:%d",
                &yr, &mon, &day, &hr, &min, &sec, &milli, &tzhr, &tzmin);
    std::tm tm {};
    tm.tm_year = yr - 1900;
    tm.tm_mon = mon - 1;
    tm.tm_mday = day;
    tm.tm_hour = hr;
    tm.tm_min = min;
    tm.tm_sec = sec;
    tm.tm_wday = 0;
    tm.tm_yday = 0;
    tm.tm_isdst = -1;
    int offset = GetTimezoneOffset();
    tm.tm_sec += offset + extra_offset;
    mktime(&tm);
    std::array<char, 512> tmp {};
    std::strftime(tmp.data(), sizeof(tmp), fmt.c_str(), &tm);
    return tmp.data();
}

void ScrollListBoxToSelected(Gtk::ListBox &list) {
    auto cb = [&list]() -> bool {
        const auto selected = list.get_selected_row();
        if (selected == nullptr) return false;
        int x, y;
        selected->translate_coordinates(list, 0, 0, x, y);
        if (y < 0) return false;
        const auto adj = list.get_adjustment();
        if (!adj) return false;
        int min, nat;
        selected->get_preferred_height(min, nat);
        adj->set_value(y - (adj->get_page_size() - nat) / 2.0);

        return false;
    };
    Glib::signal_idle().connect(sigc::track_obj(cb, list));
}

// surely theres a better way to do this
bool StringContainsCaseless(const Glib::ustring &str, const Glib::ustring &sub) {
    const auto regex = Glib::Regex::create(Glib::Regex::escape_string(sub), Glib::REGEX_CASELESS);
    return regex->match(str);
}

std::string IntToCSSColor(int color) {
    int r = (color & 0xFF0000) >> 16;
    int g = (color & 0x00FF00) >> 8;
    int b = (color & 0x0000FF) >> 0;
    std::stringstream ss;
    ss << std::hex << std::setw(2) << std::setfill('0') << r
       << std::hex << std::setw(2) << std::setfill('0') << g
       << std::hex << std::setw(2) << std::setfill('0') << b;
    return ss.str();
}

Gdk::RGBA IntToRGBA(int color) {
    Gdk::RGBA ret;
    ret.set_red(((color & 0xFF0000) >> 16) / 255.0);
    ret.set_green(((color & 0x00FF00) >> 8) / 255.0);
    ret.set_blue(((color & 0x0000FF) >> 0) / 255.0);
    ret.set_alpha(255.0);
    return ret;
}

// so widgets can modify the menu before it is displayed
// maybe theres a better way to do this idk
void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu, const sigc::slot<void()> &pre_callback) {
    sigc::signal<void()> signal;
    signal.connect(pre_callback);
    widget->signal_button_press_event().connect([&menu, signal](GdkEventButton *ev) -> bool {
        if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
            signal.emit();
            menu.popup_at_pointer(reinterpret_cast<const GdkEvent *>(ev));
            return true;
        }
        return false;
        // clang-format off
    }, false);
    // clang-format on
}

std::vector<std::string> StringSplit(const std::string &str, const char *delim) {
    std::vector<std::string> parts;
    char *token = std::strtok(const_cast<char *>(str.c_str()), delim);
    while (token != nullptr) {
        parts.emplace_back(token);
        token = std::strtok(nullptr, delim);
    }
    return parts;
}

std::string GetExtension(std::string url) {
    url = StringSplit(url, "?")[0];
    url = StringSplit(url, "/").back();
    return url.find('.') != std::string::npos ? url.substr(url.find_last_of('.')) : "";
}

bool IsURLViewableImage(const std::string &url) {
    std::string lw_url = url;
    std::transform(lw_url.begin(), lw_url.end(), lw_url.begin(), ::tolower);

    const auto ext = GetExtension(lw_url);
    static const char *exts[] = { ".jpeg",
                                  ".jpg",
                                  ".png", nullptr };
    const char *str = ext.c_str();
    for (int i = 0; exts[i] != nullptr; i++)
        if (strcmp(str, exts[i]) == 0)
            return true;
    return false;
}

void AddPointerCursor(Gtk::Widget &widget) {
    widget.signal_realize().connect([&widget]() {
        auto window = widget.get_window();
        auto display = window->get_display();
        auto cursor = Gdk::Cursor::create(display, "pointer");
        window->set_cursor(cursor);
    });
}

bool util::IsFolder(std::string_view path) {
    std::error_code ec;
    const auto status = std::filesystem::status(path, ec);
    if (ec) return false;
    return status.type() == std::filesystem::file_type::directory;
}

bool util::IsFile(std::string_view path) {
    std::error_code ec;
    const auto status = std::filesystem::status(path, ec);
    if (ec) return false;
    return status.type() == std::filesystem::file_type::regular;
}

constexpr bool IsLeapYear(int year) {
    if (year % 4 != 0) return false;
    if (year % 100 != 0) return true;
    return (year % 400) == 0;
}

constexpr static int SecsPerMinute = 60;
constexpr static int SecsPerHour = 3600;
constexpr static int SecsPerDay = 86400;
constexpr static std::array<int, 12> DaysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// may god smite whoever is responsible for the absolutely abominable api that is C time functions
// i shouldnt have to write this. mktime ALMOST works but it adds the current timezone offset. WHY???
uint64_t util::TimeToEpoch(int year, int month, int day, int hour, int minute, int seconds) {
    uint64_t secs = 0;
    for (int y = 1970; y < year; ++y)
        secs += (IsLeapYear(y) ? 366 : 365) * SecsPerDay;
    for (int m = 1; m < month; ++m) {
        secs += DaysOfMonth[m - 1] * SecsPerDay;
        if (m == 2 && IsLeapYear(year)) secs += SecsPerDay;
    }
    secs += (day - 1) * SecsPerDay;
    secs += hour * SecsPerHour;
    secs += minute * SecsPerMinute;
    secs += seconds;
    return secs;
}