1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Services for using the XDG Base Directory specification
19 # This is a low-level module, it is meant to be wrapped by higher level services.
21 # For more information, refer to the documentation of the `libxdg-basedir`
22 # project or the official specification at:
23 # http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
24 module xdg_basedir
is pkgconfig
("libxdg-basedir")
30 # Handle to a local cache of XDG base directories
31 extern class XdgBasedir `{ xdgHandle* `}
33 # Initialize a handle to an XDG data cache and initialize the cache.
34 new `{ return xdgInitHandle(NULL); `}
36 # Has this instance been correctly initialized?
37 fun is_valid
: Bool do return not address_is_null
39 # Wipe handle of XDG data cache.
40 fun destroy
`{ xdgWipeHandle(self); `}
42 # Update the data cache.
44 # This should not be done frequently as it reallocates the cache.
45 # Even if updating the cache fails the handle remains valid and can
46 # be used to access XDG data as it was before `update
` was called.
48 # Returns `true` if the update was successful.
49 fun update: Bool `{ return xdgUpdateData(self); `}
51 # Base directory for user specific data files.
52 fun data_home
: String import NativeString.to_s
`{
53 return NativeString_to_s((char*)xdgDataHome(self));
56 # Base directory for user specific configuration files.
57 fun config_home
: String import NativeString.to_s
`{
58 return NativeString_to_s((char*)xdgConfigHome(self));
61 # Base directory for user specific non-essential data files.
62 fun cache_home
: String import NativeString.to_s
`{
63 return NativeString_to_s((char*)xdgCacheHome(self));
66 # Preference-ordered set of base directories to search for data files
67 # in addition to the $XDG_DATA_HOME base directory.
68 fun data_dirs
: Array[String] do return native_data_dirs
.to_string_array
70 private fun native_data_dirs
: ConstPointer `{
71 return xdgDataDirectories(self);
74 # Preference-ordered set of base directories to search for data files
75 # with $XDG_DATA_HOME prepended.
77 # The base directory defined by $XDG_DATA_HOME is considered more
78 # important than any of the base directories defined by $XDG_DATA_DIRS.
79 fun searchable_data_dirs
: Array[String]
81 return native_searchable_data_dirs
.to_string_array
84 private fun native_searchable_data_dirs
: ConstPointer `{
85 return xdgSearchableDataDirectories(self);
88 # Preference-ordered set of base directories to search for configuration
89 # files in addition to the $XDG_CONFIG_HOME base directory.
90 fun config_dirs
: Array[String] do return native_config_dirs
.to_string_array
92 private fun native_config_dirs
: ConstPointer `{
93 return xdgConfigDirectories(self);
96 # Preference-ordered set of base directories to search for configuration
97 # files with $XDG_CONFIG_HOME prepended.
99 # The base directory defined by $XDG_CONFIG_HOME is considered more
100 # important than any of the base directories defined by $XDG_CONFIG_DIRS.
101 fun searchable_config_dirs
: Array[String]
103 return native_searchable_config_dirs
.to_string_array
106 private fun native_searchable_config_dirs
: ConstPointer `{
107 return xdgSearchableConfigDirectories(self);
111 private extern class ConstPointer `{ const void * `}
112 # Convert a C `char
**` to a Nit `Array[String]`
113 fun to_string_array: Array[String]
114 import Array[String], Array[String].add, NativeString.to_s `{
115 char
**strings
= (char
**)self;
117 Array_of_String aos
= new_Array_of_String
();
119 while (strings
[p
] != NULL) {
120 Array_of_String_add(aos
, NativeString_to_s((char
*)strings
[p
]));
128 # Version of XDG Base Directory specification implemented in this library.
129 fun xdg_basedir_spec: Float `{ return XDG_BASEDIR_SPEC; `}
131 # Get an instance of XdgBasedir
132 fun xdg_basedir
: XdgBasedir do return once
new XdgBasedir