feat: progress AyuSync implementation

fix: build errors
This commit is contained in:
ZavaruKitsu 2023-07-03 03:26:46 +03:00
parent b6b3ad5ff9
commit dc9f464f84
10 changed files with 1261 additions and 29 deletions

View file

@ -122,6 +122,7 @@ PRIVATE
ayu/sync/models.h
ayu/sync/utils/ayu_pipe_wrapper.cpp
ayu/sync/utils/ayu_pipe_wrapper.h
ayu/sync/utils/process_utils.hpp
ayu/libs/pipe.hpp
ayu/libs/json.hpp
ayu/libs/json_ext.hpp

View file

@ -7,9 +7,10 @@
#include "ayu_database.h"
#include <QString>
#include "entities.h"
#include "ayu/sqlite/sqlite_orm.h"
#include "QtCore/QString"
#include "ayu/libs/sqlite/sqlite_orm.h"
#include "main/main_session.h"
using namespace sqlite_orm;

File diff suppressed because it is too large Load diff

View file

@ -362,7 +362,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <system_error> // std::error_code, std::system_error
#include <string> // std::string
@ -7458,7 +7458,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <memory> // std::unique_ptr
#include <type_traits> // std::integral_constant
@ -7917,7 +7917,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <type_traits> // std::enable_if_t, std::is_arithmetic, std::is_same, std::true_type, std::false_type, std::make_index_sequence, std::index_sequence
#include <memory> // std::default_delete
@ -8371,7 +8371,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <type_traits> // std::enable_if_t, std::is_arithmetic, std::is_same, std::enable_if
#include <stdlib.h> // atof, atoi, atoll
@ -8789,7 +8789,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <string> // std::string
#include <utility> // std::move
@ -9978,7 +9978,7 @@ namespace sqlite_orm {
}
#pragma once
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <memory> // std::unique_ptr/shared_ptr, std::make_unique/std::make_shared
#include <system_error> // std::system_error
@ -10019,12 +10019,12 @@ namespace sqlite_orm {
// #include "mapped_row_extractor.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
// #include "object_from_column_builder.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <type_traits> // std::is_member_object_pointer
// #include "functional/static_magic.h"
@ -10282,7 +10282,7 @@ namespace sqlite_orm {
// #include "function.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <type_traits>
#include <string> // std::string
#include <tuple> // std::tuple
@ -10809,7 +10809,7 @@ namespace sqlite_orm {
// #include "view.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <string> // std::string
#include <utility> // std::forward, std::move
#include <tuple> // std::tuple, std::make_tuple
@ -10820,7 +10820,7 @@ namespace sqlite_orm {
// #include "iterator.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared
#include <type_traits> // std::decay
#include <utility> // std::move
@ -10946,7 +10946,7 @@ namespace sqlite_orm {
// #include "prepared_statement.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <memory> // std::unique_ptr
#include <iterator> // std::iterator_traits
@ -10964,7 +10964,7 @@ namespace sqlite_orm {
// #include "connection_holder.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <atomic>
#include <string> // std::string
@ -12893,7 +12893,7 @@ namespace sqlite_orm {
// #include "storage_base.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <functional> // std::function, std::bind
#include <string> // std::string
@ -12914,7 +12914,7 @@ namespace sqlite_orm {
// #include "pragma.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <string> // std::string
#include <functional> // std::function
@ -13556,7 +13556,7 @@ namespace sqlite_orm {
// #include "limit_accessor.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <map> // std::map
#include <functional> // std::function
@ -13782,7 +13782,7 @@ namespace sqlite_orm {
// #include "backup.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <system_error> // std::system_error
#include <string> // std::string
@ -13860,7 +13860,7 @@ namespace sqlite_orm {
// #include "values_to_tuple.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
#include <type_traits> // std::index_sequence, std::make_index_sequence
#include <tuple> // std::tuple, std::tuple_size, std::get
@ -13871,7 +13871,7 @@ namespace sqlite_orm {
// #include "arg_values.h"
#include "ayu/sqlite/sqlite3.h"
#include "ayu/libs/sqlite/sqlite3.h"
// #include "row_extractor.h"

View file

@ -6,6 +6,65 @@
// Copyright @Radolyn, 2023
#include "ayu_sync_controller.h"
#include "ayu/libs/process.hpp"
#include "ayu/sync/utils/process_utils.hpp"
#include <QString>
#include <thread>
namespace AyuSync {
} // AyuSync
std::optional<ayu_sync_controller> controller = std::nullopt;
bool isAgentDownloaded() {
return std::filesystem::exists(AgentPath);
}
bool isAgentRunning() {
return is_process_running(AgentFilename);
}
void initialize() {
if (controller.has_value()) {
return;
}
controller = ayu_sync_controller();
}
ayu_sync_controller &getControllerInstance() {
initialize();
return controller.value();
}
void ayu_sync_controller::initializeAgent() {
if (!isAgentDownloaded()) {
return;
}
if (!isAgentRunning()) {
auto configPath = std::filesystem::absolute("./tdata/sync_preferences.json");
auto process = nes::process{AgentPath, {configPath.string()}};
process.detach();
}
pipe = std::make_unique<ayu_pipe_wrapper>();
std::thread receiverThread(&ayu_sync_controller::receiver, this);
}
void ayu_sync_controller::receiver() {
while (true) {
auto p = pipe->receive();
invokeHandler(p);
}
}
void ayu_sync_controller::invokeHandler(json p) {
DEBUG_LOG(("Invoking handler on %1").arg(p.dump().c_str()));
auto userId = p["userId"].get<long>();
auto type = p["type"].get<std::string>();
DEBUG_LOG(("userId: %1, type: %1").arg(userId).arg(type.c_str()));
}
}

View file

@ -7,10 +7,34 @@
#pragma once
#include "ayu/libs/json.hpp"
#include "utils/ayu_pipe_wrapper.h"
using json = nlohmann::json;
const std::string AgentFilename =
#ifdef _WIN32
"AyuSyncAgent.exe";
#else
"AyuSyncAgent";
#endif
const std::string AgentPath = "./AyuSync/" + AgentFilename;
namespace AyuSync {
class ayu_sync_controller {
public:
void initializeAgent();
void invokeHandler(json p);
private:
void receiver();
std::unique_ptr<ayu_pipe_wrapper> pipe;
};
} // AyuSync
ayu_sync_controller &getControllerInstance();
bool isAgentDownloaded();
bool isAgentRunning();
}

View file

@ -6,7 +6,6 @@
// Copyright @Radolyn, 2023
#include <sstream>
#include <iostream>
#include "ayu_pipe_wrapper.h"
#include "ayu/libs/bit_converter.hpp"

View file

@ -0,0 +1,86 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <locale>
#include <codecvt>
#ifdef _WIN32
#include <windows.h>
#include <tlhelp32.h>
#else
#include <dirent.h>
#include <unistd.h>
#endif
// A function to check if a process is running by its name
// Bing AI generated
bool is_process_running(const std::string &name) {
#ifdef _WIN32
// Create a snapshot of all processes
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create snapshot\n";
return false;
}
// Iterate over the processes and compare the names
PROCESSENTRY32 entry;
entry.dwSize = sizeof(entry);
if (!Process32First(snapshot, &entry)) {
std::cerr << "Failed to get first process\n";
CloseHandle(snapshot);
return false;
}
do {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string entry_name = converter.to_bytes(entry.szExeFile);
if (name == entry_name) {
// Found a match
CloseHandle(snapshot);
return true;
}
} while (Process32Next(snapshot, &entry));
// No match found
CloseHandle(snapshot);
return false;
#else
// Open the /proc directory
DIR* dir = opendir("/proc");
if (dir == nullptr) {
std::cerr << "Failed to open /proc\n";
return false;
}
// Read the subdirectories
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
// Check if the subdirectory is a number (pid)
std::string pid = entry->d_name;
if (std::all_of(pid.begin(), pid.end(), isdigit)) {
// Read the /proc/pid/cmdline file
std::string cmdline_file = "/proc/" + pid + "/cmdline";
std::ifstream file(cmdline_file);
if (file.is_open()) {
// Get the first word of the file (process name)
std::string process_name;
std::getline(file, process_name, '\0');
// Remove the path if present
size_t pos = process_name.rfind('/');
if (pos != std::string::npos) {
process_name = process_name.substr(pos + 1);
}
// Compare the names
if (name == process_name) {
// Found a match
closedir(dir);
return true;
}
}
}
}
// No match found
closedir(dir);
return false;
#endif
}

View file

@ -5,6 +5,7 @@
//
// Copyright @Radolyn, 2023
#include "ayu/sync/ayu_sync_controller.h"
#include "ayu/ui/boxes/edit_edited_mark.h"
#include "ayu/ui/boxes/edit_deleted_mark.h"
#include "ayu/ayu_settings.h"
@ -325,6 +326,23 @@ namespace Settings {
});
}
void Ayu::SetupAyuSync(not_null<Ui::VerticalLayout *> container) {
AddSubsectionTitle(container, rpl::single(QString("AyuSync")));
auto text = AyuSync::isAgentDownloaded() ?
QString("Open preferences") :
QString("Download agent");
AddButton(
container,
rpl::single(text),
st::settingsButtonNoIcon
)->addClickHandler([=] {
auto controller = &AyuSync::getControllerInstance();
controller->initializeAgent();
});
}
void Ayu::SetupBetaFunctions(not_null<Ui::VerticalLayout *> container) {
auto settings = &AyuSettings::getInstance();
@ -393,6 +411,9 @@ namespace Settings {
AddSkip(container);
SetupCustomization(container, controller);
AddSkip(container);
SetupAyuSync(container);
AddSkip(container);
SetupBetaFunctions(container);

View file

@ -31,14 +31,14 @@ namespace Settings {
void SetupQoLToggles(not_null<Ui::VerticalLayout *> container);
void
SetupCustomization(not_null<Ui::VerticalLayout *> container, not_null<Window::SessionController *> controller);
void SetupCustomization(not_null<Ui::VerticalLayout *> container, not_null<Window::SessionController *> controller);
void
SetupShowPeerId(not_null<Ui::VerticalLayout *> container, not_null<Window::SessionController *> controller);
void SetupShowPeerId(not_null<Ui::VerticalLayout *> container, not_null<Window::SessionController *> controller);
void SetupRecentStickersLimitSlider(not_null<Ui::VerticalLayout *> container);
void SetupAyuSync(not_null<Ui::VerticalLayout *> container);
void SetupBetaFunctions(not_null<Ui::VerticalLayout *> container);
void SetupAyuGramSettings(not_null<Ui::VerticalLayout *> container, not_null<Window::SessionController *> null);