Skip to content

Commit

Permalink
Add file and directory parameter type to server's parameters dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Oct 26, 2024
1 parent 819da15 commit 113e035
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 21 deletions.
11 changes: 9 additions & 2 deletions Data/Scripts/directory.nut
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ function UploadFile(FileName, options) {

function GetServerParamList() {
return {
directory = "Directory"
directory = {
title = "Directory",
type = "filename",
directory = true
},
downloadUrl = "Download path (ftp or http)",
convertUncPath = "Convert UNC path \"\\\\\" to file://"
convertUncPath = {
title = "Convert UNC path \"\\\\\" to file://",
type = "boolean",
}
}
}
13 changes: 11 additions & 2 deletions Source/3rdpart/PropertyItemEditors.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,17 @@ class CPropertyButtonWindow :
LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) lParam;
if( m_wndButton != lpdis->hwndItem ) return 0;
CDCHandle dc(lpdis->hDC);
// Paint as ellipsis button
dc.DrawFrameControl(&lpdis->rcItem, DFC_BUTTON, (lpdis->itemState & ODS_SELECTED) != 0 ? DFCS_BUTTONPUSH | DFCS_PUSHED : DFCS_BUTTONPUSH);

HTHEME htheme = OpenThemeDataExEx(m_wndButton, L"BUTTON");
if (htheme) {
int uState = (lpdis->itemState & ODS_SELECTED) != 0 ? PBS_PRESSED : PBS_NORMAL;
DrawThemeBackground(htheme, dc, BP_PUSHBUTTON, uState, &lpdis->rcItem, 0);

CloseThemeData(htheme);
} else {
// Paint as ellipsis button
dc.DrawFrameControl(&lpdis->rcItem, DFC_BUTTON, (lpdis->itemState & ODS_SELECTED) != 0 ? DFCS_BUTTONPUSH | DFCS_PUSHED : DFCS_BUTTONPUSH);
}
dc.SetBkMode(TRANSPARENT);
LPCTSTR pstrEllipsis = _T("...");
dc.DrawText(pstrEllipsis, ::lstrlen(pstrEllipsis), &lpdis->rcItem, DT_CENTER | DT_EDITCONTROL | DT_SINGLELINE | DT_VCENTER);
Expand Down
8 changes: 6 additions & 2 deletions Source/3rdpart/PropertyItemImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ class CPropertyFileNameItem : public CPropertyItem
RECT rcWin = rc;
win->m_prop = this;
win->Create(hWnd, rcWin, szText);
/*RECT rcText;
win->GetRect(&rcText);
rcText.top += 3;
win->SetRect(&rcText);*/
ATLASSERT(win->IsWindow());
return *win;
}
Expand Down Expand Up @@ -654,10 +658,10 @@ class CPropertyFileNameItem : public CPropertyItem
USES_CONVERSION;
LPCTSTR pstrFileName = OLE2CT(m_val.bstrVal);
LPCTSTR p = pstrFileName;
while( *p != '\0' ) {
/* while (*p != '\0') {
if( *p == _T(':') || *p == _T('\\') ) pstrFileName = p + 1;
p = ::CharNext(p);
}
}*/
::lstrcpyn(pstr, pstrFileName, cchMax);
return TRUE;
}
Expand Down
4 changes: 3 additions & 1 deletion Source/3rdpart/PropertyList.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ class ATL_NO_VTABLE CPropertyListImpl :
GetItemRect(idx, &rc);
if( (m_dwExtStyle & PLS_EX_CATEGORIZED) != 0 ) rc.left += CATEGORY_INDENT;
rc.left += m_iMiddle + 1;
rc.bottom--;
//rc.top += 2;
}

BOOL _SpawnInplaceWindow(IProperty* prop, int idx)
Expand All @@ -524,7 +526,7 @@ class ATL_NO_VTABLE CPropertyListImpl :
// Create a new editor window
RECT rcValue = { 0 };
_GetInPlaceRect(idx, rcValue);
rcValue.bottom--;

