soc.2009.transport: 267a0ac8: Added fallback ReceiveFile if legacy use...

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Sat Aug 8 06:45:59 EDT 2009


-----------------------------------------------------------------
Revision: 267a0ac84edd3b91e76f132a6129daed1bd6ca27
Ancestor: 2f26f8c05c959585563be8d9b9b87bb1984e2f45
Author: hanzz at soc.pidgin.im
Date: 2009-08-08T10:42:39
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/267a0ac84edd3b91e76f132a6129daed1bd6ca27

Modified files:
        CMakeLists.txt filetransfermanager.cpp filetransfermanager.h
        filetransferrepeater.cpp filetransferrepeater.h

ChangeLog: 

Added fallback ReceiveFile if legacy user doesn't support filetransfer

-------------- next part --------------
============================================================
--- CMakeLists.txt	9060def2830375f6df77a8c1e1fcb5875cf87333
+++ CMakeLists.txt	01df60a83a21339a3f65cb27a0ff632a665d16ad
@@ -64,7 +64,6 @@
 	sendfile.cpp
 	thread.cpp
 	gatewayhandler.cpp
-	receivefile.cpp
 	geventloop.cpp
 	autoconnectloop.cpp
 	usermanager.cpp
@@ -108,7 +107,6 @@
 	sendfile.h
 	thread.h
 	gatewayhandler.h
-	receivefile.h
 	log.h
 	geventloop.h
 	autoconnectloop.h
