/ src / compat.c
compat.c
  1  /*
  2      Gpredict: Real-time satellite tracking and orbit prediction program
  3  
  4      Copyright (C)  2001-2017  Alexandru Csete, OZ9AEC.
  5  
  6      Comments, questions and bugreports should be submitted via
  7      http://sourceforge.net/projects/gpredict/
  8      More details can be found at the project home page:
  9  
 10              http://gpredict.oz9aec.net/
 11   
 12      This program is free software; you can redistribute it and/or modify
 13      it under the terms of the GNU General Public License as published by
 14      the Free Software Foundation; either version 2 of the License, or
 15      (at your option) any later version.
 16    
 17      This program is distributed in the hope that it will be useful,
 18      but WITHOUT ANY WARRANTY; without even the implied warranty of
 19      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20      GNU General Public License for more details.
 21    
 22      You should have received a copy of the GNU General Public License
 23      along with this program; if not, visit http://www.fsf.org/
 24  */
 25  #ifdef HAVE_CONFIG_H
 26  #include <build-config.h>
 27  #endif
 28  #include <glib.h>
 29  #include <stdlib.h>
 30  #include <locale.h>
 31  #include "compat.h"
 32  
 33  /**
 34   * Get data directory.
 35   *
 36   * On linux it corresponds to the PACKAGE_DATA_DIR macro defined in build-config.h
 37   * The function returns a newly allocated gchar * which must be free when
 38   * it is no longer needed.
 39   */
 40  gchar          *get_data_dir()
 41  {
 42      gchar          *dir = NULL;
 43  
 44  #ifdef G_OS_UNIX
 45      char* data_dir = getenv("GPREDICT_DATA_DIR");
 46      dir = g_strconcat(data_dir ? data_dir : PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S, "data", NULL);
 47  #else
 48  #ifdef G_OS_WIN32
 49      gchar          *buff =
 50          g_win32_get_package_installation_directory_of_module(NULL);
 51  
 52      dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
 53                        "share", G_DIR_SEPARATOR_S, "gpredict",
 54                        G_DIR_SEPARATOR_S, "data", NULL);
 55      g_free(buff);
 56  #endif
 57  #endif
 58  
 59      return dir;
 60  }
 61  
 62  /**
 63   * Get absolute file name of a data file.
 64   *
 65   * This function returns the absolute file name of a data file. It is intended to
 66   * be a one-line filename constructor.
 67   * The returned gchar * should be freed when no longer needed.
 68   */
 69  gchar          *data_file_name(const gchar * data)
 70  {
 71      gchar          *filename = NULL;
 72      gchar          *buff;
 73  
 74      buff = get_data_dir();
 75      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, data, NULL);
 76      g_free(buff);
 77  
 78      return filename;
 79  }
 80  
 81  /**
 82   * Get maps directory.
 83   *
 84   * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/maps
 85   * The function returns a newly allocated gchar * which must be free when
 86   * it is no longer needed.
 87   */
 88  gchar          *get_maps_dir()
 89  {
 90      gchar          *dir = NULL;
 91  
 92  #ifdef G_OS_UNIX
 93      dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "maps", NULL);
 94  #else
 95  #ifdef G_OS_WIN32
 96      gchar          *buff =
 97          g_win32_get_package_installation_directory_of_module(NULL);
 98  
 99      dir = g_strconcat(buff, G_DIR_SEPARATOR_S, "share", G_DIR_SEPARATOR_S,
100                        /* FIXME */
101                        "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
102                        G_DIR_SEPARATOR_S, "maps", NULL);
103      g_free(buff);
104  #endif
105  #endif
106  
107      return dir;
108  }
109  
110  /**
111   * Get absolute file name of a map file.
112   *
113   * This function returns the absolute file name of a map file. It is intended to
114   * be a one-line filename constructor.
115   * The returned gchar * should be freed when no longer needed.
116   */
117  gchar          *map_file_name(const gchar * map)
118  {
119      gchar          *filename = NULL;
120      gchar          *buff;
121  
122      buff = get_maps_dir();
123      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, map, NULL);
124      g_free(buff);
125  
126      return filename;
127  }
128  
129  /**
130   * Get logo directory.
131   *
132   * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/logos
133   * The function returns a newly allocated gchar * which must be free when
134   * it is no longer needed.
135   */
136  gchar          *get_logo_dir()
137  {
138      gchar          *dir = NULL;
139  
140  #ifdef G_OS_UNIX
141      dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "logos", NULL);
142  #else
143  #ifdef G_OS_WIN32
144      gchar          *buff =
145          g_win32_get_package_installation_directory_of_module(NULL);
146  
147      dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
148                        "share", G_DIR_SEPARATOR_S,
149                        "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
150                        G_DIR_SEPARATOR_S, "logos", NULL);
151      g_free(buff);
152  #endif
153  #endif
154  
155      return dir;
156  }
157  
158  /**
159   * Get icon directory.
160   *
161   * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/icons
162   * The function returns a newly allocated gchar * which must be free when
163   * it is no longer needed.
164   */
165  gchar          *get_icon_dir()
166  {
167      gchar          *dir = NULL;
168  
169  #ifdef G_OS_UNIX
170      dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "icons", NULL);
171  #else
172  #ifdef G_OS_WIN32
173      gchar          *buff =
174          g_win32_get_package_installation_directory_of_module(NULL);
175  
176      dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
177                        "share", G_DIR_SEPARATOR_S,
178                        "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
179                        G_DIR_SEPARATOR_S, "icons", NULL);
180      g_free(buff);
181  #endif
182  #endif
183  
184      return dir;
185  }
186  
187  /**
188   * Get absolute file name of a logo file.
189   *
190   * This function returns the absolute file name of a logo file. It is intended
191   * to be a one-line filename constructor.
192   * The returned gchar * should be freed when no longer needed.
193   */
194  gchar          *logo_file_name(const gchar * logo)
195  {
196      gchar          *filename = NULL;
197      gchar          *buff;
198  
199      buff = get_logo_dir();
200      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, logo, NULL);
201      g_free(buff);
202  
203      return filename;
204  }
205  
206  /**
207   * Get absolute file name of an icon file.
208   *
209   * This function returns the absolute file name of an icon file. It is intended to
210   * be a one-line filename constructor.
211   * The returned gchar * should be freed when no longer needed.
212   */
213  gchar          *icon_file_name(const gchar * icon)
214  {
215      gchar          *filename = NULL;
216      gchar          *buff;
217  
218      buff = get_icon_dir();
219      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, icon, NULL);
220      g_free(buff);
221  
222      return filename;
223  }
224  
225  /**
226   * Get the old user configuration directory.
227   *
228   * On linux it corresponds to $HOME/.gpredict2
229   * The function returns a newly allocated gchar * which must be free when
230   * it is no longer needed.
231   */
232  gchar          *get_old_conf_dir(void)
233  {
234      gchar          *dir;
235  
236      dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, ".gpredict2", NULL);
237      return dir;
238  }
239  
240  /**
241   * Get user configuration directory.
242   *
243   * Linux: $HOME/.config/Gpredict
244   * Windows: C:\Users\username\Gpredict
245   * Mac OS X: /home/username/Library/Application Support/Gpredict
246   *
247   * The function returns a newly allocated gchar * which must be free when
248   * it is no longer needed.
249   */
250  gchar          *get_user_conf_dir(void)
251  {
252      gchar          *dir = NULL;
253  
254  #ifdef G_OS_UNIX
255      dir =
256          g_strconcat(g_get_user_config_dir(), G_DIR_SEPARATOR_S, "Gpredict",
257                      NULL);
258  #endif
259  #ifdef G_OS_WIN32
260      // FIXME: does this work?
261      dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, "Gpredict", NULL);
262  #endif
263  /* see gtk-osx.sourceforge.net -> Integration */
264  #ifdef MAC_INTEGRATION
265      dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
266                        "Library", G_DIR_SEPARATOR_S,
267                        "Application Support", G_DIR_SEPARATOR_S, "Gpredict",
268                        NULL);
269  #endif
270  
271      return dir;
272  }
273  
274  /** Get USER_CONF_DIR/modules */
275  gchar          *get_modules_dir(void)
276  {
277      gchar          *confdir;
278      gchar          *dir;
279  
280      confdir = get_user_conf_dir();
281      dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "modules", NULL);
282      g_free(confdir);
283  
284      return dir;
285  }
286  
287  /** Get USER_CONF_DIR/satdata */
288  gchar          *get_satdata_dir(void)
289  {
290      gchar          *confdir;
291      gchar          *dir;
292  
293      confdir = get_user_conf_dir();
294      dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "satdata", NULL);
295      g_free(confdir);
296  
297      return dir;
298  }
299  
300  /** Get USER_CONF_DIR/trsp */
301  gchar          *get_trsp_dir(void)
302  {
303      gchar          *confdir;
304      gchar          *dir;
305  
306      confdir = get_user_conf_dir();
307      dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "trsp", NULL);
308      g_free(confdir);
309  
310      return dir;
311  }
312  
313  /** Get USER_CONF_DIR/hwconf */
314  gchar          *get_hwconf_dir(void)
315  {
316      gchar          *confdir;
317      gchar          *dir;
318  
319      confdir = get_user_conf_dir();
320      dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "hwconf", NULL);
321      g_free(confdir);
322  
323      return dir;
324  }
325  
326  /** Get full path of a .sat or .cat file */
327  gchar          *sat_file_name(const gchar * satfile)
328  {
329      gchar          *filename = NULL;
330      gchar          *buff;
331  
332      buff = get_satdata_dir();
333      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, satfile, NULL);
334      g_free(buff);
335  
336      return filename;
337  }
338  
339  /** Build satellite file path from catnum (integer) */
340  gchar          *sat_file_name_from_catnum(guint catnum)
341  {
342      gchar          *filename;
343      gchar          *buff;
344      gchar          *dir;
345  
346      buff = g_strdup_printf("%d.sat", catnum);
347      dir = get_satdata_dir();
348  
349      filename = g_strconcat(dir, G_DIR_SEPARATOR_S, buff, NULL);
350  
351      g_free(buff);
352      g_free(dir);
353  
354      return filename;
355  }
356  
357  /** Build satellite file path from catnum (string) */
358  gchar          *sat_file_name_from_catnum_s(gchar * catnum)
359  {
360      gchar          *filename;
361      gchar          *buff;
362      gchar          *dir;
363  
364      buff = g_strdup_printf("%s.sat", catnum);
365      dir = get_satdata_dir();
366  
367      filename = g_strconcat(dir, G_DIR_SEPARATOR_S, buff, NULL);
368  
369      g_free(buff);
370      g_free(dir);
371  
372      return filename;
373  }
374  
375  /** Get full path of a .trsp file */
376  gchar          *trsp_file_name(const gchar * trspfile)
377  {
378      gchar          *filename = NULL;
379      gchar          *buff;
380  
381      buff = get_trsp_dir();
382      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, trspfile, NULL);
383      g_free(buff);
384  
385      return filename;
386  }
387  
388  /** Get full path of a .rig or .rot file */
389  gchar          *hw_file_name(const gchar * hwfile)
390  {
391      gchar          *filename = NULL;
392      gchar          *buff;
393  
394      buff = get_hwconf_dir();
395      filename = g_strconcat(buff, G_DIR_SEPARATOR_S, hwfile, NULL);
396      g_free(buff);
397  
398      return filename;
399  }
400  
401  gchar const* get_locale_thousands_sep()
402  {
403  	struct lconv *locale;
404  
405  	locale = localeconv();
406  
407  	if (!locale) {
408  		return NULL;
409  	}
410  
411  	return locale->thousands_sep;
412  }