2007-12-21 16:51:42 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
2007-12-21 16:51:42 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-12-21 16:51:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-20 23:58:09 +00:00
|
|
|
#include <errno.h> /* EINVAL */
|
2014-07-08 00:08:43 +00:00
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
#include "transmission.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "magnet.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "session.h" /* tr_sessionFindTorrentFile() */
|
|
|
|
#include "torrent.h" /* tr_ctorGetSave() */
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-04-02 20:43:42 +00:00
|
|
|
#include "utils.h" /* tr_new0 */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2007-12-21 16:51:42 +00:00
|
|
|
|
|
|
|
struct optional_args
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isSet_paused;
|
|
|
|
bool isSet_connected;
|
|
|
|
bool isSet_downloadDir;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isPaused;
|
|
|
|
uint16_t peerLimit;
|
|
|
|
char* downloadDir;
|
2007-12-21 16:51:42 +00:00
|
|
|
};
|
|
|
|
|
2008-05-23 20:04:41 +00:00
|
|
|
/** Opaque class used when instantiating torrents.
|
2008-09-23 19:11:04 +00:00
|
|
|
* @ingroup tr_ctor */
|
2007-12-21 16:51:42 +00:00
|
|
|
struct tr_ctor
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_session const* session;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool saveInOurTorrentsDir;
|
|
|
|
bool doDelete;
|
|
|
|
|
|
|
|
tr_priority_t bandwidthPriority;
|
|
|
|
bool isSet_metainfo;
|
|
|
|
bool isSet_delete;
|
|
|
|
tr_variant metainfo;
|
|
|
|
char* sourceFile;
|
|
|
|
|
|
|
|
struct optional_args optionalArgs[2];
|
|
|
|
|
|
|
|
char* cookies;
|
|
|
|
char* incompleteDir;
|
|
|
|
|
|
|
|
tr_file_index_t* want;
|
|
|
|
tr_file_index_t wantSize;
|
|
|
|
tr_file_index_t* notWant;
|
|
|
|
tr_file_index_t notWantSize;
|
|
|
|
tr_file_index_t* low;
|
|
|
|
tr_file_index_t lowSize;
|
|
|
|
tr_file_index_t* normal;
|
|
|
|
tr_file_index_t normalSize;
|
|
|
|
tr_file_index_t* high;
|
|
|
|
tr_file_index_t highSize;
|
2007-12-21 16:51:42 +00:00
|
|
|
};
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void setSourceFile(tr_ctor* ctor, char const* sourceFile)
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ctor->sourceFile);
|
|
|
|
ctor->sourceFile = tr_strdup(sourceFile);
|
2008-02-13 02:24:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void clearMetainfo(tr_ctor* ctor)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ctor->isSet_metainfo)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2013-08-24 18:18:38 +00:00
|
|
|
ctor->isSet_metainfo = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&ctor->metainfo);
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
2008-02-13 02:24:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setSourceFile(ctor, NULL);
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:30:29 +00:00
|
|
|
int tr_ctorSetMetainfo(tr_ctor* ctor, void const* metainfo, size_t len)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
|
|
|
int err;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
clearMetainfo(ctor);
|
|
|
|
err = tr_variantFromBenc(&ctor->metainfo, metainfo, len);
|
2019-07-14 12:40:41 +00:00
|
|
|
ctor->isSet_metainfo = err == 0;
|
2007-12-21 16:51:42 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_ctorGetSourceFile(tr_ctor const* ctor)
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
|
|
|
return ctor->sourceFile;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, char const* magnet_link)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int err;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_magnet_info* magnet_info = tr_magnetParse(magnet_link);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (magnet_info == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
err = -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t len;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant tmp;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* str;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_magnetCreateMetainfo(magnet_info, &tmp);
|
|
|
|
str = tr_variantToStr(&tmp, TR_VARIANT_FMT_BENC, &len);
|
2017-04-20 16:02:19 +00:00
|
|
|
err = tr_ctorSetMetainfo(ctor, (uint8_t const*)str, len);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(str);
|
|
|
|
tr_variantFree(&tmp);
|
|
|
|
tr_magnetFree(magnet_info);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* metainfo;
|
|
|
|
size_t len;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
metainfo = tr_loadFile(filename, &len, NULL);
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (metainfo != NULL && len != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
err = tr_ctorSetMetainfo(ctor, metainfo, len);
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
clearMetainfo(ctor);
|
2007-12-21 16:51:42 +00:00
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setSourceFile(ctor, filename);
|
2008-02-13 02:24:12 +00:00
|
|
|
|
2008-01-03 16:01:29 +00:00
|
|
|
/* if no `name' field was set, then set it from the filename */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ctor->isSet_metainfo)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* info;
|
|
|
|
|
|
|
|
if (tr_variantDictFindDict(&ctor->metainfo, TR_KEY_info, &info))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
if (!tr_variantDictFindStr(info, TR_KEY_name_utf_8, &name, NULL) &&
|
|
|
|
!tr_variantDictFindStr(info, TR_KEY_name, &name, NULL))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
name = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (tr_str_is_empty(name))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* base = tr_sys_path_basename(filename, NULL);
|
|
|
|
|
2016-03-13 10:41:52 +00:00
|
|
|
if (base != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddStr(info, TR_KEY_name, base);
|
|
|
|
tr_free(base);
|
|
|
|
}
|
2008-01-03 16:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(metainfo);
|
2007-12-21 16:51:42 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_ctorSetMetainfoFromHash(tr_ctor* ctor, char const* hashString)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* filename;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
filename = tr_sessionFindTorrentFile(ctor->session, hashString);
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (filename == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
err = EINVAL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-14 14:39:13 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
err = tr_ctorSetMetainfoFromFile(ctor, filename);
|
|
|
|
}
|
2007-12-21 16:51:42 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorSetFilePriorities(tr_ctor* ctor, tr_file_index_t const* files, tr_file_index_t fileCount, tr_priority_t priority)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t** myfiles;
|
|
|
|
tr_file_index_t* mycount;
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (priority)
|
|
|
|
{
|
|
|
|
case TR_PRI_LOW:
|
|
|
|
myfiles = &ctor->low;
|
|
|
|
mycount = &ctor->lowSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_PRI_HIGH:
|
|
|
|
myfiles = &ctor->high;
|
|
|
|
mycount = &ctor->highSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default /*TR_PRI_NORMAL*/:
|
|
|
|
myfiles = &ctor->normal;
|
|
|
|
mycount = &ctor->normalSize;
|
|
|
|
break;
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(*myfiles);
|
|
|
|
*myfiles = tr_memdup(files, sizeof(tr_file_index_t) * fileCount);
|
2009-04-02 20:43:42 +00:00
|
|
|
*mycount = fileCount;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorInitTorrentPriorities(tr_ctor const* ctor, tr_torrent* tor)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < ctor->lowSize; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentInitFilePriority(tor, ctor->low[i], TR_PRI_LOW);
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < ctor->normalSize; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentInitFilePriority(tor, ctor->normal[i], TR_PRI_NORMAL);
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < ctor->highSize; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentInitFilePriority(tor, ctor->high[i], TR_PRI_HIGH);
|
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorSetFilesWanted(tr_ctor* ctor, tr_file_index_t const* files, tr_file_index_t fileCount, bool wanted)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_file_index_t** myfiles = wanted ? &ctor->want : &ctor->notWant;
|
|
|
|
tr_file_index_t* mycount = wanted ? &ctor->wantSize : &ctor->notWantSize;
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(*myfiles);
|
|
|
|
*myfiles = tr_memdup(files, sizeof(tr_file_index_t) * fileCount);
|
2009-04-02 20:43:42 +00:00
|
|
|
*mycount = fileCount;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorInitTorrentWanted(tr_ctor const* ctor, tr_torrent* tor)
|
2009-04-02 20:43:42 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (ctor->notWantSize != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentInitFileDLs(tor, ctor->notWant, ctor->notWantSize, false);
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (ctor->wantSize != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentInitFileDLs(tor, ctor->want, ctor->wantSize, true);
|
|
|
|
}
|
2009-04-02 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetDeleteSource(tr_ctor* ctor, bool deleteSource)
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
2013-08-24 18:08:38 +00:00
|
|
|
ctor->doDelete = deleteSource;
|
|
|
|
ctor->isSet_delete = true;
|
2008-02-13 02:24:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetDeleteSource(tr_ctor const* ctor, bool* setme)
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2008-02-13 02:24:12 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ctor->isSet_delete)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (setme != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
*setme = ctor->doDelete;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-02-13 02:24:12 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2008-02-13 02:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetSave(tr_ctor* ctor, bool saveInOurTorrentsDir)
|
2007-12-22 17:30:31 +00:00
|
|
|
{
|
2013-08-24 18:08:38 +00:00
|
|
|
ctor->saveInOurTorrentsDir = saveInOurTorrentsDir;
|
2007-12-22 17:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetSave(tr_ctor const* ctor)
|
2007-12-22 17:30:31 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
return ctor != NULL && ctor->saveInOurTorrentsDir;
|
2007-12-22 17:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetPaused(tr_ctor* ctor, tr_ctorMode mode, bool isPaused)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ctor != NULL);
|
|
|
|
TR_ASSERT(mode == TR_FALLBACK || mode == TR_FORCE);
|
2013-08-24 18:08:38 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct optional_args* args = &ctor->optionalArgs[mode];
|
2013-08-24 18:08:38 +00:00
|
|
|
args->isSet_paused = true;
|
|
|
|
args->isPaused = isPaused;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetPeerLimit(tr_ctor* ctor, tr_ctorMode mode, uint16_t peerLimit)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ctor != NULL);
|
|
|
|
TR_ASSERT(mode == TR_FALLBACK || mode == TR_FORCE);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct optional_args* args = &ctor->optionalArgs[mode];
|
2013-08-24 18:08:38 +00:00
|
|
|
args->isSet_connected = true;
|
2008-05-12 16:33:17 +00:00
|
|
|
args->peerLimit = peerLimit;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorSetDownloadDir(tr_ctor* ctor, tr_ctorMode mode, char const* directory)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ctor != NULL);
|
|
|
|
TR_ASSERT(mode == TR_FALLBACK || mode == TR_FORCE);
|
2013-08-24 18:08:38 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct optional_args* args = &ctor->optionalArgs[mode];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(args->downloadDir);
|
2008-10-14 17:57:44 +00:00
|
|
|
args->downloadDir = NULL;
|
2013-08-24 18:08:38 +00:00
|
|
|
args->isSet_downloadDir = false;
|
2008-10-14 03:54:57 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(directory))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2013-08-24 18:08:38 +00:00
|
|
|
args->isSet_downloadDir = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
args->downloadDir = tr_strdup(directory);
|
2008-06-11 23:40:27 +00:00
|
|
|
}
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorSetIncompleteDir(tr_ctor* ctor, char const* directory)
|
2009-10-21 05:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ctor->incompleteDir);
|
|
|
|
ctor->incompleteDir = tr_strdup(directory);
|
2009-10-21 05:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetPeerLimit(tr_ctor const* ctor, tr_ctorMode mode, uint16_t* setmeCount)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2017-04-20 16:02:19 +00:00
|
|
|
struct optional_args const* args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!args->isSet_connected)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (setmeCount != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-05-12 16:33:17 +00:00
|
|
|
*setmeCount = args->peerLimit;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetPaused(tr_ctor const* ctor, tr_ctorMode mode, bool* setmeIsPaused)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2017-04-20 16:02:19 +00:00
|
|
|
struct optional_args const* args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!args->isSet_paused)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (setmeIsPaused != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2013-08-24 18:08:38 +00:00
|
|
|
*setmeIsPaused = args->isPaused;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetDownloadDir(tr_ctor const* ctor, tr_ctorMode mode, char const** setmeDownloadDir)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2017-04-20 16:02:19 +00:00
|
|
|
struct optional_args const* args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!args->isSet_downloadDir)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (setmeDownloadDir != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
*setmeDownloadDir = args->downloadDir;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetIncompleteDir(tr_ctor const* ctor, char const** setmeIncompleteDir)
|
2009-10-21 05:03:10 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2009-10-21 05:03:10 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ctor->incompleteDir == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-21 05:03:10 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-21 05:03:10 +00:00
|
|
|
*setmeIncompleteDir = ctor->incompleteDir;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-21 05:03:10 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2009-10-21 05:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetMetainfo(tr_ctor const* ctor, tr_variant const** setme)
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
bool ret = true;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ctor->isSet_metainfo)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (setme != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-02-13 02:24:12 +00:00
|
|
|
*setme = &ctor->metainfo;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2007-12-21 22:18:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_session* tr_ctorGetSession(tr_ctor const* ctor)
|
2009-04-02 17:30:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return (tr_session*)ctor->session;
|
2009-04-02 17:30:29 +00:00
|
|
|
}
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool isPriority(int i)
|
2010-02-02 07:48:03 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return i == TR_PRI_LOW || i == TR_PRI_NORMAL || i == TR_PRI_HIGH;
|
2010-02-02 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetBandwidthPriority(tr_ctor* ctor, tr_priority_t priority)
|
2010-02-02 07:48:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isPriority(priority))
|
|
|
|
{
|
2010-02-02 07:48:03 +00:00
|
|
|
ctor->bandwidthPriority = priority;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-02-02 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_priority_t tr_ctorGetBandwidthPriority(tr_ctor const* ctor)
|
2010-02-02 07:48:03 +00:00
|
|
|
{
|
|
|
|
return ctor->bandwidthPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_ctor* tr_ctorNew(tr_session const* session)
|
2007-12-21 22:18:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctor* ctor = tr_new0(struct tr_ctor, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
ctor->session = session;
|
2010-02-02 07:48:03 +00:00
|
|
|
ctor->bandwidthPriority = TR_PRI_NORMAL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session != NULL)
|
2010-04-23 14:13:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorSetDeleteSource(ctor, tr_sessionGetDeleteSource(session));
|
|
|
|
tr_ctorSetPaused(ctor, TR_FALLBACK, tr_sessionGetPaused(session));
|
|
|
|
tr_ctorSetPeerLimit(ctor, TR_FALLBACK, session->peerLimitPerTorrent);
|
|
|
|
tr_ctorSetDownloadDir(ctor, TR_FALLBACK, tr_sessionGetDownloadDir(session));
|
2009-04-02 17:30:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_ctorSetSave(ctor, true);
|
2007-12-21 22:18:40 +00:00
|
|
|
return ctor;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorFree(tr_ctor* ctor)
|
|
|
|
{
|
|
|
|
clearMetainfo(ctor);
|
|
|
|
tr_free(ctor->optionalArgs[1].downloadDir);
|
|
|
|
tr_free(ctor->optionalArgs[0].downloadDir);
|
|
|
|
tr_free(ctor->incompleteDir);
|
|
|
|
tr_free(ctor->want);
|
|
|
|
tr_free(ctor->notWant);
|
|
|
|
tr_free(ctor->low);
|
|
|
|
tr_free(ctor->high);
|
|
|
|
tr_free(ctor->normal);
|
|
|
|
tr_free(ctor);
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|