Kaydet (Commit) c9f27f64 authored tarafından Will Thompson's avatar Will Thompson Kaydeden (comit) Matúš Kukan

tubes: Add preliminary API for sending a file

Crashes if the file transfer fails.

file-transfer-helper.[ch] are, as their license headers state, LGPL
v2.1+. They come from Empathy. I am in the process of refactoring them
so they can live in a future version of telepathy-glib (which is itself
LGPL v2.1+), so their presence here is temporary.
üst 9bcf81c5
......@@ -61,4 +61,8 @@ $(eval $(call gb_Library_add_exception_objects,tubes,\
tubes/source/contact-list \
))
$(eval $(call gb_Library_add_cobjects,tubes,\
tubes/source/file-transfer-helper \
))
# vim:set shiftwidth=4 tabstop=4 noexpandtab: */
......@@ -31,8 +31,10 @@
#include <sal/config.h>
#include "tubes/packet.hxx"
#include "tubes/file-transfer-helper.h"
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <rtl/ustring.hxx>
#include <telepathy-glib/telepathy-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <queue>
......@@ -68,6 +70,10 @@ public:
/** Queue incoming data as TelePacket */
void queue( const char* pDBusSender, const char* pPacket, int nSize );
typedef void (*FileSentCallback)( bool aSuccess, void* pUserData);
void sendFile( rtl::OUString &localUri, FileSentCallback pCallback, void* pUserData);
// --- following only to be called only by manager's callbacks ---
// TODO: make friends instead
......
/*
* empathy-ft-handler.h - Header for EmpathyFTHandler
* Copyright (C) 2009 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
*/
/* empathy-ft-handler.h */
#ifndef __EMPATHY_FT_HANDLER_H__
#define __EMPATHY_FT_HANDLER_H__
#include <glib-object.h>
#include <gio/gio.h>
#include <telepathy-glib/contact.h>
#include <telepathy-glib/file-transfer-channel.h>
G_BEGIN_DECLS
#define EMPATHY_TYPE_FT_HANDLER empathy_ft_handler_get_type()
#define EMPATHY_FT_HANDLER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \
EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandler))
#define EMPATHY_FT_HANDLER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), \
EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerClass))
#define EMPATHY_IS_FT_HANDLER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_FT_HANDLER))
#define EMPATHY_IS_FT_HANDLER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), EMPATHY_TYPE_FT_HANDLER))
#define EMPATHY_FT_HANDLER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), \
EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerClass))
typedef struct _EmpathyFTHandlerPriv EmpathyFTHandlerPriv;
typedef struct {
GObject parent;
EmpathyFTHandlerPriv *priv;
} EmpathyFTHandler;
typedef struct {
GObjectClass parent_class;
} EmpathyFTHandlerClass;
#define EMPATHY_FT_ERROR_QUARK g_quark_from_static_string ("EmpathyFTError")
typedef enum {
EMPATHY_FT_ERROR_FAILED,
EMPATHY_FT_ERROR_HASH_MISMATCH,
EMPATHY_FT_ERROR_TP_ERROR,
EMPATHY_FT_ERROR_SOCKET,
EMPATHY_FT_ERROR_NOT_SUPPORTED,
EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE
} EmpathyFTErrorEnum;
/**
* EmpathyFTHandlerReadyCallback:
* @handler: the handler which is now ready
* @error: a #GError if the operation failed, or %NULL
* @user_data: user data passed to the callback
*/
typedef void (* EmpathyFTHandlerReadyCallback) (EmpathyFTHandler *handler,
GError *error,
gpointer user_data);
GType empathy_ft_handler_get_type (void);
/* public methods */
void empathy_ft_handler_new_outgoing (
TpAccount *account,
TpContact *contact,
GFile *source,
gint64 action_time,
EmpathyFTHandlerReadyCallback callback,
gpointer user_data);
void empathy_ft_handler_new_incoming (TpFileTransferChannel *channel,
EmpathyFTHandlerReadyCallback callback,
gpointer user_data);
void empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
GFile *destination);
void empathy_ft_handler_start_transfer (EmpathyFTHandler *handler);
void empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler);
/* properties of the transfer */
const char * empathy_ft_handler_get_filename (EmpathyFTHandler *handler);
const char * empathy_ft_handler_get_content_type (EmpathyFTHandler *handler);
TpContact * empathy_ft_handler_get_contact (EmpathyFTHandler *handler);
GFile * empathy_ft_handler_get_gfile (EmpathyFTHandler *handler);
gboolean empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler);
gboolean empathy_ft_handler_is_incoming (EmpathyFTHandler *handler);
guint64 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler);
guint64 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler);
gboolean empathy_ft_handler_is_completed (EmpathyFTHandler *handler);
gboolean empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler);
G_END_DECLS
#endif /* __EMPATHY_FT_HANDLER_H__ */
......@@ -138,6 +138,8 @@ public:
*/
bool popPacket( TelePacket& rPacket );
void sendFile( rtl::OUString &localUri, TeleConference::FileSentCallback pCallback, void* pUserData);
/// Only for use with MainLoopFlusher
GMainLoop* getMainLoop() const;
......
......@@ -59,6 +59,7 @@ public:
void testStartBuddySession2();
void testSendPacket();
void testReceivePacket();
void testSendFile();
void testFlushLoops();
void testDestroyManager1();
void testDestroyManager2();
......@@ -81,6 +82,7 @@ public:
CPPUNIT_TEST( testStartBuddySession2 );
CPPUNIT_TEST( testSendPacket );
CPPUNIT_TEST( testReceivePacket );
CPPUNIT_TEST( testSendFile );
CPPUNIT_TEST( testFlushLoops );
CPPUNIT_TEST( testDestroyManager1 );
CPPUNIT_TEST( testDestroyManager2 );
......@@ -285,6 +287,25 @@ void TestTeleTubes::testReceivePacket()
CPPUNIT_ASSERT( nReceivedPackets == nSentPackets);
}
static void TestTeleTubes_FileSent( bool success, void *user_data)
{
TestTeleTubes *self = reinterpret_cast<TestTeleTubes *>(user_data);
CPPUNIT_ASSERT( success);
g_main_loop_quit (self->mpMainLoop);
}
void TestTeleTubes::testSendFile()
{
TpAccount *pAcc1 = mpManager1->getAccount(maOffererIdentifier);
CPPUNIT_ASSERT( pAcc1 != 0);
/* This has to run after testContactList has run successfully. */
CPPUNIT_ASSERT( mpAccepterContact != 0);
mpManager1->sendFile( maTestConfigIniURL,
TestTeleTubes_FileSent, this);
spinMainLoop();
}
void TestTeleTubes::testFlushLoops()
{
mpManager1->flushLoop();
......
......@@ -445,6 +445,82 @@ void TeleConference::queue( const char* pDBusSender, const char* pPacketData, in
}
class SendFileRequest {
public:
SendFileRequest( TeleConference *pSelf,
TeleConference::FileSentCallback pCallback, void* pUserData)
: mpSelf(pSelf)
, mpCallback(pCallback)
, mpUserData(pUserData) {};
/* FIXME: make a shared pointer? */
TeleConference* mpSelf;
TeleConference::FileSentCallback mpCallback;
void* mpUserData;
};
static void TeleConference_TransferDone( EmpathyFTHandler *handler, TpFileTransferChannel *, gpointer user_data)
{
SendFileRequest *request = reinterpret_cast<SendFileRequest *>(user_data);
request->mpCallback(true, request->mpUserData);
delete request;
g_object_unref (handler);
}
static void TeleConference_TransferError( EmpathyFTHandler *handler, const GError *error, gpointer user_data)
{
SendFileRequest *request = reinterpret_cast<SendFileRequest *>(user_data);
SAL_INFO( "tubes", "TeleConference_TransferError: " << error->message);
request->mpCallback(false, request->mpUserData);
delete request;
g_object_unref (handler);
}
static void TeleConference_FTReady( EmpathyFTHandler *handler, GError *error, gpointer user_data)
{
SendFileRequest *request = reinterpret_cast<SendFileRequest *>(user_data);
if ( error != 0 )
{
request->mpCallback(error == 0, request->mpUserData);
delete request;
g_object_unref (handler);
}
else
{
g_signal_connect(handler, "transfer-done",
G_CALLBACK (TeleConference_TransferDone), request);
g_signal_connect(handler, "transfer-error",
G_CALLBACK (TeleConference_TransferError), request);
empathy_ft_handler_start_transfer(handler);
}
}
void TeleConference::sendFile( rtl::OUString &localUri, FileSentCallback pCallback, void* pUserData)
{
INFO_LOGGER( "TeleConference::sendFile");
SAL_WARN_IF( ( !mpAccount || !mpChannel), "tubes",
"can't send a file before the tube is set up");
if ( !mpAccount || !mpChannel)
return;
GFile *pSource = g_file_new_for_uri(
OUStringToOString( localUri, RTL_TEXTENCODING_UTF8).getStr() );
SendFileRequest *pReq = new SendFileRequest( this, pCallback, pUserData);
empathy_ft_handler_new_outgoing( mpAccount,
tp_channel_get_target_contact( mpChannel),
pSource,
0,
&TeleConference_FTReady, pReq);
}
bool TeleConference::popPacket( TelePacket& rPacket )
{
INFO_LOGGER( "TeleConference::popPacket");
......
This diff is collapsed.
......@@ -591,6 +591,17 @@ bool TeleManager::popPacket( TelePacket& rPacket )
return false;
}
void TeleManager::sendFile( rtl::OUString &localUri, TeleConference::FileSentCallback pCallback, void* pUserData)
{
INFO_LOGGER( "TeleManager::sendFile");
/* TODO: pluralize */
for (TeleConferenceVector::const_iterator it = maConferences.begin(); it != maConferences.end(); ++it)
{
(*it)->sendFile( localUri, pCallback, pUserData);
return;
}
}
void TeleManager::unregisterConference( TeleConferencePtr pConference )
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment