refactor: change tr_torrentNew() args (#2337)

This commit is contained in:
Charles Kerr 2021-12-24 16:05:17 -06:00 committed by GitHub
parent f3228e9670
commit b058daff4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 30 additions and 62 deletions

View File

@ -236,7 +236,6 @@ int tr_main(int argc, char* argv[])
{ {
tr_session* h; tr_session* h;
tr_ctor* ctor; tr_ctor* ctor;
tr_torrent* tor = nullptr;
tr_variant settings; tr_variant settings;
char const* configDir; char const* configDir;
@ -327,9 +326,8 @@ int tr_main(int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
tor = tr_torrentNew(ctor, nullptr, nullptr); tr_torrent* tor = tr_torrentNew(ctor, nullptr);
tr_ctorFree(ctor); tr_ctorFree(ctor);
if (tor == nullptr) if (tor == nullptr)
{ {
fprintf(stderr, "Failed opening torrent file `%s'\n", torrentPath); fprintf(stderr, "Failed opening torrent file `%s'\n", torrentPath);

View File

@ -233,11 +233,9 @@ static tr_watchdir_status onFileAdded(tr_watchdir_t dir, char const* name, void*
if (err == 0) if (err == 0)
{ {
tr_torrentNew(ctor, &err, nullptr); if (tr_torrentNew(ctor, nullptr) == nullptr)
if (err == TR_PARSE_ERR)
{ {
tr_logAddError("Error parsing .torrent file \"%s\"", name); tr_logAddError("Unable to add .torrent file \"%s\"", name);
} }
else else
{ {

View File

@ -188,10 +188,7 @@ void OptionsDialog::Impl::sourceChanged(Gtk::FileChooserButton* b)
/* maybe instantiate a torrent */ /* maybe instantiate a torrent */
if (!filename.empty() || tor_ == nullptr) if (!filename.empty() || tor_ == nullptr)
{ {
int err = 0;
bool new_file = false; bool new_file = false;
int duplicate_id = 0;
tr_torrent* torrent;
if (!filename.empty() && (filename_.empty() || !tr_sys_path_is_same(filename.c_str(), filename_.c_str(), nullptr))) if (!filename.empty() && (filename_.empty() || !tr_sys_path_is_same(filename.c_str(), filename_.c_str(), nullptr)))
{ {
@ -204,15 +201,15 @@ void OptionsDialog::Impl::sourceChanged(Gtk::FileChooserButton* b)
tr_ctorSetPaused(ctor_.get(), TR_FORCE, true); tr_ctorSetPaused(ctor_.get(), TR_FORCE, true);
tr_ctorSetDeleteSource(ctor_.get(), false); tr_ctorSetDeleteSource(ctor_.get(), false);
if (torrent = tr_torrentNew(ctor_.get(), &err, &duplicate_id); torrent != nullptr) tr_torrent* duplicate_of = nullptr;
if (tr_torrent* const torrent = tr_torrentNew(ctor_.get(), &duplicate_of); torrent != nullptr)
{ {
removeOldTorrent(); removeOldTorrent();
tor_ = torrent; tor_ = torrent;
} }
else if (new_file) else if (new_file)
{ {
tr_torrent* tor = duplicate_id != 0 ? core_->find_torrent(duplicate_id) : nullptr; gtr_add_torrent_error_dialog(*b, duplicate_of, filename_);
gtr_add_torrent_error_dialog(*b, err, tor, filename_);
} }
updateTorrent(); updateTorrent();

View File

@ -1016,7 +1016,7 @@ tr_torrent* Session::Impl::create_new_torrent(tr_ctor* ctor)
* doesn't have any concept of the glib trash API */ * doesn't have any concept of the glib trash API */
tr_ctorGetDeleteSource(ctor, &do_trash); tr_ctorGetDeleteSource(ctor, &do_trash);
tr_ctorSetDeleteSource(ctor, false); tr_ctorSetDeleteSource(ctor, false);
tr_torrent* const tor = tr_torrentNew(ctor, nullptr, nullptr); tr_torrent* const tor = tr_torrentNew(ctor, nullptr);
if (tor != nullptr && do_trash) if (tor != nullptr && do_trash)
{ {

View File

@ -219,16 +219,12 @@ Gtk::Window* getWindow(Gtk::Widget* w)
} // namespace } // namespace
void gtr_add_torrent_error_dialog(Gtk::Widget& child, int err, tr_torrent* duplicate_torrent, std::string const& filename) void gtr_add_torrent_error_dialog(Gtk::Widget& child, tr_torrent* duplicate_torrent, std::string const& filename)
{ {
Glib::ustring secondary; Glib::ustring secondary;
auto* win = getWindow(&child); auto* win = getWindow(&child);
if (err == TR_PARSE_ERR) if (duplicate_torrent != nullptr)
{
secondary = gtr_sprintf(_("The torrent file \"%s\" contains invalid data."), filename);
}
else if (err == TR_PARSE_DUPLICATE)
{ {
secondary = gtr_sprintf( secondary = gtr_sprintf(
_("The torrent file \"%s\" is already in use by \"%s.\""), _("The torrent file \"%s\" is already in use by \"%s.\""),
@ -237,7 +233,7 @@ void gtr_add_torrent_error_dialog(Gtk::Widget& child, int err, tr_torrent* dupli
} }
else else
{ {
secondary = gtr_sprintf(_("The torrent file \"%s\" encountered an unknown error."), filename); secondary = gtr_sprintf(_("Unable to add torrent file \"%s\"."), filename);
} }
auto w = std::make_shared<Gtk::MessageDialog>( auto w = std::make_shared<Gtk::MessageDialog>(

View File

@ -107,11 +107,7 @@ void gtr_combo_box_set_active_enum(Gtk::ComboBox&, int value);
void gtr_unrecognized_url_dialog(Gtk::Widget& parent, Glib::ustring const& url); void gtr_unrecognized_url_dialog(Gtk::Widget& parent, Glib::ustring const& url);
void gtr_add_torrent_error_dialog( void gtr_add_torrent_error_dialog(Gtk::Widget& window_or_child, tr_torrent* duplicate_torrent, std::string const& filename);
Gtk::Widget& window_or_child,
int err,
tr_torrent* duplicate_torrent,
std::string const& filename);
/* pop up the context menu if a user right-clicks. /* pop up the context menu if a user right-clicks.
if the row they right-click on isn't selected, select it. */ if the row they right-click on isn't selected, select it. */

View File

@ -1506,21 +1506,20 @@ static char const* blocklistUpdate(
static void addTorrentImpl(struct tr_rpc_idle_data* data, tr_ctor* ctor) static void addTorrentImpl(struct tr_rpc_idle_data* data, tr_ctor* ctor)
{ {
auto err = int{}; tr_torrent* duplicate_of = nullptr;
auto duplicate_id = int{}; tr_torrent* tor = tr_torrentNew(ctor, &duplicate_of);
tr_torrent* tor = tr_torrentNew(ctor, &err, &duplicate_id);
tr_ctorFree(ctor); tr_ctorFree(ctor);
auto key = tr_quark{}; auto key = tr_quark{};
char const* result = "invalid or corrupt torrent file"; char const* result = "invalid or corrupt torrent file";
if (err == 0) if (tor != nullptr)
{ {
key = TR_KEY_torrent_added; key = TR_KEY_torrent_added;
result = nullptr; result = nullptr;
} }
else if (err == TR_PARSE_DUPLICATE) else if (duplicate_of != nullptr)
{ {
tor = tr_torrentFindFromId(data->session, duplicate_id); tor = duplicate_of;
key = TR_KEY_torrent_duplicate; key = TR_KEY_torrent_duplicate;
result = "duplicate torrent"; result = "duplicate torrent";
} }

View File

@ -2044,8 +2044,7 @@ static void sessionLoadTorrents(void* vdata)
tr_buildBuf(path, dirname_sv, "/", name); tr_buildBuf(path, dirname_sv, "/", name);
tr_ctorSetMetainfoFromFile(data->ctor, path.c_str()); tr_ctorSetMetainfoFromFile(data->ctor, path.c_str());
tr_torrent* const tor = tr_torrentNew(data->ctor, nullptr, nullptr); if (tr_torrent* const tor = tr_torrentNew(data->ctor, nullptr); tor != nullptr)
if (tor != nullptr)
{ {
torrents.push_back(tor); torrents.push_back(tor);
} }

View File

@ -788,7 +788,7 @@ tr_parse_result tr_torrentParse(tr_ctor const* ctor, tr_info* setmeInfo)
return TR_PARSE_OK; return TR_PARSE_OK;
} }
tr_torrent* tr_torrentNew(tr_ctor const* ctor, int* setme_error, int* setme_duplicate_id) tr_torrent* tr_torrentNew(tr_ctor const* ctor, tr_torrent** setme_duplicate_of)
{ {
TR_ASSERT(ctor != nullptr); TR_ASSERT(ctor != nullptr);
auto* const session = tr_ctorGetSession(ctor); auto* const session = tr_ctorGetSession(ctor);
@ -799,25 +799,15 @@ tr_torrent* tr_torrentNew(tr_ctor const* ctor, int* setme_error, int* setme_dupl
auto parsed = tr_metainfoParse(session, metainfo, nullptr); auto parsed = tr_metainfoParse(session, metainfo, nullptr);
if (!parsed) if (!parsed)
{ {
if (setme_error != nullptr)
{
*setme_error = TR_PARSE_ERR;
}
return nullptr; return nullptr;
} }
tr_torrent const* const dupe = session->getTorrent(parsed->info.hash); auto* const duplicate_of = session->getTorrent(parsed->info.hash);
if (dupe != nullptr) if (duplicate_of != nullptr)
{ {
if (setme_duplicate_id != nullptr) if (setme_duplicate_of != nullptr)
{ {
*setme_duplicate_id = tr_torrentId(dupe); *setme_duplicate_of = duplicate_of;
}
if (setme_error != nullptr)
{
*setme_error = TR_PARSE_DUPLICATE;
} }
return nullptr; return nullptr;

View File

@ -953,13 +953,10 @@ void tr_metainfoFree(tr_info* inf);
* Returns a pointer to the torrent on success, or nullptr on failure. * Returns a pointer to the torrent on success, or nullptr on failure.
* *
* @param ctor the builder struct * @param ctor the builder struct
* @param setme_error TR_PARSE_ERR if the parsing failed. * @param setme_duplicate_of If the torrent couldn't be created because it's a duplicate,
* TR_PARSE_OK if parsing succeeded and it's not a duplicate. * this is set to point to the original torrent.
* TR_PARSE_DUPLICATE if parsing succeeded but it's a duplicate.
* @param setme_duplicate_id when setmeError is TR_PARSE_DUPLICATE,
* this field is set to the duplicate torrent's id.
*/ */
tr_torrent* tr_torrentNew(tr_ctor const* ctor, int* setme_error, int* setme_duplicate_id); tr_torrent* tr_torrentNew(tr_ctor const* ctor, tr_torrent** setme_duplicate_of);
/** @} */ /** @} */

View File

@ -1862,7 +1862,7 @@ bool trashDataFile(char const* filename, tr_error** error)
if (result == TR_PARSE_OK) if (result == TR_PARSE_OK)
{ {
fHandle = tr_torrentNew(ctor, NULL, NULL); fHandle = tr_torrentNew(ctor, NULL);
} }
tr_ctorFree(ctor); tr_ctorFree(ctor);

View File

@ -77,9 +77,8 @@ protected:
tr_ctorSetPaused(ctor, TR_FORCE, true); tr_ctorSetPaused(ctor, TR_FORCE, true);
// create the torrent // create the torrent
auto err = int{}; auto* const tor = tr_torrentNew(ctor, nullptr);
auto* tor = tr_torrentNew(ctor, &err, nullptr); EXPECT_NE(nullptr, tor);
EXPECT_EQ(0, err);
// cleanup // cleanup
tr_free(metainfo); tr_free(metainfo);

View File

@ -384,9 +384,8 @@ protected:
tr_free(metainfo); tr_free(metainfo);
// create the torrent // create the torrent
auto err = int{}; auto* const tor = tr_torrentNew(ctor, nullptr);
auto* tor = tr_torrentNew(ctor, &err, nullptr); EXPECT_NE(nullptr, tor);
EXPECT_EQ(0, err);
// cleanup // cleanup
tr_ctorFree(ctor); tr_ctorFree(ctor);