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

#include <glib.h>
#include <string.h>

#define G_PASTE_DAEMON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_PASTE_TYPE_DAEMON, GPasteDaemonPrivate))

#define G_PASTE_BUS_NAME "org.gnome.GPaste"

G_DEFINE_TYPE (GPasteDaemon, g_paste_daemon, G_TYPE_OBJECT)

struct _GPasteDaemonPrivate
{
    GDBusConnection         *connection;
    gchar                   *object_path;
    GPasteHistory           *history;
    GPasteSettings          *settings;
    GPasteClipboardsManager *clipboards_manager;
    GPasteKeybinder         *keybinder;
    GDBusNodeInfo           *g_paste_daemon_dbus_info;
    GDBusInterfaceVTable     g_paste_daemon_dbus_vtable;
};

enum
{
    REEXECUTE_SELF,

    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
g_paste_daemon_send_dbus_reply (GDBusConnection       *connection,
                                GDBusMethodInvocation *invocation,
                                GVariant              *reply)
{
    GDBusMessage *reply_message = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));

    if (!reply)
        reply = g_variant_new_tuple (NULL, 0);

    g_dbus_message_set_body (reply_message, reply);
    g_dbus_connection_send_message (connection,
                                    reply_message,
                                    G_DBUS_SEND_MESSAGE_FLAGS_NONE,
                                    NULL, /* out serial */
                                    NULL); /* error */
    g_object_unref (reply_message);
}

static void
g_paste_daemon_get_history (GPasteDaemon          *self,
                            GDBusConnection       *connection,
                            GDBusMethodInvocation *invocation)
{
    GPasteDaemonPrivate *priv = self->priv;
    GSList *history = g_paste_history_get_history (priv->history);
    guint length = MIN (g_slist_length (history), g_paste_settings_get_max_displayed_history_size (priv->settings));
    const gchar **displayed_history = g_new (const gchar *, length + 1);

    for (guint i = 0; i < length; ++i, history = g_slist_next (history))
        displayed_history[i] = g_paste_item_get_display_string (history->data);
    displayed_history[length] = NULL;

    GVariant *variant = g_variant_new_strv (displayed_history, -1);

    g_free (displayed_history);
    g_paste_daemon_send_dbus_reply (connection, invocation, g_variant_new_tuple (&variant, 1));
}

static gchar *
g_paste_daemon_get_dbus_string_parameter (GVariant *parameters,
                                          gsize    *length)
{
    GVariantIter parameters_iter;

    g_variant_iter_init (&parameters_iter, parameters);

    GVariant *variant = g_variant_iter_next_value (&parameters_iter);
    gchar *value = g_variant_dup_string (variant, length);

    g_variant_unref (variant);

    return value;
}

static void
g_paste_daemon_backup_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    GPasteDaemonPrivate *priv = self->priv;
    GPasteSettings *settings = priv->settings;

    gchar *old_name = g_strdup (g_paste_settings_get_history_name (settings));

    g_paste_settings_set_history_name (settings, name);
    g_paste_history_save (priv->history);
    g_paste_settings_set_history_name (settings, old_name);

    g_free (old_name);
    g_free (name);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_switch_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    g_paste_history_switch (self->priv->history, name);

    g_free (name);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_delete_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    GPasteDaemonPrivate *priv = self->priv;
    GPasteHistory *history = priv->history;

    gchar *old_history = g_strdup (g_paste_settings_get_history_name (priv->settings));

    g_paste_history_switch (history, name);
    g_paste_history_delete (history);
    g_paste_history_switch (history, old_history);

    g_free (name);
    g_free (old_history);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_list_histories (GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation)
{
    gchar **history_names = g_paste_history_list ();
    GVariant *variant = g_variant_new_strv ((const gchar **) history_names, -1);

    g_strfreev (history_names);

    g_paste_daemon_send_dbus_reply (connection, invocation, g_variant_new_tuple (&variant, 1));
}

