Skip to content

Commit

Permalink
Return XDG_DATA_HOME from Sys_HomeDirectory()
Browse files Browse the repository at this point in the history
Fall back on $HOME/.local/share, if necessary. Return "\0" on fail, just
like the WIndows version.

Use system("mkdir -p") to crate the directory structure in
Sys_RegisterQWURLProtocol_f()
  • Loading branch information
mcz committed Oct 26, 2022
1 parent dcd155e commit c0a3e02
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions sys_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,19 @@ const char* Sys_FontsDirectory(void)

const char* Sys_HomeDirectory(void)
{
char *ev = getenv("HOME");
if (ev) {
return ev;
}
return "";
char *ev, *buf;
if (!(ev = getenv("XDG_DATA_HOME"))) {
if((ev = getenv("HOME"))) {
buf = malloc(strlen(ev) + 15);
sprintf(buf, "%s/.local/share", ev);
} else {
buf = malloc(1);
buf[0] = 0;
}
} else {
buf = strdup(ev);
}
return(buf)
}

#ifdef __MACOSX__
Expand All @@ -759,12 +767,21 @@ void Sys_RegisterQWURLProtocol_f(void)
char open_cmd[MAX_PATH*2+1024] = { 0 };
char exe_path[MAX_PATH] = { 0 };
char buf[MAX_PATH] = { 0 };
const char *homedir=Sys_HomeDirectory();
const char *homedir = Sys_HomeDirectory();
int nchar = -1;
FILE *fptr;

qbool quiet = Cmd_Argc() == 2 && !strcmp(Cmd_Argv(1), "quiet");

if (!homedir) {
Con_Printf("Failed to get home directory.");
return;
}
snprintf(buf, sizeof(buf), "%s/applications", homedir);
free(homedir)
snprintf(open_cmd, sizeof(open_cmd), "mkdir -pm 0755 \"%s\"", buf);
system(open_cmd);

nchar = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
if (nchar < 0 || nchar >= sizeof exe_path) {
Con_Printf("Failed to get executable path.");
Expand All @@ -784,29 +801,16 @@ void Sys_RegisterQWURLProtocol_f(void)
"Terminal=false\n"
"StartupNotify=false\n"
, exe_path, "%u", com_basedir);
snprintf(buf, sizeof(buf), "%s/.local/share/applications/qw-url-handler.desktop", homedir);

fptr = fopen(buf, "wb+");
if(!fptr){
//create directory path to url handler file
snprintf(buf, sizeof(buf), "%s/.local", homedir);
Sys_mkdir(buf);
snprintf(buf, sizeof(buf), "%s/.local/share", homedir);
Sys_mkdir(buf);
snprintf(buf, sizeof(buf), "%s/.local/share/applications", homedir);
Sys_mkdir(buf);
//attempt opening the file again
snprintf(buf, sizeof(buf), "%s/.local/share/applications/qw-url-handler.desktop", homedir);
fptr = fopen(buf, "wb+");
}
if (fptr == NULL) {
strlcat(buf, "/qw-url-handler.desktop", sizeof(buf));

if (!(fptr = fopen(buf, "w"))) {
Con_Printf("Failed to open qw-url-handler file.");
return;
}
fprintf(fptr, "%s", open_cmd);
fclose(fptr);

if(system("xdg-mime default qw-url-handler.desktop x-scheme-handler/qw") != 0){
if(system("xdg-mime default qw-url-handler.desktop x-scheme-handler/qw") != 0){
Con_Printf("Failed to register qw-url-handler file with xdg-mime, please make sure this program is installed.");
}

Expand Down

0 comments on commit c0a3e02

Please sign in to comment.