2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: BaseObj.C /main/4 1998/08/03 16:30:23 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
36 const char *ACTION_NOT_FOUND = "ActionNotFound";
38 BaseObj::BaseObj(BaseObj *parent,
45 _init_children = false;
46 _init_attributes = false;
53 _lastActionName = NULL;
54 _lastActionOutput = NULL;
55 _lastActionStatus = 0;
60 p->NotifyCreate(this);
72 BaseObj *parent = _parent;
75 parent->NotifyDelete(this);
76 parent = parent->_parent;
79 for (i = 0; i < _numAttributes; i++)
81 delete _attributes[i]->ReferenceName;
82 delete _attributes[i]->Value;
83 delete _attributes[i]->DisplayName;
84 delete _attributes[i]->DisplayValue;
85 delete _attributes[i]->DefaultValue;
86 delete _attributes[i]->DisplayDefaultValue;
87 delete _attributes[i]->Help;
88 delete _attributes[i]->ContextualHelp;
89 delete _attributes[i]->Listing;
90 delete _attributes[i]->Dependancies;
91 delete _attributes[i];
94 for (i = 0; i < _numActions; i++)
96 delete _actions[i]->ReferenceName;
97 delete _actions[i]->DisplayName;
98 delete _actions[i]->Nmemonic;
99 delete _actions[i]->AcceleratorText;
100 delete _actions[i]->Accelerator;
101 delete _actions[i]->Help;
102 delete _actions[i]->ContextualHelp;
103 delete _actions[i]->Dependancies;
108 free(_lastActionName);
109 delete [] _lastActionOutput;
115 char *BaseObj::DisplayName()
121 _displayName = STRDUP(_name);
126 void BaseObj::ReadAttributes()
128 if (_init_attributes == false)
130 LoadAttributes(_numAttributes, _attributes);
131 _init_attributes = true;
135 char *BaseObj::Details()
141 _details = STRDUP("");
146 void BaseObj::UpdateDetails()
153 void BaseObj::UpdateChildren()
157 _init_children = true;
160 BaseObj **BaseObj::Children()
162 if (_init_children == false)
165 _init_children = true;
170 int BaseObj::NumChildren()
172 if (_init_children == false)
175 _init_children = true;
180 void BaseObj::DeleteAction(const char *name)
184 if (!HasAction(name, &action))
187 Action **new_actions;
191 new_actions = new Action*[_numActions - 1];
192 for (i = 0; i < _numActions; i++)
193 if (_actions[i] != action)
194 new_actions[index++] = _actions[i];
195 delete action->ReferenceName;
196 delete action->DisplayName;
197 delete action->Nmemonic;
198 delete action->AcceleratorText;
199 delete action->Accelerator;
201 delete action->ContextualHelp;
202 delete action->Dependancies;
206 _actions = new_actions;
211 void BaseObj::DeleteAttribute(const char *name)
213 Attribute *attribute;
215 if (!HasAttribute(name, &attribute))
218 Attribute **new_attributes;
222 new_attributes = new Attribute*[_numAttributes - 1];
223 for (i = 0; i < _numAttributes; i++)
224 if (_attributes[i] != attribute)
225 new_attributes[index++] = _attributes[i];
226 delete attribute->ReferenceName;
227 delete attribute->Value;
228 delete attribute->DisplayName;
229 delete attribute->DisplayValue;
230 delete attribute->DefaultValue;
231 delete attribute->DisplayDefaultValue;
232 delete attribute->Help;
233 delete attribute->ContextualHelp;
234 delete attribute->Listing;
235 delete attribute->Dependancies;
238 delete []_attributes;
239 _attributes = new_attributes;
244 void BaseObj::AddAttribute(const char *ReferenceName,
245 const char *DisplayName,
247 const char *ContextualHelp,
248 Characteristics Mask,
249 ValueList ValueListType,
251 const char *Dependancies,
252 const char *DefaultValue,
253 const char *DisplayDefaultValue)
255 Attribute **new_attributes;
258 new_attributes = new Attribute*[_numAttributes + 1];
259 for (i = 0; i < _numAttributes; i++)
260 new_attributes[i] = _attributes[i];
262 delete []_attributes;
263 _attributes = new_attributes;
264 Attribute *attribute = new Attribute;
265 attribute->ReferenceName = STRDUP(ReferenceName);
266 attribute->Value = NULL;
267 attribute->DisplayName = STRDUP(DisplayName);
268 attribute->DisplayValue = NULL;
269 attribute->DefaultValue = STRDUP(DefaultValue);
270 attribute->DisplayDefaultValue = STRDUP(DisplayDefaultValue);
271 attribute->Mask = Mask;
272 attribute->Help = STRDUP(Help);
273 attribute->ContextualHelp = STRDUP(ContextualHelp);
274 attribute->Listing = STRDUP(Listing);
275 attribute->ValueListType = ValueListType;
276 attribute->Dependancies = STRDUP(Dependancies);
277 _attributes[_numAttributes] = attribute;
282 void BaseObj::AddAction(ActionHandler Handler,
283 const char *ReferenceName,
284 const char *DisplayName,
285 const char *Nmemonic,
287 const char *ContextualHelp,
288 boolean InputRequired,
289 const char *AcceleratorText,
290 const char *Accelerator,
291 const char *Dependancies)
293 Action **new_actions;
296 new_actions = new Action*[_numActions + 1];
297 for (i = 0; i < _numActions; i++)
298 new_actions[i] = _actions[i];
301 _actions = new_actions;
302 Action *action = new Action;
303 action->Handler = Handler;
304 action->ReferenceName = STRDUP(ReferenceName);
305 action->DisplayName = STRDUP(DisplayName);
306 action->Nmemonic = STRDUP(Nmemonic);
307 action->AcceleratorText = STRDUP(AcceleratorText);
308 action->Accelerator = STRDUP(Accelerator);
309 action->InputRequired = InputRequired;
310 action->Help = STRDUP(Help);
311 action->ContextualHelp = STRDUP(ContextualHelp);
312 action->Dependancies = STRDUP(Dependancies);
313 _actions[_numActions] = action;
318 void BaseObj::AddToParent()
323 BaseObj **new_children;
326 new_children = new BaseObj*[_parent->_numChildren + 1];
327 for (i = 0; i < _parent->_numChildren; i++)
328 new_children[i] = _parent->_children[i];
330 delete []_parent->_children;
331 _parent->_children = new_children;
332 _parent->_children[_parent->_numChildren] = this;
334 _parent->_numChildren++;
337 void BaseObj::DeleteFromParent()
342 BaseObj **new_children;
346 new_children = new BaseObj*[_parent->_numChildren - 1];
347 for (i = 0; i < _parent->_numChildren; i++)
348 if (_parent->_children[i] != this)
349 new_children[index++] = _parent->_children[i];
351 delete []_parent->_children;
352 _parent->_children = new_children;
354 _parent->_numChildren--;
357 int BaseObj::NumSiblings()
360 return _parent->_numChildren;
365 BaseObj ** BaseObj::Siblings()
368 return _parent->_children;
373 boolean BaseObj::HasAction(const char *actionName,
378 for (i = 0; i < _numActions; i++)
379 if (!strcmp(_actions[i]->ReferenceName, actionName))
381 *action = _actions[i];
387 boolean BaseObj::HasAction(Action *action)
392 return HasAction(action->ReferenceName, &dummy);
397 boolean BaseObj::HasAction(const char *actionName)
401 return HasAction(actionName, &dummy);
404 boolean BaseObj::SendAction(Action *action,
409 delete [] _lastActionOutput;
410 free(_lastActionName);
411 _lastActionOutput = NULL;
412 _lastActionName = strdup(action->ReferenceName);
413 if ((status = HasAction(action)) == true)
414 _lastActionStatus = (*action->Handler)(this, &_lastActionOutput,
418 _lastActionStatus = -1;
419 int len = strlen("'%s' is not an action of %s") +
420 strlen(ObjectClassName()) + strlen(action->ReferenceName) + 1;
421 _lastActionOutput = new char [len];
422 sprintf(_lastActionOutput, "'%s' is not an action of %s",
423 ObjectClassName(), action->ReferenceName);
428 boolean BaseObj::SendAction(const char *actionName,
433 (void) HasAction(actionName, &action);
434 return SendAction(action, requestor);
437 char * BaseObj::AttributeValue(char *referenceName)
442 if (HasAttribute(referenceName, &dummy))
448 boolean BaseObj::HasAttribute(const char *attributeName,
449 Attribute **attribute)
453 for (i = 0; i < _numAttributes; i++)
454 if (!strcmp(_attributes[i]->ReferenceName, attributeName))
456 *attribute = _attributes[i];
462 boolean BaseObj::HasAttribute(Attribute *attribute)
466 return HasAttribute(attribute->ReferenceName, &dummy);
469 boolean BaseObj::HasAttribute(const char *attributeName)
473 return HasAttribute(attributeName, &dummy);
476 void BaseObj::DeleteChildren()
480 BaseObj **kids = new BaseObj*[_numChildren];
481 int i, n = _numChildren;
482 // Save children first before deleting a child since the destructor
483 // updates the _children variable
484 for (i = 0; i < n; i++)
485 kids[i] = _children[i];
486 for (i = 0; i < n; i++)
493 void BaseObj::Dump(boolean verbose, int level)
497 for (i = 0; i < level; i++) printf(" ");
498 printf("%s : %s\n", Name(), ObjectClassName());
501 for (i = -1; i <= level; i++) printf(" ");
502 printf("Display Name = '%s'\n", _displayName);
503 for (i = -1; i <= level; i++) printf(" ");
504 printf("Details = '%s'\n", _details);
505 for (i = -1; i <= level; i++) printf(" ");
506 printf("Number Actions = %d\n", _numActions);
507 for (j = 0; j < _numActions; j++)
509 for (i = -2; i <= level; i++) printf(" ");
510 printf("Action.%s = '%s'\n", _actions[j]->ReferenceName,
511 _actions[j]->DisplayName);
513 for (i = -1; i <= level; i++) printf(" ");
514 printf("Number Attributes = %d\n", _numAttributes);
515 for (j = 0; j < _numAttributes; j++)
517 for (i = -2; i <= level; i++) printf(" ");
518 printf("Attribute.%s = '%s'\n", _attributes[j]->ReferenceName,
519 _attributes[j]->DisplayValue);
521 for (i = -1; i <= level; i++) printf(" ");
522 printf("Number Children = %d\n", _numChildren);
526 // Dump object hierarchy
527 void BaseObj::DumpHierarchy(boolean verbose, int level)
531 Dump(verbose, level);
532 for (i = 0; i < _numChildren; i++)
533 _children[i]->DumpHierarchy(verbose, level + 1);
536 void BaseObj::Error(const char *message)
539 printf("%s: (%s) %s\n", ObjectClassName(), Name(), message);
542 int BaseObj::RunCommand(const char *command,
548 Invoke *_thread = new Invoke(command, std_out, std_err);
550 status = _thread->status;