From 9a8f693f3d840038248529ebe21d7e9ec6021466 Mon Sep 17 00:00:00 2001 From: defnax Date: Thu, 14 Jan 2010 13:03:43 +0000 Subject: [PATCH] Added attaching files feature for Forums, not working properly yet. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2038 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- retroshare-gui/src/gui/ForumsDialog.cpp | 48 +++++++++ retroshare-gui/src/gui/ForumsDialog.h | 2 + .../src/gui/forums/CreateForumMsg.cpp | 97 ++++++++++++++++++- .../src/gui/forums/CreateForumMsg.h | 9 +- .../src/gui/forums/CreateForumMsg.ui | 34 +++++++ 5 files changed, 188 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/ForumsDialog.cpp b/retroshare-gui/src/gui/ForumsDialog.cpp index a8d282e1b..0797d9f30 100644 --- a/retroshare-gui/src/gui/ForumsDialog.cpp +++ b/retroshare-gui/src/gui/ForumsDialog.cpp @@ -93,6 +93,8 @@ ForumsDialog::ForumsDialog(QWidget *parent) connect( ui.threadTreeWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( changedThread2() ) ); connect( ui.viewBox, SIGNAL( currentIndexChanged ( int ) ), this, SLOT( insertThreads() ) ); + connect(ui.postText, SIGNAL( anchorClicked(const QUrl &)), SLOT(anchorClicked(const QUrl &))); + connect(ui.expandButton, SIGNAL(clicked()), this, SLOT(togglefileview())); @@ -1065,3 +1067,49 @@ void ForumsDialog::replytomessage() } } +void ForumsDialog::anchorClicked (const QUrl& link ) +{ + #ifdef FORUM_DEBUG + std::cerr << "ForumsDialog::anchorClicked link.scheme() : " << link.scheme().toStdString() << std::endl; + #endif + + if (link.scheme() == "file") { + std::string fileName = link.queryItemValue(QString("fileName")).toStdString(); + std::string fileHash = link.queryItemValue(QString("fileHash")).toStdString(); + uint32_t fileSize = link.queryItemValue(QString("fileSize")).toInt(); + + #ifdef FORUM_DEBUG + std::cerr << "ForumsDialog::anchorClicked FileRequest : fileName : " << fileName << ". fileHash : " << fileHash << ". fileSize : " << fileSize; + std::cerr << ". source id : " << dialogId << std::endl; + #endif + + if (fileName != "" && fileHash != "") { + + std::list srcIds; + //srcIds.push_front(dialogId); + rsFiles->FileRequest(fileName, fileHash, fileSize, "", 0, srcIds); + + QMessageBox mb(tr("File Request Confirmation"), tr("The file has been added to your download list."),QMessageBox::Information,QMessageBox::Ok,0,0); + mb.setButtonText( QMessageBox::Ok, "OK" ); + mb.exec(); + } + else + { + QMessageBox mb(tr("File Request Error"), tr("The file link is malformed."),QMessageBox::Information,QMessageBox::Ok,0,0); + mb.setButtonText( QMessageBox::Ok, "OK" ); + mb.exec(); + } + } + else if (link.scheme() == "http") + { + QDesktopServices::openUrl(link); + } + else if (link.scheme() == "") + { + //it's probably a web adress, let's add http:// at the beginning of the link + QString newAddress = link.toString(); + newAddress.prepend("http://"); + QDesktopServices::openUrl(QUrl(newAddress)); + } + +} \ No newline at end of file diff --git a/retroshare-gui/src/gui/ForumsDialog.h b/retroshare-gui/src/gui/ForumsDialog.h index 963a04c99..72496f32e 100644 --- a/retroshare-gui/src/gui/ForumsDialog.h +++ b/retroshare-gui/src/gui/ForumsDialog.h @@ -37,6 +37,8 @@ public: void loadForumEmoticons(); + + void anchorClicked (const QUrl &); private slots: diff --git a/retroshare-gui/src/gui/forums/CreateForumMsg.cpp b/retroshare-gui/src/gui/forums/CreateForumMsg.cpp index 4406e7dc1..c74c63fbf 100644 --- a/retroshare-gui/src/gui/forums/CreateForumMsg.cpp +++ b/retroshare-gui/src/gui/forums/CreateForumMsg.cpp @@ -24,9 +24,27 @@ #include #include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "rsiface/rsforums.h" +#include "rsiface/rsfiles.h" + +#include "gui/feeds/SubFileItem.h" + +#include + /** Constructor */ CreateForumMsg::CreateForumMsg(std::string fId, std::string pId) @@ -42,6 +60,8 @@ CreateForumMsg::CreateForumMsg(std::string fId, std::string pId) connect( ui.postmessage_action, SIGNAL( triggered (bool) ), this, SLOT( createMsg( ) ) ); connect( ui.close_action, SIGNAL( triggered (bool) ), this, SLOT( cancelMsg( ) ) ); connect( ui.emoticonButton, SIGNAL(clicked()), this, SLOT(smileyWidgetForums())); + connect( ui.attachFileButton, SIGNAL(clicked() ), this , SLOT(addFile())); + newMsg(); @@ -280,4 +300,79 @@ void CreateForumMsg::smileyWidgetForums() void CreateForumMsg::addSmileys() { ui.forumMessage->setText(ui.forumMessage->toHtml() + qobject_cast(sender())->toolTip().split("|").first()); -} \ No newline at end of file +} + +void CreateForumMsg::addFile() +{ + // select a file + QString qfile = QFileDialog::getOpenFileName(this, tr("Add Extra File"), "", "", 0, + QFileDialog::DontResolveSymlinks); + std::string filePath = qfile.toStdString(); + if (filePath != "") + { + CreateForumMsg::addAttachment(filePath); + } +} + +void CreateForumMsg::addAttachment(std::string filePath) { + /* add a SubFileItem to the attachment section */ + std::cerr << "CreateForumMsg::addFile() hashing file."; + std::cerr << std::endl; + + /* add widget in for new destination */ + SubFileItem *file = new SubFileItem(filePath); + //file-> + + ui.vboxLayout->addWidget(file, 1, 0); + + //when the file is local or is finished hashing, call the fileHashingFinished method to send a chat message + if (file->getState() == SFI_STATE_LOCAL) { + fileHashingFinished(file); + } else { + QObject::connect(file,SIGNAL(fileFinished(SubFileItem *)), SLOT(fileHashingFinished(SubFileItem *))) ; + } +} + +void CreateForumMsg::fileHashingFinished(SubFileItem* file) { + std::cerr << "CreateForumMsg::fileHashingFinished() started."; + std::cerr << std::endl; + + //check that the file is ok tos end + if (file->getState() == SFI_STATE_ERROR) { + #ifdef CHAT_DEBUG + std::cerr << "CreateForumMsg::fileHashingFinished error file is not hashed."; + #endif + return; + } + + //convert fileSize from uint_64 to string for html link + char fileSizeChar [100]; + sprintf(fileSizeChar, "%lld", file->FileSize()); + std::string fileSize = *(&fileSizeChar); + + std::string mesgString = "" + + "file:?fileHash=" + (file->FileHash()) + "&fileName=" + (file->FileName()) + "&fileSize=" + fileSize + ""; +#ifdef CHAT_DEBUG + std::cerr << "CreateForumMsg::anchorClicked mesgString : " << mesgString << std::endl; +#endif + + const char * messageString = mesgString.c_str (); + + //convert char massageString to w_char + wchar_t* message; + int requiredSize = mbstowcs(NULL, messageString, 0); // C4996 + /* Add one to leave room for the NULL terminator */ + message = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t )); + if (! message) { + std::cerr << ("Memory allocation failure.\n"); + } + int size = mbstowcs( message, messageString, requiredSize + 1); // C4996 + if (size == (size_t) (-1)) { + printf("Couldn't convert string--invalid multibyte character.\n"); + } + + ui.forumMessage->setHtml(QString::fromStdWString(message)); + +} + + diff --git a/retroshare-gui/src/gui/forums/CreateForumMsg.h b/retroshare-gui/src/gui/forums/CreateForumMsg.h index 7bf9d968d..fa91ef5f7 100644 --- a/retroshare-gui/src/gui/forums/CreateForumMsg.h +++ b/retroshare-gui/src/gui/forums/CreateForumMsg.h @@ -26,6 +26,8 @@ #include #include +#include "gui/feeds/SubFileItem.h" + #include "ui_CreateForumMsg.h" class CreateForumMsg : public QMainWindow @@ -38,6 +40,8 @@ public: void newMsg(); /* cleanup */ void loadEmoticonsForums(); + + void fileHashingFinished(SubFileItem* file); private slots: @@ -47,7 +51,10 @@ private slots: void smileyWidgetForums(); void addSmileys(); - + void addFile(); + void addAttachment(std::string); + + private: std::string mForumId; diff --git a/retroshare-gui/src/gui/forums/CreateForumMsg.ui b/retroshare-gui/src/gui/forums/CreateForumMsg.ui index 7f2538dd3..138c7b9d7 100644 --- a/retroshare-gui/src/gui/forums/CreateForumMsg.ui +++ b/retroshare-gui/src/gui/forums/CreateForumMsg.ui @@ -88,6 +88,23 @@ + + + + + + + + :/images/add-share24.png:/images/add-share24.png + + + + 24 + 24 + + + + @@ -112,6 +129,23 @@ + + + + + 16777215 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + +