Files
mcde/programs/dtfile/NetView.c
T
lotherk 3eddc96fbe dtfile: the network list asks again when the first browse is empty (t0305)
The empty computer list was not the layout: with MCDE_NET_DEBUG the browse in
the view reports n=3, an XmList that is managed with 3 items, 136x181 and
visibleItemCount 10, and clicking a host fills the right list (SFTP, SSH,
SMB).  The earlier empty window was the first browse after the start: it can
come back before avahi has resolved the services it found (only the "+"
lines, no "=" ones), and the view kept the empty result until Refresh.

Browse now asks once more after two seconds when it found no computer, and
Refresh resets that so a manual refresh may retry too.  The debug line (n,
list and item count, geometry) stays behind MCDE_NET_DEBUG.

Measured (Xvfb :78, avahi-publish of mcdetest on kitt.local, HOME with an
empty Network folder, ttsession): list "N95, lotherk's MacBook Air, KITT",
note "3 computers found."; KITT selected shows Files (SFTP), Terminal (SSH),
SMB; Terminal (SSH) runs the dtterm stub with
	-title KITT -e ssh -- kitt.local
-- from the network view, no shell, the host after "--".  Screenshots
/tmp/opencode/netview-window.png and netview-ssh.png; make mcde-linux
MCDE_DIRS="install programs/dtfile" exit=0.
2026-11-16 13:33:42 +00:00

832 lines
24 KiB
C

