Skip to content

Commit

Permalink
Show better error message when unable to create directory for diction…
Browse files Browse the repository at this point in the history
…ary for all users (#326)
  • Loading branch information
Predelnik authored Aug 20, 2023
1 parent 9c89295 commit fd44c9a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/common/winapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::optional<std::wstring> browse_for_directory(HWND parent_wnd, const wchar_t
bi.pidlRoot = pidl_root;
bi.lpszTitle = title.c_str();
bi.pszDisplayName = path.data();
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = browse_callback_proc;
bi.lParam = reinterpret_cast<LPARAM>(initial_path);
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
Expand All @@ -94,8 +94,6 @@ std::optional<std::wstring> browse_for_directory(HWND parent_wnd, const wchar_t
ret = sz_path.data();
CoTaskMemFree(pidl);
// free memory used

CoUninitialize();
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion src/npp/EditorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class EditorInterface {
virtual TextPosition get_document_line_count() const = 0;
virtual std::string get_active_document_text() const = 0;
virtual RECT editor_rect() const = 0;

virtual int get_view_count() const = 0;
virtual std::wstring get_editor_directory() const = 0;

// winapi function moved here for easier testing
virtual std::optional<POINT> get_mouse_cursor_pos() const = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/npp/NppInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NppInterface::NppInterface(const NppData *nppData)
: m_npp_data{*nppData} {
}

std::wstring NppInterface::get_npp_directory() {
std::wstring NppInterface::get_npp_directory() const {
std::vector<wchar_t> npp_path(MAX_PATH);
send_msg_to_npp(NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(npp_path.data()));
return npp_path.data();
Expand Down Expand Up @@ -70,6 +70,8 @@ HWND NppInterface::get_view_hwnd() const {
return handle;
}

std::wstring NppInterface::get_editor_directory() const { return get_npp_directory(); }

LRESULT NppInterface::send_msg_to_scintilla(UINT msg, WPARAM w_param, LPARAM l_param) const {
assert(m_target_view != NppViewType::COUNT && "Outside view scope");
return SendMessage(get_view_hwnd(), msg, w_param, l_param);
Expand Down
6 changes: 3 additions & 3 deletions src/npp/NppInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NppData;
class NppInterface : public EditorInterface {
public:
explicit NppInterface(const NppData *nppData);
std::wstring get_npp_directory();
std::wstring get_npp_directory() const;
bool is_allocate_cmdid_supported() const;
int allocate_cmdid(int requested_number);
void set_menu_item_check(int cmd_id, bool checked);
Expand Down Expand Up @@ -109,9 +109,8 @@ class NppInterface : public EditorInterface {
HMENU get_menu_handle(int menu_type) const;
int get_target_view() const override;
int get_indicator_value_at(int indicator_id, TextPosition position) const override;

public:
HWND get_view_hwnd() const override;
std::wstring get_editor_directory() const override;

private:
LRESULT send_msg_to_npp(UINT Msg, WPARAM wParam = 0, LPARAM lParam = 0) const;
Expand All @@ -122,6 +121,7 @@ class NppInterface : public EditorInterface {
std::vector<std::wstring> get_open_filenames_helper(int enum_val, int msg) const;
int active_view() const override;
std::optional<POINT> get_mouse_cursor_pos() const override;

private:
const NppData &m_npp_data;
mutable enum_array<NppViewType, std::optional<int>> m_lexer_cache;
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
//

#include "Constants.h"
#include "Settings.h"
#include "menuCmdID.h"
#include "resource.h"
#include "Settings.h"
#include "CheckedList/CheckedList.h"
#include "common/raii.h"
#include "common/winapi.h"
Expand All @@ -35,10 +35,10 @@
#include "ui/ConnectionSettingsDialog.h"
#include "ui/ContextMenuHandler.h"
#include "ui/DownloadDictionariesDialog.h"
#include "ui/SelectMultipleLanguagesDialog.h"
#include "ui/MenuItem.h"
#include "ui/ProgressDialog.h"
#include "ui/RemoveDictionariesDialog.h"
#include "ui/SelectMultipleLanguagesDialog.h"
#include "ui/SettingsDialog.h"
#include "ui/SuggestionMenuButton.h"

Expand Down Expand Up @@ -418,7 +418,7 @@ void init_classes() {
suggestions_button->do_dialog();

settings_dlg = std::make_unique<SettingsDialog>(static_cast<HINSTANCE>(h_module), npp_data.npp_handle, *npp, *settings, *speller_container);
download_dics_dlg = std::make_unique<DownloadDictionariesDialog>(static_cast<HINSTANCE>(h_module), npp_data.npp_handle, *settings, *speller_container);
download_dics_dlg = std::make_unique<DownloadDictionariesDialog>(static_cast<HINSTANCE>(h_module), npp_data.npp_handle, *settings, *speller_container, *npp);

settings_dlg->download_dics_dlg_requested.connect([]() { download_dics_dlg->do_dialog(); });

Expand Down
23 changes: 16 additions & 7 deletions src/ui/DownloadDictionariesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,28 @@
#include "ProgressDialog.h"
#include "unzip.h"
#include "CheckedList/CheckedList.h"
#include "common/Utility.h"
#include "common/ProgressData.h"
#include "common/Utility.h"
#include "common/WindowsDefs.h"
#include "common/string_utils.h"
#include "common/winapi.h"
#include "common/WindowsDefs.h"
#include "network/GithubFileListProvider.h"
#include "network/UrlHelpers.h"
#include "npp/EditorInterface.h"
#include "plugin/Constants.h"
#include "plugin/Plugin.h"
#include "plugin/resource.h"
#include "plugin/Settings.h"
#include "plugin/resource.h"
#include "spellers/HunspellInterface.h"
#include "spellers/SpellerContainer.h"

#include <Wininet.h>
#include <fcntl.h>
#include <filesystem>
#include <io.h>
#include <variant>
#include <Wininet.h>

namespace fs = std::filesystem;

void DownloadDictionariesDialog::do_dialog() {
if (!isCreated()) {
Expand Down Expand Up @@ -76,8 +80,8 @@ DownloadDictionariesDialog::~DownloadDictionariesDialog() {
DestroyIcon(m_refresh_icon);
}

DownloadDictionariesDialog::DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container)
: m_settings(settings), m_github_provider(std::make_unique<GitHubFileListProvider>(parent, settings)), m_speller_container(speller_container) {
DownloadDictionariesDialog::DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container, const EditorInterface &editor)
: m_settings(settings), m_github_provider(std::make_unique<GitHubFileListProvider>(parent, settings)), m_speller_container(speller_container), m_editor(editor) {
m_github_provider->file_list_received.connect([this](const std::vector<FileDescription> &list) { on_new_file_list(list); });
m_github_provider->error_happened.connect([this](const std::string &description) {
auto wstr = to_wstring(description);
Expand Down Expand Up @@ -135,8 +139,13 @@ bool DownloadDictionariesDialog::prepare_downloading() {
p->get_progress_data()->set(0, L"");
get_progress_dlg()->update();
p->set_top_message(L"");
std::wstring dictionary_path;
if (m_settings.data.download_install_dictionaries_for_all_users)
dictionary_path = fs::weakly_canonical(fs::path(m_editor.get_editor_directory()) / m_settings.data.hunspell_system_path).wstring();
else
dictionary_path = m_settings.data.hunspell_user_path;
if (!check_for_directory_existence(
m_settings.data.download_install_dictionaries_for_all_users ? m_settings.data.hunspell_system_path : m_settings.data.hunspell_user_path,
dictionary_path,
false,
_hParent)) // If path doesn't exist we're gonna try to create it
// else it's finish
Expand Down
5 changes: 4 additions & 1 deletion src/ui/DownloadDictionariesDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <optional>

class EditorInterface;

enum class UrlType {
ftp,
ftp_web_proxy,
Expand Down Expand Up @@ -88,7 +90,7 @@ class DownloadDictionariesDialog : public StaticDialog {

public:
~DownloadDictionariesDialog() override;
DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container);
DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container, const EditorInterface &editor);
void do_dialog();
// Maybe hunspell interface should be passed here
INT_PTR WINAPI run_dlg_proc(UINT message, WPARAM w_param,
Expand Down Expand Up @@ -160,4 +162,5 @@ class DownloadDictionariesDialog : public StaticDialog {
bool m_address_is_set = false;
std::unique_ptr<GitHubFileListProvider> m_github_provider;
const SpellerContainer &m_speller_container;
const EditorInterface &m_editor;
};
2 changes: 2 additions & 0 deletions test/MockEditorInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ void MockEditorInterface::set_mouse_cursor_pos(const std::optional<POINT> &pos)
m_cursor_pos = pos;
}

std::wstring MockEditorInterface::get_editor_directory() const { return {}; }

MockedDocumentInfo *MockEditorInterface::active_document() {
if (m_documents[m_target_view].empty())
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion test/MockEditorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ class MockEditorInterface : public EditorInterface {
void set_editor_rect(int left, int top, int right, int bottom);
std::optional<POINT> get_mouse_cursor_pos() const override;
void set_mouse_cursor_pos(const std::optional<POINT> &pos);
std::wstring get_editor_directory() const override;

private:
void set_target_view(int view_index) const override;
int get_target_view() const override;
const MockedDocumentInfo *active_document() const;
MockedDocumentInfo *active_document();
private:
int active_view() const override;

public:
Expand Down

0 comments on commit fd44c9a

Please sign in to comment.