Fix logic in dtprintinfo
[oweals/cde.git] / cde / programs / dtprintinfo / objects / BaseObj.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: BaseObj.h /main/5 1998/04/06 13:34:28 mgreess $ */
24 /*                                                                      *
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.                                *
29  */
30
31 #ifndef BASEOBJ_H
32 #define BASEOBJ_H
33
34 #include <stdio.h>
35 #include "Invoke.h"
36
37 #ifndef _BOOLEAN_
38 #define _BOOLEAN_
39 #if (defined(sun) && OSMAJORVERSION <= 5 && OSMINORVERSION <= 3)|| defined(USL) || defined(__uxp__)
40   #include <sys/types.h>
41   #define boolean boolean_t
42   #define true B_TRUE
43   #define false B_FALSE
44 #elif defined(linux)
45   #define false 0
46   #define true 1
47   #define boolean int
48 #elif defined(CSRG_BASED)
49 #include <stdbool.h>
50 #define boolean bool
51 #else
52   typedef enum
53   {
54      false = 0,
55      true = 1
56   } boolean;
57 #endif
58 #endif
59
60
61 #ifndef STRDUP
62 #define STRDUP(string) (string ? strdup(string) : NULL)
63 #define STRCMP(s1, s2) (s1 && s2 ? strcmp(s1, s2) : (s1 ? 1 : -1))
64 #define STRLEN(string) (string ? strlen(string) : 0)
65 #endif
66
67 typedef enum
68 {
69    STRING,
70    MULT_LINE_STRING,
71    INTEGER,
72    REAL,
73    FILE_NAME,
74    DIRECTORY_NAME,
75    HOST_NAME,
76    USER_NAME,
77    GROUP_NAME,
78    DATE,
79    TIME,
80    MONEY,
81    HOUR,
82    MINUTE,
83    SECOND,
84    DAY,
85    MONTH,
86    YEAR
87 } AttributeType;
88
89 typedef enum
90 {
91    OPTIONAL = 0,
92    ALLOW_DIRECT_ENTRY = 1,
93    REQUIRED = 2,
94    EDITABLE_AFTER_CREATE = 4, 
95    EDITABLE_DURING_CREATE = 8, 
96    ECHO_INPUT = 16
97 } Characteristics;
98
99 typedef enum
100 {
101    INFORMATION_LINE,
102    NO_LIST,
103    SINGLE_SELECT_LIST,
104    MULTI_SELECT_LIST,
105    RANGE,
106    MULTI_SELECT_MIN_RANGE,
107    MULTI_SELECT_MAX_RANGE,
108    MULTI_SELECT_RANGE
109 } ValueList;
110
111 #define AllowDirectEntry(attr) (attr->Mask & ALLOW_DIRECT_ENTRY)
112 #define IsRequired(attr) (attr->Mask & REQUIRED)
113 #define EditableAfterCreate(attr) (attr->Mask & EDITABLE_AFTER_CREATE)
114 #define EditableDuringCreate(attr) (attr->Mask & EDITABLE_DURING_CREATE)
115 #define EchoInput(attr) (attr->Mask & ECHO_INPUT)
116
117 typedef struct
118 {
119    char *ReferenceName;
120    AttributeType Type;
121    Characteristics Mask;
122    char *DisplayName;
123    char *Value;
124    char *DisplayValue;
125    char *DefaultValue;
126    char *DisplayDefaultValue;
127    char *Help;
128    char *ContextualHelp;
129    ValueList ValueListType;
130    char *Listing;
131    char *Dependancies;
132    // The base class initializes the next 4 variables
133    int  n_values;
134    char **CompiledValueList;
135    char **CompiledDisplayValueList;
136    char **CompiledHelpValueList;
137 } Attribute;
138
139 class BaseObj;
140
141 typedef int (*ActionHandler) (BaseObj *, char **output, BaseObj *requestor);
142
143 typedef struct
144 {
145    ActionHandler Handler;
146    char *ReferenceName;
147    char *DisplayName;
148    char *Nmemonic;
149    char *Help;
150    char *ContextualHelp;
151    char *AcceleratorText;
152    char *Accelerator;
153    boolean InputRequired;
154    char *Dependancies;
155 } Action;
156
157 extern const char *ACTION_NOT_FOUND;
158
159 class BaseObj {
160
161  protected:
162    char *_name;
163    char *_displayName;
164    char *_details;
165    BaseObj *_parent;
166    BaseObj **_children;
167    int _numChildren;
168    Attribute **_attributes;
169    int _numAttributes;
170    Action **_actions;
171    int _numActions;
172    boolean _init_children;
173    boolean _init_attributes;
174    char *_lastActionName;
175    char *_lastActionOutput;
176    int _lastActionStatus;
177
178    BaseObj(BaseObj *parent,
179           const char *name);
180
181    void AddToParent();
182    void DeleteFromParent();
183    void DeleteAttribute(const char *ReferenceName);
184    void AddAttribute(const char *ReferenceName,
185                      const char *DisplayName = NULL,
186                      const char *Help = NULL,
187                      const char *ContextualHelp = NULL,
188                      Characteristics Mask = OPTIONAL,
189                      ValueList ValueListType = NO_LIST,
190                      const char *Listing = NULL,
191                      const char *Dependancies = NULL,
192                      const char *DefaultValue = NULL,
193                      const char *DisplayDefaultValue = NULL);
194    void DeleteAction(const char *ReferenceName);
195    void AddAction(ActionHandler Handler,
196                   const char *ReferenceName,
197                   const char *DisplayName = NULL,
198                   const char *Nmemonic = NULL,
199                   const char *Help = NULL,
200                   const char *ContextualHelp = NULL,
201                   boolean InputRequired = false,
202                   const char *AcceleratorText = NULL,
203                   const char *Accelerator = NULL,
204                   const char *Dependancies = NULL);
205
206    // Derived classes should redefine these functions
207    virtual void InitChildren() { _children = NULL; _numChildren = 0; }
208    virtual void InitDetails() { }
209    virtual void LoadAttributes(int /*n_attrs*/, Attribute ** /*attrs*/) { }
210    virtual void InitDisplayName() { }
211
212    // These messages are sent to all parents
213    virtual void NotifyCreate(BaseObj *) { } ;
214    virtual void NotifyDelete(BaseObj *) { } ;
215
216  public:
217
218    virtual ~BaseObj();             // destructor
219
220    boolean HasAttribute(Attribute *action);
221    boolean HasAttribute(const char *ReferenceName);
222    boolean HasAttribute(const char *ReferenceName, Attribute **action);
223    boolean HasAction(Action *action);
224    boolean HasAction(const char *ReferenceName);
225    boolean HasAction(const char *ReferenceName, Action **action);
226    boolean SendAction(Action *action,
227                       BaseObj *requestor = NULL);
228    boolean SendAction(const char *ReferenceName, 
229                       BaseObj *requestor = NULL);
230
231    char * LastActionName()        { return _lastActionName; }
232    char * LastActionOutput()      { return _lastActionOutput; }
233    int LastActionStatus()         { return _lastActionStatus; }
234
235    const char * Name()            { return _name; }
236    char * DisplayName();
237    char * Details();
238    char * AttributeValue(char *ReferenceName);
239    BaseObj * Parent()             { return _parent; }
240    const int NumActions()         { return _numActions; }
241    const int NumAttributes()      { return _numAttributes; }
242    Action **Actions()             { return _actions; }
243    Attribute **Attributes()       { return _attributes; }
244    BaseObj ** Children();
245    int NumChildren();
246    void ReadAttributes();
247    void UpdateChildren();
248    void UpdateDetails();
249    void DeleteChildren();
250    void SetInitChildren()         { _init_children = true; }
251
252    int RunCommand(const char *command,
253                   char **std_out = NULL,
254                   char **std_err = NULL);
255
256    // These are for children
257    BaseObj ** Siblings();
258    int NumSiblings();
259
260    // Log Error message
261    void Error(const char *message);
262
263    // Dumps object to stdout
264    void Dump(boolean verbose = false,
265              int level = 0);
266    // Dumps object heirarchy to stdout
267    void DumpHierarchy(boolean verbose = false,
268                       int level = 0);
269
270    virtual const char *const ObjectClassName() { return "BaseObj"; }
271
272 };
273
274 #endif // BASEOBJ_H