<commit_before>/*
 *      This file is part of GPaste.
 *
 *      Copyright 2011-2014 <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-clipboards-manager-private.h"

#include <gpaste-image-item.h>
#include <gpaste-text-item.h>
#include <gpaste-uris-item.h>

struct _GPasteClipboardsManagerPrivate
{
    GSList         *clipboards;
    GPasteHistory  *history;
    GPasteSettings *settings;

    gulong          selected_signal;
};

G_DEFINE_TYPE_WITH_PRIVATE (GPasteClipboardsManager, g_paste_clipboards_manager, G_TYPE_OBJECT)

static void
g_paste_clipboards_manager_add_clipboard_finish (GPasteClipboardsManagerPrivate *priv,
                                                 GPasteClipboard                *clipboard)
{
    if (!g_paste_clipboard_get_text (clipboard) &&
        !g_paste_clipboard_get_image_checksum (clipboard))
    {
        const GSList *history = g_paste_history_get_history (priv->history);
        if (history)
            g_paste_clipboard_select_item (clipboard, history->data);
    }
}

static void
g_paste_clipboards_manager_on_text_ready (GPasteClipboard *clipboard,
                                          const gchar     *text G_GNUC_UNUSED,
                                          gpointer         user_data)
{
    g_paste_clipboards_manager_add_clipboard_finish (user_data, clipboard);
}

static void
g_paste_clipboards_manager_on_image_ready (GPasteClipboard *clipboard,
                                           GdkPixbuf       *image,
                                           gpointer         user_data)
{
    g_clear_object (&image);
    g_paste_clipboards_manager_add_clipboard_finish (user_data, clipboard);
}

/**
 * g_paste_clipboards_manager_add_clipboard:
 * @self: a #GPasteClipboardsManager instance
 * @clipboard: (transfer none): the GPasteClipboard to add
 *
 * Add a #GPasteClipboard to the #GPasteClipboardsManager
 *
 * Returns:
 */
G_PASTE_VISIBLE void
g_paste_clipboards_manager_add_clipboard (GPasteClipboardsManager *self,
                                          GPasteClipboard         *clipboard)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARDS_MANAGER (self));
    g_return_if_fail (G_PASTE_IS_CLIPBOARD (clipboard));

    GPasteClipboardsManagerPrivate *priv = g_paste_clipboards_manager_get_instance_private (self);
    GtkClipboard *real = g_paste_clipboard_get_real (clipboard);

    priv->clipboards = g_slist_prepend (priv->clipboards, g_object_ref (clipboard));

    if (gtk_clipboard_wait_is_uris_available (real) ||
        gtk_clipboard_wait_is_text_available (real))
    {
        g_paste_clipboard_set_text (clipboard,
                                    g_paste_clipboards_manager_on_text_ready,
                                    priv);
    }
    else if (gtk_clipboard_wait_is_image_available (real))
    {
        g_paste_clipboard_set_image (clipboard,
                                     g_paste_clipboards_manager_on_image_ready,
                                     priv);
    }
}

/**
 * g_paste_clipboards_manager_sync_from_to:
 * @self: a #GPasteClipboardsManager instance
 * @from: the Source clipboard type
 * @to: the destination clipboard type
 *
 * Sync a clipboard into another
 *
 * Returns:
 */
G_PASTE_VISIBLE void
g_paste_clipboards_manager_sync_from_to (GPasteClipboardsManager *self,
                                         GdkAtom                  from,
                                         GdkAtom                  to)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARDS_MANAGER (self));

    GPasteClipboardsManagerPrivate *priv = g_paste_clipboards_manager_get_instance_private (self);
    GtkClipboard *_from = NULL, *_to = NULL;

    for (GSList *clipboard = priv->clipboards; clipboard; clipboard = g_slist_next (clipboard))
    {
        GPasteClipboard *clip = clipboard->data;
        GdkAtom cur = g_paste_clipboard_get_target (clip);

        if (cur == from)
            _from = g_paste_clipboard_get_real (clip);
        else if (cur == to)
            _to = g_paste_clipboard_get_real (clip);
    }

    if (_from && _to)
    {
        gchar *text = gtk_clipboard_wait_for_text (_from);
        if (text)
            gtk_clipboard_set_text (_to, text, -1);
    }
}

