added back functionality to choose DL directory for each channel

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8581 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2015-07-07 00:52:52 +00:00
parent f91ca2b47e
commit 46a4273668
6 changed files with 283 additions and 68 deletions

View file

@ -97,7 +97,11 @@ virtual bool getRelatedPosts(const uint32_t &token, std::vector<RsGxsChannelPost
virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) = 0; virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) = 0;
virtual bool setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled) = 0; virtual bool setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled) = 0;
virtual bool getChannelAutoDownload(const RsGxsGroupId &groupid) = 0; virtual bool getChannelAutoDownload(const RsGxsGroupId &groupid, bool& enabled) = 0;
virtual bool setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std::string& directory)=0;
virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::string& directory)=0;
//virtual void setChannelAutoDownload(uint32_t& token, const RsGxsGroupId& groupId, bool autoDownload) = 0; //virtual void setChannelAutoDownload(uint32_t& token, const RsGxsGroupId& groupId, bool autoDownload) = 0;
//virtual bool setMessageStatus(const std::string &msgId, const uint32_t status, const uint32_t statusMask); //virtual bool setMessageStatus(const std::string &msgId, const uint32_t status, const uint32_t statusMask);

View file

@ -25,6 +25,8 @@
#include "services/p3gxschannels.h" #include "services/p3gxschannels.h"
#include "serialiser/rsgxschannelitems.h" #include "serialiser/rsgxschannelitems.h"
#include "util/radix64.h"
#include "util/rsmemory.h"
#include <retroshare/rsidentity.h> #include <retroshare/rsidentity.h>
#include <retroshare/rsfiles.h> #include <retroshare/rsfiles.h>
@ -60,7 +62,7 @@ RsGxsChannels *rsGxsChannels = NULL;
#define DUMMYDATA_PERIOD 60 // long enough for some RsIdentities to be generated. #define DUMMYDATA_PERIOD 60 // long enough for some RsIdentities to be generated.
#define CHANNEL_DOWNLOAD_PERIOD (3600 * 24 * 7) #define CHANNEL_DOWNLOAD_PERIOD (3600 * 24 * 7)
#define CHANNEL_MAX_AUTO_DL (1024 * 1024 * 1024) #define CHANNEL_MAX_AUTO_DL (8 * 1024 * 1024 * 1024ull) // 8 GB. Just a security ;-)
/********************************************************************************/ /********************************************************************************/
/******************* Startup / Tick ******************************************/ /******************* Startup / Tick ******************************************/
@ -182,8 +184,9 @@ void p3GxsChannels::notifyChanges(std::vector<RsGxsNotify *> &changes)
std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first; std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first;
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
bool enabled = false ;
if (autoDownloadEnabled(mit->first)) if (autoDownloadEnabled(mit->first, enabled) && enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::notifyChanges() AutoDownload for Group: " << mit->first; std::cerr << "p3GxsChannels::notifyChanges() AutoDownload for Group: " << mit->first;
@ -439,12 +442,83 @@ bool p3GxsChannels::setChannelAutoDownload(const RsGxsGroupId &groupId, bool ena
} }
bool p3GxsChannels::getChannelAutoDownload(const RsGxsGroupId &groupId) bool p3GxsChannels::getChannelAutoDownload(const RsGxsGroupId &groupId, bool& enabled)
{ {
return autoDownloadEnabled(groupId); return autoDownloadEnabled(groupId,enabled);
} }
bool p3GxsChannels::setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std::string& directory)
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::setDownloadDirectory() id: " << groupId << " to: " << directory << std::endl;
#endif
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(groupId);
if (it == mSubscribedGroups.end())
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::setAutoDownload() Missing Group" << std::endl;
#endif
return false;
}
/* extract from ServiceString */
SSGxsChannelGroup ss;
ss.load(it->second.mServiceString);
if (directory == ss.mDownloadDirectory)
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::setDownloadDirectory() WARNING setting looks okay already" << std::endl;
#endif
}
/* we are just going to set it anyway. */
ss.mDownloadDirectory = directory;
std::string serviceString = ss.save();
uint32_t token;
it->second.mServiceString = serviceString; // update Local Cache.
RsGenExchange::setGroupServiceString(token, groupId, serviceString); // update dbase.
/* now reload it */
std::list<RsGxsGroupId> groups;
groups.push_back(groupId);
request_SpecificSubscribedGroups(groups);
return true;
}
bool p3GxsChannels::getChannelDownloadDirectory(const RsGxsGroupId & id,std::string& directory)
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::autoDownloadEnabled(" << id << ")" << std::endl;
#endif
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(id);
if (it == mSubscribedGroups.end())
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::autoDownloadEnabled() No Entry" << std::endl;
#endif
return false;
}
/* extract from ServiceString */
SSGxsChannelGroup ss;
ss.load(it->second.mServiceString);
directory = ss.mDownloadDirectory;
return true ;
}
void p3GxsChannels::request_AllSubscribedGroups() void p3GxsChannels::request_AllSubscribedGroups()
{ {
@ -511,7 +585,9 @@ void p3GxsChannels::load_SubscribedGroups(const uint32_t &token)
#endif #endif
updateSubscribedGroup(*it); updateSubscribedGroup(*it);
if (autoDownloadEnabled(it->mGroupId)) bool enabled = false ;
if (autoDownloadEnabled(it->mGroupId,enabled) && enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::load_SubscribedGroups() remembering AutoDownload Group: " << it->mGroupId; std::cerr << "p3GxsChannels::load_SubscribedGroups() remembering AutoDownload Group: " << it->mGroupId;
@ -706,8 +782,10 @@ void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg)
return; return;
} }
bool enabled = false ;
/* check that autodownload is set */ /* check that autodownload is set */
if (autoDownloadEnabled(msg.mMeta.mGroupId)) if (autoDownloadEnabled(msg.mMeta.mGroupId,enabled) && enabled )
{ {
@ -720,31 +798,39 @@ void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg)
time_t age = time(NULL) - msg.mMeta.mPublishTs; time_t age = time(NULL) - msg.mMeta.mPublishTs;
if (age < (time_t) CHANNEL_DOWNLOAD_PERIOD ) if (age < (time_t) CHANNEL_DOWNLOAD_PERIOD )
{ {
/* start download */ /* start download */
// NOTE WE DON'T HANDLE PRIVATE CHANNELS HERE. // NOTE WE DON'T HANDLE PRIVATE CHANNELS HERE.
// MORE THOUGHT HAS TO GO INTO THAT STUFF. // MORE THOUGHT HAS TO GO INTO THAT STUFF.
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::handleUnprocessedPost() START DOWNLOAD"; std::cerr << "p3GxsChannels::handleUnprocessedPost() START DOWNLOAD";
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
std::list<RsGxsFile>::const_iterator fit; std::list<RsGxsFile>::const_iterator fit;
for(fit = msg.mFiles.begin(); fit != msg.mFiles.end(); ++fit) for(fit = msg.mFiles.begin(); fit != msg.mFiles.end(); ++fit)
{ {
std::string fname = fit->mName; std::string fname = fit->mName;
Sha1CheckSum hash = Sha1CheckSum(fit->mHash); Sha1CheckSum hash = Sha1CheckSum(fit->mHash);
uint64_t size = fit->mSize; uint64_t size = fit->mSize;
std::list<RsPeerId> srcIds;
std::string localpath = "";
TransferRequestFlags flags = RS_FILE_REQ_BACKGROUND | RS_FILE_REQ_ANONYMOUS_ROUTING;
if (size < CHANNEL_MAX_AUTO_DL) std::list<RsPeerId> srcIds;
rsFiles->FileRequest(fname, hash, size, localpath, flags, srcIds); std::string localpath = "";
} TransferRequestFlags flags = RS_FILE_REQ_BACKGROUND | RS_FILE_REQ_ANONYMOUS_ROUTING;
}
if (size < CHANNEL_MAX_AUTO_DL)
{
std::string directory ;
if(getChannelDownloadDirectory(msg.mMeta.mGroupId,directory))
localpath = directory ;
rsFiles->FileRequest(fname, hash, size, localpath, flags, srcIds);
}
else
std::cerr << "WARNING: Channel file is not auto-downloaded because its size exceeds the threshold of " << CHANNEL_MAX_AUTO_DL << " bytes." << std::endl;
}
}
/* mark as processed */ /* mark as processed */
uint32_t token; uint32_t token;
@ -797,7 +883,7 @@ void p3GxsChannels::handleResponse(uint32_t token, uint32_t req_type)
/********************************************************************************************/ /********************************************************************************************/
bool p3GxsChannels::autoDownloadEnabled(const RsGxsGroupId &id) bool p3GxsChannels::autoDownloadEnabled(const RsGxsGroupId &id,bool& enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::autoDownloadEnabled(" << id << ")"; std::cerr << "p3GxsChannels::autoDownloadEnabled(" << id << ")";
@ -820,38 +906,68 @@ bool p3GxsChannels::autoDownloadEnabled(const RsGxsGroupId &id)
/* extract from ServiceString */ /* extract from ServiceString */
SSGxsChannelGroup ss; SSGxsChannelGroup ss;
ss.load(it->second.mServiceString); ss.load(it->second.mServiceString);
return ss.mAutoDownload; enabled = ss.mAutoDownload;
}
#define RSGXSCHANNEL_MAX_SERVICE_STRING 128 return true ;
}
bool SSGxsChannelGroup::load(const std::string &input) bool SSGxsChannelGroup::load(const std::string &input)
{ {
char line[RSGXSCHANNEL_MAX_SERVICE_STRING]; int download_val;
int download_val; mAutoDownload = false;
mAutoDownload = false; mDownloadDirectory.clear();
if (1 == sscanf(input.c_str(), "D:%d", &download_val))
{ RsTemporaryMemory tmpmem(input.length());
if (download_val == 1)
{ if (1 == sscanf(input.c_str(), "D:%d", &download_val))
mAutoDownload = true; {
} if (download_val == 1)
} mAutoDownload = true;
return true; }
else if( 2 == sscanf(input.c_str(),"v2 {D:%d} {P:%[^}]}",&download_val,(unsigned char*)tmpmem))
{
if (download_val == 1)
mAutoDownload = true;
std::vector<uint8_t> vals = Radix64::decode(std::string((char*)(unsigned char *)tmpmem)) ;
mDownloadDirectory = std::string((char*)vals.data(),vals.size());
}
else if( 1 == sscanf(input.c_str(),"v2 {D:%d}",&download_val))
{
if (download_val == 1)
mAutoDownload = true;
}
else
{
std::cerr << "SSGxsChannelGroup::load(): could not parse string \"" << input << "\"" << std::endl;
return false ;
}
std::cerr << "DECODED STRING: autoDL=" << mAutoDownload << ", directory=\"" << mDownloadDirectory << "\"" << std::endl;
return true;
} }
std::string SSGxsChannelGroup::save() const std::string SSGxsChannelGroup::save() const
{ {
std::string output; std::string output = "v2 ";
if (mAutoDownload)
{ if (mAutoDownload)
output += "D:1"; output += "{D:1}";
} else
else output += "{D:0}";
{
output += "D:0"; if(!mDownloadDirectory.empty())
} {
return output; std::string encoded_str ;
Radix64::encode(mDownloadDirectory.c_str(),mDownloadDirectory.length(),encoded_str);
output += " {P:" + encoded_str + "}";
}
std::cerr << "ENCODED STRING: " << output << std::endl;
return output;
} }
bool p3GxsChannels::setAutoDownload(const RsGxsGroupId &groupId, bool enabled) bool p3GxsChannels::setAutoDownload(const RsGxsGroupId &groupId, bool enabled)

