home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / dlgseldg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.7 KB  |  493 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // dlgseldg.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "mailmisc.h"
  24. #include "xp_time.h"
  25. #include "xplocale.h"
  26. #include "wfemsg.h"
  27. #include "dateedit.h"
  28. #include "nethelp.h"
  29. #include "dlgseldg.h"
  30. #include "prefapi.h"
  31. #include "nethelp.h"
  32. #include "xp_help.h"
  33.  
  34. #ifdef _DEBUG
  35. #define new DEBUG_NEW
  36. #undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39.  
  40. extern "C" void HelperInitFonts( HDC hdc , HFONT *phFont, HFONT *phBoldFont);
  41.  
  42. CDiscussionsList::CDiscussionsList(): CMailFolderList()
  43. {
  44.     m_iPosIndex = 0;
  45.     m_iPosName = 50;
  46.     m_iPosStatus = 100;
  47.     m_pDWordArray = new CDWordArray;  //holds a list of groups with selection values.
  48.     m_bHasSelectables= FALSE; //Flag that tells us if anything can be selected 
  49.  
  50. }
  51.  
  52. CDiscussionsList::~CDiscussionsList()
  53. {
  54.     FolderData *pData = NULL;
  55.  
  56.     while (-1 != m_pDWordArray->GetUpperBound())
  57.     {
  58.         pData = (FolderData*)m_pDWordArray->GetAt(m_pDWordArray->GetUpperBound());
  59.         m_pDWordArray->RemoveAt(m_pDWordArray->GetUpperBound());
  60.         m_pDWordArray->FreeExtra();
  61.         if (pData)
  62.         {
  63.             delete pData;
  64.             pData = NULL;
  65.         }
  66.     }
  67.     if (m_pDWordArray)
  68.         delete m_pDWordArray;
  69. }
  70.  
  71. void CDiscussionsList::SetColumnPositions(int iPosIndex, int iPosName, int iPosStatus)
  72. {
  73.     m_iPosIndex = iPosIndex;
  74.     m_iPosName = iPosName;
  75.     m_iPosStatus = iPosStatus;
  76. }
  77.  
  78. BEGIN_MESSAGE_MAP( CDiscussionsList, CMailFolderList )
  79.     ON_WM_LBUTTONDOWN()
  80. END_MESSAGE_MAP( )
  81.  
  82.  
  83. int  CDiscussionsList::PopulateNews(MSG_Master *pMaster, BOOL bRoots)
  84. {
  85.     m_pMaster = pMaster;
  86.     int index = 0;
  87.     int nCount=0;
  88.     m_iInitialDepth = 1;
  89.  
  90.     ::SendMessage( m_hWnd, m_nResetContent, (WPARAM) 0, (LPARAM) 0 );
  91.  
  92.     int32 iLines = MSG_GetFolderChildren (m_pMaster, NULL, NULL, 0);
  93.     MSG_FolderInfo **ppFolderInfo = new MSG_FolderInfo *[iLines];
  94.  
  95.     ASSERT(ppFolderInfo);
  96.     if (ppFolderInfo)
  97.     {
  98.         MSG_GetFolderChildren (m_pMaster, NULL, ppFolderInfo, iLines);
  99.         for (int i = 0; i < iLines; i++)
  100.         {
  101.             MSG_FolderLine folderLine;
  102.             if (MSG_GetFolderLineById (m_pMaster, ppFolderInfo[i], &folderLine)) 
  103.             {
  104.                 if ( folderLine.flags & MSG_FOLDER_FLAG_NEWSGROUP ||
  105.                      folderLine.flags & MSG_FOLDER_FLAG_NEWS_HOST || 
  106.                      folderLine.flags & MSG_FOLDER_FLAG_IMAPBOX   ||
  107.                      folderLine.flags & MSG_FOLDER_FLAG_CAT_CONTAINER) 
  108.                 {
  109.                     if ( bRoots )
  110.                     {
  111.                         FolderData *pFolderData = new FolderData;
  112.                         if (pFolderData)
  113.                         {
  114.                             pFolderData->infoData = folderLine.id;
  115.                             pFolderData->bDownLoad = (BOOL)(MSG_GetFolderPrefFlags(folderLine.id) & MSG_FOLDER_PREF_OFFLINE);
  116.                             ::SendMessage( m_hWnd, m_nAddString, (WPARAM) 0, (LPARAM) folderLine.name );
  117.                             ::SendMessage( m_hWnd, m_nSetItemData, (WPARAM) index,(DWORD)(pFolderData) );
  118.                             TRY
  119.                             {
  120.                                 m_pDWordArray->Add((DWORD)pFolderData);
  121.                             }
  122.                             CATCH(CMemoryException, e)
  123.                             {
  124.                                 delete [] ppFolderInfo;
  125.                                 return  0;
  126.                             }
  127.                             END_CATCH
  128.                             index++;
  129.                         }
  130.                         if ( folderLine.numChildren > 0 ) 
  131.                         {
  132.                             SubPopulate(index, folderLine.id );
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.         delete [] ppFolderInfo;
  139.     }
  140.  
  141.     return 1;    
  142. }
  143.  
  144.  
  145. void CDiscussionsList::SubPopulate(int &index, MSG_FolderInfo *folder )
  146. {
  147.     int32 iLines = MSG_GetFolderChildren (m_pMaster, folder, NULL, 0);
  148.     MSG_FolderInfo **ppFolderInfo = new MSG_FolderInfo *[iLines];
  149.  
  150.     ASSERT(ppFolderInfo);
  151.     if (ppFolderInfo)
  152.     {
  153.         MSG_GetFolderChildren (m_pMaster, folder, ppFolderInfo, iLines);
  154.         for (int i = 0; i < iLines; i++)
  155.         {
  156.             MSG_FolderLine folderLine;
  157.             if (MSG_GetFolderLineById (m_pMaster, ppFolderInfo[i], &folderLine)) 
  158.             {
  159.                     m_bHasSelectables = TRUE;
  160.                     FolderData *pFolderData = new FolderData;
  161.                     if (pFolderData)
  162.                     {
  163.                         pFolderData->infoData = folderLine.id;
  164.                         pFolderData->bDownLoad = (BOOL)(MSG_GetFolderPrefFlags(folderLine.id) & MSG_FOLDER_PREF_OFFLINE);
  165.                         ::SendMessage( m_hWnd, m_nAddString, (WPARAM) 0, (LPARAM) folderLine.name );
  166.                         ::SendMessage( m_hWnd, m_nSetItemData, (WPARAM) index,(DWORD)(pFolderData) );
  167.                         TRY
  168.                         {
  169.                             m_pDWordArray->Add((DWORD)pFolderData);
  170.                         }
  171.                         CATCH(CMemoryException, e)
  172.                         {
  173.                             delete [] ppFolderInfo;
  174.                             return;
  175.                         }
  176.                         END_CATCH
  177.                         index++;
  178.                     }
  179.                     if ( folderLine.numChildren > 0 ) {
  180.                         SubPopulate(index, folderLine.id);
  181.                     }
  182.             }
  183.         }
  184.         delete [] ppFolderInfo;
  185.     }
  186. }
  187.  
  188.  
  189.  
  190. void CDiscussionsList::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  191. {
  192.     HDC hDC = lpDrawItemStruct->hDC;
  193.     RECT rcItem = lpDrawItemStruct->rcItem;
  194.     RECT rcTemp = rcItem;
  195.     RECT rcText;
  196.     DWORD dwItemData = lpDrawItemStruct->itemData;
  197.     HBRUSH hBrushWindow = ::CreateSolidBrush( GetSysColor( COLOR_WINDOW ) );
  198.     HBRUSH hBrushHigh = ::CreateSolidBrush( GetSysColor( COLOR_HIGHLIGHT ) );
  199.     HBRUSH hBrushFill = NULL;
  200.  
  201.     if ( !m_hFont ) {
  202.         HelperInitFonts( hDC, &m_hFont, &m_hBoldFont);
  203.     }
  204.  
  205.     HFONT hOldFont = NULL;
  206.     if ( m_hFont ) {
  207.         hOldFont = (HFONT) ::SelectObject( hDC,(HFONT)&m_hFont );
  208.     }
  209.  
  210.     if ( lpDrawItemStruct->itemState & ODS_SELECTED ) {
  211.         hBrushFill = hBrushHigh;
  212.         ::SetBkColor( hDC, GetSysColor( COLOR_HIGHLIGHT ) );
  213.         ::SetTextColor( hDC, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
  214.     } else {
  215.         hBrushFill = hBrushWindow;
  216.         ::SetBkColor( hDC, GetSysColor( COLOR_WINDOW ) );
  217.         ::SetTextColor( hDC, GetSysColor( COLOR_WINDOWTEXT ) );
  218.     }
  219.  
  220.     VERIFY(::FillRect( hDC, &rcItem, hBrushFill ));
  221.  
  222.     if ( lpDrawItemStruct->itemID != -1 &&  dwItemData )
  223.     {
  224.         FolderData *  itemInfo = (FolderData *) dwItemData;
  225.         MSG_FolderLine folderLine;
  226.         MSG_GetFolderLineById( m_pMaster, (MSG_FolderInfo*)itemInfo->infoData , &folderLine);
  227.  
  228.         int idxImage = WFE_MSGTranslateFolderIcon( folderLine.level,
  229.                                                    folderLine.flags, 
  230.                                                    folderLine.numChildren );
  231.  
  232.         if ( m_hBoldFont && folderLine.unseen > 0 ) 
  233.         {
  234.             ::SelectObject( hDC, (HFONT)&m_hBoldFont );
  235.         }
  236.         int iIndent = 4;
  237.  
  238.         BOOL bStatic = FALSE;
  239. #ifdef _WIN32
  240.     if ( sysInfo.m_bWin4 )
  241.         bStatic = ( lpDrawItemStruct->itemState & ODS_COMBOBOXEDIT ) ? TRUE : FALSE;
  242.     else 
  243. #endif
  244.         bStatic = m_bStaticCtl;
  245.  
  246.         if (!bStatic)
  247.             iIndent += (folderLine.level - m_iInitialDepth) * 8;
  248.  
  249. //Draw the news bitmap
  250.         m_pIImageMap->DrawImage( idxImage, iIndent, rcItem.top, hDC, FALSE );
  251.         LPCTSTR name = (LPCTSTR) folderLine.prettyName;
  252.         if ( !name || !name[0] )
  253.             name = folderLine.name;
  254. //Draw the text
  255.         ::DrawText( hDC, name, -1, &rcTemp, DT_SINGLELINE|DT_CALCRECT|DT_NOPREFIX );
  256.         int iWidth = rcTemp.right - rcTemp.left;
  257.  
  258.         rcTemp = rcItem;
  259.         rcText = rcItem;
  260.         rcTemp.left = iIndent + 20;
  261.         rcTemp.right = rcTemp.left + iWidth + 4;
  262.  
  263.         VERIFY(::FillRect( hDC, &rcTemp, hBrushFill ));
  264.         rcText.left = rcTemp.left + 2;
  265.         rcText.right = rcTemp.right - 2;
  266.         ::DrawText( hDC, name, -1, &rcText, DT_SINGLELINE|DT_VCENTER|DT_NOPREFIX );
  267. //handle the checkmark if it's a (news group or IMAP mail folder or a category container) and not a host
  268.         if ((folderLine.flags & MSG_FOLDER_FLAG_NEWSGROUP || 
  269.             folderLine.flags & MSG_FOLDER_FLAG_IMAPBOX   ||
  270.             folderLine.flags & MSG_FOLDER_FLAG_CAT_CONTAINER) &&
  271.             !(folderLine.flags & MSG_FOLDER_FLAG_IMAP_SERVER))
  272.         {
  273.             BOOL bEnabled = itemInfo->bDownLoad;
  274.             m_pIImageMap->DrawTransImage( bEnabled ? IDX_CHECKMARK : IDX_CHECKBOX,
  275.                                           m_iPosStatus, rcItem.top, hDC );
  276.         }
  277. //Draw the focus
  278.         if ( lpDrawItemStruct->itemAction & ODA_FOCUS && 
  279.              lpDrawItemStruct->itemState & ODS_SELECTED )
  280.         {
  281.             ::DrawFocusRect( hDC, &lpDrawItemStruct->rcItem );
  282.         }    
  283.     }
  284.  
  285.     if ( hBrushHigh ) 
  286.         VERIFY( ::DeleteObject( hBrushHigh ));
  287.     if ( hBrushWindow ) 
  288.         VERIFY( ::DeleteObject( hBrushWindow ));
  289.  
  290.     if ( hOldFont )
  291.         ::SelectObject( hDC, hOldFont );
  292. }
  293.  
  294.  
  295. UINT CDiscussionsList::ItemFromPoint(CPoint pt, BOOL& bOutside) const
  296. {
  297.     RECT rect;
  298.     GetClientRect(&rect);
  299.  
  300.     int iHeight = GetItemHeight(0);
  301.     int iCount = GetCount();
  302.     int iTopIndex = GetTopIndex();
  303.  
  304.     int iListHeight = iHeight * ( iCount - iTopIndex );
  305.     rect.bottom = rect.bottom < iListHeight ? rect.bottom : iListHeight;
  306.  
  307.     bOutside = !::PtInRect(&rect, pt);
  308.  
  309.     if ( bOutside ) {
  310.         return 0;
  311.     } 
  312.  
  313.     return (pt.y / iHeight) + iTopIndex; 
  314. }
  315.  
  316. void CDiscussionsList::OnLButtonDown( UINT nFlags, CPoint point )
  317. {
  318.     if ( point.x > m_iPosStatus ) {
  319.         BOOL bOutside;
  320.         UINT item = ItemFromPoint( point, bOutside );
  321.         RECT rcItem;
  322.         GetItemRect( item, &rcItem );
  323.  
  324.         if ( (point.y < rcItem.bottom) && (point.y >= rcItem.top) ) {
  325.             FolderData *pItemData = (FolderData *)GetItemData( item );
  326.             if ( pItemData ) {
  327.                 pItemData->bDownLoad = !pItemData->bDownLoad;
  328.                 InvalidateRect( &rcItem );
  329.             }
  330.         }
  331.     } else {
  332.         CListBox::OnLButtonDown( nFlags, point );
  333.     }
  334. }
  335.  
  336. /////////////////////////////////////////////////////////////////////////////
  337. // CDlgSelectGroups dialog
  338.  
  339.  
  340. CDlgSelectGroups::CDlgSelectGroups(CWnd* pParent /*=NULL*/)
  341.     : CDialog(CDlgSelectGroups::IDD, pParent)
  342. {
  343.     //{{AFX_DATA_INIT(CDlgSelectGroups)
  344.         // NOTE: the ClassWizard will add member initialization here
  345.     //}}AFX_DATA_INIT
  346.  
  347.     m_nDiscussionSelectionCount=0;
  348.     m_nMailSelectionCount=0;
  349. }
  350.  
  351.  
  352. void CDlgSelectGroups::DoDataExchange(CDataExchange* pDX)
  353. {
  354.     CDialog::DoDataExchange(pDX);
  355.     //{{AFX_DATA_MAP(CDlgSelectGroups)
  356.         // NOTE: the ClassWizard will add DDX and DDV calls here
  357.     //}}AFX_DATA_MAP
  358. }
  359.  
  360.  
  361. BEGIN_MESSAGE_MAP(CDlgSelectGroups, CDialog)
  362.     //{{AFX_MSG_MAP(CDlgSelectGroups)
  363.     ON_BN_CLICKED(IDC_BUTTON_SELECT_ALL, OnButtonSelectAll)
  364.     ON_BN_CLICKED(IDC_HELP_SELECT_FOR_DOWNLOAD, OnHelp)
  365.  
  366.     //}}AFX_MSG_MAP
  367. END_MESSAGE_MAP()
  368.  
  369. /////////////////////////////////////////////////////////////////////////////
  370. // CDlgSelectGroups message handlers
  371.  
  372. void CDlgSelectGroups::OnButtonSelectAll() 
  373. {
  374.  
  375.     CListBox *pList = (CListBox*)GetDlgItem(IDC_LIST_DISCUSSIONS);
  376.     if (pList)
  377.     {
  378.         int nCount = pList->GetCount();
  379.         CRect rect;
  380.         MSG_FolderLine folderLine;
  381.         MSG_Master *pMaster = WFE_MSGGetMaster();
  382.         for (int i = 0; i < nCount ; i++)
  383.         {
  384.             FolderData *pData = (FolderData*)pList->GetItemData(i);
  385.             if (pData)
  386.             {   //Make sure we are setting the properties of a group, not a host
  387.                 MSG_GetFolderLineById( pMaster, pData->infoData, &folderLine );
  388.                 if ( folderLine.flags & MSG_FOLDER_FLAG_NEWSGROUP || 
  389.                     folderLine.flags & MSG_FOLDER_FLAG_IMAPBOX    ||
  390.                     folderLine.flags & MSG_FOLDER_FLAG_CAT_CONTAINER)  
  391.                 {
  392.                     pData->bDownLoad=TRUE;
  393.                     pList->SetItemData(i,(DWORD)pData);
  394.                     pList->GetItemRect(i,&rect);
  395.                     pList->InvalidateRect(rect);
  396.                 }
  397.             }
  398.         }
  399.  
  400.     }
  401. }
  402.  
  403. void CDlgSelectGroups::OnOK() 
  404. {                   
  405.     MSG_FolderLine folderLine;
  406.     MSG_Master *pMaster = WFE_MSGGetMaster();
  407.  
  408.     for (int i=0 ; i <= m_DiscussionList.m_pDWordArray->GetUpperBound(); i++)
  409.     {
  410.         FolderData *pData = (FolderData*)m_DiscussionList.m_pDWordArray->GetAt(i);
  411.         if (pData)
  412.         {   
  413.             if (pData->bDownLoad)
  414.             {
  415.                 MSG_SetFolderPrefFlags(pData->infoData, MSG_FOLDER_PREF_OFFLINE);
  416.                 MSG_GetFolderLineById( pMaster, pData->infoData, &folderLine );
  417.                 if ( folderLine.flags & MSG_FOLDER_FLAG_IMAPBOX) 
  418.                     ++m_nMailSelectionCount; //count the selected mail folders
  419.                 else
  420.                     ++m_nDiscussionSelectionCount;     //count the selected discussion groups
  421.             }
  422.             else
  423.                 MSG_SetFolderPrefFlags(pData->infoData, !MSG_FOLDER_PREF_OFFLINE);
  424.         }
  425.     }
  426.  
  427.     PREF_SetIntPref("mail.selection.count",(int32)m_nMailSelectionCount);
  428.     PREF_SetIntPref("offline.news.discussions_count",(int32)m_nDiscussionSelectionCount);
  429.     
  430.     CDialog::OnOK();
  431. }
  432.  
  433.  
  434. BOOL CDlgSelectGroups::OnInitDialog() 
  435. {
  436.     CDialog::OnInitDialog();
  437.  
  438.     m_DiscussionList.SubclassDlgItem( IDC_LIST_DISCUSSIONS, this );
  439.  
  440.     m_iIndex = 0;
  441.  
  442.     int iPosIndex, iPosName, iPosStatus;
  443.     int iListLeft = 4;
  444.     RECT rect, rcText;
  445.     
  446.     CWnd *widget = GetDlgItem(IDC_LIST_DISCUSSIONS);
  447.     widget->GetWindowRect(&rect);
  448.  
  449.     ::SetRect( &rcText, 0, 0, 64, 64 );
  450.  
  451.     iPosIndex = iListLeft + 4;
  452.     iPosName = iPosIndex + rcText.right * 3;
  453.     iPosStatus = rect.right - rect.left - 20 - GetSystemMetrics(SM_CXVSCROLL);
  454.  
  455.     m_DiscussionList.SetColumnPositions( iPosIndex, iPosName, iPosStatus );
  456.  
  457.     m_DiscussionList.PopulateNews(WFE_MSGGetMaster(),TRUE);
  458.  
  459.     if (!m_DiscussionList.m_bHasSelectables)
  460.         AfxMessageBox(IDS_NOTHING_SUBSCRIBED);
  461.  
  462.     return TRUE;
  463. }
  464.  
  465. void CDlgSelectGroups::OnCancel() 
  466. {
  467.     BOOL bEnabled = FALSE;
  468.     MSG_FolderLine folderLine;
  469.     MSG_Master *pMaster = WFE_MSGGetMaster();
  470.  
  471. #ifdef WE_USE_SELECTION_COUNT // currently not using this, so don't calculate it
  472.     for (int i=0 ; i <= m_DiscussionList.m_pDWordArray->GetUpperBound(); i++)
  473.     {
  474.         FolderData *pData = (FolderData*)m_DiscussionList.m_pDWordArray->GetAt(i);
  475.         BOOL bEnabled = (BOOL)(MSG_GetFolderPrefFlags(pData->infoData) & MSG_FOLDER_PREF_OFFLINE);
  476.         if (bEnabled)
  477.         {
  478.                 MSG_GetFolderLineById( pMaster, pData->infoData, &folderLine );
  479.                 if ( folderLine.flags & MSG_FOLDER_FLAG_IMAPBOX) 
  480.                     ++m_nMailSelectionCount; //count the selected mail folders
  481.                 else
  482.                     ++m_nDiscussionSelectionCount;     //count the selected discussion groups
  483.         }
  484.     }
  485. #endif WE_USE_SELECTION_COUNT
  486.     CDialog::OnCancel();
  487. }
  488.  
  489. void CDlgSelectGroups::OnHelp() 
  490. {
  491.     NetHelp(HELP_OFFLINE_DISCUSSION_GROUPS);
  492. }
  493.