Files
mcde/programs/dtsystemsettings/defaults.c
T

286 lines
7.8 KiB
C

/*
* defaults.c -- dtsystemsettings: the default programs (t0180)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The reference's "General" page of the browser, made the desktop's own:
* which program opens a web page, mail, an image, a film, a text file, and
* how a link from another program arrives (a new window or a tab in the
* window that is there). The choices are kept in ~/.dt/defaults, a small
* "key: value" file:
*
* web: firefox
* mail: dtmail
* image: dtphoto
* video: xine
* text: dtpad
* web-open: tab
*
* The actions of programs/types read it (McdeWeb and its neighbours); the
* file is English and stays readable. A program is offered only when it is
* on this computer ($PATH), and the entry says so when none is.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ToggleB.h>
#include "ui.h"
typedef struct {
const char *key; /* the line of ~/.dt/defaults */
const char *label;
const char *const candidates[6];
} Kind;
static const Kind kinds[] = {
{ "web", "Web browser:", { "firefox", "chromium", "epiphany",
"dillo", NULL } },
{ "mail", "Mail:", { "dtmail", "thunderbird", "evolution", "mutt",
NULL } },
{ "image", "Images:", { "dtphoto", "eog", "feh", "display", NULL } },
{ "video", "Video:", { "xine", "vlc", "mpv", "totem", NULL } },
{ "text", "Text:", { "dtpad", "gedit", "mousepad", "leafpad",
NULL } },
};
#define NKINDS ((int) (sizeof kinds / sizeof kinds[0]))
static Widget windowToggle, tabToggle, status;
/* ---- ~/.dt/defaults ("key: value") -------------------------------------- */
static void
DefaultsPath(char *buf, size_t len)
{
const char *home = getenv("HOME");
(void)snprintf(buf, len, "%s/.dt/defaults", home != NULL ? home :
".");
}
/* the value of KEY, "" when the file does not name it */
static void
Get(const char *key, char *buf, size_t len)
{
char path[1024], line[256];
FILE *f;
buf[0] = '\0';
DefaultsPath(path, sizeof path);
if ((f = fopen(path, "r")) == NULL)
return;
while (fgets(line, sizeof line, f) != NULL) {
char *c;
line[strcspn(line, "\r\n")] = '\0';
if ((c = strchr(line, ':')) == NULL)
continue;
*c = '\0';
if (strcmp(line, key) != 0)
continue;
c++;
while (*c == ' ' || *c == '\t')
c++;
(void)snprintf(buf, len, "%s", c);
break;
}
fclose(f);
}
static void
Set(const char *key, const char *value)
{
char path[1024], tmp[1100], line[256];
FILE *f;
int done = 0;
DefaultsPath(path, sizeof path);
(void)snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL)
return;
fputs("# the desktop's default programs (System Settings > Default\n"
"# Programs): \"key: value\", one a line, English keys.\n", f);
{
FILE *old = fopen(path, "r");
if (old != NULL) {
while (fgets(line, sizeof line, old) != NULL) {
char copy[256], *c;
(void)snprintf(copy, sizeof copy, "%s", line);
copy[strcspn(copy, "\r\n")] = '\0';
if ((c = strchr(copy, ':')) != NULL) {
*c = '\0';
if (strcmp(copy, key) == 0) {
done = 1;
continue;
}
}
if (strncmp(line, "# the desktop's", 15) == 0)
continue; /* the header anew */
fputs(line, f);
}
fclose(old);
}
}
fprintf(f, "%s: %s\n", key, value);
fclose(f);
(void)rename(tmp, path);
(void)done;
}
/* 1 when NAME is on this computer */
static int
Present(const char *name)
{
char path[1024], *p, *save = NULL;
const char *env = getenv("PATH");
struct stat st;
if (env == NULL)
env = "/usr/bin:/bin";
(void)snprintf(path, sizeof path, "%s", env);
for (p = strtok_r(path, ":", &save); p != NULL;
p = strtok_r(NULL, ":", &save)) {
char file[1400];
(void)snprintf(file, sizeof file, "%s/%s", p, name);
if (stat(file, &st) == 0 && S_ISREG(st.st_mode) &&
access(file, X_OK) == 0)
return 1;
}
return 0;
}
/* ---- the rows ----------------------------------------------------------- */
static void
Chosen(Widget w, XtPointer data, XtPointer call)
{
const Kind *k = (const Kind *) data;
const char *value = (const char *) XtName(w);
char line[128];
(void) call;
(void)snprintf(line, sizeof line, "%s", value);
Set(k->key, line);
SetText(status, "Kept in ~/.dt/defaults.");
}
static Widget
Row(Widget parent, const Kind *k)
{
Widget row = XtVaCreateManagedWidget("defaultRow",
xmRowColumnWidgetClass, parent, XmNorientation, XmHORIZONTAL,
XmNspacing, 10, XmNmarginWidth, 0, NULL);
XmString s = XmStringCreateLocalized((char *) k->label);
Widget l = XtVaCreateManagedWidget("defaultLabel",
xmLabelWidgetClass, row, XmNlabelString, s, XmNwidth, 200,
XmNrecomputeSize, False, XmNalignment, XmALIGNMENT_END, NULL);
Widget opt, pd, item[6];
char cur[64];
int i, n = 0, at = 0;
XmStringFree(s);
(void) l;
opt = XmCreateOptionMenu(row, "defaultChoice", NULL, 0);
pd = XmCreatePulldownMenu(opt, "defaultMenu", NULL, 0);
Get(k->key, cur, sizeof cur);
for (i = 0; k->candidates[i] != NULL; i++) {
XmString is;
if (!Present(k->candidates[i]))
continue;
is = XmStringCreateLocalized((char *) k->candidates[i]);
item[n] = XtVaCreateManagedWidget(k->candidates[i],
xmPushButtonGadgetClass, pd, XmNlabelString, is, NULL);
XmStringFree(is);
XtAddCallback(item[n], XmNactivateCallback, Chosen,
(XtPointer) k);
if (cur[0] != '\0' && strcmp(cur, k->candidates[i]) == 0)
at = n;
n++;
}
if (n == 0) {
XmString is = XmStringCreateLocalized((char *) "(none on this "
"computer)");
item[0] = XtVaCreateManagedWidget("none",
xmPushButtonGadgetClass, pd, XmNlabelString, is, NULL);
XmStringFree(is);
XtSetSensitive(item[0], False);
n = 1;
}
XtVaSetValues(opt, XmNsubMenuId, pd, XmNmenuHistory, item[at], NULL);
XtManageChild(opt);
return row;
}
static void
LinkChosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
if (((XmToggleButtonCallbackStruct *) call)->set)
Set("web-open", (const char *) data);
}
Widget
MakeDefaults(Widget parent)
{
Widget box = XtVaCreateWidget("defaults", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget radio;
char v[32];
int i;
(void) Text(box, "defaultsLabel", "Open with:");
for (i = 0; i < NKINDS; i++)
(void) Row(box, &kinds[i]);
(void) Text(box, "linksLabel", "A link from another program opens:");
radio = XtVaCreateManagedWidget("linksMode",
xmRowColumnWidgetClass, box, XmNorientation, XmVERTICAL,
XmNradioBehavior, True, XmNspacing, 2, XmNmarginWidth, 0, NULL);
windowToggle = XtVaCreateManagedWidget("in a new window",
xmToggleButtonWidgetClass, radio, NULL);
tabToggle = XtVaCreateManagedWidget("in a tab of the window that is "
"there", xmToggleButtonWidgetClass, radio, NULL);
Get("web-open", v, sizeof v);
XmToggleButtonSetState(strcmp(v, "window") == 0 ? windowToggle :
tabToggle, True, False);
{
/* the pair again once the pane is laid out (a radio state set
* before does not paint, measured) */
Widget want = strcmp(v, "window") == 0 ? windowToggle :
tabToggle;
XmToggleButtonSetState(windowToggle, False, False);
XmToggleButtonSetState(tabToggle, False, False);
XmToggleButtonSetState(want, True, False);
}
XtAddCallback(windowToggle, XmNvalueChangedCallback, LinkChosen,
(XtPointer) "window");
XtAddCallback(tabToggle, XmNvalueChangedCallback, LinkChosen,
(XtPointer) "tab");
(void) Text(box, "linksNote", "The web action of the desktop reads it "
"and starts the browser with the window or the tab.");
(void) Text(box, "defaultsNote", "The choices are kept in "
"~/.dt/defaults; the programs of programs/types read the file.");
status = Text(box, "defaultsStatus", "");
return box;
}