View file

@ -50,6 +50,7 @@ class SSGxsChannelGroup
std::string save() const; std::string save() const;
bool mAutoDownload; bool mAutoDownload;
std::string mDownloadDirectory ;
}; };
@ -97,7 +98,9 @@ virtual bool updateGroup(uint32_t &token, RsGxsChannelGroup &group);
// no tokens... should be cached. // no tokens... should be cached.
virtual bool setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled); virtual bool setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled);
virtual bool getChannelAutoDownload(const RsGxsGroupId &groupid); virtual bool getChannelAutoDownload(const RsGxsGroupId &groupid, bool& enabled);
virtual bool setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std::string& directory);
virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::string& directory);
/* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */ /* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */
virtual bool getCommentData(const uint32_t &token, std::vector<RsGxsComment> &msgs) virtual bool getCommentData(const uint32_t &token, std::vector<RsGxsComment> &msgs)
@ -173,7 +176,7 @@ static uint32_t channelsAuthenPolicy();
void updateSubscribedGroup(const RsGroupMetaData &group); void updateSubscribedGroup(const RsGroupMetaData &group);
void clearUnsubscribedGroup(const RsGxsGroupId &id); void clearUnsubscribedGroup(const RsGxsGroupId &id);
bool setAutoDownload(const RsGxsGroupId &groupId, bool enabled); bool setAutoDownload(const RsGxsGroupId &groupId, bool enabled);
bool autoDownloadEnabled(const RsGxsGroupId &id); bool autoDownloadEnabled(const RsGxsGroupId &id, bool &enabled);

