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