static void
g_paste_clipboards_manager_notify_finish (GPasteClipboardsManagerPrivate *priv,
                                          GPasteClipboard                *clipboard,
                                          GPasteItem                     *item,
                                          const gchar                    *synchronized_text,
                                          gboolean                        something_in_clipboard)
{
    GPasteHistory *history = priv->history;

    if (item)
        g_paste_history_add (history, item);

    if (!something_in_clipboard)
    {
        const GSList *hist = g_paste_history_get_history (history);
        if (hist)
            g_paste_clipboard_select_item (clipboard, hist->data);<commit_msg>core: no more sync in clipboard handling<commit_after>/*
 *      This file is part of GPaste.
 *
 *      Copyright 2011-2014 <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-clipboards-manager-private.h"

#include <gpaste-image-item.h>
#include <gpaste-text-item.h>
#include <gpaste-uris-item.h>

struct _GPasteClipboardsManagerPrivate
{
    GSList         *clipboards;
    GPasteHistory  *history;
    GPasteSettings *settings;

    gulong          selected_signal;
};

G_DEFINE_TYPE_WITH_PRIVATE (GPasteClipboardsManager, g_paste_clipboards_manager, G_TYPE_OBJECT)

static void
g_paste_clipboards_manager_add_clipboard_finish (GPasteClipboardsManagerPrivate *priv,
                                                 GPasteClipboard                *clipboard)
{
    if (!g_paste_clipboard_get_text (clipboard) &&
        !g_paste_clipboard_get_image_checksum (clipboard))
    {
        const GSList *history = g_paste_history_get_history (priv->history);
        if (history)
            g_paste_clipboard_select_item (clipboard, history->data);
    }
}

static void
g_paste_clipboards_manager_on_text_ready (GPasteClipboard *clipboard,
                                          const gchar     *text G_GNUC_UNUSED,
                                          gpointer         user_data)
{
    g_paste_clipboards_manager_add_clipboard_finish (user_data, clipboard);
}

static void
g_paste_clipboards_manager_on_image_ready (GPasteClipboard *clipboard,
                                           GdkPixbuf       *image,
                                           gpointer         user_data)
{
    g_clear_object (&image);
    g_paste_clipboards_manager_add_clipboard_finish (user_data, clipboard);
}

/**
 * g_paste_clipboards_manager_add_clipboard:
 * @self: a #GPasteClipboardsManager instance
 * @clipboard: (transfer none): the GPasteClipboard to add
 *
 * Add a #GPasteClipboard to the #GPasteClipboardsManager
 *
 * Returns:
 */
G_PASTE_VISIBLE void
g_paste_clipboards_manager_add_clipboard (GPasteClipboardsManager *self,
                                          GPasteClipboard         *clipboard)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARDS_MANAGER (self));
    g_return_if_fail (G_PASTE_IS_CLIPBOARD (clipboard));

    GPasteClipboardsManagerPrivate *priv = g_paste_clipboards_manager_get_instance_private (self);
    GtkClipboard *real = g_paste_clipboard_get_real (clipboard);

    priv->clipboards = g_slist_prepend (priv->clipboards, g_object_ref (clipboard));

    if (gtk_clipboard_wait_is_uris_available (real) ||
        gtk_clipboard_wait_is_text_available (real))
    {
        g_paste_clipboard_set_text (clipboard,
                                    g_paste_clipboards_manager_on_text_ready,
                                    priv);
    }
    else if (gtk_clipboard_wait_is_image_available (real))
    {
        g_paste_clipboard_set_image (clipboard,
                                     g_paste_clipboards_manager_on_image_ready,
                                     priv);
    }
}

static void
g_paste_clipboards_manager_sync_ready (GtkClipboard *clipboard G_GNUC_UNUSED,
                                       const gchar  *text,
                                       gpointer user_data)
{
    if (text)
        gtk_clipboard_set_text (user_data, text, -1);
}

/**
 * g_paste_clipboards_manager_sync_from_to:
 * @self: a #GPasteClipboardsManager instance
 * @from: the Source clipboard type
 * @to: the destination clipboard type
 *
 * Sync a clipboard into another
 *
 * Returns:
 */
G_PASTE_VISIBLE void
g_paste_clipboards_manager_sync_from_to (GPasteClipboardsManager *self,
                                         GdkAtom                  from,
                                         GdkAtom                  to)
{
    g_return_if_fail (G_PASTE_IS_CLIPBOARDS_MANAGER (self));

    GPasteClipboardsManagerPrivate *priv = g_paste_clipboards_manager_get_instance_private (self);
    GtkClipboard *_from = NULL, *_to = NULL;

    for (GSList *clipboard = priv->clipboards; clipboard; clipboard = g_slist_next (clipboard))
    {
        GPasteClipboard *clip = clipboard->data;
        GdkAtom cur = g_paste_clipboard_get_target (clip);

        if (cur == from)
            _from = g_paste_clipboard_get_real (clip);
        else if (cur == to)
            _to = g_paste_clipboard_get_real (clip);
    }

    if (_from && _to)
    {
        gtk_clipboard_request_text (_from,
                                    g_paste_clipboards_manager_sync_ready,
                                    _to);
    }
}

static void
g_paste_clipboards_manager_notify_finish (GPasteClipboardsManagerPrivate *priv,
                                          GPasteClipboard                *clipboard,
                                          GPasteItem                     *item,
                                          const gchar                    *synchronized_text,
                                          gboolean                        something_in_clipboard)
{
    GPasteHistory *history = priv->history;

    if (item)
        g_paste_history_add (history, item);

    if (!something_in_clipboard)
    {
        const GSList *hist = g_paste_history_get_history (history);
        if (hist)
            g_paste_clipboard_select_item (clipboard, hist->data);
{'fraction_non_alphanumeric': 0.06719709417971115, 'fraction_numerical': 0.0017296549338406987, 'mean_word_length': 2.1621547716707683, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3425953', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3264, 'n_words': 732}