attribute.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <precomp.h>
  2. #include "attribute.h"
  3. #include <api/config/items/cfgitemi.h>
  4. Attribute::Attribute(const wchar_t *newname, const wchar_t *_desc) :
  5. NamedW(newname), desc(_desc), cfgitemi(NULL), private_storage(NULL) { }
  6. Attribute::~Attribute() {
  7. delete private_storage;
  8. }
  9. const wchar_t *Attribute::getAttributeName() {
  10. return NamedW::getName();
  11. }
  12. const wchar_t *Attribute::getAttributeDesc() {
  13. return desc;
  14. }
  15. int Attribute::getValueAsInt()
  16. {
  17. wchar_t buf[1024]=L"";
  18. getData(buf, 1024);
  19. return WTOI(buf);
  20. }
  21. int Attribute::setValueAsInt(int newval, bool def)
  22. {
  23. return setData(StringPrintfW(newval), def);
  24. }
  25. double Attribute::getValueAsDouble()
  26. {
  27. wchar_t buf[1024] = {0};
  28. getData(buf, 1024);
  29. return WTOF(buf);
  30. }
  31. double Attribute::setValueAsDouble(double newval, bool def)
  32. {
  33. return setData(StringPrintfW(newval), def);
  34. }
  35. int Attribute::getDataLen() {
  36. if (private_storage != NULL)
  37. return (int)private_storage->len()+1;
  38. ASSERTPR(WASABI_API_CONFIG != NULL, "getDataLen() before API");
  39. int r = WASABI_API_CONFIG->getStringPrivateLen(mkTag());
  40. if (r < 0) {
  41. r = (int)default_val.len()+1;
  42. }
  43. return r;
  44. }
  45. int Attribute::getData(wchar_t *data, int data_len)
  46. {
  47. if (data == NULL || data_len < 0)
  48. return 0;
  49. if (private_storage != NULL)
  50. {
  51. if (private_storage->isempty())
  52. {
  53. if (data_len >= 1) {
  54. *data = 0;
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. WCSCPYN(data, private_storage->getValue(), data_len);
  60. return MIN((int)private_storage->len(), data_len);
  61. }
  62. ASSERTPR(WASABI_API_CONFIG != NULL, "can't get without api");
  63. if (WASABI_API_CONFIG == NULL) return 0;
  64. int r = WASABI_API_CONFIG->getStringPrivate(mkTag(), data, data_len, default_val.isempty() ? L"" : default_val.getValue());
  65. return r;
  66. }
  67. int Attribute::setData(const wchar_t *data, bool def)
  68. {
  69. if (def) { // setting default value
  70. default_val = data;
  71. return 1;
  72. }
  73. ASSERTPR(WASABI_API_CONFIG != NULL, "can't set data before api");
  74. if (WASABI_API_CONFIG == NULL) return 0;
  75. int r = setDataNoCB(data);
  76. if (r && cfgitemi != NULL) cfgitemi->cfgitem_onAttribSetValue(this);
  77. return r;
  78. }
  79. int Attribute::setDataNoCB(const wchar_t *data)
  80. {
  81. if (private_storage != NULL) {
  82. private_storage->setValue(data);
  83. } else {
  84. WASABI_API_CONFIG->setStringPrivate(mkTag(), data);
  85. }
  86. dependent_sendEvent(Attribute::depend_getClassGuid(), Event_DATACHANGE);
  87. return 1;
  88. }
  89. void Attribute::setCfgItem(CfgItemI *item)
  90. {
  91. delete private_storage; private_storage = NULL;
  92. ASSERT(cfgitemi == NULL || item == NULL);
  93. cfgitemi = item;
  94. if (cfgitemi != NULL)
  95. {
  96. if (cfgitemi->cfgitem_usePrivateStorage())
  97. private_storage = new StringW;
  98. }
  99. }
  100. StringW Attribute::mkTag()
  101. {
  102. StringW ret;
  103. if (cfgitemi)
  104. {
  105. ret = cfgitemi->cfgitem_getPrefix();
  106. }
  107. ret.cat(getName());
  108. return ret;
  109. }
  110. void Attribute::disconnect() {
  111. setCfgItem(NULL);
  112. }