Skip to content

Commit

Permalink
feat: support to set appId for DConfig
Browse files Browse the repository at this point in the history
  It's useful for generic config to explicitly specify application id.
  • Loading branch information
18202781743 committed Mar 1, 2024
1 parent 9c96f05 commit 333bf84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/global/dconfig.zh_CN.dox
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ sudo make install
@param[in] parent 父对象
@note 调用者只构造backend,由DConfig释放。

@fn static void DConfig::setAppId(const QString &appId)
@brief 显示指定应用Id,不采用DSGApplication::id()作为应用Id
@param[in] appId 配置文件所属的应用Id
@note 需要在QCoreApplication构造前设置。

@fn QString Dtk::Core::DConfig::backendName()
@brief 配置策略后端名称
@return 配置策略后端名称
Expand Down
2 changes: 2 additions & 0 deletions include/global/dconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class LIBDTKCORESHARED_EXPORT DConfig : public QObject, public DObject
static DConfig *createGeneric(DConfigBackend *backend, const QString &name, const QString &subpath = QString(),
QObject *parent = nullptr);

static void setAppId(const QString &appId);

QString backendName() const;

QStringList keyList() const;

Check warning on line 56 in include/global/dconfig.h

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

function QStringList Dtk::Core::DConfig::keyList is not documented!
Expand Down
17 changes: 16 additions & 1 deletion src/dconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ DConfigBackend::~DConfigBackend()
{
}

static QString _globalAppId;
class Q_DECL_HIDDEN DConfigPrivate : public DObjectPrivate
{
public:
Expand Down Expand Up @@ -600,7 +601,7 @@ DConfig::DConfig(const QString &name, const QString &subpath, QObject *parent)
}

DConfig::DConfig(DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent)
: DConfig(backend, DSGApplication::id(), name, subpath, parent)
: DConfig(backend, _globalAppId.isEmpty() ? DSGApplication::id() : _globalAppId, name, subpath, parent)
{

}
Expand Down Expand Up @@ -645,6 +646,20 @@ DConfig *DConfig::createGeneric(DConfigBackend *backend, const QString &name, co
return new DConfig(backend, NoAppId, name, subpath, parent);
}

/*!
* \brief Explicitly specify application Id for config.
* \param appId
* @note It's should be called before QCoreApplication constructed.
*/
void DConfig::setAppId(const QString &appId)
{
if (!_globalAppId.isEmpty()) {
qCWarning(cfLog, "`setAppId`should only be called once.");
}
_globalAppId = appId;
qCDebug(cfLog, "Explicitly specify application Id as appId=%s for config.", qPrintable(appId));
}

/*!
@~english
* @brief Use custom configuration policy backend to construct objects
Expand Down

0 comments on commit 333bf84

Please sign in to comment.