Skip to content

Commit

Permalink
Fix multi-select in Add torrents, add start torrent option (#552)
Browse files Browse the repository at this point in the history
Closes #550
  • Loading branch information
vktr committed Mar 17, 2018
1 parent e822414 commit ce84389
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 32 deletions.
4 changes: 3 additions & 1 deletion lang/1033.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@
"restart_required": "Restart required",
"file": "File",
"comment": "Comment",
"skip_add_torrent_dialog": "Skip 'Add torrent' dialog"
"skip_add_torrent_dialog": "Skip 'Add torrent' dialog",
"start_torrent": "Start torrent",
"auto_managed": "Auto managed"
}
}
77 changes: 53 additions & 24 deletions src/picotorrent/addtorrentdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using pt::AddTorrentDialog;

wxBEGIN_EVENT_TABLE(AddTorrentDialog, wxDialog)
EVT_CHECKBOX(ptID_SEQUENTIAL_DOWNLOAD, OnSequentialDownloadChanged)
EVT_CHECKBOX(ptID_START_TORRENT, OnStartTorrentChanged)
EVT_CHOICE(ptID_TORRENT_LIST, OnTorrentChanged)
EVT_DATAVIEW_ITEM_CONTEXT_MENU(ptID_FILE_LIST, OnFileContextMenu)
EVT_DIRPICKER_CHANGED(ptID_SAVE_PATH, OnSavePathChanged)
Expand Down Expand Up @@ -67,12 +68,14 @@ AddTorrentDialog::AddTorrentDialog(wxWindow* parent,
torrentSizer->Add(torrentGrid, 1, wxEXPAND | wxALL, 5);

// Storage
wxStaticBoxSizer* storageSizer = new wxStaticBoxSizer(wxVERTICAL, pnl, i18n(translator, "storage"));
wxStaticBoxSizer* storageSizer = new wxStaticBoxSizer(wxVERTICAL, pnl, i18n(translator, "preferences"));
wxFlexGridSizer* storageGrid = new wxFlexGridSizer(2, 10, 25);

m_savePath = new wxDirPickerCtrl(storageSizer->GetStaticBox(), ptID_SAVE_PATH, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE | wxDIRP_SMALL);
m_filesView = new wxDataViewCtrl(storageSizer->GetStaticBox(), ptID_FILE_LIST);
m_filesView = new wxDataViewCtrl(storageSizer->GetStaticBox(), ptID_FILE_LIST, wxDefaultPosition, wxDefaultSize, wxDV_MULTIPLE);
m_sequentialMode = new wxCheckBox(storageSizer->GetStaticBox(), ptID_SEQUENTIAL_DOWNLOAD, i18n(m_trans, "sequential_download"));
m_startTorrent = new wxCheckBox(storageSizer->GetStaticBox(), ptID_START_TORRENT, i18n(m_trans, "start_torrent"));
m_startTorrent->SetValue(true);

auto nameCol = m_filesView->AppendIconTextColumn(
i18n(m_trans, "name"),
Expand Down Expand Up @@ -101,11 +104,15 @@ AddTorrentDialog::AddTorrentDialog(wxWindow* parent,
m_filesView->AssociateModel(m_filesViewModel);
m_filesViewModel->DecRef();

wxBoxSizer* checkSizer = new wxBoxSizer(wxHORIZONTAL);
checkSizer->Add(m_sequentialMode, 1, wxEXPAND);
checkSizer->Add(m_startTorrent, 1, wxEXPAND);

storageGrid->AddGrowableCol(1, 1);
storageGrid->Add(new wxStaticText(storageSizer->GetStaticBox(), wxID_ANY, m_trans->Translate("save_path")), 0, wxALIGN_CENTER_VERTICAL);
storageGrid->Add(m_savePath, 1, wxEXPAND);
storageGrid->AddSpacer(0);
storageGrid->Add(m_sequentialMode, 1, wxEXPAND);
storageGrid->Add(checkSizer, 1, wxEXPAND);

storageSizer->Add(storageGrid, 0, wxEXPAND | wxALL, 5);
storageSizer->Add(m_filesView, 1, wxEXPAND | wxALL, 5);
Expand Down Expand Up @@ -215,32 +222,37 @@ void AddTorrentDialog::OnSavePathChanged(wxFileDirPickerEvent& event)

void AddTorrentDialog::OnSetPriority(wxCommandEvent& event)
{
wxDataViewItem item = m_filesView->GetSelection();
std::vector<int> fileIndices = m_filesViewModel->GetFileIndices(item);
wxDataViewItemArray items;
m_filesView->GetSelections(items);

int idx = m_torrents->GetSelection();
lt::add_torrent_params& params = m_params.at(idx);

for (auto index : fileIndices)
for (wxDataViewItem& item : items)
{
switch (event.GetId())
std::vector<int> fileIndices = m_filesViewModel->GetFileIndices(item);

int idx = m_torrents->GetSelection();
lt::add_torrent_params& params = m_params.at(idx);

for (auto index : fileIndices)
{
case FileContextMenu::ptID_PRIO_MAXIMUM:
params.file_priorities.at(index) = lt::top_priority;
break;
case FileContextMenu::ptID_PRIO_NORMAL:
params.file_priorities.at(index) = lt::default_priority;
break;
case FileContextMenu::ptID_PRIO_LOW:
params.file_priorities.at(index) = lt::low_priority;
break;
case FileContextMenu::ptID_PRIO_DO_NOT_DOWNLOAD:
params.file_priorities.at(index) = lt::dont_download;
break;
switch (event.GetId())
{
case FileContextMenu::ptID_PRIO_MAXIMUM:
params.file_priorities.at(index) = lt::top_priority;
break;
case FileContextMenu::ptID_PRIO_NORMAL:
params.file_priorities.at(index) = lt::default_priority;
break;
case FileContextMenu::ptID_PRIO_LOW:
params.file_priorities.at(index) = lt::low_priority;
break;
case FileContextMenu::ptID_PRIO_DO_NOT_DOWNLOAD:
params.file_priorities.at(index) = lt::dont_download;
break;
}
}
}

m_filesViewModel->UpdatePriorities(params.file_priorities);
m_filesViewModel->UpdatePriorities(params.file_priorities);
}
}

void AddTorrentDialog::OnSequentialDownloadChanged(wxCommandEvent&)
Expand All @@ -258,6 +270,23 @@ void AddTorrentDialog::OnSequentialDownloadChanged(wxCommandEvent&)
}
}

