Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtsearchpath / dtsp / IconSearchPath.C
1 /* $XConsortium: IconSearchPath.C /main/3 1995/11/03 12:31:13 rswiston $ */
2 /*******************************************************************
3 **  (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
4 **  All rights are reserved.  Copying or other reproduction of this
5 **  program except for archival purposes is prohibited without prior
6 **  written consent of Hewlett-Packard Company.
7 ********************************************************************
8 ****************************<+>*************************************/
9
10 #include "SearchPath.h"
11 #include <stdlib.h>
12 #include "TTFile.h"
13 #include "Options.h"
14
15 extern Options * options;
16
17 /**********************************************************************
18  *
19  * IconSearchPath
20  *
21  *      This constructor creates DTICONSEARCHPATH in a three-step 
22  *      process.
23  *
24  *      1. gathers environment variables and defaults to create a 
25  *         colon-separated list of paths
26  *      2. normalizes the list into host:/path format
27  *      3. builds the final version of the path
28  *
29  *      Hierarchy of search paths:
30  *              DTSPUSERICON
31  *              User's home directory
32  *              System Administrator's configuration directory
33  *              DTSPSYSICON
34  *              Factory location
35  *
36  **********************************************************************/
37 IconSearchPath::IconSearchPath
38         (
39         CDEEnvironment * user,
40         const char *     envvar,
41         const char *     ist,
42         const char *     znd,
43         const char *     sep
44         ) : SearchPath(user,envvar,sep),
45             first(ist),
46             second(znd)
47 {
48     if (user->DTICONSP()) {
49         if (user->DTUSERICONSP()) {
50             search_path = *user->DTUSERICONSP() + ",";
51             if (user->DTUSERAPPSP())
52                 search_path += *user->DTUSERAPPSP() + ",";
53             search_path += user->HOME() + ",";
54             if (!user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
55                 !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
56                 search_path += user->SysAdmConfig() + ",";
57             search_path += *user->DTICONSP() + ",";
58             if (user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
59                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
60                 search_path.replace(user->OS()->LocalHost() + ":,","");
61             if (user->DTAPPSP())
62                 search_path += *user->DTAPPSP() + ",";
63             search_path += user->SysAdmConfig() + "," +
64                            user->FactoryInstall();
65         }
66         else {
67             if (user->DTUSERAPPSP())
68                 search_path = *user->DTUSERAPPSP() + ",";
69             search_path += user->HOME() + ",";
70             if (!user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
71                 !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
72                 search_path += user->SysAdmConfig() + ",";
73             search_path += *user->DTICONSP() + ",";
74             if (user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
75                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
76                 search_path.replace(user->OS()->LocalHost() + ":,","");
77             if (user->DTAPPSP())
78                 search_path += *user->DTAPPSP() + ",";
79             search_path += user->SysAdmConfig() + "," +
80                            user->FactoryInstall();
81         }
82     }
83     else if (user->DTUSERICONSP()) {
84         search_path = *user->DTUSERICONSP() + ",";
85         if (user->DTUSERAPPSP())
86             search_path += *user->DTUSERAPPSP() + ",";
87         search_path += *user->DTAPPSP();
88     }
89     else {
90         if (user->DTUSERAPPSP())
91             search_path = *user->DTUSERAPPSP() + ",";
92         search_path += *user->DTAPPSP();
93     }
94
95     // NormalizePath should remove duplicates
96     AddPredefinedPath();
97
98     if (options->CheckingUser())
99         search_path = *user->DTAPPSP();
100
101     NormalizePath();
102     TraversePath();
103 }
104
105
106 /************************************************************************
107  *  MakeIconSearchPath()
108  *
109  *     Given a search path element (host name:path name pair), construct
110  *     the appropriate path for this particular desktop subsystem:
111  *
112  *     DTICONSEARCHPATH
113  *
114  *     A path is constructed so that each host:/path pair is appended.
115  ************************************************************************/
116 void IconSearchPath::MakePath
117         (
118         const CString & pair
119         ) 
120 {
121     CTokenizedString element(pair,":");
122     CString host_element = element.next();
123     CString path_element = element.next();
124
125     if (path_element.contains(user->HOME())) {
126         if (host_element == user->OS()->LocalHost()) {
127             if (user->OS()->isDirectory(path_element + "/icons")
128                  || options->dontOptimize()) {
129                 AddToPath (path_element + "/icons/%B%M" + first);
130                 AddToPath (path_element + "/icons/%B%M" + second);
131                 AddToPath (path_element + "/icons/%B");
132             }
133         }
134         else {
135             CString dir(ConstructPath(path_element + "/icons", &host_element));
136             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
137                 AddToPath (dir + "/%B%M" + first);
138                 AddToPath (dir + "/%B%M" + second);
139                 AddToPath (dir + "/%B");
140             }
141         }
142     }
143     else {
144         if (host_element == user->OS()->LocalHost()) {
145             CString dirname(path_element);
146             if (validSearchPath(dirname)) {
147                 dirname += "/icons";
148                 if (user->OS()->isDirectory (dirname)
149                      || options->dontOptimize()) {
150                     AddToPath (dirname + "/%L/%B%M" + first);
151                     AddToPath (dirname + "/%L/%B%M" + second);
152                     AddToPath (dirname + "/%L/%B");
153                 }
154                 dirname += "/C";
155             }
156             if (user->OS()->isDirectory (dirname) || options->dontOptimize()) {
157                 AddToPath (dirname + "/%B%M" + first);
158                 AddToPath (dirname + "/%B%M" + second);
159                 AddToPath (dirname + "/%B");
160             }           
161         }
162         else {
163             CString dir = ConstructPath(path_element, &host_element);
164             if (validSearchPath(dir)) {
165                 dir += "/icons";
166                 if (user->OS()->isDirectory (dir) || options->dontOptimize()) {
167                     AddToPath (dir + "/%L/%B%M" + first);
168                     AddToPath (dir + "/%L/%B%M" + second);
169                     AddToPath (dir + "/%L/%B");
170                 }
171                 dir += "/C";
172             }
173             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
174                 AddToPath (dir + "/%B%M" + first);
175                 AddToPath (dir + "/%B%M" + second);
176                 AddToPath (dir + "/%B");
177             }
178         }
179     }
180 }
181
182 /*********************************************************************
183  *  validSearchPath()
184  *
185  *    This member function verifies that the path in question is a
186  *    standard CDE location, i.e. /etc/dt/appconfig or /usr/dt/appconfig
187  *    so that the appropriate massaging can take place.
188  *
189  *********************************************************************/
190 int IconSearchPath::validSearchPath
191         (
192         const CString & st
193         ) const
194 {
195     if (st == user->SysAdmConfig())     // ...,/etc/dt/appconfig,...
196         return 1;
197
198     if (st == user->FactoryInstall())   // ...,/usr/dt/appconfig,...
199         return 1;
200
201     // ...,/nfs/machine/etc/dt/appconfig,...
202
203     if (st.contains(user->SysAdmConfig(),"",Separator().data()))
204         return 1;
205
206     // ...,/nfs/machine/usr/dt/appconfig,...
207
208     if (st.contains(user->FactoryInstall(),"",Separator().data()))
209         return 1;
210
211     // If this is an APP-specified path, it also needs to be massaged.
212     // The elements in the APP paths were originally specified using 
213     // host:/path, but were converted to /path for Icon searchpaths
214     // so use the comma as the leader and the trailer.
215
216     if (user->DTAPPSP() && 
217         user->DTAPPSP()->contains (st, ",", ","))
218         return 1;
219
220     if (user->DTUSERAPPSP() && 
221         user->DTUSERAPPSP()->contains (st, ",", ","))
222         return 1;
223
224     return 0;
225 }