home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / edhdrdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.0 KB  |  355 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. // edhdrdlg.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "netscape.h"
  24. #include "edhdrdlg.h"
  25. #include "wfemsg.h"
  26. #include "prefapi.h"
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. //Edit Header Dialog called from CCustomHeadersDlg
  35.  
  36. CEditHeadersDlg::CEditHeadersDlg(CString strHeader, CWnd* pParent /*=NULL*/ )
  37.     : CDialog(CEditHeadersDlg::IDD, pParent)
  38. {
  39.     m_pParent = pParent;
  40.  
  41.     //{{AFX_DATA_INIT(CEditHeadersDlg)
  42.     m_strHeader = strHeader;
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46.  
  47. void CEditHeadersDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CDialog::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CEditHeadersDlg)
  51.     DDX_Text(pDX, IDC_EDIT_HEADERS, m_strHeader);
  52.     DDV_MaxChars(pDX, m_strHeader, 65);
  53.     //}}AFX_DATA_MAP
  54. }
  55.  
  56.  
  57. BEGIN_MESSAGE_MAP(CEditHeadersDlg, CDialog)
  58.     //{{AFX_MSG_MAP(CEditHeadersDlg)
  59.     ON_EN_CHANGE(IDC_EDIT_HEADERS, OnChangeEditHeader)
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63.  
  64. BOOL CEditHeadersDlg::OnInitDialog() 
  65. {
  66.     CDialog::OnInitDialog();
  67.     m_MNResourceSwitcher.Reset();
  68.     return TRUE;  // return TRUE unless you set the focus to a control
  69.                   // EXCEPTION: OCX Property Pages should return FALSE
  70. }
  71.  
  72.  
  73.  
  74. void CEditHeadersDlg::OnChangeEditHeader() 
  75. {
  76.  
  77.     CWnd *pWnd = (CWnd*) GetDlgItem(IDOK);
  78.  
  79.     CEdit *pEdit = (CEdit*) GetDlgItem(IDC_EDIT_HEADERS);
  80.     CString strText;
  81.     pEdit->GetWindowText(strText);
  82.  
  83.     if (strText.IsEmpty() )
  84.     {
  85.         UpdateData();
  86.         pWnd->EnableWindow(FALSE);
  87.         return;
  88.     }
  89.  
  90.     char buff;
  91.     for (int i=0; i < strText.GetLength(); i++)
  92.     {
  93.         buff = strText.GetAt(i);
  94.         if ( (isspace((int)buff)) || (buff == ':') || ((int)buff < 32) || ((int)buff > 127) )
  95.         {
  96.             CString strMessage;
  97.             CString strCaption;
  98.             strMessage.LoadString(IDS_HEADER_TEXT_WARN);
  99.             strCaption.LoadString(IDS_CUSTOM_HEADER_ERROR);
  100.             ::MessageBox(this->GetSafeHwnd(), strMessage, strCaption, MB_OK);
  101.             UpdateData(FALSE);
  102.             pEdit->SetFocus();
  103.             return;
  104.         }
  105.     }
  106.  
  107.     UpdateData();
  108.  
  109.     if (!m_strHeader.IsEmpty() && pWnd)
  110.     {
  111.         pWnd->EnableWindow();
  112.     }
  113.     else if(m_strHeader.IsEmpty() && pWnd)
  114.     {
  115.         pWnd->EnableWindow(FALSE);
  116.     }
  117.  
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CCustomHeadersDlg dialog
  122.  
  123.  
  124. CCustomHeadersDlg::CCustomHeadersDlg(CWnd* pParent /*=NULL*/)
  125.     : CDialog(CCustomHeadersDlg::IDD, pParent)
  126. {
  127.     m_pParent  = pParent;
  128.     if (!CDialog::Create(CCustomHeadersDlg::IDD, pParent))
  129.     {
  130.         TRACE0("Warning: creation of CCustomHeadersDlg dialog failed\n");
  131.         if (m_pParent)
  132.             PostMessage(WM_EDIT_CUSTOM_DONE,0,IDCANCEL);
  133.  
  134.         return;
  135.     }
  136.     if( m_pParent && ::IsWindow(m_pParent->m_hWnd) ){
  137.         m_pParent->EnableWindow(FALSE);
  138.     }
  139.  
  140.     m_nReturnCode = IDCANCEL;  //initial value
  141.  
  142.     this->EnableWindow(TRUE);
  143.     //{{AFX_DATA_INIT(CCustomHeadersDlg)
  144.     m_strHeader = _T("");
  145.     //}}AFX_DATA_INIT
  146. }
  147.  
  148.  
  149. void CCustomHeadersDlg::DoDataExchange(CDataExchange* pDX)
  150. {
  151.     CDialog::DoDataExchange(pDX);
  152.     //{{AFX_DATA_MAP(CCustomHeadersDlg)
  153.     DDX_Control(pDX, IDC_HEADER_LIST, m_lbHeaderList);
  154.     //}}AFX_DATA_MAP
  155. }
  156.  
  157.  
  158. BEGIN_MESSAGE_MAP(CCustomHeadersDlg, CDialog)
  159.     //{{AFX_MSG_MAP(CCustomHeadersDlg)
  160.     ON_BN_CLICKED(IDC_ADD_HEADER, OnAddHeader)
  161.     ON_BN_CLICKED(IDC_EDIT_HEADER_NAME, OnEditHeader)
  162.     ON_LBN_SETFOCUS(IDC_HEADER_LIST, OnSetfocusHeaderList)
  163.     ON_BN_CLICKED(IDC_REMOVE_HEADER, OnRemoveHeader)
  164.     ON_COMMAND(IDCANCEL, OnClose)
  165.     //}}AFX_MSG_MAP
  166. END_MESSAGE_MAP()
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CCustomHeadersDlg message handlers
  170.  
  171. BOOL CCustomHeadersDlg::OnInitDialog() 
  172. {
  173.     CDialog::OnInitDialog();
  174.  
  175.  
  176.     MSG_FolderInfo *pInbox = NULL;
  177.     MSG_GetFoldersWithFlag (WFE_MSGGetMaster(), MSG_FOLDER_FLAG_INBOX, &pInbox, 1);
  178.     uint16 numItems;
  179.     MSG_GetNumAttributesForFilterScopes (WFE_MSGGetMaster(), scopeMailFolder, (void**)&pInbox, 1, &numItems); 
  180.     MSG_SearchMenuItem * pHeaderItems = new MSG_SearchMenuItem [numItems];
  181.     
  182.     if (!pHeaderItems) 
  183.         return FALSE;  //something bad happened here!!
  184.  
  185.     MSG_GetAttributesForFilterScopes (WFE_MSGGetMaster(), scopeMailFolder, (void**)&pInbox, 1, pHeaderItems, &numItems);
  186.  
  187.     for (int i=0; i < numItems; i++)
  188.     {
  189.         if ( (pHeaderItems[i].attrib == attribOtherHeader) && pHeaderItems[i].isEnabled )
  190.         {
  191.             m_lbHeaderList.AddString(pHeaderItems[i].name);
  192.         }
  193.     }
  194.  
  195.     delete pHeaderItems;
  196.  
  197.  
  198.     return TRUE;  // return TRUE unless you set the focus to a control
  199.                   // EXCEPTION: OCX Property Pages should return FALSE
  200. }
  201.  
  202. void CCustomHeadersDlg::OnAddHeader() 
  203. {
  204.     
  205.     CEditHeadersDlg EditDlg( " ",this);
  206.     if (IDOK == EditDlg.DoModal())
  207.     {
  208.         if ( !EditDlg.m_strHeader.IsEmpty())
  209.         {
  210.             m_lbHeaderList.AddString(EditDlg.m_strHeader);
  211.         }
  212.         else
  213.         {
  214.             return;
  215.         }
  216.     }
  217.     else
  218.     {
  219.         return;
  220.     }
  221.  
  222.     CWnd *pWnd = (CWnd*) GetDlgItem(IDC_ADD_HEADER);
  223.  
  224.     if (pWnd)
  225.     {
  226.         pWnd->SetFocus();
  227.     }
  228.     UpdateData();    
  229. }
  230.  
  231. void CCustomHeadersDlg::OnEditHeader() 
  232. {
  233.     int iSel = m_lbHeaderList.GetCurSel();
  234.     CString strHeader;
  235.  
  236.     if (iSel >= 0)
  237.     {
  238.         m_lbHeaderList.GetText(iSel,strHeader);
  239.     }
  240.     else
  241.     {
  242.         return;
  243.     }
  244.  
  245.     CEditHeadersDlg EditDlg( strHeader,this);
  246.     if (IDOK == EditDlg.DoModal())
  247.     {
  248.         if ( !EditDlg.m_strHeader.IsEmpty() && !(EditDlg.m_strHeader.Compare(strHeader) == 0) )
  249.         {
  250.             m_lbHeaderList.DeleteString(iSel);
  251.             m_lbHeaderList.AddString(EditDlg.m_strHeader);
  252.         }
  253.         else
  254.         {
  255.             return;
  256.         }
  257.     }
  258.     else
  259.     {
  260.         return;
  261.     }
  262.  
  263.     CWnd *pWnd = (CWnd*) GetDlgItem(IDC_ADD_HEADER);
  264.  
  265.     if (pWnd)
  266.     {
  267.         pWnd->SetFocus();
  268.     }
  269.     UpdateData();    
  270. }
  271.  
  272.  
  273.  
  274.  
  275. void CCustomHeadersDlg::OnSetfocusHeaderList() 
  276. {
  277.     CWnd *pWndRemove = (CWnd*) GetDlgItem(IDC_REMOVE_HEADER);
  278.     CWnd *pWndEdit   = (CWnd*) GetDlgItem(IDC_EDIT_HEADER_NAME);
  279.     if (pWndRemove && pWndEdit)
  280.     {
  281.         if (m_lbHeaderList.GetCount() != 0)
  282.         {
  283.             pWndRemove->EnableWindow();
  284.             pWndEdit->EnableWindow();
  285.         }
  286.  
  287.     }    
  288. }
  289.  
  290. void CCustomHeadersDlg::OnRemoveHeader() 
  291. {
  292.     int iSel = m_lbHeaderList.GetCurSel();
  293.     m_lbHeaderList.DeleteString(iSel);
  294.     if (m_lbHeaderList.GetCount() == 0)
  295.     {
  296.         CWnd *pWnd = (CWnd*) GetDlgItem(IDC_REMOVE_HEADER);
  297.         CWnd *pWnd2 = (CWnd*) GetDlgItem(IDC_EDIT_HEADER_NAME);
  298.         
  299.         if (pWnd)
  300.         {
  301.             pWnd->EnableWindow(FALSE);
  302.             pWnd2->EnableWindow(FALSE);
  303.         }
  304.         pWnd = (CWnd*) GetDlgItem(IDC_ADD_HEADER);
  305.         if (pWnd)
  306.             pWnd->SetFocus();
  307.     }
  308.     else
  309.         m_lbHeaderList.SetFocus();
  310. }
  311.  
  312. void CCustomHeadersDlg::OnOK() 
  313. {
  314.     // TODO: Add extra validation here
  315.     if (m_lbHeaderList.GetCount() != 0)
  316.     {
  317.         int nListCount = m_lbHeaderList.GetCount();
  318.         CString strTemp;
  319.         m_lbHeaderList.GetText(0,strTemp);
  320.         CString strHeaderPrefList = strTemp; 
  321.  
  322.         for (int i = 1; i < nListCount; i++)
  323.         {
  324.             m_lbHeaderList.GetText(i,strTemp);
  325.             strHeaderPrefList += ": " + strTemp; 
  326.         }
  327.         PREF_SetCharPref("mailnews.customHeaders", strHeaderPrefList);
  328.     }
  329.     else
  330.     {
  331.         PREF_SetCharPref("mailnews.customHeaders","");
  332.     }
  333.     m_nReturnCode = IDOK;
  334.     OnClose();
  335. }
  336.  
  337. void CCustomHeadersDlg::PostNcDestroy()
  338. {
  339.     CDialog::PostNcDestroy();
  340.     if( m_pParent && ::IsWindow(m_pParent->m_hWnd) ){
  341.         m_pParent->EnableWindow(TRUE);
  342.         // Return focus to parent window
  343.         m_pParent->SetActiveWindow();
  344.         m_pParent->SetFocus();
  345.     }
  346.     delete this;
  347. }
  348.  
  349.  
  350. void CCustomHeadersDlg::OnClose()
  351. {
  352.     GetParent()->PostMessage(WM_EDIT_CUSTOM_DONE,0,m_nReturnCode);
  353.     DestroyWindow();
  354. }
  355.