void AddTorrentDialog::OnStartTorrentChanged(wxCommandEvent&)
{
int idx = m_torrents->GetSelection();
lt::add_torrent_params& params = m_params.at(idx);

if (m_startTorrent->IsChecked())
{
params.flags &= ~lt::torrent_flags::paused;
params.flags |= lt::torrent_flags::auto_managed;
}
else
{
params.flags |= lt::torrent_flags::paused;
params.flags &= ~lt::torrent_flags::auto_managed;
}
}

void AddTorrentDialog::OnTorrentChanged(wxCommandEvent& event)
{
LoadTorrentInfo(event.GetInt());
Expand Down
3 changes: 3 additions & 0 deletions src/picotorrent/addtorrentdlg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace pt
ptID_TORRENT_LIST = wxID_HIGHEST,
ptID_SAVE_PATH,
ptID_SEQUENTIAL_DOWNLOAD,
ptID_START_TORRENT,
ptID_TORRENT_FILE_LIST,
ptID_FILE_LIST
};
Expand All @@ -46,6 +47,7 @@ namespace pt
void OnSavePathChanged(wxFileDirPickerEvent&);
void OnSetPriority(wxCommandEvent&);
void OnSequentialDownloadChanged(wxCommandEvent&);
void OnStartTorrentChanged(wxCommandEvent&);
void OnTorrentChanged(wxCommandEvent&);

wxDECLARE_EVENT_TABLE();
Expand All @@ -57,6 +59,7 @@ namespace pt
wxStaticText* m_infoHash;
wxDirPickerCtrl* m_savePath;
wxCheckBox* m_sequentialMode;
wxCheckBox* m_startTorrent;
wxDataViewCtrl* m_filesView;
FileStorageViewModel* m_filesViewModel;

