From 11729cdc0cc94b291e45bd1e08dc4e2c20c0674b Mon Sep 17 00:00:00 2001 From: James Westman Date: Mon, 28 Jan 2019 08:27:40 -0600 Subject: [PATCH] Fix g_get_user_special_dir() on macOS This uses newer methods that support more folders such as Downloads. The Objective-C code is in a separate file, gosxutils.m. Based on !85 by Patrick Griffis. --- glib/gosxutils.m | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ glib/gutils.c | 53 ++++--------------------------------------- glib/meson.build | 8 ++++++- 3 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 glib/gosxutils.m diff --git a/glib/gosxutils.m b/glib/gosxutils.m new file mode 100644 index 000000000..7a0d84d31 --- /dev/null +++ b/glib/gosxutils.m @@ -0,0 +1,58 @@ +/* GLIB - Library of useful routines for C programming + * Copyright (C) 2018-2019 Patrick Griffis, James Westman + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +#include "config.h" + +#ifndef HAVE_COCOA +#error "Can only build gutils-macos.m on MacOS" +#endif + +#include +#include "gutils.h" +#include "gstrfuncs.h" + +static gchar * +find_folder (NSSearchPathDirectory type) +{ + gchar *filename; + NSString *path; + NSArray *paths; + + paths = NSSearchPathForDirectoriesInDomains (type, NSUserDomainMask, YES); + path = [paths firstObject]; + if (path == nil) + { + return NULL; + } + + filename = g_strdup ([path UTF8String]); + + return filename; +} + +void +load_user_special_dirs_macos(gchar **table) +{ + table[G_USER_DIRECTORY_DESKTOP] = find_folder (NSDesktopDirectory); + table[G_USER_DIRECTORY_DOCUMENTS] = find_folder (NSDocumentDirectory); + table[G_USER_DIRECTORY_DOWNLOAD] = find_folder (NSDownloadsDirectory); + table[G_USER_DIRECTORY_MUSIC] = find_folder (NSMusicDirectory); + table[G_USER_DIRECTORY_PICTURES] = find_folder (NSPicturesDirectory); + table[G_USER_DIRECTORY_PUBLIC_SHARE] = find_folder (NSSharedPublicDirectory); + table[G_USER_DIRECTORY_TEMPLATES] = NULL; + table[G_USER_DIRECTORY_VIDEOS] = find_folder (NSMoviesDirectory); +} \ No newline at end of file diff --git a/glib/gutils.c b/glib/gutils.c index 6fe473785..2e2d45785 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -120,10 +120,6 @@ # include #endif -#ifdef HAVE_CARBON -#include -#endif - #ifdef HAVE_CODESET #include #endif @@ -1519,56 +1515,15 @@ g_get_user_runtime_dir (void) return user_runtime_dir; } -#ifdef HAVE_CARBON +#ifdef HAVE_COCOA -static gchar * -find_folder (OSType type) -{ - gchar *filename = NULL; - FSRef found; - - if (FSFindFolder (kUserDomain, type, kDontCreateFolder, &found) == noErr) - { - CFURLRef url = CFURLCreateFromFSRef (kCFAllocatorSystemDefault, &found); - - if (url) - { - CFStringRef path = CFURLCopyFileSystemPath (url, kCFURLPOSIXPathStyle); - - if (path) - { - filename = g_strdup (CFStringGetCStringPtr (path, kCFStringEncodingUTF8)); - - if (! filename) - { - filename = g_new0 (gchar, CFStringGetLength (path) * 3 + 1); - - CFStringGetCString (path, filename, - CFStringGetLength (path) * 3 + 1, - kCFStringEncodingUTF8); - } - - CFRelease (path); - } - - CFRelease (url); - } - } - - return filename; -} +/* Implemented in gutils-macos.m */ +void load_user_special_dirs_macos (gchar **table); static void load_user_special_dirs (void) { - g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = find_folder (kDesktopFolderType); - g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = find_folder (kDocumentsFolderType); - g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = find_folder (kDesktopFolderType); /* XXX correct ? */ - g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = find_folder (kMusicDocumentsFolderType); - g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = find_folder (kPictureDocumentsFolderType); - g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = NULL; - g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = NULL; - g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = find_folder (kMovieDocumentsFolderType); + load_user_special_dirs_macos (g_user_special_dirs); } #elif defined(G_OS_WIN32) diff --git a/glib/meson.build b/glib/meson.build index 512ab82d2..8350ea283 100644 --- a/glib/meson.build +++ b/glib/meson.build @@ -230,6 +230,10 @@ else platform_deps = [] endif +if host_system == 'darwin' + glib_sources += files('gosxutils.m') +endif + glib_sources += files('gthread-@0@.c'.format(threads_implementation)) if enable_dtrace @@ -254,6 +258,7 @@ else pcre_objects = [libpcre.extract_all_objects()] endif +glib_c_args = ['-DG_LOG_DOMAIN="GLib"', '-DGLIB_COMPILATION'] + pcre_static_args + glib_hidden_visibility_args libglib = library('glib-2.0', glib_dtrace_obj, glib_dtrace_hdr, sources : [deprecated_sources, glib_sources], @@ -266,7 +271,8 @@ libglib = library('glib-2.0', link_args : [noseh_link_args, glib_link_flags, win32_ldflags], include_directories : configinc, dependencies : pcre_deps + [thread_dep, libintl, librt] + libiconv + platform_deps, - c_args : ['-DG_LOG_DOMAIN="GLib"', '-DGLIB_COMPILATION'] + pcre_static_args + glib_hidden_visibility_args + c_args : glib_c_args, + objc_args : glib_c_args, ) libglib_dep = declare_dependency(