/* * Info.c -- MCDE: the Info window of the file manager (Info.h) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * 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); }