/* * Connect.c -- MCDE: the Connect to Server dialog (Connect.h) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The reference's window: "Server Address:" with the address field, a "+" * that keeps it, a menu of the servers used before, "Favorite Servers:" as * a list, Remove, Browse (the network view) and Connect (Return). The * favourites are one address a line in ~/.dt/servers, the history the last * ten in ~/.dt/servers.recent, newest first. * * The mount is the network view's (NetView.c, McdeSys): this dialog only * asks for the address and says what went wrong, in place under the field. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Encaps.h" #include "SharedProcs.h" #include "FileMgr.h" #include "Navigation.h" #include "Connect.h" #define RECENT_MAX 10 #define FAV_MAX 64 typedef struct { FileMgrRec *rec; Widget shell; Widget form; Widget address; Widget history; /* the menu of servers used before */ Widget list; /* the favourites */ Widget error; Widget remove; } Connect; static Connect conn; static void RecentChosen(Widget w, XtPointer client, XtPointer call); /* how many servers System Settings > Appearance asks for (~/.dt/recent.conf, * "servers=N"); 10 when the file does not say */ static int RecentServers(void) { char path[600], line[256]; const char *home = getenv("HOME"); FILE *fp; int n = RECENT_MAX; if (home == NULL || (size_t) snprintf(path, sizeof path, "%s/.dt/recent.conf", home) >= sizeof path) return RECENT_MAX; if ((fp = fopen(path, "r")) == NULL) return RECENT_MAX; while (fgets(line, sizeof line, fp) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (strncmp(line, "servers=", 8) == 0) n = atoi(line + 8); } fclose(fp); if (n < 0) n = 0; if (n > RECENT_MAX) n = RECENT_MAX; return n; } /* ---- the two files ------------------------------------------------------ */ static void FilePath(const char *name, char *buf, size_t len) { const char *home = getenv("HOME"); (void)snprintf(buf, len, "%s/.dt/%s", home != NULL ? home : ".", name); } /* the lines of NAME, at most MAX, into LIST; how many */ static int ReadLines(const char *name, char **list, int max) { char path[512], line[512]; FILE *fp; int n = 0; FilePath(name, path, sizeof path); if ((fp = fopen(path, "r")) == NULL) return 0; while (n < max && fgets(line, sizeof line, fp) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (line[0] == '\0') continue; list[n++] = XtNewString(line); } fclose(fp); return n; } static void WriteLines(const char *name, char **list, int n) { char path[512], tmp[600]; FILE *fp; int i; FilePath(name, path, sizeof path); (void)snprintf(tmp, sizeof tmp, "%s.new", path); if ((fp = fopen(tmp, "w")) == NULL) return; for (i = 0; i < n; i++) fprintf(fp, "%s\n", list[i]); fclose(fp); (void)rename(tmp, path); } static void FreeLines(char **list, int n) { int i; for (i = 0; i < n; i++) XtFree(list[i]); } /* the address into ~/.dt/servers, once */ static void FavouriteAdd(const char *address) { char *list[FAV_MAX]; int n = ReadLines("servers", list, FAV_MAX), i; Boolean have = False; for (i = 0; i < n; i++) if (strcmp(list[i], address) == 0) have = True; if (!have && n < FAV_MAX) list[n++] = XtNewString(address); WriteLines("servers", list, n); FreeLines(list, n); } /* the address into ~/.dt/servers.recent, newest first, once */ static void RecentAdd(const char *address) { char *list[RECENT_MAX + 1]; int max = RecentServers(); int n = ReadLines("servers.recent", list, RECENT_MAX), i, m = 0; char *out[RECENT_MAX]; if (max <= 0) { WriteLines("servers.recent", out, 0); FreeLines(list, n); return; } for (i = 0; i < n; i++) if (strcmp(list[i], address) != 0 && m < max - 1) out[m++] = list[i]; if (m < max) out[m++] = XtNewString(address); WriteLines("servers.recent", out, m); for (i = 0; i < n; i++) XtFree(list[i]); XtFree(out[m - 1]); } /* ---- the dialog --------------------------------------------------------- */ static void SetError(const char *text) { XmString s = XmStringCreateLocalized((char *) text); XtVaSetValues(conn.error, XmNlabelString, s, NULL); XmStringFree(s); } static void FillFavourites(void) { char *list[FAV_MAX]; int n = ReadLines("servers", list, FAV_MAX), i; XmListDeleteAllItems(conn.list); for (i = 0; i < n; i++) { XmString s = XmStringCreateLocalized(list[i]); XmListAddItemUnselected(conn.list, s, 0); XmStringFree(s); } FreeLines(list, n); XtSetSensitive(conn.remove, False); } static void FillHistory(Widget w, XtPointer client, XtPointer call) { char *list[RECENT_MAX]; Widget pd = NULL, *kids = NULL; Cardinal nk = 0, k; int n, i; (void)w; (void)client; (void)call; XtVaGetValues(conn.history, XmNsubMenuId, &pd, NULL); if (pd == NULL) return; XtVaGetValues(pd, XmNchildren, &kids, XmNnumChildren, &nk, NULL); for (k = 0; k < nk; k++) XtDestroyWidget(kids[k]); n = ReadLines("servers.recent", list, RecentServers()); for (i = 0; i < n; i++) { XmString s = XmStringCreateLocalized(list[i]); Widget item = XtVaCreateManagedWidget("recentItem", xmPushButtonGadgetClass, pd, XmNlabelString, s, NULL); XmStringFree(s); XtAddCallback(item, XmNactivateCallback, RecentChosen, XtNewString(list[i])); XtFree(list[i]); } if (n == 0) { XmString s = XmStringCreateLocalized((char *) "No servers yet"); (void)XtVaCreateManagedWidget("recentNone", xmLabelGadgetClass, pd, XmNlabelString, s, NULL); XmStringFree(s); } } /* the address as the dialog gives it: an address without a scheme is an * sftp one */ static void Normalise(const char *in, char *out, size_t len) { if (strstr(in, "://") != NULL) (void)snprintf(out, len, "%s", in); else (void)snprintf(out, len, "sftp://%s", in); } static void ConnectNow(Widget w, XtPointer client, XtPointer call) { char *text = XmTextFieldGetString(conn.address); char url[512], mp[1100], err[512]; (void)w; (void)client; (void)call; if (text == NULL || text[0] == '\0') { SetError("An address looks like sftp://host/folder, " "nfs://host/path or afp://host/volume."); return; } Normalise(text, url, sizeof url); XtFree(text); if (_McdeNetConnectAddress(conn.shell, url, mp, sizeof mp, err, sizeof err)) { RecentAdd(url); XtPopdown(conn.shell); if (conn.rec != NULL) _McdeNavGoTo(conn.rec, mp); } else { SetError(err); XmProcessTraversal(conn.address, XmTRAVERSE_CURRENT); } } static void AddFavourite(Widget w, XtPointer client, XtPointer call) { char *text = XmTextFieldGetString(conn.address); char url[512]; (void)w; (void)client; (void)call; if (text == NULL || text[0] == '\0') return; Normalise(text, url, sizeof url); FavouriteAdd(url); XtFree(text); FillFavourites(); } static void RemoveFavourite(Widget w, XtPointer client, XtPointer call) { char path[512], *list[FAV_MAX], *sel = NULL; int n, i, m = 0; Widget *items = NULL; (void)w; (void)client; (void)call; XtVaGetValues(conn.list, XmNselectedItems, &items, XmNselectedItemCount, &n, NULL); if (n < 1 || items == NULL || items[0] == NULL) return; if (!XmStringGetLtoR(items[0], XmFONTLIST_DEFAULT_TAG, &sel)) return; n = ReadLines("servers", list, FAV_MAX); { char *keep[FAV_MAX]; for (i = 0; i < n; i++) if (strcmp(list[i], sel) != 0) keep[m++] = list[i]; else XtFree(list[i]); WriteLines("servers", keep, m); } XtFree(sel); FillFavourites(); FilePath("servers", path, sizeof path); } static void BrowseServers(Widget w, XtPointer client, XtPointer call) { (void)w; (void)client; (void)call; XtPopdown(conn.shell); if (conn.rec != NULL) _McdeNetShow(conn.rec, True); } static void Chosen(Widget w, XtPointer client, XtPointer call) { XmListCallbackStruct *c = (XmListCallbackStruct *) call; char *text = NULL; (void)w; (void)client; if (XmStringGetLtoR(c->item, XmFONTLIST_DEFAULT_TAG, &text)) { XmTextFieldSetString(conn.address, text); XtFree(text); } XtSetSensitive(conn.remove, True); } static void RecentChosen(Widget w, XtPointer client, XtPointer call) { char *text = (char *) client; (void)w; (void)call; if (text == NULL) return; XmTextFieldSetString(conn.address, text); ConnectNow(NULL, NULL, NULL); } static void Build(void) { Widget form, row, lab; XmString s; conn.shell = XtVaCreatePopupShell("mcdeConnect", topLevelShellWidgetClass, conn.rec->shell, XmNtitle, "Connect to Server", XmNallowShellResize, True, NULL); form = XtVaCreateManagedWidget("connectForm", xmFormWidgetClass, conn.shell, XmNmarginWidth, 10, XmNmarginHeight, 10, XmNwidth, 490, XmNheight, 250, NULL); conn.form = form; lab = XtVaCreateManagedWidget("addressLabel", xmLabelWidgetClass, form, XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, NULL); s = XmStringCreateLocalized((char *) "Server Address:"); XtVaSetValues(lab, XmNlabelString, s, NULL); XmStringFree(s); conn.address = XtVaCreateManagedWidget("address", xmTextFieldWidgetClass, form, XmNcolumns, 30, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, lab, XmNtopOffset, 4, XmNleftAttachment, XmATTACH_FORM, NULL); { Widget plus = XtVaCreateManagedWidget("plus", xmPushButtonWidgetClass, form, XmNlabelString, XmStringCreateLocalized((char *) "+"), XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, conn.address, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, conn.address, XmNleftOffset, 6, NULL); XtAddCallback(plus, XmNactivateCallback, AddFavourite, NULL); } /* the history: an option menu of the last servers, made on opening * (a cascade button outside a menu bar did not post its menu, * measured 2026-11-14) */ conn.history = XmCreateOptionMenu(form, "history", NULL, 0); XtVaSetValues(conn.history, XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, conn.address, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, conn.address, XmNleftOffset, 40, NULL); XtManageChild(conn.history); { Widget pd = XmCreatePulldownMenu(conn.history, "historyMenu", NULL, 0); XtVaSetValues(conn.history, XmNsubMenuId, pd, NULL); XtAddCallback(pd, XmNmapCallback, FillHistory, NULL); { XmString s = XmStringCreateLocalized((char *) "Recent"); XtVaSetValues(conn.history, XmNlabelString, s, NULL); XmStringFree(s); } } conn.error = XtVaCreateManagedWidget("error", xmLabelWidgetClass, form, XmNalignment, XmALIGNMENT_BEGINNING, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, conn.address, XmNtopOffset, 8, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); lab = XtVaCreateManagedWidget("favouritesLabel", xmLabelWidgetClass, form, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, conn.error, XmNtopOffset, 8, XmNleftAttachment, XmATTACH_FORM, NULL); s = XmStringCreateLocalized((char *) "Favorite Servers:"); XtVaSetValues(lab, XmNlabelString, s, NULL); XmStringFree(s); conn.list = XmCreateScrolledList(form, "favourites", NULL, 0); XtVaSetValues(conn.list, XmNvisibleItemCount, 5, XmNselectionPolicy, XmBROWSE_SELECT, NULL); XtVaSetValues(XtParent(conn.list), XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, lab, XmNtopOffset, 4, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, NULL, NULL); XtAddCallback(conn.list, XmNbrowseSelectionCallback, Chosen, NULL); XtManageChild(conn.list); row = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); XtVaSetValues(XtParent(conn.list), XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, row, XmNbottomOffset, 8, NULL); conn.remove = XtVaCreateManagedWidget("remove", xmPushButtonWidgetClass, row, XmNlabelString, XmStringCreateLocalized((char *) "Remove"), XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, NULL); XtAddCallback(conn.remove, XmNactivateCallback, RemoveFavourite, NULL); XtSetSensitive(conn.remove, False); { /* Browse left of Connect, as the reference has them */ Widget connectw = XtVaCreateManagedWidget("connect", xmPushButtonWidgetClass, row, XmNlabelString, XmStringCreateLocalized((char *) "Connect"), XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); Widget browse = XtVaCreateManagedWidget("browse", xmPushButtonWidgetClass, row, XmNlabelString, XmStringCreateLocalized((char *) "Browse"), XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, connectw, XmNrightOffset, 8, NULL); XtAddCallback(browse, XmNactivateCallback, BrowseServers, NULL); XtAddCallback(connectw, XmNactivateCallback, ConnectNow, NULL); } XtAddCallback(conn.address, XmNactivateCallback, ConnectNow, NULL); } void _McdeConnectShow(FileMgrRec *rec) { if (rec == NULL) return; conn.rec = rec; if (conn.shell == NULL) { Build(); XtVaSetValues(conn.shell, XmNtransientFor, rec->shell, NULL); } SetError(""); FillFavourites(); { char *recent[RECENT_MAX]; int n = ReadLines("servers.recent", recent, RecentServers()); XmTextFieldSetString(conn.address, n > 0 ? recent[0] : "sftp://"); FreeLines(recent, n); } XtPopup(conn.shell, XtGrabNone); XmProcessTraversal(conn.address, XmTRAVERSE_CURRENT); XmTextFieldSetSelection(conn.address, 0, XmTextFieldGetLastPosition(conn.address), CurrentTime); }