<commit_before>/*
 *
 *  OBEX Client
 *
 *  Copyright (C) 2011 Intel Corporation
 *
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <gdbus.h>

#include "log.h"

#include "transfer.h"
#include "session.h"
#include "driver.h"
#include "opp.h"

#define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
#define OPP_INTERFACE "org.openobex.ObjectPush"
#define ERROR_INF OPP_INTERFACE ".Error"

struct opp_data {
	struct obc_session *session;
};

static DBusConnection *conn = NULL;

static void send_file_callback(struct obc_session *session,
						struct obc_transfer *transfer,
						GError *err, void *user_data)
{
	DBusMessage *msg = user_data;
	DBusMessage *reply;

	if (err != NULL)
		reply = g_dbus_create_error(msg,
				ERROR_INF ".Failed", "%s", err->message);
	else
		reply = dbus_message_new_method_return(msg);

	g_dbus_send_message(conn, reply);
	dbus_message_unref(msg);
}

static DBusMessage *opp_send_file(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct opp_data *opp = user_data;
	struct obc_transfer *transfer;
	DBusMessage *reply;
	char *filename;
	char *basename;
	GError *err = NULL;

	if (dbus_message_get_args(message, NULL,
					DBUS_TYPE_STRING, &filename,
					DBUS_TYPE_INVALID) == FALSE)
		return g_dbus_create_error(message,
				ERROR_INF ".InvalidArguments", NULL);

	basename = g_path_get_basename(filename);

	transfer = obc_transfer_put(NULL, basename, filename, NULL, 0, &err);

	g_free(basename);

	if (transfer == NULL)
		goto fail;

	if (!obc_session_queue(opp->session, transfer, send_file_callback,
								message, &err))
		goto fail;

	dbus_message_ref(message);
	return NULL;

fail:
	reply = g_dbus_create_error(message,
				ERROR_INF ".Failed", "%s", err->message);
	g_error_free(err);
	return reply;
}

static void pull_complete_callback(struct obc_session *session,
					struct obc_transfer *transfer,
					GError *err, void *user_data)
{
	DBusMessage *message = user_data;

	if (err != NULL) {
		DBusMessage *error = g_dbus_create_error(message,
					"org.openobex.Error.Failed",
					"%s", err->message);
		g_dbus_send_message(conn, error);
		goto done;
	}

	g_dbus_send_reply(conn, message, DBUS_TYPE_INVALID);

done:
	dbus_message_unref(message);
}

static DBusMessage *opp_pull_business_card(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct opp_data *opp = user_data;
	struct obc_transfer *pull;
	DBusMessageIter iter;
	DBusMessage *reply;
	const char *filename = NULL;
	GError *err = NULL;

	dbus_message_iter_init(message, &iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		return g_dbus_create_error(message,
				ERROR_INF ".InvalidArguments", NULL);

	dbus_message_iter_get_basic(&iter, &filename);

	pull = obc_transfer_get("text/x-vcard", NULL, filename, &err);
	if (pull == NULL)
		goto fail;

	if (!obc_session_queue(opp->session, pull, pull_complete_callback,
								message, &err))
		goto fail;

	dbus_message_ref(message);

	return NULL;

fail:
	reply = g_dbus_create_error(message,
				ERROR_INF ".Failed", "%s", err->message);
	g_error_free(err);
	return reply;
}

static DBusMessage *opp_exchange_business_cards(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	return g_dbus_create_error(message, "org.openobex.Error.Failed", NULL);
}

static const GDBusMethodTable opp_methods[] = {
	{ GDBUS_ASYNC_METHOD("SendFile",
		GDBUS_ARGS({ "sourcefile", "s" }),
		NULL,
		opp_send_file) },
	{ GDBUS_ASYNC_METHOD("PullBusinessCard",
		GDBUS_ARGS({ "targetfile", "s" }),
		NULL,
		opp_pull_business_card) },
	{ GDBUS_ASYNC_METHOD("ExchangeBusinessCards",
		GDBUS_ARGS({ "clientfile", "s" }, { "targetfile", "s" }),
		NULL,
		opp_exchange_business_cards) },
	{ }
};

static void opp_free(void *data)
{
	struct opp_data *opp = data;

	obc_session_unref(opp->session);
	g_free(opp);
}

static int opp_probe(struct obc_session *session)
{
	struct opp_data *opp;
	const char *path;

	path = obc_session_get_path(session);

	DBG("%s", path);

	opp = g_try_new0(struct opp_data, 1);
	if (!opp)
		return -ENOMEM;

	opp->session = obc_session_ref(session);

	if (!g_dbus_register_interface(conn, path, OPP_INTERFACE, opp_methods,
						NULL, NULL, opp, opp_free)) {<commit_msg>obexd: ObjectPush sessions return transfers<commit_after>/*
 *
 *  OBEX Client
 *
 *  Copyright (C) 2011 Intel Corporation
 *
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <gdbus.h>

#include "log.h"

#include "transfer.h"
#include "session.h"
#include "driver.h"
#include "opp.h"

#define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
#define OPP_INTERFACE "org.openobex.ObjectPush"
#define ERROR_INF OPP_INTERFACE ".Error"

struct opp_data {
	struct obc_session *session;
};

static DBusConnection *conn = NULL;

static DBusMessage *opp_send_file(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct opp_data *opp = user_data;
	struct obc_transfer *transfer;
	DBusMessage *reply;
	char *filename;
	char *basename;
	GError *err = NULL;

	if (dbus_message_get_args(message, NULL,
					DBUS_TYPE_STRING, &filename,
					DBUS_TYPE_INVALID) == FALSE)
		return g_dbus_create_error(message,
				ERROR_INF ".InvalidArguments", NULL);

	basename = g_path_get_basename(filename);

	transfer = obc_transfer_put(NULL, basename, filename, NULL, 0, &err);

	g_free(basename);

	if (transfer == NULL)
		goto fail;

	if (!obc_session_queue(opp->session, transfer, NULL, NULL, &err))
		goto fail;

	return obc_transfer_create_dbus_reply(transfer, message);

fail:
	reply = g_dbus_create_error(message,
				ERROR_INF ".Failed", "%s", err->message);
	g_error_free(err);
	return reply;
}

static DBusMessage *opp_pull_business_card(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct opp_data *opp = user_data;
	struct obc_transfer *pull;
	DBusMessage *reply;
	const char *filename = NULL;
	GError *err = NULL;

	if (dbus_message_get_args(message, NULL,
				DBUS_TYPE_STRING, &filename,
				DBUS_TYPE_INVALID) == FALSE)
		return g_dbus_create_error(message,
				ERROR_INF ".InvalidArguments", NULL);

	pull = obc_transfer_get("text/x-vcard", NULL, filename, &err);
	if (pull == NULL)
		goto fail;

	if (!obc_session_queue(opp->session, pull, NULL, NULL, &err))
		goto fail;

	return obc_transfer_create_dbus_reply(pull, message);

fail:
	reply = g_dbus_create_error(message,
				ERROR_INF ".Failed", "%s", err->message);
	g_error_free(err);
	return reply;
}

static DBusMessage *opp_exchange_business_cards(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	return g_dbus_create_error(message, "org.openobex.Error.Failed", NULL);
}

static const GDBusMethodTable opp_methods[] = {
	{ GDBUS_METHOD("SendFile",
		GDBUS_ARGS({ "sourcefile", "s" }),
		GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
		opp_send_file) },
	{ GDBUS_METHOD("PullBusinessCard",
		GDBUS_ARGS({ "targetfile", "s" }),
		GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
		opp_pull_business_card) },
	{ GDBUS_METHOD("ExchangeBusinessCards",
		GDBUS_ARGS({ "clientfile", "s" }, { "targetfile", "s" }),
		GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
		opp_exchange_business_cards) },
	{ }
};

static void opp_free(void *data)
{
	struct opp_data *opp = data;

	obc_session_unref(opp->session);
	g_free(opp);
}

static int opp_probe(struct obc_session *session)
{
	struct opp_data *opp;
	const char *path;

	path = obc_session_get_path(session);

	DBG("%s", path);

	opp = g_try_new0(struct opp_data, 1);
	if (!opp)
		return -ENOMEM;

	opp->session = obc_session_ref(session);

	if (!g_dbus_register_interface(conn, path, OPP_INTERFACE, opp_methods,
						NULL, NULL, opp, opp_free)) {
{'fraction_non_alphanumeric': 0.1179787464758187, 'fraction_numerical': 0.01019301669919757, 'mean_word_length': 4.897058823529412, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4779718', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3259, 'n_words': 839}