DataSource.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Controls/DataSource.h>
  28. #include <Rocket/Controls/DataSourceListener.h>
  29. #include <Rocket/Core/StringUtilities.h>
  30. #include <Rocket/Core/Log.h>
  31. #include <algorithm>
  32. namespace Rocket {
  33. namespace Controls {
  34. const Rocket::Core::String DataSource::CHILD_SOURCE("#child_data_source");
  35. const Rocket::Core::String DataSource::DEPTH("#depth");
  36. const Rocket::Core::String DataSource::NUM_CHILDREN("#num_children");
  37. typedef std::map< Rocket::Core::String, DataSource* > DataSourceMap;
  38. static DataSourceMap data_sources;
  39. DataSource::DataSource(const Rocket::Core::String& _name)
  40. {
  41. if (!_name.Empty())
  42. {
  43. name = _name;
  44. }
  45. else
  46. {
  47. name.FormatString(64, "%x", this);
  48. }
  49. data_sources[name] = this;
  50. }
  51. DataSource::~DataSource()
  52. {
  53. ListenerList listeners_copy = listeners;
  54. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  55. {
  56. (*i)->OnDataSourceDestroy(this);
  57. }
  58. DataSourceMap::iterator iterator = data_sources.find(name);
  59. if (iterator != data_sources.end() &&
  60. iterator->second == this)
  61. {
  62. data_sources.erase(name);
  63. }
  64. }
  65. const Rocket::Core::String& DataSource::GetDataSourceName()
  66. {
  67. return name;
  68. }
  69. DataSource* DataSource::GetDataSource(const Rocket::Core::String& data_source_name)
  70. {
  71. DataSourceMap::iterator i = data_sources.find(data_source_name);
  72. if (i == data_sources.end())
  73. {
  74. return NULL;
  75. }
  76. return (*i).second;
  77. }
  78. void DataSource::AttachListener(DataSourceListener* listener)
  79. {
  80. if (find(listeners.begin(), listeners.end(), listener) != listeners.end())
  81. {
  82. ROCKET_ERROR;
  83. return;
  84. }
  85. listeners.push_back(listener);
  86. }
  87. void DataSource::DetachListener(DataSourceListener* listener)
  88. {
  89. ListenerList::iterator i = find(listeners.begin(), listeners.end(), listener);
  90. ROCKET_ASSERT(i != listeners.end());
  91. if (i != listeners.end())
  92. {
  93. listeners.erase(i);
  94. }
  95. }
  96. void* DataSource::GetScriptObject() const
  97. {
  98. return NULL;
  99. }
  100. void DataSource::NotifyRowAdd(const Rocket::Core::String& table, int first_row_added, int num_rows_added)
  101. {
  102. ListenerList listeners_copy = listeners;
  103. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  104. {
  105. (*i)->OnRowAdd(this, table, first_row_added, num_rows_added);
  106. }
  107. }
  108. void DataSource::NotifyRowRemove(const Rocket::Core::String& table, int first_row_removed, int num_rows_removed)
  109. {
  110. ListenerList listeners_copy = listeners;
  111. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  112. {
  113. (*i)->OnRowRemove(this, table, first_row_removed, num_rows_removed);
  114. }
  115. }
  116. void DataSource::NotifyRowChange(const Rocket::Core::String& table, int first_row_changed, int num_rows_changed)
  117. {
  118. ListenerList listeners_copy = listeners;
  119. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  120. {
  121. (*i)->OnRowChange(this, table, first_row_changed, num_rows_changed);
  122. }
  123. }
  124. void DataSource::NotifyRowChange(const Rocket::Core::String& table)
  125. {
  126. ListenerList listeners_copy = listeners;
  127. for (ListenerList::iterator i = listeners_copy.begin(); i != listeners_copy.end(); ++i)
  128. {
  129. (*i)->OnRowChange(this, table);
  130. }
  131. }
  132. void DataSource::BuildRowEntries(Rocket::Core::StringList& row, const RowMap& row_map, const Rocket::Core::StringList& columns)
  133. {
  134. // Reserve the number of entries.
  135. row.resize(columns.size());
  136. for (size_t i = 0; i < columns.size(); i++)
  137. {
  138. // Look through our row_map for each entry and add it to the result set
  139. RowMap::const_iterator itr = row_map.find(columns[i]);
  140. if (itr != row_map.end())
  141. {
  142. row[i] = (*itr).second;
  143. }
  144. else
  145. {
  146. row[i] = "";
  147. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Failed to find required data source column %s", columns[i].CString());
  148. }
  149. }
  150. }
  151. }
  152. }