View file

@ -19,6 +19,11 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
****************************************************************/ ****************************************************************/
#include <QMenu>
#include <QFileDialog>
#include <retroshare/rsfiles.h>
#include "GxsChannelDialog.h" #include "GxsChannelDialog.h"
#include "GxsChannelGroupDialog.h" #include "GxsChannelGroupDialog.h"
#include "GxsChannelPostsWidget.h" #include "GxsChannelPostsWidget.h"
@ -138,19 +143,101 @@ GxsMessageFrameWidget *GxsChannelDialog::createMessageFrameWidget(const RsGxsGro
return new GxsChannelPostsWidget(groupId); return new GxsChannelPostsWidget(groupId);
} }
void GxsChannelDialog::setDefaultDirectory()
{
RsGxsGroupId grpId = groupId() ;
if (grpId.isNull())
return ;
rsGxsChannels->setChannelDownloadDirectory(grpId,"") ;
}
void GxsChannelDialog::specifyDownloadDirectory()
{
RsGxsGroupId grpId = groupId() ;
if (grpId.isNull())
return ;
QString dir = QFileDialog::getExistingDirectory(NULL,tr("Select channel download directory")) ;
if(dir.isNull())
return ;
rsGxsChannels->setChannelDownloadDirectory(grpId,std::string(dir.toUtf8())) ;
}
void GxsChannelDialog::setDownloadDirectory()
{
RsGxsGroupId grpId = groupId() ;
if (grpId.isNull())
return ;
QAction *action = qobject_cast<QAction*>(sender()) ;
if(!action)
return ;
QString directory = action->data().toString() ;
rsGxsChannels->setChannelDownloadDirectory(grpId,std::string(directory.toUtf8())) ;
}
void GxsChannelDialog::groupTreeCustomActions(RsGxsGroupId grpId, int subscribeFlags, QList<QAction*> &actions) void GxsChannelDialog::groupTreeCustomActions(RsGxsGroupId grpId, int subscribeFlags, QList<QAction*> &actions)
{ {
bool isSubscribed = IS_GROUP_SUBSCRIBED(subscribeFlags); bool isSubscribed = IS_GROUP_SUBSCRIBED(subscribeFlags);
bool autoDownload = rsGxsChannels->getChannelAutoDownload(grpId); bool autoDownload ;
rsGxsChannels->getChannelAutoDownload(grpId,autoDownload);
if (isSubscribed) if (isSubscribed)
{ {
QAction *action = autoDownload ? (new QAction(QIcon(":/images/redled.png"), tr("Disable Auto-Download"), this)) QAction *action = autoDownload ? (new QAction(QIcon(":/images/redled.png"), tr("Disable Auto-Download"), this))
: (new QAction(QIcon(":/images/start.png"),tr("Enable Auto-Download"), this)); : (new QAction(QIcon(":/images/start.png"),tr("Enable Auto-Download"), this));
connect(action, SIGNAL(triggered()), this, SLOT(toggleAutoDownload())); connect(action, SIGNAL(triggered()), this, SLOT(toggleAutoDownload()));
actions.append(action); actions.append(action);
}
std::string dl_directory;
rsGxsChannels->getChannelDownloadDirectory(grpId,dl_directory) ;
QMenu *mnu = new QMenu(tr("Set download directory")) ;
if(dl_directory.empty())
mnu->addAction(QIcon(":/images/start.png"),tr("[Default directory]"), this, SLOT(setDefaultDirectory())) ;
else
mnu->addAction(tr("[Default directory]"), this, SLOT(setDefaultDirectory())) ;
std::list<SharedDirInfo> lst ;
rsFiles->getSharedDirectories(lst) ;
bool found = false ;
for(std::list<SharedDirInfo>::const_iterator it(lst.begin());it!=lst.end();++it)
{
QAction *action = NULL;
if(dl_directory == it->filename)
{
action = new QAction(QIcon(":/images/start.png"),QString::fromUtf8(it->filename.c_str()),NULL) ;
found = true ;
}
else
action = new QAction(QString::fromUtf8(it->filename.c_str()),NULL) ;
connect(action,SIGNAL(triggered()),this,SLOT(setDownloadDirectory())) ;
action->setData(QString::fromUtf8(it->filename.c_str())) ;
mnu->addAction(action) ;
}
if(!found && !dl_directory.empty())
{
QAction *action = new QAction(QIcon(":/images/start.png"),QString::fromUtf8(dl_directory.c_str()),NULL) ;
connect(action,SIGNAL(triggered()),this,SLOT(setDownloadDirectory())) ;
action->setData(QString::fromUtf8(dl_directory.c_str())) ;
mnu->addAction(action) ;
}
mnu->addAction(tr("Specify..."), this, SLOT(specifyDownloadDirectory())) ;
actions.push_back( mnu->menuAction()) ;
}
} }
RsGxsCommentService *GxsChannelDialog::getCommentService() RsGxsCommentService *GxsChannelDialog::getCommentService()
@ -170,8 +257,9 @@ void GxsChannelDialog::toggleAutoDownload()
return; return;
} }
bool autoDownload = rsGxsChannels->getChannelAutoDownload(grpId); bool autoDownload ;
if (!rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
if(!rsGxsChannels->getChannelAutoDownload(grpId,autoDownload) || !rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
{ {
std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set"; std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set";
std::cerr << std::endl; std::cerr << std::endl;

View file

@ -51,6 +51,9 @@ protected:
private slots: private slots:
void toggleAutoDownload(); void toggleAutoDownload();
void setDefaultDirectory();
void setDownloadDirectory();
void specifyDownloadDirectory();
private: private:
/* GxsGroupFrameDialog */ /* GxsGroupFrameDialog */

View file

@ -249,7 +249,8 @@ void GxsChannelPostsWidget::insertChannelDetails(const RsGxsChannelGroup &group)
ui->subscribeToolButton->setSubscribed(IS_GROUP_SUBSCRIBED(group.mMeta.mSubscribeFlags)); ui->subscribeToolButton->setSubscribed(IS_GROUP_SUBSCRIBED(group.mMeta.mSubscribeFlags));
bool autoDownload = rsGxsChannels->getChannelAutoDownload(group.mMeta.mGroupId); bool autoDownload ;
rsGxsChannels->getChannelAutoDownload(group.mMeta.mGroupId,autoDownload);
setAutoDownload(autoDownload); setAutoDownload(autoDownload);
if (IS_GROUP_SUBSCRIBED(group.mMeta.mSubscribeFlags)) { if (IS_GROUP_SUBSCRIBED(group.mMeta.mSubscribeFlags)) {
@ -464,8 +465,8 @@ void GxsChannelPostsWidget::toggleAutoDownload()
return; return;
} }
bool autoDownload = rsGxsChannels->getChannelAutoDownload(grpId); bool autoDownload ;
if (!rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload)) if(!rsGxsChannels->getChannelAutoDownload(grpId,autoDownload) || !rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
{ {
std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set"; std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set";
std::cerr << std::endl; std::cerr << std::endl;