Files
lotherk 63acc18853 MCDE: one settings window for every program, dtpad first (t0164)
lib/DtWidget/McdePrefs.c and include/Dt/McdePrefs.h: the reference's
preferences window -- a row of icon tabs from appconfig/icons/C (the active
one pressed), the tab's form under it, no OK or Cancel.  A tab's form is
built the first time it is shown; Escape closes, the close box only hides
it.  McdePrefsRow() puts a right-aligned label left of a widget and stacks
the rows; McdePrefsSeparator() draws the group lines.

Values are kept in ~/.dt/<prog>.conf as KEY=VALUE (English, the tab shown
last as lasttab=) -- McdePrefsConf.c does that without X, so test_prefs
tests it without a display: get/set, keep the other keys, replace instead of
doubling, no comment pile-up.

dtpad is the first user: Options > Preferences... opens two tabs, "Editing"
(Wrap to fit, Overstrike) and "Window" (Status line).  A change goes to the
Dt Editor at once, the Options menu's toggle follows, and the value is
written.  At start dtpad overlays ~/.dt/dtpad.conf over its X resources, so
the next start has it.

Measured under Xvfb with xdotool:

  - Options > Preferences... shows the icon tabs and the rows
    (docs/screenshots/leopard/dtpad-preferences-mcde.png);
  - switching "Wrap to fit" on writes "wrap=1" into ~/.dt/dtpad.conf (cat;
    the header is written once, lasttab=Editing is kept);
  - starting dtpad with "wrap=1" and "status=1" in that file shows the
    status line (Line: 1  Total: 1) without any menu being touched -- the
    change survives a restart;
  - Escape closes the window;
  - make check in lib/DtWidget: test_prefs PASS.

Open: dtfile's Prefs.c and dtcalendar (t0149, not built yet) are not moved
to it -- dtpad proves the building block; Command+, still finds a program's
menu entry through the old name heuristic instead of an explicit
registration.  Both are follow-up tasks.
2026-11-14 11:52:34 +00:00

111 lines
2.7 KiB
C

/*
* McdePrefsConf.c -- MCDE: ~/.dt/<prog>.conf (McdePrefs.h)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* One KEY=VALUE a line, English, comments with '#'; written as a whole
* (temp file, then renamed over it), so a half-written file is never read.
* No X here: test_prefs.c tests it without a display.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <Dt/McdePrefs.h>
#define MAXLINES 256
#define LINELEN 512
static int
ConfPath(const char *prog, char *buf, size_t len)
{
const char *home = getenv("HOME");
if (home == NULL || prog == NULL || prog[0] == '\0')
return -1;
if ((size_t) snprintf(buf, len, "%s/.dt/%s.conf", home, prog) >= len)
return -1;
return 0;
}
int
McdePrefsConfGet(const char *prog, const char *key, char *buf, size_t len)
{
char path[1024], line[LINELEN], *eq;
FILE *f;
size_t kl;
if (buf == NULL || len == 0 || ConfPath(prog, path, sizeof path) != 0)
return 0;
buf[0] = '\0';
if ((f = fopen(path, "r")) == NULL)
return 0;
kl = strlen(key);
while (fgets(line, sizeof line, f) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, key, kl) != 0 || line[kl] != '=')
continue;
eq = line + kl + 1;
(void)snprintf(buf, len, "%s", eq);
fclose(f);
return 1;
}
fclose(f);
return 0;
}
int
McdePrefsConfSet(const char *prog, const char *key, const char *value)
{
char path[1024], tmp[1100], dir[1024], lines[MAXLINES][LINELEN];
char line[LINELEN], *slash;
FILE *f;
int n = 0, i, done = 0;
size_t kl;
if (ConfPath(prog, path, sizeof path) != 0)
return -1;
kl = strlen(key);
if ((f = fopen(path, "r")) != NULL) {
while (n < MAXLINES && fgets(line, sizeof line, f) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#')
continue; /* our header is written anew */
if (strncmp(line, key, kl) == 0 && line[kl] == '=')
continue;
(void)snprintf(lines[n++], sizeof lines[0], "%s", line);
}
fclose(f);
}
if (value != NULL && n < MAXLINES) {
(void)snprintf(lines[n], sizeof lines[0], "%s=%s", key, value);
n++;
done = 1;
}
(void)done;
/* ~/.dt, so that the first write has somewhere to go */
(void)snprintf(dir, sizeof dir, "%s", path);
if ((slash = strrchr(dir, '/')) != NULL) {
*slash = '\0';
(void)mkdir(dir, 0755);
}
(void)snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL)
return -1;
fputs("# MCDE's settings (System Settings style): KEY=VALUE, one a "
"line\n", f);
for (i = 0; i < n; i++)
fprintf(f, "%s\n", lines[i]);
if (fclose(f) != 0 || rename(tmp, path) != 0) {
(void)unlink(tmp);
return -1;
}
return 0;
}