otsdaq  v1_01_02
 All Classes Namespaces Functions
DesktopIconConfiguration_configuration.cc
1 #include "otsdaq-core/ConfigurationPluginDataFormats/DesktopIconConfiguration.h"
2 #include "otsdaq-core/Macros/ConfigurationPluginMacros.h"
3 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
4 
5 #include <iostream>
6 #include <fstream> // std::fstream
7 #include <stdio.h>
8 using namespace ots;
9 
10 #define DESKTOP_ICONS_FILE std::string(getenv("SERVICE_DATA_PATH")) + "/OtsWizardData/iconList.dat"
11 
12 //DesktopIconConfiguration Column names
13 #define COL_NAME "IconName"
14 #define COL_STATUS "Status"
15 #define COL_CAPTION "Caption"
16 #define COL_ALTERNATE_TEXT "AlternateText"
17 #define COL_FORCE_ONLY_ONE_INSTANCE "ForceOnlyOneInstance"
18 #define COL_REQUIRED_PERMISSION_LEVEL "RequiredPermissionLevel"
19 #define COL_IMAGE_URL "ImageURL"
20 #define COL_WINDOW_CONTENT_URL "WindowContentURL"
21 #define COL_APP_LINK "LinkToApplicationConfiguration"
22 #define COL_PARAMETER_LINK "LinkToParameterConfiguration"
23 #define COL_PARAMETER_KEY "windowParameterKey"
24 #define COL_PARAMETER_VALUE "windowParameterValue"
25 #define COL_FOLDER_PATH "FolderPath"
26 
27 //XDAQ App Column names
28 #define COL_APP_ID "Id"
29 
30 
31 
32 //==============================================================================
33 DesktopIconConfiguration::DesktopIconConfiguration(void) :
34  ConfigurationBase("DesktopIconConfiguration")
35 {
37  //WARNING: the names used in C++ MUST match the Configuration INFO //
39 
40  // <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
41 // <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ConfigurationInfo.xsd">
42 // <CONFIGURATION Name="DesktopIconConfiguration">
43 // <VIEW Name="DESKTOP_ICON_CONFIGURATION" Type="File,Database,DatabaseTest" Description="This%20table%20is%20used%20to%20specify%20the%20Icons%20available%20on%20the%20otsdaq%20Desktop.%20%0A%0AUsually%20a%20Desktop%20Icon%20opens%20the%20GUI%20to%20an%20otsdaq%20app%2C%20but%20an%20Icon%20may%20be%20configured%20to%20open%20any%20content%20accessible%20through%20the%20user's%20browser%20(Note%3A%20the%20target%20server%20may%20have%20to%20allow%20cross-origin%20requests%20for%20full%20functionality).%20%0A%0AHere%20is%20an%20explanation%20of%20the%20features%20associated%20with%20each%20column%3A%0A%3CINDENT%3E%0A-%20IconName%3A%0A%3CINDENT%3EThis%20is%20the%20unique%20ID%20value%20for%20each%20row.%3C%2FINDENT%3E%0A-%20Status%3A%0A%3CINDENT%3EIf%20On%2C%20the%20Icon%20will%20be%20shown%20on%20the%20Desktop.%20When%20Off%2C%20the%20Icon%20will%20not%20be%20displayed.%3C%2FINDENT%3E%0A-%20Caption%3A%0A%3CINDENT%3EThis%20is%20the%20text%20shown%20underneath%20the%20Icon.%3C%2FINDENT%3E%0A-%20AlternateText%3A%0A%3CINDENT%3EIf%20the%20ImageURL%20is%20omitted%20(left%20as%20default%20or%20blank)%2C%20then%20the%20alternate%20text%20is%20used%20in%20place%20of%20the%20Icon%20image.%20This%20is%20useful%20if%20you%20have%20not%20yet%20found%20an%20image%20to%20use%20for%20a%20particular%20icon.%3C%2FINDENT%3E%0A-%20ForceOnlyOneInstance%3A%0A%3CINDENT%3EIf%20True%2C%20then%20only%20window%20is%20allowed%20on%20the%20Desktop.%20If%20False%2C%20then%20each%20time%20the%20Icon%20is%20clicked%20a%20new%20instance%20of%20the%20window%20will%20open.%20For%20example%2C%20it%20is%20common%20to%20force%20only%20one%20instance%20of%20the%20state%20machine%20window.%3C%2FINDENT%3E%0A-%20RequiredPermissionLevel%3A%0A%3CINDENT%3EThis%20value%20is%20the%20minimum%20permission%20level%20for%20a%20user%20to%20have%20access%20to%20this%20Icon.%20If%20a%20user%20has%20insufficient%20access%2C%20the%20Icon%20will%20not%20appear%20on%20their%20Desktop.%20As%20a%20reminder%2C%20permission%20levels%20go%20from%201%20(lowest)%20to%20255%20(admin-level)%3B%20and%20only%20admins%20can%20modify%20the%20access%20level%20of%20users.%3C%2FINDENT%3E%0A-%20ImageURL%3A%0A%3CINDENT%3EThis%20is%20the%20URL%20to%20the%20image%20used%20for%20the%20Icon.%20The%20native%20resolution%20for%20Icons%20is%2064%20x%2064%20pixels.%20If%20left%20as%20the%20default%20value%20or%20blank%2C%20the%20the%20AlternateText%20field%20will%20be%20used%20for%20the%20Icon%20image.%3C%2FINDENT%3E%0A-%20WindowContentURL%3A%0A%3CINDENT%3EThis%20is%20the%20URL%20of%20the%20window%20content%20that%20will%20be%20opened%20when%20the%20user%20clicks%20this%20Icon.%20The%20window%20content%20is%20usually%20an%20otsdaq%20app%20but%20may%20be%20any%20web%20content%20accessible%20through%20the%20user's%20browser.%3C%2FINDENT%3E%0A-%20LinkToApplicationConfiguration%2FApplicationUID%3A%0A%3CINDENT%3EWhen%20the%20Icon%20refers%20to%20an%20otsdaq%20app%2C%20these%20two%20fields%20comprise%20the%20configuration%20link%20that%20connects%20the%20Icon%20to%20the%20app's%20ID%20-%20the%20app%20ID%20is%20used%20to%20target%20the%20app%20when%20sending%20requests.%20The%20LinkToApplicationConfiguration%20field%20is%20the%20configuration%20table%20part%20of%20the%20link%20and%20the%20ApplicationUID%20field%20is%20the%20UID%20part%20of%20the%20link.%3C%2FINDENT%3E%0A-%20FolderPath%3A%0A%3CINDENT%3EThis%20field%20is%20used%20to%20organize%20Icons%20into%20folders%20on%20the%20Desktop.%20For%20example%2C%20a%20value%20of%20%22myFolder%2FmySubfolder%22%20places%20this%20Icon%20inside%20a%20folder%20named%20%22mySubFolder%22%20which%20is%20inside%20the%20folder%20%22myFolder%22%20on%20the%20Desktop.%3C%2FINDENT%3E%0A%3C%2FINDENT%3E">
44 // <COLUMN Type="UID" Name="IconName" StorageName="ICON_NAME" DataType="VARCHAR2" DataChoices=""/>
45 // <COLUMN Type="OnOff" Name="Status" StorageName="STATUS" DataType="VARCHAR2" DataChoices=""/>
46 // <COLUMN Type="Data" Name="Caption" StorageName="CAPTION" DataType="VARCHAR2" DataChoices=""/>
47 // <COLUMN Type="Data" Name="AlternateText" StorageName="ALTERNATE_TEXT" DataType="VARCHAR2" DataChoices=""/>
48 // <COLUMN Type="TrueFalse" Name="ForceOnlyOneInstance" StorageName="FORCE_ONLY_ONE_INSTANCE" DataType="VARCHAR2" DataChoices=""/>
49 // <COLUMN Type="Data" Name="RequiredPermissionLevel" StorageName="REQUIRED_PERMISSION_LEVEL" DataType="VARCHAR2" DataChoices=""/>
50 // <COLUMN Type="Data" Name="ImageURL" StorageName="IMAGE_URL" DataType="VARCHAR2" DataChoices=""/>
51 // <COLUMN Type="Data" Name="WindowContentURL" StorageName="WINDOW_CONTENT_URL" DataType="VARCHAR2" DataChoices=""/>
52 // <COLUMN Type="ChildLink-0" Name="LinkToApplicationConfiguration" StorageName="LINK_TO_APPLICATION_CONFIGURATION" DataType="VARCHAR2" DataChoices=""/>
53 // <COLUMN Type="ChildLinkUID-0" Name="ApplicationUID" StorageName="APPLICATION_UID" DataType="VARCHAR2" DataChoices=""/>
54 // <COLUMN Type="Data" Name="FolderPath" StorageName="FOLDER_PATH" DataType="VARCHAR2" DataChoices=""/>
55 // <COLUMN Type="Comment" Name="CommentDescription" StorageName="COMMENT_DESCRIPTION" DataType="VARCHAR2" DataChoices=""/>
56 // <COLUMN Type="Author" Name="Author" StorageName="AUTHOR" DataType="VARCHAR2" DataChoices=""/>
57 // <COLUMN Type="Timestamp" Name="RecordInsertionTime" StorageName="RECORD_INSERTION_TIME" DataType="TIMESTAMP WITH TIMEZONE" DataChoices=""/>
58 // </VIEW>
59 // </CONFIGURATION>
60 // </ROOT>
61 
62 }
63 
64 //==============================================================================
65 DesktopIconConfiguration::~DesktopIconConfiguration(void)
66 {}
67 
68 //==============================================================================
69 void DesktopIconConfiguration::init(ConfigurationManager *configManager)
70 {
71  __MOUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
72  __MOUT__ << configManager->__SELF_NODE__ << std::endl;
73 
74 
75  bool status;
76  std::string val;
77  unsigned int intVal;
78 
79  bool first = true;
80 
81  auto childrenMap = configManager->__SELF_NODE__.getChildren();
82 
83 
84  //generate icons file
85  std::fstream fs;
86  fs.open(DESKTOP_ICONS_FILE, std::fstream::out | std::fstream::trunc);
87  if(fs.fail())
88  {
89  __SS__ << "Failed to open Desktop Icons run file: " << DESKTOP_ICONS_FILE << std::endl;
90  throw std::runtime_error(ss.str());
91  }
92 
93  for(auto &child:childrenMap)
94  {
95  child.second.getNode(COL_STATUS ).getValue(status);
96  if(!status) continue;
97 
98  if(first) first = false;
99  else fs << ",";
100 
101  child.second.getNode(COL_CAPTION ).getValue(val);
102  fs << removeCommas(val, false, true);
103  //__MOUT__ << "Icon caption: " << val << std::endl;
104 
105  fs << ",";
106  child.second.getNode(COL_ALTERNATE_TEXT ).getValue(val);
107  fs << removeCommas(val, false, true);
108 
109  fs << ",";
110  child.second.getNode(COL_FORCE_ONLY_ONE_INSTANCE ).getValue(status);
111  fs << (status?"1":"0");
112 
113  fs << ",";
114  child.second.getNode(COL_REQUIRED_PERMISSION_LEVEL ).getValue(val);
115  fs << removeCommas(val);
116 
117  fs << ",";
118  child.second.getNode(COL_IMAGE_URL ).getValue(val);
119  fs << removeCommas(val,true);
120 
121  fs << ",";
122  child.second.getNode(COL_WINDOW_CONTENT_URL ).getValue(val);
123  val = removeCommas(val,true);
124  fs << val;
125 
126  bool addedAppId = false;
127  //add URN/LID if link is given
128  if(!child.second.getNode(COL_APP_LINK ).isDisconnected())
129  {
130  //if last character is not '='
131  // then assume need to add "?urn="
132  if(val[val.size()-1] != '=')
133  fs << "?urn=";
134 
135  //__MOUT__ << "Following Application link." << std::endl;
136  child.second.getNode(COL_APP_LINK ).getNode(COL_APP_ID ).getValue(intVal);
137 
138  //__MOUT__ << "URN/LID=" << intVal << std::endl;
139  fs << intVal; //append number
140  addedAppId = true;
141  }
142 
143  //add parameters if link is given
144  if(!child.second.getNode(COL_PARAMETER_LINK ).isDisconnected())
145  {
146  //if there is no '?' found
147  // then assume need to add "?"
148  if(val.find('?') == std::string::npos)
149  fs << '?';
150  else if(addedAppId ||
151  val[val.size()-1] != '?') //if not first parameter, add &
152  fs << '&';
153 
154  //now add each paramter separated by &
155  auto paramGroupMap = child.second.getNode(COL_PARAMETER_LINK ).getChildren();
156  bool notFirst = false;
157  for(const auto param:paramGroupMap)
158  {
159  if(notFirst)
160  fs << '&';
161  else
162  notFirst = true;
163  fs << ConfigurationManager::encodeURIComponent(
164  param.second.getNode(COL_PARAMETER_KEY).getValue<std::string>()) << "=" <<
165  ConfigurationManager::encodeURIComponent(
166  param.second.getNode(COL_PARAMETER_VALUE).getValue<std::string>());
167  }
168  }
169 
170  fs << ",";
171  child.second.getNode(COL_FOLDER_PATH ).getValue(val);
172  if(val == ViewColumnInfo::DATATYPE_STRING_DEFAULT) val = "";
173  fs << removeCommas(val,true);
174  }
175 
176  //close icons file
177  fs.close();
178 }
179 
180 std::string DesktopIconConfiguration::removeCommas(const std::string &str,
181  bool andHexReplace, bool andHTMLReplace)
182 {
183  std::string retStr = "";
184  retStr.reserve(str.length());
185 
186  for(unsigned int i=0;i<str.length();++i)
187  if(str[i] != ',')
188  retStr += str[i];
189  else if(andHexReplace)
190  retStr += "%2C";
191  else if(andHTMLReplace)
192  retStr += "&#44;";
193 
194  return retStr;
195 }
196 
197 DEFINE_OTS_CONFIGURATION(DesktopIconConfiguration)