4 * $TOG: MailSession.C /main/9 1999/03/26 16:51:51 mgreess $
6 * RESTRICTED CONFIDENTIAL INFORMATION:
8 * The information in this document is subject to special
9 * restrictions in a confidential disclosure agreement between
10 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
11 * document outside HP, IBM, Sun, USL, SCO, or Univel without
12 * Sun's specific written approval. This document and all copies
13 * and derivative works thereof must be returned or destroyed at
16 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
23 #include <sys/types.h>
25 #include <X11/Intrinsic.h>
26 #include <DtMail/DtMail.hh>
27 #include <DtMail/DtMailError.hh>
28 #include <DtMail/DtMailXtProc.h>
29 #include <DtMail/IO.hh>
31 #include "MailSession.hh"
32 #include "MemUtils.hh"
33 #include "RoamMenuWindow.h"
35 DtMail::Session * MailSession::_app_session = NULL;
36 int MailSession::_num_deactivated = 0;
38 MailSession::MailSession(DtMailEnv & error, XtAppContext context)
39 : _open_mailboxes(128)
44 error.setError(DTME_ObjectInUse);
48 _app_session = new DtMail::Session(error, "dtmail");
52 if ((DTMailError_t)error == DTME_NoMsgCat) error.clear();
54 // Else need to translate the error into an DTME_* error
55 // defined in DtMailError.hh
58 XtAppAddInput(context,
59 _app_session->eventFileDesc(error),
60 (XtPointer)XtInputReadMask,
66 // Used to be both XtInputReadMask and XtInputExceptMask
67 // went to the same procedure. Resulted in problems in
68 // when using R6 polling instead of R5 select. Now
69 // use separate input handlers.
71 XtAppAddInput(context,
72 _app_session->eventFileDesc(error),
73 (XtPointer)XtInputExceptMask,
78 DtMailDamageContext = context;
81 MailSession::~MailSession(void)
83 for (int mb = 0; mb < _open_mailboxes.length(); mb++)
85 delete _open_mailboxes[mb]->handle;
86 delete _open_mailboxes[mb]->path;
93 MailSession::isMboxOpen(const char *path)
95 int pos = locate(path);
96 if (pos >= 0) return (TRUE);
101 MailSession::getRMW(const char *path)
103 int slot = locate(path);
105 return(_open_mailboxes[slot]->rmw);
110 MailSession::getRMW(DtMail::MailBox *mbox)
112 int slot = locate(mbox);
113 return(_open_mailboxes[slot]->rmw);
120 DtMailCallback cb_func,
122 DtMailBoolean auto_create,
123 DtMailBoolean request_lock,
124 DtMailBoolean auto_parse)
126 // First, see if we have this open in this process.
127 int slot = locate(path);
129 // We do not allow open an already opened mailbox
130 // because this may cause memory fault when a message is
131 // deleted from one RMW and the other RMW would like to
132 // read the already deleted message.
133 // (1) set error to DTME_AlreadyOpened
137 error.setError(DTME_AlreadyOpened);
138 if (client_data == NULL)
140 _open_mailboxes[slot]->open_ref_count += 1;
141 return _open_mailboxes[slot]->handle;
145 // Create a handle for determining what to do next. This will
146 // add us to the file session so we will start getting call
147 // back requests on this file.
148 DtMail::MailBox * mailbox = _app_session->mailBoxConstruct(
149 error, DtMailFileObject,
151 cb_func, client_data);
154 error.setError(DTME_ObjectCreationFailed);
159 // Does this file exist? If it doesn't and create is
160 // on then make one. If create is off, then raise an
164 DTMAIL_DEFAULT_OPEN_MODE,
165 DTMAIL_DEFAULT_CREATE_MODE,
169 // Need to translate from BE error to a FE error that the
170 // user can understand. Opening a mail container can result
171 // in a variety of errors. Translate those that are considered
172 // to be relevant (for now) - the set can always be extended.
173 // Toss others into an UnknownFormat error.
180 addToList(mailbox, path, (RoamMenuWindow *) client_data);
185 MailSession::close(DtMailEnv & error, const DtMail::MailBox * mb)
187 // Find the mail box slot.
188 int slot = locate(mb);
191 error.setError(DTME_ObjectInvalid);
195 MailBoxItem * mbi = _open_mailboxes[slot];
196 mbi->open_ref_count -= 1;
197 if (mbi->open_ref_count <= 0)
199 if (! _open_mailboxes[slot]->is_active) _num_deactivated--;
204 _open_mailboxes.remove(slot);
210 MailSession::convert(
212 const char *, // old_path
213 const char *, // new_path
214 DtMailStatusCallback, // cb_func
215 void *) // client_data
223 DtMail::Message &, // msg
224 const char *) // path
230 MailSession::locate(const char *path)
235 SafeStat(path, &pathbuf);
236 for (slot = 0; slot < _open_mailboxes.length(); slot++)
238 if (strcmp(_open_mailboxes[slot]->path, path) == 0)
241 if (! _open_mailboxes[slot]->stated)
243 _open_mailboxes[slot]->stated = TRUE;
245 _open_mailboxes[slot]->path,
246 &_open_mailboxes[slot]->statbuf);
249 if (pathbuf.st_ino == _open_mailboxes[slot]->statbuf.st_ino &&
250 pathbuf.st_dev == _open_mailboxes[slot]->statbuf.st_dev)
258 MailSession::locate(const DtMail::MailBox *mb)
260 for (int slot = 0; slot < _open_mailboxes.length(); slot++)
261 if (mb == _open_mailboxes[slot]->handle)
268 MailSession::locate(const RoamMenuWindow *rmw)
270 for (int slot = 0; slot < _open_mailboxes.length(); slot++)
271 if (rmw == _open_mailboxes[slot]->rmw)
278 MailSession::isActiveRMW(RoamMenuWindow *rmw)
280 int slot = locate((const RoamMenuWindow *) rmw);
281 if (slot < 0) return 0;
283 return _open_mailboxes[slot]->is_active;
287 MailSession::activateRMW(RoamMenuWindow *rmw)
289 int slot = locate((const RoamMenuWindow *) rmw);
290 if (slot < 0) return;
292 if (! _open_mailboxes[slot]->is_active)
294 DtMail::MailBox *mailbox = rmw->mailbox();
295 mailbox->enableMailRetrieval();
297 DtMailEnv mail_error;
299 rmw->checkForMail(mail_error);
301 _open_mailboxes[slot]->is_active = TRUE;
307 MailSession::deactivateRMW(RoamMenuWindow *rmw)
309 int slot = locate((const RoamMenuWindow *) rmw);
310 if (slot < 0) return;
312 if (_open_mailboxes[slot]->is_active)
314 DtMail::MailBox *mailbox = rmw->mailbox();
315 mailbox->disableMailRetrieval();
317 _open_mailboxes[slot]->is_active = FALSE;
323 MailSession::addToList(
328 MailBoxItem * mbi = new MailBoxItem;
331 mbi->is_active = TRUE;
333 mbi->open_ref_count = 1;
334 mbi->path = strdup_n(path);
337 _open_mailboxes.append(mbi);
341 MailSession::lockCB(Tt_message, Tttk_op, const char*, uid_t, int, void*)
343 // We never give up the lock during a copy!
344 return TT_ERR_INVALID;