Skip to content

Commit

Permalink
Merge pull request #1087 from vktr/fix/open-file-dialog-unicode
Browse files Browse the repository at this point in the history
Fix unicode issue with the OpenFileDialog
  • Loading branch information
vktr committed Dec 29, 2020
2 parents a8733db + 7b7f9f0 commit 1b3f652
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/picotorrent/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::wstring Utils::toHumanFileSize(int64_t bytes)

std::string Utils::toStdString(std::wstring const& input)
{
int size = WideCharToMultiByte(CP_UTF8, 0, &input[0], (int)input.size(), NULL, 0, NULL, NULL);
int size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), NULL, 0, NULL, NULL);
std::string result(size, 0);
WideCharToMultiByte(CP_UTF8, 0, &input[0], (int)input.size(), &result[0], size, NULL, NULL);
return result;
Expand Down
25 changes: 19 additions & 6 deletions src/picotorrent/ui/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,13 @@ void MainFrame::HandleParams(std::vector<std::string> const& files, std::vector<

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

ParseTorrentFiles(params, converted);
}

if (!magnets.empty())
Expand Down Expand Up @@ -744,7 +750,7 @@ void MainFrame::OnFileAddTorrent(wxCommandEvent&)
ofd.SetTitle(i18n("add_torrent_s"));
ofd.Show(this);

std::vector<std::string> files;
std::vector<std::wstring> files;
ofd.GetFiles(files);

if (files.empty())
Expand Down Expand Up @@ -844,18 +850,25 @@ void MainFrame::OnViewPreferences(wxCommandEvent&)
}
}

void MainFrame::ParseTorrentFiles(std::vector<lt::add_torrent_params>& params, std::vector<std::string> const& paths)
void MainFrame::ParseTorrentFiles(std::vector<lt::add_torrent_params>& params, std::vector<std::wstring> const& paths)
{
for (std::string const& path : paths)
for (std::wstring const& path : paths)
{
lt::error_code ec;
lt::add_torrent_params param;

param.ti = std::make_shared<lt::torrent_info>(path, ec);
std::ifstream in(path, std::ios::binary);
std::stringstream ss;
ss << in.rdbuf();

param.ti = std::make_shared<lt::torrent_info>(
ss.str().data(),
static_cast<int>(ss.str().size()),
ec);

if (ec)
{
BOOST_LOG_TRIVIAL(error) << "Failed to parse torrent file: " << ec;
BOOST_LOG_TRIVIAL(error) << "Failed to parse torrent file: " << ec.message();
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/ui/mainframe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace Models
void OnIconize(wxIconizeEvent&);
void OnTaskBarLeftDown(wxTaskBarIconEvent&);
void OnViewPreferences(wxCommandEvent&);
void ParseTorrentFiles(std::vector<libtorrent::add_torrent_params>& params, std::vector<std::string> const& paths);
void ParseTorrentFiles(std::vector<libtorrent::add_torrent_params>& params, std::vector<std::wstring> const& paths);
void ShowTorrentContextMenu(wxCommandEvent&);
void UpdateLabels();

Expand Down
5 changes: 2 additions & 3 deletions src/picotorrent/ui/win32/openfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OpenFileDialog::~OpenFileDialog()
m_wrappedDialog->Release();
}

void OpenFileDialog::GetFiles(std::vector<std::string>& files)
void OpenFileDialog::GetFiles(std::vector<std::wstring>& files)
{
IShellItemArray* results = nullptr;

Expand All @@ -60,8 +60,7 @@ void OpenFileDialog::GetFiles(std::vector<std::string>& files)
if (SUCCEEDED(item->GetDisplayName(SIGDN_FILESYSPATH, &path))
&& path != nullptr)
{
files.push_back(
Utils::toStdString(path));
files.push_back(path);
CoTaskMemFree(path);
}

Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/ui/win32/openfiledialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace pt::UI::Win32
OpenFileDialog();
~OpenFileDialog();

void GetFiles(std::vector<std::string>& files);
void GetFiles(std::vector<std::wstring>& files);

void SetFileTypes(std::vector<std::tuple<std::wstring, std::wstring>> const& types);
void SetOption(Option opt);
Expand Down

0 comments on commit 1b3f652

Please sign in to comment.