2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
26 * $TOG: MailSession.C /main/9 1999/03/26 16:51:51 mgreess $
28 * RESTRICTED CONFIDENTIAL INFORMATION:
30 * The information in this document is subject to special
31 * restrictions in a confidential disclosure agreement between
32 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
33 * document outside HP, IBM, Sun, USL, SCO, or Univel without
34 * Sun's specific written approval. This document and all copies
35 * and derivative works thereof must be returned or destroyed at
38 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
45 #include <sys/types.h>
47 #include <X11/Intrinsic.h>
48 #include <DtMail/DtMail.hh>
49 #include <DtMail/DtMailError.hh>
50 #include <DtMail/DtMailXtProc.h>
51 #include <DtMail/IO.hh>
53 #include "MailSession.hh"
54 #include "MemUtils.hh"
55 #include "RoamMenuWindow.h"
57 DtMail::Session * MailSession::_app_session = NULL;
58 int MailSession::_num_deactivated = 0;
60 MailSession::MailSession(DtMailEnv & error, XtAppContext context)
61 : _open_mailboxes(128)
66 error.setError(DTME_ObjectInUse);
70 _app_session = new DtMail::Session(error, "dtmail");
74 if ((DTMailError_t)error == DTME_NoMsgCat) error.clear();
76 // Else need to translate the error into an DTME_* error
77 // defined in DtMailError.hh
80 XtAppAddInput(context,
81 _app_session->eventFileDesc(error),
82 (XtPointer)XtInputReadMask,
88 // Used to be both XtInputReadMask and XtInputExceptMask
89 // went to the same procedure. Resulted in problems in
90 // when using R6 polling instead of R5 select. Now
91 // use separate input handlers.
93 XtAppAddInput(context,
94 _app_session->eventFileDesc(error),
95 (XtPointer)XtInputExceptMask,
100 DtMailDamageContext = context;
103 MailSession::~MailSession(void)
105 for (int mb = 0; mb < _open_mailboxes.length(); mb++)
107 delete _open_mailboxes[mb]->handle;
108 delete _open_mailboxes[mb]->path;
115 MailSession::isMboxOpen(const char *path)
117 int pos = locate(path);
118 if (pos >= 0) return (TRUE);
123 MailSession::getRMW(const char *path)
125 int slot = locate(path);
127 return(_open_mailboxes[slot]->rmw);
132 MailSession::getRMW(DtMail::MailBox *mbox)
134 int slot = locate(mbox);
135 return(_open_mailboxes[slot]->rmw);
142 DtMailCallback cb_func,
144 DtMailBoolean auto_create,
145 DtMailBoolean request_lock,
146 DtMailBoolean auto_parse)
148 // First, see if we have this open in this process.
149 int slot = locate(path);
151 // We do not allow open an already opened mailbox
152 // because this may cause memory fault when a message is
153 // deleted from one RMW and the other RMW would like to
154 // read the already deleted message.
155 // (1) set error to DTME_AlreadyOpened
159 error.setError(DTME_AlreadyOpened);
160 if (client_data == NULL)
162 _open_mailboxes[slot]->open_ref_count += 1;
163 return _open_mailboxes[slot]->handle;
167 // Create a handle for determining what to do next. This will
168 // add us to the file session so we will start getting call
169 // back requests on this file.
170 DtMail::MailBox * mailbox = _app_session->mailBoxConstruct(
171 error, DtMailFileObject,
173 cb_func, client_data);
176 error.setError(DTME_ObjectCreationFailed);
181 // Does this file exist? If it doesn't and create is
182 // on then make one. If create is off, then raise an
186 DTMAIL_DEFAULT_OPEN_MODE,
187 DTMAIL_DEFAULT_CREATE_MODE,
191 // Need to translate from BE error to a FE error that the
192 // user can understand. Opening a mail container can result
193 // in a variety of errors. Translate those that are considered
194 // to be relevant (for now) - the set can always be extended.
195 // Toss others into an UnknownFormat error.
202 addToList(mailbox, path, (RoamMenuWindow *) client_data);
207 MailSession::close(DtMailEnv & error, const DtMail::MailBox * mb)
209 // Find the mail box slot.
210 int slot = locate(mb);
213 error.setError(DTME_ObjectInvalid);
217 MailBoxItem * mbi = _open_mailboxes[slot];
218 mbi->open_ref_count -= 1;
219 if (mbi->open_ref_count <= 0)
221 if (! _open_mailboxes[slot]->is_active) _num_deactivated--;
226 _open_mailboxes.remove(slot);
232 MailSession::convert(
234 const char *, // old_path
235 const char *, // new_path
236 DtMailStatusCallback, // cb_func
237 void *) // client_data
245 DtMail::Message &, // msg
246 const char *) // path
252 MailSession::locate(const char *path)
257 SafeStat(path, &pathbuf);
258 for (slot = 0; slot < _open_mailboxes.length(); slot++)
260 if (strcmp(_open_mailboxes[slot]->path, path) == 0)
263 if (! _open_mailboxes[slot]->stated)
265 _open_mailboxes[slot]->stated = TRUE;
267 _open_mailboxes[slot]->path,
268 &_open_mailboxes[slot]->statbuf);
271 if (pathbuf.st_ino == _open_mailboxes[slot]->statbuf.st_ino &&
272 pathbuf.st_dev == _open_mailboxes[slot]->statbuf.st_dev)
280 MailSession::locate(const DtMail::MailBox *mb)
282 for (int slot = 0; slot < _open_mailboxes.length(); slot++)
283 if (mb == _open_mailboxes[slot]->handle)
290 MailSession::locate(const RoamMenuWindow *rmw)
292 for (int slot = 0; slot < _open_mailboxes.length(); slot++)
293 if (rmw == _open_mailboxes[slot]->rmw)
300 MailSession::isActiveRMW(RoamMenuWindow *rmw)
302 int slot = locate((const RoamMenuWindow *) rmw);
303 if (slot < 0) return 0;
305 return _open_mailboxes[slot]->is_active;
309 MailSession::activateRMW(RoamMenuWindow *rmw)
311 int slot = locate((const RoamMenuWindow *) rmw);
312 if (slot < 0) return;
314 if (! _open_mailboxes[slot]->is_active)
316 DtMail::MailBox *mailbox = rmw->mailbox();
317 mailbox->enableMailRetrieval();
319 DtMailEnv mail_error;
321 rmw->checkForMail(mail_error);
323 _open_mailboxes[slot]->is_active = TRUE;
329 MailSession::deactivateRMW(RoamMenuWindow *rmw)
331 int slot = locate((const RoamMenuWindow *) rmw);
332 if (slot < 0) return;
334 if (_open_mailboxes[slot]->is_active)
336 DtMail::MailBox *mailbox = rmw->mailbox();
337 mailbox->disableMailRetrieval();
339 _open_mailboxes[slot]->is_active = FALSE;
345 MailSession::addToList(
350 MailBoxItem * mbi = new MailBoxItem;
353 mbi->is_active = TRUE;
355 mbi->open_ref_count = 1;
356 mbi->path = strdup_n(path);
359 _open_mailboxes.append(mbi);
363 MailSession::lockCB(Tt_message, Tttk_op, const char*, uid_t, int, void*)
365 // We never give up the lock during a copy!
366 return TT_ERR_INVALID;