Refactoring Plugin Admin codes

This commit is contained in:
Don HO 2018-06-12 03:20:37 +02:00
parent a99fa9ae50
commit c30c0de0fc
2 changed files with 140 additions and 167 deletions

View File

@ -147,57 +147,45 @@ bool findStrNoCase(const generic_string & strHaystack, const generic_string & st
return (it != strHaystack.end());
}
bool PluginsAdminDlg::isFoundInAvailableListFromIndex(int index, generic_string str2search, bool inWhichPart) const
{
PluginUpdateInfo* pui = _availableList.getPluginInfoFromIndex(index);
generic_string searchIn;
if (inWhichPart == _inNames)
searchIn = pui->_displayName;
else //(inWhichPart == inDescs)
searchIn = pui->_description;
return (findStrNoCase(searchIn, str2search));
}
long PluginsAdminDlg::searchFromCurrentSel(generic_string str2search, bool inWhichPart, bool isNextMode) const
{
// search from curent selected item or from the beginning
long currentIndex = _availableListView.getSelectedIndex();
int nbItem = static_cast<int>(_availableListView.nbItem());
long currentIndex = _availableList.getSelectedIndex();
int nbItem = static_cast<int>(_availableList.nbItem());
if (currentIndex == -1)
{
// no selection, let's search from 0 to the end
for (int i = 0; i < nbItem; ++i)
{
size_t j = _availableListView.getLParamFromIndex(i);
generic_string searchIn;
if (inWhichPart == inNames)
searchIn = _availablePluginList[j]._name;
else //(inWhichPart == inDescs)
searchIn = _availablePluginList[j]._description;
if (findStrNoCase(searchIn, str2search))
if (isFoundInAvailableListFromIndex(i, str2search, inWhichPart))
return i;
}
}
else
else // with selection, let's search from currentIndex
{
// with selection, let's search from currentIndex
// from current position to the end
for (int i = currentIndex + (isNextMode ? 1 : 0); i < nbItem; ++i)
{
size_t j = _availableListView.getLParamFromIndex(i);
generic_string searchIn;
if (inWhichPart == inNames)
searchIn = _availablePluginList[j]._name;
else //(inWhichPart == inDescs)
searchIn = _availablePluginList[j]._description;
if (findStrNoCase(searchIn, str2search))
if (isFoundInAvailableListFromIndex(i, str2search, inWhichPart))
return i;
}
// from to begining to current position
for (int i = 0; i < currentIndex + (isNextMode ? 1 : 0); ++i)
{
size_t j = _availableListView.getLParamFromIndex(i);
generic_string searchIn;
if (inWhichPart == inNames)
searchIn = _availablePluginList[j]._name;
else //(inWhichPart == inDescs)
searchIn = _availablePluginList[j]._description;
if (findStrNoCase(searchIn, str2search))
if (isFoundInAvailableListFromIndex(i, str2search, inWhichPart))
return i;
}
}
@ -300,29 +288,29 @@ void PluginsAdminDlg::create(int dialogID, bool isRTL, bool msgDestParent)
generic_string vesionStr = pNativeSpeaker->getAttrNameStr(TEXT("Version"), "PluginAdmin", "Version");
generic_string stabilityStr = pNativeSpeaker->getAttrNameStr(TEXT("Stability"), "PluginAdmin", "Stability");
_availableListView.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_availableListView.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_availableListView.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_availableListView.setStyleOption(LVS_EX_CHECKBOXES);
_availableList.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_availableList.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_availableList.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_availableList.setViewStyleOption(LVS_EX_CHECKBOXES);
_availableListView.init(_hInst, _hSelf);
_availableListView.reSizeTo(listRect);
_availableList.initView(_hInst, _hSelf);
_availableList.reSizeView(listRect);
_updateListView.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_updateListView.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_updateListView.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_updateListView.setStyleOption(LVS_EX_CHECKBOXES);
_updateList.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_updateList.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_updateList.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_updateList.setViewStyleOption(LVS_EX_CHECKBOXES);
_updateListView.init(_hInst, _hSelf);
_updateListView.reSizeTo(listRect);
_updateList.initView(_hInst, _hSelf);
_updateList.reSizeView(listRect);
_installedListView.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_installedListView.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_installedListView.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_installedListView.setStyleOption(LVS_EX_CHECKBOXES);
_installedList.addColumn(columnInfo(pluginStr, nppParam->_dpiManager.scaleX(200)));
_installedList.addColumn(columnInfo(vesionStr, nppParam->_dpiManager.scaleX(100)));
_installedList.addColumn(columnInfo(stabilityStr, nppParam->_dpiManager.scaleX(70)));
_installedList.setViewStyleOption(LVS_EX_CHECKBOXES);
_installedListView.init(_hInst, _hSelf);
_installedListView.reSizeTo(listRect);
_installedList.initView(_hInst, _hSelf);
_installedList.reSizeView(listRect);
HWND hDesc = ::GetDlgItem(_hSelf, IDC_PLUGINADM_EDIT);
::MoveWindow(hDesc, descRect.left, descRect.top, descRect.right, descRect.bottom, TRUE);
@ -351,6 +339,7 @@ void PluginsAdminDlg::collectNppCurrentStatusInfos()
bool PluginsAdminDlg::installPlugins()
{
/*
vector<size_t> indexes = _availableListView.getCheckedIndexes();
NppParameters *pNppParameters = NppParameters::getInstance();
@ -377,11 +366,13 @@ bool PluginsAdminDlg::installPlugins()
Process updater(updaterFullPath.c_str(), updaterParams.c_str(), updaterDir.c_str());
updater.run();
}
*/
return true;
}
bool PluginsAdminDlg::updatePlugins()
{
/*
vector<size_t> indexes = _updateListView.getCheckedIndexes();
NppParameters *pNppParameters = NppParameters::getInstance();
@ -406,11 +397,13 @@ bool PluginsAdminDlg::updatePlugins()
Process updater(updaterFullPath.c_str(), updaterParams.c_str(), updaterDir.c_str());
updater.run();
}
*/
return true;
}
bool PluginsAdminDlg::removePlugins()
{
/*
vector<size_t> indexes = _installedListView.getCheckedIndexes();
NppParameters *pNppParameters = NppParameters::getInstance();
@ -433,11 +426,23 @@ bool PluginsAdminDlg::removePlugins()
Process updater(updaterFullPath.c_str(), updaterParams.c_str(), updaterDir.c_str());
updater.run();
}
*/
return true;
}
void PluginViewList::pushBack(PluginUpdateInfo* pi)
{
_list.push_back(pi);
bool loadFromJson(vector<PluginUpdateInfo> & pl, const json& j)
vector<generic_string> values2Add;
values2Add.push_back(pi->_displayName);
Version v = pi->_version;
values2Add.push_back(v.toString());
values2Add.push_back(TEXT("Yes"));
_ui.addLine(values2Add, reinterpret_cast<LPARAM>(pi));
}
bool loadFromJson(PluginViewList & pl, const json& j)
{
if (j.empty())
return false;
@ -451,28 +456,29 @@ bool loadFromJson(vector<PluginUpdateInfo> & pl, const json& j)
for (const auto& i : jArray)
{
try {
PluginUpdateInfo pi;
//std::unique_ptr<PluginUpdateInfo*> pi = make_unique<PluginUpdateInfo*>();
PluginUpdateInfo* pi = new PluginUpdateInfo();
string valStr = i.at("folder-name").get<std::string>();
pi._name = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_folderName = wmc->char2wchar(valStr.c_str(), CP_ACP);
valStr = i.at("display-name").get<std::string>();
pi._alias = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_displayName = wmc->char2wchar(valStr.c_str(), CP_ACP);
valStr = i.at("author").get<std::string>();
pi._author = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_author = wmc->char2wchar(valStr.c_str(), CP_ACP);
valStr = i.at("description").get<std::string>();
pi._description = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_description = wmc->char2wchar(valStr.c_str(), CP_ACP);
valStr = i.at("repository").get<std::string>();
pi._repository = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_repository = wmc->char2wchar(valStr.c_str(), CP_ACP);
valStr = i.at("homepage").get<std::string>();
pi._homepage = wmc->char2wchar(valStr.c_str(), CP_ACP);
pi->_homepage = wmc->char2wchar(valStr.c_str(), CP_ACP);
pl.push_back(pi);
pl.pushBack(pi);
}
catch (...) // Every field is mandatory. If one of property is missing, an exception is thrown then this plugin will be ignored
{
@ -511,72 +517,25 @@ bool PluginsAdminDlg::updateListAndLoadFromJson()
nppPluginListJson >> pluginsJson;
// initialize available list view
loadFromJson(_availablePluginList, pluginsJson);
updateAvailableListView();
loadFromJson(_availableList, pluginsJson);
// initialize update list view
checkUpdates();
updateUpdateListView();
// initialize installed list view
loadFromPluginInfos();
updateInstalledListView();
return true;
}
void PluginsAdminDlg::updateAvailableListView()
{
size_t i = 0;
//
for (const auto& pui : _availablePluginList)
{
vector<generic_string> values2Add;
values2Add.push_back(pui._name);
Version v = pui._version;
values2Add.push_back(v.toString());
values2Add.push_back(TEXT("Yes"));
_availableListView.addLine(values2Add, i++);
}
}
void PluginsAdminDlg::updateInstalledListView()
{
size_t i = 0;
//
for (const auto& lpi : _installedPluginList)
{
vector<generic_string> values2Add;
values2Add.push_back(lpi._name);
Version v = lpi._version;
values2Add.push_back(v.toString());
values2Add.push_back(TEXT("Yes"));
_installedListView.addLine(values2Add, i++);
}
}
void PluginsAdminDlg::updateUpdateListView()
{
size_t i = 0;
//
for (const auto& pui : _updatePluginList)
{
vector<generic_string> values2Add;
values2Add.push_back(pui._name);
Version v = pui._version;
values2Add.push_back(v.toString());
values2Add.push_back(TEXT("Yes"));
_updateListView.addLine(values2Add, i++);
}
}
PluginUpdateInfo::PluginUpdateInfo(const generic_string& fullFilePath, const generic_string& filename)
{
if (not::PathFileExists(fullFilePath.c_str()))
if (!::PathFileExists(fullFilePath.c_str()))
return;
_fullFilePath = fullFilePath;
_name = filename;
_displayName = filename;
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const char *path = wmc->wchar2char(fullFilePath.c_str(), CP_ACP);
@ -594,8 +553,8 @@ bool PluginsAdminDlg::loadFromPluginInfos()
for (const auto& i : _pPluginsManager->_loadedDlls)
{
PluginUpdateInfo pui(i._fullFilePath, i._fileName);
_installedPluginList.push_back(pui);
PluginUpdateInfo* pui = new PluginUpdateInfo(i._fullFilePath, i._fileName);
_installedList.pushBack(pui);
}
return true;
@ -622,7 +581,7 @@ bool PluginsAdminDlg::searchInPlugins(bool isNextMode) const
if (foundIndex == -1)
return false;
_availableListView.setSelection(foundIndex);
_availableList.setSelection(foundIndex);
return true;
}
@ -639,9 +598,9 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
showUpdate = false;
showInstalled = false;
long infoIndex = _availableListView.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_availablePluginList.size()))
desc = _availablePluginList.at(infoIndex).describe();
long infoIndex = _availableList.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_availableList.nbItem()))
desc = _availableList.getPluginInfoFromIndex(infoIndex)->describe();
}
break;
@ -651,9 +610,9 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
showUpdate = true;
showInstalled = false;
long infoIndex = _updateListView.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_updatePluginList.size()))
desc = _updatePluginList.at(infoIndex).describe();
long infoIndex = _updateList.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_updateList.nbItem()))
desc = _updateList.getPluginInfoFromIndex(infoIndex)->describe();
}
break;
@ -663,9 +622,9 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
showUpdate = false;
showInstalled = true;
long infoIndex = _installedListView.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_installedPluginList.size()))
desc = _installedPluginList.at(infoIndex).describe();
long infoIndex = _installedList.getSelectedIndex();
if (infoIndex != -1 && infoIndex < static_cast<long>(_installedList.nbItem()))
desc = _installedList.getPluginInfoFromIndex(infoIndex)->describe();
}
break;
@ -673,9 +632,9 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
return;
}
_availableListView.display(showAvailable);
_updateListView.display(showUpdate);
_installedListView.display(showInstalled);
_availableList.displayView(showAvailable);
_updateList.displayView(showUpdate);
_installedList.displayView(showInstalled);
::SetDlgItemText(_hSelf, IDC_PLUGINADM_EDIT, desc.c_str());
@ -686,7 +645,7 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
::ShowWindow(hInstallButton, showAvailable ? SW_SHOW : SW_HIDE);
if (showAvailable)
{
vector<size_t> checkedArray = _availableListView.getCheckedIndexes();
vector<size_t> checkedArray = _availableList.getCheckedIndexes();
showAvailable = checkedArray.size() > 0;
}
::EnableWindow(hInstallButton, showAvailable);
@ -694,7 +653,7 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
::ShowWindow(hUpdateButton, showUpdate ? SW_SHOW : SW_HIDE);
if (showUpdate)
{
vector<size_t> checkedArray = _updateListView.getCheckedIndexes();
vector<size_t> checkedArray = _updateList.getCheckedIndexes();
showUpdate = checkedArray.size() > 0;
}
::EnableWindow(hUpdateButton, showUpdate);
@ -702,7 +661,7 @@ void PluginsAdminDlg::switchDialog(int indexToSwitch)
::ShowWindow(hRemoveButton, showInstalled ? SW_SHOW : SW_HIDE);
if (showInstalled)
{
vector<size_t> checkedArray = _installedListView.getCheckedIndexes();
vector<size_t> checkedArray = _installedList.getCheckedIndexes();
showInstalled = checkedArray.size() > 0;
}
::EnableWindow(hRemoveButton, showInstalled);
@ -754,11 +713,9 @@ INT_PTR CALLBACK PluginsAdminDlg::run_dlgProc(UINT message, WPARAM wParam, LPARA
case IDC_PLUGINADM_REMOVE:
{
removePlugins();
return true;
}
default :
break;
}
@ -777,30 +734,26 @@ INT_PTR CALLBACK PluginsAdminDlg::run_dlgProc(UINT message, WPARAM wParam, LPARA
switchDialog(indexClicked);
}
}
else if (pnmh->hwndFrom == _availableListView.getHSelf() ||
pnmh->hwndFrom == _updateListView.getHSelf() ||
pnmh->hwndFrom == _installedListView.getHSelf())
else if (pnmh->hwndFrom == _availableList.getViewHwnd() ||
pnmh->hwndFrom == _updateList.getViewHwnd() ||
pnmh->hwndFrom == _installedList.getViewHwnd())
{
ListView* pListView;
vector<PluginUpdateInfo>* pPluginInfos;
PluginViewList* pViewList;
int buttonID;
if (pnmh->hwndFrom == _availableListView.getHSelf())
if (pnmh->hwndFrom == _availableList.getViewHwnd())
{
pListView = &_availableListView;
pPluginInfos = &_availablePluginList;
pViewList = &_availableList;
buttonID = IDC_PLUGINADM_INSTALL;
}
else if (pnmh->hwndFrom == _updateListView.getHSelf())
else if (pnmh->hwndFrom == _updateList.getViewHwnd())
{
pListView = &_updateListView;
pPluginInfos = &_updatePluginList;
pViewList = &_updateList;
buttonID = IDC_PLUGINADM_UPDATE;
}
else // pnmh->hwndFrom == _installedListView.getHSelf()
else // pnmh->hwndFrom == _installedList.getViewHwnd()
{
pListView = &_installedListView;
pPluginInfos = &_installedPluginList;
pViewList = &_installedList;
buttonID = IDC_PLUGINADM_REMOVE;
}
@ -814,15 +767,15 @@ INT_PTR CALLBACK PluginsAdminDlg::run_dlgProc(UINT message, WPARAM wParam, LPARA
(pnmv->uNewState & LVIS_STATEIMAGEMASK) == INDEXTOSTATEIMAGEMASK(1)) // unchecked
{
HWND hButton = ::GetDlgItem(_hSelf, buttonID);
vector<size_t> checkedArray = pListView->getCheckedIndexes();
vector<size_t> checkedArray = pViewList->getCheckedIndexes();
bool showButton = checkedArray.size() > 0;
::EnableWindow(hButton, showButton);
}
else if (pnmv->uNewState & LVIS_SELECTED)
{
size_t infoIndex = pListView->getLParamFromIndex(pnmv->iItem);
generic_string desc = pPluginInfos->at(infoIndex).describe();
PluginUpdateInfo* pui = pViewList->getPluginInfoFromIndex(pnmv->iItem);
generic_string desc = pui->describe();
::SetDlgItemText(_hSelf, IDC_PLUGINADM_EDIT, desc.c_str());
}
}

