commit fd3ce483f09e93cbe86d5e6885ed54357d703a03 Author: Selim Mustafaev Date: Mon Jun 12 10:46:46 2023 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7521fe4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + +# CLion + +cmake-build-debug/ +.idea/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3fa4f64 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.16) + +project(AutoCatQt VERSION 0.1 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_PREFIX_PATH "/home/selim/qt/6.4.3/gcc_64/lib/cmake") + +find_package(Qt6 6.4 REQUIRED COMPONENTS Quick) +find_package(nlohmann_json REQUIRED) + +qt_standard_project_setup() + +qt_add_executable(appAutoCatQt + main.cpp + services/Settings.cpp services/Settings.h models/User.cpp models/User.h) + +qt_add_qml_module(appAutoCatQt + URI AutoCatQt + VERSION 1.0 + QML_FILES views/LoginWindow.qml + QML_FILES views/MainWindow.qml +) + +set_target_properties(appAutoCatQt PROPERTIES + MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com + MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} + MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} + MACOSX_BUNDLE TRUE + WIN32_EXECUTABLE TRUE +) + +target_link_libraries(appAutoCatQt + PRIVATE Qt6::Quick + PRIVATE nlohmann_json::nlohmann_json +) + +install(TARGETS appAutoCatQt + BUNDLE DESTINATION . + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ec756a4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,25 @@ +#include "services/Settings.h" + +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QQuickWindow::setGraphicsApi(QSGRendererInterface::Software); + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, + &app, []() { QCoreApplication::exit(-1); }, + Qt::QueuedConnection); + + auto settings = Settings::instance(); + if(settings.user().token.empty()) { + engine.load(QUrl(u"qrc:/AutoCatQt/views/LoginWindow.qml"_qs)); + } else { + engine.load(QUrl(u"qrc:/AutoCatQt/views/MainWindow.qml"_qs)); + } + + return app.exec(); +} diff --git a/models/User.cpp b/models/User.cpp new file mode 100644 index 0000000..996bf03 --- /dev/null +++ b/models/User.cpp @@ -0,0 +1,9 @@ +// +// Created by selim on 5/6/23. +// + +#include "User.h" + +User::User(std::string_view email, std::string_view token): email(email), token(token) { + +} diff --git a/models/User.h b/models/User.h new file mode 100644 index 0000000..2e6eabd --- /dev/null +++ b/models/User.h @@ -0,0 +1,28 @@ +// +// Created by selim on 5/6/23. +// + +#ifndef AUTOCATQT_USER_H +#define AUTOCATQT_USER_H + +#include +#include +#include + +class User { +public: + std::string email; + std::string token; + std::optional googleIdToken; + std::optional googleRefreshToken; + +public: + User() = default; + User(const User& user) = default; + User(User&&) = default; + User(std::string_view email, std::string_view token); + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(User, email, token) +}; + +#endif //AUTOCATQT_USER_H diff --git a/services/Settings.cpp b/services/Settings.cpp new file mode 100644 index 0000000..2c15749 --- /dev/null +++ b/services/Settings.cpp @@ -0,0 +1,68 @@ +// +// Created by selim on 5/6/23. +// +#include "Settings.h" +#include + +#include +#include +#include + +fs::path Settings::getDataPath() { + auto dataDir = std::getenv("XDG_DATA_HOME"); + if(dataDir) { + return fs::path(dataDir) / "autocat"; + } + + auto home = std::getenv("HOME"); + if(home) { + return fs::path(home) / ".local" / "share" / "autocat"; + } + + throw std::runtime_error("Failed to find data home directory"); +} + +std::string readString(const fs::path& path) { + std::ifstream stream(path); + stream.seekg(0, std::ios::end); + auto size = stream.tellg(); + std::string buffer(size, ' '); + stream.seekg(0); + stream.read(&buffer[0], size); + return buffer; +} + +void writeString(const fs::path& path, const std::string& text) { + std::ofstream stream(path); + stream << text; +} + +Settings Settings::instance() { + static Settings instance; + return instance; +} + +Settings::Settings() { + _dataPath = getDataPath(); + if(!fs::exists(_dataPath)) { + fs::create_directories(_dataPath); + } +} + +User Settings::user() const { + auto userFile = _dataPath / "user.json"; + if(fs::exists(userFile)) { + auto jsonText = readString(userFile); + std::cout << "User json text: " << jsonText << std::endl; + return nlohmann::json::parse(jsonText).get(); + } else { + return {}; + } +} + +void Settings::setUser(const User &user) { + auto userFile = _dataPath / "user.json"; + nlohmann::json json = user; + auto userString = json.dump(4); + writeString(userFile, userString); +} diff --git a/services/Settings.h b/services/Settings.h new file mode 100644 index 0000000..1d26976 --- /dev/null +++ b/services/Settings.h @@ -0,0 +1,26 @@ +// +// Created by selim on 5/6/23. +// + +#ifndef AUTOCATQT_SETTINGS_H +#define AUTOCATQT_SETTINGS_H + +#include "../models/User.h" +#include + +namespace fs = std::filesystem; + +class Settings { +private: + fs::path _dataPath; + +public: + static Settings instance(); + Settings(); + static fs::path getDataPath(); + + [[nodiscard]] User user() const; + void setUser(const User& user); +}; + +#endif //AUTOCATQT_SETTINGS_H diff --git a/views/LoginWindow.qml b/views/LoginWindow.qml new file mode 100644 index 0000000..2c8ba2c --- /dev/null +++ b/views/LoginWindow.qml @@ -0,0 +1,9 @@ +import QtQuick +import QtQuick.Window + +Window { + height: 480 + width: 640 + title: "Login" + visible: true +} \ No newline at end of file diff --git a/views/MainWindow.qml b/views/MainWindow.qml new file mode 100644 index 0000000..cda436f --- /dev/null +++ b/views/MainWindow.qml @@ -0,0 +1,9 @@ +import QtQuick +import QtQuick.Window + +Window { + height: 480 + title: "Main window" + visible: true + width: 640 +} \ No newline at end of file