rename `NativeString` to `CString`
[nit.git] / lib / xdg_basedir / xdg_basedir.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Services for using the XDG Base Directory specification
18 #
19 # This is a low-level module, it is meant to be wrapped by higher level services.
20 #
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")
25
26 in "C Header" `{
27 #include <basedir.h>
28 `}
29
30 # Handle to a local cache of XDG base directories
31 extern class XdgBasedir `{ xdgHandle* `}
32
33 # Initialize a handle to an XDG data cache and initialize the cache.
34 new `{ return xdgInitHandle(NULL); `}
35
36 # Has this instance been correctly initialized?
37 fun is_valid: Bool do return not address_is_null
38
39 # Wipe handle of XDG data cache.
40 fun destroy `{ xdgWipeHandle(self); `}
41
42 # Update the data cache.
43 #
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.
47 #
48 # Returns `true` if the update was successful.
49 fun update: Bool `{ return xdgUpdateData(self); `}
50
51 # Base directory for user specific data files.
52 fun data_home: String import CString.to_s `{
53 return CString_to_s((char*)xdgDataHome(self));
54 `}
55
56 # Base directory for user specific configuration files.
57 fun config_home: String import CString.to_s `{
58 return CString_to_s((char*)xdgConfigHome(self));
59 `}
60
61 # Base directory for user specific non-essential data files.
62 fun cache_home: String import CString.to_s `{
63 return CString_to_s((char*)xdgCacheHome(self));
64 `}
65
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
69
70 private fun native_data_dirs: ConstPointer `{
71 return xdgDataDirectories(self);
72 `}
73
74 # Preference-ordered set of base directories to search for data files
75 # with $XDG_DATA_HOME prepended.
76 #
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]
80 do
81 return native_searchable_data_dirs.to_string_array
82 end
83
84 private fun native_searchable_data_dirs: ConstPointer `{
85 return xdgSearchableDataDirectories(self);
86 `}
87
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
91
92 private fun native_config_dirs: ConstPointer `{
93 return xdgConfigDirectories(self);
94 `}
95
96 # Preference-ordered set of base directories to search for configuration
97 # files with $XDG_CONFIG_HOME prepended.
98 #
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]
102 do
103 return native_searchable_config_dirs.to_string_array
104 end
105
106 private fun native_searchable_config_dirs: ConstPointer `{
107 return xdgSearchableConfigDirectories(self);
108 `}
109 end
110
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, CString.to_s `{
115 char **strings = (char**)self;
116
117 Array_of_String aos = new_Array_of_String();
118 int p = 0;
119 while (strings[p] != NULL) {
120 Array_of_String_add(aos, CString_to_s((char*)strings[p]));
121 p ++;
122 }
123
124 return aos;
125 `}
126 end
127
128 # Version of XDG Base Directory specification implemented in this library.
129 fun xdg_basedir_spec: Float `{ return XDG_BASEDIR_SPEC; `}
130
131 # Get an instance of XdgBasedir
132 fun xdg_basedir: XdgBasedir do return once new XdgBasedir