otsdaq  v2_04_01
DesktopIconTable_table.cc
1 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq-core/Macros/TablePluginMacros.h"
3 #include "otsdaq-core/TablePlugins/DesktopIconTable.h"
4 #include "otsdaq-core/WebUsersUtilities/WebUsers.h"
5 
6 #include <stdio.h>
7 #include <fstream> // std::fstream
8 #include <iostream>
9 using namespace ots;
10 
11 #define DESKTOP_ICONS_FILE \
12  std::string(__ENV__("SERVICE_DATA_PATH")) + "/OtsWizardData/iconList.dat"
13 
14 // DesktopIconTable Column names
15 #define COL_NAME "IconName"
16 #define COL_STATUS TableViewColumnInfo::COL_NAME_STATUS
17 #define COL_CAPTION "Caption"
18 #define COL_ALTERNATE_TEXT "AlternateText"
19 #define COL_FORCE_ONLY_ONE_INSTANCE "ForceOnlyOneInstance"
20 #define COL_REQUIRED_PERMISSION_LEVEL "RequiredPermissionLevel"
21 #define COL_IMAGE_URL "ImageURL"
22 #define COL_WINDOW_CONTENT_URL "WindowContentURL"
23 #define COL_APP_LINK "LinkToApplicationTable"
24 #define COL_PARAMETER_LINK "LinkToParameterTable"
25 #define COL_PARAMETER_KEY "windowParameterKey"
26 #define COL_PARAMETER_VALUE "windowParameterValue"
27 #define COL_FOLDER_PATH "FolderPath"
28 
29 // XDAQ App Column names
30 #define COL_APP_ID "Id"
31 
32 //==============================================================================
33 DesktopIconTable::DesktopIconTable(void) : TableBase("DesktopIconTable")
34 {
35  // Icon list no longer passes through file! so delete it from user's $USER_DATA
36  std::system(("rm -rf " + (std::string)DESKTOP_ICONS_FILE).c_str());
37 
39  // WARNING: the names used in C++ MUST match the Table INFO //
41 }
42 
43 //==============================================================================
44 DesktopIconTable::~DesktopIconTable(void) {}
45 
46 //==============================================================================
47 void DesktopIconTable::init(ConfigurationManager* configManager)
48 {
49  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
50  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
51 
52  unsigned int intVal;
53 
54  auto childrenMap = configManager->__SELF_NODE__.getChildren();
55 
56  activeDesktopIcons_.clear();
57 
59  bool addedAppId;
60  bool numeric;
61  unsigned int i;
62  for(auto& child : childrenMap)
63  {
64  if(!child.second.getNode(COL_STATUS).getValue<bool>())
65  continue;
66 
67  activeDesktopIcons_.push_back(DesktopIconTable::DesktopIcon());
68  icon = &(activeDesktopIcons_.back());
69 
70  icon->caption_ = child.second.getNode(COL_CAPTION).getValue<std::string>();
71  icon->alternateText_ =
72  child.second.getNode(COL_ALTERNATE_TEXT).getValue<std::string>();
73  icon->enforceOneWindowInstance_ =
74  child.second.getNode(COL_FORCE_ONLY_ONE_INSTANCE).getValue<bool>();
75  icon->permissionThresholdString_ =
76  child.second.getNode(COL_REQUIRED_PERMISSION_LEVEL).getValue<std::string>();
77  icon->imageURL_ = child.second.getNode(COL_IMAGE_URL).getValue<std::string>();
78  icon->windowContentURL_ =
79  child.second.getNode(COL_WINDOW_CONTENT_URL).getValue<std::string>();
80  icon->folderPath_ = child.second.getNode(COL_FOLDER_PATH).getValue<std::string>();
81 
82  if(icon->folderPath_ == TableViewColumnInfo::DATATYPE_STRING_DEFAULT)
83  icon->folderPath_ = ""; // convert DEFAULT to empty string
84 
85  if(icon->permissionThresholdString_ ==
86  TableViewColumnInfo::DATATYPE_STRING_DEFAULT)
87  icon->permissionThresholdString_ =
88  "1"; // convert DEFAULT to standard user allow
89 
90  numeric = true;
91  for(i = 0; i < icon->permissionThresholdString_.size(); ++i)
92  if(!(icon->permissionThresholdString_[i] >= '0' &&
93  icon->permissionThresholdString_[i] <= '9'))
94  {
95  numeric = false;
96  break;
97  }
98  // for backwards compatibility, if permissions threshold is a single number
99  // assume it is the threshold intended for the WebUsers::DEFAULT_USER_GROUP group
100  if(numeric)
101  icon->permissionThresholdString_ =
102  WebUsers::DEFAULT_USER_GROUP + ":" + icon->permissionThresholdString_;
103 
104  // remove all commas from member strings because desktop icons are served to
105  // client in comma-separated string
106  icon->caption_ = removeCommas(
107  icon->caption_, false /*andHexReplace*/, true /*andHTMLReplace*/);
108  icon->alternateText_ = removeCommas(
109  icon->alternateText_, false /*andHexReplace*/, true /*andHTMLReplace*/);
110  icon->imageURL_ = removeCommas(icon->imageURL_, true /*andHexReplace*/);
111  icon->windowContentURL_ =
112  removeCommas(icon->windowContentURL_, true /*andHexReplace*/);
113  icon->folderPath_ = removeCommas(
114  icon->folderPath_, false /*andHexReplace*/, true /*andHTMLReplace*/);
115 
116  // add URN/LID to windowContentURL_, if link is given
117  addedAppId = false;
118  if(!child.second.getNode(COL_APP_LINK).isDisconnected())
119  {
120  // if last character is not '='
121  // then assume need to add "?urn="
122  if(icon->windowContentURL_[icon->windowContentURL_.size() - 1] != '=')
123  icon->windowContentURL_ += "?urn=";
124 
125  //__COUT__ << "Following Application link." << std::endl;
126  child.second.getNode(COL_APP_LINK).getNode(COL_APP_ID).getValue(intVal);
127  icon->windowContentURL_ += std::to_string(intVal);
128 
129  //__COUT__ << "URN/LID=" << intVal << std::endl;
130  addedAppId = true;
131  }
132 
133  // add parameters if link is given
134  if(!child.second.getNode(COL_PARAMETER_LINK).isDisconnected())
135  {
136  // if there is no '?' found
137  // then assume need to add "?"
138  if(icon->windowContentURL_.find('?') == std::string::npos)
139  icon->windowContentURL_ += '?';
140  else if(addedAppId ||
141  icon->windowContentURL_[icon->windowContentURL_.size() - 1] !=
142  '?') // if not first parameter, add &
143  icon->windowContentURL_ += '&';
144 
145  // now add each paramter separated by &
146  auto paramGroupMap = child.second.getNode(COL_PARAMETER_LINK).getChildren();
147  bool notFirst = false;
148  for(const auto param : paramGroupMap)
149  {
150  if(notFirst)
151  icon->windowContentURL_ += '&';
152  else
153  notFirst = true;
154  icon->windowContentURL_ +=
155  ConfigurationManager::encodeURIComponent(
156  param.second.getNode(COL_PARAMETER_KEY).getValue<std::string>()) +
157  "=" +
158  ConfigurationManager::encodeURIComponent(
159  param.second.getNode(COL_PARAMETER_VALUE)
160  .getValue<std::string>());
161  }
162  }
163  } // end main icon extraction loop
164 
165  //
166  // //generate icons file
167  // std::fstream fs;
168  // fs.open(DESKTOP_ICONS_FILE, std::fstream::out | std::fstream::trunc);
169  // if(fs.fail())
170  // {
171  // __SS__ << "Failed to open Desktop Icons run file: " << DESKTOP_ICONS_FILE <<
172  // std::endl;
173  // __SS_THROW__;
174  // }
175  //
176  // for(auto &child:childrenMap)
177  // {
178  // child.second.getNode(COL_STATUS ).getValue(status);
179  // if(!status) continue;
180  //
181  // if(first) first = false;
182  // else fs << ",";
183  //
184  // child.second.getNode(COL_CAPTION ).getValue(val);
185  // fs << removeCommas(val, false, true);
186  // //__COUT__ << "Icon caption: " << val << std::endl;
187  //
188  // fs << ",";
189  // child.second.getNode(COL_ALTERNATE_TEXT ).getValue(val);
190  // fs << removeCommas(val, false, true);
191  //
192  // fs << ",";
193  // child.second.getNode(COL_FORCE_ONLY_ONE_INSTANCE ).getValue(status);
194  // fs << (status?"1":"0");
195  //
196  // fs << ",";
197  // child.second.getNode(COL_REQUIRED_PERMISSION_LEVEL ).getValue(val);
198  // fs << removeCommas(val);
199  //
200  // fs << ",";
201  // child.second.getNode(COL_IMAGE_URL ).getValue(val);
202  // fs << removeCommas(val,true);
203  //
204  // fs << ",";
205  // child.second.getNode(COL_WINDOW_CONTENT_URL ).getValue(val);
206  // val = removeCommas(val,true);
207  // fs << val;
208  //
209  // bool addedAppId = false;
210  // //add URN/LID if link is given
211  // if(!child.second.getNode(COL_APP_LINK ).isDisconnected())
212  // {
213  // //if last character is not '='
214  // // then assume need to add "?urn="
215  // if(val[val.size()-1] != '=')
216  // fs << "?urn=";
217  //
218  // //__COUT__ << "Following Application link." << std::endl;
219  // child.second.getNode(COL_APP_LINK ).getNode(COL_APP_ID
220  //).getValue(intVal);
221  //
222  // //__COUT__ << "URN/LID=" << intVal << std::endl;
223  // fs << intVal; //append number
224  // addedAppId = true;
225  // }
226  //
227  // //add parameters if link is given
228  // if(!child.second.getNode(COL_PARAMETER_LINK ).isDisconnected())
229  // {
230  // //if there is no '?' found
231  // // then assume need to add "?"
232  // if(val.find('?') == std::string::npos)
233  // fs << '?';
234  // else if(addedAppId ||
235  // val[val.size()-1] != '?') //if not first parameter, add &
236  // fs << '&';
237  //
238  // //now add each paramter separated by &
239  // auto paramGroupMap = child.second.getNode(COL_PARAMETER_LINK
240  //).getChildren(); bool notFirst = false; for(const auto
241  // param:paramGroupMap)
242  // {
243  // if(notFirst)
244  // fs << '&';
245  // else
246  // notFirst = true;
247  // fs << ConfigurationManager::encodeURIComponent(
248  // param.second.getNode(COL_PARAMETER_KEY).getValue<std::string>())
249  //<<
250  //"="
251  //<< ConfigurationManager::encodeURIComponent(
252  // param.second.getNode(COL_PARAMETER_VALUE).getValue<std::string>());
253  // }
254  // }
255  //
256  // fs << ",";
257  // child.second.getNode(COL_FOLDER_PATH ).getValue(val);
258  // if(val == TableViewColumnInfo::DATATYPE_STRING_DEFAULT) val = "";
259  // fs << removeCommas(val,true);
260  // }
261  //
262  // //close icons file
263  // fs.close();
264 }
265 
266 std::string DesktopIconTable::removeCommas(const std::string& str,
267  bool andHexReplace,
268  bool andHTMLReplace)
269 {
270  std::string retStr = "";
271  retStr.reserve(str.length());
272 
273  for(unsigned int i = 0; i < str.length(); ++i)
274  if(str[i] != ',')
275  retStr += str[i];
276  else if(andHexReplace)
277  retStr += "%2C";
278  else if(andHTMLReplace)
279  retStr += "&#44;";
280 
281  return retStr;
282 }
283 
284 DEFINE_OTS_TABLE(DesktopIconTable)