View File

@ -50,15 +50,15 @@ struct PluginUpdateInfo
{
generic_string _fullFilePath;
generic_string _id;
generic_string _name;
generic_string _folderName;
generic_string _displayName;
Version _version;
generic_string _homepage;
generic_string _sourceUrl;
generic_string _description;
generic_string _author;
generic_string _md5;
generic_string _alias;
generic_string _id;
generic_string _repository;
generic_string describe();
@ -84,15 +84,41 @@ struct NppCurrentStatus
bool shouldLaunchInAdmMode() { return _isInProgramFiles; };
};
class PluginViewList
{
public:
PluginViewList() {};
~PluginViewList() {
_ui.destroy();
for (auto i : _list)
{
delete i;
}
};
void pushBack(PluginUpdateInfo* pi);
HWND getViewHwnd() { return _ui.getHSelf(); };
void displayView(bool doShow) const { _ui.display(doShow); };
std::vector<size_t> getCheckedIndexes() const { return _ui.getCheckedIndexes(); };
long getSelectedIndex() const { return _ui.getSelectedIndex(); };
void setSelection(int index) const { _ui.setSelection(index); };
void initView(HINSTANCE hInst, HWND parent) { _ui.init(hInst, parent); };
void addColumn(const columnInfo & column2Add) { _ui.addColumn(column2Add); };
void reSizeView(RECT & rc) { _ui.reSizeTo(rc); }
void setViewStyleOption(int32_t extraStyle) { _ui.setStyleOption(extraStyle); };
size_t nbItem() const { return _ui.nbItem(); };
PluginUpdateInfo* getPluginInfoFromIndex(int index) const { return reinterpret_cast<PluginUpdateInfo*>(_ui.getLParamFromIndex(index)); };
private:
std::vector<PluginUpdateInfo*> _list;
ListView _ui;
};
class PluginsAdminDlg final : public StaticDialog
{
public :
PluginsAdminDlg() {};
~PluginsAdminDlg() {
_availableListView.destroy();
_updateListView.destroy();
_installedListView.destroy();
}
~PluginsAdminDlg() {}
void init(HINSTANCE hInst, HWND parent) {
Window::init(hInst, parent);
};
@ -113,13 +139,9 @@ public :
};
void switchDialog(int indexToSwitch);
bool updateListAndLoadFromJson(); // call GitUup for the 1st time
void updateAvailableListView();
void updateInstalledListView();
void updateUpdateListView();
void setPluginsManager(PluginsManager *pluginsManager) { _pPluginsManager = pluginsManager; };
bool updateListAndLoadFromJson();
void setAdminMode(bool isAdm) { _nppCurrentStatus._isAdminMode = isAdm; };
bool installPlugins();
@ -132,27 +154,25 @@ protected:
private :
TabBar _tab;
ListView _availableListView;
ListView _updateListView;
ListView _installedListView;
std::vector<PluginUpdateInfo> _availablePluginList; // All plugins (pluginList.json) - installed plugins
std::vector<PluginUpdateInfo> _updatePluginList; // A list returned by gitup.exe
std::vector<PluginUpdateInfo> _installedPluginList; // for each installed plugin, check its json file
PluginViewList _availableList; // All plugins (pluginList.json) - installed plugins
PluginViewList _updateList; // A list returned by gitup.exe
PluginViewList _installedList; // for each installed plugin, check its json file
PluginsManager *_pPluginsManager = nullptr;
NppCurrentStatus _nppCurrentStatus;
void collectNppCurrentStatusInfos();
bool searchInPlugins(bool isNextMode) const;
const bool inNames = true;
const bool inDescs = false;
const bool _inNames = true;
const bool _inDescs = false;
bool isFoundInAvailableListFromIndex(int index, generic_string str2search, bool inWhichPart) const;
long searchFromCurrentSel(generic_string str2search, bool inWhichPart, bool isNextMode) const;
long searchInNamesFromCurrentSel(generic_string str2search, bool isNextMode) const {
return searchFromCurrentSel(str2search, inNames, isNextMode);
return searchFromCurrentSel(str2search, _inNames, isNextMode);
};
long searchInDescsFromCurrentSel(generic_string str2search, bool isNextMode) const {
return searchFromCurrentSel(str2search, inDescs, isNextMode);
return searchFromCurrentSel(str2search, _inDescs, isNextMode);
};
bool loadFromPluginInfos();