try loading resources from share, fallback to cwd

This commit is contained in:
ouwou 2021-06-30 18:15:03 -04:00
parent 220aa6d13a
commit 698ec52d5c
12 changed files with 80 additions and 15 deletions

View File

@ -22,7 +22,7 @@
Abaddon::Abaddon()
: m_settings("abaddon.ini")
, m_emojis("res/emojis.bin")
, m_emojis(GetResPath() + "/emojis.bin")
, m_discord(m_settings.GetUseMemoryDB()) { // stupid but easy
LoadFromSettings();
@ -426,6 +426,24 @@ void Abaddon::on_user_menu_remove_recipient() {
m_discord.RemoveGroupDMRecipient(m_main_window->GetChatActiveChannel(), m_shown_user_menu_id);
}
std::string Abaddon::GetCSSPath() {
const static auto path = Platform::FindResourceFolder() + "/css";
return path;
}
std::string Abaddon::GetResPath() {
const static auto path = Platform::FindResourceFolder() + "/res";
return path;
}
std::string Abaddon::GetCSSPath(const std::string &path) {
return GetCSSPath() + path;
}
std::string Abaddon::GetResPath(const std::string &path) {
return GetResPath() + path;
}
void Abaddon::ActionConnect() {
if (!m_discord.IsStarted())
StartDiscord();
@ -643,11 +661,11 @@ void Abaddon::ActionReloadSettings() {
void Abaddon::ActionReloadCSS() {
try {
Gtk::StyleContext::remove_provider_for_screen(Gdk::Screen::get_default(), m_css_provider);
m_css_provider->load_from_path(m_settings.GetMainCSS());
m_css_provider->load_from_path(GetCSSPath("/" + m_settings.GetMainCSS()));
Gtk::StyleContext::add_provider_for_screen(Gdk::Screen::get_default(), m_css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Gtk::StyleContext::remove_provider_for_screen(Gdk::Screen::get_default(), m_css_low_provider);
m_css_low_provider->load_from_path("./css/application-low-priority.css");
m_css_low_provider->load_from_path(GetCSSPath("/application-low-priority.css"));
Gtk::StyleContext::add_provider_for_screen(Gdk::Screen::get_default(), m_css_low_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1);
} catch (Glib::Error &e) {
Gtk::MessageDialog dlg(*m_main_window, "css failed to load (" + e.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);

View File

@ -86,6 +86,11 @@ public:
void ManageHeapWindow(Gtk::Window *window);
static std::string GetCSSPath();
static std::string GetResPath();
static std::string GetCSSPath(const std::string &path);
static std::string GetResPath(const std::string &path);
protected:
void ShowGuildVerificationGateDialog(Snowflake guild_id);

View File

@ -21,8 +21,9 @@ ChatInputIndicator::ChatInputIndicator()
m_label.show();
// try loading gif
if (!std::filesystem::exists("./res/typing_indicator.gif")) return;
auto gif_data = ReadWholeFile("./res/typing_indicator.gif");
const static auto path = Abaddon::GetResPath("/typing_indicator.gif");
if (!std::filesystem::exists(path)) return;
auto gif_data = ReadWholeFile(path);
auto loader = Gdk::PixbufLoader::create();
loader->signal_size_prepared().connect([&](int inw, int inh) {
int w, h;

View File

@ -17,7 +17,8 @@ MemberListUserRow::MemberListUserRow(const std::optional<GuildData> &guild, cons
static bool crown = Abaddon::Get().GetSettings().GetShowOwnerCrown();
if (crown && guild.has_value() && guild->OwnerID == data.ID) {
try {
auto pixbuf = Gdk::Pixbuf::create_from_file("./res/crown.png", 12, 12);
const static auto crown_path = Abaddon::GetResPath("/crown.png");
auto pixbuf = Gdk::Pixbuf::create_from_file(crown_path, 12, 12);
m_crown = Gtk::manage(new Gtk::Image(pixbuf));
m_crown->set_valign(Gtk::ALIGN_CENTER);
m_crown->set_margin_end(8);

View File

@ -15,9 +15,10 @@ RateLimitIndicator::RateLimitIndicator()
add(m_img);
m_label.show();
if (std::filesystem::exists("./res/clock.png")) {
const static auto clock_path = Abaddon::GetResPath("/clock.png");
if (std::filesystem::exists(clock_path)) {
try {
const auto pixbuf = Gdk::Pixbuf::create_from_file("./res/clock.png");
const auto pixbuf = Gdk::Pixbuf::create_from_file(clock_path);
int w, h;
GetImageDimensions(pixbuf->get_width(), pixbuf->get_height(), w, h, 20, 10);
m_img.property_pixbuf() = pixbuf->scale_simple(w, h, Gdk::INTERP_BILINEAR);

View File

@ -1,5 +1,6 @@
#include "imgmanager.hpp"
#include "util.hpp"
#include "abaddon.hpp"
ImageManager::ImageManager() {
m_cb_dispatcher.connect(sigc::mem_fun(*this, &ImageManager::RunCallbacks));
@ -112,7 +113,7 @@ Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetPlaceholder(int size) {
return m_pixs.at(name);
try {
auto buf = Gdk::Pixbuf::create_from_file("res/decamarks.png", size, size);
auto buf = Gdk::Pixbuf::create_from_file(Abaddon::Get().GetResPath() + "/decamarks.png", size, size);
m_pixs[name] = buf;
return buf;
} catch (std::exception &e) {

View File

@ -1,6 +1,14 @@
#include "platform.hpp"
#include <string>
#include <fstream>
#include <filesystem>
bool 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;
}
#if defined(_WIN32) && defined(_MSC_VER)
#include <Windows.h>
@ -59,3 +67,29 @@ bool Platform::SetupFonts() {
return true;
}
#endif
#if defined(_WIN32)
std::string Platform::FindResourceFolder() {
return ".";
}
#elif defined(__linux__)
std::string Platform::FindResourceFolder() {
static std::string path;
static bool found = false;
if (found) return path;
if (IsFolder("/usr/share/abaddon/res") && IsFolder("/usr/share/abaddon/css")) {
path = "/usr/share/abaddon";
} else {
puts("resources are not in /usr/share/abaddon, will try to load from cwd");
path = ".";
}
found = true;
return path;
}
#else
std::string Platform::FindResourceFolder() {
puts("unknown OS, trying to load resources from cwd");
}
#endif

View File

@ -1,5 +1,7 @@
#pragma once
#include <string>
namespace Platform {
bool SetupFonts();
std::string FindResourceFolder();
}

View File

@ -75,7 +75,7 @@ bool SettingsManager::GetPrefetch() const {
}
std::string SettingsManager::GetMainCSS() const {
return GetSettingString("gui", "css", "./css/main.css");
return GetSettingString("gui", "css", "main.css");
}
bool SettingsManager::GetShowAnimations() const {

View File

@ -116,7 +116,8 @@ GuildSettingsMembersListItem::GuildSettingsMembersListItem(const GuildData &guil
static bool crown = Abaddon::Get().GetSettings().GetShowOwnerCrown();
if (crown && guild.OwnerID == member.User->ID) {
try {
auto pixbuf = Gdk::Pixbuf::create_from_file("./res/crown.png", 12, 12);
const static auto crown_path = Abaddon::GetResPath("/crown.png");
auto pixbuf = Gdk::Pixbuf::create_from_file(crown_path, 12, 12);
m_crown = Gtk::manage(new Gtk::Image(pixbuf));
m_crown->set_valign(Gtk::ALIGN_CENTER);
m_crown->set_margin_start(10);

View File

@ -7,7 +7,7 @@ ConnectionItem::ConnectionItem(const ConnectionData &conn)
, m_box(Gtk::ORIENTATION_HORIZONTAL) {
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
try {
pixbuf = Gdk::Pixbuf::create_from_file("./res/" + conn.Type + ".png", 32, 32);
pixbuf = Gdk::Pixbuf::create_from_file(Abaddon::GetResPath("/" + conn.Type + ".png"), 32, 32);
} catch (const Glib::Exception &e) {}
std::string url;
if (conn.Type == "github")
@ -53,7 +53,8 @@ ConnectionItem::ConnectionItem(const ConnectionData &conn)
m_overlay.add(m_box);
if (conn.IsVerified) {
try {
static auto pb = Gdk::Pixbuf::create_from_file("./res/checkmark.png", 24, 24);
const static auto checkmarks_path = Abaddon::GetResPath("/checkmark.png");
static auto pb = Gdk::Pixbuf::create_from_file(checkmarks_path, 24, 24);
m_check = Gtk::manage(new Gtk::Image(pb));
m_check->get_style_context()->add_class("profile-connection-check");
m_check->set_margin_end(25);

View File

@ -111,9 +111,9 @@ void ProfileWindow::OnFetchProfile(const UserProfileData &data) {
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
try {
if (name == "verifiedbot")
pixbuf = Gdk::Pixbuf::create_from_file("./res/checkmark.png", 24, 24);
pixbuf = Gdk::Pixbuf::create_from_file(Abaddon::GetResPath("/checkmark.png"), 24, 24);
else
pixbuf = Gdk::Pixbuf::create_from_file("./res/" + name + ".png", 24, 24);
pixbuf = Gdk::Pixbuf::create_from_file(Abaddon::GetResPath("/" + name + ".png"), 24, 24);
} catch (const Glib::Exception &e) {
pixbuf = Abaddon::Get().GetImageManager().GetPlaceholder(24);
}