X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/3957ef85e98bb3d37a58ebae9cd84fc992967d34..9197052a703fd0e70beb0bb9f5726da2db3cfc73:/src/util.c diff --git a/src/util.c b/src/util.c index 732abbef..d8939f9f 100644 --- a/src/util.c +++ b/src/util.c @@ -1,12 +1,12 @@ /* * Viking - GPS data editor * Copyright (C) 2007, Guilhem Bonnefille - * Based on: - * Copyright (C) 2003-2007, Leandro A. F. Pereira + * Copyright (C) 2014, Rob Norris * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -17,6 +17,11 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ + /* + * Dependencies must be just on Glib + * see ui_utils for thing that depend on Gtk + * see vikutils for things that further depend on other Viking types + */ #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -24,98 +29,37 @@ #include #include #include +#include #include "util.h" -#include "dialog.h" #include "globals.h" -#include "download.h" -#include "preferences.h" -#include "vikmapslayer.h" -#include "settings.h" +#include "fileutils.h" -/* #ifdef WINDOWS #include +#else +#include #endif -#ifndef WINDOWS -static gboolean spawn_command_line_async(const gchar * cmd, - const gchar * arg) -{ - gchar *cmdline = NULL; - gboolean status; - - cmdline = g_strdup_printf("%s '%s'", cmd, arg); - g_debug("Running: %s", cmdline); - - status = g_spawn_command_line_async(cmdline, NULL); - - g_free(cmdline); - - return status; -} -#endif -*/ - -void open_url(GtkWindow *parent, const gchar * url) +guint util_get_number_of_cpus () { - GError *error = NULL; - gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error ); - if ( error ) { - a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message ); - g_error_free ( error ); - } -} - -void new_email(GtkWindow *parent, const gchar * address) -{ - gchar *uri = g_strdup_printf("mailto:%s", address); - GError *error = NULL; - gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error ); - if ( error ) { - a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message ); - g_error_free ( error ); - } - /* +#if GLIB_CHECK_VERSION (2, 36, 0) + return g_get_num_processors(); +#else + long nprocs = 1; #ifdef WINDOWS - ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0); + SYSTEM_INFO info; + GetSystemInfo(&info); + nprocs = info.dwNumberOfProcessors; #else - if (!spawn_command_line_async("xdg-email", uri)) - a_dialog_error_msg ( parent, _("Could not create new email.") ); +#ifdef _SC_NPROCESSORS_ONLN + nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (nprocs < 1) + nprocs = 1; +#endif +#endif + return nprocs; #endif - */ - g_free(uri); - uri = NULL; -} -gchar *uri_escape(gchar *str) -{ - gchar *esc_str = g_malloc(3*strlen(str)); - gchar *dst = esc_str; - gchar *src; - - for (src = str; *src; src++) { - if (*src == ' ') - *dst++ = '+'; - else if (g_ascii_isalnum(*src)) - *dst++ = *src; - else { - g_sprintf(dst, "%%%02hhX", *src); - dst += 3; - } - } - *dst = '\0'; - - return(esc_str); -} - - -GList * str_array_to_glist(gchar* data[]) -{ - GList *gl = NULL; - gpointer * p; - for (p = (gpointer)data; *p; p++) - gl = g_list_prepend(gl, *p); - return g_list_reverse(gl); } /** @@ -160,180 +104,177 @@ gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar // Remove newline from val and also any other whitespace *key = g_strstrip ( *key ); *val = g_strstrip ( *val ); - return TRUE; } -typedef struct { - GtkWindow *window; // Layer needed for redrawing - gchar *version; // Image list -} new_version_thread_data; +static GSList* deletion_list = NULL; -static gboolean new_version_available_message ( new_version_thread_data *nvtd ) +/** + * util_add_to_deletion_list: + * + * Add a name of a file into the list that is to be deleted on program exit + * Normally this is for files that get used asynchronously, + * so we don't know when it's time to delete them - other than at this program's end + */ +void util_add_to_deletion_list ( const gchar* filename ) { - // Only a simple goto website option is offered - // Trying to do an installation update is platform specific - if ( a_dialog_yes_or_no ( nvtd->window, - _("There is a newer version of Viking available: %s\n\nDo you wish to go to Viking's website now?"), nvtd->version ) ) - // NB 'VIKING_URL' redirects to the Wiki, here we want to go the main site. - open_url ( nvtd->window, "http://sourceforge.net/projects/viking/" ); - - g_free ( nvtd->version ); - g_free ( nvtd ); - return FALSE; + deletion_list = g_slist_append ( deletion_list, g_strdup (filename) ); } -#define VIK_SETTINGS_VERSION_CHECKED_DATE "version_checked_date" - -static void latest_version_thread ( GtkWindow *window ) +/** + * util_remove_all_in_deletion_list: + * + * Delete all the files in the deletion list + * This should only be called on program exit + */ +void util_remove_all_in_deletion_list ( void ) { - // Need to allow a few redirects, as SF file is often served from different server - DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL, NULL }; - gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options ); - //gchar *filename = g_strdup ( "VERSION" ); - if ( !filename ) { - return; - } - - GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL ); - if ( !mf ) - return; - - gchar *text = g_mapped_file_get_contents ( mf ); - - gint latest_version = viking_version_to_number ( text ); - gint my_version = viking_version_to_number ( VIKING_VERSION ); - - g_debug ( "The lastest version is: %s", text ); - - if ( my_version < latest_version ) { - new_version_thread_data *nvtd = g_malloc ( sizeof(new_version_thread_data) ); - nvtd->window = window; - nvtd->version = g_strdup ( text ); - gdk_threads_add_idle ( (GSourceFunc) new_version_available_message, nvtd ); - } - else - g_debug ( "Running the lastest version: %s", VIKING_VERSION ); - - g_mapped_file_unref ( mf ); - if ( filename ) { - g_remove ( filename ); - g_free ( filename ); - } + while ( deletion_list ) + { + if ( g_remove ( (const char*)deletion_list->data ) ) + g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data ); + g_free ( deletion_list->data ); + deletion_list = g_slist_delete_link ( deletion_list, deletion_list ); + } +} - // Update last checked time - GTimeVal time; - g_get_current_time ( &time ); - a_settings_set_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, g_time_val_to_iso8601(&time) ); +/** + * Removes characters from a string, in place. + * + * @param string String to search. + * @param chars Characters to remove. + * + * @return @a string - return value is only useful when nesting function calls, e.g.: + * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode + * + * @see @c g_strdelimit. + **/ +gchar *util_str_remove_chars(gchar *string, const gchar *chars) +{ + const gchar *r; + gchar *w = string; + + g_return_val_if_fail(string, NULL); + if (G_UNLIKELY(EMPTY(chars))) + return string; + + foreach_str(r, string) + { + if (!strchr(chars, *r)) + *w++ = *r; + } + *w = 0x0; + return string; } -#define VIK_SETTINGS_VERSION_CHECK_PERIOD "version_check_period_days" +/** + * In 'extreme' debug mode don't remove temporary files + * thus the contents can be inspected if things go wrong + * with the trade off the user may need to delete tmp files manually + * Only use this for 'occasional' downloaded temporary files that need interpretation, + * rather than large volume items such as Bing attributions. + */ +int util_remove ( const gchar *filename ) +{ + if ( vik_debug && vik_verbose ) { + g_warning ( "Not removing file: %s", filename ); + return 0; + } + else + return g_remove ( filename ); +} /** - * check_latest_version: - * @window: Somewhere where we may need use the display to inform the user about the version status + * Stream write buffer to a temporary file (in one go) * - * Periodically checks the released latest VERSION file on the website to compare with the running version + * @param buffer The buffer to write + * @param count Size of the buffer to write * + * @return the filename of the buffer that was written */ -void check_latest_version ( GtkWindow *window ) +gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count ) { - if ( ! a_vik_get_check_version () ) - return; - - gboolean do_check = FALSE; - - gint check_period; - if ( ! a_settings_get_integer ( VIK_SETTINGS_VERSION_CHECK_PERIOD, &check_period ) ) { - check_period = 14; - } - - // Get last checked date... - GDate *gdate_last = g_date_new(); - GDate *gdate_now = g_date_new(); - GTimeVal time_last; - gchar *last_checked_date = NULL; + GFileIOStream *gios; + GError *error = NULL; + gchar *tmpname = NULL; - // When no previous date available - set to do the version check - if ( a_settings_get_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, &last_checked_date) ) { - if ( g_time_val_from_iso8601 ( last_checked_date, &time_last ) ) { - g_date_set_time_val ( gdate_last, &time_last ); - } - else - do_check = TRUE; - } - else - do_check = TRUE; +#if GLIB_CHECK_VERSION(2,32,0) + GFile *gf = g_file_new_tmp ( "vik-tmp.XXXXXX", &gios, &error ); + tmpname = g_file_get_path (gf); +#else + gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error ); + if ( error ) { + g_warning ( "%s", error->message ); + g_error_free ( error ); + return NULL; + } + gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error ); + if ( error ) { + g_warning ( "%s", error->message ); + g_error_free ( error ); + return NULL; + } +#endif - GTimeVal time_now; - g_get_current_time ( &time_now ); - g_date_set_time_val ( gdate_now, &time_now ); + gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error ); + if ( error ) { + g_warning ( "%s", error->message ); + g_error_free ( error ); + return NULL; + } - if ( ! do_check ) { - // Dates available so do the comparison - g_date_add_days ( gdate_last, check_period ); - if ( g_date_compare ( gdate_last, gdate_now ) < 0 ) - do_check = TRUE; - } + GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) ); + if ( g_output_stream_write ( gos, buffer, count, NULL, &error ) < 0 ) { + g_critical ( "Couldn't write tmp %s file due to %s", tmpname, error->message ); + g_free (tmpname); + tmpname = NULL; + } - g_date_free ( gdate_last ); - g_date_free ( gdate_now ); + g_output_stream_close ( gos, NULL, &error ); + g_object_unref ( gios ); - if ( do_check ) { -#if GLIB_CHECK_VERSION (2, 32, 0) - g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL ); -#else - g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL ); -#endif - } + return tmpname; } /** - * set_auto_features_on_first_run: + * util_make_absolute_filename: * - * Ask the user's opinion to set some of Viking's default behaviour + * Returns a newly allocated string of the absolute filename or + * NULL if name is already absolute (or dirpath is unusable) */ -void set_auto_features_on_first_run ( void ) +gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath ) { - gboolean auto_features = FALSE; - if ( a_vik_very_first_run () ) { - - GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL ); - - if ( a_dialog_yes_or_no ( GTK_WINDOW(win), - _("This appears to be Viking's very first run.\n\nDo you wish to enable automatic internet features?\n\nIndividual settings can be controlled in the Preferences."), NULL ) ) - auto_features = TRUE; - } - - if ( auto_features ) { - // Set Maps to autodownload - // Ensure the default is true - maps_layer_set_autodownload_default ( TRUE ); - - // Simplistic repeat of preference settings - // Only the name & type are important for setting a preference via this 'external' way - - // Enable auto add map + - // Enable IP lookup - VikLayerParam pref_add_map[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "add_default_map_layer", VIK_LAYER_PARAM_BOOLEAN, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_CHECKBUTTON, NULL, NULL, NULL, NULL, NULL, NULL, }, }; - VikLayerParam pref_startup_method[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "startup_method", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_COMBOBOX, NULL, NULL, NULL, NULL, NULL, NULL}, }; - - VikLayerParamData vlp_data; - vlp_data.b = TRUE; - a_preferences_run_setparam ( vlp_data, pref_add_map ); - - vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION; - a_preferences_run_setparam ( vlp_data, pref_startup_method ); - - // Only on Windows make checking for the latest version on by default - // For other systems it's expected a Package manager or similar controls the installation, so leave it off -#ifdef WINDOWS - VikLayerParam pref_startup_version_check[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "check_version", VIK_LAYER_PARAM_BOOLEAN, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_CHECKBUTTON, NULL, NULL, NULL, NULL, }, }; - vlp_data.b = TRUE; - a_preferences_run_setparam ( vlp_data, pref_startup_version_check ); -#endif + if ( !dirpath ) return NULL; + + // Is it ready absolute? + if ( g_path_is_absolute ( filename ) ) { + return NULL; + } + else { + // Otherwise create the absolute filename from the given directory and filename + gchar *full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL ); + gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name + g_free ( full ); + return absolute; + } +} - // Ensure settings are saved for next time - a_preferences_save_to_file (); - } +/** + * util_make_absolute_filenames: + * + * Ensures the supplied list of filenames are all absolute + */ +void util_make_absolute_filenames ( GList *filenames, const gchar *dirpath ) +{ + if ( !filenames ) return; + if ( !dirpath ) return; + + GList *gl; + foreach_list ( gl, filenames ) { + gchar *fn = util_make_absolute_filename ( gl->data, dirpath ); + if ( fn ) { + g_free ( gl->data ); + gl->data = fn; + } + } }