//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/c5607871-120e-404c-9042-2603fb37af10/MAIL/CPP/SENDMAIL.CPP

//Warning: This code has been marked up for HTML

/***************************************************************************
$name: SENDMAIL.CPP
$version: 1.0 
$date_modified: 121498 
$description: Contains functions for sending mail messages.  NOTE: The
              following has Not been implemented:
              1. CC: target
              2. BC: Target
              3. Ability to specify more than one recipient
$owner: GroupWise SDK Team Lead
Copyright (c) 1998 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#include "resrc1.h"
#include "sendmail.h"
#include "abook.h"
#include "gwoapi.h"
#include "util.h"



//---- PROTOTYPE ----

BOOL WINAPI SendMailDlgProc(HWND  hwndDlg, UINT  uMsg, WPARAM  wParam, LPARAM  lParam);
void SendMailMessage(HWND hWndDlg);
void InitDialog(HWND hWnd);



//---- GLOBALS ----

extern HINSTANCE ghInst;      // from main.cpp

extern IGWSession* pIGWSession;
extern IGWAccount *pIGWAccount;



BSTR bsFrom = NULL;
HANDLE hWndAttach;
HIMAGELIST imageList;



/*-------------------------------------------
CreateSendailDialog 
 
 IN:   hWnd      - Handle to parent window
-------------------------------------------*/
void CreateSendMailDialog(HWND hWnd)
{
   DialogBox(ghInst, MAKEINTRESOURCE(IDD_MAIL), hWnd, (DLGPROC)SendMailDlgProc);
}



/*---------------------------------------------------------------------------------
SendMailDlgProc - The send mail dialog procedure

  IN:      hWnd   -   Handle to window
         uMsg   -   Message to watch for
         wParam   -   WPARAM
         lParam   -   LPARAM

  RETURN:   TRUE if message was handled, FALSE otherwise
---------------------------------------------------------------------------------*/
BOOL WINAPI SendMailDlgProc(HWND  hWnd, UINT  uMsg, WPARAM  wParam, LPARAM  lParam)
{
OPENFILENAME  ofn;
char szDirName[MAX_PATH];   // directory string

char szFile[260];         // filename string

char szFileTitle[260];      // file title string

LV_ITEM lvItem;



   switch(uMsg) {
      case WM_INITDIALOG:
         InitDialog(hWnd);
         return TRUE;

       case WM_COMMAND:
         switch(LOWORD(wParam)) {
            case IDCANCEL:
               EndDialog(hWnd, TRUE);
               break;

            case IDSEND:
               SendMailMessage(hWnd);
               EndDialog(hWnd, TRUE);
               break;

            case IDADDRESSBOOK: {
               Recip recip;
               HWND hCtl;

               if(ShowAddressBook(hWnd, &recip) == IDOK) {

                  hCtl = GetDlgItem(hWnd, IDC_TO);

                  SendMessage(hCtl, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)recip.DisplayName);
                  SendMessage(hCtl, CB_SETCURSEL, 0, 0);
               }

               return TRUE;
            }

            case IDATTACH:
               // Get windows system directory

               GetSystemDirectory(szDirName, sizeof(szDirName));
               szFile[0] = '\0';

               // Set ofn struct to 0

               memset(&ofn, 0, sizeof(OPENFILENAME));

               ofn.lStructSize = sizeof(OPENFILENAME); 
               ofn.hwndOwner = hWnd; 
               ofn.lpstrFile = szFile; 
               ofn.nMaxFile = sizeof(szFile); 
               ofn.lpstrFileTitle = szFileTitle; 
               ofn.nMaxFileTitle = sizeof(szFileTitle); 
               ofn.lpstrInitialDir = szDirName; 
               ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 

               // Bring up the OpenFileName Dialog

               if(GetOpenFileName(&ofn)) {
                  lvItem.mask = LVIF_TEXT | TVIF_IMAGE; 
                  lvItem.iItem = ListView_GetItemCount( (HWND) hWndAttach); 
                  lvItem.iSubItem = 0; 
                  lvItem.iImage = 0;
                  lvItem.pszText = szFile; 

                  // Insert item into the attachment list view on mail dialog

                  ListView_InsertItem( (HWND) hWndAttach, &lvItem);   
               }

               break;
         }
          break;

      default:
         return FALSE;
   } //switch

   return FALSE;
}