/*
* NetView.c -- MCDE: the network in the file manager window
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* "Network" in the sidebar (Navigation.c) shows, in place of the work area
* (as the column view does, Columns.c):
*
* - at the top "Connect to Server:" with an address -- sftp://host/path,
* nfs://host/path, afp://host/volume;
* - on the left the computers on the network that announce themselves
* (McdeSys, mcde_sys_net_browse: DNS-SD), with Refresh;
* - on the right what the chosen one offers: its files by SFTP, a
* terminal by SSH, its NFS exports, its SMB shares (listed; this
* computer cannot open them yet).
*
* Connecting mounts the share on ~/Network/HOST[/NAME] (_McdeNetMount) and
* opens that folder in the window: the view goes back to the icons and
* dtfile goes there (_McdeNavGoTo). When the server wants a password, a
* dialog asks for it (the field shows stars) and the mount is tried again.
* A server that is gone keeps the mount tool busy for minutes: the mount
* runs as a job (McdeSys), a dialog "Connecting ..." with Cancel stands in
* front of the window, which goes on drawing, and the time-out is the
* resource Dtfile*connectTimeout (seconds, 20).
* Finding the computers takes a few seconds: it runs after the view is
* drawn.
*
* C99, POSIX and Motif (ext/src/mcde/AGENTS.md).
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include <Xm/Protocols.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/SelectioB.h>
#include <Xm/TextF.h>
#include <Mcde/McdeSys.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Navigation.h"
#include "Connect.h"
#define MAX_HOSTS 64
#define MAX_ITEMS 64
/* what the right side lists: an action on the chosen computer */
typedef enum { ITEM_SFTP, ITEM_SSH, ITEM_NFS, ITEM_SMB, ITEM_AFP } ItemKind;
static struct {
FileMgrRec *rec;
Widget form, address, hostList, itemList, note;
McdeSysNetHost hosts[MAX_HOSTS];
int nhosts, chosen;
int retried; /* the empty first browse was asked again */
ItemKind kind[MAX_ITEMS];
char item[MAX_ITEMS][256];
int nitems;
char pendingUrl[600], pendingMount[1100];
} nv;
/*
* A name as the computers announce it is UTF-8 ("Konrad\u2019s computer");
* CDE's interface fonts are ISO 8859-1 and show the typographic quote as
* three wrong characters (measured with a real announcement). The usual
* punctuation becomes its ASCII form; the rest is left as it is.
*/
static void
Plain(const char *in, char *out, size_t len)
{
static const struct { const char *utf8, *ascii; } map[] = {
{ "\xe2\x80\x98", "'" }, { "\xe2\x80\x99", "'" },
{ "\xe2\x80\x9c", "\"" }, { "\xe2\x80\x9d", "\"" },
{ "\xe2\x80\x93", "-" }, { "\xe2\x80\x94", "-" },
{ "\xe2\x80\xa6", "..." }, { "\xc2\xa0", " " },
};
size_t o = 0, i, n;
while (*in != '\0' && o + 4 < len) {
for (i = 0; i < sizeof map / sizeof map[0]; i++) {
n = strlen(map[i].utf8);
if (strncmp(in, map[i].utf8, n) == 0)
break;
}
if (i < sizeof map / sizeof map[0]) {
n = strlen(map[i].utf8);
o += (size_t) snprintf(out + o, len - o, "%s",
map[i].ascii);
in += n;
} else
out[o++] = *in++;
}
out[o] = '\0';
}
static void
Note(const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(nv.note, XmNlabelString, s, NULL);
XmStringFree(s);
XmUpdateDisplay(nv.note);
}
static void
AddItem(ItemKind k, const char *text, const char *label)
{
XmString s;
if (nv.nitems >= MAX_ITEMS)
return;
nv.kind[nv.nitems] = k;
snprintf(nv.item[nv.nitems], sizeof nv.item[0], "%s", text);
nv.nitems++;
{
char plain[512];
Plain(label, plain, sizeof plain);
s = XmStringCreateLocalized(plain);
}
XmListAddItemUnselected(nv.itemList, s, 0);
XmStringFree(s);
}
/* ---- connecting --------------------------------------------------------- */
/* ~/Network/HOST[/NAME], made if need be */
static void
MountPoint(const char *host, const char *name, char *buf, size_t len)
{
const char *home = getenv("HOME");
char sub[300];
size_t i;
snprintf(buf, len, "%s/Network", home != NULL ? home : "/tmp");
(void) mkdir(buf, 0700);
snprintf(buf + strlen(buf), len - strlen(buf), "/%s", host);
(void) mkdir(buf, 0700);
if (name != NULL && name[0] != '\0') {
/* "/export/home" -> "export-home" */
snprintf(sub, sizeof sub, "%s", name[0] == '/' ? name + 1 : name);
for (i = 0; sub[i] != '\0'; i++)
if (sub[i] == '/')
sub[i] = '-';
snprintf(buf + strlen(buf), len - strlen(buf), "/%s",
sub[0] != '\0' ? sub : "root");
(void) mkdir(buf, 0700);
}
}
/* something is mounted on PATH already: its device differs from its
* parent's */
static Boolean
Mounted(const char *path)
{
char parent[1100];
struct stat a, b;
char *slash;
snprintf(parent, sizeof parent, "%s", path);
if ((slash = strrchr(parent, '/')) == NULL || slash == parent)
return False;
*slash = '\0';
return stat(path, &a) == 0 && stat(parent, &b) == 0 &&
a.st_dev != b.st_dev;
}
/* ---- the mount, without freezing the window ------------------------------ */
#define CONNECT_SECONDS 20
#define CONNECT_SECONDS_MIN 3
#define CONNECT_SECONDS_MAX 600
typedef struct {
Boolean done; /* leave the loop */
Boolean ended; /* the tool has ended: its answer is there */
Boolean cancelled; /* the user said Cancel */
XtInputId input;
XtIntervalId timer;
} MountWait;
static void
WaitInput(XtPointer client, int *source, XtInputId *id)
{
MountWait *w = (MountWait *) client;
(void) source;
XtRemoveInput(*id); /* it stays readable: once is enough */
w->input = 0;
w->ended = True;
w->done = True;
}
static void
WaitTimer(XtPointer client, XtIntervalId *id)
{
MountWait *w = (MountWait *) client;
(void) id;
w->timer = 0;
w->done = True;
}
static void
WaitCancel(Widget wid, XtPointer client, XtPointer call)
{
MountWait *w = (MountWait *) client;
(void) wid;
(void) call;
w->cancelled = True;
w->done = True;
}
/* Dtfile*connectTimeout, in seconds; a value that is no number or lies
* outside 3 to 600 is the default (resources: the pattern of File.c) */
static int
ConnectSeconds(Widget w)
{
Display *dpy = XtDisplay(w);
String name, cls;
char nm[128], cl[128], *type, *end;
XrmValue v;
long n;
XtGetApplicationNameAndClass(dpy, &name, &cls);
(void) snprintf(nm, sizeof nm, "%s.connectTimeout", name);
(void) snprintf(cl, sizeof cl, "%s.ConnectTimeout", cls);
if (!XrmGetResource(XrmGetDatabase(dpy), nm, cl, &type, &v) ||
v.addr == NULL)
return CONNECT_SECONDS;
n = strtol(v.addr, &end, 10);
if (end == v.addr || *end != '\0' || n < CONNECT_SECONDS_MIN ||
n > CONNECT_SECONDS_MAX)
return CONNECT_SECONDS;
return (int) n;
}
/*
* What mcde_sys_net_mount does, with the window drawing meanwhile: the mount
* is a job; a modal "Connecting ..." dialog with Cancel (also the close
* button and Escape) stands over PARENT, and the events are processed until
* the tool has ended (its descriptor is an Xt input: no timer polls it), the
* user cancelled or Dtfile*connectTimeout has passed. Returns what
* mcde_sys_net_mount returns, MCDE_SYS_OK or a failure with
* mcde_sys_error(). A job that is given up is ended, the tool with it.
*/
int
_McdeNetMount(Widget parent, const char *url, const char *mountpoint,
const char *password)
{
McdeSysJob *job;
MountWait w;
Widget dialog, shell;
XtAppContext app = XtWidgetToApplicationContext(parent);
Display *dpy = XtDisplay(parent);
XmString msg, title;
char text[700];
Atom del;
Arg args[8];
int n = 0, seconds = ConnectSeconds(parent);
if ((job = mcde_sys_net_mount_start(url, mountpoint, password)) == NULL)
return MCDE_SYS_EFAIL; /* mcde_sys_error() says why */
memset(&w, 0, sizeof w);
(void) snprintf(text, sizeof text, "Connecting to %s ...", url);
msg = XmStringCreateLocalized(text);
title = XmStringCreateLocalized((char *) "Connecting");
XtSetArg(args[n], XmNmessageString, msg); n++;
XtSetArg(args[n], XmNdialogTitle, title); n++;
XtSetArg(args[n], XmNdialogStyle, XmDIALOG_PRIMARY_APPLICATION_MODAL); n++;
XtSetArg(args[n], XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON); n++;
XtSetArg(args[n], XmNautoUnmanage, False); n++;
dialog = XmCreateWorkingDialog(parent, (char *) "connecting", args, n);
XmStringFree(msg);
XmStringFree(title);
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_OK_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
XtAddCallback(dialog, XmNcancelCallback, WaitCancel, &w);
shell = XtParent(dialog);
/* the close button of the window manager is Cancel, not a destroy */
XtVaSetValues(shell, XmNdeleteResponse, XmDO_NOTHING, NULL);
del = XmInternAtom(dpy, (char *) "WM_DELETE_WINDOW", False);
XmAddWMProtocolCallback(shell, del, WaitCancel, &w);
w.input = XtAppAddInput(app, mcde_sys_job_fd(job),
(XtPointer) XtInputReadMask, WaitInput, &w);
w.timer = XtAppAddTimeOut(app, (unsigned long) seconds * 1000UL,
WaitTimer, &w);
XtManageChild(dialog);
while (!w.done)
XtAppProcessEvent(app, XtIMAll);
if (w.input != 0)
XtRemoveInput(w.input);
if (w.timer != 0)
XtRemoveTimeOut(w.timer);
XtUnmanageChild(dialog);
XtDestroyWidget(shell);
if (w.ended)
return mcde_sys_job_finish(job);
if (w.cancelled)
return mcde_sys_job_cancel(job, "Cancelled.");
(void) snprintf(text, sizeof text, "The server did not answer within "
"%d seconds.", seconds);
return mcde_sys_job_cancel(job, text);
}
static void Open(const char *mountpoint);
static void AskPassword(void);
static void
Connect(const char *url, const char *mountpoint, const char *password)
{
char text[700];
if (Mounted(mountpoint)) {
Open(mountpoint);
return;
}
snprintf(text, sizeof text, "Connecting to %s ...", url);
Note(text);
if (_McdeNetMount(nv.rec->shell, url, mountpoint, password) ==
MCDE_SYS_OK && Mounted(mountpoint)) {
Note("");
Open(mountpoint);
return;
}
/* SFTP without a key, AFP: the server wants a password */
if (password == NULL && (strncmp(url, "sftp://", 7) == 0 ||
strncmp(url, "afp://", 6) == 0)) {
snprintf(nv.pendingUrl, sizeof nv.pendingUrl, "%s", url);
snprintf(nv.pendingMount, sizeof nv.pendingMount, "%s",
mountpoint);
AskPassword();
return;
}
snprintf(text, sizeof text, "Could not connect: %s", mcde_sys_error());
Note(text);
}
/* the mounted folder in this window, in the icon view */
static void
Open(const char *mountpoint)
{
FileMgrRec *rec = nv.rec;
_McdeNetShow(rec, False);
_McdeNavGoTo(rec, mountpoint);
}
/* ---- the password ------------------------------------------------------- */
static char secret[128];
static size_t secretLen;
/* the field shows a star for every character; the text is kept here */
static void
SecretVerify(Widget w, XtPointer data, XtPointer call)
{
XmTextVerifyCallbackStruct *cb = (XmTextVerifyCallbackStruct *) call;
size_t from = (size_t) cb->startPos, to = (size_t) cb->endPos;
size_t add = cb->text != NULL ? (size_t) cb->text->length : 0, i;
(void) w;
(void) data;
if (to > secretLen)
to = secretLen;
if (from > to || secretLen - (to - from) + add >= sizeof secret) {
cb->doit = False;
return;
}
memmove(secret + from + add, secret + to, secretLen - to);
if (add > 0)
memcpy(secret + from, cb->text->ptr, add);
secretLen = secretLen - (to - from) + add;
secret[secretLen] = '\0';
for (i = 0; i < add; i++)
cb->text->ptr[i] = '*';
}
static void
PasswordDone(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call;
(void) data;
if (cb->reason == XmCR_OK)
Connect(nv.pendingUrl, nv.pendingMount, secret);
else
Note("");
memset(secret, 0, sizeof secret);
secretLen = 0;
XtDestroyWidget(XtParent(w));
}
static void
AskPassword(void)
{
Widget d, field;
XmString msg;
char text[700];
memset(secret, 0, sizeof secret);
secretLen = 0;
snprintf(text, sizeof text, "The password for %s:", nv.pendingUrl);
msg = XmStringCreateLocalized(text);
d = XmCreatePromptDialog(nv.form, "mcdeNetPassword", NULL, 0);
{
XmString title = XmStringCreateLocalized("Connect to Server");
XtVaSetValues(d, XmNselectionLabelString, msg,
XmNdialogTitle, title, NULL);
XmStringFree(title);
}
XmStringFree(msg);
field = XmSelectionBoxGetChild(d, XmDIALOG_TEXT);
XtAddCallback(field, XmNmodifyVerifyCallback, SecretVerify, NULL);
XtUnmanageChild(XmSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, PasswordDone, NULL);
XtAddCallback(d, XmNcancelCallback, PasswordDone, NULL);
XtManageChild(d);
}
/* ---- the computers ------------------------------------------------------ */
static void
HostChosen(Widget w, XtPointer client, XtPointer call)
{
XmListCallbackStruct *cb = (XmListCallbackStruct *) call;
static char names[MAX_ITEMS][256];
McdeSysNetHost *h;
char label[400];
int i, n;
(void) w;
(void) client;
nv.chosen = cb->item_position - 1;
XmListDeleteAllItems(nv.itemList);
nv.nitems = 0;
if (nv.chosen < 0 || nv.chosen >= nv.nhosts)
return;
h = &nv.hosts[nv.chosen];
if (h->kinds & MCDE_NET_HAS_SSH) {
AddItem(ITEM_SFTP, "", "Files (SFTP)");
AddItem(ITEM_SSH, "", "Terminal (SSH)");
}
if (h->kinds & MCDE_NET_HAS_AFP)
AddItem(ITEM_AFP, "", "Files (AFP) -- the volume by address");
if (h->kinds & MCDE_NET_HAS_NFS) {
Note("Asking for its NFS folders ...");
n = mcde_sys_net_shares(h->host, MCDE_NET_HAS_NFS, names,
MAX_ITEMS);
for (i = 0; i < n; i++) {
snprintf(label, sizeof label, "NFS: %s", names[i]);
AddItem(ITEM_NFS, names[i], label);
}
}
if (h->kinds & MCDE_NET_HAS_SMB) {
Note("Asking for its SMB shares ...");
n = mcde_sys_net_shares(h->host, MCDE_NET_HAS_SMB, names,
MAX_ITEMS);
for (i = 0; i < n; i++) {
snprintf(label, sizeof label, "SMB: %s (cannot be opened "
"here yet)", names[i]);
AddItem(ITEM_SMB, names[i], label);
}
if (n < 0) {
snprintf(label, sizeof label, "SMB: shares not listed -- "
"%s", mcde_sys_error());
AddItem(ITEM_SMB, "", label);
}
}
Note(nv.nitems == 0 ? "It offers nothing this computer can open." : "");
}
/*
* dtterm with ssh to HOST, started without a shell: the name and the host
* come from the network (mDNS), and system() would hand them to sh, and a
* host beginning with "-" to ssh as an option. McdeSys keeps only host
* names GoodHost() accepts (netmount.c, mcde_sys_net_add); "--" ends ssh's
* options all the same. The terminal is detached by a second fork, so no
* child is left to be reaped (the pattern of daemon(3)).
*/
static void
SshTerminal(const char *name, const char *host)
{
pid_t pid;
if ((pid = fork()) == -1)
return;
if (pid == 0) {
if (fork() == 0) {
(void) setsid();
execlp("dtterm", "dtterm", "-title", name, "-e",
"ssh", "--", host, (char *) NULL);
}
_exit(0);
}
(void) waitpid(pid, NULL, 0);
}
static void
ItemOpen(Widget w, XtPointer client, XtPointer call)
{
XmListCallbackStruct *cb = (XmListCallbackStruct *) call;
int i = cb->item_position - 1;
McdeSysNetHost *h;
char url[600], mp[1100];
(void) w;
(void) client;
if (i < 0 || i >= nv.nitems || nv.chosen < 0 || nv.chosen >= nv.nhosts)
return;
h = &nv.hosts[nv.chosen];
switch (nv.kind[i]) {
case ITEM_SFTP:
snprintf(url, sizeof url, "sftp://%s", h->host);
MountPoint(h->host, NULL, mp, sizeof mp);
Connect(url, mp, NULL);
break;
case ITEM_SSH:
/* a terminal of CDE's with ssh in it */
SshTerminal(h->name, h->host);
break;
case ITEM_NFS:
snprintf(url, sizeof url, "nfs://%s%s", h->host, nv.item[i]);
MountPoint(h->host, nv.item[i], mp, sizeof mp);
Connect(url, mp, NULL);
break;
case ITEM_AFP:
snprintf(url, sizeof url, "afp://%s/", h->host);
XmTextFieldSetString(nv.address, url);
Note("Add the volume to the address and press Connect.");
break;
case ITEM_SMB:
Note("SMB shares can be listed, not opened, on this computer "
"yet.");
break;
}
}
static void
Browse(XtPointer client, XtIntervalId *id)
{
XmString s;
char label[500];
int i;
(void) client;
(void) id;
if (nv.form == NULL || !XtIsManaged(nv.form)) {
nv.retried = 0;
return;
}
Note("Looking for computers on the network ...");
XmListDeleteAllItems(nv.hostList);
XmListDeleteAllItems(nv.itemList);
nv.nitems = 0;
nv.chosen = -1;
nv.nhosts = mcde_sys_net_browse(nv.hosts, MAX_HOSTS, 3000);
if (nv.nhosts < 0) {
nv.nhosts = 0;
Note(mcde_sys_error());
return;
}
/* A browse right after the session starts can come back before avahi has
* resolved the services it found (only the "+" lines, no "=" ones): the
* list would stay empty until Refresh. Ask once more after two seconds. */
if (nv.nhosts == 0 && !nv.retried) {
nv.retried = 1;
XtAppAddTimeOut(XtWidgetToApplicationContext(nv.form), 2000,
Browse, NULL);
return;
}
nv.retried = 0;
for (i = 0; i < nv.nhosts; i++) {
Plain(nv.hosts[i].name, label, sizeof label);
s = XmStringCreateLocalized(label);
XmListAddItemUnselected(nv.hostList, s, 0);
XmStringFree(s);
}
snprintf(label, sizeof label, nv.nhosts == 1 ? "%d computer found." :
"%d computers found.", nv.nhosts);
Note(label);
if (getenv("MCDE_NET_DEBUG") != NULL) {
XtArgVal items = 0;
Dimension w = 0, h = 0;
int vis = -1;
XtVaGetValues(nv.hostList, XmNitemCount, &items,
XmNwidth, &w, XmNheight, &h,
XmNvisibleItemCount, &vis, NULL);
fprintf(stderr, "netview: n=%d list=%p managed=%d items=%ld "
"%ux%u visible=%d\n", nv.nhosts, (void *) nv.hostList,
XtIsManaged(nv.hostList), (long) items, (unsigned) w,
(unsigned) h, vis);
fprintf(stderr, "netview: form=%p managed=%d sw=%p managed=%d\n",
(void *) nv.form, XtIsManaged(nv.form),
(void *) XtParent(nv.hostList),
XtIsManaged(XtParent(nv.hostList)));
}
}
static void
RefreshCB(Widget w, XtPointer client, XtPointer call)
{
(void) w;
(void) client;
(void) call;
nv.retried = 0;
Browse(NULL, NULL);
}
/* "Connect to Server": the address as written */
static void
ConnectCB(Widget w, XtPointer client, XtPointer call)
{
char *url = XmTextFieldGetString(nv.address), host[256], mp[1100];
const char *p, *end;
const char *path;
(void) w;
(void) client;
(void) call;
if (url == NULL || (p = strstr(url, "://")) == NULL) {
Note("An address looks like sftp://host/folder, nfs://host/path "
"or afp://host/volume.");
XtFree(url);
return;
}
p += 3;
if (strchr(p, '@') != NULL && (strchr(p, '/') == NULL ||
strchr(p, '@') < strchr(p, '/')))
p = strchr(p, '@') + 1;
end = strchr(p, '/');
snprintf(host, sizeof host, "%.*s",
(int) (end != NULL ? (size_t) (end - p) : strlen(p)), p);
path = end != NULL ? end : "";
MountPoint(host, path, mp, sizeof mp);
Connect(url, mp, NULL);
XtFree(url);
}
/* ---- the view ----------------------------------------------------------- */
void
_McdeNetCreate(FileMgrRec *rec, Widget parent, Widget work)
{
Widget top, label, b;
XmString s;
Arg a[10];
int n;
if (nv.rec != NULL) /* one network view: in the first window */
return;
nv.rec = rec;
n = 0;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, work); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(a[n], XmNleftWidget, work); n++;
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_FORM); n++;
nv.form = XmCreateForm(parent, "mcdeNetwork", a, n);
rec->mcde_network = nv.form;
top = XtVaCreateManagedWidget("connect", xmRowColumnWidgetClass,
nv.form, XmNorientation, XmHORIZONTAL,
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
s = XmStringCreateLocalized("Connect to Server:");
label = XtVaCreateManagedWidget("label", xmLabelWidgetClass, top,
XmNlabelString, s, NULL);
XmStringFree(s);
(void) label;
nv.address = XtVaCreateManagedWidget("address", xmTextFieldWidgetClass,
top, XmNcolumns, 34, NULL);
XtAddCallback(nv.address, XmNactivateCallback, ConnectCB, NULL);
s = XmStringCreateLocalized("Connect");
b = XtVaCreateManagedWidget("connectButton", xmPushButtonWidgetClass,
top, XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(b, XmNactivateCallback, ConnectCB, NULL);
s = XmStringCreateLocalized("Refresh");
b = XtVaCreateManagedWidget("refresh", xmPushButtonWidgetClass, top,
XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(b, XmNactivateCallback, RefreshCB, NULL);
nv.note = XtVaCreateManagedWidget("note", xmLabelWidgetClass, nv.form,
XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
Note("");
n = 0;
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, top); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNbottomWidget, nv.note); n++;
nv.hostList = XmCreateScrolledList(nv.form, "computers", a, n);
XtVaSetValues(XtParent(nv.hostList), XmNwidth, 230, NULL);
XtManageChild(nv.hostList);
XtAddCallback(nv.hostList, XmNbrowseSelectionCallback, HostChosen,
NULL);
n = 0;
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, top); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNleftWidget, XtParent(nv.hostList)); n++;
XtSetArg(a[n], XmNleftOffset, 8); n++;
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNbottomWidget, nv.note); n++;
nv.itemList = XmCreateScrolledList(nv.form, "offers", a, n);
XtManageChild(nv.itemList);
XtAddCallback(nv.itemList, XmNdefaultActionCallback, ItemOpen, NULL);
}
/* the network on (in place of the work area and the columns) or off */
void
_McdeNetShow(FileMgrRec *rec, Boolean on)
{
Widget other;
if (rec->mcde_network == NULL || rec->mcde_work == NULL)
return;
other = rec->mcde_columns_on && rec->mcde_columns != NULL ?
rec->mcde_columns : rec->mcde_work;
/* the form around keeps its size while one part replaces the other
* (Columns.c) */
XtVaSetValues(XtParent(rec->mcde_work), XmNresizePolicy, XmRESIZE_NONE,
NULL);
if (on) {
Dimension w = 0, h = 0;
if (XtIsManaged(rec->mcde_network))
return;
XtVaGetValues(other, XmNwidth, &w, XmNheight, &h, NULL);
XtVaSetValues(rec->mcde_network, XmNwidth, w, XmNheight, h,
XmNresizePolicy, XmRESIZE_NONE, NULL);
XtManageChild(rec->mcde_network);
XtUnmanageChild(other);
XtAppAddTimeOut(XtWidgetToApplicationContext(rec->mcde_network),
100, Browse, NULL);
} else if (XtIsManaged(rec->mcde_network)) {
XtManageChild(other);
XtUnmanageChild(rec->mcde_network);
}
}
/* Command+K: the Connect to Server dialog (Connect.c); "Browse" in it opens
* the network view. */
void
_McdeNetConnect(FileMgrRec *rec)
{
if (rec == NULL)
return;
_McdeConnectShow(rec);
}
/*
* The Connect to Server dialog's mount: URL as written (sftp://, nfs://,
* afp://), on ~/Network/HOST[/NAME] as the network view mounts it. 1 with
* MP (the mount point) and an empty ERR, 0 with ERR the reason.
*/
int
_McdeNetConnectAddress(Widget parent, const char *url, char *mp,
size_t mplen, char *err, size_t errlen)
{
const char *p, *end, *path;
char host[256];
if (err != NULL && errlen > 0)
err[0] = '\0';
if (url == NULL || (p = strstr(url, "://")) == NULL) {
if (err != NULL)
(void)snprintf(err, errlen, "An address looks like "
"sftp://host/folder, nfs://host/path or "
"afp://host/volume.");
return 0;
}
p += 3;
if (strchr(p, '@') != NULL && (strchr(p, '/') == NULL ||
strchr(p, '@') < strchr(p, '/')))
p = strchr(p, '@') + 1;
end = strchr(p, '/');
(void)snprintf(host, sizeof host, "%.*s", (int) (end != NULL ?
(size_t) (end - p) : strlen(p)), p);
if (host[0] == '\0') {
if (err != NULL)
(void)snprintf(err, errlen, "There is no server name "
"in the address.");
return 0;
}
path = end != NULL ? end : "";
MountPoint(host, path, mp, mplen);
if (Mounted(mp))
return 1;
if (_McdeNetMount(parent, url, mp, NULL) != MCDE_SYS_OK) {
if (err != NULL)
(void)snprintf(err, errlen, "Could not connect: %s",
mcde_sys_error());
return 0;
}
if (!Mounted(mp)) {
if (err != NULL)
(void)snprintf(err, errlen, "The server answered, but "
"%s is not there.", mp);
return 0;
}
return 1;
}