Expand Down
39 changes: 33 additions & 6 deletions src/picotorrent/torrentcontextmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BEGIN_EVENT_TABLE(TorrentContextMenu, wxMenu)
EVT_MENU(ptID_FORCE_RECHECK, TorrentContextMenu::ForceRecheck)
EVT_MENU(ptID_FORCE_REANNOUNCE, TorrentContextMenu::ForceReannounce)
EVT_MENU(ptID_SEQUENTIAL_DOWNLOAD, TorrentContextMenu::SequentialDownload)
EVT_MENU(ptID_AUTO_MANAGED, TorrentContextMenu::AutoManaged)
END_EVENT_TABLE()

TorrentContextMenu::TorrentContextMenu(
Expand All @@ -54,8 +55,7 @@ TorrentContextMenu::TorrentContextMenu(
[this](lt::torrent_handle const& th)
{
lt::torrent_status ts = th.status();
return (ts.flags & (lt::torrent_flags::auto_managed
| lt::torrent_flags::paused)) == lt::torrent_flags::paused;
return (ts.flags & lt::torrent_flags::paused) == lt::torrent_flags::paused;
});

bool allNotPaused = std::all_of(
Expand All @@ -64,8 +64,7 @@ TorrentContextMenu::TorrentContextMenu(
[this](lt::torrent_handle const& th)
{
lt::torrent_status ts = th.status();
return !((ts.flags & (lt::torrent_flags::auto_managed
| lt::torrent_flags::paused)) == lt::torrent_flags::paused);
return !((ts.flags & lt::torrent_flags::paused) == lt::torrent_flags::paused);
});

wxMenuItem* resume = Append(ptID_RESUME, i18n(tr, "resume"));
Expand All @@ -82,6 +81,18 @@ TorrentContextMenu::TorrentContextMenu(
}

AppendSeparator();

if (m_state->selected_torrents.size() == 1)
{
wxMenuItem* autoManagedItem = Append(ptID_AUTO_MANAGED, i18n(tr, "auto_managed"));

if ((m_state->selected_torrents[0].flags() & lt::torrent_flags::auto_managed) == lt::torrent_flags::auto_managed)
{
autoManagedItem->SetCheckable(true);
autoManagedItem->Check();
}
}

Append(ptID_FORCE_REANNOUNCE, i18n(tr, "force_reannounce"));
Append(ptID_FORCE_RECHECK, i18n(tr, "force_recheck"));

Expand All @@ -105,6 +116,23 @@ TorrentContextMenu::TorrentContextMenu(
Append(ptID_OPEN_IN_EXPLORER, i18n(tr, "open_in_explorer"));
}

void TorrentContextMenu::AutoManaged(wxCommandEvent& WXUNUSED(event))
{
for (lt::torrent_handle& th : m_state->selected_torrents)
{
lt::torrent_flags_t flags = th.flags();

if ((flags & lt::torrent_flags::auto_managed) == lt::torrent_flags::auto_managed)
{
th.unset_flags(lt::torrent_flags::auto_managed);
}
else
{
th.set_flags(lt::torrent_flags::auto_managed);
}
}
}

void TorrentContextMenu::CopyInfoHash(wxCommandEvent& WXUNUSED(event))
{
std::stringstream ss;
Expand Down Expand Up @@ -182,7 +210,6 @@ void TorrentContextMenu::Pause(wxCommandEvent& WXUNUSED(event))
{
for (lt::torrent_handle& th : m_state->selected_torrents)
{
th.unset_flags(lt::torrent_flags::auto_managed);
th.pause(lt::torrent_handle::graceful_pause);
}
}
Expand All @@ -201,7 +228,7 @@ void TorrentContextMenu::Resume(wxCommandEvent& WXUNUSED(event))
{
for (lt::torrent_handle& th : m_state->selected_torrents)
{
th.set_flags(lt::torrent_flags::auto_managed);
th.resume();
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/picotorrent/torrentcontextmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ namespace pt
ptID_OPEN_IN_EXPLORER,
ptID_FORCE_RECHECK,
ptID_FORCE_REANNOUNCE,
ptID_SEQUENTIAL_DOWNLOAD
ptID_SEQUENTIAL_DOWNLOAD,
ptID_AUTO_MANAGED
};

wxDECLARE_EVENT_TABLE();

void AutoManaged(wxCommandEvent&);
void CopyInfoHash(wxCommandEvent&);
void ForceReannounce(wxCommandEvent&);
void ForceRecheck(wxCommandEvent&);
Expand Down

0 comments on commit ce84389

Please sign in to comment.