diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 290a8da7..75a07c18 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -395,6 +395,19 @@ int mcde_sys_process_command(pid_t pid, char *buf, size_t len); * marks the first kind "(not responding)". */ int mcde_sys_process_stopped(pid_t pid); +/* + * A file's comment and its lock, as the Info window of the file manager + * shows them. The comment is an extended attribute in the user namespace + * (user.mcde.comment on Linux, mcde.comment in EXTATTR_NAMESPACE_USER on + * NetBSD); an empty TEXT removes it; MCDE_SYS_ENOTSUP when the file system + * has no extended attributes. The lock is the immutable flag + * (FS_IMMUTABLE_FL on Linux, chflags(2) UF_IMMUTABLE on NetBSD). + */ +int mcde_sys_file_comment_get(const char *path, char *buf, size_t len); +int mcde_sys_file_comment_set(const char *path, const char *text); +int mcde_sys_file_locked_get(const char *path, int *locked); +int mcde_sys_file_locked_set(const char *path, int locked); + /* * The time zone: names as the tz database has them ("Europe/Lisbon"). * /etc/localtime is a symbolic link into /usr/share/zoneinfo on NetBSD and diff --git a/lib/McdeSys/common.c b/lib/McdeSys/common.c index 75e9ddca..a75487e6 100644 --- a/lib/McdeSys/common.c +++ b/lib/McdeSys/common.c @@ -482,3 +482,40 @@ mcde_sys_process_stopped(pid_t pid) return -1; return mcde_sys_process_stopped_backend(pid); } + +/* the comment and the lock of a file: the backends know the extended + * attribute and the immutable flag of their system */ +int +mcde_sys_file_comment_get(const char *path, char *buf, size_t len) +{ + if (path == NULL || buf == NULL || len < 2) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No file and no room for the " + "comment."); + buf[0] = '\0'; + return mcde_sys_file_comment_get_backend(path, buf, len); +} + +int +mcde_sys_file_comment_set(const char *path, const char *text) +{ + if (path == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No file for the comment."); + return mcde_sys_file_comment_set_backend(path, text != NULL ? text : ""); +} + +int +mcde_sys_file_locked_get(const char *path, int *locked) +{ + if (path == NULL || locked == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No file for the lock."); + *locked = 0; + return mcde_sys_file_locked_get_backend(path, locked); +} + +int +mcde_sys_file_locked_set(const char *path, int locked) +{ + if (path == NULL) + return mcde_sys_fail(MCDE_SYS_EINVAL, "No file for the lock."); + return mcde_sys_file_locked_set_backend(path, locked); +} diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 4d34bdcf..80ad2b73 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -20,6 +20,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -889,6 +894,93 @@ mcde_sys_process_stopped_backend(pid_t pid) } } +/* ---- a file's comment: the extended attribute user.mcde.comment + * (xattr(7)); ENOTSUP when the file system has none, ENODATA when there is + * no comment yet */ +int +mcde_sys_file_comment_get_backend(const char *path, char *buf, size_t len) +{ + ssize_t n; + + buf[0] = '\0'; + n = getxattr(path, "user.mcde.comment", buf, len - 1); + if (n < 0) { + if (errno == ENODATA) + return MCDE_SYS_OK; + if (errno == ENOTSUP || errno == EOPNOTSUPP) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This file system " + "keeps no comments."); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the comment: " + "%s.", strerror(errno)); + } + buf[n] = '\0'; + return MCDE_SYS_OK; +} + +int +mcde_sys_file_comment_set_backend(const char *path, const char *text) +{ + if (text[0] == '\0') { + if (removexattr(path, "user.mcde.comment") != 0 && + errno != ENODATA && errno != ENOTSUP) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot remove the " + "comment: %s.", strerror(errno)); + return MCDE_SYS_OK; + } + if (setxattr(path, "user.mcde.comment", text, strlen(text), 0) != 0) { + if (errno == ENOTSUP || errno == EOPNOTSUPP) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This file system " + "keeps no comments."); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write the comment: " + "%s.", strerror(errno)); + } + return MCDE_SYS_OK; +} + +/* ---- the immutable flag (chattr(1) i): FS_IOC_GETFLAGS/SETFLAGS */ +int +mcde_sys_file_locked_get_backend(const char *path, int *locked) +{ + int fd, flags = 0; + + *locked = 0; + if ((fd = open(path, O_RDONLY)) < 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot open %s: %s.", path, + strerror(errno)); + if (ioctl(fd, FS_IOC_GETFLAGS, &flags) == 0) + *locked = (flags & FS_IMMUTABLE_FL) != 0; + close(fd); + return MCDE_SYS_OK; +} + +int +mcde_sys_file_locked_set_backend(const char *path, int locked) +{ + int fd, flags = 0; + + if ((fd = open(path, O_RDONLY)) < 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot open %s: %s.", path, + strerror(errno)); + if (ioctl(fd, FS_IOC_GETFLAGS, &flags) != 0) { + close(fd); + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This file system has no " + "lock."); + } + if (locked) + flags |= FS_IMMUTABLE_FL; + else + flags &= ~FS_IMMUTABLE_FL; + if (ioctl(fd, FS_IOC_SETFLAGS, &flags) != 0) { + int err = errno; + + close(fd); + return mcde_sys_fail(MCDE_SYS_EPERM, "Cannot %s the lock: %s.", + locked ? "set" : "clear", strerror(err)); + } + close(fd); + return MCDE_SYS_OK; +} + /* ---- sharing: systemd's units, switched with systemctl(1); Debian's names * (ssh, smbd and nmbd, nfs-server, netatalk). AppleTalk has no server on * Linux distributions any more. */ diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index 06a04d95..bb4440b1 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include #include @@ -1282,6 +1284,79 @@ mcde_sys_process_stopped_backend(pid_t pid) return 0; } +/* ---- a file's comment: EXTATTR_NAMESPACE_USER, "mcde.comment" */ +int +mcde_sys_file_comment_get_backend(const char *path, char *buf, size_t len) +{ + ssize_t n; + + buf[0] = '\0'; + n = extattr_get_file(path, EXTATTR_NAMESPACE_USER, "mcde.comment", buf, + len - 1); + if (n < 0) { + if (errno == ENOATTR) + return MCDE_SYS_OK; + if (errno == ENOTSUP || errno == EOPNOTSUPP) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This file system " + "keeps no comments."); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the comment: " + "%s.", strerror(errno)); + } + buf[n] = '\0'; + return MCDE_SYS_OK; +} + +int +mcde_sys_file_comment_set_backend(const char *path, const char *text) +{ + if (text[0] == '\0') { + if (extattr_delete_file(path, EXTATTR_NAMESPACE_USER, + "mcde.comment") != 0 && errno != ENOATTR && + errno != ENOTSUP) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot remove the " + "comment: %s.", strerror(errno)); + return MCDE_SYS_OK; + } + if (extattr_set_file(path, EXTATTR_NAMESPACE_USER, "mcde.comment", text, + strlen(text)) < 0) { + if (errno == ENOTSUP || errno == EOPNOTSUPP) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This file system " + "keeps no comments."); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write the comment: " + "%s.", strerror(errno)); + } + return MCDE_SYS_OK; +} + +/* ---- the immutable flag: chflags(2), UF_IMMUTABLE */ +int +mcde_sys_file_locked_get_backend(const char *path, int *locked) +{ + struct stat st; + + *locked = 0; + if (stat(path, &st) != 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read %s: %s.", path, + strerror(errno)); + *locked = (st.st_flags & UF_IMMUTABLE) != 0; + return MCDE_SYS_OK; +} + +int +mcde_sys_file_locked_set_backend(const char *path, int locked) +{ + struct stat st; + + if (stat(path, &st) != 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read %s: %s.", path, + strerror(errno)); + if (chflags(path, locked ? (st.st_flags | UF_IMMUTABLE) : + (st.st_flags & ~UF_IMMUTABLE)) != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Cannot %s the lock: %s.", + locked ? "set" : "clear", strerror(errno)); + return MCDE_SYS_OK; +} + /* ---- sharing: rc.conf and the rc.d scripts (rc.subr(8)); the servers of * pkgsrc bring their scripts in share/examples/rc.d, copied to /etc/rc.d * when first switched on, as PKG_RCD_SCRIPTS=YES would diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 9a4ff364..8a4a61dd 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -58,6 +58,13 @@ int mcde_sys_process_args(pid_t pid, char *buf, size_t len); /* the backend: 1 when PID is stopped or a zombie, 0 when it runs, -1 when * it cannot be asked (no such process, no permission) */ int mcde_sys_process_stopped_backend(pid_t pid); +/* the backend: a file's comment (extended attribute) and its lock (the + * immutable flag); MCDE_SYS_ENOTSUP when the file system cannot */ +int mcde_sys_file_comment_get_backend(const char *path, char *buf, + size_t len); +int mcde_sys_file_comment_set_backend(const char *path, const char *text); +int mcde_sys_file_locked_get_backend(const char *path, int *locked); +int mcde_sys_file_locked_set_backend(const char *path, int locked); /* the backend, for share.c: where the servers keep their configuration * (NULL: not on this system), and how an exports(5) line reads */ const char *mcde_sys_exports_file(void); diff --git a/programs/dtfile/Dtfile b/programs/dtfile/Dtfile index 45817fa0..9b111b76 100644 --- a/programs/dtfile/Dtfile +++ b/programs/dtfile/Dtfile @@ -48,8 +48,8 @@ Dtfile*viewMenu.showHiddenObjects.acceleratorText: %|nls-11-#Cmd+S#| Dtfile*viewMenu.showHiddenObjects.accelerator: %|nls-12-#Mod4s#| Dtfile*actionMenu.copy.acceleratorText: %|nls-15-#Cmd+C#| Dtfile*actionMenu.copy.accelerator: %|nls-16-#Mod4c#| -Dtfile*actionMenu.permissions.acceleratorText: %|nls-17-#Cmd+I#| -Dtfile*actionMenu.permissions.accelerator: %|nls-18-#Mod4i#| +Dtfile*fileMenu.getInfo.acceleratorText: Cmd+I +Dtfile*fileMenu.getInfo.accelerator: Mod4i Dtfile*selectAll.acceleratorText: %|nls-19-#Ctrl+/#| Dtfile*selectAll.accelerator: %|nls-20-#Ctrlslash#| Dtfile*deselectAll.acceleratorText: %|nls-21-#Ctrl+\\#| diff --git a/programs/dtfile/FileMgr.c b/programs/dtfile/FileMgr.c index 4803c62b..220add50 100644 --- a/programs/dtfile/FileMgr.c +++ b/programs/dtfile/FileMgr.c @@ -162,6 +162,7 @@ #include "Main.h" #include "Navigation.h" #include "Peek.h" +#include "Info.h" #include "Common.h" #include "Filter.h" #include "Find.h" @@ -531,6 +532,7 @@ Mod4k:McdeKey(@,connect)\n\ :osfPageDown:FMInput(@)\n\ :osfPageUp:FMInput(@)\n\ Shift Mod4Delete:McdeKey(@,emptytrash)\n\ +Mod4i:McdeKey(@,info)\n\ Mod1space:McdeKey(@,peekfull)\n\ space:McdeKey(@,peek)\n\ F1:DrawingAreaInput() ManagerGadgetHelp()\n\ @@ -2414,6 +2416,8 @@ McdeKey( sscanf( params[0], "%p", (void **) &fileMgrRec ); if( strcmp( params[1], "connect" ) == 0 ) _McdeNetConnect( fileMgrRec ); + else if( strcmp( params[1], "info" ) == 0 ) + _InfoShow( fileMgrRec ); else if( strcmp( params[1], "peek" ) == 0 ) _McdePeekToggle( fileMgrRec ); else if( strcmp( params[1], "peekfull" ) == 0 ) diff --git a/programs/dtfile/Info.c b/programs/dtfile/Info.c new file mode 100644 index 00000000..070afe00 --- /dev/null +++ b/programs/dtfile/Info.c @@ -0,0 +1,692 @@ +/* + * Info.c -- MCDE: the Info window of the file manager (Info.h) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * The reference's window: name and what it is at the top, then sections + * that open and close with a click -- General (kind, size, on disk, where, + * created, modified, locked), Permissions (owner, group, everyone: read and + * write, read only, none), Comment (an extended attribute, through + * McdeSys) and Preview (the miniature of McdeThumb). One window per file + * manager, so Cmd+I again replaces what it shows and the sections keep + * their state. + * + * 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 +#include + +#include "Encaps.h" +#include "SharedProcs.h" +#include "FileMgr.h" +#include "Info.h" + +#define MAX_WINDOWS 32 +#define PREVIEW_W 240 +#define PREVIEW_H 160 + +typedef struct { + FileMgrRec *rec; + Widget shell; + Widget form; + Widget box; + Widget icon; + Widget name; + Widget summary; + Widget genT, genF; + Widget kindV, sizeV, diskV, whereV, createdV, modifiedV; + Widget lockedT; + Widget permT, permF, ownerM, groupM, otherM; + Widget comT, comF, comment, save, comNote; + Widget prevT, prevF, prevImage; + Pixmap iconpm, prevpm; + char *path; +} Info; + +static Info infos[MAX_WINDOWS]; + +static void SectionToggled(Widget w, XtPointer client, XtPointer call); + +/* ---- small helpers ------------------------------------------------------ */ + +static FileMgrData * +DataOf(FileMgrRec *rec) +{ + DialogData *dd = _DtGetInstanceData((XtPointer) rec); + + return dd != NULL ? (FileMgrData *) dd->data : NULL; +} + +static Info * +InfoOf(FileMgrRec *rec, Boolean make) +{ + int i; + + for (i = 0; i < MAX_WINDOWS; i++) + if (infos[i].rec == rec) + return &infos[i]; + if (!make) + return NULL; + for (i = 0; i < MAX_WINDOWS; i++) + if (infos[i].rec == NULL) { + memset(&infos[i], 0, sizeof infos[i]); + infos[i].rec = rec; + infos[i].iconpm = infos[i].prevpm = XmUNSPECIFIED_PIXMAP; + return &infos[i]; + } + return NULL; +} + +static char * +Join(const char *dir, const char *name) +{ + size_t n = strlen(dir) + strlen(name) + 2; + char *p = XtMalloc((Cardinal) n); + + (void)snprintf(p, n, "%s%s%s", dir, + strcmp(dir, "/") == 0 ? "" : "/", name); + return p; +} + +static char * +ChosenPath(FileMgrData *data) +{ + FileViewData *fvd; + DirectorySet *ds; + + if (data == NULL || data->selected_file_count != 1 || + data->selection_list == NULL) + return NULL; + fvd = data->selection_list[0]; + if (fvd == NULL || fvd->file_data == NULL || + strcmp(fvd->file_data->file_name, "..") == 0 || + strcmp(fvd->file_data->file_name, ".") == 0) + return NULL; + ds = (DirectorySet *) fvd->directory_set; + if (ds == NULL || ds->name == NULL) + return NULL; + return Join(ds->name, fvd->file_data->file_name); +} + +static void +SetLabel(Widget w, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + + XtVaSetValues(w, XmNlabelString, s, NULL); + XmStringFree(s); +} + +static void +SizeText(off_t bytes, char *buf, size_t len) +{ + static const char *const units[] = { "bytes", "KB", "MB", "GB", "TB" }; + double v = (double) bytes; + int u = 0; + + while (v >= 1024.0 && u < 4) { + v /= 1024.0; + u++; + } + if (u == 0) + (void)snprintf(buf, len, "%lld bytes", (long long) bytes); + else + (void)snprintf(buf, len, "%.1f %s (%lld bytes)", v, units[u], + (long long) bytes); +} + +static const char * +KindText(const char *path, const struct stat *st, char *buf, size_t len) +{ + char *type, *label; + + if (S_ISDIR(st->st_mode)) + return "Folder"; + type = DtDtsFileToDataType((char *) path); + if (type == NULL) + return "File"; + label = DtDtsDataTypeToAttributeValue(type, "TYPE_LABEL", NULL); + (void)snprintf(buf, len, "%s", label != NULL ? label : type); + if (label != NULL) + DtDtsFreeAttributeValue(label); + DtDtsFreeDataType(type); + return buf; +} + +/* the icon of a file from CDE's data types (Columns.c, Peek.c) */ +static Pixmap +TypeIcon(Widget w, const char *path) +{ + char *type, *icon; + Pixmap pm = XmUNSPECIFIED_PIXMAP; + Pixel fg, bg; + size_t i; + static const char *const sizes[] = { ".l.pm", ".m.pm", ".l.bm" }; + + if (path == NULL || (type = DtDtsFileToDataType((char *) path)) == NULL) + return pm; + icon = DtDtsDataTypeToAttributeValue(type, DtDTS_DA_ICON, NULL); + if (icon != NULL) { + XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL); + for (i = 0; i < sizeof sizes / sizeof sizes[0] && + pm == XmUNSPECIFIED_PIXMAP; i++) { + char buf[256]; + + (void)snprintf(buf, sizeof buf, "%s%s", icon, sizes[i]); + pm = XmGetPixmap(XtScreen(w), buf, fg, bg); + if (pm == XmUNSPECIFIED_PIXMAP) { + (void)snprintf(buf, sizeof buf, + "%s/appconfig/icons/C/%s%s", + CDE_INSTALLATION_TOP, icon, sizes[i]); + pm = XmGetPixmap(XtScreen(w), buf, fg, bg); + } + } + DtDtsFreeAttributeValue(icon); + } + DtDtsFreeDataType(type); + return pm; +} + +/* ---- the sections ------------------------------------------------------- */ + +/* a row: LABEL at the left, the value widget to its right */ +static Widget +Row(Info *in, Widget form, const char *label, Widget above) +{ + Widget l; + XmString s = XmStringCreateLocalized((char *) label); + + (void)in; + l = XtVaCreateManagedWidget("rowLabel", xmLabelWidgetClass, form, + XmNlabelString, s, + XmNalignment, XmALIGNMENT_END, + XmNleftAttachment, XmATTACH_FORM, + XmNtopAttachment, above != NULL ? XmATTACH_WIDGET : XmATTACH_FORM, + XmNtopWidget, above, + XmNtopOffset, above != NULL ? 6 : 0, NULL); + XmStringFree(s); + return l; +} + +static Widget +Value(Info *in, Widget form, Widget label, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + Widget v; + + (void)in; + v = XtVaCreateManagedWidget("rowValue", xmLabelWidgetClass, form, + XmNlabelString, s, + XmNalignment, XmALIGNMENT_BEGINNING, + XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, label, + XmNleftOffset, 10, + XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, label, + XmNrightAttachment, XmATTACH_FORM, NULL); + XmStringFree(s); + return v; +} + +/* a section: a toggle with the arrow in its label and the form under it */ +static Widget +Section(Info *in, const char *title, Widget *formp) +{ + Widget t, f; + XmString s; + char label[64]; + + (void)snprintf(label, sizeof label, "v %s", title); + s = XmStringCreateLocalized(label); + t = XtVaCreateManagedWidget("section", xmToggleButtonWidgetClass, + in->box, XmNlabelString, s, XmNindicatorOn, False, + XmNset, True, XmNalignment, XmALIGNMENT_BEGINNING, + XmNmarginTop, 10, NULL); + XmStringFree(s); + f = XtVaCreateManagedWidget("sectionBody", xmFormWidgetClass, + in->box, XmNmarginLeft, 14, XmNmarginBottom, 4, NULL); + *formp = f; + XtAddCallback(t, XmNvalueChangedCallback, SectionToggled, f); + return t; +} + +static void +SectionToggled(Widget w, XtPointer client, XtPointer call) +{ + XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call; + Widget form = (Widget) client; + char *text = NULL; + + XtVaGetValues(w, XmNlabelString, &text, NULL); + if (text == NULL) + return; + text[0] = c->set ? 'v' : '>'; + { + XmString s = XmStringCreateLocalized(text); + + XtVaSetValues(w, XmNlabelString, s, NULL); + XmStringFree(s); + } + XtFree(text); + if (c->set) + XtManageChild(form); + else + XtUnmanageChild(form); +} + +/* ---- permissions -------------------------------------------------------- */ + +static void +PermChanged(Widget w, XtPointer client, XtPointer call) +{ + Info *in = client; + Widget menus[3], item; + struct stat st; + mode_t mode = 0; + int i; + + (void)w; + (void)call; + if (in->path == NULL || stat(in->path, &st) != 0) + return; + menus[0] = in->ownerM; + menus[1] = in->groupM; + menus[2] = in->otherM; + for (i = 0; i < 3; i++) { + long v = 2; + + item = NULL; + XtVaGetValues(menus[i], XmNmenuHistory, &item, NULL); + if (item != NULL) + XtVaGetValues(item, XmNuserData, &v, NULL); + mode |= (mode_t) (v == 0 ? 6 : v == 1 ? 4 : 0) << + ((2 - i) * 3); + } + (void)chmod(in->path, mode); +} + +static Widget +ModeMenu(Info *in, Widget form, Widget label) +{ + static const char *const names[] = { "Read & Write", "Read only", + "No access" }; + Widget menu, pd, b; + int v; + + menu = XmCreateOptionMenu(form, "mode", NULL, 0); + XtVaSetValues(menu, + XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, label, + XmNleftOffset, 10, + XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, label, + XmNrightAttachment, XmATTACH_FORM, NULL); + pd = XmCreatePulldownMenu(menu, "modes", NULL, 0); + for (v = 0; v < 3; v++) { + XmString s = XmStringCreateLocalized((char *) names[v]); + + b = XtVaCreateManagedWidget("modeItem", xmPushButtonGadgetClass, + pd, XmNlabelString, s, XmNuserData, (XtPointer) (long) v, + NULL); + XmStringFree(s); + XtAddCallback(b, XmNactivateCallback, PermChanged, in); + } + XtVaSetValues(menu, XmNsubMenuId, pd, NULL); + XtManageChild(menu); + return menu; +} + +/* select the item of MENU that belongs to the file's bits SHIFT */ +static void +SetMode(Widget menu, mode_t mode, int shift) +{ + int v = (int) ((mode >> shift) & 7); + int want = (v & 6) == 6 ? 0 : (v & 4) == 4 ? 1 : 2; + Widget pd = NULL, *kids = NULL; + Cardinal n = 0, i; + + XtVaGetValues(menu, XmNsubMenuId, &pd, NULL); + if (pd == NULL) + return; + XtVaGetValues(pd, XmNchildren, &kids, XmNnumChildren, &n, NULL); + for (i = 0; i < n; i++) { + long val = -1; + + XtVaGetValues(kids[i], XmNuserData, &val, NULL); + if (val == want) { + XtVaSetValues(menu, XmNmenuHistory, kids[i], NULL); + break; + } + } +} + +/* ---- comment and lock --------------------------------------------------- */ + +static void +SaveComment(Widget w, XtPointer client, XtPointer call) +{ + Info *in = client; + char *text; + int rc; + + (void)w; + (void)call; + if (in->path == NULL) + return; + text = XmTextFieldGetString(in->comment); + rc = mcde_sys_file_comment_set(in->path, text != NULL ? text : ""); + XmTextFieldSetSelection(in->comment, 0, + XmTextFieldGetLastPosition(in->comment), CurrentTime); + if (rc != MCDE_SYS_OK) { + SetLabel(in->comNote, mcde_sys_error()); + XtSetSensitive(in->comment, False); + XtSetSensitive(in->save, False); + } else + SetLabel(in->comNote, ""); + XtFree(text); +} + +static void +LockToggled(Widget w, XtPointer client, XtPointer call) +{ + Info *in = client; + XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call; + int rc; + + if (in->path == NULL) + return; + rc = mcde_sys_file_locked_set(in->path, c->set); + if (rc != MCDE_SYS_OK) { + XtVaSetValues(w, XmNset, c->set ? XmUNSET : XmSET, NULL); + XBell(XtDisplay(w), 0); + SetLabel(in->genT, "v General"); + } +} + +/* ---- the window --------------------------------------------------------- */ + +static void +KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont) +{ + Info *in = client; + + (void)w; + if (XLookupKeysym(&event->xkey, 0) == XK_Escape) { + XtPopdown(in->shell); + *cont = False; + } +} + +static void +Build(Info *in) +{ + Widget form, sw, genF, permF, comF, prevF, l; + + in->shell = XtVaCreatePopupShell("mcdeInfo", + topLevelShellWidgetClass, in->rec->shell, + XmNtitle, "Info", XmNallowShellResize, True, NULL); + form = XtVaCreateManagedWidget("infoForm", xmFormWidgetClass, + in->shell, XmNmarginWidth, 10, XmNmarginHeight, 10, + XmNwidth, 360, XmNheight, 640, NULL); + in->form = form; + + sw = XtVaCreateManagedWidget("infoScrolled", xmScrolledWindowWidgetClass, + form, + XmNscrollingPolicy, XmAUTOMATIC, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_FORM, NULL); + in->box = XtVaCreateManagedWidget("infoBox", xmRowColumnWidgetClass, + sw, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_TIGHT, + XmNmarginWidth, 6, XmNmarginHeight, 6, NULL); + + /* the head: the type's picture, the name, size and date */ + in->icon = XtVaCreateManagedWidget("infoIcon", xmLabelWidgetClass, + in->box, XmNalignment, XmALIGNMENT_BEGINNING, NULL); + in->name = XtVaCreateManagedWidget("infoName", xmLabelWidgetClass, + in->box, XmNalignment, XmALIGNMENT_BEGINNING, NULL); + in->summary = XtVaCreateManagedWidget("infoSummary", + xmLabelWidgetClass, in->box, XmNalignment, XmALIGNMENT_BEGINNING, + NULL); + + in->genT = Section(in, "General", &genF); + in->genF = genF; + l = Row(in, genF, "Kind:", NULL); + in->kindV = Value(in, genF, l, ""); + l = Row(in, genF, "Size:", l); + in->sizeV = Value(in, genF, l, ""); + l = Row(in, genF, "On disk:", l); + in->diskV = Value(in, genF, l, ""); + l = Row(in, genF, "Where:", l); + in->whereV = Value(in, genF, l, ""); + l = Row(in, genF, "Created:", l); + in->createdV = Value(in, genF, l, ""); + l = Row(in, genF, "Modified:", l); + in->modifiedV = Value(in, genF, l, ""); + in->lockedT = XtVaCreateManagedWidget("locked", xmToggleButtonWidgetClass, + genF, XmNlabelString, XmStringCreateLocalized((char *) "Locked"), + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, l, + XmNtopOffset, 8, XmNleftAttachment, XmATTACH_FORM, NULL); + XtAddCallback(in->lockedT, XmNvalueChangedCallback, LockToggled, in); + + in->permT = Section(in, "Permissions", &permF); + in->permF = permF; + l = Row(in, permF, "Owner:", NULL); + in->ownerM = ModeMenu(in, permF, l); + l = Row(in, permF, "Group:", l); + in->groupM = ModeMenu(in, permF, l); + l = Row(in, permF, "Everyone:", l); + in->otherM = ModeMenu(in, permF, l); + + in->comT = Section(in, "Comment", &comF); + in->comF = comF; + in->comment = XtVaCreateManagedWidget("comment", xmTextFieldWidgetClass, + comF, XmNcolumns, 34, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, NULL, NULL); + in->save = XtVaCreateManagedWidget("save", xmPushButtonWidgetClass, + comF, XmNlabelString, XmStringCreateLocalized((char *) "Save"), + XmNtopAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + XtVaSetValues(in->comment, XmNrightAttachment, XmATTACH_WIDGET, + XmNrightWidget, in->save, XmNrightOffset, 6, NULL); + XtAddCallback(in->save, XmNactivateCallback, SaveComment, in); + XtAddCallback(in->comment, XmNactivateCallback, SaveComment, in); + in->comNote = XtVaCreateManagedWidget("comNote", xmLabelWidgetClass, + comF, XmNalignment, XmALIGNMENT_BEGINNING, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, in->comment, + XmNtopOffset, 4, XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + + in->prevT = Section(in, "Preview", &prevF); + in->prevF = prevF; + in->prevImage = XtVaCreateManagedWidget("preview", + xmLabelWidgetClass, prevF, XmNalignment, XmALIGNMENT_BEGINNING, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, NULL); + + XtAddEventHandler(in->shell, KeyPressMask, False, KeyHandler, in); + XtVaSetValues(in->shell, XmNdeleteResponse, XmUNMAP, NULL); +} + +/* ---- what it shows ------------------------------------------------------ */ + +static void +Fill(Info *in) +{ + struct stat st; + char buf[512], kind[160], when[64], *where; + const char *name; + Pixmap icon; + char *comment = NULL; + + if (in->path == NULL || stat(in->path, &st) != 0) { + SetLabel(in->name, "Nothing is chosen."); + SetLabel(in->summary, ""); + return; + } + name = strrchr(in->path, '/'); + name = name != NULL && name[1] != '\0' ? name + 1 : in->path; + SetLabel(in->name, name); + SizeText(st.st_size, buf, sizeof buf); + (void)strftime(when, sizeof when, "%e %b %Y, %H:%M", + localtime(&st.st_mtime)); + { + char sum[600]; + + (void)snprintf(sum, sizeof sum, "%s - %s", buf, when); + SetLabel(in->summary, sum); + } + + /* the type's picture, made anew: Motif takes a label's pixmap + * reliably only when the label is created */ + if (in->iconpm != XmUNSPECIFIED_PIXMAP) + McdeThumbFreePixmap(XtScreen(in->shell), in->iconpm); + icon = TypeIcon(in->icon, in->path); + in->iconpm = icon; + XtDestroyWidget(in->icon); + in->icon = XtVaCreateManagedWidget("infoIcon", xmLabelWidgetClass, + in->box, XmNalignment, XmALIGNMENT_BEGINNING, + XmNlabelType, icon != XmUNSPECIFIED_PIXMAP ? XmPIXMAP : XmSTRING, + XmNlabelPixmap, icon, NULL); + if (icon == XmUNSPECIFIED_PIXMAP) { + /* a pixmap label without one would show the widget's name */ + SetLabel(in->icon, ""); + XtUnmanageChild(in->icon); + } + + SetLabel(in->kindV, KindText(in->path, &st, kind, sizeof kind)); + SizeText(st.st_size, buf, sizeof buf); + SetLabel(in->sizeV, buf); + { + char disk[64]; + + SizeText((off_t) st.st_blocks * 512, disk, sizeof disk); + SetLabel(in->diskV, disk); + } + where = XtNewString(in->path); + { + char *slash = strrchr(where, '/'); + + if (slash != NULL && slash != where) + *slash = '\0'; + else + (void)snprintf(where, strlen(where) + 1, "/"); + } + SetLabel(in->whereV, where); + XtFree(where); + /* POSIX gives no birth time; NetBSD has st_birthtime, Linux does + * not -- there the line says so instead of showing another time */ +#if defined(__NetBSD__) + { + char created[64]; + + (void)strftime(created, sizeof created, "%e %b %Y, %H:%M", + localtime(&st.st_birthtime)); + SetLabel(in->createdV, created); + } +#else + SetLabel(in->createdV, "not known"); +#endif + SetLabel(in->modifiedV, when); + + { + int locked = 0; + + if (mcde_sys_file_locked_get(in->path, &locked) == MCDE_SYS_OK) + XtVaSetValues(in->lockedT, XmNset, locked ? XmSET : + XmUNSET, NULL); + else + XtSetSensitive(in->lockedT, False); + } + + SetMode(in->ownerM, st.st_mode, 6); + SetMode(in->groupM, st.st_mode, 3); + SetMode(in->otherM, st.st_mode, 0); + + { + int rc; + + if ((comment = XtMalloc(512)) != NULL) { + rc = mcde_sys_file_comment_get(in->path, comment, 512); + if (rc == MCDE_SYS_OK) { + XmTextFieldSetString(in->comment, comment); + SetLabel(in->comNote, ""); + XtSetSensitive(in->comment, True); + XtSetSensitive(in->save, True); + } else { + XmTextFieldSetString(in->comment, ""); + SetLabel(in->comNote, mcde_sys_error()); + XtSetSensitive(in->comment, False); + XtSetSensitive(in->save, False); + } + XtFree(comment); + } + } + + if (!S_ISDIR(st.st_mode)) { + Pixmap pm = XmUNSPECIFIED_PIXMAP; + int w = 0, h = 0; + + if (McdeThumbLoad(XtScreen(in->shell), in->path, PREVIEW_W, + PREVIEW_H, &pm, &w, &h) == 0) { + if (in->prevpm != XmUNSPECIFIED_PIXMAP) + McdeThumbFreePixmap(XtScreen(in->shell), + in->prevpm); + in->prevpm = pm; + XtVaSetValues(in->prevImage, XmNlabelType, XmPIXMAP, + XmNlabelPixmap, pm, NULL); + } else + SetLabel(in->prevImage, "No preview."); + } else if (in->prevpm != XmUNSPECIFIED_PIXMAP) { + McdeThumbFreePixmap(XtScreen(in->shell), in->prevpm); + in->prevpm = XmUNSPECIFIED_PIXMAP; + SetLabel(in->prevImage, ""); + } +} + +/* ---- what the menu calls ------------------------------------------------ */ + +void +_InfoShow(FileMgrRec *rec) +{ + Info *in = InfoOf(rec, True); + FileMgrData *data = DataOf(rec); + char *path; + + if (in == NULL) + return; + if (in->shell == NULL) + Build(in); + if (in->path != NULL) { + XtFree(in->path); + in->path = NULL; + } + path = ChosenPath(data); + if (path == NULL) { + XBell(XtDisplay(rec->shell), 0); + return; + } + in->path = path; + Fill(in); + XtVaSetValues(in->shell, XmNtransientFor, rec->shell, NULL); + XtPopup(in->shell, XtGrabNone); +} diff --git a/programs/dtfile/Info.h b/programs/dtfile/Info.h new file mode 100644 index 00000000..922f28e3 --- /dev/null +++ b/programs/dtfile/Info.h @@ -0,0 +1,17 @@ +/* + * Info.h -- MCDE: the Info window of the file manager (Info.c) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + */ + +#ifndef _Info_h +#define _Info_h + +#include "FileMgr.h" + +/* Get Info (Cmd+I): a narrow window with the file's name, size and date + * and collapsible sections -- General, Permissions, Comment, Preview. A + * second call for the same window replaces what it shows. */ +extern void _InfoShow(FileMgrRec *rec); + +#endif /* _Info_h */ diff --git a/programs/dtfile/Makefile.am b/programs/dtfile/Makefile.am index 071ea0eb..63dccde5 100644 --- a/programs/dtfile/Makefile.am +++ b/programs/dtfile/Makefile.am @@ -41,7 +41,7 @@ dtfile_SOURCES = ChangeDir.c ChangeDirP.c Command.c Common.c \ ModAttr.c ModAttrP.c MultiView.c OverWrite.c \ Prefs.c PrefsP.c SharedMsgs.c SharedProcs.c \ ToolTalk.c Trash.c Utils.c fsDialog.c \ - Navigation.c Columns.c NetView.c Peek.c GoToFolder.c + Navigation.c Columns.c NetView.c Peek.c GoToFolder.c Info.c # Mind the quoting here... SCRIPTFLAGS = -DSHAPE -D_ILS_MACROS -DSUN_PERF \ diff --git a/programs/dtfile/Menu.c b/programs/dtfile/Menu.c index a65e2654..94e01fc0 100644 --- a/programs/dtfile/Menu.c +++ b/programs/dtfile/Menu.c @@ -96,6 +96,7 @@ #include "Help.h" #include "SharedMsgs.h" #include "GoToFolder.h" +#include "Info.h" #define INDICATOR_TIME 12 @@ -108,7 +109,7 @@ #endif #if defined(ADD_SHOW_TRASH) -static int menuItemCount = 53; +static int menuItemCount = 54; #else static int menuItemCount = 51; #endif @@ -132,6 +133,10 @@ static void ShowGoToFolder( Widget w, XtPointer client_data, XtPointer call_data ); +static void ShowInfoCB( + Widget w, + XtPointer client_data, + XtPointer call_data ); static void EmptyTrashCB( Widget w, XtPointer client_data, @@ -420,6 +425,18 @@ CreateMenu( mainMenu[j].name = "rename"; mainMenu[j++].activateCallback = ShowRenameFileDialog; + /* MCDE: the reference's Get Info (Cmd+I), before the old + * permissions dialog, which keeps its item without the key */ + mainMenu[j].type = MENU_BUTTON; + mainMenu[j].maskBit = MODIFY; + mainMenu[j].label = GETMESSAGE(20, 160, "Get Info"); + mainMenu[j].label = XtNewString(mainMenu[j].label); + mainMenu[j].mnemonic = GETMESSAGE(20, 161, "I"); + mainMenu[j].mnemonic = XtNewString(mainMenu[j].mnemonic); + mainMenu[j].helpData = HELP_PROPERTIES_COMMAND_STR; + mainMenu[j].name = "getInfo"; + mainMenu[j++].activateCallback = ShowInfoCB; + mainMenu[j].type = MENU_BUTTON; mainMenu[j].maskBit = MODIFY; mainMenu[j].label = GETMESSAGE(20, 150, "Change Permissions..."); @@ -1004,6 +1021,30 @@ ShowGoToFolder( _GoToFolderShow(file_mgr_rec); } +/* MCDE: File > Get Info (Cmd+I) opens the Info window (Info.c) */ +static void +ShowInfoCB( + Widget w, + XtPointer client_data, + XtPointer call_data ) +{ + FileMgrRec * file_mgr_rec = NULL; + Widget mbar; + Arg args[1]; + + (void) call_data; + if (w == NULL) + return; + if ((XtArgVal) client_data == FM_POPUP) + mbar = XtParent(w); + else + mbar = (Widget) XmGetPostedFromWidget(XtParent(w)); + XtSetArg(args[0], XmNuserData, &file_mgr_rec); + XtGetValues(mbar, args, 1); + if (file_mgr_rec != NULL) + _InfoShow(file_mgr_rec); +} + /* MCDE: empty the Trash from this window's File menu */ static void EmptyTrashCB( diff --git a/programs/dtfile/dtfile.msg b/programs/dtfile/dtfile.msg index ac3def7a..7008799a 100644 --- a/programs/dtfile/dtfile.msg +++ b/programs/dtfile/dtfile.msg @@ -1190,6 +1190,9 @@ $ Message 158 is the text for the File.EmptyTrash menu option 158 Empty Trash... $ Message 159 is the text for the File.GoToFolder menu option 159 Go to Folder... +$ Messages 160-161 are the text and mnemonic for the File.GetInfo option +160 Get Info +161 I