Linux + iPhone Continuity / iMessage / SMS
add libnotify notifcations on file received
Zack Bartela268fd415890
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txtindex 3c970d8..9443569 100644--- a/src/core/CMakeLists.txt+++ b/src/core/CMakeLists.txt@@ -13,6 +13,8 @@ add_library(tether STATICsrc/wayland_protocols/wlr-data-control-unstable-v1.cpp)+pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0 gio-2.0)+target_include_directories(tether PUBLICinclude)@@ -23,4 +25,5 @@ target_link_libraries(tether PUBLICOpenSSL::SSLOpenSSL::CryptoPkgConfig::AVAHI+ PkgConfig::GLIB)diff --git a/src/core/include/tether/file_transfer.hpp b/src/core/include/tether/file_transfer.hppindex 9f7a80d..992589f 100644--- a/src/core/include/tether/file_transfer.hpp+++ b/src/core/include/tether/file_transfer.hpp@@ -1,6 +1,7 @@#pragma once#include <filesystem>+#include <functional>#include <fstream>#include <map>#include <memory>@@ -17,6 +18,7 @@ namespace tether {bool handle_start(const std::string& transfer_id, const std::string& filename, size_t size);bool handle_chunk(const std::string& transfer_id, int chunk_index, const std::string& b64_data);bool handle_end(const std::string& transfer_id);+ void set_on_complete(std::function<void(const std::filesystem::path&)> callback);private:std::string get_downloads_dir();@@ -32,6 +34,7 @@ namespace tether {std::mutex mutex_;std::map<std::string, std::shared_ptr<Transfer>> transfers_;+ std::function<void(const std::filesystem::path&)> on_complete_;};extern FileReceiveManager* g_file_manager;diff --git a/src/core/src/file_transfer.cpp b/src/core/src/file_transfer.cppindex 39673e0..c346c4f 100644--- a/src/core/src/file_transfer.cpp+++ b/src/core/src/file_transfer.cpp@@ -1,8 +1,7 @@#include "tether/file_transfer.hpp"#include "tether/base64.hpp"+#include <glib.h>#include <iostream>-#include <stdlib.h>-#include <stdio.h>namespace tether {@@ -19,33 +18,32 @@ FileReceiveManager::~FileReceiveManager() {}std::string FileReceiveManager::get_downloads_dir() {- std::string path;- FILE* pipe = popen("xdg-user-dir DOWNLOAD 2>/dev/null", "r");- if (pipe) {- char buffer[1024];- if (fgets(buffer, sizeof(buffer), pipe) != nullptr) {- path = buffer;- if (!path.empty() && path.back() == '\n') path.pop_back();- }- pclose(pipe);+ if (const char* downloads = g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD)) {+ return downloads;}- if (path.empty() || path == getenv("HOME")) {- const char* home = getenv("HOME");- if (home) path = std::string(home) + "/Downloads";++ if (const char* home = g_get_home_dir()) {+ return std::string(home) + "/Downloads";}- return path;++ return "Downloads";}std::filesystem::path FileReceiveManager::resolve_filename(const std::string& raw_name) {auto dl_dir = std::filesystem::path(get_downloads_dir());std::filesystem::create_directories(dl_dir);-- std::filesystem::path p = dl_dir / raw_name;++ std::string safe_name = std::filesystem::path(raw_name).filename().string();+ if (safe_name.empty()) {+ safe_name = "received-file";+ }++ std::filesystem::path p = dl_dir / safe_name;if (!std::filesystem::exists(p)) return p;-+std::string stem = p.stem().string();std::string ext = p.extension().string();-+int counter = 1;while (true) {p = dl_dir / (stem + "(" + std::to_string(counter) + ")" + ext);@@ -56,20 +54,20 @@ std::filesystem::path FileReceiveManager::resolve_filename(const std::string& rabool FileReceiveManager::handle_start(const std::string& transfer_id, const std::string& filename, size_t size) {std::lock_guard<std::mutex> lock(mutex_);-+if (transfers_.find(transfer_id) != transfers_.end()) return false;-+auto t = std::make_shared<Transfer>();t->transfer_id = transfer_id;t->expected_size = size;t->filepath = resolve_filename(filename);-+t->stream = std::make_unique<std::ofstream>(t->filepath, std::ios::binary);if (!t->stream->is_open()) {std::cerr << "FileReceiveManager: Failed to open output file: " << t->filepath << std::endl;return false;}-+transfers_[transfer_id] = t;std::cout << "FileReceiveManager: Started transfer " << transfer_id << " saving to " << t->filepath << std::endl;return true;@@ -77,32 +75,50 @@ bool FileReceiveManager::handle_start(const std::string& transfer_id, const std:bool FileReceiveManager::handle_chunk(const std::string& transfer_id, int chunk_index, const std::string& b64_data) {std::lock_guard<std::mutex> lock(mutex_);-+auto it = transfers_.find(transfer_id);if (it == transfers_.end()) return false;-+auto& t = it->second;auto raw_data = base64_decode(b64_data);-+t->stream->write(reinterpret_cast<const char*>(raw_data.data()), raw_data.size());t->bytes_written += raw_data.size();-+return true;}bool FileReceiveManager::handle_end(const std::string& transfer_id) {- std::lock_guard<std::mutex> lock(mutex_);-- auto it = transfers_.find(transfer_id);- if (it == transfers_.end()) return false;-- auto& t = it->second;- t->stream->close();-- std::cout << "FileReceiveManager: Finished transfer " << transfer_id << ". Wrote " << t->bytes_written << " bytes to " << t->filepath << std::endl;-- transfers_.erase(it);+ std::function<void(const std::filesystem::path&)> on_complete;+ std::filesystem::path completed_path;++ {+ std::lock_guard<std::mutex> lock(mutex_);++ auto it = transfers_.find(transfer_id);+ if (it == transfers_.end()) return false;++ auto& t = it->second;+ t->stream->close();++ completed_path = t->filepath;+ on_complete = on_complete_;++ std::cout << "FileReceiveManager: Finished transfer " << transfer_id << ". Wrote " << t->bytes_written << " bytes to " << t->filepath << std::endl;++ transfers_.erase(it);+ }++ if (on_complete) {+ on_complete(completed_path);+ }+return true;}+void FileReceiveManager::set_on_complete(std::function<void(const std::filesystem::path&)> callback) {+ std::lock_guard<std::mutex> lock(mutex_);+ on_complete_ = std::move(callback);+}+}diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txtindex 54b33fe..21c11c1 100644--- a/src/daemon/CMakeLists.txt+++ b/src/daemon/CMakeLists.txt@@ -1,8 +1,11 @@add_executable(tetherdmain.cpp+ notification.cpp)-target_link_libraries(tetherd PRIVATE tether)+pkg_check_modules(LIBNOTIFY REQUIRED IMPORTED_TARGET libnotify)++target_link_libraries(tetherd PRIVATE tether PkgConfig::LIBNOTIFY)# Output into root build dir instead of nested build/src/daemonset_target_properties(tetherd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")diff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex 30dcdac..d234404 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -1,6 +1,7 @@#include <csignal>#include <iostream>#include <nlohmann/json.hpp>+#include "notification.hpp"#include <tether/core.hpp>#include <tether/crypto.hpp>#include <tether/discovery.hpp>@@ -78,6 +79,14 @@ int main(int argc, char** argv) {}tether::FileReceiveManager file_mgr;+ tether::FileArrivalNotifier notifier;+ if (!notifier.init()) {+ std::cerr << "Warning: desktop notifications unavailable" << std::endl;+ } else {+ file_mgr.set_on_complete([¬ifier](const std::filesystem::path& path) {+ notifier.notify_file_arrived(path);+ });+ }tether::g_file_manager = &file_mgr;std::cout << "tetherd is running. Press Ctrl-C to stop." << std::endl;diff --git a/src/daemon/notification.cpp b/src/daemon/notification.cppnew file mode 100644index 0000000..093fe21--- /dev/null+++ b/src/daemon/notification.cpp@@ -0,0 +1,186 @@+#include "notification.hpp"++#include <gio/gio.h>+#include <glib.h>+#include <libnotify/notify.h>++#include <iostream>+#include <string>+#include <thread>++namespace tether {++namespace {++struct NotificationRequest {+ std::filesystem::path path;+};++struct NotificationActionData {+ std::string uri;+};++void free_request(gpointer data) {+ delete static_cast<NotificationRequest*>(data);+}++void free_action_data(gpointer data) {+ delete static_cast<NotificationActionData*>(data);+}++bool launch_uri(const std::string& uri) {+ GError* error = nullptr;+ gboolean ok = g_app_info_launch_default_for_uri(uri.c_str(), nullptr, &error);+ if (!ok) {+ std::cerr << "Failed to open URI '" << uri << "'";+ if (error) {+ std::cerr << ": " << error->message;+ g_error_free(error);+ }+ std::cerr << std::endl;+ return false;+ }++ return true;+}++void on_notification_action(NotifyNotification*, char*, gpointer user_data) {+ auto* action = static_cast<NotificationActionData*>(user_data);+ if (!action) {+ return;+ }++ launch_uri(action->uri);+}++void on_notification_closed(NotifyNotification* notification, gpointer) {+ g_object_unref(notification);+}++gboolean show_notification_on_main(gpointer user_data) {+ std::unique_ptr<NotificationRequest> request(static_cast<NotificationRequest*>(user_data));++ auto file = std::filesystem::absolute(request->path);+ auto file_string = file.string();+ auto folder_string = file.parent_path().string();+ auto file_uri = g_filename_to_uri(file_string.c_str(), nullptr, nullptr);+ auto folder_uri = g_filename_to_uri(folder_string.c_str(), nullptr, nullptr);++ if (!file_uri || !folder_uri) {+ if (file_uri) g_free(file_uri);+ if (folder_uri) g_free(folder_uri);+ std::cerr << "Failed to create notification URI for " << file << std::endl;+ return G_SOURCE_REMOVE;+ }++ NotifyNotification* notification = notify_notification_new(+ "File arrived",+ file.filename().c_str(),+ "document-save");++ notify_notification_set_hint(notification, "desktop-entry", g_variant_new_string("tether"));+ notify_notification_set_hint(notification, "resident", g_variant_new_boolean(TRUE));+ notify_notification_set_timeout(notification, 15000);++ auto* open_file = new NotificationActionData{file_uri};+ auto* open_folder = new NotificationActionData{folder_uri};++ g_signal_connect(notification, "closed", G_CALLBACK(on_notification_closed), nullptr);+ notify_notification_add_action(+ notification,+ "open-file",+ "Open File",+ on_notification_action,+ open_file,+ free_action_data);+ notify_notification_add_action(+ notification,+ "open-folder",+ "Open Folder",+ on_notification_action,+ open_folder,+ free_action_data);++ GError* error = nullptr;+ gboolean shown = notify_notification_show(notification, &error);+ if (!shown) {+ std::cerr << "Failed to show notification";+ if (error) {+ std::cerr << ": " << error->message;+ g_error_free(error);+ }+ std::cerr << std::endl;+ g_object_unref(notification);+ }+ g_free(file_uri);+ g_free(folder_uri);+ return G_SOURCE_REMOVE;+}++} // namespace++struct FileArrivalNotifier::Impl {+ GMainContext* context = nullptr;+ GMainLoop* loop = nullptr;+ std::thread thread;+ bool initialized = false;+};++FileArrivalNotifier::FileArrivalNotifier() : impl_(std::make_unique<Impl>()) {}++FileArrivalNotifier::~FileArrivalNotifier() {+ if (!impl_->initialized) {+ return;+ }++ g_main_context_invoke(impl_->context, [](gpointer data) -> gboolean {+ auto* loop = static_cast<GMainLoop*>(data);+ g_main_loop_quit(loop);+ return G_SOURCE_REMOVE;+ }, impl_->loop);++ if (impl_->thread.joinable()) {+ impl_->thread.join();+ }++ g_main_loop_unref(impl_->loop);+ g_main_context_unref(impl_->context);+ notify_uninit();+}++bool FileArrivalNotifier::init() {+ if (impl_->initialized) {+ return true;+ }++ if (!notify_init("tetherd")) {+ std::cerr << "Failed to initialize libnotify" << std::endl;+ return false;+ }++ impl_->context = g_main_context_new();+ impl_->loop = g_main_loop_new(impl_->context, FALSE);+ impl_->thread = std::thread([ctx = impl_->context, loop = impl_->loop]() {+ g_main_context_push_thread_default(ctx);+ g_main_loop_run(loop);+ g_main_context_pop_thread_default(ctx);+ });+ impl_->initialized = true;+ return true;+}++void FileArrivalNotifier::notify_file_arrived(const std::filesystem::path& path) {+ if (!impl_->initialized) {+ return;+ }++ auto* request = new NotificationRequest{path};+ g_main_context_invoke_full(+ impl_->context,+ G_PRIORITY_DEFAULT,+ show_notification_on_main,+ request,+ nullptr);+}++} // namespace tetherdiff --git a/src/daemon/notification.hpp b/src/daemon/notification.hppnew file mode 100644index 0000000..8493e15--- /dev/null+++ b/src/daemon/notification.hpp@@ -0,0 +1,24 @@+#pragma once++#include <filesystem>+#include <memory>++namespace tether {++ class FileArrivalNotifier {+ public:+ FileArrivalNotifier();+ ~FileArrivalNotifier();++ FileArrivalNotifier(const FileArrivalNotifier&) = delete;+ FileArrivalNotifier& operator=(const FileArrivalNotifier&) = delete;++ bool init();+ void notify_file_arrived(const std::filesystem::path& path);++ private:+ struct Impl;+ std::unique_ptr<Impl> impl_;+ };++} // namespace tether