/*---------------------------------------------
SendMailMessage

   IN:      hWndDlg   - Handle to the dialog

  NOTE:      Sends the actual mail message
---------------------------------------------*/
void SendMailMessage(HWND hWndDlg)
{
DIGWFolder* pDIGWWorkFolder;
DIGWMessages* pDIGWMessages;
DIGWMessage* pDIGWMessage;
DIGWMessage* pDIGWRetMessage = NULL;
DIGWRecipients* pDIGWRecipients;
DIGWRecipient* pDIGWRecipient;
DIGWFormattedText* pDSubject;
DIGWFormattedText* pDBodyText;
DIGWAttachments* pDIGWAttachments;
DIGWAttachment* pDIGWAttachment;
IGWFolder* pIGWWorkFolder;
IGWMessages* pIGWMessages;
IGWMessage* pIGWMessage;
IGWRecipients* pIGWRecipients;
IGWRecipient* pIGWRecipient;
IGWFormattedText* pSubject;
IGWFormattedText* pBodyText;
IGWAttachments* pIGWAttachments;
VARIANT vClass, vType, vTargetType, vVersion;
TCHAR* szTo;
TCHAR* szSubject;
TCHAR* szMessage;
UINT len;
BSTR bstrClass = NULL;
BSTR bstrName = NULL;




   // Get the TO string

   len = GetWindowTextLength(GetDlgItem(hWndDlg, IDC_TO)) + 1;
   szTo = new TCHAR[len];
   GetDlgItemText(hWndDlg, IDC_TO, szTo, len);


   // Get the SUBJECT string

   len = GetWindowTextLength(GetDlgItem(hWndDlg, IDC_SUBJECT)) + 1;
   szSubject = new TCHAR[len];
   GetDlgItemText(hWndDlg, IDC_SUBJECT, szSubject, len);


   // Get the MESSAGE string

   len = GetWindowTextLength(GetDlgItem(hWndDlg, IDC_MESSAGE)) + 1;
   szMessage = new TCHAR[len];
   GetDlgItemText(hWndDlg, IDC_MESSAGE, szMessage, len);



   // Get the Work in progress folder

   pIGWAccount->get_WorkFolder(&pDIGWWorkFolder);

   // Get Interface for work in progress folder

   if(pDIGWWorkFolder && SUCCEEDED(pDIGWWorkFolder->QueryInterface(IID_IGWFolder, (LPVOID*)&pIGWWorkFolder))) {
      pDIGWWorkFolder->Release();

      pIGWWorkFolder->get_Messages(&pDIGWMessages);

      // Get messages Interface

      if(pDIGWMessages && SUCCEEDED(pDIGWMessages->QueryInterface(IID_IGWMessages, (LPVOID*)&pIGWMessages))) {
         pDIGWMessages->Release();

         // Specify the class of message we are sending

         bstrClass = SysAllocString(OLESTR("GW.MESSAGE.MAIL"));
         VariantInit(&vClass);
         VariantInit(&vType);
         VariantInit(&vVersion);

         // Set class and type to empty

         V_VT(&vClass) = VT_BSTR;
         V_VT(&vType) = VT_I4;
         V_VT(&vVersion) = VT_EMPTY;

         V_BSTR(&vClass) = bstrClass;
         V_I4(&vType) = egwDraft;

         // This is where we get the Message Object to work with

         pIGWMessages->Add(vClass, vType, vVersion, &pDIGWMessage);

         // Get Interface for Message Object

         if(pDIGWMessage && SUCCEEDED(pDIGWMessage->QueryInterface(IID_IGWMessage, (LPVOID*)&pIGWMessage))) {
            pDIGWMessage->Release();

            pIGWMessage->put_FromText(bsFrom);

            // SUBJECT 

            pIGWMessage->get_Subject(&pDSubject);

            // Get the Interface for SUBJECT Formatted text

            if(pDSubject && SUCCEEDED(pDSubject->QueryInterface(IID_IGWFormattedText, (LPVOID*)&pSubject)))
               pDSubject->Release();

               pSubject->put_PlainText(SysAllocString(TO_OLE_STRING(szSubject)));


            // BODY TEXT

            pIGWMessage->get_BodyText(&pDBodyText);

            // Get the Interface for BODY TEXT Formatted text

            if(pDBodyText && SUCCEEDED(pDBodyText->QueryInterface(IID_IGWFormattedText, (LPVOID*)&pBodyText))) {
               pDBodyText->Release();

               pBodyText->put_PlainText(SysAllocString(TO_OLE_STRING(szMessage)));
            }



            // Insert the attachments to the message

            pIGWMessage->get_Attachments(&pDIGWAttachments);

            if(pDIGWAttachments && SUCCEEDED(pDIGWAttachments->QueryInterface(IID_IGWAttachments, (void**)&pIGWAttachments))) {
               pDIGWAttachments->Release();

               VARIANT vFilename, vType, vDisplayName;
               int nCount;
               char textBuff[255];

               VariantInit(&vFilename);
               VariantInit(&vType);
               VariantInit(&vDisplayName);

               V_VT(&vFilename) = VT_BSTR;
               V_VT(&vType) = VT_EMPTY;
               V_VT(&vDisplayName) = VT_EMPTY;

               // How many attachments do we have ???

               nCount = ListView_GetItemCount( (HWND) hWndAttach);

               for(int i=0; i<nCount; i++) {
                  ListView_GetItemText( (HWND) hWndAttach, i, 0, textBuff, 255);

                  V_BSTR(&vFilename) = SysAllocString(TO_OLE_STRING(textBuff));

                  // Add the attachment to the message

                  pIGWAttachments->Add(vFilename, vType, vDisplayName, &pDIGWAttachment);
               }

               pIGWAttachments->Release();
            }

            // This is where we get the Recipients object to work with

            pIGWMessage->get_Recipients(&pDIGWRecipients);

            // Get Interface for the recipients

            if(pDIGWRecipients && SUCCEEDED(pDIGWRecipients->QueryInterface(IID_IGWRecipients, (LPVOID*)&pIGWRecipients))) {
               pDIGWRecipients->Release();

               // If we set target type to empty, TO: will be the default

               VariantInit(&vTargetType);
               V_VT(&vTargetType) = VT_EMPTY;

               // Name to be resolved

               bstrName = SysAllocString(TO_OLE_STRING(szTo));

               // Add the name(s) to be resolved

               pIGWRecipients->AddByDisplayName(bstrName, vTargetType, &pDIGWRecipient);

               // Get interface for recipient

               if(pDIGWRecipient && SUCCEEDED(pDIGWRecipient->QueryInterface(IID_IGWRecipient, (LPVOID*)&pIGWRecipient)))
                  pDIGWRecipient->Release();

                  // Set the Resolve parameter to empty so Resolve will

                  // try to use all address books to resolve to

                  pIGWRecipient->Resolve(vTargetType);

                  // Send the message

                  pIGWMessage->Send(&pDIGWRetMessage);

                  // Release Recipient

                  pIGWRecipient->Release();

                  // release the new mail message pointer

                  if ( pDIGWRetMessage != NULL )
                  {
                     pDIGWRetMessage->Release();
                  }
            }

            // Release Recipients

            pIGWRecipients->Release();
         }

         // Release Messages

         pIGWMessages->Release();
      }

      // Release Work In Progress Folder

      pIGWWorkFolder->Release();
   }


   delete[] szTo;
   delete[] szSubject;
   delete[] szMessage;
}



