Files
mcde/programs/dtfile/Connect.c
T
lotherk dc8dfd2cc7 MCDE dtsystemsettings: Appearance, the four groups and the fonts (t0099)
The pane is the reference's form now: Appearance (a menu of the palettes with
the chosen palette's eight colours as a strip under it, then "In use:"),
Highlight Color (the eight colours of the active palette, written as
*highlightColor and *selectColor for programs started later), the scroll
bars, the number of recent items, font smoothing -- and the fonts, which
lose their own pane (the "fonts" id still opens Appearance, the entry is
gone from the overview).

What the pane writes is what the session reads:

  *<screen>*ColorPalette, *highlightColor, *selectColor   _DtAddToResource
  Dtwm*mcdeTitleBarDoubleClick: none|minimize             then dtwm restarts
  ~/.dt/recent.conf: applications=N, documents=N, servers=N
  ~/.config/fontconfig/conf.d/60-mcde-antialias.conf      antialias=false

dtwm gained the title-bar double click: a new resource (WmResNames.h,
WmGlobal.h, WmResource.c, default "none") and, in CheckButtonPressBuiltin
(WmCEvent.c), the double-click branch on the title -- F_Minimize or
F_Maximize instead of the move.  dtbar's Recent Items and Connect to
Server's history now read their counts from ~/.dt/recent.conf
(ItemsPush/FillRecent, ReadLines/RecentAdd).

Two things the task itself said to drop are dropped, with the reason in the
pane: Motif has no separate arrow buttons at both ends of a scroll bar and no
smooth scrolling (an animation), and a click in a Motif scroll bar pages
while the middle button jumps to the place.

Font smoothing is a fontconfig rule, not *Xft.antialias: measured here, Xft
ignores that X resource (205 grey levels with and without it), while a
<match target="font"> antialias=false rule gives 2.  The rule reaches GTK and
Qt as well (one look).

Measured under Xvfb with xdotool:

  - "Off" + Apply -> the file is written and an Xft render test in the same
    session drops from 205 distinct grey levels to 2; with the size limit 20
    the same test (size 12) gives 2, with 4 it gives 205 -- the rule holds
    only up to the size.
  - the title-bar box + Apply writes Dtwm*mcdeTitleBarDoubleClick: minimize,
    dtwm restarts, and a double click on dtpad's title bar leaves its window
    "Map State: IsUnMapped" (IsViewable before).
  - a palette choice writes *0*ColorPalette: Camouflage.dp (and dtthemesync's
    colours), the strip and the Highlight menu follow it.
  - Documents=5 with eight entries in ~/.dt/recent shows five in the bar's
    Recent Items; Servers=0 leaves the Connect to Server field at "sftp://",
    Servers=10 prefills the stored address.

Pictures: docs/screenshots/leopard/System-Preferences-Appearance-mcde.png
(the four groups) and ...-Appearance-fonts-mcde.png (scrolled to the fonts).
make check: test_panes PASS.

Open: a highlight colour reaches programs started later, not the running
ones; "Applications" has no recent list to feed; the dtwm value takes effect
when the window manager restarts (the Apply button does it); the antialias
rule is read at program start; and the fonts list is six rows inside the
scrolled pane.
2026-11-14 09:59:02 +00:00

535 lines
14 KiB
C

/*
* Connect.c -- MCDE: the Connect to Server dialog (Connect.h)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Xm/CascadeB.h>
#include <Xm/Form.h>
#include <Xm/LabelG.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MenuShell.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/TextF.h>
#include <Xm/Xm.h>
#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(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);
}