# HG changeset patch # User Paper # Date 1701909753 18000 # Node ID 649786bae914590d94095bd8e7983c2ae29a7aea # Parent 168382a89b21de662fb0e8483136e0129c7d2353 *: etc. code cleanup I've removed most macros and stuff dep/animia: [UNTESTED] use raw C++ instead of Objective-C++ diff -r 168382a89b21 -r 649786bae914 CMakeLists.txt --- a/CMakeLists.txt Wed Dec 06 13:44:36 2023 -0500 +++ b/CMakeLists.txt Wed Dec 06 19:42:33 2023 -0500 @@ -104,7 +104,6 @@ src/services/anilist.cc # Tracking - src/track/constants.cc src/track/media.cc # Qt resources diff -r 168382a89b21 -r 649786bae914 dep/animia/CMakeLists.txt --- a/dep/animia/CMakeLists.txt Wed Dec 06 13:44:36 2023 -0500 +++ b/dep/animia/CMakeLists.txt Wed Dec 06 19:42:33 2023 -0500 @@ -30,28 +30,16 @@ # check anyway. if (HAVE_COREFOUNDATION) list(APPEND DEFINES HAVE_COREFOUNDATION) + list(APPEND SRC_FILES src/win/quartz.cc) - find_library(FOUNDATION_LIBRARY Foundation) - list(APPEND LIBRARIES ${FOUNDATION_LIBRARY}) + find_library(FOUNDATION_LIBRARY Foundation) + find_library(COREGRAPHICS_LIBRARY CoreGraphics) + find_library(APPKIT_LIBRARY AppKit) + list(APPEND LIBRARIES ${FOUNDATION_LIBRARY} ${COREGRAPHICS_LIBRARY} ${APPKIT_LIBRARY}) else() message(STATUS "You don't have Core Foundation. How? What kind of voodoo magic did you do to cause this?") message(WARNING "LaunchServices support will not be compiled.") endif() - - check_language(OBJCXX) - if(CMAKE_OBJCXX_COMPILER) - enable_language(OBJCXX) - list(APPEND SRC_FILES - src/win/quartz.mm - ) - - # we likely already have Foundation at this point. - find_library(COREGRAPHICS_LIBRARY CoreGraphics) - find_library(APPKIT_LIBRARY AppKit) - list(APPEND LIBRARIES ${COREGRAPHICS_LIBRARY} ${APPKIT_LIBRARY}) - else() # NOT CMAKE_OBJCXX_COMPILER - message(WARNING "An Objective-C++ compiler couldn't be found! Window enumeration support will not be compiled.") - endif() # CMAKE_OBJCXX_COMPILER elseif(WIN32) list(APPEND DEFINES WIN32) list(APPEND SRC_FILES diff -r 168382a89b21 -r 649786bae914 dep/animia/include/animia/util/osx.h --- a/dep/animia/include/animia/util/osx.h Wed Dec 06 13:44:36 2023 -0500 +++ b/dep/animia/include/animia/util/osx.h Wed Dec 06 19:42:33 2023 -0500 @@ -4,8 +4,40 @@ #include "animia/types.h" #include +#ifdef HAVE_COREFOUNDATION +#include +#endif + namespace animia::internal::osx::util { +#ifdef HAVE_COREFOUNDATION + +/* I don't want to have to call CFRelease */ +template +struct CFDeleter { + using pointer = T; + void operator()(pointer p) { CFRelease(p); } +} + +template +typedef CFReference = std::unique_ptr; + +template +bool GetCFNumber(const CFNumber& num, T& result) { + if (!num) + return false; + + CFNumberType type = CFNumberGetType(num); + if (!CFNumberGetValue(num, type, result)) + return false; + + return true; +} + +bool StringFromCFString(CFStringRef string, std::string& result); + +#endif // HAVE_COREFOUNDATION + bool GetProcessName(pid_t pid, std::string& result); } diff -r 168382a89b21 -r 649786bae914 dep/animia/src/util/osx.cc --- a/dep/animia/src/util/osx.cc Wed Dec 06 13:44:36 2023 -0500 +++ b/dep/animia/src/util/osx.cc Wed Dec 06 19:42:33 2023 -0500 @@ -3,23 +3,9 @@ #include #include -#ifdef HAVE_COREFOUNDATION -#include -#endif - namespace animia::internal::osx::util { #ifdef HAVE_COREFOUNDATION -/* I don't want to have to call CFRelease */ -template -struct CFDeleter { - using pointer = T; - void operator()(pointer p) { CFRelease(p); } -} - -template -typedef CFReference = std::unique_ptr; - /* all of these LaunchServices things use *internal functions* that are subject * to change. Granted, it's not very likely that these will change very much * because I'm fairly sure Apple uses them lots in their own internal code. @@ -93,6 +79,17 @@ return true; } + +bool StringFromCFString(CFStringRef string, std::string& result) { + if (!string) + return false; + + result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); + if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) + return false; + + return true; +} #endif // HAVE_COREFOUNDATION static bool GetProcessArgs(pid_t pid, std::string& args) { diff -r 168382a89b21 -r 649786bae914 dep/animia/src/win/quartz.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dep/animia/src/win/quartz.cc Wed Dec 06 19:42:33 2023 -0500 @@ -0,0 +1,84 @@ +/* We actually DON'T need Objective-C for most of this file. + * GetWindowTitle() is the only function that really needs it. + * (and even then, we can use the C bindings for it...) + * + * However, being able to use the Foundation classes makes things + * so, so, so much easier, and so I've decided to make this file + * in Objective-C++. +*/ +#include "animia/win/quartz.h" +#include "animia.h" + +#include +#include + +#include +#include +#include + +namespace animia::internal::quartz { + +static bool GetWindowTitle(unsigned int wid, std::string& result) { + // id app = [NSApplication sharedApplication]; + id app = cls_msg(cls("NSApplication"), sel("sharedApplication")); + + // id window = [app windowWithWindowNumber: wid]; + id window = msg(app, sel("windowWithWindowNumber:"), wid); + + // return [[window title] UTF8String]; + return StringFromCFString(reinterpret_cast(msg(window, "title")), result); +} + +bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) { + if (!window_proc) + return false; + + CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID); + if (!windows) + return false; + + CFIndex i = 0; count = CFArrayGetCount(windows); + for (; i < count; i++) { + CFDictionaryRef window = CFArrayGetValueAtIndex(windows, i); + if (!window) + continue; + + Process proc; + { + { + CFNumber num; + CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerPID", &num); + osx::util::GetCFNumber(num, proc.pid); + } + { + CFStringRef str; + CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerName", &str); + osx::util::StringFromCFString(str, proc.name); + } + if (proc.name.empty()) + osx::util::GetProcessName(proc.pid, proc.name); + } + + Window win; + { + { + CFNumber num; + CFDictionaryGetValueIfPresent(window, "kCGWindowNumber", &num); + osx::util::GetCFNumber(num, win.id); + } + { + CFStringRef str; + CFDictionaryGetValueIfPresent(window, "kCGWindowName", &str); + osx::util::GetCFNumber(str, win.class_name); + } + GetWindowTitle(win.id, win.text); + } + + if (!window_proc(proc, win)) + return false; + } + + return true; +} + +} // namespace animia::win::detail diff -r 168382a89b21 -r 649786bae914 dep/animia/src/win/quartz.mm --- a/dep/animia/src/win/quartz.mm Wed Dec 06 13:44:36 2023 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/* We actually DON'T need Objective-C for most of this file. - * GetWindowTitle() is the only function that really needs it. - * (and even then, we can use the C bindings for it...) - * - * However, being able to use the Foundation classes makes things - * so, so, so much easier, and so I've decided to make this file - * in Objective-C++. -*/ -#include "animia/win/quartz.h" -#include "animia.h" - -#import -#import -#import - -namespace animia::internal::quartz { - -template -static bool IntegerFromNSNumber(NSNumber* num, T& result) { - if (!num) - return false; - - result = [num intValue]; - return true; -} - -static bool StringFromNSString(NSString* string, std::string& result) { - if (!string) - return false; - - result = [string UTF8String]; - return true; -} - -/* This is really the only a*/ -static bool GetWindowTitle(unsigned int wid, std::string& result) { - NSWindow* window = [NSApp windowWithWindowNumber: wid]; - if (!window) - return false; - - return StringFromNSString([window title], result); -} - -bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) { - if (!window_proc) - return false; - - NSMutableArray* windows = reinterpret_cast(CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID)); - if (!windows) - return false; - - for (NSDictionary* window in windows) { - if (!window) - continue; - - Process proc; - { - IntegerFromNSNumber([window objectForKey:@"kCGWindowOwnerPID"], proc.pid); - StringFromNSString([window objectForKey:@"kCGWindowOwnerName"], proc.name); - if (proc.name.empty()) - osx::util::GetProcessName(proc.pid, proc.name); - } - - Window win; - { - IntegerFromNSNumber([window objectForKey:@"kCGWindowNumber"], win.id); - StringFromNSString([window objectForKey:@"kCGWindowName"], win.class_name); - GetWindowTitle(win.id, win.text); - } - - if (!window_proc(proc, win)) - return false; - } - - return true; -} - -} // namespace animia::win::detail diff -r 168382a89b21 -r 649786bae914 include/core/anime.h --- a/include/core/anime.h Wed Dec 06 13:44:36 2023 -0500 +++ b/include/core/anime.h Wed Dec 06 19:42:33 2023 -0500 @@ -70,6 +70,9 @@ POINT_3 // 1-3, should be represented in smileys }; +constexpr std::array ScoreFormats{ScoreFormat::POINT_100, ScoreFormat::POINT_10_DECIMAL, ScoreFormat::POINT_10, + ScoreFormat::POINT_5, ScoreFormat::POINT_3}; + struct ListInformation { int id = 0; int progress = 0; diff -r 168382a89b21 -r 649786bae914 include/core/config.h --- a/include/core/config.h Wed Dec 06 13:44:36 2023 -0500 +++ b/include/core/config.h Wed Dec 06 19:42:33 2023 -0500 @@ -59,23 +59,12 @@ } torrents; }; -#define WIDEIFY_EX(x) L##x -#define WIDEIFY(x) WIDEIFY_EX(x) - -/* only on these platforms keep this uppercase. - this will not remove compatibility with older - versions, since these platforms are case insensitive - (on macOS, only by default) */ #if (defined(WIN32) || defined(MACOSX)) -#define CONFIG_DIR "Minori" +constexpr std::string_view CONFIG_DIR = "Minori"; #else -#define CONFIG_DIR "minori" +constexpr std::string_view CONFIG_DIR = "minori"; #endif -#define CONFIG_WDIR WIDEIFY(CONFIG_DIR) -#define CONFIG_NAME "config.ini" -#define CONFIG_WNAME WIDEIFY(CONFIG_NAME) - -#define MAX_LINE_LENGTH 256 +constexpr std::string_view CONFIG_NAME = "config.ini"; #endif // __core__config_h diff -r 168382a89b21 -r 649786bae914 include/core/version.h --- a/include/core/version.h Wed Dec 06 13:44:36 2023 -0500 +++ b/include/core/version.h Wed Dec 06 19:42:33 2023 -0500 @@ -1,6 +1,6 @@ #ifndef __core__version_h #define __core__version_h -#define MINORI_VERSION "v0.1alpha" +constexpr std::string_view MINORI_VERSION = "v0.1-alpha"; #endif // __core__version_h diff -r 168382a89b21 -r 649786bae914 include/gui/dialog/settings.h --- a/include/gui/dialog/settings.h Wed Dec 06 13:44:36 2023 -0500 +++ b/include/gui/dialog/settings.h Wed Dec 06 19:42:33 2023 -0500 @@ -50,6 +50,7 @@ private: QWidget* CreateAnimeListWidget(); + decltype(session.config.anime_list.score_format) format; Themes theme; QLocale locale; Anime::TitleLanguage language; diff -r 168382a89b21 -r 649786bae914 include/gui/translate/anime.h --- a/include/gui/translate/anime.h Wed Dec 06 13:44:36 2023 -0500 +++ b/include/gui/translate/anime.h Wed Dec 06 19:42:33 2023 -0500 @@ -19,6 +19,7 @@ std::string ToLocalString(const Anime::SeriesStatus status); std::string ToLocalString(const Anime::Services service); std::string ToLocalString(const Anime::TitleLanguage language); +std::string ToLocalString(const Anime::ScoreFormat language); Anime::ListStatus ToListStatus(const std::string& str); Anime::SeriesFormat ToSeriesFormat(const std::string& str); diff -r 168382a89b21 -r 649786bae914 include/track/constants.h --- a/include/track/constants.h Wed Dec 06 13:44:36 2023 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -#ifndef __track__constants_h -#define __track__constants_h -#include -#include - -namespace Track { -namespace Constants { - -extern const std::vector default_media_extensions; -extern const std::vector default_media_players; - -} -} - -#endif // __track__constants_h diff -r 168382a89b21 -r 649786bae914 src/gui/dialog/about.cc --- a/src/gui/dialog/about.cc Wed Dec 06 13:44:36 2023 -0500 +++ b/src/gui/dialog/about.cc Wed Dec 06 19:42:33 2023 -0500 @@ -21,10 +21,6 @@ return N; } -/* used for JSON for Modern C++ */ -#define CONCAT_VERSION_NX(major, minor, patch) "v" #major "." #minor "." #patch -#define CONCAT_VERSION(major, minor, patch) CONCAT_VERSION_NX(major, minor, patch) - /* Ahhh, my dumb little hack to get this to be constexpr :) */ static constexpr const char pugixml_version[] = { PUGIXML_VERSION / 1000 % 10 + '0', /* Major */ @@ -50,9 +46,9 @@ QHBoxLayout* layout = new QHBoxLayout(this); /* we have to generate this on-the-fly for localization purposes */ - static const QString html = QString( + const QString html = QString( "" - "