/*------------------------------------------------
InitDialog

   IN:      hWnd   -   Handle to dialog

  NOTE:      Initialize the FROM controls on the
         dialog with name of   person sending
         the message
-------------------------------------------------*/
void InitDialog(HWND hWnd)
{
DIGWAddress* pDIGWAddress;
IGWAddress* pIGWAddress;


   // This is who you are

   pIGWAccount->get_Owner(&pDIGWAddress);

   if(pDIGWAddress && SUCCEEDED(pDIGWAddress->QueryInterface(IID_IGWAddress, (LPVOID*)&pIGWAddress))) {
      pDIGWAddress->Release();

      pIGWAddress->get_DisplayName(&bsFrom);

      // We will automatically put your name into the [From] field

      if(bsFrom)
         SetDlgItemText(hWnd, IDC_FROM, FROM_OLE_STRING(bsFrom));

      pIGWAddress->Release();
   }
   else {
      ErrorMessage("QueryInterface on IID_IGWAddress Failed");
   }


   hWndAttach = GetDlgItem(hWnd, IDC_ATTACHLIST);

   imageList = ImageList_Create(32, 32, ILC_COLOR,   2, 2);

            // Add icons to image list

   ImageList_AddIcon(imageList, LoadIcon(ghInst, MAKEINTRESOURCE(IDI_MAIL)));
   ImageList_AddIcon(imageList, LoadIcon(ghInst, MAKEINTRESOURCE(IDI_FILE)));

   // Set the image list to the attachment list view control

   ListView_SetImageList( (HWND) hWndAttach, imageList, LVSIL_NORMAL);
}