Example #1
0
static void
backup_callback (const gchar *value,
                 gpointer     user_data)
{
    GPasteSettingsUiStackPrivate *priv = user_data;
    GError *error = NULL;

    g_paste_client_backup_history (priv->client, value, &error);

    if (g_paste_settings_ui_check_connection_error (error))
        return;

    g_paste_settings_ui_stack_private_refill_histories (priv);
}
Example #2
0
int
main (int argc, char *argv[])
{
    bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    int status = EXIT_SUCCESS;

    g_type_init ();

    GPasteClient *client = g_paste_client_new ();
    GError *error = NULL;
    const gchar *arg1, *arg2;

    if (!client)
    {
        if (argc == 2 && is_help (argv[1]))
        {
            show_help (argv[0]);
            return EXIT_SUCCESS;
        }

        failure_exit ();
    }

    if (!isatty (fileno (stdin)))
    {
        /* We are being piped */
        GString *data = g_string_new ("");
        gchar c;

        while ((c = fgetc (stdin)) != EOF)
            data = g_string_append_c (data, c);

        data->str[data->len - 1] = '\0';

        g_paste_client_add (client, data->str, &error);

        g_string_free (data, TRUE);
    }
    else
    {
        switch (argc)
        {
        case 1:
            show_history (client, FALSE, FALSE, &error);
            break;
        case 2:
            arg1 = argv[1];
            if (is_help (arg1))
            {
                show_help (argv[0]);
            }
            else if (g_strcmp0 (arg1, "start") == 0 ||
                     g_strcmp0 (arg1, "d") == 0 ||
                     g_strcmp0 (arg1, "daemon") == 0)
            {
                g_paste_client_track (client, TRUE, &error);
            }
            else if (g_strcmp0 (arg1, "stop") == 0 ||
                     g_strcmp0 (arg1, "q") == 0 ||
                     g_strcmp0 (arg1, "quit") == 0)
            {
                g_paste_client_track (client, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "e") == 0 ||
                     g_strcmp0 (arg1, "empty") == 0)
            {
                g_paste_client_empty (client, &error);
            }
            else if (g_strcmp0 (arg1, "v") == 0 ||
                     g_strcmp0 (arg1, "version") == 0 ||
                     g_strcmp0 (arg1, "-v") == 0 ||
                     g_strcmp0 (arg1, "--version") == 0)
            {
                printf ("%s\n", PACKAGE_STRING);
            }
#ifdef ENABLE_APPLET
            else if (g_strcmp0 (arg1, "applet") == 0)
            {
                g_spawn_command_line_async (PKGLIBEXECDIR "/gpaste-applet", &error);
                if (error)
                {
                    fprintf (stderr, _("Couldn't spawn gpaste-applet.\n"));
                    g_clear_error (&error);
                    status = EXIT_FAILURE;
                }
            }
#endif
            else if (g_strcmp0 (arg1, "s") == 0 ||
                     g_strcmp0 (arg1, "settings") == 0 ||
                     g_strcmp0 (arg1, "p") == 0 ||
                     g_strcmp0 (arg1, "preferences") == 0)
            {
                execl (PKGLIBEXECDIR "/gpaste-settings", "GPaste-Settings", NULL);
            }
            else if (g_strcmp0 (arg1, "dr") == 0 ||
                     g_strcmp0 (arg1, "daemon-reexec") == 0)
            {
                g_paste_client_reexecute (client, &error);
                if (error && error->code == G_DBUS_ERROR_NO_REPLY)
                {
                    printf (_("Successfully reexecuted the daemon\n"));
                }
            }
            else if (g_strcmp0 (arg1, "h") == 0 ||
                     g_strcmp0 (arg1, "history") == 0)
            {
                show_history (client, FALSE, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "rh") == 0 ||
                     g_strcmp0 (arg1, "raw-history") == 0)
            {
                show_history (client, TRUE, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "zh") == 0 ||
                     g_strcmp0 (arg1, "zero-history") == 0)
            {
                show_history (client, FALSE, TRUE, &error);
            }
            else if (g_strcmp0 (arg1, "lh") == 0 ||
                     g_strcmp0 (arg1, "list-histories") == 0)
            {
                gchar **histories = g_paste_client_list_histories (client, &error);
                if (!error)
                {
                    for (gchar **h = histories; *h; ++h)
                        printf ("%s\n", *h);
                    g_strfreev (histories);
                }
            }
            else
            {
                show_help (argv[0]);
                status = EXIT_FAILURE;
            }
            break;
        case 3:
            arg1 = argv[1];
            arg2 = argv[2];
            if (g_strcmp0 (arg1, "bh") == 0||
                g_strcmp0 (arg1, "backup-history") == 0)
            {
                g_paste_client_backup_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "sh") == 0 ||
                     g_strcmp0 (arg1, "switch-history") == 0)
            {
                g_paste_client_switch_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "dh") == 0 ||
                     g_strcmp0 (arg1, "delete-history") == 0)
            {
                g_paste_client_delete_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "a") == 0 ||
                     g_strcmp0 (arg1, "add") == 0)
            {
                g_paste_client_add (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "g") == 0||
                     g_strcmp0 (arg1, "get") == 0)
            {
                printf ("%s", g_paste_client_get_element (client, g_ascii_strtoull (arg2, NULL, 0), &error));
            }
            else if (g_strcmp0 (arg1, "s") == 0 ||
                     g_strcmp0 (arg1, "set") == 0 ||
                     g_strcmp0 (arg1, "select") == 0)
            {
                g_paste_client_select (client, g_ascii_strtoull (arg2, NULL, 0), &error);
            }
            else if (g_strcmp0 (arg1, "d") == 0 ||
                     g_strcmp0 (arg1, "delete") == 0)
            {
                g_paste_client_delete (client, g_ascii_strtoull (arg2, NULL, 0), &error);
            }
            else if (g_strcmp0 (arg1, "f") == 0 ||
                     g_strcmp0 (arg1, "file") == 0)
            {
               gchar *content = NULL;

               if (g_file_get_contents (arg2,
                                        &content,
                                        NULL, /* length */
                                        &error))
               {
                   g_paste_client_add (client, content, &error);
                   g_free (content);
               }
               else
               {
                   fprintf (stderr, _("Could not read file: %s\n"), arg2);
                   g_clear_error (&error);
                   status = EXIT_FAILURE;
               }
            }
            else
            {
                show_help (argv[0]);
                status = EXIT_FAILURE;
            }
            break;
        default:
            show_help (argv[0]);
            status = EXIT_FAILURE;
            break;
        }
    }

    if (error)
    {
        g_error_free (error);
        failure_exit ();
    }

    g_object_unref (client);

    return status;
}