mirror of
https://github.com/cottongin/cardputerzero-sync.git
synced 2026-11-20 14:44:47 -06:00
feat(device-ui): add C++ IPC client, Tailscale peer parser, and shadcn Zinc themes
This commit is contained in:
1242
device-ui/src/ipc_client.cpp
Normal file
1242
device-ui/src/ipc_client.cpp
Normal file
File diff suppressed because it is too large
Load Diff
126
device-ui/src/ipc_client.hpp
Normal file
126
device-ui/src/ipc_client.hpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include "theme.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cp0 {
|
||||
|
||||
/// Per-watched-folder queue and existence snapshot.
|
||||
struct FolderStatus {
|
||||
std::string id;
|
||||
std::string path;
|
||||
std::string label;
|
||||
bool enabled = true;
|
||||
bool exists = false;
|
||||
int pending = 0;
|
||||
int uploading = 0;
|
||||
int synced = 0;
|
||||
int failed = 0;
|
||||
int64_t pending_bytes = 0;
|
||||
};
|
||||
|
||||
/// Single log line from the daemon's SQLite log ring buffer.
|
||||
struct LogEntry {
|
||||
int64_t id = 0;
|
||||
int64_t ts = 0;
|
||||
std::string level;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
/// Full status snapshot from IPC `status` or read-only SQLite + config fallback.
|
||||
struct DaemonStatus {
|
||||
bool ok = false;
|
||||
bool from_fallback = false;
|
||||
std::string error;
|
||||
std::string target_host;
|
||||
int target_port = 8765;
|
||||
std::string theme = "dark";
|
||||
bool mac_online = false;
|
||||
std::string mac_dest_root;
|
||||
bool syncing = false;
|
||||
std::string current_file;
|
||||
int pending_count = 0;
|
||||
int64_t pending_bytes = 0;
|
||||
int synced_count = 0;
|
||||
int failed_count = 0;
|
||||
int next_retry_sec = 0;
|
||||
int64_t last_sync_ts = 0;
|
||||
std::string last_synced_file;
|
||||
std::string last_error;
|
||||
std::vector<FolderStatus> folders;
|
||||
std::vector<LogEntry> logs;
|
||||
};
|
||||
|
||||
/// Result of a mutating IPC command (`sync_now`, `add_folder`, etc.).
|
||||
struct CommandResult {
|
||||
bool ok = false;
|
||||
std::string error;
|
||||
std::string raw_json;
|
||||
};
|
||||
|
||||
/// Non-self Tailscale peer extracted from `tailscale status --json`.
|
||||
struct TailscalePeer {
|
||||
std::string id;
|
||||
std::string hostname;
|
||||
std::string dns_name;
|
||||
std::string os;
|
||||
bool online = false;
|
||||
std::vector<std::string> tailscale_ips;
|
||||
};
|
||||
|
||||
/// Return default Unix socket path (`CP0_SYNC_SOCK` or `${XDG_RUNTIME_DIR:-/tmp}/cp0-sync.sock`).
|
||||
std::string default_sock_path();
|
||||
|
||||
/// Return default SQLite state DB path (`CP0_SYNC_DB` or `$HOME/.local/share/cp0-sync/state.db`).
|
||||
std::string default_db_path();
|
||||
|
||||
/// Return default daemon config path (`CP0_SYNC_CONFIG` or `$HOME/.config/cp0-sync/config.json`).
|
||||
std::string default_config_path();
|
||||
|
||||
/// Parse `status` JSON response string into a DaemonStatus struct.
|
||||
DaemonStatus parse_status_json(const std::string& json_str);
|
||||
|
||||
/// Parse `tailscale status --json` output, excluding Self and sorting online macOS/darwin peers first.
|
||||
std::vector<TailscalePeer> parse_tailscale_peers(const std::string& json_str);
|
||||
|
||||
/// Unix domain socket IPC client with read-only SQLite fallback and Tailscale peer discovery.
|
||||
class IpcClient {
|
||||
public:
|
||||
IpcClient() = default;
|
||||
explicit IpcClient(std::string sock_path, std::string db_path = "", std::string config_path = "");
|
||||
|
||||
/// Fetch live daemon status over AF_UNIX, falling back to read-only SQLite if socket is unreachable.
|
||||
DaemonStatus fetch_status() const;
|
||||
|
||||
/// Send a single newline-delimited JSON command over AF_UNIX with a non-blocking timeout.
|
||||
std::string send_command(const std::string& json_line, int timeout_ms = 1000) const;
|
||||
|
||||
/// Read cached state directly from SQLite (`SQLITE_OPEN_READONLY`) and `config.json`.
|
||||
DaemonStatus read_sqlite_fallback() const;
|
||||
|
||||
/// Execute `tailscale status --json` locally and return sorted non-self peers.
|
||||
std::vector<TailscalePeer> list_tailscale_peers() const;
|
||||
|
||||
// Typed command helpers matching Task 4 IPC protocol
|
||||
CommandResult sync_now() const;
|
||||
CommandResult add_folder(const std::string& path, const std::string& label = "") const;
|
||||
CommandResult remove_folder(const std::string& folder_id) const;
|
||||
CommandResult set_target(const std::string& host, int port = 8765) const;
|
||||
CommandResult set_theme(ThemeMode theme) const;
|
||||
CommandResult retry_failed() const;
|
||||
|
||||
private:
|
||||
std::string resolved_sock_path() const;
|
||||
std::string resolved_db_path() const;
|
||||
std::string resolved_config_path() const;
|
||||
CommandResult run_json_command(const std::string& payload) const;
|
||||
|
||||
std::string sock_path_;
|
||||
std::string db_path_;
|
||||
std::string config_path_;
|
||||
};
|
||||
|
||||
} // namespace cp0
|
||||
59
device-ui/src/theme.cpp
Normal file
59
device-ui/src/theme.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "theme.hpp"
|
||||
|
||||
namespace cp0 {
|
||||
|
||||
ShadcnPalette get_palette(ThemeMode mode) {
|
||||
if (mode == ThemeMode::Light) {
|
||||
return ShadcnPalette{
|
||||
/* background */ 0xfafafa,
|
||||
/* card */ 0xffffff,
|
||||
/* muted */ 0xf4f4f5,
|
||||
/* border */ 0xe4e4e7,
|
||||
/* ring */ 0x71717a,
|
||||
/* foreground */ 0x09090b,
|
||||
/* muted_foreground */ 0x71717a,
|
||||
/* active_tab_bg */ 0xffffff,
|
||||
/* active_tab_fg */ 0x09090b,
|
||||
/* active_tab_border */ 0xe4e4e7,
|
||||
/* status_online */ 0x16a34a,
|
||||
/* status_online_bg */ 0xdcfce7,
|
||||
/* status_sleeping */ 0xd97706,
|
||||
/* status_sleeping_bg */ 0xfef3c7,
|
||||
/* status_error */ 0xdc2626,
|
||||
/* status_error_bg */ 0xfee2e2,
|
||||
};
|
||||
}
|
||||
|
||||
return ShadcnPalette{
|
||||
/* background */ 0x09090b,
|
||||
/* card */ 0x09090b,
|
||||
/* muted */ 0x18181b,
|
||||
/* border */ 0x27272a,
|
||||
/* ring */ 0xd4d4d8,
|
||||
/* foreground */ 0xfafafa,
|
||||
/* muted_foreground */ 0xa1a1aa,
|
||||
/* active_tab_bg */ 0x27272a,
|
||||
/* active_tab_fg */ 0xfafafa,
|
||||
/* active_tab_border */ 0x27272a,
|
||||
/* status_online */ 0x4ade80,
|
||||
/* status_online_bg */ 0x11261a,
|
||||
/* status_sleeping */ 0xfbbf24,
|
||||
/* status_sleeping_bg */ 0x271d0b,
|
||||
/* status_error */ 0xf87171,
|
||||
/* status_error_bg */ 0x2b1214,
|
||||
};
|
||||
}
|
||||
|
||||
ThemeMode toggle_theme(ThemeMode mode) {
|
||||
return mode == ThemeMode::Dark ? ThemeMode::Light : ThemeMode::Dark;
|
||||
}
|
||||
|
||||
std::string theme_to_string(ThemeMode mode) {
|
||||
return mode == ThemeMode::Light ? "light" : "dark";
|
||||
}
|
||||
|
||||
ThemeMode theme_from_string(const std::string& theme) {
|
||||
return theme == "light" ? ThemeMode::Light : ThemeMode::Dark;
|
||||
}
|
||||
|
||||
} // namespace cp0
|
||||
46
device-ui/src/theme.hpp
Normal file
46
device-ui/src/theme.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace cp0 {
|
||||
|
||||
/// Supported UI color themes matching daemon config values ("dark" | "light").
|
||||
enum class ThemeMode {
|
||||
Dark,
|
||||
Light,
|
||||
};
|
||||
|
||||
/// Exact shadcn/ui Zinc color tokens (24-bit RGB hex values).
|
||||
struct ShadcnPalette {
|
||||
uint32_t background;
|
||||
uint32_t card;
|
||||
uint32_t muted;
|
||||
uint32_t border;
|
||||
uint32_t ring;
|
||||
uint32_t foreground;
|
||||
uint32_t muted_foreground;
|
||||
uint32_t active_tab_bg;
|
||||
uint32_t active_tab_fg;
|
||||
uint32_t active_tab_border;
|
||||
uint32_t status_online;
|
||||
uint32_t status_online_bg;
|
||||
uint32_t status_sleeping;
|
||||
uint32_t status_sleeping_bg;
|
||||
uint32_t status_error;
|
||||
uint32_t status_error_bg;
|
||||
};
|
||||
|
||||
/// Return the Zinc Dark or Zinc Light palette tokens from Spec Section 5.4.
|
||||
ShadcnPalette get_palette(ThemeMode mode);
|
||||
|
||||
/// Toggle between ThemeMode::Dark and ThemeMode::Light.
|
||||
ThemeMode toggle_theme(ThemeMode mode);
|
||||
|
||||
/// Convert ThemeMode to its persisted config/IPC string ("dark" or "light").
|
||||
std::string theme_to_string(ThemeMode mode);
|
||||
|
||||
/// Parse a theme string ("light" -> Light, anything else -> Dark).
|
||||
ThemeMode theme_from_string(const std::string& theme);
|
||||
|
||||
} // namespace cp0
|
||||
Reference in New Issue
Block a user