application.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "client/application.hpp"
  2. #include <QQuickStyle>
  3. #include <QFont>
  4. #include <QDebug>
  5. using namespace binom;
  6. int main(int argc, char *argv[]){
  7. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  8. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  9. #endif
  10. return Application(argc, argv, QStringLiteral("qrc:/qml/main.qml")).start();
  11. }
  12. void Application::initQML(const QUrl qml_url) {
  13. qmlRegisterSingletonType<API>("LibreHub", 1, 0, "API",
  14. [this](QQmlEngine *engine, QJSEngine *script_engine) -> QObject * {
  15. Q_UNUSED(engine)
  16. Q_UNUSED(script_engine)
  17. return &api;
  18. });
  19. QQuickStyle::setStyle("Material");
  20. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  21. &app, [qml_url](QObject *obj, const QUrl &objUrl) {
  22. if (!obj && qml_url == objUrl)
  23. QCoreApplication::exit(-1);
  24. }, Qt::QueuedConnection);
  25. engine.load(qml_url);
  26. }
  27. Application::Application([[maybe_unused]]AppIniter app_initer, int argc, char* argv[], QUrl qml_url)
  28. : app(argc, argv),
  29. api(app_initer.database_path) {
  30. initQML(std::move(qml_url));
  31. }
  32. Application::Application(int argc, char* argv[], QUrl qml_url) try
  33. : Application(processArgs(argc, argv), argc, argv, qml_url) {}
  34. catch(const Exception& e) {
  35. std::cerr << e.full() << '\n';
  36. std::exit(EXIT_FAILURE);
  37. } catch(const std::exception& e) {
  38. std::cerr << e.what() << '\n';
  39. std::exit(EXIT_FAILURE);
  40. } catch(...) {
  41. std::cerr << "Unexpected exception!\n";
  42. std::exit(EXIT_FAILURE);
  43. }
  44. int Application::start() {
  45. try {
  46. return app.exec();
  47. } catch(const Exception& e) {
  48. std::cerr << e.full() << '\n';
  49. std::exit(EXIT_FAILURE);
  50. } catch(const std::exception& e) {
  51. std::cerr << e.what() << '\n';
  52. std::exit(EXIT_FAILURE);
  53. } catch(...) {
  54. std::cerr << "Unexpected exception!\n";
  55. std::exit(EXIT_FAILURE);
  56. }
  57. }
  58. Application::AppIniter Application::processArgs(int argc, char* argv[]) {
  59. #define ifeq(str_1, str_2) if(isstreq(str_1, str_2))
  60. #define elifeq(str_1, str_2) else if(isstreq(str_1, str_2))
  61. if(argc <= 1) {
  62. fs::path path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/LibreHubClient";
  63. if(!fs::exists(path))
  64. fs::create_directory(path);
  65. return AppIniter{path.string()};
  66. }
  67. enum class Token {
  68. flag,
  69. db_path
  70. }token = Token(0);
  71. AppIniter app_init;
  72. for((--argc, ++argv); argc ;(--argc, ++argv)) {
  73. switch (token) {
  74. default: continue;
  75. case Token::flag:
  76. if(isstreq(*argv, "-d") || isstreq(*argv, "--db-path"))
  77. token = Token::db_path;
  78. continue;
  79. case Token::db_path:
  80. app_init.database_path = *argv;
  81. fs::path path = app_init.database_path;
  82. if(!fs::exists(path))
  83. fs::create_directory(path);
  84. continue;
  85. }
  86. }
  87. return app_init;
  88. #undef ifeq
  89. #undef elifeq
  90. }