<commit_before>/*
 *      This file is part of GPaste.
 *
 *      Copyright 2011 <NAME> <<EMAIL>>
 *
 *      GPaste 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, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      GPaste is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with GPaste.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gpaste-clipboard-private.h"

#define G_PASTE_CLIPBOARD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_PASTE_TYPE_CLIPBOARD, GPasteClipboardPrivate))

G_DEFINE_TYPE (GPasteClipboard, g_paste_clipboard, G_TYPE_OBJECT)

struct _GPasteClipboardPrivate
{
    GdkAtom target;
    GtkClipboard *real;
    GPasteSettings *settings;
    gchar *text;
    gchar *image_checksum;
};

static GdkAtom g_paste_clipboard_copy_files_target;

/**
 * g_paste_clipboard_get_target:
 * @self: a GPasteClipboard instance
 *
 * Get the target the GPasteClipboard points to
 *
 * Returns: (transfer none): the GdkAtom representing the target (Primary, Clipboard, ...)
 */
GdkAtom
g_paste_clipboard_get_target (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->target;
}

/**
 * g_paste_clipboard_get_real:
 * @self: a GPasteClipboard instance
 *
 * Get the GtkClipboard linked to the GPasteClipboard
 *
 * Returns: (transfer none): the GtkClipboard used in the GPasteClipboard
 */
GtkClipboard *
g_paste_clipboard_get_real (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->real;
}

/**
 * g_paste_clipboard_get_text:
 * @self: a GPasteClipboard instance
 *
 * Get the text stored in the GPasteClipboard
 *
 * Returns: read-only string containing the text or NULL
 */
const gchar *
g_paste_clipboard_get_text (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->text;
}

static void
_g_paste_clipboard_set_text (GPasteClipboard *self,
                             const gchar     *text)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARD (self));

    GPasteClipboardPrivate *priv = self->priv;

    g_free (priv->text);
    g_free (priv->image_checksum);

    priv->text = g_strdup (text);
    priv->image_checksum = NULL;
}

/**
 * g_paste_clipboard_set_text:
 * @self: a GPasteClipboard instance
 *
 * Put the text from the intern GtkClipboard in the GPasteClipboard
 *
 * Returns: The new text if it was modified, or NULL
 */
const gchar *
g_paste_clipboard_set_text (GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    GPasteClipboardPrivate *priv = self->priv;
    gchar *text = gtk_clipboard_wait_for_text (priv->real);

    if (!text)
        return NULL;

    gboolean trim_items = g_paste_settings_get_trim_items (priv->settings);
    gchar *stripped = g_strstrip (g_strdup (text));

    if ((g_strcmp0 (stripped, "") == 0) ||
        (priv->text &&
            (g_strcmp0 (priv->text, (trim_items ? stripped : text)) == 0)))
                return NULL;

    if (!stripped)
    {
        _g_paste_clipboard_set_text (self, text);
        goto out;
    }

    if (priv->target == GDK_SELECTION_CLIPBOARD &&
        g_strcmp0 (text, stripped) != 0)
            g_paste_clipboard_select_text (self, stripped);
    else
        _g_paste_clipboard_set_text (self, stripped);

out:
    g_free (stripped);
    g_free (text);

    return priv->text;
}<commit_msg>libgpaste: fix a memleak reported by lzap<commit_after>/*
 *      This file is part of GPaste.
 *
 *      Copyright 2011 <NAME> <<EMAIL>>
 *
 *      GPaste 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, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      GPaste is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with GPaste.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gpaste-clipboard-private.h"

#define G_PASTE_CLIPBOARD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_PASTE_TYPE_CLIPBOARD, GPasteClipboardPrivate))

G_DEFINE_TYPE (GPasteClipboard, g_paste_clipboard, G_TYPE_OBJECT)

struct _GPasteClipboardPrivate
{
    GdkAtom target;
    GtkClipboard *real;
    GPasteSettings *settings;
    gchar *text;
    gchar *image_checksum;
};

static GdkAtom g_paste_clipboard_copy_files_target;

/**
 * g_paste_clipboard_get_target:
 * @self: a GPasteClipboard instance
 *
 * Get the target the GPasteClipboard points to
 *
 * Returns: (transfer none): the GdkAtom representing the target (Primary, Clipboard, ...)
 */
GdkAtom
g_paste_clipboard_get_target (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->target;
}

/**
 * g_paste_clipboard_get_real:
 * @self: a GPasteClipboard instance
 *
 * Get the GtkClipboard linked to the GPasteClipboard
 *
 * Returns: (transfer none): the GtkClipboard used in the GPasteClipboard
 */
GtkClipboard *
g_paste_clipboard_get_real (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->real;
}

/**
 * g_paste_clipboard_get_text:
 * @self: a GPasteClipboard instance
 *
 * Get the text stored in the GPasteClipboard
 *
 * Returns: read-only string containing the text or NULL
 */
const gchar *
g_paste_clipboard_get_text (const GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    return self->priv->text;
}

static void
_g_paste_clipboard_set_text (GPasteClipboard *self,
                             const gchar     *text)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARD (self));

    GPasteClipboardPrivate *priv = self->priv;

    g_free (priv->text);
    g_free (priv->image_checksum);

    priv->text = g_strdup (text);
    priv->image_checksum = NULL;
}

/**
 * g_paste_clipboard_set_text:
 * @self: a GPasteClipboard instance
 *
 * Put the text from the intern GtkClipboard in the GPasteClipboard
 *
 * Returns: The new text if it was modified, or NULL
 */
const gchar *
g_paste_clipboard_set_text (GPasteClipboard *self)
{
    g_return_val_if_fail (G_PASTE_IS_CLIPBOARD (self), NULL);

    GPasteClipboardPrivate *priv = self->priv;
    gchar *text = gtk_clipboard_wait_for_text (priv->real);

    if (!text)
        return NULL;

    gboolean trim_items = g_paste_settings_get_trim_items (priv->settings);
    gchar *stripped = g_strstrip (g_strdup (text));
    const gchar *ret = NULL;

    if ((g_strcmp0 (stripped, "") == 0) ||
        (priv->text &&
            (g_strcmp0 (priv->text, (trim_items ? stripped : text)) == 0)))
                goto ignore;

    if (!stripped)
    {
        _g_paste_clipboard_set_text (self, text);
        goto out;
    }

    if (priv->target == GDK_SELECTION_CLIPBOARD &&
        g_strcmp0 (text, stripped) != 0)
            g_paste_clipboard_select_text (self, stripped);
    else
        _g_paste_clipboard_set_text (self, stripped);

out:
    ret = priv->text;

ignore:
    g_free (stripped);
    g_free (text);

    return ret;
}
{'fraction_non_alphanumeric': 0.09378588745693506, 'fraction_numerical': 0.0028071966313640423, 'mean_word_length': 3.4916905444126076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '788803', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2612, 'n_words': 711}