Skip to content

Commit fd44c9a

Browse files
authored
Show better error message when unable to create directory for dictionary for all users (#326)
1 parent 9c89295 commit fd44c9a

File tree

9 files changed

+34
-20
lines changed

9 files changed

+34
-20
lines changed

src/common/winapi.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ std::optional<std::wstring> browse_for_directory(HWND parent_wnd, const wchar_t
8282
bi.pidlRoot = pidl_root;
8383
bi.lpszTitle = title.c_str();
8484
bi.pszDisplayName = path.data();
85-
bi.ulFlags = BIF_RETURNONLYFSDIRS;
85+
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
8686
bi.lpfn = browse_callback_proc;
8787
bi.lParam = reinterpret_cast<LPARAM>(initial_path);
8888
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
@@ -94,8 +94,6 @@ std::optional<std::wstring> browse_for_directory(HWND parent_wnd, const wchar_t
9494
ret = sz_path.data();
9595
CoTaskMemFree(pidl);
9696
// free memory used
97-
98-
CoUninitialize();
9997
}
10098
return ret;
10199
}

src/npp/EditorInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ class EditorInterface {
122122
virtual TextPosition get_document_line_count() const = 0;
123123
virtual std::string get_active_document_text() const = 0;
124124
virtual RECT editor_rect() const = 0;
125-
126125
virtual int get_view_count() const = 0;
126+
virtual std::wstring get_editor_directory() const = 0;
127127

128128
// winapi function moved here for easier testing
129129
virtual std::optional<POINT> get_mouse_cursor_pos() const = 0;

src/npp/NppInterface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ NppInterface::NppInterface(const NppData *nppData)
2525
: m_npp_data{*nppData} {
2626
}
2727

28-
std::wstring NppInterface::get_npp_directory() {
28+
std::wstring NppInterface::get_npp_directory() const {
2929
std::vector<wchar_t> npp_path(MAX_PATH);
3030
send_msg_to_npp(NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(npp_path.data()));
3131
return npp_path.data();
@@ -70,6 +70,8 @@ HWND NppInterface::get_view_hwnd() const {
7070
return handle;
7171
}
7272

73+
std::wstring NppInterface::get_editor_directory() const { return get_npp_directory(); }
74+
7375
LRESULT NppInterface::send_msg_to_scintilla(UINT msg, WPARAM w_param, LPARAM l_param) const {
7476
assert(m_target_view != NppViewType::COUNT && "Outside view scope");
7577
return SendMessage(get_view_hwnd(), msg, w_param, l_param);

src/npp/NppInterface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NppData;
3333
class NppInterface : public EditorInterface {
3434
public:
3535
explicit NppInterface(const NppData *nppData);
36-
std::wstring get_npp_directory();
36+
std::wstring get_npp_directory() const;
3737
bool is_allocate_cmdid_supported() const;
3838
int allocate_cmdid(int requested_number);
3939
void set_menu_item_check(int cmd_id, bool checked);
@@ -109,9 +109,8 @@ class NppInterface : public EditorInterface {
109109
HMENU get_menu_handle(int menu_type) const;
110110
int get_target_view() const override;
111111
int get_indicator_value_at(int indicator_id, TextPosition position) const override;
112-
113-
public:
114112
HWND get_view_hwnd() const override;
113+
std::wstring get_editor_directory() const override;
115114

116115
private:
117116
LRESULT send_msg_to_npp(UINT Msg, WPARAM wParam = 0, LPARAM lParam = 0) const;
@@ -122,6 +121,7 @@ class NppInterface : public EditorInterface {
122121
std::vector<std::wstring> get_open_filenames_helper(int enum_val, int msg) const;
123122
int active_view() const override;
124123
std::optional<POINT> get_mouse_cursor_pos() const override;
124+
125125
private:
126126
const NppData &m_npp_data;
127127
mutable enum_array<NppViewType, std::optional<int>> m_lexer_cache;

src/plugin/Plugin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
//
2020

2121
#include "Constants.h"
22+
#include "Settings.h"
2223
#include "menuCmdID.h"
2324
#include "resource.h"
24-
#include "Settings.h"
2525
#include "CheckedList/CheckedList.h"
2626
#include "common/raii.h"
2727
#include "common/winapi.h"
@@ -35,10 +35,10 @@
3535
#include "ui/ConnectionSettingsDialog.h"
3636
#include "ui/ContextMenuHandler.h"
3737
#include "ui/DownloadDictionariesDialog.h"
38-
#include "ui/SelectMultipleLanguagesDialog.h"
3938
#include "ui/MenuItem.h"
4039
#include "ui/ProgressDialog.h"
4140
#include "ui/RemoveDictionariesDialog.h"
41+
#include "ui/SelectMultipleLanguagesDialog.h"
4242
#include "ui/SettingsDialog.h"
4343
#include "ui/SuggestionMenuButton.h"
4444

@@ -418,7 +418,7 @@ void init_classes() {
418418
suggestions_button->do_dialog();
419419

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

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

src/ui/DownloadDictionariesDialog.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@
2121
#include "ProgressDialog.h"
2222
#include "unzip.h"
2323
#include "CheckedList/CheckedList.h"
24-
#include "common/Utility.h"
2524
#include "common/ProgressData.h"
25+
#include "common/Utility.h"
26+
#include "common/WindowsDefs.h"
2627
#include "common/string_utils.h"
2728
#include "common/winapi.h"
28-
#include "common/WindowsDefs.h"
2929
#include "network/GithubFileListProvider.h"
3030
#include "network/UrlHelpers.h"
31+
#include "npp/EditorInterface.h"
3132
#include "plugin/Constants.h"
3233
#include "plugin/Plugin.h"
33-
#include "plugin/resource.h"
3434
#include "plugin/Settings.h"
35+
#include "plugin/resource.h"
3536
#include "spellers/HunspellInterface.h"
3637
#include "spellers/SpellerContainer.h"
3738

39+
#include <Wininet.h>
3840
#include <fcntl.h>
41+
#include <filesystem>
3942
#include <io.h>
4043
#include <variant>
41-
#include <Wininet.h>
44+
45+
namespace fs = std::filesystem;
4246

4347
void DownloadDictionariesDialog::do_dialog() {
4448
if (!isCreated()) {
@@ -76,8 +80,8 @@ DownloadDictionariesDialog::~DownloadDictionariesDialog() {
7680
DestroyIcon(m_refresh_icon);
7781
}
7882

79-
DownloadDictionariesDialog::DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container)
80-
: m_settings(settings), m_github_provider(std::make_unique<GitHubFileListProvider>(parent, settings)), m_speller_container(speller_container) {
83+
DownloadDictionariesDialog::DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container, const EditorInterface &editor)
84+
: m_settings(settings), m_github_provider(std::make_unique<GitHubFileListProvider>(parent, settings)), m_speller_container(speller_container), m_editor(editor) {
8185
m_github_provider->file_list_received.connect([this](const std::vector<FileDescription> &list) { on_new_file_list(list); });
8286
m_github_provider->error_happened.connect([this](const std::string &description) {
8387
auto wstr = to_wstring(description);
@@ -135,8 +139,13 @@ bool DownloadDictionariesDialog::prepare_downloading() {
135139
p->get_progress_data()->set(0, L"");
136140
get_progress_dlg()->update();
137141
p->set_top_message(L"");
142+
std::wstring dictionary_path;
143+
if (m_settings.data.download_install_dictionaries_for_all_users)
144+
dictionary_path = fs::weakly_canonical(fs::path(m_editor.get_editor_directory()) / m_settings.data.hunspell_system_path).wstring();
145+
else
146+
dictionary_path = m_settings.data.hunspell_user_path;
138147
if (!check_for_directory_existence(
139-
m_settings.data.download_install_dictionaries_for_all_users ? m_settings.data.hunspell_system_path : m_settings.data.hunspell_user_path,
148+
dictionary_path,
140149
false,
141150
_hParent)) // If path doesn't exist we're gonna try to create it
142151
// else it's finish

src/ui/DownloadDictionariesDialog.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <optional>
2424

25+
class EditorInterface;
26+
2527
enum class UrlType {
2628
ftp,
2729
ftp_web_proxy,
@@ -88,7 +90,7 @@ class DownloadDictionariesDialog : public StaticDialog {
8890

8991
public:
9092
~DownloadDictionariesDialog() override;
91-
DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container);
93+
DownloadDictionariesDialog(HINSTANCE h_inst, HWND parent, const Settings &settings, const SpellerContainer &speller_container, const EditorInterface &editor);
9294
void do_dialog();
9395
// Maybe hunspell interface should be passed here
9496
INT_PTR WINAPI run_dlg_proc(UINT message, WPARAM w_param,
@@ -160,4 +162,5 @@ class DownloadDictionariesDialog : public StaticDialog {
160162
bool m_address_is_set = false;
161163
std::unique_ptr<GitHubFileListProvider> m_github_provider;
162164
const SpellerContainer &m_speller_container;
165+
const EditorInterface &m_editor;
163166
};

test/MockEditorInterface.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ void MockEditorInterface::set_mouse_cursor_pos(const std::optional<POINT> &pos)
716716
m_cursor_pos = pos;
717717
}
718718

719+
std::wstring MockEditorInterface::get_editor_directory() const { return {}; }
720+
719721
MockedDocumentInfo *MockEditorInterface::active_document() {
720722
if (m_documents[m_target_view].empty())
721723
return nullptr;

test/MockEditorInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ class MockEditorInterface : public EditorInterface {
141141
void set_editor_rect(int left, int top, int right, int bottom);
142142
std::optional<POINT> get_mouse_cursor_pos() const override;
143143
void set_mouse_cursor_pos(const std::optional<POINT> &pos);
144+
std::wstring get_editor_directory() const override;
144145

145146
private:
146147
void set_target_view(int view_index) const override;
147148
int get_target_view() const override;
148149
const MockedDocumentInfo *active_document() const;
149150
MockedDocumentInfo *active_document();
150-
private:
151151
int active_view() const override;
152152

153153
public:

0 commit comments

Comments
 (0)