Commit cbfcfe24 authored by Kartikeya Sharma's avatar Kartikeya Sharma Committed by Carlos Soriano Sánchez

file-operations: use standard print functions

We were using a print function with custom handlers.

Not only this function is hard to understand and follow, but is also non
standard which makes it problematic for translation (where everything is
check using c standards to avoid crashing the application if the translator
makes some unsuported change).

This patch removes every use case of f() function and uses g_strdup_printf
and g_strdup in its place and uses custom functions directly to convert
required parameters into gchar* types.

https://bugzilla.gnome.org/show_bug.cgi?id=775091
parent 1182b2c3
......@@ -947,35 +947,13 @@ has_invalid_xml_char (char *str)
return FALSE;
}
static char *
custom_full_name_to_string (char *format,
va_list va)
{
GFile *file;
file = va_arg (va, GFile *);
return g_file_get_parse_name (file);
}
static void
custom_full_name_skip (va_list *va)
{
(void) va_arg (*va, GFile *);
}
static char *
custom_basename_to_string (char *format,
va_list va)
static gchar *
get_basename (GFile *file)
{
GFile *file;
GFileInfo *info;
char *name, *basename, *tmp;
gchar *name, *basename, *tmp;
GMount *mount;
file = va_arg (va, GFile *);
if ((mount = nautilus_get_mounted_mount_for_root (file)) != NULL)
{
name = g_mount_get_name (mount);
......@@ -1026,91 +1004,9 @@ custom_basename_to_string (char *format,
g_free (tmp);
}
return name;
}
static void
custom_basename_skip (va_list *va)
{
(void) va_arg (*va, GFile *);
}
static char *
custom_size_to_string (char *format,
va_list va)
{
goffset size;
size = va_arg (va, goffset);
return g_format_size (size);
}
static void
custom_size_skip (va_list *va)
{
(void) va_arg (*va, goffset);
}
static char *
custom_time_to_string (char *format,
va_list va)
{
int secs;
secs = va_arg (va, int);
return get_formatted_time (secs);
}
static void
custom_time_skip (va_list *va)
{
(void) va_arg (*va, int);
}
static char *
custom_mount_to_string (char *format,
va_list va)
{
GMount *mount;
mount = va_arg (va, GMount *);
return g_mount_get_name (mount);
}
static void
custom_mount_skip (va_list *va)
{
(void) va_arg (*va, GMount *);
}
static EelPrintfHandler handlers[] =
{
{ 'F', custom_full_name_to_string, custom_full_name_skip },
{ 'B', custom_basename_to_string, custom_basename_skip },
{ 'S', custom_size_to_string, custom_size_skip },
{ 'T', custom_time_to_string, custom_time_skip },
{ 'V', custom_mount_to_string, custom_mount_skip },
{ 0 }
};
static char *
f (const char *format,
...)
{
va_list va;
char *res;
va_start (va, format);
res = eel_strdup_vprintf_with_custom (handlers, format, va);
va_end (va);
return res;
}
#define op_job_new(__type, parent_window) ((__type *) (init_common (sizeof (__type), parent_window)))
static gpointer
......@@ -1603,22 +1499,25 @@ confirm_delete_from_trash (CommonJob *job,
if (file_count == 1)
{
prompt = f (_("Are you sure you want to permanently delete “%B” "
"from the trash?"), files->data);
g_autofree gchar *basename = NULL;
basename = get_basename (files->data);
prompt = g_strdup_printf (_("Are you sure you want to permanently delete “%s” "
"from the trash?"), basename);
}
else
{
prompt = f (ngettext ("Are you sure you want to permanently delete "
"the %'d selected item from the trash?",
"Are you sure you want to permanently delete "
"the %'d selected items from the trash?",
file_count),
file_count);
prompt = g_strdup_printf (ngettext ("Are you sure you want to permanently delete "
"the %'d selected item from the trash?",
"Are you sure you want to permanently delete "
"the %'d selected items from the trash?",
file_count),
file_count);
}
response = run_warning (job,
prompt,
f (_("If you delete an item, it will be permanently lost.")),
g_strdup (_("If you delete an item, it will be permanently lost.")),
NULL,
FALSE,
CANCEL, DELETE,
......@@ -1639,11 +1538,11 @@ confirm_empty_trash (CommonJob *job)
return TRUE;
}
prompt = f (_("Empty all items from Trash?"));
prompt = g_strdup (_("Empty all items from Trash?"));
response = run_warning (job,
prompt,
f (_("All items in the Trash will be permanently deleted.")),
g_strdup (_("All items in the Trash will be permanently deleted.")),
NULL,
FALSE,
CANCEL, _("Empty _Trash"),
......@@ -1676,21 +1575,24 @@ confirm_delete_directly (CommonJob *job,
if (file_count == 1)
{
prompt = f (_("Are you sure you want to permanently delete “%B”?"),
files->data);
g_autofree gchar *basename = NULL;
basename = get_basename (files->data);
prompt = g_strdup_printf (_("Are you sure you want to permanently delete “%s”?"),
basename);
}
else
{
prompt = f (ngettext ("Are you sure you want to permanently delete "
"the %'d selected item?",
"Are you sure you want to permanently delete "
"the %'d selected items?", file_count),
file_count);
prompt = g_strdup_printf (ngettext ("Are you sure you want to permanently delete "
"the %'d selected item?",
"Are you sure you want to permanently delete "
"the %'d selected items?", file_count),
file_count);
}
response = run_warning (job,
prompt,
f (_("If you delete an item, it will be permanently lost.")),
g_strdup (_("If you delete an item, it will be permanently lost.")),
NULL,
FALSE,
CANCEL, DELETE,
......@@ -1736,17 +1638,20 @@ report_delete_progress (CommonJob *job,
if (source_info->num_files == 1)
{
g_autofree gchar *basename = NULL;
if (files_left == 0)
{
status = _("Deleted “%B”");
status = _("Deleted “%s”");
}
else
{
status = _("Deleting “%B”");
status = _("Deleting “%s”");
}
basename = get_basename (G_FILE (delete_job->files->data));
nautilus_progress_info_take_status (job->progress,
f (status,
(GFile *) delete_job->files->data));
g_strdup_printf (status, basename));
}
else
{
......@@ -1763,8 +1668,8 @@ report_delete_progress (CommonJob *job,
source_info->num_files);
}
nautilus_progress_info_take_status (job->progress,
f (status,
source_info->num_files));
g_strdup_printf (status,
source_info->num_files));
}
elapsed = g_timer_elapsed (job->time, NULL);
......@@ -1785,17 +1690,17 @@ report_delete_progress (CommonJob *job,
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files + 1,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files + 1,
source_info->num_files);
}
else
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
}
}
else
......@@ -1805,14 +1710,15 @@ report_delete_progress (CommonJob *job,
gchar *time_left_message;
gchar *files_per_second_message;
gchar *concat_detail;
g_autofree gchar *formatted_time = NULL;
/* To translators: %T will expand to a time duration like "2 minutes".
* So the whole thing will be something like "1 / 5 -- 2 hours left (4 files/sec)"
*
* The singular/plural form will be used depending on the remaining time (i.e. the %T argument).
*/
time_left_message = ngettext ("%'d / %'d \xE2\x80\x94 %T left",
"%'d / %'d \xE2\x80\x94 %T left",
time_left_message = ngettext ("%'d / %'d \xE2\x80\x94 %s left",
"%'d / %'d \xE2\x80\x94 %s left",
seconds_count_format_time_units (remaining_time));
transfer_rate += 0.5;
files_per_second_message = ngettext ("(%d file/sec)",
......@@ -1820,10 +1726,11 @@ report_delete_progress (CommonJob *job,
(int) transfer_rate);
concat_detail = g_strconcat (time_left_message, " ", files_per_second_message, NULL);
details = f (concat_detail,
transfer_info->num_files + 1, source_info->num_files,
remaining_time,
(int) transfer_rate);
formatted_time = get_formatted_time (remaining_time);
details = g_strdup_printf (concat_detail,
transfer_info->num_files + 1, source_info->num_files,
formatted_time,
(int) transfer_rate);
g_free (concat_detail);
}
......@@ -1831,9 +1738,9 @@ report_delete_progress (CommonJob *job,
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
}
}
nautilus_progress_info_take_details (job->progress, details);
......@@ -1948,6 +1855,7 @@ file_deleted_callback (GFile *file,
char *secondary;
char *details = NULL;
int response;
g_autofree gchar *basename = NULL;
job = data->job;
source_info = data->source_info;
......@@ -1971,27 +1879,33 @@ file_deleted_callback (GFile *file,
return;
}
primary = f (_("Error while deleting."));
primary = g_strdup (_("Error while deleting."));
file_type = g_file_query_file_type (file,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
job->cancellable);
basename = get_basename (file);
if (file_type == G_FILE_TYPE_DIRECTORY)
{
secondary = IS_IO_ERROR (error, PERMISSION_DENIED) ?
f (_("There was an error deleting the folder “%B”."),
file) :
f (_("You do not have sufficient permissions to delete the folder “%B”."),
file);
g_strdup_printf (_("There was an error deleting the"
"folder “%s”."),
basename) :
g_strdup_printf (_("You do not have sufficient permissions "
"to delete the folder “%s”."),
basename);
}
else
{
secondary = IS_IO_ERROR (error, PERMISSION_DENIED) ?
f (_("There was an error deleting the file “%B”."),
file) :
f (_("You do not have sufficient permissions to delete the file “%B”."),
file);
g_strdup_printf (_("There was an error deleting the "
"file “%s”."),
basename) :
g_strdup_printf (_("You do not have sufficient permissions "
"to delete the file “%s”."),
basename);
}
details = error->message;
......@@ -2110,17 +2024,20 @@ report_trash_progress (CommonJob *job,
if (source_info->num_files == 1)
{
g_autofree gchar *basename = NULL;
if (files_left > 0)
{
status = _("Trashing “%B”");
status = _("Trashing “%s”");
}
else
{
status = _("Trashed “%B”");
status = _("Trashed “%s”");
}
basename = get_basename (G_FILE (delete_job->files->data));
nautilus_progress_info_take_status (job->progress,
f (status,
(GFile *) delete_job->files->data));
g_strdup_printf (status, basename));
}
else
{
......@@ -2137,8 +2054,8 @@ report_trash_progress (CommonJob *job,
source_info->num_files);
}
nautilus_progress_info_take_status (job->progress,
f (status,
source_info->num_files));
g_strdup_printf (status,
source_info->num_files));
}
......@@ -2160,17 +2077,17 @@ report_trash_progress (CommonJob *job,
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files + 1,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files + 1,
source_info->num_files);
}
else
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
}
}
else
......@@ -2180,24 +2097,27 @@ report_trash_progress (CommonJob *job,
gchar *time_left_message;
gchar *files_per_second_message;
gchar *concat_detail;
g_autofree gchar *formatted_time = NULL;
/* To translators: %T will expand to a time duration like "2 minutes".
* So the whole thing will be something like "1 / 5 -- 2 hours left (4 files/sec)"
*
* The singular/plural form will be used depending on the remaining time (i.e. the %T argument).
*/
time_left_message = ngettext ("%'d / %'d \xE2\x80\x94 %T left",
"%'d / %'d \xE2\x80\x94 %T left",
time_left_message = ngettext ("%'d / %'d \xE2\x80\x94 %s left",
"%'d / %'d \xE2\x80\x94 %s left",
seconds_count_format_time_units (remaining_time));
files_per_second_message = ngettext ("(%d file/sec)",
"(%d files/sec)",
(int) (transfer_rate + 0.5));
concat_detail = g_strconcat (time_left_message, " ", files_per_second_message, NULL);
details = f (concat_detail,
transfer_info->num_files + 1, source_info->num_files,
remaining_time,
(int) transfer_rate + 0.5);
formatted_time = get_formatted_time (remaining_time);
details = g_strdup_printf (concat_detail,
transfer_info->num_files + 1,
source_info->num_files,
formatted_time,
(int) transfer_rate + 0.5);
g_free (concat_detail);
}
......@@ -2205,9 +2125,9 @@ report_trash_progress (CommonJob *job,
{
/* To translators: %'d is the number of files completed for the operation,
* so it will be something like 2/14. */
details = f (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
details = g_strdup_printf (_("%'d / %'d"),
transfer_info->num_files,
source_info->num_files);
}
}
nautilus_progress_info_set_details (job->progress, details);
......@@ -2238,6 +2158,7 @@ trash_file (CommonJob *job,
GError *error;
char *primary, *secondary, *details;
int response;
g_autofree gchar *basename = NULL;
if (should_skip_file (job, file))
{
......@@ -2273,8 +2194,12 @@ trash_file (CommonJob *job,
goto skip;
}
basename = get_basename (file);
/* Translators: %B is a file name */
primary = f (_("“%B” can’t be put in the trash. Do you want to delete it immediately?"), file);
primary = g_strdup_printf (_("“%s” can’t be put in the trash. Do you want "
"to delete it immediately?"),
basename);
details = NULL;
secondary = NULL;
if (!IS_IO_ERROR (error, NOT_SUPPORTED))
......@@ -2283,7 +2208,7 @@ trash_file (CommonJob *job,
}
else if (!g_file_is_native (file))
{
secondary = f (_("This remote location does not support sending items to the trash."));
secondary = g_strdup (_("This remote location does not support sending items to the trash."));
}
response = run_question (job,
......@@ -2639,13 +2564,18 @@ unmount_mount_callback (GObject *source_object,
{
if (error->code != G_IO_ERROR_FAILED_HANDLED)
{
g_autofree gchar *mount_name = NULL;
mount_name = g_mount_get_name (G_MOUNT (source_object));
if (data->eject)
{
primary = f (_("Unable to eject %V"), source_object);
primary = g_strdup_printf (_("Unable to eject %s"),
mount_name);
}
else
{
primary = f (_("Unable to unmount %V"), source_object);
primary = g_strdup_printf (_("Unable to unmount %s"),
mount_name);
}
eel_show_error_dialog (primary,
error->message,
......@@ -3050,45 +2980,57 @@ report_preparing_count_progress (CommonJob *job,
default:
case OP_KIND_COPY:
{
s = f (ngettext ("Preparing to copy %'d file (%S)",
"Preparing to copy %'d files (%S)",
source_info->num_files),
source_info->num_files, source_info->num_bytes);
g_autofree gchar *formatted_size = NULL;
formatted_size = g_format_size (source_info->num_bytes);
s = g_strdup_printf (ngettext ("Preparing to copy %'d file (%s)",
"Preparing to copy %'d files (%s)",
source_info->num_files),
source_info->num_files,
formatted_size);
}
break;
case OP_KIND_MOVE:
{
s = f (ngettext ("Preparing to move %'d file (%S)",
"Preparing to move %'d files (%S)",
source_info->num_files),
source_info->num_files, source_info->num_bytes);
g_autofree gchar *formatted_size = NULL;
formatted_size = g_format_size (source_info->num_bytes);
s = g_strdup_printf (ngettext ("Preparing to move %'d file (%s)",
"Preparing to move %'d files (%s)",
source_info->num_files),
source_info->num_files,
formatted_size);
}
break;
case OP_KIND_DELETE:
{
s = f (ngettext ("Preparing to delete %'d file (%S)",
"Preparing to delete %'d files (%S)",
source_info->num_files),
source_info->num_files, source_info->num_bytes);
g_autofree gchar *formatted_size = NULL;
formatted_size = g_format_size (source_info->num_bytes);
s = g_strdup_printf (ngettext ("Preparing to delete %'d file (%s)",
"Preparing to delete %'d files (%s)",
source_info->num_files),
source_info->num_files,
formatted_size);
}
break;
case OP_KIND_TRASH:
{
s = f (ngettext ("Preparing to trash %'d file",
"Preparing to trash %'d files",
source_info->num_files),
source_info->num_files);
s = g_strdup_printf (ngettext ("Preparing to trash %'d file",
"Preparing to trash %'d files",
source_info->num_files),
source_info->num_files);
}
break;
case OP_KIND_COMPRESS:
s = f (ngettext ("Preparing to compress %'d file",
"Preparing to compress %'d files",
source_info->num_files),
source_info->num_files);
s = g_strdup_printf (ngettext ("Preparing to compress %'d file",
"Preparing to compress %'d files",
source_info->num_files),
source_info->num_files);
}
nautilus_progress_info_take_details (job->progress, s);
......@@ -3118,26 +3060,26 @@ get_scan_primary (OpKind kind)
default:
case OP_KIND_COPY:
{
return f (_("Error while copying."));
return g_strdup (_("Error while copying."));
}
case OP_KIND_MOVE:
{
return f (_("Error while moving."));
return g_strdup (_("Error while moving."));
}
case OP_KIND_DELETE:
{
return f (_("Error while deleting."));
return g_strdup (_("Error while deleting."));
}
case OP_KIND_TRASH:
{
return f (_("Error while moving files to trash."));
return g_strdup (_("Error while moving files to trash."));
}
case OP_KIND_COMPRESS:
return f (_("Error while compressing files."));
return g_strdup (_("Error while compressing files."));
}
}
......@@ -3204,17 +3146,22 @@ retry:
}
else if (error)
{
g_autofree gchar *basename = NULL;
primary = get_scan_primary (source_info->op);
details = NULL;
basename = get_basename (dir);
if (IS_IO_ERROR (error, PERMISSION_DENIED))
{
secondary = f (_("Files in the folder “%B” cannot be handled because you do "
"not have permissions to see them."), dir);
secondary = g_strdup_printf (_("Files in the folder “%s” cannot be handled "
"because you do not have permissions to see them."),
basename);
}
else
{
secondary = f (_("There was an error getting information about the files in the folder “%B”."), dir);
secondary = g_strdup_printf (_("There was an error getting information about the "
"files in the folder “%s”."), basename);
details = error->message;
}
......@@ -3258,17 +3205,21 @@ retry:
}
else
{
g_autofree gchar *basename = NULL;
primary = get_scan_primary (source_info->op);
details = NULL;
basename = get_basename (dir);
if (IS_IO_ERROR (error, PERMISSION_DENIED))
{
secondary = f (_("The folder “%B” cannot be handled because you do not have "
"permissions to read it."), dir);
secondary = g_strdup_printf (_("The folder “%s” cannot be handled because you "
"do not have permissions to read it."),
basename);
}
else
{
secondary = f (_("There was an error reading the folder “%B”."), dir);
secondary = g_strdup_printf (_("There was an error reading the folder “%s”."),
basename);
details = error->message;
}
/* set show_all to TRUE here, as we don't know how many
......@@ -3364,17 +3315,21 @@ retry:
}
else
{
g_autofree gchar *basename = NULL;
primary = get_scan_primary (source_info->op);
details = NULL;
basename = get_basename (file);
if (IS_IO_ERROR (error, PERMISSION_DENIED))
{
secondary = f (_("The file “%B” cannot be handled because you do not have "
"permissions to read it."), file);
secondary = g_strdup_printf (_("The file “%s” cannot be handled because you do not have "
"permissions to read it."), basename);
}
else
{
secondary = f (_("There was an error getting information about “%B”."), file);
secondary = g_strdup_printf (_("There was an error getting information about “%s”."),
basename);
details = error->message;
}
/* set show_all to TRUE here, as we don't know how many
......@@ -3490,22 +3445,25 @@ retry:
if (info == NULL)
{
g_autofree gchar *basename = NULL;
if (IS_IO_ERROR (error, CANCELLED))
{
g_error_free (error);
return;
}
primary = f (_("Error while copying to “%B”."), dest);
basename = get_basename (dest);
primary = g_strdup_printf (_("Error while copying to “%s”."), basename);
details = NULL;
if (IS_IO_ERROR (error, PERMISSION_DENIED))
{
secondary = f (_("You do not have permissions to access the destination folder."));
secondary = g_strdup (_("You do not have permissions to access the destination folder."));
}
else
{
secondary = f (_("There was an error getting information about the destination."));
secondary = g_strdup (_("There was an error getting information about the destination."));
details = error->message;
}
......@@ -3555,8 +3513,11 @@ retry:
if (file_type != G_FILE_TYPE_DIRECTORY)
{
primary = f (_("Error while copying to “%B”."), dest);