Skip to content

Commit

Permalink
Add cd_icc_save_data() so that we can set _ICC_PROFILE without re-rea…
Browse files Browse the repository at this point in the history
…ding the file
  • Loading branch information
hughsie committed Jul 3, 2013
1 parent 6eebc97 commit 9c6de68
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
81 changes: 59 additions & 22 deletions lib/colord/cd-icc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,24 +1253,20 @@ cd_icc_save_file_mkdir_parents (GFile *file, GError **error)
}

/**
* cd_icc_save_file:
* cd_icc_save_data:
* @icc: a #CdIcc instance.
* @file: a #GFile
* @flags: a set of #CdIccSaveFlags
* @cancellable: A #GCancellable or %NULL
* @error: A #GError or %NULL
*
* Saves an ICC profile to a local or remote file.
* Saves an ICC profile to an allocated memory location.
*
* Return vale: %TRUE for success.
* Return vale: A #GBytes structure, or %NULL for error
*
* Since: 0.1.32
* Since: 1.0.2
**/
gboolean
cd_icc_save_file (CdIcc *icc,
GFile *file,
GBytes *
cd_icc_save_data (CdIcc *icc,
CdIccSaveFlags flags,
GCancellable *cancellable,
GError **error)
{
CdIccPrivate *priv = icc->priv;
Expand All @@ -1279,14 +1275,13 @@ cd_icc_save_file (CdIcc *icc,
const gchar *key;
const gchar *value;
gboolean ret = FALSE;
gchar *data = NULL;
GError *error_local = NULL;
GBytes *data = NULL;
gchar *data_tmp = NULL;
GList *l;
GList *md_keys = NULL;
guint i;

g_return_val_if_fail (CD_IS_ICC (icc), FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);

/* convert profile kind */
for (i = 0; map_profile_kind[i].colord != CD_PROFILE_KIND_LAST; i++) {
Expand Down Expand Up @@ -1435,9 +1430,9 @@ cd_icc_save_file (CdIcc *icc,
}

/* allocate and get profile data */
data = g_new0 (gchar, length);
data_tmp = g_new0 (gchar, length);
ret = cmsSaveProfileToMem (priv->lcms_profile,
data, &length);
data_tmp, &length);
if (!ret) {
g_set_error_literal (error,
CD_ICC_ERROR,
Expand All @@ -1446,15 +1441,60 @@ cd_icc_save_file (CdIcc *icc,
goto out;
}

/* success */
data = g_bytes_new (data_tmp, length);
out:
g_list_free (md_keys);
if (dict != NULL)
cmsDictFree (dict);
g_free (data_tmp);
return data;
}

/**
* cd_icc_save_file:
* @icc: a #CdIcc instance.
* @file: a #GFile
* @flags: a set of #CdIccSaveFlags
* @cancellable: A #GCancellable or %NULL
* @error: A #GError or %NULL
*
* Saves an ICC profile to a local or remote file.
*
* Return vale: %TRUE for success.
*
* Since: 0.1.32
**/
gboolean
cd_icc_save_file (CdIcc *icc,
GFile *file,
CdIccSaveFlags flags,
GCancellable *cancellable,
GError **error)
{
gboolean ret;
GBytes *data = NULL;
GError *error_local = NULL;

g_return_val_if_fail (CD_IS_ICC (icc), FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);

/* get data */
data = cd_icc_save_data (icc, flags, error);
if (data == NULL) {
ret = FALSE;
goto out;
}

/* ensure parent directories exist */
ret = cd_icc_save_file_mkdir_parents (file, error);
if (!ret)
goto out;

/* actually write file */
ret = g_file_replace_contents (file,
data,
length,
g_bytes_get_data (data, NULL),
g_bytes_get_size (data),
NULL,
FALSE,
G_FILE_CREATE_NONE,
Expand All @@ -1465,16 +1505,13 @@ cd_icc_save_file (CdIcc *icc,
g_set_error (error,
CD_ICC_ERROR,
CD_ICC_ERROR_FAILED_TO_SAVE,
"failed to dump ICC file: %s",
"failed to save ICC file: %s",
error_local->message);
g_error_free (error_local);
goto out;
}
out:
g_list_free (md_keys);
if (dict != NULL)
cmsDictFree (dict);
g_free (data);
g_bytes_unref (data);
return ret;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/colord/cd-icc.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ gboolean cd_icc_load_handle (CdIcc *icc,
CdIccLoadFlags flags,
GError **error)
G_GNUC_WARN_UNUSED_RESULT;
GBytes *cd_icc_save_data (CdIcc *icc,
CdIccSaveFlags flags,
GError **error)
G_GNUC_WARN_UNUSED_RESULT;
gboolean cd_icc_save_file (CdIcc *icc,
GFile *file,
CdIccSaveFlags flags,
Expand Down

0 comments on commit 9c6de68

Please sign in to comment.