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