Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtimsstart / select.c
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 /* @(#)$XConsortium: select.c /main/6 1996/11/21 12:17:43 cde-hp $ */
24
25 #include        "xims.h"
26
27 static int      fill_ims_ent(/* list, top, last */);
28 static int      setup_local_selection(/* sel, list, conf_all */);
29 static int      setup_remote_selection(/* sel */);
30 static int      setup_user_selection(/* sel, list, idx */);
31
32
33 void    ximsSelect()
34 {
35     int         ret;
36     bool        use_win;
37     char        *def_ims;
38     ImsList     *list = (ImsList *) 0;
39     FileSel     *fsel = (FileSel *) 0;
40     UserSelection       *sel = &userSel;
41
42     OpState = State_Select;
43
44     /* clear_UserSelection(sel); */
45     sel->iconic = -1;
46
47     if (!localList) {
48         if ((ret = get_ims_list(&list, NULL, False)) != NoError) {
49             OpState = State_Select_Err;
50             OpErrCode = ret;
51             return;
52         }
53         localList = list;
54     } else
55         list = localList;
56
57     use_win = True;
58     def_ims = NULL;
59
60     if (Opt.SelectMode == SEL_MODE_GIVEN) {
61         sel->name = NEWSTR(Opt.ImsName);
62         use_win = False;
63     } else {
64
65         if (read_user_selection(&sel->fsel, NULL) == NoError) {
66             fsel = sel->fsel;
67 #ifdef  DEBUG
68             if (DebugLvl > 1)   pr_FileSel(sel->fsel);
69 #endif
70         }
71
72         if (list->default_idx >= 0)
73             def_ims = list->elist[list->default_idx]->name;
74
75         if ((OpFlag & FLAG_NOSAVE) || Opt.SelectMode == SEL_MODE_NOAUTO) {
76             use_win = True;
77         } else if (Opt.SelectMode == SEL_MODE_AUTO) {
78             use_win = False;
79 #ifdef  SelectMode_ONCE
80         } else if (Opt.SelectMode == SEL_MODE_ONCE) {
81             if (fsel->name)
82                 use_win = False;
83 #endif  /* SelectMode_ONCE */
84
85 #ifdef  AutoSelectionForSoleIMS
86         } else if (list->num_ent == 1) {
87             DPR(("ximsSelect(): only one entry in locale_conf\n"));
88             sel->name = NEWSTR(list->elist[0]->name);
89             use_win = False;
90 #endif  /* AutoSelectionForSoleIMS */
91
92         } else if (fsel) {
93             if (fsel->select_mode != SEL_MODE_NOAUTO
94                 && ((fsel->select_mode == SEL_MODE_AUTO ||
95                                         list->def_selmode == SEL_MODE_AUTO)
96                             && (def_ims || fsel->name))
97 #ifdef  SelectMode_ONCE
98                     || ((fsel->select_mode == SEL_MODE_ONCE
99                             || list->def_selmode == SEL_MODE_ONCE)
100                         && fsel->name)
101 #endif  /* SelectMode_ONCE */
102                     )
103                 use_win = False;
104         } else if (list->def_selmode == SEL_MODE_AUTO && def_ims) {
105             DPR(("ximsSelect(): def_selmode==AUTO in locale_conf\n"));
106             use_win = False;
107         }
108     }
109
110     if (sel->iconic == -1) {
111         sel->iconic = fsel ? fsel->iconic : 0;
112     } else if (sel->iconic != fsel->iconic) { /* this should never happen ! */
113         sel->flag |= F_SELECT_CHANGED;
114     }
115
116     if (fsel && fsel->select_mode == SEL_MODE_NONE) {
117         if (list->def_selmode != SEL_MODE_NONE) {
118             fsel->select_mode = list->def_selmode;
119             sel->flag |= F_SELECT_CHANGED;
120         }
121     }
122
123     if (!sel->name && fsel && fsel->name)
124         sel->name = NEWSTR(fsel->name);
125     else if (def_ims)
126         sel->name = NEWSTR(def_ims);
127
128     if (Opt.HostName)
129         sel->hostname = NEWSTR(Opt.HostName);
130     else if (fsel && fsel->hostname)
131         sel->hostname = NEWSTR(fsel->hostname);
132
133     if (sel->hostname)
134         sel->host_type = check_hostname(sel->hostname);
135     else
136         sel->host_type = HOST_LOCAL;
137
138     if (use_win == True) {
139         ret = setup_local_selection(sel, list, True);
140         ret = start_selection_window();
141     } else {
142
143         switch (sel->host_type) {
144             case HOST_REMOTE:
145                 ret = setup_remote_selection(sel);
146                 if (ret != NoError) {
147                     put_xims_errmsg(ret, 0, 0, 0);
148                 }
149                 break;
150
151             case HOST_LOCAL:
152                 ret = setup_local_selection(sel, list, False);
153                 break;
154
155             case HOST_UNKNOWN:
156                 ret = ErrUnknownHost;
157                 /* put_xims_errmsg(ret, sel->hostname, 0, 0); */
158                 break;
159         }
160     }
161
162     OpErrCode = ret;
163     OpState = ret == NoError ? State_Select_Done : State_Select_Err;
164
165     return;
166 }
167
168
169 static int      fill_ims_ent(list, top, last)
170     ImsList     *list;
171     int         top, last;
172 {
173     int         ret;
174     int         i;
175     ImsConf     *ims;
176     ImsEnt      *ent;
177
178     /* if (top < 0 || last < 0) return 0; */
179     ims = (ImsConf *) 0;
180     for (i = top; i <= last; i++) {     /* read IMS conf */
181         ret = NoError;
182         ent = list->elist[i];
183         if (!ent->ims) {
184             if (!ims)   ims = ALLOC(1, ImsConf);
185             ret = read_imsconf(ims, ent->name, ent->fname);
186             if (ret == NoError) {
187                 ent->ims = ims;
188                 ims = (ImsConf *) 0;
189             }
190         }
191         if (ret == NoError)
192             set_ims_status(ent);
193         else
194             ent->status = ret;
195     }
196     if (ims)    FREE(ims);
197
198     return last - top + 1;
199 }
200
201
202 static int      setup_local_selection(sel, list, conf_all)
203     UserSelection       *sel;
204     ImsList     *list;
205     int         conf_all;
206 {
207     int         ret;
208     int         idx, top, last;
209
210     idx = get_ims_idx(list, sel->name);
211     top = last = 0;
212     if (conf_all)       last = list->num_ent - 1;
213     else if (idx >= 0)  top = last = idx;
214     fill_ims_ent(list, top, last);
215
216     ret = setup_user_selection(sel, list, idx);
217
218     return ret;
219 }
220
221 static int      setup_remote_selection(sel)
222     UserSelection       *sel;
223 {
224     int         ret;
225     int         idx;
226     ImsList     *new_list;
227
228     ret = get_remote_conf(&new_list, sel->hostname, NULL, sel->name);
229
230     if (ret == NoError) {
231         idx = get_ims_idx(new_list, sel->name);
232         ret = setup_user_selection(sel, new_list, idx);
233     }
234
235     return ret;
236 }
237
238 static int      setup_user_selection(sel, list, idx)
239     UserSelection       *sel;
240     ImsList     *list;
241     int         idx;
242 {
243     if (sel->name) {
244         if (idx >= 0) {
245             sel->ent = list->elist[idx];
246             sel->status = sel->ent->status;
247         } else {
248             /* this ims isn't registered in locale_conf */
249             sel->status = ErrNotRegistered;
250         }
251     } else
252         sel->status = ErrNoSelection;
253     sel->ims_idx = idx;
254     sel->list = list;
255
256     return sel->status;
257 }
258
259 int     update_user_selection(sel, list, idx, host, host_type)
260     UserSelection       *sel;
261     ImsList     *list;
262     int         idx;
263     char        *host;
264     int         host_type;
265 {
266     ImsEnt      *ent;
267
268     if (idx < 0 || idx >= list->num_ent)        return False;
269     ent = list->elist[idx];
270
271     if (ent->status != NoError) return False;
272
273     if (strcmp(sel->name, ent->name)) {
274         RENEWSTR(sel->name, ent->name);
275     }
276     if ((host) && (!(sel->hostname) || strcmp(sel->hostname, host))) {
277         RENEWSTR(sel->hostname, host);
278     }
279     sel->host_type = host_type;
280
281     if (sel->list && sel->list != localList && sel->list != list) {
282         clear_ImsList(sel->list);
283         FREE(sel->list);
284     }
285     sel->list = list;
286     sel->ent = ent;
287     sel->status = ent->status;
288     sel->ims_idx = idx;
289     sel->flag |= F_SELECT_CHANGED;
290
291     return True;
292 }
293
294
295 int     get_ims_idx(list, name)
296     ImsList     *list;
297     char        *name;
298 {
299     int         idx;
300
301     if (name) {
302         for (idx = 0; idx < list->num_ent; idx++)
303             if (strcmp(name, list->elist[idx]->name) == 0)
304                 return idx;
305     }
306     return -1;
307 }
308
309 int     set_ims_status(ent)
310     ImsEnt      *ent;
311 {
312     int         ret = NoError;
313     ImsConf     *ims = ent->ims;
314
315     if (ent->name && strcmp(ent->name, NAME_NONE) == 0) {
316         ret = NoError;
317     } else if (!ims) {
318         ret = ErrNoImsConf;
319     } else if (ims->cmd_path && !(ims->flags & F_BUILTIN)
320                         && !is_executable(ims->cmd_path)) {
321         DPR(("set_ims_status(%s): executable '%s' not exist\n",
322                                         ent->name, ims->cmd_path));
323         ret = ErrNoExecutable;
324     }
325
326     ent->status = ret;
327     return ret;
328 }
329
330
331 int     get_ims_list(listp, locale, fill_ent)
332     ImsList     **listp;
333     char        *locale;
334     int         fill_ent;
335 {
336     int         ret = NoError;
337     ImsList     *list;
338
339     list = ALLOC(1, ImsList);
340
341     if ((ret = read_localeconf(list, locale)) != NoError) {
342         ret = ErrNoLocaleConf;
343     } else if (list->num_ent == 0) {
344         DPR(("get_ims_list(%s): no IMS in locale conf\n", locale));
345         ret = ErrNoImsEntry;
346     } else if (fill_ent) {
347         fill_ims_ent(list, 0, list->num_ent - 1);
348     }
349
350     if (ret == NoError) {
351         list->status = ret;
352     } else {
353         clear_ImsList(list);
354         FREE(list);
355         list = (ImsList *) 0;
356     }
357     *listp = list;
358
359     return ret;
360 }
361