1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 #include "../base/defines.h"
11 
12 
13 
14 
15 #include "DXStrings.h"
16 #include "lex.h"
17 #include "PanelAccessManager.h"
18 #include "PanelGroupManager.h"
19 #include "Network.h"
20 #include "ControlPanel.h"
21 #include "ErrorDialogManager.h"
22 #include "List.h"
23 #include "ListIterator.h"
24 
PanelAccessManager(Network * n,ControlPanel * cp)25 PanelAccessManager::PanelAccessManager(Network *n, ControlPanel *cp)
26 {
27     this->network = n;
28     this->cp = cp;
29     this->isActive = FALSE;
30 }
~PanelAccessManager()31 PanelAccessManager::~PanelAccessManager()
32 {
33     this->clear();
34 }
35 
clear()36 void PanelAccessManager::clear()
37 {
38     this->allowAllPanelAccess();
39     this->allowAllGroupAccess();
40     this->deactivate();
41 }
42 
43 //
44 // Determine if the given panel (specified by instance number) is accessible
45 //
isAccessiblePanel(int instanceNumber)46 boolean PanelAccessManager::isAccessiblePanel(int instanceNumber)
47 {
48     return !this->isActivated() ||
49  	   !this->inaccessiblePanels.isMember((void*)instanceNumber);
50 }
isActivated()51 boolean PanelAccessManager::isActivated()
52 {
53     return this->isActive;
54 }
activate()55 void PanelAccessManager::activate()
56 {
57     this->isActive = TRUE;
58 }
deactivate()59 void PanelAccessManager::deactivate()
60 {
61     this->isActive = FALSE;
62 }
63 //
64 // Determine if the given panel group  is accessible
65 //
isAccessibleGroup(const char * name)66 boolean     PanelAccessManager::isAccessibleGroup(const char *name)
67 {
68     if (!this->isActivated())
69 	return TRUE;
70 
71     ListIterator li(this->inaccessibleGroups);
72     char *gname;
73     while ( (gname = (char*)li.getNext()) )
74         if (EqualString(gname, name))
75             return FALSE;
76 
77     return TRUE;
78 }
79 //
80 // Get the index'th (1 based) inaccessible panel.
81 //
getInaccessiblePanel(int index)82 ControlPanel *PanelAccessManager::getInaccessiblePanel(int index)
83 {
84     ListIterator iterator(this->inaccessiblePanels);
85     List	obsolete;
86     Network	*net = this->network;
87     int		instance;
88     ControlPanel *p, *found = NULL;
89 
90     ASSERT(index > 0);
91 
92     while (!found && (instance = (int)(long)iterator.getNext())) {
93 	p = net->getPanelByInstance(instance);
94 	if (!p)  {
95 	    //
96 	    // cp was deleted from the network behind our backs so save
97 	    // it and remove it later.
98 	    //
99 	    obsolete.appendElement((void*)instance);
100 	} else if (--index == 0)
101 	    found = p;
102     }
103 
104     //
105     // Clean the deleted panels out of the inaccessible list.
106     // Note, that because we stop the above loop as soon as we find the
107     // requested panel, there may be deleted panels left in the list
108     // after the following loop.
109     //
110     if (obsolete.getSize() > 0) {
111 	iterator.setList(obsolete);
112 	while ( (instance = (int)(long)iterator.getNext()) ) {
113 	    this->inaccessiblePanels.removeElement((void*)instance);
114 	}
115     }
116 
117     return found;
118 }
119 //
120 // Get the index'th (1 based) inaccessible group.
121 //
getInaccessibleGroup(int index)122 const char *PanelAccessManager::getInaccessibleGroup(int index)
123 {
124     ListIterator iterator(this->inaccessibleGroups);
125     List        obsolete;
126     Network     *net = this->network;
127     PanelGroupManager *pgm = net->getPanelGroupManager();
128     char        *name, *found = NULL;
129 
130     ASSERT(index > 0);
131 
132     while (!found && (name = (char*)iterator.getNext())) {
133         if (!pgm->getPanelGroup(name, NULL))  {
134             //
135             // group was deleted from the network behind our backs so save
136             // it and remove it later.
137             //
138             obsolete.appendElement((void*)name);
139         } else if (--index == 0)
140             found = name;
141     }
142 
143 
144     //
145     // Clean the deleted groups out of the inaccessible list.
146     // Note, that because we stop the above loop as soon as we find the
147     // requested panel, there may be deleted groups left in the list
148     // after the following loop.
149     //
150     if (obsolete.getSize() > 0) {
151         ListIterator iterator(obsolete);
152         while ( (name = (char*)iterator.getNext()) ) {
153 	    char gname[64];
154 	    strcpy(gname,name);
155             this->inaccessibleGroups.removeElement(name);
156         }
157     }
158 
159     return found;
160 }
161 
162 //
163 // Allow access to all panels.
164 //
allowAllPanelAccess()165 void        PanelAccessManager::allowAllPanelAccess()
166 {
167     if (this->inaccessiblePanels.getSize() != 0) {
168 	this->inaccessiblePanels.clear();
169 	this->network->setFileDirty();
170     }
171     this->activate();
172 }
173 //
174 // Allow access to all groups.
175 //
allowAllGroupAccess()176 void        PanelAccessManager::allowAllGroupAccess()
177 {
178 
179     if (this->inaccessibleGroups.getSize()) {
180 	char *name;
181 	ListIterator li(this->inaccessibleGroups);
182 
183 	while( (name = (char*)li.getNext()) )
184 	    delete name;
185 
186 	this->inaccessibleGroups.clear();
187 	this->network->setFileDirty();
188     }
189     this->activate();
190 }
191 //
192 // Add a panel (specified by instance number) to the list of inaccessible
193 // panels.
194 //
addInaccessiblePanel(int instance)195 void        PanelAccessManager::addInaccessiblePanel(int instance)
196 {
197     if (!this->inaccessiblePanels.isMember((void*)instance)){
198         this->inaccessiblePanels.appendElement((void*)instance);
199     	this->network->setFileDirty();
200     }
201     this->activate();
202 }
203 //
204 // Add a group(specified by name) to the list of inaccessible groups.
205 //
addInaccessibleGroup(const char * name)206 void        PanelAccessManager::addInaccessibleGroup(const char *name)
207 {
208     if (this->isAccessibleGroup(name)) {
209 	char *n = DuplicateString(name);
210         this->inaccessibleGroups.appendElement((void*)n);
211         this->network->setFileDirty();
212     }
213     this->activate();
214 }
215 //
216 // Return TRUE if cfgPrintInaccessibleComment needs to be called.
217 //
hasCfgComment()218 boolean PanelAccessManager::hasCfgComment()
219 {
220 #if 0
221     return (this->isActivated() &&
222 	    ((this->inaccessiblePanels.getSize() > 0) ||
223     	     (this->inaccessibleGroups.getSize() > 0)));
224 #else
225     return this->isActivated();
226 #endif
227 }
228 //
229 // Print a control panel's 'inaccessible' comment, which indicates
230 // which panel instances can not be opened from this panel.
231 // 	Format = '// inaccessible: %d %d...'
232 // where the numbers printed are the panel instance numbers.
233 // NOTE: instance numbers in the file are 0 based, while internally they
234 // 	are 1 based.
235 //
cfgPrintInaccessibleComment(FILE * f)236 boolean PanelAccessManager::cfgPrintInaccessibleComment(FILE *f)
237 {
238     ControlPanel *cp;
239     int	i;
240     const char *name;
241 
242     if (!this->isActivated())
243 	return TRUE;
244 
245 
246     //
247     // Print inaccessible panels
248     //
249     if (fprintf(f, "// inaccessible panels:") < 0)
250 	return FALSE;
251     cp = this->getInaccessiblePanel(1);
252     for (i=1 ; (cp=this->getInaccessiblePanel(i)) ; i++)
253     {
254     	if (fprintf(f, " %d",cp->getInstanceNumber()-1) < 0)
255 	   return FALSE;
256 
257     }
258     if (fprintf(f, "\n") < 0)
259        return FALSE;
260 
261     //
262     // Print inaccessible groups
263     //
264     if (fprintf(f, "// inaccessible groups:") < 0)
265 	return FALSE;
266 
267     for (i=1 ; (name = this->getInaccessibleGroup(i)) ; i++)
268     {
269 	if (fprintf(f, " \"%s\"",name) < 0)
270 	    return FALSE;
271     }
272     if (fprintf(f, "\n") < 0)
273 	return FALSE;
274 
275     return TRUE;
276 }
277 //
278 // Parse a control panel's 'inaccessible' comment, which indicates
279 // which panel instances can not be opened from this panel.
280 // 	Format = '// inaccessible: %d %d...'
281 // where the numbers printed are the panel instance numbers.
282 //
cfgParseInaccessibleComment(const char * comment,const char * filename,int lineno)283 boolean PanelAccessManager::cfgParseInaccessibleComment(const char *comment,
284                                 const char *filename, int lineno)
285 {
286     char gname[64], name[32];
287 
288     if (strncmp(comment," inaccessible",STRLEN(" inaccessible")))
289 	return FALSE;
290 
291     char* p = (char *) strchr(comment,':');
292     if (!p)
293 	return FALSE;
294 
295     p++;
296 
297     sscanf(comment, "%*s %s", name);
298     if (EqualString(name, "panels:"))
299     {
300       SkipWhiteSpace(p);
301       while (*p && *p != '\n') {
302 	int index = 0;
303 	if (!IsInteger(p, index)) {
304 	    ErrorMessage("Non-integer panel instance number (file %s, line %d)",
305 					filename, lineno);
306 	    return TRUE;
307 	}
308 	int instance = atoi(p);
309 	if (instance < 0) {
310 	    ErrorMessage("Negative panel instance number (file %s, line %d)",
311 					filename, lineno);
312 	    return TRUE;
313 	}
314 	instance++;		// instances are 1 based internally
315 	this->addInaccessiblePanel(instance);
316   	p = &p[index];
317         SkipWhiteSpace(p);
318       }
319       this->activate();
320     }
321     else if (EqualString(name, "groups:"))
322     {
323         p = strchr(p, '"');
324         if (p) {
325 	  do {
326 	    p++;
327 	    if (!*p) {
328 		ErrorMessage("Panel group name format error (file %s, line %d)",
329 						filename, lineno);
330 		return TRUE;
331 	    }
332 	    int i;
333 	    for (i=0 ; *p && *p != '"'; p++, i++)
334 		gname[i] = *p;
335 	    if (!*p) {
336 	        ErrorMessage("Panel group name format error (file %s, line %d)",
337 					    filename, lineno);
338     		return TRUE;
339 	    }
340 	    gname[i] = '\0';
341 	    this->addInaccessibleGroup(gname);
342 	    // Move to first quote of next group name
343 	    p++;
344 	    if (*p)
345 		p = strchr(p, '"');
346 	    else
347 		p = NULL;
348 	  } while (p);
349 	}
350         this->activate();
351     }
352     else
353     {
354 	return FALSE;
355     }
356 
357     return TRUE;
358 
359 }
360 
361