Minori " MINORI_VERSION "

" + "

Minori " + QString::fromUtf8(MINORI_VERSION.data(), MINORI_VERSION.size()) + "

" "

" " " + QCoreApplication::tr("Author:") + "
" " Paper (@mrpapersonic)" @@ -65,9 +61,9 @@ ", " "Anitomy" ", " - "JSON for Modern C++ " CONCAT_VERSION(NLOHMANN_JSON_VERSION_MAJOR, - NLOHMANN_JSON_VERSION_MINOR, - NLOHMANN_JSON_VERSION_PATCH) "" + "JSON for Modern C++ v" + QString::number(NLOHMANN_JSON_VERSION_MAJOR) + "." + + QString::number(NLOHMANN_JSON_VERSION_MINOR) + "." + + QString::number(NLOHMANN_JSON_VERSION_PATCH) + "" ", " "pugixml v" + pugixml_version + "" ", " diff -r 168382a89b21 -r 649786bae914 src/gui/dialog/settings/application.cc --- a/src/gui/dialog/settings/application.cc Wed Dec 06 13:44:36 2023 -0500 +++ b/src/gui/dialog/settings/application.cc Wed Dec 06 19:42:33 2023 -0500 @@ -3,6 +3,8 @@ #include "gui/dialog/settings.h" #include "gui/theme.h" #include "gui/locale.h" +#include "gui/translate/anime.h" + #include #include #include @@ -11,6 +13,7 @@ #include #include #include + #include QWidget* SettingsPageApplication::CreateAnimeListWidget() { @@ -107,7 +110,7 @@ { /* Application locale */ { - QLabel* locale_combo_box_label = new QLabel(tr("Set application locale:"), appearance_group_box); + QLabel* locale_combo_box_label = new QLabel(tr("Application locale:"), appearance_group_box); appearance_layout->addWidget(locale_combo_box_label); } @@ -123,6 +126,7 @@ for (size_t i = 0; i < available_locales.size(); i++) if (available_locales[i] == locale) locale_combo_box->setCurrentIndex(i); + appearance_layout->addWidget(locale_combo_box); } } @@ -130,19 +134,21 @@ { /* Application theme */ { - QLabel* theme_combo_box_label = new QLabel(tr("Application theme:"), appearance_group_box); - appearance_layout->addWidget(theme_combo_box_label); + QLabel* rating_combo_box_label = new QLabel(tr("Rating system:"), appearance_group_box); + appearance_layout->addWidget(rating_combo_box_label); } { - QComboBox* theme_combo_box = new QComboBox(appearance_group_box); - theme_combo_box->addItem(tr("Default")); - theme_combo_box->addItem(tr("Light")); - theme_combo_box->addItem(tr("Dark")); - connect(theme_combo_box, QOverload::of(&QComboBox::currentIndexChanged), this, - [this](int index) { theme = static_cast(index); }); - theme_combo_box->setCurrentIndex(static_cast(theme)); - appearance_layout->addWidget(theme_combo_box); + QComboBox* rating_combo_box = new QComboBox(appearance_group_box); + + for (const auto& score_format : Anime::ScoreFormats) + rating_combo_box->addItem(Strings::ToQString(Translate::ToLocalString(score_format)), static_cast(score_format)); + + connect(rating_combo_box, QOverload::of(&QComboBox::currentIndexChanged), this, + [this, rating_combo_box](int index) { format = static_cast(rating_combo_box->itemData(index).toInt()); }); + + rating_combo_box->setCurrentIndex(static_cast(format)); + appearance_layout->addWidget(rating_combo_box); } } @@ -220,6 +226,7 @@ SettingsPageApplication::SettingsPageApplication(QWidget* parent) : SettingsPage(parent, tr("Application")) { language = session.config.anime_list.language; + format = session.config.anime_list.score_format; theme = session.config.theme.GetTheme(); locale = session.config.locale.GetLocale(); highlighted_anime_above_others = session.config.anime_list.highlighted_anime_above_others; diff -r 168382a89b21 -r 649786bae914 src/gui/pages/statistics.cc --- a/src/gui/pages/statistics.cc Wed Dec 06 13:44:36 2023 -0500 +++ b/src/gui/pages/statistics.cc Wed Dec 06 19:42:33 2023 -0500 @@ -75,9 +75,11 @@ } /* me abusing macros :) */ -#define ADD_TIME_SEGMENT(r, x, s, p) \ - if (x > 0) \ - r << x << ((x == 1) ? s : p) +static void add_time_segment(std::ostringstream& str, int x, const std::string_view& s, const std::string_view& p) { + if (x > 0) + str << x << ((x == 1) ? s : p); +} + std::string StatisticsPage::MinutesToDateString(const int minutes) { /* ew */ int years = (minutes * (1 / 525949.2F)); @@ -86,10 +88,10 @@ int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); std::ostringstream return_stream; - ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); - ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); - ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); - ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); + add_time_segment(return_stream, years, " year ", " years "); + add_time_segment(return_stream, months, " month ", " months "); + add_time_segment(return_stream, days, " day ", " days "); + add_time_segment(return_stream, hours, " hour ", " hours "); if (rest_minutes > 0 || return_stream.str().size() == 0) return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); return return_stream.str(); @@ -105,16 +107,15 @@ int seconds = sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); std::ostringstream return_stream; - ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); - ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); - ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); - ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); - ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); + add_time_segment(return_stream, years, " year ", " years "); + add_time_segment(return_stream, months, " month ", " months "); + add_time_segment(return_stream, days, " day ", " days "); + add_time_segment(return_stream, hours, " hour ", " hours "); + add_time_segment(return_stream, minutes, " minute ", " minutes "); if (seconds > 0 || return_stream.str().size() == 0) return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); return return_stream.str(); } -#undef ADD_TIME_SEGMENT inline int GetTotalWithScore(const int score) { int count = 0; diff -r 168382a89b21 -r 649786bae914 src/track/constants.cc --- a/src/track/constants.cc Wed Dec 06 13:44:36 2023 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#include "track/constants.h" - -/* right now, these are just const vectors, but eventually - I'll make a class to manage these and make them disableable */ - -namespace Track { -namespace Constants { - -const std::vector default_media_extensions = { - "mkv", - "mp4", - "m4v", /* apple's stupid DRM thing */ - "avi", - "webm", /* matroska's retarded inbred cousin */ - /* QuickTime */ - "mov", - "qt", - /* MPEG transport stream */ - "mts", - "m2ts", - "ts", - /* MPEG-1, typically not used for anime */ - "mpg", - "mp2", - "mpeg", - "mpe", - "mpv", - /* MPEG-2 */ - "m2v" - /* 3GPP */ - "3gp", - "3g2", - /* Windows Media */ - "asf", - "wmv", - /* Adobe Flash */ - "flv", - "swf", /* not exactly a video format */ - /* Ogg Video */ - "ogv", - /* RealPlayer (who tf uses this?) */ - "rm", - "rmvb", - /* Nullsoft Streaming Video (Winamp) */ - "nsv", - /* Material Exchange Format (Sony?) */ - "mxf" -}; - -const std::vector default_media_players = { -#ifdef MACOSX - "VLC", "IINA", "QuickTime Player" -#elif WIN32 - "vlc.exe", "mpc-hc.exe", "mpc-hc64.exe", "wmplayer.exe", "mpv.exe" -#else // linux, unix, whatevs - "vlc", "mpv", "mpc-qt" -#endif -}; - -} -} diff -r 168382a89b21 -r 649786bae914 src/track/media.cc --- a/src/track/media.cc Wed Dec 06 13:44:36 2023 -0500 +++ b/src/track/media.cc Wed Dec 06 19:42:33 2023 -0500 @@ -1,16 +1,17 @@ #include "track/media.h" -#include "track/constants.h" -#include "animia.h" -#include "anitomy/anitomy.h" #include "core/filesystem.h" #include "core/strings.h" #include "core/session.h" + +#include +#include + #include #include #include #include -#include -#include + +#include "animia.h" namespace Track { namespace Media {