}
}
+gboolean
+zak_form_cgi_form_element_validator_file_size_is_valid (ZakFormCgiFormElementValidatorFile *validator, guint size)
+{
+ gboolean ret;
+
+ ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator);
+
+ ret = TRUE;
+
+ if (priv->size_min > 0
+ && size < priv->size_min)
+ {
+ zak_form_element_validator_set_message (ZAK_FORM_ELEMENT_VALIDATOR (validator),
+ g_strdup_printf (_("The size must be greater than %d."),
+ priv->size_min));
+ ret = FALSE;
+ }
+ if (priv->size_max > 0
+ && size > priv->size_max)
+ {
+ zak_form_element_validator_set_message (ZAK_FORM_ELEMENT_VALIDATOR (validator),
+ g_strdup_printf (_("The size cannot be greater than %d."),
+ priv->size_max));
+ ret = FALSE;
+ }
+
+ return ret;
+}
+
+gboolean
+zak_form_cgi_form_element_validator_file_mime_is_valid (ZakFormCgiFormElementValidatorFile *validator, const gchar *mime)
+{
+ gboolean ret;
+
+ gchar **splitted;
+ guint l;
+ guint i;
+ gboolean found;
+
+ ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator);
+
+ g_return_val_if_fail (mime != NULL, FALSE);
+
+ ret = TRUE;
+
+ found = FALSE;
+
+ splitted = g_strsplit (priv->mime_type, "|", -1);
+ l = g_strv_length (splitted);
+ for (i = 0; i < l; i++)
+ {
+ if (g_strcmp0 (mime, splitted[i]) == 0)
+ {
+ found = TRUE;
+ break;
+ }
+ }
+ g_strfreev (splitted);
+
+ if (!found)
+ {
+ zak_form_element_validator_set_message (ZAK_FORM_ELEMENT_VALIDATOR (validator),
+ g_strdup_printf (_("The file must be of type «%s»."),
+ priv->mime_type_message != NULL ? priv->mime_type_message : priv->mime_type));
+ ret = FALSE;
+ }
+
+ return ret;
+}
+
/* PRIVATE */
static void
zak_form_cgi_form_element_validator_file_set_property (GObject *object,
&& cgi_file->name != NULL
&& g_strcmp0 (cgi_file->name, "") != 0)
{
- if (priv->size_min > 0
- && cgi_file->size < priv->size_min)
- {
- zak_form_element_validator_set_message (validator,
- g_strdup_printf (_("The size must be greater than %d."),
- priv->size_min));
- ret = FALSE;
- }
- if (priv->size_max > 0
- && cgi_file->size > priv->size_max)
- {
- zak_form_element_validator_set_message (validator,
- g_strdup_printf (_("The size cannot be greater than %d."),
- priv->size_max));
- ret = FALSE;
- }
+ ret = zak_form_cgi_form_element_validator_file_size_is_valid (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator), cgi_file->size);
+
if (priv->mime_type != NULL)
{
- gchar **splitted;
- guint l;
- guint i;
- gboolean found;
gchar *mime;
- found = FALSE;
-
mime = g_content_type_get_mime_type (cgi_file->content_type);
if (mime == NULL)
mime = g_strdup (cgi_file->content_type);
}
- splitted = g_strsplit (priv->mime_type, "|", -1);
- l = g_strv_length (splitted);
- for (i = 0; i < l; i++)
- {
- if (g_strcmp0 (mime, splitted[i]) == 0)
- {
- found = TRUE;
- break;
- }
- }
- g_strfreev (splitted);
-
- if (mime != NULL)
- {
- g_free (mime);
- }
-
- if (!found)
- {
- zak_form_element_validator_set_message (validator,
- g_strdup_printf (_("The file must be of type «%s»."),
- priv->mime_type_message != NULL ? priv->mime_type_message : priv->mime_type));
- ret = FALSE;
- }
+ ret = zak_form_cgi_form_element_validator_file_mime_is_valid (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator), mime);
}
}
else if (priv->size_min > 0)
/*
- * Copyright (C) 2016-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2016-2020 Andrea Zagli <azagli@libero.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
#include <libzakformcgi/formelementsubmit.h>
#include <libzakformcgi/formelementtextarea.h>
#include <libzakformcgi/formelementtext.h>
+#include <libzakformcgi/formelementvalidatorfile.h>
#include <libzakformcgi/form.h>
#endif /* __LIBZAKFORMCGI_H__ */