Verified Commit c821508c authored by Didier WECKMANN's avatar Didier WECKMANN
Browse files

enh(io): allow to specify when the open dialog is shown

parent 96206c8f
......@@ -211,7 +211,7 @@
<service>sight::module::io::session::SReader</service>
<desc>Session reader</desc>
<config>
<extension name=".sight" description="Sight session file"/>
<dialog extension=".sight" description="Sight session file"/>
</config>
</extension>
......@@ -220,7 +220,7 @@
<service>sight::module::io::session::SWriter</service>
<desc>Session writer</desc>
<config>
<extension name=".sight" description="Sight session file"/>
<dialog extension=".sight" description="Sight session file"/>
</config>
</extension>
......
......@@ -105,7 +105,7 @@
<service>sight::module::io::session::SReader</service>
<desc>Session reader</desc>
<config>
<extension name=".sample" description="Sample Sight session file"/>
<dialog extension=".sample" description="Sample Sight session file" policy="always"/>
<password policy="once" encryption="salted"/>
</config>
</extension>
......@@ -115,7 +115,7 @@
<service>sight::module::io::session::SWriter</service>
<desc>Session writer</desc>
<config>
<extension name=".sample" description="Sample Sight session file"/>
<dialog extension=".sample" description="Sample Sight session file" policy="once"/>
<password policy="once" encryption="salted"/>
</config>
</extension>
......
......@@ -53,6 +53,16 @@ public:
SIGHT_DECLARE_SERVICE(IReader, sight::service::IService);
/// Enum to define a password policy
enum class DialogPolicy : uint8_t
{
NEVER = 0, /// Never use show the dialog
ONCE = 1, /// Show only once, store the location as long as the service is started
ALWAYS = 2, /// Always show the location dialog
DEFAULT = ALWAYS, /// Default behavior if nothing is set
INVALID = 255 /// Used for error management
};
/**
* @name Slots API
* @{
......@@ -158,6 +168,51 @@ public:
//@}
/// Convenience function to convert from dialogPolicy enum value to string
constexpr static std::string_view dialogPolicyToString(DialogPolicy policy) noexcept
{
switch(policy)
{
case DialogPolicy::NEVER:
return "never";
case DialogPolicy::ONCE:
return "once";
case DialogPolicy::ALWAYS:
return "always";
default:
return "default";
}
}
/// Convenience function to convert from string to PasswordPolicy enum value
constexpr static DialogPolicy stringToDialogPolicy(std::string_view policy) noexcept
{
if(constexpr auto NEVER = dialogPolicyToString(DialogPolicy::NEVER); policy == NEVER)
{
return DialogPolicy::NEVER;
}
else if(constexpr auto ONCE = dialogPolicyToString(DialogPolicy::ONCE); policy == ONCE)
{
return DialogPolicy::ONCE;
}
else if(constexpr auto ALWAYS = dialogPolicyToString(DialogPolicy::ALWAYS); policy == ALWAYS)
{
return DialogPolicy::ALWAYS;
}
else if(policy.empty() || policy == "default")
{
return DialogPolicy::DEFAULT;
}
else
{
// Error case
return DialogPolicy::INVALID;
}
}
protected:
IO_BASE_API IReader() noexcept;
......
......@@ -54,6 +54,16 @@ public:
SIGHT_DECLARE_SERVICE(IWriter, sight::service::IService);
/// Enum to define a password policy
enum class DialogPolicy : uint8_t
{
NEVER = 0, /// Never use show the dialog
ONCE = 1, /// Show only once, store the location as long as the service is started
ALWAYS = 2, /// Always show the location dialog
DEFAULT = ALWAYS, /// Default behavior if nothing is set
INVALID = 255 /// Used for error management
};
/**
* @name Slots API
* @{
......@@ -152,6 +162,51 @@ public:
/// Returns if reading has been cancelled by user
IO_BASE_API bool hasFailed() const;
/// Convenience function to convert from dialogPolicy enum value to string
constexpr static std::string_view dialogPolicyToString(DialogPolicy policy) noexcept
{
switch(policy)
{
case DialogPolicy::NEVER:
return "never";
case DialogPolicy::ONCE:
return "once";
case DialogPolicy::ALWAYS:
return "always";
default:
return "default";
}
}
/// Convenience function to convert from string to PasswordPolicy enum value
constexpr static DialogPolicy stringToDialogPolicy(std::string_view policy) noexcept
{
if(constexpr auto NEVER = dialogPolicyToString(DialogPolicy::NEVER); policy == NEVER)
{
return DialogPolicy::NEVER;
}
else if(constexpr auto ONCE = dialogPolicyToString(DialogPolicy::ONCE); policy == ONCE)
{
return DialogPolicy::ONCE;
}
else if(constexpr auto ALWAYS = dialogPolicyToString(DialogPolicy::ALWAYS); policy == ALWAYS)
{
return DialogPolicy::ALWAYS;
}
else if(policy.empty() || policy == "default")
{
return DialogPolicy::DEFAULT;
}
else
{
// Error case
return DialogPolicy::INVALID;
}
}
protected:
IO_BASE_API IWriter() noexcept;
......
......@@ -74,6 +74,9 @@ public:
/// Extension description to use for file save dialog
std::string m_extensionDescription {"Sight session"};
/// Dialog policy to use for the file location
DialogPolicy m_dialogPolicy = {DialogPolicy::DEFAULT};
/// Password policy to use
PasswordKeeper::PasswordPolicy m_passwordPolicy {PasswordKeeper::PasswordPolicy::DEFAULT};
......@@ -99,12 +102,14 @@ SReader::~SReader() noexcept = default;
void SReader::starting()
{
clearLocations();
}
//-----------------------------------------------------------------------------
void SReader::stopping()
{
clearLocations();
}
//-----------------------------------------------------------------------------
......@@ -116,11 +121,17 @@ void SReader::configuring()
const auto& tree = this->getConfigTree();
// Extension configuration
const auto& extension = tree.get_child_optional("extension.<xmlattr>");
if(extension.is_initialized())
const auto& dialog = tree.get_child_optional("dialog.<xmlattr>");
if(dialog.is_initialized())
{
m_pimpl->m_extensionName = extension->get<std::string>("name");
m_pimpl->m_extensionDescription = extension->get<std::string>("description");
m_pimpl->m_extensionName = dialog->get<std::string>("extension");
m_pimpl->m_extensionDescription = dialog->get<std::string>("description");
m_pimpl->m_dialogPolicy = stringToDialogPolicy(dialog->get<std::string>("policy"));
SIGHT_THROW_IF(
"Cannot read dialog policy.",
m_pimpl->m_dialogPolicy == DialogPolicy::INVALID
);
}
// Password configuration
......@@ -157,7 +168,8 @@ void SReader::updating()
m_readFailed = true;
// Show the save dialog if the path is empty
if(!hasLocationDefined())
if((!hasLocationDefined() && m_pimpl->m_dialogPolicy != DialogPolicy::NEVER)
|| m_pimpl->m_dialogPolicy == DialogPolicy::ALWAYS)
{
openLocationDialog();
}
......
......@@ -54,7 +54,7 @@ namespace sight::module::io::session
* @code{.xml}
<service type="sight::module::io::session::SReader">
<inout key="data" uid="..." />
<extension name=".sample" description="Sample Sight session file"/>
<dialog extension=".sample" description="Sample Sight session file" policy="always"/>
<password policy="once, encryption=salted"/>
</service>
@endcode
......@@ -63,10 +63,15 @@ namespace sight::module::io::session
* - \b data [sight::data::Object]: object to read.
*
* @subsection Configuration Configuration
* - \b extensions(optional):
* \b name: defines the file extension that will be used for the session file.
* If the extensions is not specified, default ".zip" will be used.
* - \b dialog(optional):
* \b extension: defines the file extension that will be used for the session file.
* If the extension is not specified, default ".zip" will be used.
* \b description: allows to display a label in front of extension when the file dialog is shown.
* \b policy:
* - \b "never": never show the open dialog
* - \b "once": show only once, store the location as long as the service is started
* - \b "always": always show the location dialog
* - \b "default": default behavior, which is "always"
*
* - \b password(optional):
* \b policy: defines if we should protect the session file using a password and when to ask for it. It could be:
......
......@@ -73,6 +73,9 @@ public:
/// Extension description to use for file save dialog
std::string m_extensionDescription {"Sight session"};
/// Dialog policy to use for the file location
DialogPolicy m_dialogPolicy = {DialogPolicy::DEFAULT};
/// Password policy to use
PasswordKeeper::PasswordPolicy m_passwordPolicy {PasswordKeeper::PasswordPolicy::DEFAULT};
......@@ -95,12 +98,14 @@ SWriter::~SWriter() noexcept = default;
void SWriter::starting()
{
clearLocations();
}
//-----------------------------------------------------------------------------
void SWriter::stopping()
{
clearLocations();
}
//-----------------------------------------------------------------------------
......@@ -111,12 +116,18 @@ void SWriter::configuring()
const auto& tree = this->getConfigTree();
// Extension configuration
const auto& extension = tree.get_child_optional("extension.<xmlattr>");
if(extension.is_initialized())
// Dialog configuration
const auto& dialog = tree.get_child_optional("dialog.<xmlattr>");
if(dialog.is_initialized())
{
m_pimpl->m_extensionName = extension->get<std::string>("name");
m_pimpl->m_extensionDescription = extension->get<std::string>("description");
m_pimpl->m_extensionName = dialog->get<std::string>("extension");
m_pimpl->m_extensionDescription = dialog->get<std::string>("description");
m_pimpl->m_dialogPolicy = stringToDialogPolicy(dialog->get<std::string>("policy"));
SIGHT_THROW_IF(
"Cannot read dialog policy.",
m_pimpl->m_dialogPolicy == DialogPolicy::INVALID
);
}
// Password configuration
......@@ -150,7 +161,8 @@ void SWriter::updating()
m_writeFailed = true;
// Show the save dialog if the path is empty
if(!hasLocationDefined())
if((!hasLocationDefined() && m_pimpl->m_dialogPolicy != DialogPolicy::NEVER)
|| m_pimpl->m_dialogPolicy == DialogPolicy::ALWAYS)
{
openLocationDialog();
}
......
......@@ -54,7 +54,7 @@ namespace sight::module::io::session
* @code{.xml}
<service type="sight::module::io::session::SWriter">
<in key="data" uid="..." />
<extension name=".sample" description="Sample Sight session file"/>
<dialog extension=".sample" description="Sample Sight session file" policy="once"/>
<password policy="once", encryption=salted/>
</service>
@endcode
......@@ -63,10 +63,15 @@ namespace sight::module::io::session
* - \b data [sight::data::Object]: object to write.
*
* @subsection Configuration Configuration
* - \b extensions(optional):
* \b name: defines the file extension that will be used for the session file.
* If the extensions is not specified, default ".zip" will be used.
* - \b dialog(optional):
* \b extension: defines the file extension that will be used for the session file.
* If the extension is not specified, default ".zip" will be used.
* \b description: allows to display a label in front of extension when the file dialog is shown.
* \b policy:
* - \b "never": never show the open dialog
* - \b "once": show only once, store the location as long as the service is started
* - \b "always": always show the location dialog
* - \b "default": default behavior, which is "always"
*
* - \b password(optional):
* \b policy: defines if we should protect the session file using a password and when to ask for it. It could be:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment