refactor: add an enumeration of the script types (#1934)

* refactor: add an enumeration of the script types

This simplifies the API by having a single set of functions that can be
used for getting/setting all the script types.
This commit is contained in:
Charles Kerr 2021-10-12 22:46:12 -05:00 committed by GitHub
parent 16dcc224ec
commit 98e16a178f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 94 deletions

View File

@ -541,6 +541,8 @@
"rpc-version" | number | the current RPC API version
"rpc-version-minimum" | number | the minimum RPC API version supported
"rpc-version-semver" | number | the current RPC API version in a semver-compatible string
"script-torrent-added-filename" | string | filename of the script to run
"script-torrent-added-enabled" | boolean | whether or not to call the "done" script
"script-torrent-done-filename" | string | filename of the script to run
"script-torrent-done-enabled" | boolean | whether or not to call the "done" script
"seedRatioLimit" | double | the default seed ratio for torrents to use
@ -847,9 +849,11 @@
| | | torrent-get | new arg "editDate"
| | | torrent-get | new request arg "format"
----+-------+------+----------------------+-------------------------------
17 | 5.3.0 | 3.xx | torrent-get | new arg "file-count"
17 | 5.3.0 | 3.xx | session-get | new arg "rpc-version-semver"
| | | session-get | new arg "script-torrent-added-enabled"
| | | session-get | new arg "script-torrent-added-filename"
| | | torrent-get | new arg "file-count"
| | | torrent-get | new arg "primary-mime-type"
| | | session-get | new arg "rpc-version-semver"
5.1. Upcoming Breakage

View File

@ -1349,11 +1349,11 @@ static void on_prefs_changed(TrCore const* core, tr_quark const key, gpointer da
break;
case TR_KEY_script_torrent_done_enabled:
tr_sessionSetTorrentDoneScriptEnabled(tr, gtr_pref_flag_get(key));
tr_sessionSetScriptEnabled(tr, TR_SCRIPT_ON_TORRENT_DONE, gtr_pref_flag_get(key));
break;
case TR_KEY_script_torrent_done_filename:
tr_sessionSetTorrentDoneScript(tr, gtr_pref_string_get(key));
tr_sessionSetScript(tr, TR_SCRIPT_ON_TORRENT_DONE, gtr_pref_string_get(key));
break;
case TR_KEY_start_added_torrents:

View File

@ -2216,22 +2216,22 @@ static char const* sessionSet(
if (tr_variantDictFindStr(args_in, TR_KEY_script_torrent_added_filename, &str, nullptr))
{
tr_sessionSetTorrentAddedScript(session, str);
tr_sessionSetScript(session, TR_SCRIPT_ON_TORRENT_ADDED, str);
}
if (tr_variantDictFindBool(args_in, TR_KEY_script_torrent_added_enabled, &boolVal))
{
tr_sessionSetTorrentAddedScriptEnabled(session, boolVal);
tr_sessionSetScriptEnabled(session, TR_SCRIPT_ON_TORRENT_ADDED, boolVal);
}
if (tr_variantDictFindStr(args_in, TR_KEY_script_torrent_done_filename, &str, nullptr))
{
tr_sessionSetTorrentDoneScript(session, str);
tr_sessionSetScript(session, TR_SCRIPT_ON_TORRENT_DONE, str);
}
if (tr_variantDictFindBool(args_in, TR_KEY_script_torrent_done_enabled, &boolVal))
{
tr_sessionSetTorrentDoneScriptEnabled(session, boolVal);
tr_sessionSetScriptEnabled(session, TR_SCRIPT_ON_TORRENT_DONE, boolVal);
}
if (tr_variantDictFindBool(args_in, TR_KEY_trash_original_torrent_files, &boolVal))
@ -2508,19 +2508,19 @@ static void addSessionField(tr_session* s, tr_variant* d, tr_quark key)
break;
case TR_KEY_script_torrent_added_filename:
tr_variantDictAddStr(d, key, tr_sessionGetTorrentAddedScript(s));
tr_variantDictAddStr(d, key, tr_sessionGetScript(s, TR_SCRIPT_ON_TORRENT_ADDED));
break;
case TR_KEY_script_torrent_added_enabled:
tr_variantDictAddBool(d, key, tr_sessionIsTorrentAddedScriptEnabled(s));
tr_variantDictAddBool(d, key, tr_sessionIsScriptEnabled(s, TR_SCRIPT_ON_TORRENT_ADDED));
break;
case TR_KEY_script_torrent_done_filename:
tr_variantDictAddStr(d, key, tr_sessionGetTorrentDoneScript(s));
tr_variantDictAddStr(d, key, tr_sessionGetScript(s, TR_SCRIPT_ON_TORRENT_DONE));
break;
case TR_KEY_script_torrent_done_enabled:
tr_variantDictAddBool(d, key, tr_sessionIsTorrentDoneScriptEnabled(s));
tr_variantDictAddBool(d, key, tr_sessionIsScriptEnabled(s, TR_SCRIPT_ON_TORRENT_DONE));
break;
case TR_KEY_queue_stalled_enabled:

View File

@ -466,10 +466,10 @@ void tr_sessionGetSettings(tr_session* s, tr_variant* d)
tr_variantDictAddStr(d, TR_KEY_rpc_whitelist, tr_sessionGetRPCWhitelist(s));
tr_variantDictAddBool(d, TR_KEY_rpc_whitelist_enabled, tr_sessionGetRPCWhitelistEnabled(s));
tr_variantDictAddBool(d, TR_KEY_scrape_paused_torrents_enabled, s->scrapePausedTorrents);
tr_variantDictAddBool(d, TR_KEY_script_torrent_added_enabled, tr_sessionIsTorrentAddedScriptEnabled(s));
tr_variantDictAddStr(d, TR_KEY_script_torrent_added_filename, tr_sessionGetTorrentAddedScript(s));
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_enabled, tr_sessionIsTorrentDoneScriptEnabled(s));
tr_variantDictAddStr(d, TR_KEY_script_torrent_done_filename, tr_sessionGetTorrentDoneScript(s));
tr_variantDictAddBool(d, TR_KEY_script_torrent_added_enabled, tr_sessionIsScriptEnabled(s, TR_SCRIPT_ON_TORRENT_ADDED));
tr_variantDictAddStr(d, TR_KEY_script_torrent_added_filename, tr_sessionGetScript(s, TR_SCRIPT_ON_TORRENT_ADDED));
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_enabled, tr_sessionIsScriptEnabled(s, TR_SCRIPT_ON_TORRENT_DONE));
tr_variantDictAddStr(d, TR_KEY_script_torrent_done_filename, tr_sessionGetScript(s, TR_SCRIPT_ON_TORRENT_DONE));
tr_variantDictAddInt(d, TR_KEY_seed_queue_size, tr_sessionGetQueueSize(s, TR_UP));
tr_variantDictAddBool(d, TR_KEY_seed_queue_enabled, tr_sessionGetQueueEnabled(s, TR_UP));
tr_variantDictAddBool(d, TR_KEY_alt_speed_enabled, tr_sessionUsesAltSpeed(s));
@ -1137,22 +1137,22 @@ static void sessionSetImpl(void* vdata)
if (tr_variantDictFindBool(settings, TR_KEY_script_torrent_added_enabled, &boolVal))
{
tr_sessionSetTorrentAddedScriptEnabled(session, boolVal);
tr_sessionSetScriptEnabled(session, TR_SCRIPT_ON_TORRENT_ADDED, boolVal);
}
if (tr_variantDictFindStr(settings, TR_KEY_script_torrent_added_filename, &strVal, NULL))
{
tr_sessionSetTorrentAddedScript(session, strVal);
tr_sessionSetScript(session, TR_SCRIPT_ON_TORRENT_ADDED, strVal);
}
if (tr_variantDictFindBool(settings, TR_KEY_script_torrent_done_enabled, &boolVal))
{
tr_sessionSetTorrentDoneScriptEnabled(session, boolVal);
tr_sessionSetScriptEnabled(session, TR_SCRIPT_ON_TORRENT_DONE, boolVal);
}
if (tr_variantDictFindStr(settings, TR_KEY_script_torrent_done_filename, &strVal, nullptr))
{
tr_sessionSetTorrentDoneScript(session, strVal);
tr_sessionSetScript(session, TR_SCRIPT_ON_TORRENT_DONE, strVal);
}
if (tr_variantDictFindBool(settings, TR_KEY_scrape_paused_torrents_enabled, &boolVal))
@ -2125,8 +2125,6 @@ void tr_sessionClose(tr_session* session)
}
tr_device_info_free(session->downloadDir);
tr_free(session->torrentAddedScript);
tr_free(session->torrentDoneScript);
tr_free(session->configDir);
tr_free(session->resumeDir);
tr_free(session->torrentDir);
@ -2841,68 +2839,36 @@ char const* tr_sessionGetRPCBindAddress(tr_session const* session)
*****
****/
bool tr_sessionIsTorrentAddedScriptEnabled(tr_session const* session)
void tr_sessionSetScriptEnabled(tr_session* session, TrScript type, bool enabled)
{
TR_ASSERT(tr_isSession(session));
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
return session->isTorrentAddedScriptEnabled;
session->scripts_enabled[type] = enabled;
}
void tr_sessionSetTorrentAddedScriptEnabled(tr_session* session, bool isEnabled)
bool tr_sessionIsScriptEnabled(tr_session const* session, TrScript type)
{
TR_ASSERT(tr_isSession(session));
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
session->isTorrentAddedScriptEnabled = isEnabled;
return session->scripts_enabled[type];
}
char const* tr_sessionGetTorrentAddedScript(tr_session const* session)
void tr_sessionSetScript(tr_session* session, TrScript type, char const* script)
{
TR_ASSERT(tr_isSession(session));
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
return session->torrentAddedScript;
session->scripts[type].assign(script ? script : "");
}
void tr_sessionSetTorrentAddedScript(tr_session* session, char const* scriptFilename)
char const* tr_sessionGetScript(tr_session const* session, TrScript type)
{
TR_ASSERT(tr_isSession(session));
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
if (session->torrentAddedScript != scriptFilename)
{
tr_free(session->torrentAddedScript);
session->torrentAddedScript = tr_strdup(scriptFilename);
}
}
bool tr_sessionIsTorrentDoneScriptEnabled(tr_session const* session)
{
TR_ASSERT(tr_isSession(session));
return session->isTorrentDoneScriptEnabled;
}
void tr_sessionSetTorrentDoneScriptEnabled(tr_session* session, bool isEnabled)
{
TR_ASSERT(tr_isSession(session));
session->isTorrentDoneScriptEnabled = isEnabled;
}
char const* tr_sessionGetTorrentDoneScript(tr_session const* session)
{
TR_ASSERT(tr_isSession(session));
return session->torrentDoneScript;
}
void tr_sessionSetTorrentDoneScript(tr_session* session, char const* scriptFilename)
{
TR_ASSERT(tr_isSession(session));
if (session->torrentDoneScript != scriptFilename)
{
tr_free(session->torrentDoneScript);
session->torrentDoneScript = tr_strdup(scriptFilename);
}
return session->scripts[type].c_str();
}
/***

View File

@ -14,9 +14,11 @@
#define TR_NAME "Transmission"
#include <array>
#include <cstring> // memcmp()
#include <list>
#include <map>
#include <string>
#include <unordered_set>
#include <vector>
@ -129,8 +131,6 @@ struct tr_session
bool isLPDEnabled;
bool isBlocklistEnabled;
bool isPrefetchEnabled;
bool isTorrentAddedScriptEnabled;
bool isTorrentDoneScriptEnabled;
bool isClosing;
bool isClosed;
bool isIncompleteFileNamingEnabled;
@ -140,6 +140,7 @@ struct tr_session
bool pauseAddedTorrent;
bool deleteSourceTorrent;
bool scrapePausedTorrents;
std::array<bool, TR_SCRIPT_N_TYPES> scripts_enabled;
uint8_t peer_id_ttl_hours;
@ -206,8 +207,7 @@ struct tr_session
std::map<uint8_t const*, tr_torrent*, CompareHash> torrentsByHash;
std::map<char const*, tr_torrent*, CompareHashString> torrentsByHashString;
char* torrentAddedScript;
char* torrentDoneScript;
std::array<std::string, TR_SCRIPT_N_TYPES> scripts;
char* configDir;
char* resumeDir;

View File

@ -845,6 +845,16 @@ static bool setLocalErrorIfFilesDisappeared(tr_torrent* tor)
static void torrentCallScript(tr_torrent const* tor, char const* script);
static void callScriptIfEnabled(tr_torrent const* tor, TrScript type)
{
auto* session = tor->session;
if (tr_sessionIsScriptEnabled(session, type))
{
torrentCallScript(tor, tr_sessionGetScript(session, type));
}
}
static void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
{
tr_session* session = tr_ctorGetSession(ctor);
@ -965,9 +975,9 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
if (isNewTorrent)
{
if (tr_torrentHasMetadata(tor) && tr_sessionIsTorrentAddedScriptEnabled(session))
if (tr_torrentHasMetadata(tor))
{
torrentCallScript(tor, tr_sessionGetTorrentAddedScript(session));
callScriptIfEnabled(tor, TR_SCRIPT_ON_TORRENT_ADDED);
}
if (!tr_torrentHasMetadata(tor) && !doStart)
@ -1928,10 +1938,7 @@ static void stopTorrent(void* vtor)
refreshCurrentDir(tor);
tr_torrentVerify(tor, nullptr, nullptr);
if (tr_sessionIsTorrentAddedScriptEnabled(tor->session))
{
torrentCallScript(tor, tr_sessionGetTorrentAddedScript(tor->session));
}
callScriptIfEnabled(tor, TR_SCRIPT_ON_TORRENT_ADDED);
}
}
@ -2228,11 +2235,10 @@ void tr_torrentRecheckCompleteness(tr_torrent* tor)
tr_torrentSetDirty(tor);
if (tr_torrentIsSeed(tor) && tr_sessionIsTorrentDoneScriptEnabled(tor->session))
if (tr_torrentIsSeed(tor))
{
tr_torrentSave(tor);
torrentCallScript(tor, tr_sessionGetTorrentDoneScript(tor->session));
callScriptIfEnabled(tor, TR_SCRIPT_ON_TORRENT_DONE);
}
}

View File

@ -699,21 +699,21 @@ tr_torrent** tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor, int* set
***
**/
bool tr_sessionIsTorrentAddedScriptEnabled(tr_session const*);
enum TrScript
{
TR_SCRIPT_ON_TORRENT_ADDED,
TR_SCRIPT_ON_TORRENT_DONE,
void tr_sessionSetTorrentAddedScriptEnabled(tr_session*, bool isEnabled);
TR_SCRIPT_N_TYPES
};
char const* tr_sessionGetTorrentAddedScript(tr_session const*);
void tr_sessionSetScript(tr_session*, TrScript, char const* script_filename);
void tr_sessionSetTorrentAddedScript(tr_session*, char const* scriptFilename);
char const* tr_sessionGetScript(tr_session const*, TrScript);
bool tr_sessionIsTorrentDoneScriptEnabled(tr_session const*);
void tr_sessionSetScriptEnabled(tr_session*, TrScript, bool enabled);
void tr_sessionSetTorrentDoneScriptEnabled(tr_session*, bool isEnabled);
char const* tr_sessionGetTorrentDoneScript(tr_session const*);
void tr_sessionSetTorrentDoneScript(tr_session*, char const* scriptFilename);
bool tr_sessionIsScriptEnabled(tr_session const*, TrScript);
/** @} */

