Merge branch 'cde-fixups-1' of ssh://git.code.sf.net/p/cdesktopenv/code into cde...
[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 libraries 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(sun)
45   #include <sys/types.h>
46   #define boolean boolean_t
47 #if defined(__XOPEN_OR_POSIX)
48   #define true _B_TRUE
49   #define false _B_FALSE
50 #else
51   #define true B_TRUE
52   #define false B_FALSE
53 #endif
54 #elif defined(linux)
55   #define false 0
56   #define true 1
57   #define boolean int
58 #elif defined(CSRG_BASED)
59 #include <stdbool.h>
60 #define boolean bool
61 #else
62   typedef enum
63   {
64      false = 0,
65      true = 1
66   } boolean;
67 #endif
68 #endif
69
70
71 #ifndef STRDUP
72 #define STRDUP(string) (string ? strdup(string) : NULL)
73 #define STRCMP(s1, s2) (s1 && s2 ? strcmp(s1, s2) : (s1 ? 1 : -1))
74 #define STRLEN(string) (string ? strlen(string) : 0)
75 #endif
76
77 typedef enum
78 {
79    STRING,
80    MULT_LINE_STRING,
81    INTEGER,
82    REAL,
83    FILE_NAME,
84    DIRECTORY_NAME,
85    HOST_NAME,
86    USER_NAME,
87    GROUP_NAME,
88    DATE,
89    TIME,
90    MONEY,
91    HOUR,
92    MINUTE,
93    SECOND,
94    DAY,
95    MONTH,
96    YEAR
97 } AttributeType;
98
99 typedef enum
100 {
101    OPTIONAL = 0,
102    ALLOW_DIRECT_ENTRY = 1,
103    REQUIRED = 2,
104    EDITABLE_AFTER_CREATE = 4, 
105    EDITABLE_DURING_CREATE = 8, 
106    ECHO_INPUT = 16
107 } Characteristics;
108
109 typedef enum
110 {
111    INFORMATION_LINE,
112    NO_LIST,
113    SINGLE_SELECT_LIST,
114    MULTI_SELECT_LIST,
115    RANGE,
116    MULTI_SELECT_MIN_RANGE,
117    MULTI_SELECT_MAX_RANGE,
118    MULTI_SELECT_RANGE
119 } ValueList;
120
121 #define AllowDirectEntry(attr) (attr->Mask & ALLOW_DIRECT_ENTRY)
122 #define IsRequired(attr) (attr->Mask & REQUIRED)
123 #define EditableAfterCreate(attr) (attr->Mask & EDITABLE_AFTER_CREATE)
124 #define EditableDuringCreate(attr) (attr->Mask & EDITABLE_DURING_CREATE)
125 #define EchoInput(attr) (attr->Mask & ECHO_INPUT)
126
127 typedef struct
128 {
129    char *ReferenceName;
130    AttributeType Type;
131    Characteristics Mask;
132    char *DisplayName;
133    char *Value;
134    char *DisplayValue;
135    char *DefaultValue;
136    char *DisplayDefaultValue;
137    char *Help;
138    char *ContextualHelp;
139    ValueList ValueListType;
140    char *Listing;
141    char *Dependancies;
142    // The base class initializes the next 4 variables
143    int  n_values;
144    char **CompiledValueList;
145    char **CompiledDisplayValueList;
146    char **CompiledHelpValueList;
147 } Attribute;
148
149 class BaseObj;
150
151 typedef int (*ActionHandler) (BaseObj *, char **output, BaseObj *requestor);
152
153 typedef struct
154 {
155    ActionHandler Handler;
156    char *ReferenceName;
157    char *DisplayName;
158    char *Nmemonic;
159    char *Help;
160    char *ContextualHelp;
161    char *AcceleratorText;
162    char *Accelerator;
163    boolean InputRequired;
164    char *Dependancies;
165 } Action;
166
167 extern const char *ACTION_NOT_FOUND;
168
169 class BaseObj {
170
171  protected:
172    char *_name;
173    char *_displayName;
174    char *_details;
175    BaseObj *_parent;
176    BaseObj **_children;
177    int _numChildren;
178    Attribute **_attributes;
179    int _numAttributes;
180    Action **_actions;
181    int _numActions;
182    boolean _init_children;
183    boolean _init_attributes;
184    char *_lastActionName;
185    char *_lastActionOutput;
186    int _lastActionStatus;
187
188    BaseObj(BaseObj *parent,
189           const char *name);
190
191    void AddToParent();
192    void DeleteFromParent();
193    void DeleteAttribute(const char *ReferenceName);
194    void AddAttribute(const char *ReferenceName,
195                      const char *DisplayName = NULL,
196                      const char *Help = NULL,
197                      const char *ContextualHelp = NULL,
198                      Characteristics Mask = OPTIONAL,
199                      ValueList ValueListType = NO_LIST,
200                      const char *Listing = NULL,
201                      const char *Dependancies = NULL,
202                      const char *DefaultValue = NULL,
203                      const char *DisplayDefaultValue = NULL);
204    void DeleteAction(const char *ReferenceName);
205    void AddAction(ActionHandler Handler,
206                   const char *ReferenceName,
207                   const char *DisplayName = NULL,
208                   const char *Nmemonic = NULL,
209                   const char *Help = NULL,
210                   const char *ContextualHelp = NULL,
211                   boolean InputRequired = false,
212                   const char *AcceleratorText = NULL,
213                   const char *Accelerator = NULL,
214                   const char *Dependancies = NULL);
215
216    // Derived classes should redefine these functions
217    virtual void InitChildren() { _children = NULL; _numChildren = 0; }
218    virtual void InitDetails() { }
219    virtual void LoadAttributes(int /*n_attrs*/, Attribute ** /*attrs*/) { }
220    virtual void InitDisplayName() { }
221
222    // These messages are sent to all parents
223    virtual void NotifyCreate(BaseObj *) { } ;
224    virtual void NotifyDelete(BaseObj *) { } ;
225
226  public:
227
228    virtual ~BaseObj();             // destructor
229
230    boolean HasAttribute(Attribute *action);
231    boolean HasAttribute(const char *ReferenceName);
232    boolean HasAttribute(const char *ReferenceName, Attribute **action);
233    boolean HasAction(Action *action);
234    boolean HasAction(const char *ReferenceName);
235    boolean HasAction(const char *ReferenceName, Action **action);
236    boolean SendAction(Action *action,
237                       BaseObj *requestor = NULL);
238    boolean SendAction(const char *ReferenceName, 
239                       BaseObj *requestor = NULL);
240
241    char * LastActionName()        { return _lastActionName; }
242    char * LastActionOutput()      { return _lastActionOutput; }
243    int LastActionStatus()         { return _lastActionStatus; }
244
245    const char * Name()            { return _name; }
246    char * DisplayName();
247    char * Details();
248    char * AttributeValue(char *ReferenceName);
249    BaseObj * Parent()             { return _parent; }
250    const int NumActions()         { return _numActions; }
251    const int NumAttributes()      { return _numAttributes; }
252    Action **Actions()             { return _actions; }
253    Attribute **Attributes()       { return _attributes; }
254    BaseObj ** Children();
255    int NumChildren();
256    void ReadAttributes();
257    void UpdateChildren();
258    void UpdateDetails();
259    void DeleteChildren();
260    void SetInitChildren()         { _init_children = true; }
261
262    int RunCommand(const char *command,
263                   char **std_out = NULL,
264                   char **std_err = NULL);
265
266    // These are for children
267    BaseObj ** Siblings();
268    int NumSiblings();
269
270    // Log Error message
271    void Error(const char *message);
272
273    // Dumps object to stdout
274    void Dump(boolean verbose = false,
275              int level = 0);
276    // Dumps object heirarchy to stdout
277    void DumpHierarchy(boolean verbose = false,
278                       int level = 0);
279
280    virtual const char *const ObjectClassName() { return "BaseObj"; }
281
282 };
283
284 #endif // BASEOBJ_H