Skip to content

Commit

Permalink
Merge pull request #1124 from SadE54/command_line_save_path
Browse files Browse the repository at this point in the history
Destination path can be chosen for command line added files (#1098)
  • Loading branch information
vktr committed Jan 21, 2021
2 parents 592250d + 3c3175b commit fc9cd65
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 34 deletions.
24 changes: 18 additions & 6 deletions src/picotorrent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ Application::~Application()
bool Application::OnCmdLineParsed(wxCmdLineParser& parser)
{
long waitForPid = -1;
wxString save_path = "";

if (parser.Found("wait-for-pid", &waitForPid))
{
m_options.pid = waitForPid;
}

m_options.silent = parser.Found("silent");

if (parser.Found("save-path", &save_path))
{
m_options.save_path = Utils::toStdString(save_path.ToStdWstring());
}

for (size_t i = 0; i < parser.GetParamCount(); i++)
{
std::string arg = Utils::toStdString(parser.GetParam(i).ToStdWstring());
Expand Down Expand Up @@ -81,6 +90,7 @@ bool Application::OnInit()

auto db = std::make_shared<pt::Core::Database>(env);


if (!db->Migrate())
{
wxMessageBox(
Expand Down Expand Up @@ -122,7 +132,7 @@ bool Application::OnInit()
m_persistence = std::make_unique<PersistenceManager>(db);
wxPersistenceManager::Set(*m_persistence);

auto mainFrame = new UI::MainFrame(env, db, cfg);
auto mainFrame = new UI::MainFrame(env, db, cfg, m_options);

std::for_each(
m_plugins.begin(),
Expand Down Expand Up @@ -161,9 +171,7 @@ bool Application::OnInit()
break;
}

mainFrame->HandleParams(
m_options.files,
m_options.magnets);
mainFrame->HandleParams(m_options);

return true;
}
Expand All @@ -172,8 +180,10 @@ void Application::OnInitCmdLine(wxCmdLineParser& parser)
{
static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_OPTION, NULL, "wait-for-pid", NULL, wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_PARAM, NULL, NULL, "params", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_OPTION, NULL, "wait-for-pid", NULL, wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, NULL, "silent", NULL, wxCMD_LINE_VAL_NONE , wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_OPTION, NULL, "save-path", NULL, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_PARAM, NULL, NULL, "params",wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_NONE }
};

Expand All @@ -186,6 +196,8 @@ void Application::ActivateOtherInstance()
json j;
j["files"] = m_options.files;
j["magnet_links"] = m_options.magnets;
j["silent"] = m_options.silent;
j["save_path"] = m_options.save_path;

wxClient client;
auto conn = client.MakeConnection(
Expand Down
12 changes: 3 additions & 9 deletions src/picotorrent/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <memory>
#include <vector>

#include "applicationoptions.hpp"

class wxSingleInstanceChecker;

namespace pt
Expand All @@ -28,18 +30,10 @@ namespace API
virtual void OnInitCmdLine(wxCmdLineParser&) wxOVERRIDE;

private:
struct Options
{
Options() : pid(-1) {}
long pid;
std::vector<std::string> files;
std::vector<std::string> magnets;
};

void ActivateOtherInstance();
void WaitForPreviousInstance(long pid);

Options m_options;
pt::CommandLineOptions m_options;
std::vector<API::IPlugin*> m_plugins;
std::unique_ptr<PersistenceManager> m_persistence;
std::unique_ptr<wxSingleInstanceChecker> m_singleInstance;
Expand Down
17 changes: 17 additions & 0 deletions src/picotorrent/applicationoptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <vector>

namespace pt
{
struct CommandLineOptions
{
CommandLineOptions() : pid(-1), silent(false) {}
long pid;
bool silent;
std::vector<std::string> files;
std::vector<std::string> magnets;
std::string save_path;
};
}
10 changes: 9 additions & 1 deletion src/picotorrent/ipc/applicationoptionsconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <nlohmann/json.hpp>
#include <wx/taskbarbutton.h>

#include "../applicationoptions.hpp"
#include "../ui/mainframe.hpp"

using json = nlohmann::json;
using pt::IPC::ApplicationOptionsConnection;



ApplicationOptionsConnection::ApplicationOptionsConnection(pt::UI::MainFrame* frame)
: m_frame(frame)
{
Expand All @@ -21,10 +24,15 @@ bool ApplicationOptionsConnection::OnExecute(const wxString&, const void *data,
m_frame->GetEventHandler()->CallAfter([this, textData]()
{
json j;
pt::CommandLineOptions options;

try
{
j = json::parse(textData);
options.files = j["files"].get<std::vector<std::string>>();
options.magnets = j["magnet_links"].get<std::vector<std::string>>();
options.save_path = j["save_path"].get<std::string>();
options.silent = j["silent"].get<bool>();
}
catch (std::exception const& e)
{
Expand All @@ -41,7 +49,7 @@ bool ApplicationOptionsConnection::OnExecute(const wxString&, const void *data,

m_frame->Raise();
m_frame->Show();
m_frame->HandleParams(j["files"], j["magnet_links"]);
m_frame->HandleParams(options);
});

return true;
Expand Down
1 change: 1 addition & 0 deletions src/picotorrent/ui/dialogs/createtorrentdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "createtorrentdialog.hpp"

#include <filesystem>
#include <fstream>

#include <boost/log/trivial.hpp>
#include <fmt/format.h>
Expand Down
44 changes: 29 additions & 15 deletions src/picotorrent/ui/mainframe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "mainframe.hpp"

#include <filesystem>
#include <fstream>
#include <regex>

#include <boost/log/trivial.hpp>
Expand All @@ -13,6 +14,7 @@
#include <wx/splitter.h>
#include <wx/taskbarbutton.h>

#include "../applicationoptions.hpp"
#include "../bittorrent/addparams.hpp"
#include "../bittorrent/session.hpp"
#include "../bittorrent/sessionstatistics.hpp"
Expand Down Expand Up @@ -46,11 +48,12 @@ using pt::UI::MainFrame;

const char* WindowTitle = "PicoTorrent";

MainFrame::MainFrame(std::shared_ptr<Core::Environment> env, std::shared_ptr<Core::Database> db, std::shared_ptr<Core::Configuration> cfg)
MainFrame::MainFrame(std::shared_ptr<Core::Environment> env, std::shared_ptr<Core::Database> db, std::shared_ptr<Core::Configuration> cfg, pt::CommandLineOptions const& options)
: wxFrame(nullptr, wxID_ANY, WindowTitle, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, "MainFrame"),
m_env(env),
m_db(db),
m_cfg(cfg),
m_options(options),
m_session(new BitTorrent::Session(this, db, cfg, env)),
m_splitter(new wxSplitterWindow(this, ptID_MAIN_SPLITTER)),
m_statusBar(new StatusBar(this)),
Expand Down Expand Up @@ -398,7 +401,7 @@ MainFrame::~MainFrame()
delete m_taskBarIcon;
}

void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params, bool use_commandline_options)
{
bool didRemove = false;

Expand All @@ -421,10 +424,13 @@ void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)

if (didRemove)
{
auto err = i18n("some_torrents_already_in_session");
if (params.empty()) err = i18n("all_torrents_already_in_session");
if (!m_options.silent || !use_commandline_options)
{
auto err = i18n("some_torrents_already_in_session");
if (params.empty()) err = i18n("all_torrents_already_in_session");

wxMessageBox(err, "PicoTorrent", wxOK, this);
wxMessageBox(err, "PicoTorrent", wxOK, this);
}
}

if (params.empty())
Expand All @@ -444,7 +450,14 @@ void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
auto our = new BitTorrent::AddParams();

p.flags |= lt::torrent_flags::duplicate_is_error;
p.save_path = m_cfg->Get<std::string>("default_save_path").value();
if (!m_options.save_path.empty() && use_commandline_options)
{
p.save_path = m_options.save_path;
}
else
{
p.save_path = m_cfg->Get<std::string>("default_save_path").value();
}
p.userdata = lt::client_data_t(our);

// If we have a param with an info hash and no torrent info,
Expand Down Expand Up @@ -494,7 +507,7 @@ void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
}
}

if (m_cfg->Get<bool>("skip_add_torrent_dialog").value())
if (m_cfg->Get<bool>("skip_add_torrent_dialog").value() || (m_options.silent && use_commandline_options))
{
for (lt::add_torrent_params& p : params)
{
Expand Down Expand Up @@ -522,24 +535,25 @@ void MainFrame::AddTorrents(std::vector<lt::add_torrent_params>& params)
m_session->AddMetadataSearch(hashes);
}

void MainFrame::HandleParams(std::vector<std::string> const& files, std::vector<std::string> const& magnets)
void MainFrame::HandleParams(pt::CommandLineOptions const& options)
{
std::vector<lt::add_torrent_params> params;

if (!files.empty())
m_options = options;
if (!m_options.files.empty())
{
std::vector<std::wstring> converted;
for (auto const& file : files)
for (auto const& file : m_options.files)
{
converted.push_back(Utils::toStdWString(file));
}

ParseTorrentFiles(params, converted);
}

if (!magnets.empty())
if (!m_options.magnets.empty())
{
for (std::string const& magnet : magnets)
for (std::string const& magnet : m_options.magnets)
{
lt::error_code ec;
lt::add_torrent_params tp = lt::parse_magnet_uri(magnet, ec);
Expand All @@ -554,7 +568,7 @@ void MainFrame::HandleParams(std::vector<std::string> const& files, std::vector<
}
}

AddTorrents(params);
AddTorrents(params, true);
}

void MainFrame::CheckDiskSpace(std::vector<pt::BitTorrent::TorrentHandle*> const& torrents)
Expand Down Expand Up @@ -733,7 +747,7 @@ void MainFrame::OnFileAddMagnetLink(wxCommandEvent&)
if (dlg.ShowModal() == wxID_OK)
{
auto params = dlg.GetParams();
this->AddTorrents(params);
this->AddTorrents(params, false);
}
}

Expand All @@ -760,7 +774,7 @@ void MainFrame::OnFileAddTorrent(wxCommandEvent&)

std::vector<lt::add_torrent_params> params;
this->ParseTorrentFiles(params, files);
this->AddTorrents(params);
this->AddTorrents(params, false);
}

void MainFrame::OnFileCreateTorrent(wxCommandEvent&)
Expand Down
11 changes: 8 additions & 3 deletions src/picotorrent/ui/mainframe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <unordered_set>
#include <vector>

#include "../applicationoptions.hpp"

class wxSplitterWindow;
class wxTaskBarIconEvent;

Expand Down Expand Up @@ -54,15 +56,17 @@ namespace Models
MainFrame(
std::shared_ptr<Core::Environment> env,
std::shared_ptr<Core::Database> db,
std::shared_ptr<Core::Configuration> cfg);
std::shared_ptr<Core::Configuration> cfg,
pt::CommandLineOptions const& options);

virtual ~MainFrame();

void HandleParams(std::vector<std::string> const& files, std::vector<std::string> const& magnets);
void HandleParams(pt::CommandLineOptions const& options);

private:
wxMenuBar* CreateMainMenu();

void AddTorrents(std::vector<libtorrent::add_torrent_params>& params);
void AddTorrents(std::vector<libtorrent::add_torrent_params>& params, bool use_commandline_options);
void CheckDiskSpace(std::vector<BitTorrent::TorrentHandle*> const& updatedTorrents);
void CreateFilterMenuItems();
void CreateLabelMenuItems();
Expand Down Expand Up @@ -93,6 +97,7 @@ namespace Models
std::shared_ptr<Core::Database> m_db;
std::shared_ptr<Core::Configuration> m_cfg;
std::unique_ptr<IPC::Server> m_ipc;
pt::CommandLineOptions m_options;

wxMenu* m_viewMenu;
wxMenu* m_filtersMenu;
Expand Down

0 comments on commit fc9cd65

Please sign in to comment.