dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtfile / ToolTalk.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 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: ToolTalk.c /main/6 1999/09/16 13:41:16 mgreess $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           ToolTalk.c
28  *
29  *   COMPONENT_NAME: Desktop File Manager (dtfile)
30  *
31  *   Description:    Contains routines to handle tooltalk messages.
32  *
33  *   FUNCTIONS: FileCallback
34  *              FinalizeToolTalkSession
35  *              InitializeToolTalkProcid
36  *              InitializeToolTalkSession
37  *              MediaCallback
38  *              SessionCallback
39  *              ToolTalkError
40  *
41  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
42  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
43  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
44  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
45  *
46  ****************************************************************************
47  ************************************<+>*************************************/
48
49 #include <Xm/XmP.h>
50 #include <Xm/DrawingA.h>
51 #include <Xm/DrawingAP.h>
52 #include <Xm/MessageB.h>
53 #include <Xm/RowColumn.h>
54 #include <Xm/MwmUtil.h>
55
56 #include <Dt/Icon.h>
57 #include <Dt/IconP.h>
58 #include <Dt/IconFile.h>
59
60 #include <Dt/HelpDialog.h>
61
62 #ifdef SHAPE
63 #include <X11/extensions/shape.h>
64 #endif
65
66 #include <X11/Shell.h>
67 #include <X11/Xatom.h>
68 #include <Xm/Protocols.h>
69
70 #include <Dt/Session.h>
71 #include <Dt/DtP.h>
72 #include <Dt/Connect.h>
73 #include <Dt/FileM.h>
74 #include <Dt/Indicator.h>
75 #include <Dt/Lock.h>
76 #include <Dt/UserMsg.h>
77 #include <Dt/Wsm.h>
78 #include <Dt/WsmP.h>
79 #include <Dt/DtNlUtils.h>
80 #include <Dt/CommandM.h>
81 #include <Dt/EnvControlP.h>
82
83 #include <Tt/tttk.h>
84
85 #include "Encaps.h"
86 #include "SharedProcs.h"
87 #include "FileMgr.h"
88 #include "Desktop.h"
89 #include "Main.h"
90
91 /* ToolTalk messages */
92 #define FM_TT_MSG_QUIT        "Quit"
93 #define FM_TT_MSG_PAUSE       "Pause"
94 #define FM_TT_MSG_RESUME      "Resume"
95 #define FM_TT_MSG_GET_STAT    "Get_Status"
96 #define FM_TT_MSG_DO_CMD      "Do_Command"
97
98 #define VENDOR                "CDE"
99 #define VERSION               "1.0"
100
101 static char* RESTRICTED_HEADER = "-restricted";
102
103 static Tt_pattern * FileManagerToolTalkPattern = NULL;
104 static Boolean sendStopped = False;
105 XtInputId ProcessToolTalkInputId = 0;
106
107
108 Tt_message
109 FileCallback(
110         Tt_message msg,
111         Tttk_op op,
112         char * file,
113         void * clientdata,
114         int trust,
115         int self)
116 {
117   View *view = clientdata;
118   char local_host[MAXHOSTNAMELEN];
119   extern char home_host_name[];
120
121   switch (op) {
122           char *to_file;
123           DialogData *dialog_data;
124           int i;
125       default:
126           break;
127       case TTDT_MOVED:
128           if (view == 0) {
129                   break;
130           }
131           to_file = tt_message_arg_val(msg, 1);
132           strcpy(local_host, home_host_name);
133           dialog_data = (DialogData *)view->dialog_data;
134           ShowNewDirectory( (FileMgrData *)dialog_data->data,
135                             local_host, to_file);
136
137           /* Quit the old file, join the new */
138           ttdt_file_quit( view->pats, 0 );
139           view->pats = ttdt_file_join( to_file, TT_SESSION,
140                                        0, FileCallback, view );
141           if (tt_is_err( tt_ptr_error( view->pats ))) {
142               view->pats = 0;
143           }
144           tt_free( to_file );
145           tttk_message_destroy( msg );
146           msg = 0;
147   }
148   tt_free( file );
149   return msg;
150 }
151
152
153 void
154 FinalizeToolTalkSession(void)
155 {
156   Tt_status ttRc;
157   int i;
158
159   for (i = 0; i < view_count; i++)
160   {
161       if (view_set[i]->msg != 0) {
162          if (view_set[i]->pats != 0) {
163             ttdt_file_quit( view_set[i]->pats, 0 );
164             view_set[i]->pats = 0;
165          }
166          tt_message_reply( view_set[i]->msg );
167          tttk_message_destroy( view_set[i]->msg );
168          view_set[i]->msg = 0;
169       }
170   }
171
172   if( FileManagerToolTalkPattern &&
173       tt_ptr_error( FileManagerToolTalkPattern ) == TT_OK )
174   {
175     ttRc = ttdt_session_quit( NULL,
176                               FileManagerToolTalkPattern,
177                               1 );
178     if( ProcessToolTalkInputId )
179       XtRemoveInput( ProcessToolTalkInputId );
180   }
181   ttRc = ttdt_close( NULL, NULL, sendStopped );
182 }
183
184 void
185 ToolTalkError( char * procName, Tt_status errId )
186 {
187   if( tt_is_err( errId ) )
188   {
189     switch( errId )
190     {
191       case TT_ERR_NOMP:
192          XtRemoveInput( ProcessToolTalkInputId );
193          break;
194       default:
195          break;
196     }
197   }
198 }
199
200 Tt_message
201 SessionCallback( Tt_message msg, void * client_data, Tt_message contract )
202 {
203   View *view = client_data;
204   char *opString = tt_message_op( msg );
205   Tttk_op op = tttk_string_op( opString );
206   tt_free( opString );
207
208   switch (op) {
209           int i;
210       default:
211           break;
212       case TTDT_QUIT:
213           if (contract == 0) {
214                   tt_message_reply( msg );
215                   FinalizeToolTalkSession( );
216                   exit( 0 );
217           }
218           /*
219            * Instead of quitting the whole process, we are to just
220            * quit the view associated with contract.
221            */
222           if (view == 0) {
223                   tttk_message_fail( msg, TT_DESKTOP_EINVAL, 0, 1 );
224                   return 0;
225           }
226           CloseView( (DialogData *)view->dialog_data );
227           tt_message_reply( msg );
228           tttk_message_destroy( msg );
229           return 0;
230   }
231   return msg;
232 }
233
234 Tt_message
235 MediaCallback( Tt_message msg,
236                void * client_data,
237                Tttk_op op,
238                Tt_status diag,
239                unsigned char * contents,
240                int len,
241                char * file,
242                char * docname )
243 {
244         char *ceiling;
245         Boolean restricted = False;
246
247         tt_free( docname );
248         if (diag != TT_OK) {
249                 return msg;
250         }
251         if (file == 0) {
252                 if (contents == 0) {
253                         tttk_message_fail( msg, TT_DESKTOP_ENODATA, 0, 1 );
254                         return 0;
255                 } else {
256                         /* Buffers and files are the same to dtfile */
257                         /* ToolTalk buffers are always null-terminated */
258                         tt_free( file );
259                         file = (char *)contents;
260                 }
261         }
262         /* Old libtt SEGVs if you ask for a context that is not there */
263         if (tt_message_contexts_count( msg ) > 0) {
264                 ceiling = tt_message_context_val( msg, "restricted" );
265                 if (! tt_is_err( tt_ptr_error( ceiling ))) {
266                         if ((ceiling == 0) || (strcmp( ceiling, "." ) != 0)) {
267                                 tttk_message_fail( msg,
268                                                    TT_DESKTOP_ENOTSUP, 0, 1 );
269                                 return 0;
270                         }
271                         restricted = True;
272                 }
273         }
274         switch ( op ) {
275             default:
276                 break;
277             case TTME_DISPLAY:
278             case TTME_EDIT:
279                 /*
280                  * Create a display of the given directory tree
281                  */
282                 ViewDirectoryProc( file, restricted, msg );
283                 msg = 0;
284                 break;
285         }
286         tt_free( file );
287         tt_free( (caddr_t)contents );
288         return msg;
289 }
290
291 Tt_status
292 InitializeToolTalkProcid( int *ttFd, Widget topLevel, Boolean sendStarted )
293 {
294   char * procId;
295   Tt_status ttstat;
296
297 #ifdef DT_PERFORMANCE
298    printf("  InitializeToolTalkProcid\n");
299    gettimeofday(&update_time_s, NULL);
300 #endif
301   sendStopped = sendStarted;
302   procId = ttdt_open( ttFd,
303                       DTFILE_CLASS_NAME,
304                       VENDOR,
305                       VERSION,
306                       sendStarted );
307
308 #ifdef DT_PERFORMANCE
309    gettimeofday(&update_time_f, NULL);
310    if (update_time_s.tv_usec > update_time_f.tv_usec) {
311       update_time_f.tv_usec += 1000000;
312       update_time_f.tv_sec--;
313    }
314    printf("    done InitializeToolTalkProcid, time: %ld.%ld\n\n", update_time_f.tv_sec - update_time_s.tv_sec, update_time_f.tv_usec - update_time_s .tv_usec);
315 #endif
316
317   if( (ttstat = tt_ptr_error( procId )) == TT_OK )
318   {
319     ProcessToolTalkInputId =
320             XtAppAddInput( XtWidgetToApplicationContext( toplevel ),
321                            *ttFd, (XtPointer)XtInputReadMask,
322                            tttk_Xt_input_handler, procId );
323     return( TT_OK );
324   }
325   else
326   {
327     ttdt_close( NULL, NULL, sendStopped );
328     return( ttstat );
329   }
330 }
331
332
333 int
334 InitializeToolTalkSession( Widget topLevel, int ttFd )
335 {
336   Tt_status ttstat;
337
338   ttstat = ttmedia_ptype_declare( "DT_File_Manager",
339                                   0,
340                                   MediaCallback,
341                                   (void *) 0,
342                                   0);
343   if (ttstat == TT_OK)
344     ttstat = ttmedia_ptype_declare( "DT_File_Manager",
345                                     1000,
346                                     MediaCallback,
347                                     (void *) 0,
348                                     1);
349   tt_ptype_opnum_callback_add( "DT_File_Manager", 0, HandleTtRequest );
350   if (ttstat == TT_OK)
351   {
352     /*
353      * If we were started by a message, the following call to
354      * tttk_Xt_input_handler will process it.  Otherwise,
355      * tttk_Xt_input_handler will just return.
356      */
357     tttk_Xt_input_handler( NULL, 0, 0 );
358
359     FileManagerToolTalkPattern = ttdt_session_join( tt_default_session( ),
360                                                     SessionCallback,
361                                                     topLevel,
362                                                     NULL,
363                                                     1 );
364     if( tt_is_err( tt_ptr_error( FileManagerToolTalkPattern )))
365     {
366       ttdt_close( NULL, NULL, sendStopped );
367       return( 0 );
368     }
369   }
370   else
371   {
372     ttdt_close( NULL, NULL, sendStopped );
373     return( 0 );
374   }
375   return( TT_OK );
376 }