-fpermissive to allow GCC to compile old C++
[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 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: 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 }
86
87 DmxMailbox::~DmxMailbox (void)
88 {
89     delete(_fileName);
90     if (_mbox)
91       delete (_mbox);
92 }
93
94 void
95 DmxMailbox::loadMessages (void)
96 {
97         DtMailEnv       env;
98         DtMailBoolean   moreMessages = DTM_TRUE;
99         DtMail::Session *session = theRoamApp.session()->session();
100
101         // try to construct _mbox
102         _mbox = session->mailBoxConstruct (
103                                 env,
104                                 DtMailFileObject,
105                                 _fileName,
106                                 NULL,
107                                 NULL,
108                                 NULL);
109
110         if (handleError (env, "new DtMail::MailBox") == DTM_TRUE)
111                 exit (1);
112         
113         // open the mailbox
114         // (O_RDONLY for now)
115         _mbox->open (env,       /* DtMailEnv */
116                     DTM_FALSE,  /* auto_create */
117                     O_RDONLY,   /* open(2) flag */ 
118                     (mode_t) 0, /* create(2) mode */
119                     DTM_FALSE,  /* lock_flag */
120                     DTM_TRUE    /* auto_parse */
121           );
122
123         if (handleError (env, "open mailbox") == DTM_TRUE)
124                 exit (1);
125
126         // get the first message handle
127         DtMailMessageHandle     first = NULL, next = NULL, prev = NULL;
128         DtMailHeaderRequest     request;
129         DtMailHeaderLine        hdrline;
130         int                     i = _firstIndex;
131         DtMail::Message         *m = NULL;      // temporary
132
133         createHeaderRequest (request);
134
135         first = _mbox->getFirstMessageSummary (env, request, hdrline);
136
137         if (handleError (env, "get first msg summary") == DTM_TRUE)
138                 exit (1);
139
140         if (first == NULL)
141         {
142                 fprintf (stderr,
143                         "loadMessages: error w/1st message...exiting.\n");
144                 exit (1);
145         }
146
147         // this makes hash of the caching strategy, but oh well....
148         m = _mbox->getMessage (env, first);
149         if (handleError (env, "get first msg") == DTM_TRUE)
150                 exit (1);
151         _messages [i].setMessage (m);
152         
153         prev = first;
154
155         _messages [i].setHandle (first);
156         _messages [i].setHeader (hdrline);
157
158         while (moreMessages == DTM_TRUE)
159         {
160                 next = _mbox->getNextMessageSummary (env, prev,
161                                                 request, hdrline);
162
163                 if (next == NULL)
164                 {
165                         moreMessages = DTM_FALSE;
166                         if (handleError (env, "msgLoop") == DTM_TRUE)
167                                 exit (1);
168                         break; // break out if error
169                 } else {
170                         i++;    
171                         _messages [i].setHandle (next);
172                         _messages [i].setHeader (hdrline);
173                 }
174                 
175                 prev = next;
176         }       
177
178         _messageCount = i;
179
180
181         // fill in the remaining message structures .. temporary, honest!
182         for (int j = _firstIndex; j <= _messageCount; j++)
183         {
184                 m = _mbox->getMessage (env, _messages [j].msgHandle);
185                 if (handleError (env, "getMessage loop") == DTM_TRUE)
186                 {
187                         printf ("fatal error...bailing...\n");
188                         exit (1);
189                 }       
190                 _messages [j].setMessage (m);
191
192                 char    tmp [100];
193                 sprintf (tmp, "%d", j);         // ick
194                 _messages [j].setInfo (tmp);
195         }
196
197         // free header request structure
198         int k;
199         // this assumes that you have allocated all of the possible headers
200         // in the DmxHeaders -- a bad assumption, but currently true
201         for (k = 0; k < DMXNUMHDRS; k++)
202         {
203                 free (request.header_name [k]);
204         }
205         delete request.header_name;
206         
207         return;
208 }
209
210 void
211 DmxMailbox::createHeaderRequest (DtMailHeaderRequest &request)
212 {
213         // set up default headers
214         request.number_of_names = DMXNUMHDRS;
215         request.header_name = new (char* [DMXNUMHDRS]);
216         request.header_name[DMXCC] = strdup(DtMailMessageCc);
217         request.header_name[DMXFROM] = strdup(DtMailMessageSender);
218         request.header_name[DMXSUBJ] = strdup(DtMailMessageSubject);
219         request.header_name[DMXDATE] = strdup(DtMailMessageReceivedTime);
220         request.header_name[DMXCLENGTH] = strdup(DtMailMessageContentLength);
221         request.header_name[DMXSTATUS] = strdup(DtMailMessageStatus);
222         request.header_name[DMXTO] = strdup(DtMailMessageTo);
223         request.header_name[DMXV3CHARSET] = strdup(DtMailMessageV3charset);
224         request.header_name[DMXCONTENTTYPE] = strdup(DtMailMessageContentType);
225
226         return;
227 }
228
229 void
230 DmxMailbox::printMailboxInfo (void)
231 {
232         printf ("\"%s\": %d messages\n", _fileName, _messageCount);
233         return;
234 }