/* * network.cpp * Network shares in Files: the saved SMB shares, the "Connect to Server" * dialog and mounting / disconnecting shares * Copyright (c) 2026 Daniel Hammer */ #include "filemanager_internal.hpp" #include #include #include #include #include #include // A share is mounted by the smbfs.elf provider (src/smbfs), which makes it an // ordinary drive: once mounted, nothing in Files treats it specially beyond // the icon and the Disconnect action. This file only decides what to mount. // // Shares the user connected to are remembered in /config/network.toml: // [shares] // list = ["host|share|user|domain|port||label", ...] // The password is present only when "Remember password" was ticked. It is // hex-encoded so any character survives the record format, NOT encrypted: // anyone who can read the user's config directory can read it. namespace filemanager { static constexpr const char* NET_CONFIG = "network"; static constexpr int NET_MAX_SHARES = 32; struct NetShare { char host[128]; char share[128]; char user[96]; char domain[64]; char label[64]; char password[128]; bool has_password; int port; }; static NetShare g_shares[NET_MAX_SHARES]; static int g_share_count = 0; static SvgIcon g_icon_server; static SvgIcon g_icon_server_lg; static SvgIcon g_icon_network; static void net_load_icons() { if (g_icon_server.pixels) return; Color def = colors::ICON_COLOR; g_icon_server = svg_load("0:/os/icons/network-server.svg", 16, 16, def); g_icon_server_lg = svg_load("0:/os/icons/network-server.svg", 48, 48, def); g_icon_network = svg_load("0:/os/icons/network-workgroup.svg", 16, 16, def); } const SvgIcon* filemanager_net_drive_icon(bool large) { net_load_icons(); const SvgIcon* icon = large ? &g_icon_server_lg : &g_icon_server; return icon->pixels ? icon : nullptr; } const SvgIcon* filemanager_net_connect_icon() { net_load_icons(); return g_icon_network.pixels ? &g_icon_network : nullptr; } // ============================================================================ // Saved shares // ============================================================================ static bool eq_nocase(const char* a, const char* b) { for (; *a && *b; a++, b++) { char ca = (*a >= 'A' && *a <= 'Z') ? (char)(*a + 32) : *a; char cb = (*b >= 'A' && *b <= 'Z') ? (char)(*b + 32) : *b; if (ca != cb) return false; } return *a == *b; } static void hex_encode(const char* in, char* out, int out_max) { static const char digits[] = "0123456789abcdef"; int n = 0; for (; *in && n + 2 < out_max; in++) { out[n++] = digits[((unsigned char)*in) >> 4]; out[n++] = digits[((unsigned char)*in) & 0xF]; } out[n] = '\0'; } static int hex_digit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; return -1; } static void hex_decode(const char* in, int len, char* out, int out_max) { int n = 0; for (int i = 0; i + 1 < len && n + 1 < out_max; i += 2) { int hi = hex_digit(in[i]); int lo = hex_digit(in[i + 1]); if (hi < 0 || lo < 0) break; out[n++] = (char)((hi << 4) | lo); } out[n] = '\0'; } // Copy the next '|'-separated field of *cursor into out; false at the end. static bool next_field(const char** cursor, char* out, int out_max) { const char* s = *cursor; if (s == nullptr) return false; int n = 0; while (*s && *s != '|') { if (n + 1 < out_max) out[n++] = *s; s++; } out[n] = '\0'; *cursor = *s == '|' ? s + 1 : nullptr; return true; } static bool parse_share(const char* rec, NetShare* out) { montauk::memset(out, 0, sizeof(*out)); const char* cur = rec; char port[16]; char pwhex[300]; if (!next_field(&cur, out->host, sizeof(out->host)) || !next_field(&cur, out->share, sizeof(out->share)) || !next_field(&cur, out->user, sizeof(out->user)) || !next_field(&cur, out->domain, sizeof(out->domain)) || !next_field(&cur, port, sizeof(port)) || !next_field(&cur, pwhex, sizeof(pwhex))) return false; // The label is last so it may contain the separator. if (cur) montauk::strncpy(out->label, cur, sizeof(out->label)); out->port = atoi(port); out->has_password = pwhex[0] != '\0'; hex_decode(pwhex, montauk::slen(pwhex), out->password, sizeof(out->password)); return out->host[0] && out->share[0]; } static void net_read(DesktopState* ds) { g_share_count = 0; if (!ds || !ds->current_user[0]) return; montauk::toml::Doc doc = montauk::config::load_user(ds->current_user, NET_CONFIG); if (auto* arr = doc.get_array("shares.list")) { for (int i = 0; i < arr->array.count && g_share_count < NET_MAX_SHARES; i++) { auto* v = arr->array.items[i]; if (v->type != montauk::toml::Type::String) continue; if (parse_share(v->str, &g_shares[g_share_count])) g_share_count++; } } doc.destroy(); } static void net_write(DesktopState* ds) { if (!ds || !ds->current_user[0]) return; montauk::toml::Doc doc; doc.init(); auto* arr = montauk::toml::Value::make_array("shares.list"); for (int i = 0; i < g_share_count; i++) { const NetShare& s = g_shares[i]; char pwhex[300] = ""; if (s.has_password) hex_encode(s.password, pwhex, sizeof(pwhex)); char rec[900]; snprintf(rec, sizeof(rec), "%s|%s|%s|%s|%d|%s|%s", s.host, s.share, s.user, s.domain, s.port, pwhex, s.label); arr->array.push(montauk::toml::Value::make_string(nullptr, rec, montauk::slen(rec))); } doc.entries.push(arr); montauk::config::ensure_user_dir(ds->current_user); montauk::config::save_user(ds->current_user, NET_CONFIG, &doc); doc.destroy(); } static int net_find(const char* host, const char* share) { for (int i = 0; i < g_share_count; i++) { if (eq_nocase(g_shares[i].host, host) && eq_nocase(g_shares[i].share, share)) return i; } return -1; } // Remember a share that was just mounted (most recent first). Re-reads the // file first so several Files windows do not undo one another's edits. static void net_remember(DesktopState* ds, const NetShare& share) { net_read(ds); int at = net_find(share.host, share.share); if (at < 0) { if (g_share_count == NET_MAX_SHARES) g_share_count--; at = g_share_count++; } for (int i = at; i > 0; i--) g_shares[i] = g_shares[i - 1]; g_shares[0] = share; net_write(ds); } static void net_forget(DesktopState* ds, const char* host, const char* share) { net_read(ds); int at = net_find(host, share); if (at < 0) return; for (int i = at; i + 1 < g_share_count; i++) g_shares[i] = g_shares[i + 1]; g_share_count--; net_write(ds); } void filemanager_net_load(DesktopState* ds) { net_read(ds); } int filemanager_net_share_count() { return g_share_count; } // Sidebar label of saved share i, and (optionally) the drive it is mounted on. bool filemanager_net_share(int i, char* label, int label_max, int* mounted_drive) { if (i < 0 || i >= g_share_count) return false; const NetShare& s = g_shares[i]; if (label) { if (s.label[0]) snprintf(label, (size_t)label_max, "%s", s.label); else snprintf(label, (size_t)label_max, "%s on %s", s.share, s.host); } if (mounted_drive) *mounted_drive = smb::find_mount(s.host, s.share); return true; } // ============================================================================ // Drive watching // ============================================================================ // Shares are mounted and ejected by other processes, so notice drives coming // and going: refresh the sidebar, and leave a folder whose drive vanished. void filemanager_net_poll(FileManagerState* fm) { static uint64_t last_check = 0; static uint64_t last_signature = 0; uint64_t now = montauk::get_milliseconds(); if (now - last_check < 1000) return; last_check = now; int drives[FM_MAX_DRIVES]; int n = montauk::drivelist(drives, FM_MAX_DRIVES); uint64_t signature = (uint64_t)n; for (int i = 0; i < n; i++) signature = signature * 1099511628211ULL + (uint64_t)(drives[i] + 1); if (signature == last_signature) return; bool first = last_signature == 0; last_signature = signature; if (first) return; filemanager_sidebar_rebuild(fm); if (filemanager_is_computer_view(fm)) { filemanager_refresh(fm); } else if (!filemanager_is_virtual_view(fm)) { int current = atoi(fm->current_path); bool present = false; for (int i = 0; i < n; i++) present = present || drives[i] == current; if (!present) filemanager_go_home(fm); } } void filemanager_net_disconnect(FileManagerState* fm, int drive) { montauk::abi::UserFsInfo info; if (montauk::userfs_info(drive, &info) != 0) return; // Step out of the share first so nothing of ours keeps reading it. if (!filemanager_is_computer_view(fm) && !filemanager_is_virtual_view(fm) && atoi(fm->current_path) == drive) { filemanager_go_home(fm); } if (smb::unmount(drive) != 0) { filemanager_show_error(fm->desktop, fm, "Couldn't disconnect the share", info.source, montauk::PermissionDenied); } } void filemanager_net_forget(FileManagerState* fm, int saved_index) { if (saved_index < 0 || saved_index >= g_share_count) return; char host[128]; char share[128]; montauk::strncpy(host, g_shares[saved_index].host, sizeof(host)); montauk::strncpy(share, g_shares[saved_index].share, sizeof(share)); net_forget(fm->desktop, host, share); filemanager_sidebar_rebuild(fm); } // ============================================================================ // Share browsing (worker thread) // ============================================================================ static constexpr int BROWSE_MAX = 64; struct BrowseJob { // Two owners, the dialog and the worker; whoever lets go last frees it. int refs; volatile int done; int tid; char host[128]; char user[96]; char domain[64]; char password[128]; int port; int count; // shares found, or a negative smb::Status char names[BROWSE_MAX][96]; char comments[BROWSE_MAX][64]; }; static void browse_release(BrowseJob* job) { if (__atomic_sub_fetch(&job->refs, 1, __ATOMIC_ACQ_REL) == 0) { montauk::memset(job->password, 0, sizeof(job->password)); montauk::mfree(job); } } static int browse_worker(void* arg) { BrowseJob* job = (BrowseJob*)arg; smb::MontaukLink link; int r = smb::link_open(&link, job->host, job->port); if (r == smb::OK) { smb::Client client; smb::Credentials creds = {job->user, job->password, job->domain}; r = smb::connect(&client, smb::link_io(&link), job->host, creds); if (r == smb::OK) { auto* shares = (smb::ShareInfo*)montauk::malloc(sizeof(smb::ShareInfo) * BROWSE_MAX); if (shares == nullptr) { r = smb::ERR_NOMEM; } else { r = smb::list_shares(&client, shares, BROWSE_MAX); int kept = 0; for (int i = 0; i < r; i++) { // Folders people can open: disk shares that are not administrative. if (shares[i].type != smb::SHARE_TYPE_DISK || shares[i].hidden) continue; montauk::strncpy(job->names[kept], shares[i].name, sizeof(job->names[kept])); montauk::strncpy(job->comments[kept], shares[i].comment, sizeof(job->comments[kept])); kept++; } if (r >= 0) r = kept; montauk::mfree(shares); } } smb::disconnect(&client); smb::link_close(&link); } job->count = r; __atomic_store_n(&job->done, 1, __ATOMIC_RELEASE); browse_release(job); return 0; } // ============================================================================ // Connect to Server dialog // ============================================================================ enum ConnectField : int { CF_SERVER = 0, CF_SHARE, CF_USER, CF_PASSWORD, CF_COUNT, }; struct ConnectDialogState { DesktopState* desktop; FileManagerState* owner; Color accent; int mouse_x; int mouse_y; char text[CF_COUNT][128]; mtk::TextInputState input[CF_COUNT]; int focus; bool remember; int port; // Share list filled by Browse. BrowseJob* browse; char share_names[BROWSE_MAX][96]; char share_comments[BROWSE_MAX][64]; int share_count; int share_scroll; bool browsed; // Mount in progress. bool connecting; smb::MountJob job; NetShare pending; char status[160]; bool status_error; }; static constexpr int CD_W = 440; static constexpr int CD_PAD = 16; static constexpr int CD_BTN_H = 30; static constexpr int CD_FIELD_H = 30; static constexpr int CD_ROW_H = 24; static constexpr int CD_LIST_ROWS = 5; static constexpr int CD_BROWSE_W = 96; static const char* const CD_LABELS[CF_COUNT] = {"Server", "Share", "User name", "Password"}; struct ConnectLayout { int label_y[CF_COUNT]; Rect field[CF_COUNT]; Rect browse; Rect remember; Rect list; int status_y; Rect connect; Rect cancel; }; static ConnectLayout cd_layout(int cw, int ch, const mtk::Theme& theme) { ConnectLayout lo; int fh = system_font_height(); int w = cw - CD_PAD * 2; int y = CD_PAD; for (int i = 0; i < CF_COUNT; i++) { int fw = i == CF_SHARE ? w - CD_BROWSE_W - 8 : w; lo.label_y[i] = y; lo.field[i] = mtk::labeled_text_input_rect(CD_PAD, y, fw, theme, CD_FIELD_H); y = lo.field[i].y + CD_FIELD_H + 10; } lo.browse = {CD_PAD + w - CD_BROWSE_W, lo.field[CF_SHARE].y, CD_BROWSE_W, CD_FIELD_H}; lo.remember = {CD_PAD, y, w, 22}; y += 22 + 10; lo.list = {CD_PAD, y + fh + 4, w, CD_ROW_H * CD_LIST_ROWS}; lo.status_y = lo.list.y + lo.list.h + 10; int btn_w = 96; int btn_h = CD_BTN_H; int by = ch - CD_PAD - btn_h; lo.cancel = {cw - CD_PAD - btn_w, by, btn_w, btn_h}; lo.connect = {lo.cancel.x - 8 - btn_w, by, btn_w, btn_h}; return lo; } // Window height that fits the top-down layout (fields, list, status line) // above the bottom-anchored buttons. The fields' height follows the font, so // a fixed height let the list run into the buttons. static int cd_window_height(const mtk::Theme& theme) { ConnectLayout lo = cd_layout(CD_W, 0, theme); int content_h = lo.status_y + system_font_height() + 12 + CD_BTN_H + CD_PAD; return content_h + gui::TITLEBAR_HEIGHT + gui::BORDER_WIDTH; } static void cd_set_status(ConnectDialogState* st, const char* text, bool error) { montauk::strncpy(st->status, text ? text : "", sizeof(st->status)); st->status_error = error; } static void cd_set_field(ConnectDialogState* st, int field, const char* text) { montauk::strncpy(st->text[field], text ? text : "", sizeof(st->text[field])); mtk::text_input_reset(st->input[field], montauk::slen(st->text[field])); } static void cd_close(ConnectDialogState* st) { DesktopState* ds = st->desktop; for (int i = 0; ds && i < ds->window_count; i++) { if (ds->windows[i].app_data == st) { desktop_close_window(ds, i); return; } } } // The Files window that opened the dialog, if it is still there. static FileManagerState* cd_live_owner(ConnectDialogState* st) { DesktopState* ds = st->desktop; for (int i = 0; ds && i < ds->window_count; i++) { Window& w = ds->windows[i]; if (w.app_data == st->owner && w.on_draw == filemanager_on_draw) return st->owner; } return nullptr; } // Accept "DOMAIN\user" and "user@domain" in the user name field. static void cd_split_user(const char* text, char* user, int user_max, char* domain, int domain_max) { user[0] = '\0'; domain[0] = '\0'; const char* sep = nullptr; for (const char* p = text; *p; p++) { if (*p == '\\' || *p == '/') { sep = p; break; } } if (sep) { int n = (int)(sep - text); if (n >= domain_max) n = domain_max - 1; montauk::memcpy(domain, text, n); domain[n] = '\0'; montauk::strncpy(user, sep + 1, user_max); return; } montauk::strncpy(user, text, user_max); } static void cd_trim(char* s) { int len = montauk::slen(s); while (len > 0 && s[len - 1] == ' ') s[--len] = '\0'; int lead = 0; while (s[lead] == ' ') lead++; if (lead > 0) { for (int i = 0; i <= len - lead; i++) s[i] = s[i + lead]; } } // Read the form into a NetShare. The server field may also hold a whole // "smb://host/share" or "\\host\share" address. static bool cd_read_form(ConnectDialogState* st, NetShare* out, bool need_share) { montauk::memset(out, 0, sizeof(*out)); char server[128]; montauk::strncpy(server, st->text[CF_SERVER], sizeof(server)); cd_trim(server); smb::Url url; if (!smb::parse_url(server, &url)) { cd_set_status(st, "Enter the name or address of the server.", true); st->focus = CF_SERVER; return false; } montauk::strncpy(out->host, url.host, sizeof(out->host)); out->port = url.port ? url.port : st->port; char share[128]; montauk::strncpy(share, st->text[CF_SHARE], sizeof(share)); cd_trim(share); if (!share[0]) montauk::strncpy(share, url.share, sizeof(share)); montauk::strncpy(out->share, share, sizeof(out->share)); if (need_share && !out->share[0]) { cd_set_status(st, "Enter the share to open, or press Browse to list them.", true); st->focus = CF_SHARE; return false; } char user[128]; montauk::strncpy(user, st->text[CF_USER], sizeof(user)); cd_trim(user); if (!user[0] && url.user[0]) { montauk::strncpy(out->user, url.user, sizeof(out->user)); montauk::strncpy(out->domain, url.domain, sizeof(out->domain)); } else { cd_split_user(user, out->user, sizeof(out->user), out->domain, sizeof(out->domain)); } montauk::strncpy(out->password, st->text[CF_PASSWORD], sizeof(out->password)); if (!smb::mount_arg_ok(out->host) || !smb::mount_arg_ok(out->share) || !smb::mount_arg_ok(out->user) || !smb::mount_arg_ok(out->domain)) { cd_set_status(st, "Names may not contain quotation marks.", true); return false; } return true; } static void cd_start_browse(ConnectDialogState* st) { if (st->browse || st->connecting) return; NetShare form; if (!cd_read_form(st, &form, false)) return; auto* job = (BrowseJob*)montauk::malloc(sizeof(BrowseJob)); if (!job) return; montauk::memset(job, 0, sizeof(*job)); job->refs = 2; montauk::strncpy(job->host, form.host, sizeof(job->host)); montauk::strncpy(job->user, form.user, sizeof(job->user)); montauk::strncpy(job->domain, form.domain, sizeof(job->domain)); montauk::strncpy(job->password, form.password, sizeof(job->password)); job->port = form.port; int tid = montauk::thread_spawn(browse_worker, job, 256 * 1024); if (tid <= 0) { montauk::mfree(job); cd_set_status(st, "Couldn't start looking for shares.", true); return; } job->tid = tid; st->browse = job; st->share_count = 0; st->share_scroll = 0; cd_set_status(st, "Looking for shares...", false); } static void cd_start_connect(ConnectDialogState* st) { if (st->connecting) return; NetShare form; if (!cd_read_form(st, &form, true)) return; DesktopState* ds = st->desktop; FileManagerState* owner = cd_live_owner(st); int mounted = smb::find_mount(form.host, form.share); if (mounted >= 0) { char path[16]; snprintf(path, sizeof(path), "%d:/", mounted); cd_close(st); if (owner) filemanager_go_to_path(owner, path); (void)ds; return; } smb::MountRequest rq; rq.host = form.host; rq.share = form.share; rq.user = form.user; rq.domain = form.domain; rq.password = form.password; rq.label = ""; rq.port = form.port; rq.readOnly = false; if (!smb::mount_begin(&st->job, rq)) { cd_set_status(st, "Couldn't start the network share helper.", true); return; } st->pending = form; st->connecting = true; cd_set_status(st, "Connecting...", false); } static void cd_connect_finished(ConnectDialogState* st, int drive) { DesktopState* ds = st->desktop; FileManagerState* owner = cd_live_owner(st); NetShare saved = st->pending; saved.has_password = st->remember && saved.user[0] != '\0'; if (!saved.has_password) montauk::memset(saved.password, 0, sizeof(saved.password)); net_remember(ds, saved); char path[16]; snprintf(path, sizeof(path), "%d:/", drive); cd_close(st); // frees st if (owner) { filemanager_sidebar_rebuild(owner); filemanager_go_to_path(owner, path); } } static void cd_on_poll(Window* win) { ConnectDialogState* st = (ConnectDialogState*)win->app_data; if (!st) return; if (st->browse && __atomic_load_n(&st->browse->done, __ATOMIC_ACQUIRE)) { BrowseJob* job = st->browse; st->browse = nullptr; montauk::thread_join(job->tid, nullptr); st->browsed = true; st->share_count = 0; if (job->count < 0) { char msg[160]; snprintf(msg, sizeof(msg), "Couldn't list the shares: %s.", job->count == smb::ERR_NOT_FOUND ? "server not found" : job->count == smb::ERR_IO ? "could not connect to the server" : smb::status_text(job->count)); cd_set_status(st, msg, true); if (job->count == smb::ERR_AUTH) st->focus = CF_USER; } else { for (int i = 0; i < job->count && i < BROWSE_MAX; i++) { montauk::strncpy(st->share_names[i], job->names[i], sizeof(st->share_names[i])); montauk::strncpy(st->share_comments[i], job->comments[i], sizeof(st->share_comments[i])); } st->share_count = job->count; cd_set_status(st, job->count ? "Choose a share, then press Connect." : "The server offers no shares to this account.", false); } browse_release(job); win->dirty = true; } if (st->connecting) { int drive = -1; int status = 0; char error[160]; int r = smb::mount_poll(&st->job, &drive, &status, error, sizeof(error)); if (r > 0) { cd_connect_finished(st, drive); return; // the dialog is gone } if (r < 0) { st->connecting = false; char msg[200]; snprintf(msg, sizeof(msg), "Couldn't connect: %s.", error); cd_set_status(st, msg, true); if (status == smb::ERR_AUTH || status == smb::ERR_ACCESS) { st->focus = st->text[CF_USER][0] ? CF_PASSWORD : CF_USER; int len = montauk::slen(st->text[st->focus]); st->input[st->focus].selection_anchor = 0; st->input[st->focus].cursor = len; } else if (status == smb::ERR_BAD_SHARE) { st->focus = CF_SHARE; } win->dirty = true; } } } static void cd_on_draw(Window* win, Framebuffer& fb) { (void)fb; ConnectDialogState* st = (ConnectDialogState*)win->app_data; if (!st) return; Canvas c(win); mtk::Theme theme = mtk::make_theme(st->accent); c.fill(theme.window_bg); ConnectLayout lo = cd_layout(c.w, c.h, theme); int fh = system_font_height(); bool busy = st->connecting; for (int i = 0; i < CF_COUNT; i++) { mtk::draw_labeled_text_field(c, lo.field[i].x, lo.label_y[i], lo.field[i].w, CD_LABELS[i], st->text[i], st->input[i].cursor, st->focus == i && !busy, i == CF_PASSWORD, theme, CD_FIELD_H, st->input[i].selection_anchor); } if (!st->text[CF_USER][0] && st->focus != CF_USER) { c.text(lo.field[CF_USER].x + 8, lo.field[CF_USER].y + (CD_FIELD_H - fh) / 2, "Leave empty to connect as a guest", theme.text_subtle); } bool can_browse = !busy && !st->browse; mtk::draw_button(c, lo.browse, st->browse ? "Looking..." : "Browse", mtk::BUTTON_SECONDARY, mtk::widget_state(false, can_browse && lo.browse.contains(st->mouse_x, st->mouse_y), can_browse), theme); mtk::draw_checkbox(c, lo.remember, "Remember password", mtk::check_state(st->remember), theme, !busy, lo.remember.contains(st->mouse_x, st->mouse_y)); // Share list. c.text(lo.list.x, lo.list.y - fh - 4, "Shares on the server", theme.text_muted); c.fill_rect(lo.list.x, lo.list.y, lo.list.w, lo.list.h, colors::WHITE); c.rect(lo.list.x - 1, lo.list.y - 1, lo.list.w + 2, lo.list.h + 2, theme.border); if (st->share_count == 0) { c.text(lo.list.x + 10, lo.list.y + 8, st->browsed ? "No shares to show." : "Press Browse to list the shares.", theme.text_muted); } for (int v = 0; v < CD_LIST_ROWS; v++) { int i = st->share_scroll + v; if (i >= st->share_count) break; Rect r = {lo.list.x + 1, lo.list.y + v * CD_ROW_H, lo.list.w - 2, CD_ROW_H}; bool selected = montauk::streq(st->share_names[i], st->text[CF_SHARE]); if (selected) c.fill_rect(r.x, r.y, r.w, r.h, gui::mtk::accent_hover_tint(st->accent)); else if (r.contains(st->mouse_x, st->mouse_y)) c.fill_rect(r.x, r.y, r.w, r.h, theme.surface_alt); const SvgIcon* icon = filemanager_net_drive_icon(false); if (icon) c.icon(r.x + 6, r.y + (CD_ROW_H - icon->height) / 2, *icon); char fit[96]; filemanager_fit_text(st->share_names[i], fit, sizeof(fit), 150); c.text(r.x + 28, r.y + (CD_ROW_H - fh) / 2, fit, theme.text); filemanager_fit_text(st->share_comments[i], fit, sizeof(fit), r.w - 200); c.text(r.x + 190, r.y + (CD_ROW_H - fh) / 2, fit, theme.text_muted); } if (st->share_count > CD_LIST_ROWS) { int thumb_h = lo.list.h * CD_LIST_ROWS / st->share_count; if (thumb_h < 16) thumb_h = 16; int max_scroll = st->share_count - CD_LIST_ROWS; int thumb_y = lo.list.y + (lo.list.h - thumb_h) * st->share_scroll / max_scroll; c.fill_rect(lo.list.x + lo.list.w - 6, thumb_y, 4, thumb_h, theme.border); } if (st->status[0]) { char fit[160]; filemanager_fit_text(st->status, fit, sizeof(fit), c.w - CD_PAD * 2); c.text(CD_PAD, lo.status_y, fit, st->status_error ? theme.danger : theme.text_muted); } mtk::draw_button(c, lo.connect, busy ? "Connecting..." : "Connect", mtk::BUTTON_PRIMARY, mtk::widget_state(false, !busy && lo.connect.contains(st->mouse_x, st->mouse_y), !busy), theme); mtk::draw_button(c, lo.cancel, "Cancel", mtk::BUTTON_SECONDARY, mtk::widget_state(false, lo.cancel.contains(st->mouse_x, st->mouse_y), true), theme); for (int i = 0; i < CF_COUNT; i++) { if (!st->input[i].context.open) continue; mtk::draw_text_input_context_menu( c, st->input[i], theme, mtk::text_input_has_selection(st->input[i], st->text[i], sizeof(st->text[i])), i == CF_PASSWORD); } } static void cd_on_mouse(Window* win, MouseEvent& ev) { ConnectDialogState* st = (ConnectDialogState*)win->app_data; if (!st) return; mtk::DesktopHost host(win); int mx = 0; int my = 0; if (!host.map_mouse(ev, &mx, &my)) return; if (mx != st->mouse_x || my != st->mouse_y) host.invalidate(); st->mouse_x = mx; st->mouse_y = my; mtk::Theme theme = mtk::make_theme(st->accent); ConnectLayout lo = cd_layout(win->content_w, win->content_h, theme); if (lo.cancel.contains(mx, my) && ev.left_pressed()) { cd_close(st); return; } if (st->connecting) return; // An open field menu takes every event. for (int i = 0; i < CF_COUNT; i++) { if (!st->input[i].context.open) continue; mtk::text_input_handle_mouse(st->input[i], lo.field[i], st->text[i], sizeof(st->text[i]), mx, my, ev.buttons, ev.prev_buttons, win->content_w, win->content_h, true, i == CF_PASSWORD); host.invalidate(); return; } for (int i = 0; i < CF_COUNT; i++) { bool inside = lo.field[i].contains(mx, my); if ((ev.left_pressed() || ev.right_pressed()) && inside) st->focus = i; if (st->focus != i) continue; int result = mtk::text_input_handle_mouse(st->input[i], lo.field[i], st->text[i], sizeof(st->text[i]), mx, my, ev.buttons, ev.prev_buttons, win->content_w, win->content_h, true, i == CF_PASSWORD); if (result != mtk::TEXT_INPUT_NONE) host.invalidate(); } if (ev.scroll != 0 && lo.list.contains(mx, my)) { int max_scroll = st->share_count - CD_LIST_ROWS; st->share_scroll -= ev.scroll; if (st->share_scroll > max_scroll) st->share_scroll = max_scroll; if (st->share_scroll < 0) st->share_scroll = 0; host.invalidate(); } if (!ev.left_pressed()) return; host.invalidate(); if (lo.browse.contains(mx, my)) { cd_start_browse(st); } else if (lo.remember.contains(mx, my)) { st->remember = !st->remember; } else if (lo.list.contains(mx, my)) { int i = st->share_scroll + (my - lo.list.y) / CD_ROW_H; if (i >= 0 && i < st->share_count) cd_set_field(st, CF_SHARE, st->share_names[i]); } else if (lo.connect.contains(mx, my)) { cd_start_connect(st); } } static void cd_on_key(Window* win, const montauk::abi::KeyEvent& key) { ConnectDialogState* st = (ConnectDialogState*)win->app_data; if (!st || !key.pressed) return; if (key.scancode == 0x01) { // Escape cd_close(st); return; } if (st->connecting) return; if (key.ascii == '\n' || key.ascii == '\r') { cd_start_connect(st); return; } if (key.ascii == '\t') { st->focus = (st->focus + (key.shift ? CF_COUNT - 1 : 1)) % CF_COUNT; int len = montauk::slen(st->text[st->focus]); st->input[st->focus].selection_anchor = 0; st->input[st->focus].cursor = len; return; } mtk::text_input_key(st->input[st->focus], st->text[st->focus], sizeof(st->text[st->focus]), key, nullptr, nullptr, st->focus == CF_PASSWORD); if (st->status_error) cd_set_status(st, "", false); } static void cd_on_close(Window* win) { ConnectDialogState* st = (ConnectDialogState*)win->app_data; if (!st) return; if (st->connecting) smb::mount_cancel(&st->job); // A browse still running finishes on its own and frees the job. if (st->browse) browse_release(st->browse); FileManagerState* owner = cd_live_owner(st); if (owner && owner->connect_dialog == st) owner->connect_dialog = nullptr; montauk::memset(st, 0, sizeof(*st)); // drops the typed password montauk::mfree(st); win->app_data = nullptr; } // Open the dialog, prefilled from saved share `saved_index` (-1 = empty). // A saved share that needs no more input starts connecting right away. void filemanager_net_show_connect(FileManagerState* fm, int saved_index) { if (!fm || !fm->desktop) return; DesktopState* ds = fm->desktop; if (fm->connect_dialog) { for (int i = 0; i < ds->window_count; i++) { if (ds->windows[i].app_data == fm->connect_dialog) { desktop_raise_window(ds, i); return; } } fm->connect_dialog = nullptr; } auto* st = (ConnectDialogState*)montauk::malloc(sizeof(ConnectDialogState)); if (!st) return; montauk::memset(st, 0, sizeof(*st)); st->desktop = ds; st->owner = fm; st->accent = ds->settings.accent_color; st->job.pid = -1; for (int i = 0; i < CF_COUNT; i++) mtk::text_input_reset(st->input[i], 0); bool ready = false; if (saved_index >= 0 && saved_index < g_share_count) { const NetShare& s = g_shares[saved_index]; cd_set_field(st, CF_SERVER, s.host); cd_set_field(st, CF_SHARE, s.share); char user[160]; if (s.domain[0]) snprintf(user, sizeof(user), "%s\\%s", s.domain, s.user); else snprintf(user, sizeof(user), "%s", s.user); cd_set_field(st, CF_USER, user); st->port = s.port; if (s.has_password) { cd_set_field(st, CF_PASSWORD, s.password); st->remember = true; } // Guests and remembered passwords need nothing typed. ready = !s.user[0] || s.has_password; st->focus = ready ? CF_SERVER : CF_PASSWORD; } int wx = 0; int wy = 0; int cd_h = cd_window_height(mtk::make_theme(st->accent)); props_center_dialog(ds, fm, CD_W, cd_h, &wx, &wy); int idx = desktop_create_window(ds, "Connect to Server", wx, wy, CD_W, cd_h); if (idx < 0) { montauk::mfree(st); return; } Window* win = &ds->windows[idx]; fm->connect_dialog = st; win->app_data = st; win->on_draw = cd_on_draw; win->on_mouse = cd_on_mouse; win->on_key = cd_on_key; win->on_poll = cd_on_poll; win->on_close = cd_on_close; if (ready) cd_start_connect(st); } // A saved share was chosen in the sidebar: open it if it is mounted, // otherwise connect to it. void filemanager_net_open_saved(FileManagerState* fm, int saved_index) { if (saved_index < 0 || saved_index >= g_share_count) return; int drive = smb::find_mount(g_shares[saved_index].host, g_shares[saved_index].share); if (drive >= 0) { char path[16]; snprintf(path, sizeof(path), "%d:/", drive); filemanager_go_to_path(fm, path); return; } filemanager_net_show_connect(fm, saved_index); } } // namespace filemanager