Skip to content

Commit

Permalink
Implement initial support for XDG [1]
Browse files Browse the repository at this point in the history
Introduce getFontForgeUserDir(arg) where arg is one of Cache, Config, or
Data.  Replace every instance of getPfaEditDir(buffer) with
getFontForgeUserDir(Config).

[1] http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  • Loading branch information
holomorph committed Nov 21, 2013
1 parent e5363ae commit e4c2cc7
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 31 deletions.
2 changes: 1 addition & 1 deletion fontforge/autosave.c
Expand Up @@ -82,7 +82,7 @@ return( editdir );
}

static char *getAutoDirName(char *buffer) {
char *dir=getPfaEditDir(buffer);
char *dir=getFontForgeUserDir(Config);

if ( dir==NULL )
return( NULL );
Expand Down
4 changes: 2 additions & 2 deletions fontforge/encoding.c
Expand Up @@ -481,9 +481,9 @@ static char *getPfaEditEncodings(void) {

if ( encfile!=NULL )
return( encfile );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/Encodings.ps", getPfaEditDir(buffer));
sprintf(buffer,"%s/Encodings.ps", getFontForgeUserDir(Config));
encfile = copy(buffer);
return( encfile );
}
Expand Down
1 change: 1 addition & 0 deletions fontforge/fontforgevw.h
Expand Up @@ -28,6 +28,7 @@
#define _FONTFORGEVW_H_

#include "fontforge.h"
#include "gfile.h"
#include "gimage.h"
#include "baseviews.h"

Expand Down
4 changes: 2 additions & 2 deletions fontforge/fontview.c
Expand Up @@ -4947,7 +4947,7 @@ static void FVMenuMakeNamelist(GWindow gw, struct gmenuitem *UNUSED(mi), GEvent
char *filename, *temp;
FILE *file;

snprintf(buffer, sizeof(buffer),"%s/%s.nam", getPfaEditDir(buffer), fv->b.sf->fontname );
snprintf(buffer, sizeof(buffer),"%s/%s.nam", getFontForgeUserDir(Config), fv->b.sf->fontname );
temp = def2utf8_copy(buffer);
filename = gwwv_save_filename(_("Make Namelist"), temp,"*.nam");
free(temp);
Expand Down Expand Up @@ -4986,7 +4986,7 @@ return; /* Cancelled */
pt = temp;
else
++pt;
snprintf(buffer,sizeof(buffer),"%s/%s", getPfaEditDir(buffer), pt);
snprintf(buffer,sizeof(buffer),"%s/%s", getFontForgeUserDir(Config), pt);
if ( access(buffer,F_OK)==0 ) {
buts[0] = _("_Replace");
buts[1] = _("_Cancel");
Expand Down
4 changes: 2 additions & 2 deletions fontforge/groups.c
Expand Up @@ -76,9 +76,9 @@ static char *getPfaEditGroups(void) {

if ( groupname!=NULL )
return( groupname );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/groups", getPfaEditDir(buffer));
sprintf(buffer,"%s/groups", getFontForgeUserDir(Config));
groupname = copy(buffer);
return( groupname );
}
Expand Down
3 changes: 1 addition & 2 deletions fontforge/namelist.c
Expand Up @@ -680,13 +680,12 @@ return( false );
}

void LoadNamelistDir(char *dir) {
char prefdir[1024];
DIR *diro;
struct dirent *ent;
char buffer[1025];

if ( dir == NULL )
dir = getPfaEditDir(prefdir);
dir = getFontForgeUserDir(Config);
if ( dir == NULL )
return;

Expand Down
4 changes: 2 additions & 2 deletions fontforge/noprefs.c
Expand Up @@ -440,9 +440,9 @@ static char *getPfaEditPrefs(void) {

if ( prefs!=NULL )
return( prefs );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/prefs", getPfaEditDir(buffer));
sprintf(buffer,"%s/prefs", getFontForgeUserDir(Config));
prefs = copy(buffer);
return( prefs );
}
Expand Down
4 changes: 2 additions & 2 deletions fontforge/oflib.c
Expand Up @@ -544,9 +544,9 @@ static char *getOFLibDir(void) {

if ( oflibdir!=NULL )
return( oflibdir );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/OFLib", getPfaEditDir(buffer));
sprintf(buffer,"%s/OFLib", getFontForgeUserDir(Config));
if ( access(buffer,F_OK)==-1 )
if ( GFileMkDir(buffer)==-1 )
return( NULL );
Expand Down
6 changes: 3 additions & 3 deletions fontforge/prefs.c
Expand Up @@ -781,9 +781,9 @@ static char *getPfaEditPrefs(void) {

if ( prefs!=NULL )
return( prefs );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/prefs", getPfaEditDir(buffer));
sprintf(buffer,"%s/prefs", getFontForgeUserDir(Config));
prefs = copy(buffer);
return( prefs );
}
Expand Down Expand Up @@ -2651,7 +2651,7 @@ void RecentFilesRemember(char *filename) {
void LastFonts_Save(void) {
FontView *fv, *next;
char buffer[1024];
char *ffdir = getPfaEditDir(buffer);
char *ffdir = getFontForgeUserDir(Config);
FILE *preserve = NULL;

if ( ffdir ) {
Expand Down
2 changes: 1 addition & 1 deletion fontforge/python.c
Expand Up @@ -18711,7 +18711,7 @@ static struct string_list *default_pyinit_dirs(void) {
snprintf(subdir, sizeof(subdir), "python%d", PY_MAJOR_VERSION);

sharedir = getFontForgeShareDir();
userdir = getPfaEditDir(buffer);
userdir = getFontForgeUserDir(Config);

if ( sharedir!=NULL ) {
snprintf(buffer,sizeof(buffer),"%s/%s",sharedir,subdir);
Expand Down
4 changes: 2 additions & 2 deletions fontforge/start.c
Expand Up @@ -85,8 +85,8 @@ static void initlibltdl(void) {

if (!plugins_are_initialized()) {
init_plugins();
if (getPfaEditDir(buffer)!=NULL ) {
strcpy(buffer,getPfaEditDir(buffer));
if (getFontForgeUserDir(Config)!=NULL ) {
strcpy(buffer,getFontForgeUserDir(Config));
strcat(buffer,"/plugins");
lt_dladdsearchdir(strdup(buffer));
}
Expand Down
8 changes: 4 additions & 4 deletions fontforge/startui.c
Expand Up @@ -657,7 +657,7 @@ static void AddR(char *program_name, char *window_name, char *cmndline_val) {

static int ReopenLastFonts(void) {
char buffer[1024];
char *ffdir = getPfaEditDir(buffer);
char *ffdir = getFontForgeUserDir(Config);
FILE *old;
int any = 0;

Expand Down Expand Up @@ -777,12 +777,12 @@ static void ffensuredir( const char* basedir, const char* dirname, mode_t mode )
}

static void ensureDotFontForgeIsSetup() {
char *basedir = GFileGetHomeDir();
char *basedir = getFontForgeUserDir(Config);
if ( !basedir ) {
return;
}
ffensuredir( basedir, ".FontForge", S_IRWXU );
ffensuredir( basedir, ".FontForge/python", S_IRWXU );
ffensuredir( basedir, "", S_IRWXU );
ffensuredir( basedir, "python", S_IRWXU );
}

static void DoAutoRecoveryPostRecover_PromptUserGraphically(SplineFont *sf)
Expand Down
6 changes: 2 additions & 4 deletions gdraw/hotkeys.c
Expand Up @@ -41,8 +41,6 @@

static int hotkeySystemCanUseMacCommand = 0;

extern char *getPfaEditDir(char *buffer);

struct dlistnode* hotkeys = 0;

static char* hotkeyGetWindowTypeString( Hotkey* hk )
Expand Down Expand Up @@ -159,14 +157,14 @@ static char *getHotkeyFilename( char* extension ) {
char *ret=NULL;
char buffer[1025];

if ( getPfaEditDir(buffer)==NULL ) {
if ( getFontForgeUserDir(Config)==NULL ) {
fprintf(stderr,_("Can not work out where your hotkey definition file is!\n"));
return( NULL );
}
if( !extension )
extension = "";

sprintf(buffer,"%s/hotkeys%s", getPfaEditDir(buffer), extension);
sprintf(buffer,"%s/hotkeys%s", getFontForgeUserDir(Config), extension);
ret = copy(buffer);
return( ret );
}
Expand Down
4 changes: 2 additions & 2 deletions gtkui/fontview.c
Expand Up @@ -2944,7 +2944,7 @@ void FontViewMenu_SaveNamelist(GtkMenuItem *menuitem, gpointer user_data) {
SplineChar *sc;
int i;

snprintf(buffer, sizeof(buffer),"%s/%s.nam", getPfaEditDir(buffer), fv->b.sf->fontname );
snprintf(buffer, sizeof(buffer),"%s/%s.nam", getFontForgeUserDir(Config), fv->b.sf->fontname );
temp = g_filename_to_utf8(buffer,-1,&read,&written,NULL);
filename = gwwv_saveas_filename(_("Save Namelist of font"), temp,"*.nam");
free(temp);
Expand Down Expand Up @@ -2988,7 +2988,7 @@ return; /* Cancelled */
pt = temp;
else
++pt;
snprintf(buffer,sizeof(buffer),"%s/%s", getPfaEditDir(buffer), pt);
snprintf(buffer,sizeof(buffer),"%s/%s", getFontForgeUserDir(Config), pt);
if ( access(buffer,F_OK)==0 ) {
buts[0] = _("_Replace");
buts[1] = GTK_STOCK_CANCEL;
Expand Down
4 changes: 2 additions & 2 deletions gtkui/prefs.c
Expand Up @@ -526,9 +526,9 @@ static char *getPfaEditPrefs(void) {

if ( prefs!=NULL )
return( prefs );
if ( getPfaEditDir(buffer)==NULL )
if ( getFontForgeUserDir(Config)==NULL )
return( NULL );
sprintf(buffer,"%s/prefs", getPfaEditDir(buffer));
sprintf(buffer,"%s/prefs", getFontForgeUserDir(Config));
prefs = copy(buffer);
return( prefs );
}
Expand Down
88 changes: 88 additions & 0 deletions gutils/fsys.c
Expand Up @@ -70,6 +70,25 @@ static void _u_backslash_to_slash(unichar_t* c){
}
#endif

char *smprintf(char *fmt, ...) {
va_list fmtargs;
char *ret;
int len;

va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if (ret == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}

va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}

char *GFileGetHomeDir(void) {
#if defined(__MINGW32__)
Expand Down Expand Up @@ -763,6 +782,75 @@ return( NULL );
return( editdir );
}

char *getUserHomeDir(void) {
#if defined(__MINGW32__)
char* dir = getenv("APPDATA");
if( dir==NULL )
dir = getenv("USERPROFILE");
if( dir!=NULL ) {
_backslash_to_slash(buffer);
return buffer;
}
return NULL;
#else
int uid;
struct passwd *pw;
char *home = getenv("HOME");

if( home!=NULL )
return home;

uid = getuid();
while( (pw=getpwent())!=NULL ) {
if ( pw->pw_uid==uid ) {
home = pw->pw_dir;
endpwent();
return home;
}
}
endpwent();
return NULL;
#endif
}

char *getFontForgeUserDir(int dir) {
char *def, *home, *xdg;
char *buf = NULL;

/* find home directory first, it is needed if any of the xdg env vars are
* not set */
if (!(home = getUserHomeDir())) {
fprintf(stderr, "%s\n", "cannot find home directory");
exit(1);
}
#if defined(__MINGW32__)
return home;
#else
switch(dir) {
case Cache:
xdg = getenv("XDG_CACHE_HOME");
def = ".cache";
break;
case Config:
xdg = getenv("XDG_CONFIG_HOME");
def = ".config";
break;
case Data:
xdg = getenv("XDG_DATA_HOME");
def = ".local/share";
break;
default:
fprintf(stderr, "%s\n", "invalid input");
exit(1);
}
if(xdg != NULL)
buf = smprintf("%s/fontforge", xdg);
else
buf = smprintf("%s/%s/fontforge", home, def);
return buf;
#endif
}

long GFileGetSize(char *name) {
/* Get the binary file size for file 'name'. Return -1 if error. */
struct stat buf;
Expand Down
7 changes: 7 additions & 0 deletions inc/gfile.h
Expand Up @@ -27,6 +27,11 @@
#ifndef _GFILE_H
#define _GFILE_H

/* home directories for fontforge */
enum { Cache, Config, Data };

char *smprintf(char *fmt, ...);

extern char* GFileGetHomeDir(void);
extern unichar_t* u_GFileGetHomeDir(void);

Expand Down Expand Up @@ -74,6 +79,8 @@ extern char *getLocaleDir(void);
extern char *getPixmapDir(void);
extern char *getHelpDir(void);
extern char *getDotFontForgeDir(void);
extern char *getUserHomeDir(void);
extern char *getFontForgeUserDir(int dir);
extern char *getTempDir(void);

/**
Expand Down

0 comments on commit e4c2cc7

Please sign in to comment.