static void<commit_msg>gpasted: fallback history<commit_after>/*
 *      This file is part of GPaste.
 *
 *      Copyright 2011-2012 <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-daemon-private.h"

#include <glib.h>
#include <string.h>

#define G_PASTE_DAEMON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_PASTE_TYPE_DAEMON, GPasteDaemonPrivate))

#define DEFAULT_HISTORY "history"
#define G_PASTE_BUS_NAME "org.gnome.GPaste"

G_DEFINE_TYPE (GPasteDaemon, g_paste_daemon, G_TYPE_OBJECT)

struct _GPasteDaemonPrivate
{
    GDBusConnection         *connection;
    gchar                   *object_path;
    GPasteHistory           *history;
    GPasteSettings          *settings;
    GPasteClipboardsManager *clipboards_manager;
    GPasteKeybinder         *keybinder;
    GDBusNodeInfo           *g_paste_daemon_dbus_info;
    GDBusInterfaceVTable     g_paste_daemon_dbus_vtable;
};

enum
{
    REEXECUTE_SELF,

    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
g_paste_daemon_send_dbus_reply (GDBusConnection       *connection,
                                GDBusMethodInvocation *invocation,
                                GVariant              *reply)
{
    GDBusMessage *reply_message = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));

    if (!reply)
        reply = g_variant_new_tuple (NULL, 0);

    g_dbus_message_set_body (reply_message, reply);
    g_dbus_connection_send_message (connection,
                                    reply_message,
                                    G_DBUS_SEND_MESSAGE_FLAGS_NONE,
                                    NULL, /* out serial */
                                    NULL); /* error */
    g_object_unref (reply_message);
}

static void
g_paste_daemon_get_history (GPasteDaemon          *self,
                            GDBusConnection       *connection,
                            GDBusMethodInvocation *invocation)
{
    GPasteDaemonPrivate *priv = self->priv;
    GSList *history = g_paste_history_get_history (priv->history);
    guint length = MIN (g_slist_length (history), g_paste_settings_get_max_displayed_history_size (priv->settings));
    const gchar **displayed_history = g_new (const gchar *, length + 1);

    for (guint i = 0; i < length; ++i, history = g_slist_next (history))
        displayed_history[i] = g_paste_item_get_display_string (history->data);
    displayed_history[length] = NULL;

    GVariant *variant = g_variant_new_strv (displayed_history, -1);

    g_free (displayed_history);
    g_paste_daemon_send_dbus_reply (connection, invocation, g_variant_new_tuple (&variant, 1));
}

static gchar *
g_paste_daemon_get_dbus_string_parameter (GVariant *parameters,
                                          gsize    *length)
{
    GVariantIter parameters_iter;

    g_variant_iter_init (&parameters_iter, parameters);

    GVariant *variant = g_variant_iter_next_value (&parameters_iter);
    gchar *value = g_variant_dup_string (variant, length);

    g_variant_unref (variant);

    return value;
}

static void
g_paste_daemon_backup_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    GPasteDaemonPrivate *priv = self->priv;
    GPasteSettings *settings = priv->settings;

    gchar *old_name = g_strdup (g_paste_settings_get_history_name (settings));

    g_paste_settings_set_history_name (settings, name);
    g_paste_history_save (priv->history);
    g_paste_settings_set_history_name (settings, old_name);

    g_free (old_name);
    g_free (name);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_switch_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    g_paste_history_switch (self->priv->history, name);

    g_free (name);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_delete_history (GPasteDaemon          *self,
                               GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation,
                               GVariant              *parameters)
{
    gchar *name = g_paste_daemon_get_dbus_string_parameter (parameters, NULL);

    g_return_if_fail (name != NULL);

    GPasteDaemonPrivate *priv = self->priv;
    GPasteHistory *history = priv->history;

    gchar *old_history = g_strdup (g_paste_settings_get_history_name (priv->settings));

    g_paste_history_switch (history, name);
    g_paste_history_delete (history);
    g_paste_history_switch (history, (g_strcmp0 (name, old_history) == 0) ? DEFAULT_HISTORY : old_history);

    g_free (name);
    g_free (old_history);

    g_paste_daemon_send_dbus_reply (connection, invocation, NULL);
}

static void
g_paste_daemon_list_histories (GDBusConnection       *connection,
                               GDBusMethodInvocation *invocation)
{
    gchar **history_names = g_paste_history_list ();
    GVariant *variant = g_variant_new_strv ((const gchar **) history_names, -1);

    g_strfreev (history_names);

    g_paste_daemon_send_dbus_reply (connection, invocation, g_variant_new_tuple (&variant, 1));
}

static void
{'fraction_non_alphanumeric': 0.07625444488344528, 'fraction_numerical': 0.0028447254049782694, 'mean_word_length': 2.655690352397458, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '331468', 'n_tokens_mistral': 3952, 'n_tokens_neox': 3773, 'n_words': 841}