1 
2 #ifndef H2C_FILESYSTEM_H
3 #define H2C_FILESYSTEM_H
4 
5 #include <hydrogen/object.h>
6 #include <QtCore/QString>
7 
8 namespace H2Core
9 {
10 
11 /**
12  * Filesystem is a thin layer over QDir, QFile and QFileInfo
13  */
14 class Filesystem : public H2Core::Object
15 {
16 		H2_OBJECT
17 	public:
18 		/** flags available for check_permissions() */
19 		enum file_perms {
20 			is_dir =0x01,
21 			is_file=0x02,
22 			is_readable=0x04,
23 			is_writable=0x08,
24 			is_executable=0x10
25 		};
26 		static const QString songs_ext;
27 		static const QString scripts_ext;
28 		static const QString patterns_ext;
29 		static const QString playlist_ext;
30 		static const QString songs_filter_name;
31 		static const QString scripts_filter_name;
32 		static const QString patterns_filter_name;
33 		static const QString playlists_filter_name;
34 
35 		/**
36 		 * check user and system filesystem usability
37 		 * \param logger is a pointer to the logger instance which will be used
38 		 * \param sys_path an alternate system data path
39 		 */
40 		static bool bootstrap( Logger* logger, const QString& sys_path=nullptr );
41 
42 		/** returns system data path */
43 		static QString sys_data_path();
44 		/** returns user data path */
45 		static QString usr_data_path();
46 
47 		/** returns user ladspa paths */
48 		static QStringList ladspa_paths();
49 
50 		/** returns system config path */
51 		static QString sys_config_path();
52 		/** returns user config path */
53 		static QString usr_config_path();
54 		/** returns system empty sample file path */
55 		static QString empty_sample_path();
56 		/** returns system empty song file path */
57 		static QString empty_song_path();
58 		/** returns untitled song file name */
59 		static QString untitled_song_file_name();
60 		/** Returns a string containing the path to the
61 		    _click.wav_ file used in the metronome.
62 		  *
63 		  * It is a concatenation of #__sys_data_path and
64 		  * #CLICK_SAMPLE.
65 		  */
66 		static QString click_file_path();
67 		/** returns click file path from user directory if exists, otherwise from system */
68 		static QString usr_click_file_path();
69 		/** returns the path to the drumkit XSD (xml schema definition) file */
70 		static QString drumkit_xsd_path( );
71 		/** returns the path to the pattern XSD (xml schema definition) file */
72 		static QString pattern_xsd_path( );
73 		/** returns the path to the playlist pattern XSD (xml schema definition) file */
74 		static QString playlist_xsd_path( );
75 
76 		/** returns gui image path */
77 		static QString img_dir();
78 		/** returns documentation path */
79 		static QString doc_dir();
80 		/** returns internationalization path */
81 		static QString i18n_dir();
82 		/** returns user scripts path */
83 		static QString scripts_dir();
84 		/** returns user songs path */
85 		static QString songs_dir();
86 		/** returns user song path, add file extension */
87 		static QString song_path( const QString& sg_name );
88 		/** returns user patterns path */
89 		static QString patterns_dir();
90 		/** returns user patterns path for a specific drumkit */
91 		static QString patterns_dir( const QString& dk_name );
92 		/** returns user patterns path, add file extension*/
93 		static QString pattern_path( const QString& dk_name, const QString& p_name );
94 		/** returns user plugins path */
95 		static QString plugins_dir();
96 		/** returns system drumkits path */
97 		static QString sys_drumkits_dir();
98 		/** returns user drumkits path */
99 		static QString usr_drumkits_dir();
100 		/** returns user playlist path */
101 		static QString playlists_dir();
102 		/** returns user playlist path, add file extension */
103 		static QString playlist_path( const QString& pl_name );
104 		/** returns untitled playlist file name */
105 		static QString untitled_playlist_file_name();
106 		/** returns user cache path */
107 		static QString cache_dir();
108 		/** returns user repository cache path */
109 		static QString repositories_cache_dir();
110 		/** returns system demos path */
111 		static QString demos_dir();
112 		/** returns system xsd path */
113 		static QString xsd_dir();
114 		/** returns temp path */
115 		static QString tmp_dir();
116 		/**
117 		 * touch a temporary file under tmp_dir() and return it's path.
118 		 * if base has a suffix it will be preserved, spaces will be replaced by underscores.
119 		 * \param base part of the path
120 		 */
121 		static QString tmp_file_path( const QString& base );
122 
123 		/* DRUMKIT */
124 		/** Returns the basename if the given path is under an existing user or system drumkit path, otherwise the given fname */
125 		static QString prepare_sample_path( const QString& fname );
126 		/** Checks if the given filepath is under an existing user or system drumkit path, not the existence of the file */
127 		static bool file_is_under_drumkit( const QString& fname);
128 		/** Returns the index of the basename if the given path is under an existing user or system drumkit path, otherwise -1 */
129 		static int get_basename_idx_under_drumkit( const QString& fname);
130 		/** returns list of usable system drumkits ( see Filesystem::drumkit_list ) */
131 		static QStringList sys_drumkit_list( );
132 		/** returns list of usable user drumkits ( see Filesystem::drumkit_list ) */
133 		static QStringList usr_drumkit_list( );
134 		/**
135 		 * returns true if the drumkit exists within usable system or user drumkits
136 		 * \param dk_name the drumkit name
137 		 */
138 		static bool drumkit_exists( const QString& dk_name );
139 		/**
140 		 * returns path for a drumkit within user drumkit path
141 		 * \param dk_name the drumkit name
142 		 */
143 		static QString drumkit_usr_path( const QString& dk_name );
144 		/**
145 		 * returns path for a drumkit searching within user then system drumkits
146 		 * \param dk_name the drumkit name
147 		 */
148 		static QString drumkit_path_search( const QString& dk_name );
149 		/**
150 		 * returns the directory holding the named drumkit searching within user then system drumkits
151 		 * \param dk_name the drumkit name
152 		 */
153 		static QString drumkit_dir_search( const QString& dk_name );
154 		/**
155 		 * returns true if the path contains a usable drumkit
156 		 * \param dk_path the root drumkit location
157 		 */
158 		static bool drumkit_valid( const QString& dk_path );
159 		/**
160 		 * returns the path to the xml file within a supposed drumkit path
161 		 * \param dk_path the path to the drumkit
162 		 */
163 		static QString drumkit_file( const QString& dk_path );
164 
165 		/* PATTERNS */
166 		/**
167 		 * returns a list of existing drumkit sub dir into the patterns directory
168 		 */
169 		static QStringList pattern_drumkits();
170 		/**
171 		 * returns a list of existing patterns
172 		 */
173 		static QStringList pattern_list();
174 		/**
175 		 * returns a list of existing patterns
176 		 * \param path the path to look for patterns in
177 		 */
178 		static QStringList pattern_list( const QString& path );
179 
180 		/* SONGS */
181 		/** returns a list of existing songs */
182 		static QStringList song_list( );
183 		/** returns a list of existing songs, excluding the autosaved one */
184 		static QStringList song_list_cleared( );
185 		/**
186 		 * returns true if the song file exists
187 		 * \param sg_name the song name
188 		 */
189 		static bool song_exists( const QString& sg_name );
190 
191 		/** send current settings information to logger with INFO severity */
192 		static void info();
193 
194 		/* PLAYLISTS */
195 		/** returns a list of existing playlists */
196 		static QStringList playlist_list( );
197 
198 		/**
199 		 * returns true if the given path is an existing regular file
200 		 * \param path the path to the file to check
201 		 * \param silent output not messages if set to true
202 		 */
203 		static bool file_exists( const QString& path, bool silent=false );
204 		/**
205 		 * returns true if the given path is an existing readable regular file
206 		 * \param path the path to the file to check
207 		 * \param silent output not messages if set to true
208 		 */
209 		static bool file_readable( const QString& path, bool silent=false );
210 		/**
211 		 * returns true if the given path is a possibly writable file (may exist or not)
212 		 * \param path the path to the file to check
213 		 * \param silent output not messages if set to true
214 		 */
215 		static bool file_writable( const QString& path, bool silent=false );
216 		/**
217 		 * returns true if the given path is an existing executable regular file
218 		 * \param path the path to the file to check
219 		 * \param silent output not messages if set to true
220 		 */
221 		static bool file_executable( const QString& path, bool silent=false );
222 		/**
223 		 * returns true if the given path is a readable regular directory
224 		 * \param path the path to the file to check
225 		 * \param silent output not messages if set to true
226 		 */
227 		static bool dir_readable( const QString& path, bool silent=false );
228 		/**
229 		 * returns true if the given path is a writable regular directory
230 		 * \param path the path to the file to check
231 		 * \param silent output not messages if set to true
232 		 */
233 		static bool dir_writable( const QString& path, bool silent=false );
234 		/**
235 		 * returns true if the path is a readable and writable regular directory, create if it not exists
236 		 * \param path the path to the file to check
237 		 * \param create will try to create path if not exists and set to true
238 		 * \param silent output not messages if set to true
239 		 */
240 		static bool path_usable( const QString& path, bool create=true, bool silent=false );
241 		/**
242 		 * writes to a file
243 		 * \param dst the destination path
244 		 * \param content then string to write
245 		 */
246 		static bool write_to_file( const QString& dst, const QString& content );
247 		/**
248 		 * copy a source file to a destination
249 		 * \param src source file path
250 		 * \param dst destination file path
251 		 * \param overwrite allow to overwrite an existing file if set to true
252 		 */
253 		static bool file_copy( const QString& src, const QString& dst, bool overwrite=false );
254 		/**
255 		 * remove a path
256 		 * \param path the path to be removed
257 		 * \param recursive perform recursive removal if set to true
258 		 */
259 		static bool rm( const QString& path, bool recursive=false );
260 		/**
261 		 * create a path
262 		 * \param path the path to the directory to be created
263 		 */
264 		static bool mkdir( const QString& path );
265 
266 	private:
267 		static Logger* __logger;                    ///< a pointer to the logger
268 		static bool check_sys_paths();              ///< returns true if the system path is consistent
269 		static bool check_usr_paths();              ///< returns true if the user path is consistent
270 		static bool rm_fr( const QString& path );   ///< recursively remove a path
271 
272 		/**
273 		 * \return a list of usable drumkits, which means having a readable drumkit.xml file
274 		 * \param path the path to search in for drumkits
275 		 */
276 		static QStringList drumkit_list( const QString& path );
277 		/**
278 		 * \return true if all the asked permissions are ok
279 		 * \param path the path to the file to check
280 		 * \param perms bit mask of file_perms
281 		 * \param silent output not messages if set to true
282 		 */
283 		static bool check_permissions( const QString& path, const int perms, bool silent );
284 
285 		/**
286 		 * Path to the system files set in Filesystem::bootstrap().
287 		 *
288 		 * If Q_OSMACX is set, it will be a concatenation of
289 		 * QCoreApplication::applicationDirPath() and
290 		 * "/../Resources/data/" (H2CORE_HAVE_BUNDLE defined)
291 		 * or "/data/" (else). If, instead, WIN32 is set, it
292 		 * is a concatenation of
293 		 * QCoreApplication::applicationDirPath() and
294 		 * "/data/". In case the application is neither run on
295 		 * Mac or Windows, it is set to a concatenation of
296 		 * H2_SYS_PATH and "/data/".
297 		 *
298 		 * If Filesystem::bootstrap() was called with the @a
299 		 * sys_path argument preset, it will overwrite all the
300 		 * choices above.
301 		 *
302 		 * Finally, if the variable doesn't point to a
303 		 * readable directory afterwards, it is set to a
304 		 * concatenation of
305 		 * QCoreApplication::applicationDirPath(), "/", and
306 		 * LOCAL_DATA_PATH.
307 		 */
308 		static QString __sys_data_path;     ///< the path to the system files
309 		static QString __usr_data_path;     ///< the path to the user files
310 		static QString __usr_cfg_path;      ///< the path to the user config file
311 		static QStringList __ladspa_paths;  ///< paths to laspa plugins
312 };
313 
314 };
315 
316 #endif  // H2C_FILESYSTEM_H
317 
318 /* vim: set softtabstop=4 noexpandtab: */
319