View File

@ -953,10 +953,10 @@
assert(filePath.length > 0);
[fDefaults setObject:filePath forKey:@"DoneScriptPath"];
tr_sessionSetTorrentDoneScript(fHandle, filePath.fileSystemRepresentation);
tr_sessionSetScript(fHandle, TR_SCRIPT_ON_TORRENT_DONE, filePath.fileSystemRepresentation);
[fDefaults setBool:YES forKey:@"DoneScriptEnabled"];
tr_sessionSetTorrentDoneScriptEnabled(fHandle, YES);
tr_sessionSetScriptEnabled(fHandle, TR_SCRIPT_ON_TORRENT_DONE, YES);
}
[fDoneScriptPopUp selectItemAtIndex:0];
}];
@ -1001,7 +1001,7 @@
[fDefaults setBool:NO forKey:@"DoneScriptEnabled"];
[self doneScriptSheetShow:sender];
}
tr_sessionSetTorrentDoneScriptEnabled(fHandle, [fDefaults boolForKey:@"DoneScriptEnabled"]);
tr_sessionSetScriptEnabled(fHandle, TR_SCRIPT_ON_TORRENT_DONE, [fDefaults boolForKey:@"DoneScriptEnabled"]);
}
- (void)setAutoImport:(id)sender
@ -1443,10 +1443,10 @@
[fDefaults setInteger:stalledMinutes forKey:@"StalledMinutes"];
//done script
BOOL const doneScriptEnabled = tr_sessionIsTorrentDoneScriptEnabled(fHandle);
BOOL const doneScriptEnabled = tr_sessionIsScriptEnabled(fHandle, TR_SCRIPT_ON_TORRENT_DONE);
[fDefaults setBool:doneScriptEnabled forKey:@"DoneScriptEnabled"];
NSString* doneScriptPath = @(tr_sessionGetTorrentDoneScript(fHandle));
NSString* doneScriptPath = @(tr_sessionGetScript(fHandle, TR_SCRIPT_ON_TORRENT_DONE));
[fDefaults setObject:doneScriptPath forKey:@"DoneScriptPath"];
//update gui if loaded