dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtmail / dtmail / DmxMailbox.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 /* $XConsortium: DmxMailbox.C /main/3 1996/04/21 19:55:45 drk $ */
24
25 /*
26  *+SNOTICE
27  *
28  *      $:$
29  *
30  *      RESTRICTED CONFIDENTIAL INFORMATION:
31  *      
32  *      The information in this document is subject to special
33  *      restrictions in a confidential disclosure agreement between
34  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
35  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
36  *      Sun's specific written approval.  This document and all copies
37  *      and derivative works thereof must be returned or destroyed at
38  *      Sun's request.
39  *
40  *      Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
41  *
42  *+ENOTICE
43  */
44 /*
45  *                   Common Desktop Environment
46  *
47  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
48  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
49  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
50  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
51  *   (c) Copyright 1995 Digital Equipment Corp.
52  *   (c) Copyright 1995 Fujitsu Limited
53  *   (c) Copyright 1995 Hitachi, Ltd.
54  *                                                                   
55  *
56  *                     RESTRICTED RIGHTS LEGEND                              
57  *
58  *Use, duplication, or disclosure by the U.S. Government is subject to
59  *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
60  *Technical Data and Computer Software clause in DFARS 252.227-7013.  Rights
61  *for non-DOD U.S. Government Departments and Agencies are as set forth in
62  *FAR 52.227-19(c)(1,2).
63
64  *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
65  *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A. 
66  *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
67  *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
68  *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
69  *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
70  *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
71  */
72
73 #include <stdio.h>
74 #include "Dmx.h"
75 #include "MemUtils.hh"
76 #include "RoamApp.h"
77
78 const int       DmxMailbox::_firstIndex = 1;
79
80 DmxMailbox::DmxMailbox (char *filename)
81 {
82         _mbox = NULL;
83         _messageCount = 0;
84         _fileName = strdup_n(filename);
85         _message = NULL;
86 }
87
88 DmxMailbox::~DmxMailbox (void)
89 {
90     delete(_fileName);
91     if (_mbox)
92       delete (_mbox);
93 }
94
95 void
96 DmxMailbox::loadMessages (void)
97 {
98         DtMailEnv       env;
99         DtMailBoolean   moreMessages = DTM_TRUE;
100         DtMail::Session *session = theRoamApp.session()->session();
101
102         // try to construct _mbox
103         _mbox = session->mailBoxConstruct (
104                                 env,
105                                 DtMailFileObject,
106                                 _fileName,
107                                 NULL,
108                                 NULL,
109                                 NULL);
110
111         if (handleError (env, "new DtMail::MailBox") == DTM_TRUE)
112                 exit (1);
113         
114         // open the mailbox
115         // (O_RDONLY for now)
116         _mbox->open (env,       /* DtMailEnv */
117                     DTM_FALSE,  /* auto_create */
118                     O_RDONLY,   /* open(2) flag */ 
119                     (mode_t) 0, /* create(2) mode */
120                     DTM_FALSE,  /* lock_flag */
121                     DTM_TRUE    /* auto_parse */
122           );
123
124         if (handleError (env, "open mailbox") == DTM_TRUE)
125                 exit (1);
126
127         // get the first message handle
128         DtMailMessageHandle     first = NULL, next = NULL, prev = NULL;
129         DtMailHeaderRequest     request;
130         DtMailHeaderLine        hdrline;
131         int                     i = _firstIndex;
132         DtMail::Message         *m = NULL;      // temporary
133
134         createHeaderRequest (request);
135
136         first = _mbox->getFirstMessageSummary (env, request, hdrline);
137
138         if (handleError (env, "get first msg summary") == DTM_TRUE)
139                 exit (1);
140
141         if (first == NULL)
142         {
143                 fprintf (stderr,
144                         "loadMessages: error w/1st message...exiting.\n");
145                 exit (1);
146         }
147
148         // this makes hash of the caching strategy, but oh well....
149         m = _mbox->getMessage (env, first);
150         if (handleError (env, "get first msg") == DTM_TRUE)
151                 exit (1);
152         _messages [i].setMessage (m);
153         
154         prev = first;
155
156         _messages [i].setHandle (first);
157         _messages [i].setHeader (hdrline);
158
159         while (moreMessages == DTM_TRUE)
160         {
161                 next = _mbox->getNextMessageSummary (env, prev,
162                                                 request, hdrline);
163
164                 if (next == NULL)
165                 {
166                         moreMessages = DTM_FALSE;
167                         if (handleError (env, "msgLoop") == DTM_TRUE)
168                                 exit (1);
169                         break; // break out if error
170                 } else {
171                         i++;    
172                         _messages [i].setHandle (next);
173                         _messages [i].setHeader (hdrline);
174                 }
175                 
176                 prev = next;
177         }       
178
179         _messageCount = i;
180
181
182         // fill in the remaining message structures .. temporary, honest!
183         for (int j = _firstIndex; j <= _messageCount; j++)
184         {
185                 m = _mbox->getMessage (env, _messages [j].msgHandle);
186                 if (handleError (env, "getMessage loop") == DTM_TRUE)
187                 {
188                         printf ("fatal error...bailing...\n");
189                         exit (1);
190                 }       
191                 _messages [j].setMessage (m);
192
193                 char    tmp [100];
194                 sprintf (tmp, "%d", j);         // ick
195                 _messages [j].setInfo (tmp);
196         }
197
198         // free header request structure
199         int k;
200         // this assumes that you have allocated all of the possible headers
201         // in the DmxHeaders -- a bad assumption, but currently true
202         for (k = 0; k < DMXNUMHDRS; k++)
203         {
204                 free (request.header_name [k]);
205         }
206         delete request.header_name;
207         
208         return;
209 }
210
211 void
212 DmxMailbox::createHeaderRequest (DtMailHeaderRequest &request)
213 {
214         // set up default headers
215         request.number_of_names = DMXNUMHDRS;
216         request.header_name = new (char* [DMXNUMHDRS]);
217         request.header_name[DMXCC] = strdup(DtMailMessageCc);
218         request.header_name[DMXFROM] = strdup(DtMailMessageSender);
219         request.header_name[DMXSUBJ] = strdup(DtMailMessageSubject);
220         request.header_name[DMXDATE] = strdup(DtMailMessageReceivedTime);
221         request.header_name[DMXCLENGTH] = strdup(DtMailMessageContentLength);
222         request.header_name[DMXSTATUS] = strdup(DtMailMessageStatus);
223         request.header_name[DMXTO] = strdup(DtMailMessageTo);
224         request.header_name[DMXV3CHARSET] = strdup(DtMailMessageV3charset);
225         request.header_name[DMXCONTENTTYPE] = strdup(DtMailMessageContentType);
226
227         return;
228 }
229
230 void
231 DmxMailbox::printMailboxInfo (void)
232 {
233         printf ("\"%s\": %d messages\n", _fileName, _messageCount);
234         return;
235 }