============================================================
--- filetransfermanager.cpp	da8fb02810be8825162231af665c973914b42255
+++ filetransfermanager.cpp	406f38d2b235b85ef50768ae337ded8b34d36130
@@ -72,51 +72,62 @@ void FileTransferManager::handleFTBytest
 void FileTransferManager::handleFTBytestream (Bytestream *bs) {
 	Log().Get("a") << "handleFTBytestream";
 	if (std::find(m_sendlist.begin(), m_sendlist.end(), bs->target().full()) == m_sendlist.end()) {
-		if (m_info[bs->sid()].straight == false){
-			std::string filename = m_info[bs->sid()].filename;
+// 		if (m_info[bs->sid()].straight == false){
+// 			std::string filename = m_info[bs->sid()].filename;
+// 			// replace invalid characters
+// 			for (std::string::iterator it = filename.begin(); it != filename.end(); ++it) {
+// 				if (*it == '\\' || *it == '&' || *it == '/' || *it == '?' || *it == '*' || *it == ':') {
+// 					*it = '_';
+// 				}
+// 			} 
+// 			filename=p->configuration().filetransferCache+"/"+bs->target().username()+"-"+p->j->getID()+"-"+filename;
+// 			
+// 			mutex->lock();
+// 			m_progress[bs->sid()].filename=filename;
+// 			m_progress[bs->sid()].incoming=true;
+// 			m_progress[bs->sid()].state=0;
+// 			m_progress[bs->sid()].user=bs->initiator().bare();
+// 			m_progress[bs->sid()].to=bs->initiator();
+// 			m_progress[bs->sid()].from=bs->target();
+// 			m_progress[bs->sid()].stream=bs;
+// 			std::cout << "FROM:" << bs->initiator().full() << " TO:" << bs->target().full();
+// 			
+// 			mutex->unlock();
+// 			new ReceiveFile(bs,filename, m_info[bs->sid()].size,mutex,this);
+// 		}
+// 		else {
+		std::string filename = "";
+		if (m_info[bs->sid()].straight == false) {
+			filename = m_info[bs->sid()].filename;
 			// replace invalid characters
 			for (std::string::iterator it = filename.begin(); it != filename.end(); ++it) {
 				if (*it == '\\' || *it == '&' || *it == '/' || *it == '?' || *it == '*' || *it == ':') {
 					*it = '_';
 				}
 			} 
-			filename=p->configuration().filetransferCache+"/"+bs->target().username()+"-"+p->j->getID()+"-"+filename;
-			
-			mutex->lock();
-			m_progress[bs->sid()].filename=filename;
-			m_progress[bs->sid()].incoming=true;
-			m_progress[bs->sid()].state=0;
-			m_progress[bs->sid()].user=bs->initiator().bare();
-			m_progress[bs->sid()].to=bs->initiator();
-			m_progress[bs->sid()].from=bs->target();
-			m_progress[bs->sid()].stream=bs;
-			std::cout << "FROM:" << bs->initiator().full() << " TO:" << bs->target().full();
-			
-			mutex->unlock();
-			new ReceiveFile(bs,filename, m_info[bs->sid()].size,mutex,this);
+			filename = p->configuration().filetransferCache + "/" + bs->target().username() + "-" + p->j->getID() + "-" + filename;
 		}
+		User *user = p->userManager()->getUserByJID(bs->initiator().bare());
+		Log().Get("a") << "wants user" << bs->initiator().bare();
+		FiletransferRepeater *repeater = NULL;
+		if (user) {
+			Log().Get("a") << "wants repeater" << bs->target().username();
+			repeater = user->removeFiletransfer(bs->target().username());
+			if (!repeater) return;
+		}
 		else {
-			User *user = p->userManager()->getUserByJID(bs->initiator().bare());
-			Log().Get("a") << "wants user" << bs->initiator().bare();
-			FiletransferRepeater *repeater = NULL;
-			if (user) {
-				Log().Get("a") << "wants repeater" << bs->target().username();
-				repeater = user->removeFiletransfer(bs->target().username());
-				if (!repeater) return;
-			}
-			else {
-				User *user = p->userManager()->getUserByJID(bs->target().bare());
-				if (!user)
-					return;
-				repeater = user->removeFiletransfer(bs->initiator().username());
-				if (!repeater) return;
-			}
-			
-			if (repeater->isSending())
-				repeater->handleFTSendBytestream(bs);
-			else
-				repeater->handleFTReceiveBytestream(bs);
+			User *user = p->userManager()->getUserByJID(bs->target().bare());
+			if (!user)
+				return;
+			repeater = user->removeFiletransfer(bs->initiator().username());
+			if (!repeater) return;
 		}
+		
+		if (repeater->isSending())
+			repeater->handleFTSendBytestream(bs, filename);
+		else
+			repeater->handleFTReceiveBytestream(bs, filename);
+// 		}
     } else {
 		// zatim to nepotrebujem u odchozich filu
 // 		mutex->lock();
============================================================
--- filetransfermanager.h	babc9fb2cb858e6559d779855f70edde18d2b2d7
+++ filetransfermanager.h	63d3890cc2bf99a7cac9b875dfaf79dd5f817f24
@@ -22,7 +22,6 @@
 #define FILETRANSFERMANAGER_H
 
 #include "sendfile.h"
-#include "receivefile.h"
 #include "thread.h"
 
 #include <sys/stat.h>
============================================================
--- filetransferrepeater.cpp	ea1c8d88dc42f66ee0f9944d457eedfaec774016
+++ filetransferrepeater.cpp	45a085d175312677d51779caade13fe07c11d731
@@ -20,6 +20,7 @@
  
 #include "filetransferrepeater.h"
 #include "main.h"
+#include "usermanager.h"
 
 SendFileStraight::SendFileStraight(Bytestream *stream, int size, FiletransferRepeater *manager) {
     std::cout << "SendFileStraight::SendFileStraight" << " Constructor.\n";
@@ -93,8 +94,92 @@ void SendFileStraight::handleBytestreamC
 	std::cout << "socks stream error\n";
 }
 
+static gboolean transferFinished(gpointer data) {
+	ReceiveFile *receive = (ReceiveFile *) data;
+	User *user = receive->user();
+	std::string filename = receive->filename();
+	Log().Get(user->jid()) << "trying to send file "<< filename;
+	if (user->account()){
+		if (user->isConnected()){
+			Log().Get(user->jid()) << "sending download message";
+			std::string basename(g_path_get_basename(filename.c_str()));
+			if (user->isVIP()) {
+				user->p->sql()->addDownload(basename,"1");
+			}
+			else {
+				user->p->sql()->addDownload(basename,"0");
+			}
+			Message s(Message::Chat, receive->target(), "Uzivatel Vam poslal soubor '"+basename+"'. Muzete jej stahnout z adresy http://soumar.jabbim.cz/icq/" + basename +" .");
+			s.setFrom(user->jid());
+			user->receivedMessage(s);
+		}
+	}
+	receive->dispose();
+}
 
+ReceiveFile::ReceiveFile(gloox::Bytestream *stream, int size, const std::string &filename, User *user, FiletransferRepeater *manager) {
+    m_stream = stream;
+    m_size = size;
+	m_filename = filename;
+	m_user = user;
+	m_stream->registerBytestreamDataHandler (this);
+	m_target = stream->target().bare();
+    m_finished = false;
+	m_parent = manager;
+    if(!m_stream->connect()) {
+        Log().Get("ReceiveFile") << "connection can't be established!";
+        return;
+    }
+    run();
+}
 
+ReceiveFile::~ReceiveFile() {
+
+}
+
+void ReceiveFile::dispose() {
+	m_parent->parent()->ft->dispose(m_stream);
+}
+
+void ReceiveFile::exec() {
+	Log().Get("ReceiveFile") << "starting receiveFile thread";
+    m_file.open(m_filename.c_str(), std::ios_base::out | std::ios_base::binary );
+    if (!m_file) {
+        Log().Get(m_filename) << "can't create this file!";
+        return;
+    }
+	while (!m_finished) {
+		m_stream->recv();
+	}
+	m_file.close();
+	g_timeout_add(1000,&transferFinished,this);
+}
+
+void ReceiveFile::handleBytestreamData(gloox::Bytestream *s5b, const std::string &data) {
+	m_file.write(data.c_str(), data.size());
+}
+
+void ReceiveFile::handleBytestreamError(gloox::Bytestream *s5b, const gloox::IQ &iq) {
+	Log().Get("ReceiveFile") << "STREAM ERROR!";
+// 	Log().Get("ReceiveFile") << stanza->xml();
+}
+
+void ReceiveFile::handleBytestreamOpen(gloox::Bytestream *s5b) {
+	Log().Get("ReceiveFile") << "stream opened...";
+}
+
+void ReceiveFile::handleBytestreamClose(gloox::Bytestream *s5b) {
+    if (m_finished){
+		Log().Get("ReceiveFile") << "Transfer finished and we're already finished => deleting receiveFile thread";
+		delete this;
+	}
+	else{
+		Log().Get("ReceiveFile") << "Transfer finished";
+		m_finished = true;
+	}
+}
+
+
 ReceiveFileStraight::ReceiveFileStraight(gloox::Bytestream *stream, int size, FiletransferRepeater *manager) {
     m_stream = stream;
     m_size = size;
@@ -210,12 +295,17 @@ void FiletransferRepeater::requestFT() {
 	m_sid = m_main->ft->requestFT(m_to, filename, purple_xfer_get_size(m_xfer), EmptyString, EmptyString, EmptyString, EmptyString, SIProfileFT::FTTypeAll, m_from);
 }
 
-void FiletransferRepeater::handleFTReceiveBytestream(Bytestream *bs) {
+void FiletransferRepeater::handleFTReceiveBytestream(Bytestream *bs, const std::string &filename) {
 	Log().Get("ReceiveFileStraight") << "new!";
-	m_resender = new ReceiveFileStraight(bs, 0, this);
+	if (filename.empty())
+		m_resender = new ReceiveFileStraight(bs, 0, this);
+	else {
+		User *user = m_main->userManager()->getUserByJID(bs->initiator().bare());
+		m_resender = new ReceiveFile(bs, 0, filename, user, this);
+	}
 }
 
-void FiletransferRepeater::handleFTSendBytestream(Bytestream *bs) {
+void FiletransferRepeater::handleFTSendBytestream(Bytestream *bs, const std::string &filename) {
 	Log().Get("SendFileStraight") << "new!";
 	purple_xfer_request_accepted(m_xfer, NULL);
 	m_resender = new SendFileStraight(bs, 0, this);
============================================================
--- filetransferrepeater.h	ba2a9e9108db0956722db9047d05438149bfffaa
+++ filetransferrepeater.h	44ba8829587203fcb471681931ec4047b443c993
@@ -52,6 +52,33 @@ class AbstractResendClass {
 		MyMutex *m_mutex;
 };
 
+class ReceiveFile : public AbstractResendClass, public BytestreamDataHandler, public Thread {
+	public:
+		ReceiveFile(Bytestream *stream, int size, const std::string &filename, User *user, FiletransferRepeater *manager);
+		~ReceiveFile();
+
+		void exec();
+		void handleBytestreamData(Bytestream *s5b, const std::string &data);
+		void handleBytestreamError(Bytestream *s5b, const IQ &iq);
+		void handleBytestreamOpen(Bytestream *s5b);
+		void handleBytestreamClose(Bytestream *s5b);
+		
+		std::string &filename() { return m_filename; }
+		User *user () { return m_user; }
+		std::string &target() { return m_target; }
+		void dispose();
+	
+	private:
+		Bytestream *m_stream;
+		std::string m_filename;
+		std::string m_target;
+		int m_size;
+		bool m_finished;
+		User *m_user;
+		FiletransferRepeater *m_parent;
+		std::ofstream m_file;
+};
+
 class ReceiveFileStraight : public AbstractResendClass, public BytestreamDataHandler, public Thread {
 	public:
 		ReceiveFileStraight(Bytestream *stream, int size, FiletransferRepeater *manager);
@@ -102,8 +129,8 @@ class FiletransferRepeater {
 		void registerXfer(PurpleXfer *xfer);
 		void fileSendStart();
 		void fileRecvStart();
-		void handleFTReceiveBytestream(Bytestream *bs);
-		void handleFTSendBytestream(Bytestream *bs);
+		void handleFTReceiveBytestream(Bytestream *bs, const std::string &filename = "");
+		void handleFTSendBytestream(Bytestream *bs, const std::string &filename = "");
 		void gotData(const std::string &data);
 		void requestFT();
 		
@@ -113,6 +140,7 @@ class FiletransferRepeater {
 		AbstractResendClass *getResender() { return m_resender; }
 		void wantsData() { m_wantsData = true; }
 		std::ofstream m_file;
+		GlooxMessageHandler *parent() { return m_main; }
 	
 	private:
 		GlooxMessageHandler *m_main;


More information about the Commits mailing list