//::InflateRect(&rcValue, 0, -1);
m_hwndInplace = prop->CreateInplaceControl(m_hWnd, rcValue);
if( m_hwndInplace != NULL ) {
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(SRC_LIST Network/NetworkClient.cpp
Upload/Parameters/TextParameter.cpp
Upload/Parameters/ChoiceParameter.cpp
Upload/Parameters/BooleanParameter.cpp
Upload/Parameters/FileNameParameter.cpp
Upload/Parameters/ParameterFactory.cpp
ServiceLocator.cpp
Settings/BasicSettings.cpp
Expand Down Expand Up @@ -120,6 +121,7 @@ set(HEADER_LIST Network/NetworkClient.h
Upload/Parameters/TextParameter.h
Upload/Parameters/ChoiceParameter.h
Upload/Parameters/BooleanParameter.h
Upload/Parameters/FileNameParameter.h
Upload/Parameters/ParameterFactory.h
Utils/CoreUtils.h
Utils/CryptoUtils.h
Expand Down
18 changes: 18 additions & 0 deletions Source/Core/Upload/Parameters/FileNameParameter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "FileNameParameter.h"

FileNameParameter::FileNameParameter(std::string title)
: TextParameter(std::move(title))
{
}

std::string FileNameParameter::getType() const {
return TYPE;
}

bool FileNameParameter::directory() const {
return directory_;
}

void FileNameParameter::setDirectory(bool enable) {
directory_ = enable;
}
18 changes: 18 additions & 0 deletions Source/Core/Upload/Parameters/FileNameParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "TextParameter.h"

class FileNameParameter: public TextParameter {
public:
explicit FileNameParameter(std::string name);

std::string getType() const override;

bool directory() const;
void setDirectory(bool enable);

inline static const std::string TYPE = "filename";

protected:
bool directory_ = false;
};
42 changes: 34 additions & 8 deletions Source/Core/Upload/Parameters/ParameterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "TextParameter.h"
#include "ChoiceParameter.h"
#include "BooleanParameter.h"
#include "FileNameParameter.h"

class ParameterFactory {
public:
Expand All @@ -18,14 +19,22 @@ class ChoiceParameterFactory : public ParameterFactory {
public:
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override {
auto choiceParameter = std::make_unique<ChoiceParameter>(name);
auto choices = table.GetValue<Sqrat::Array>("items");
if (!!choices) {
Sqrat::Array::iterator it;
while (choices->Next(it)) {
Sqrat::Table tbl(it.getValue(), choices->GetVM());
std::string itemLabel = ScriptAPI::GetValue(tbl.GetValue<std::string>("label"));
std::string itemId = ScriptAPI::GetValue(tbl.GetValue<std::string>("id"));
choiceParameter->addItem(itemId, itemLabel);
if (table.HasKey("items")) {
auto choices = table.GetValue<Sqrat::Array>("items");
if (!!choices) {
Sqrat::Array::iterator it;
while (choices->Next(it)) {
Sqrat::Table tbl(it.getValue(), choices->GetVM());
std::string itemLabel, itemId;
try {
itemLabel = ScriptAPI::GetValue(tbl.GetValue<std::string>("label"));
itemId = ScriptAPI::GetValue(tbl.GetValue<std::string>("id"));
} catch (const std::exception& ex) {

}

choiceParameter->addItem(itemId, itemLabel);
}
}
}
return choiceParameter;
Expand All @@ -46,13 +55,30 @@ class TextParameterFactory : public ParameterFactory {
}
};

class FileNameParameterFactory : public ParameterFactory {
public:
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override {
auto fileNameParameter = std::make_unique<FileNameParameter>(name);
bool chooseDirectory = false;
try {
chooseDirectory = ScriptAPI::GetValue(table.GetValue<bool>("directory"));
} catch (const std::exception& ex) {

}

fileNameParameter->setDirectory(chooseDirectory);
return fileNameParameter;
}
};

class ParameterFactoryRegistry {
public:
ParameterFactoryRegistry() {
// Registering factories by parameter type
registry_[ChoiceParameter::TYPE] = std::make_unique<ChoiceParameterFactory>();
registry_[BooleanParameter::TYPE] = std::make_unique<BooleanParameterFactory>();
registry_[TextParameter::TYPE] = std::make_unique<TextParameterFactory>(); // TextParameter by default
registry_[FileNameParameter::TYPE] = std::make_unique<FileNameParameterFactory>();
}

std::unique_ptr<AbstractParameter> createParameter(const std::string& type, const std::string& name, Sqrat::Table& table) const {
Expand Down
23 changes: 17 additions & 6 deletions Source/Core/Upload/ScriptUploadEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,23 @@ void SqTableToParameterList(Sqrat::SharedPtr<Sqrat::Table> tbl, ParameterList& l
} else if (obj.GetType() == OT_TABLE) {
// The new way to declare a parameter (in a nested table)
Sqrat::Table table(it.getValue(), tbl->GetVM());
std::string title = ScriptAPI::GetValue(table.GetValue<std::string>("title"));
std::string typeStr = ScriptAPI::GetValue(table.GetValue<std::string>("type"));
auto newParam = SqTableToParameter(it.getName(), typeStr, table);
if (newParam) {
newParam->setTitle(title);
list.push_back(std::move(newParam));
try {
if (!table.HasKey("title")) {
throw std::runtime_error("Parameter's declaration should have 'title' key.");
}
std::string title = ScriptAPI::GetValue(table.GetValue<std::string>("title"));
std::string typeStr;

if (table.HasKey("type")) {
typeStr = ScriptAPI::GetValue(table.GetValue<std::string>("type"));
}
auto newParam = SqTableToParameter(it.getName(), typeStr, table);
if (newParam) {
newParam->setTitle(title);
list.push_back(std::move(newParam));
}
} catch (const std::exception& ex) {
LOG(ERROR) << ex.what();
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions Source/Gui/Components/ParameterListAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#include "Core/Upload/Parameters/TextParameter.h"
#include "Core/Upload/Parameters/ChoiceParameter.h"
#include "Core/Upload/Parameters/BooleanParameter.h"
#include "Core/Upload/Parameters/FileNameParameter.h"
#include "Core/Upload/UploadEngine.h"
#include "Gui/Components/MyFileDialog.h"
#include "Core/i18n/Translator.h"
#include "Gui/Components/NewStyleFolderDialog.h"

class TextParameterController : public IParameterController {
public:
Expand Down Expand Up @@ -74,16 +78,37 @@ class ChoiceParameterController : public IParameterController {
}
};

class FileNameParameterController : public IParameterController {
public:
HPROPERTY show(AbstractParameter* parameter, CString title, const std::string& value, LPARAM lParam) override {
auto par = dynamic_cast<FileNameParameter*>(parameter);
if (!par) {
return nullptr;
}
return PropCreateFileName(title, U2W(parameter->getValueAsString()), lParam);
}

bool save(AbstractParameter* parameter, const CComVariant& value) override {
parameter->setValue(WCstringToUtf8(value.bstrVal));
return true;
}
};

ParameterListAdapter::ParameterListAdapter(ParameterList* parameterList, CPropertyListCtrl* control):
parameterList_(parameterList),
control_(control)
{
registerParameterController(TextParameter::TYPE, std::make_unique<TextParameterController>());
registerParameterController(BooleanParameter::TYPE, std::make_unique<BooleanParameterController>());
registerParameterController(ChoiceParameter::TYPE, std::make_unique<ChoiceParameterController>());
registerParameterController(FileNameParameter::TYPE, std::make_unique<FileNameParameterController>());
}

void ParameterListAdapter::updateControl(ServerSettingsStruct* serverSettings) {
std::sort(parameterList_->begin(), parameterList_->end(), [](auto& l, auto& r) {
int res = lstrcmpi(IuCoreUtils::Utf8ToWstring(l->getName()).c_str(), IuCoreUtils::Utf8ToWstring(r->getName()).c_str());
return res < 0;
});
for (const auto& parameter : *parameterList_) {
HPROPERTY prop = nullptr;
CString name = U2W(parameter->getName());
Expand Down Expand Up @@ -125,6 +150,46 @@ void ParameterListAdapter::saveValues(ServerSettingsStruct* serverSettings) {
}
}

void ParameterListAdapter::browseFileDialog(HPROPERTY prop) {
auto par = reinterpret_cast<AbstractParameter*>(prop->GetItemData());

if (!par) {
return;
}

auto* fileNameParameter = dynamic_cast<FileNameParameter*>(par);
if (!fileNameParameter) {
return;
}
VARIANT vart;
vart.vt = VT_BSTR;
if (!fileNameParameter->directory()) {
// TODO: customizable file name filters
IMyFileDialog::FileFilterArray filters = {
{ TR("All files"), _T("*.*") }
};
CString fileDir = U2W(IuCoreUtils::ExtractFilePath(fileNameParameter->getValueAsString()));

auto dlg = MyFileDialogFactory::createFileDialog(control_->m_hWnd, fileDir, {}, filters, false);
if (dlg->DoModal(control_->m_hWnd) != IDOK) {
return;
}
vart.bstrVal = dlg->getFile().AllocSysString();

} else {
CString initialDir = U2W(fileNameParameter->getValueAsString());
CNewStyleFolderDialog dlg(control_->m_hWnd, initialDir, TR("Choose folder"), true);
if (dlg.DoModal(control_->m_hWnd) != IDOK) {
return;
}

vart.bstrVal = dlg.GetFolderPath().AllocSysString();
}

control_->SetItemValue(prop, &vart);
SysFreeString(vart.bstrVal);
}

void ParameterListAdapter::registerParameterController(const std::string& type, std::unique_ptr<IParameterController> controller) {
registeredTypes_[type] = std::move(controller);
}
Expand Down
1 change: 1 addition & 0 deletions Source/Gui/Components/ParameterListAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ParameterListAdapter {
ParameterListAdapter(ParameterList* parameterList, CPropertyListCtrl* control);
void updateControl(ServerSettingsStruct* serverSettings);
void saveValues(ServerSettingsStruct* serverSettings);
void browseFileDialog(HPROPERTY prop);

void registerParameterController(const std::string& type, std::unique_ptr<IParameterController> controller);
private:
Expand Down
7 changes: 7 additions & 0 deletions Source/Gui/Dialogs/ServerParamsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ LRESULT CServerParamsDlg::OnLoginEditChange(WORD wNotifyCode, WORD wID, HWND hWn
return 0;
}

LRESULT CServerParamsDlg::OnParamListBrowseFile(int idCtrl, LPNMHDR pnmh, BOOL& bHandled) {
auto nmh = reinterpret_cast<NMPROPERTYITEM*>(pnmh);
parameterListAdapter_->browseFileDialog(nmh->prop);

return 0;
}

ServerProfile CServerParamsDlg::serverProfile() const
{
return serverProfile_;
Expand Down
2 changes: 2 additions & 0 deletions Source/Gui/Dialogs/ServerParamsDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CServerParamsDlg :
COMMAND_HANDLER(IDC_DOAUTH, BN_CLICKED, OnClickedDoAuth)
COMMAND_HANDLER(IDC_BROWSESERVERFOLDERS, BN_CLICKED, OnBrowseServerFolders)
COMMAND_HANDLER(IDC_LOGINEDIT, EN_CHANGE, OnLoginEditChange)
NOTIFY_HANDLER(IDC_PARAMLIST, PIN_BROWSE, OnParamListBrowseFile)
CHAIN_MSG_MAP(CDialogResize<CServerParamsDlg>)
REFLECT_NOTIFICATIONS()
END_MSG_MAP()
Expand All @@ -68,6 +69,7 @@ class CServerParamsDlg :
LRESULT OnClickedDoAuth(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnBrowseServerFolders(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnLoginEditChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
LRESULT OnParamListBrowseFile(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
ServerProfile serverProfile() const;
protected:
CPropertyListCtrl m_wndParamList;
Expand Down

0 comments on commit 113e035

Please sign in to comment.