Commit 62b059b5 authored by Paolo Bacchilega's avatar Paolo Bacchilega

Utility functions refactoring

* renamed some functions to improve consinstency
* delete unused functions
* splitted in more files
* reimplemented some functions
* added tests
parent bfdd4730
......@@ -122,13 +122,17 @@ add_file_to_track (GthBurnTask *task,
if ((strcmp (subfolder, "") != 0) && g_hash_table_lookup (task->priv->parents, subfolder) == NULL) {
GtkTreePath *subfolder_parent_tpath;
GtkTreePath *subfolder_tpath;
char *basename;
if (subfolder_parent != NULL)
subfolder_parent_tpath = g_hash_table_lookup (task->priv->parents, subfolder_parent);
else
subfolder_parent_tpath = NULL;
subfolder_tpath = brasero_track_data_cfg_add_empty_directory (task->priv->track, _g_uri_get_basename (subfolder), subfolder_parent_tpath);
basename = _g_uri_get_basename (subfolder);
subfolder_tpath = brasero_track_data_cfg_add_empty_directory (task->priv->track, basename, subfolder_parent_tpath);
g_hash_table_insert (task->priv->parents, g_strdup (subfolder), subfolder_tpath);
g_free (basename);
}
g_free (subfolder_parent);
......@@ -325,7 +329,7 @@ start_dir_func (GFile *directory,
g_free (task->priv->current_directory);
parent = g_file_get_parent (directory);
escaped = _g_replace (g_file_info_get_display_name (info), "/", "-");
escaped = _g_utf8_replace_str (g_file_info_get_display_name (info), "/", "-");
destination = g_file_get_child_for_display_name (parent, escaped, NULL);
uri = g_file_get_uri (destination);
task->priv->current_directory = g_uri_unescape_string (uri, NULL);
......
......@@ -96,19 +96,22 @@ save_button_clicked_cb (GtkButton *button,
GFile *parent;
char *uri;
char *clean_name;
char *ext;
char *display_name;
GFile *new_file;
parent = g_file_get_parent (data->original_file);
uri = g_file_get_uri (data->original_file);
clean_name = _g_filename_clear_for_file (gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))));
display_name = g_strconcat (clean_name, _g_uri_get_file_extension (uri), NULL);
ext = _g_uri_get_extension (uri);
display_name = g_strconcat (clean_name, ext, NULL);
new_file = g_file_get_child_for_display_name (parent, display_name, NULL);
if ((new_file != NULL) && ! g_file_equal (new_file, data->original_file))
gth_file_data_set_file (data->file_data, new_file);
_g_object_unref (new_file);
g_free (display_name);
g_free (ext);
g_free (clean_name);
g_free (uri);
g_object_unref (parent);
......@@ -164,7 +167,7 @@ catalog_ready_cb (GObject *object,
char *utf8_name;
basename = g_file_get_basename (data->file_data->file);
name = _g_uri_remove_extension (basename);
name = _g_path_remove_extension (basename);
utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("name_entry")), utf8_name);
......
......@@ -622,7 +622,7 @@ get_display_name (GFile *file,
char *name;
char *utf8_name;
name = _g_uri_remove_extension (basename);
name = _g_path_remove_extension (basename);
utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
g_string_append (display_name, utf8_name);
......@@ -673,7 +673,7 @@ get_edit_name (GFile *file,
char *name;
char *utf8_name;
name = _g_uri_remove_extension (basename);
name = _g_path_remove_extension (basename);
utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
g_string_append (display_name, utf8_name);
......
......@@ -435,6 +435,7 @@ gth_file_source_catalogs_rename (GthFileSource *file_source,
if (catalog != NULL) {
char *uri;
char *clean_name;
char *ext;
char *name;
GFile *gio_new_file;
char *data;
......@@ -443,7 +444,8 @@ gth_file_source_catalogs_rename (GthFileSource *file_source,
uri = g_file_get_uri (file);
clean_name = _g_filename_clear_for_file (edit_name);
name = g_strconcat (clean_name, _g_uri_get_file_extension (uri), NULL);
ext = _g_uri_get_extension (uri);
name = g_strconcat (clean_name, ext, NULL);
new_file = g_file_get_child_for_display_name (parent, name, &error);
gth_catalog_set_file (catalog, new_file);
gth_catalog_set_name (catalog, edit_name);
......@@ -477,6 +479,7 @@ gth_file_source_catalogs_rename (GthFileSource *file_source,
g_free (data);
g_object_unref (gio_new_file);
g_free (clean_name);
g_free (ext);
g_free (name);
g_free (uri);
}
......@@ -682,7 +685,7 @@ for_each_child__visit_file (ForEachChildData *data,
data);
}
else
g_directory_foreach_child (gio_file,
_g_directory_foreach_child (gio_file,
FALSE,
TRUE,
GFILE_STANDARD_ATTRIBUTES_WITH_FAST_CONTENT_TYPE,
......@@ -944,13 +947,13 @@ copy_catalog_ready_cb (GError *error,
first_file = ccd->file_list->data;
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
char *uri;
const char *extension;
char *msg;
GtkWidget *d;
char *uri;
char *extension;
char *msg;
GtkWidget *d;
uri = g_file_get_uri (first_file);
extension = _g_uri_get_file_extension (uri);
extension = _g_uri_get_extension (uri);
if ((g_strcmp0 (extension, ".catalog") == 0) || (g_strcmp0 (extension, ".search") == 0))
msg = g_strdup_printf (_("The catalog “%s” already exists, do you want to overwrite it?"), g_file_info_get_display_name (ccd->destination->info));
else
......@@ -972,6 +975,7 @@ copy_catalog_ready_cb (GError *error,
gtk_widget_show (d);
g_free (msg);
g_free (extension);
g_free (uri);
return;
......@@ -1020,19 +1024,19 @@ _gth_file_source_catalogs_copy_catalog (CopyCatalogData *ccd,
gio_list = gth_file_source_to_gio_file_list (ccd->file_source, ccd->file_list);
gio_destination = gth_file_source_to_gio_file (ccd->file_source, ccd->destination->file);
_g_copy_files_async (gio_list,
gio_destination,
ccd->move,
GTH_FILE_COPY_DEFAULT,
default_response,
G_PRIORITY_DEFAULT,
gth_file_source_get_cancellable (ccd->file_source),
ccd->progress_callback,
ccd->user_data,
ccd->dialog_callback,
ccd->user_data,
copy_catalog_ready_cb,
ccd);
_g_file_list_copy_async (gio_list,
gio_destination,
ccd->move,
GTH_FILE_COPY_DEFAULT,
default_response,
G_PRIORITY_DEFAULT,
gth_file_source_get_cancellable (ccd->file_source),
ccd->progress_callback,
ccd->user_data,
ccd->dialog_callback,
ccd->user_data,
copy_catalog_ready_cb,
ccd);
g_object_unref (gio_destination);
_g_object_list_unref (gio_list);
......@@ -1140,12 +1144,12 @@ gth_file_source_catalogs_copy (GthFileSource *file_source,
g_free (message);
}
_g_query_info_async (cod->file_list,
GTH_LIST_DEFAULT,
GFILE_NAME_TYPE_ATTRIBUTES,
gth_file_source_get_cancellable (file_source),
copy__file_list_info_ready_cb,
cod);
_g_file_list_query_info_async (cod->file_list,
GTH_LIST_DEFAULT,
GFILE_NAME_TYPE_ATTRIBUTES,
gth_file_source_get_cancellable (file_source),
copy__file_list_info_ready_cb,
cod);
}
}
......@@ -1453,50 +1457,54 @@ gth_file_source_catalogs_get_drop_actions (GthFileSource *file_source,
GdkDragAction actions = 0;
char *dest_uri;
char *dest_scheme;
const char *dest_ext;
char *dest_ext;
gboolean dest_is_catalog;
char *file_uri;
char *file_scheme;
const char *file_ext;
char *file_ext;
gboolean file_is_catalog;
dest_uri = g_file_get_uri (destination);
dest_scheme = _g_uri_get_scheme (dest_uri);
dest_ext = _g_uri_get_file_extension (dest_uri);
dest_is_catalog = (g_strcmp0 (dest_ext, ".catalog") == 0) || (g_strcmp0 (dest_ext, ".search") == 0);
dest_ext = _g_uri_get_extension (dest_uri);
dest_is_catalog = _g_str_equal (dest_ext, ".catalog") || _g_str_equal (dest_ext, ".search");
file_uri = g_file_get_uri (file);
file_scheme = _g_uri_get_scheme (file_uri);
file_ext = _g_uri_get_file_extension (file_uri);
file_is_catalog = (g_strcmp0 (file_ext, ".catalog") == 0) || (g_strcmp0 (file_ext, ".search") == 0);
file_ext = _g_uri_get_extension (file_uri);
file_is_catalog = _g_str_equal (file_ext, ".catalog") || _g_str_equal (file_ext, ".search");
if ((g_strcmp0 (dest_scheme, "catalog://") == 0)
if (_g_str_equal (dest_scheme, "catalog")
&& dest_is_catalog
&& (g_strcmp0 (file_scheme, "file://") == 0))
&& _g_str_equal (file_scheme, "file"))
{
/* Copy files into a catalog. */
actions = GDK_ACTION_COPY;
}
else if ((g_strcmp0 (file_scheme, "catalog://") == 0)
else if (_g_str_equal (file_scheme, "catalog")
&& file_is_catalog
&& (g_strcmp0 (dest_scheme, "catalog://") == 0)
&& _g_str_equal (dest_scheme, "catalog")
&& ! dest_is_catalog)
{
/* Move a catalog into a library. */
actions = GDK_ACTION_MOVE;
}
else if ((g_strcmp0 (file_scheme, "catalog://") == 0)
else if (_g_str_equal (file_scheme, "catalog")
&& ! file_is_catalog
&& (g_strcmp0 (dest_scheme, "catalog://") == 0)
&& _g_str_equal (dest_scheme, "catalog")
&& ! dest_is_catalog)
{
/* Move a library into another library. */
actions = GDK_ACTION_MOVE;
}
g_free (file_ext);
g_free (file_scheme);
g_free (file_uri);
g_free (dest_ext);
g_free (dest_scheme);
g_free (dest_uri);
return actions;
......
......@@ -571,7 +571,7 @@ gth_organize_task_exec (GthTask *base)
break;
}
g_directory_foreach_child (self->priv->folder,
_g_directory_foreach_child (self->priv->folder,
self->priv->recursive,
TRUE,
attributes,
......@@ -738,12 +738,12 @@ organization_treeview_selection_changed_cb (GtkTreeSelection *treeselection,
gtk_widget_show (GET_WIDGET ("preview_box"));
file_list = gth_catalog_get_file_list (catalog);
_g_query_info_async (file_list,
GTH_LIST_DEFAULT,
GFILE_STANDARD_ATTRIBUTES_WITH_FAST_CONTENT_TYPE,
NULL,
file_list_info_ready_cb,
self);
_g_file_list_query_info_async (file_list,
GTH_LIST_DEFAULT,
GFILE_STANDARD_ATTRIBUTES_WITH_FAST_CONTENT_TYPE,
NULL,
file_list_info_ready_cb,
self);
}
g_free (key);
......
......@@ -333,7 +333,7 @@ add_themes_from_dir (DialogData *data,
continue;
}
if (g_strcmp0 (_g_uri_get_file_extension (g_file_info_get_name (file_info)), ".cst") != 0) {
if (g_strcmp0 (_g_uri_get_extension (g_file_info_get_name (file_info)), ".cst") != 0) {
g_object_unref (file_info);
continue;
}
......@@ -743,7 +743,7 @@ dlg_contact_sheet (GthBrowser *browser,
else
s_value = _g_settings_get_uri (data->settings, PREF_CONTACT_SHEET_DESTINATION);
if (s_value == NULL)
s_value = g_strdup (get_home_uri ());
s_value = g_strdup (_g_uri_get_home ());
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (GET_WIDGET ("destination_filechooserbutton")), s_value);
g_free (s_value);
......
......@@ -265,7 +265,7 @@ dlg_image_wall (GthBrowser *browser,
else
s_value = _g_settings_get_uri (data->settings, PREF_IMAGE_WALL_DESTINATION);
if (s_value == NULL)
s_value = g_strdup (get_home_uri ());
s_value = g_strdup (_g_uri_get_home ());
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (GET_WIDGET ("destination_filechooserbutton")), s_value);
g_free (s_value);
......
......@@ -231,6 +231,47 @@ get_page_height (GthContactSheetCreator *self,
}
static char *
_g_get_name_from_template (char **utf8_template,
int n)
{
GString *s;
int i;
char *result;
s = g_string_new (NULL);
for (i = 0; utf8_template[i] != NULL; i++) {
const char *chunk = utf8_template[i];
gunichar ch = g_utf8_get_char (chunk);
if (ch != '#')
g_string_append (s, chunk);
else {
char *s_n;
int s_n_len;
int sharps_len = g_utf8_strlen (chunk, -1);
s_n = g_strdup_printf ("%d", n);
s_n_len = strlen (s_n);
while (s_n_len < sharps_len) {
g_string_append_c (s, '0');
sharps_len--;
}
g_string_append (s, s_n);
g_free (s_n);
}
}
result = s->str;
g_string_free (s, FALSE);
return result;
}
static void
begin_page (GthContactSheetCreator *self,
int page_n)
......@@ -559,7 +600,6 @@ paint_frame (GthContactSheetCreator *self,
if (self->priv->imagemap_stream != NULL) {
char *file;
char *destination;
char *relative_uri;
char *relative_path;
char *alt_attribute;
char *line;
......@@ -567,9 +607,8 @@ paint_frame (GthContactSheetCreator *self,
file = g_file_get_uri (file_data->file);
destination = g_file_get_uri (self->priv->destination);
relative_uri = _g_uri_get_relative_path (file, destination);
relative_path = g_uri_unescape_string (relative_uri, "");
alt_attribute = _g_escape_for_html (relative_path, -1);
relative_path = _g_uri_get_relative_path (file, destination);
alt_attribute = _g_utf8_escape_xml (relative_path);
line = g_strdup_printf (" <area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"%s\" alt=\"%s\" />\n",
frame_rect->x,
......@@ -586,7 +625,6 @@ paint_frame (GthContactSheetCreator *self,
g_free (line);
g_free (alt_attribute);
g_free (relative_path);
g_free (relative_uri);
g_free (destination);
g_free (file);
}
......@@ -1127,7 +1165,7 @@ void
gth_contact_sheet_creator_set_header (GthContactSheetCreator *self,
const char *value)
{
_g_strset (&self->priv->header, value);
_g_str_set (&self->priv->header, value);
}
......@@ -1135,7 +1173,7 @@ void
gth_contact_sheet_creator_set_footer (GthContactSheetCreator *self,
const char *value)
{
_g_strset (&self->priv->footer, value);
_g_str_set (&self->priv->footer, value);
}
......@@ -1152,10 +1190,10 @@ void
gth_contact_sheet_creator_set_filename_template (GthContactSheetCreator *self,
const char *filename_template)
{
_g_strset (&self->priv->template, filename_template);
_g_str_set (&self->priv->template, filename_template);
if (self->priv->template_v != NULL)
g_strfreev (self->priv->template_v);
self->priv->template_v = _g_get_template_from_text (self->priv->template);
self->priv->template_v = _g_utf8_split_template (self->priv->template);
}
......@@ -1164,8 +1202,8 @@ gth_contact_sheet_creator_set_mime_type (GthContactSheetCreator *self,
const char *mime_type,
const char *file_extension)
{
_g_strset (&self->priv->mime_type, mime_type);
_g_strset (&self->priv->file_extension, file_extension);
_g_str_set (&self->priv->mime_type, mime_type);
_g_str_set (&self->priv->file_extension, file_extension);
}
......@@ -1249,6 +1287,6 @@ void
gth_contact_sheet_creator_set_thumbnail_caption (GthContactSheetCreator *self,
const char *caption)
{
_g_strset (&self->priv->thumbnail_caption, caption);
_g_str_set (&self->priv->thumbnail_caption, caption);
self->priv->thumbnail_caption_v = g_strsplit (self->priv->thumbnail_caption, ",", -1);
}
......@@ -212,7 +212,7 @@ get_new_wallpaper_file_async (GCancellable *cancellable,
task = g_task_new (NULL, cancellable, callback, user_data);
g_task_set_task_data (task, nw_data, (GDestroyNotify) new_wallpaper_data_free);
g_directory_foreach_child (nw_data->folder,
_g_directory_foreach_child (nw_data->folder,
FALSE,
FALSE,
GFILE_NAME_TYPE_ATTRIBUTES,
......
......@@ -216,14 +216,14 @@ const char *stupid_comment_filter[] = {
inline static char *
exiv2_key_from_attribute (const char *attribute)
{
return _g_replace (attribute, "::", ".");
return _g_utf8_replace_str (attribute, "::", ".");
}
inline static char *
exiv2_key_to_attribute (const char *key)
{
return _g_replace (key, ".", "::");
return _g_utf8_replace_str (key, ".", "::");
}
......@@ -274,11 +274,11 @@ create_metadata (const char *key,
formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
}
else if (_g_utf8_has_prefix (formatted_value_utf8, "lang=")) {
int pos;
char *formatted_clean;
const char *after_space;
char *formatted_clean;
pos = _g_utf8_first_ascii_space (formatted_value_utf8);
formatted_clean = _g_utf8_remove_prefix (formatted_value_utf8, pos + 1);
after_space = _g_utf8_after_ascii_space (formatted_value_utf8);
formatted_clean = g_strdup (after_space);
g_free (formatted_value_utf8);
formatted_value_utf8 = formatted_clean;
}
......
......@@ -70,19 +70,19 @@ facebook_album_set_property (GObject *object,
switch (property_id) {
case PROP_ID:
_g_strset (&self->id, g_value_get_string (value));
_g_str_set (&self->id, g_value_get_string (value));
break;
case PROP_NAME:
_g_strset (&self->name, g_value_get_string (value));
_g_str_set (&self->name, g_value_get_string (value));
break;
case PROP_DESCRIPTION:
_g_strset (&self->description, g_value_get_string (value));
_g_str_set (&self->description, g_value_get_string (value));
break;
case PROP_LINK:
_g_strset (&self->link, g_value_get_string (value));
_g_str_set (&self->link, g_value_get_string (value));
break;
case PROP_PRIVACY:
_g_strset (&self->privacy, g_value_get_string (value));
_g_str_set (&self->privacy, g_value_get_string (value));
break;
case PROP_COUNT:
self->count = g_value_get_int (value);
......
......@@ -74,7 +74,7 @@ facebook_image_copy (FacebookImage *source)
FacebookImage *dest;
dest = facebook_image_new ();
_g_strset (&dest->source, source->source);
_g_str_set (&dest->source, source->source);
dest->width = source->width;
dest->height = source->height;
......@@ -131,13 +131,13 @@ facebook_photo_set_property (GObject *object,
switch (property_id) {
case PROP_ID:
_g_strset (&self->id, g_value_get_string (value));
_g_str_set (&self->id, g_value_get_string (value));
break;
case PROP_PICTURE:
_g_strset (&self->picture, g_value_get_string (value));
_g_str_set (&self->picture, g_value_get_string (value));
break;
case PROP_SOURCE:
_g_strset (&self->source, g_value_get_string (value));
_g_str_set (&self->source, g_value_get_string (value));
break;
case PROP_WIDTH:
self->width = g_value_get_int (value);
......@@ -146,7 +146,7 @@ facebook_photo_set_property (GObject *object,
self->height = g_value_get_int (value);
break;
case PROP_LINK:
_g_strset (&self->link, g_value_get_string (value));
_g_str_set (&self->link, g_value_get_string (value));
break;
case PROP_CREATED_TIME:
gth_datetime_free (self->created_time);
......@@ -353,7 +353,7 @@ facebook_photo_deserialize_property (JsonSerializable *serializable,
FacebookImage *image;
image = facebook_image_new ();
_g_strset (&image->source, json_object_get_string_member (image_obj, "source"));
_g_str_set (&image->source, json_object_get_string_member (image_obj, "source"));
image->width = json_object_get_int_member (image_obj, "width");
image->height = json_object_get_int_member (image_obj, "height");
......
......@@ -111,7 +111,7 @@ static void
_facebook_service_set_access_token (FacebookService *self,
const char *token)
{
_g_strset (&self->priv->token, token);
_g_str_set (&self->priv->token, token);
}
......@@ -753,6 +753,7 @@ upload_photo_file_buffer_ready_cb (void **buffer,
GthFileData *file_data;
SoupMultipart *multipart;
char *uri;
char *basename;
SoupBuffer *body;
SoupMessage *msg;
......@@ -858,14 +859,16 @@ upload_photo_file_buffer_ready_cb (void **buffer,
}
uri = g_file_get_uri (file_data->file);
basename = _g_uri_get_basename (uri);
body = soup_buffer_new (SOUP_MEMORY_TEMPORARY, *buffer, count);
soup_multipart_append_form_file (multipart,
"source",