Back to index

lightning-sunbird  0.9+nobinonly
ProfilesDlg.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: Mozilla-sample-code 1.0
00003  *
00004  * Copyright (c) 2002 Netscape Communications Corporation and
00005  * other contributors
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this Mozilla sample software and associated documentation files
00009  * (the "Software"), to deal in the Software without restriction, including
00010  * without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit
00012  * persons to whom the Software is furnished to do so, subject to the
00013  * following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included
00016  * in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00021  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00023  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00024  * DEALINGS IN THE SOFTWARE.
00025  *
00026  * Contributor(s):
00027  *
00028  * ***** END LICENSE BLOCK ***** */
00029 
00030 // ProfilesDlg.cpp : implementation file
00031 //
00032 
00033 #include "stdafx.h"
00034 #include <afxpriv.h>
00035 #include "mfcembed.h"
00036 #include "ProfilesDlg.h"
00037 
00038 // Mozilla
00039 #include "nsIProfile.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsServiceManagerUtils.h"
00042 #include "nsMemory.h"
00043 
00044 #ifdef _DEBUG
00045 #define new DEBUG_NEW
00046 #undef THIS_FILE
00047 static char THIS_FILE[] = __FILE__;
00048 #endif
00049 
00050 // Static Routines
00051 static void ValidateProfileName(const CString& profileName, CDataExchange* pDX)
00052 {
00053     USES_CONVERSION;
00054 
00055     nsresult rv;
00056     PRBool exists = FALSE;
00057 
00058     {
00059         nsCOMPtr<nsIProfile> profileService = 
00060                  do_GetService(NS_PROFILE_CONTRACTID, &rv);
00061         rv = profileService->ProfileExists(T2CW(profileName), &exists);
00062     }
00063 
00064     if (NS_SUCCEEDED(rv) && exists)
00065     {
00066         CString errMsg;
00067         errMsg.Format(_T("Error: A profile named \"%s\" already exists."), profileName);
00068         AfxMessageBox( errMsg, MB_ICONEXCLAMATION );
00069         errMsg.Empty();
00070         pDX->Fail();
00071     }
00072 
00073     if (profileName.FindOneOf(_T("\\/")) != -1)
00074     {
00075         AfxMessageBox( _T("Error: A profile name cannot contain the characters \"\\\" or \"/\"."), MB_ICONEXCLAMATION );
00076         pDX->Fail();
00077     }
00078 }
00079 
00081 // CNewProfileDlg dialog
00082 
00083 
00084 CNewProfileDlg::CNewProfileDlg(CWnd* pParent /*=NULL*/)
00085     : CDialog(CNewProfileDlg::IDD, pParent)
00086 {
00087     //{{AFX_DATA_INIT(CNewProfileDlg)
00088     m_LocaleIndex = -1;
00089     m_Name = _T("");
00090     //}}AFX_DATA_INIT
00091 }
00092 
00093 
00094 void CNewProfileDlg::DoDataExchange(CDataExchange* pDX)
00095 {
00096     CDialog::DoDataExchange(pDX);
00097     //{{AFX_DATA_MAP(CNewProfileDlg)
00098     DDX_CBIndex(pDX, IDC_LOCALE_COMBO, m_LocaleIndex);
00099     DDX_Text(pDX, IDC_NEW_PROF_NAME, m_Name);
00100     //}}AFX_DATA_MAP
00101 
00102     pDX->PrepareEditCtrl(IDC_NEW_PROF_NAME);
00103     if (pDX->m_bSaveAndValidate)
00104     {
00105         ValidateProfileName(m_Name, pDX);
00106     }
00107 }
00108 
00109 
00110 BEGIN_MESSAGE_MAP(CNewProfileDlg, CDialog)
00111     //{{AFX_MSG_MAP(CNewProfileDlg)
00112         // NOTE: the ClassWizard will add message map macros here
00113     //}}AFX_MSG_MAP
00114 END_MESSAGE_MAP()
00115 
00117 // CNewProfileDlg message handlers
00118 
00119 
00121 // CRenameProfileDlg dialog
00122 
00123 
00124 CRenameProfileDlg::CRenameProfileDlg(CWnd* pParent /*=NULL*/)
00125     : CDialog(CRenameProfileDlg::IDD, pParent)
00126 {
00127     //{{AFX_DATA_INIT(CRenameProfileDlg)
00128     m_NewName = _T("");
00129     //}}AFX_DATA_INIT
00130 }
00131 
00132 
00133 void CRenameProfileDlg::DoDataExchange(CDataExchange* pDX)
00134 {
00135     CDialog::DoDataExchange(pDX);
00136     //{{AFX_DATA_MAP(CRenameProfileDlg)
00137     DDX_Text(pDX, IDC_NEW_NAME, m_NewName);
00138     //}}AFX_DATA_MAP
00139 
00140     pDX->PrepareEditCtrl(IDC_NEW_NAME);
00141     if (pDX->m_bSaveAndValidate)
00142     {
00143         ValidateProfileName(m_NewName, pDX);
00144     }
00145 }
00146 
00147 
00148 BEGIN_MESSAGE_MAP(CRenameProfileDlg, CDialog)
00149     //{{AFX_MSG_MAP(CRenameProfileDlg)
00150         // NOTE: the ClassWizard will add message map macros here
00151     //}}AFX_MSG_MAP
00152 END_MESSAGE_MAP()
00153 
00155 // CRenameProfileDlg message handlers
00156 
00157 
00159 // CProfilesDlg dialog
00160 
00161 
00162 CProfilesDlg::CProfilesDlg(CWnd* pParent /*=NULL*/)
00163     : CDialog(CProfilesDlg::IDD, pParent)
00164 {
00165     //{{AFX_DATA_INIT(CProfilesDlg)
00166     m_bAtStartUp = FALSE;
00167     m_bAskAtStartUp = FALSE;
00168     //}}AFX_DATA_INIT
00169 }
00170 
00171 
00172 void CProfilesDlg::DoDataExchange(CDataExchange* pDX)
00173 {
00174     CDialog::DoDataExchange(pDX);
00175     //{{AFX_DATA_MAP(CProfilesDlg)
00176     DDX_Control(pDX, IDC_LIST1, m_ProfileList);
00177     DDX_Check(pDX, IDC_CHECK_ASK_AT_START, m_bAskAtStartUp);
00178     //}}AFX_DATA_MAP
00179 
00180     if (pDX->m_bSaveAndValidate)
00181     {
00182         USES_CONVERSION;
00183 
00184         int itemIndex = m_ProfileList.GetCurSel();
00185         if (itemIndex != LB_ERR)
00186         {
00187             CString itemText;
00188             m_ProfileList.GetText(itemIndex, itemText);
00189             m_SelectedProfile.Assign(T2CW(itemText));
00190         }
00191     }
00192 }
00193 
00194 
00195 BEGIN_MESSAGE_MAP(CProfilesDlg, CDialog)
00196     //{{AFX_MSG_MAP(CProfilesDlg)
00197     ON_BN_CLICKED(IDC_PROF_NEW, OnNewProfile)
00198     ON_BN_CLICKED(IDC_PROF_RENAME, OnRenameProfile)
00199     ON_BN_CLICKED(IDC_PROF_DELETE, OnDeleteProfile)
00200     ON_LBN_DBLCLK(IDC_LIST1, OnDblclkProfile)
00201     //}}AFX_MSG_MAP
00202 END_MESSAGE_MAP()
00203 
00205 // CProfilesDlg message handlers
00206 
00207 BOOL CProfilesDlg::OnInitDialog() 
00208 {
00209     USES_CONVERSION;
00210 
00211     CDialog::OnInitDialog();
00212     
00213     PRUnichar *curProfileName = nsnull;
00214 
00215     // Fill the list of profiles
00216     nsresult rv;
00217     nsCOMPtr<nsIProfile> profileService = 
00218              do_GetService(NS_PROFILE_CONTRACTID, &rv);
00219     profileService->GetCurrentProfile(&curProfileName);
00220 
00221     PRInt32     selectedRow = 0;
00222     PRUint32    listLen;
00223     PRUnichar   **profileList;
00224     rv = profileService->GetProfileList(&listLen, &profileList);
00225 
00226     for (PRUint32 index = 0; index < listLen; index++)
00227     {
00228         CString tmpStr(W2T(profileList[index]));
00229         m_ProfileList.AddString(tmpStr);
00230         if (wcscmp(profileList[index], curProfileName) == 0)
00231             selectedRow = index;
00232     }
00233     nsMemory::Free(curProfileName);
00234 
00235     m_ProfileList.SetCurSel(selectedRow);
00236 
00237     if (m_bAtStartUp)
00238     {
00239         GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
00240     }
00241     
00242     return TRUE;  // return TRUE unless you set the focus to a control
00243                   // EXCEPTION: OCX Property Pages should return FALSE
00244 }
00245 
00246 void CProfilesDlg::OnNewProfile() 
00247 {
00248     CNewProfileDlg dialog;
00249 
00250     if (dialog.DoModal() == IDOK)
00251     {
00252         nsresult rv;
00253 
00254         nsCOMPtr<nsIProfile> profileService = 
00255                  do_GetService(NS_PROFILE_CONTRACTID, &rv);
00256         ASSERT(NS_SUCCEEDED(rv));
00257         if (NS_SUCCEEDED(rv))
00258         {
00259             USES_CONVERSION;
00260 
00261             rv = profileService->CreateNewProfile(T2CW(dialog.m_Name), nsnull, nsnull, PR_FALSE);
00262             ASSERT(NS_SUCCEEDED(rv));
00263             if (NS_SUCCEEDED(rv))
00264             {
00265                 int item = m_ProfileList.AddString(dialog.m_Name);
00266                 m_ProfileList.SetCurSel(item);
00267                 GetDlgItem(IDOK)->EnableWindow(TRUE);
00268             }
00269         }
00270     }
00271 }
00272 
00273 void CProfilesDlg::OnRenameProfile() 
00274 {
00275     CRenameProfileDlg dialog;
00276 
00277     int itemIndex = m_ProfileList.GetCurSel();
00278     ASSERT(itemIndex != LB_ERR);
00279     if (itemIndex == LB_ERR)
00280         return;
00281 
00282     m_ProfileList.GetText(itemIndex, dialog.m_CurrentName);
00283 
00284     if (dialog.DoModal() == IDOK)
00285     {
00286         USES_CONVERSION;
00287 
00288         nsresult rv;
00289 
00290         nsCOMPtr<nsIProfile> profileService = 
00291                  do_GetService(NS_PROFILE_CONTRACTID, &rv);
00292         ASSERT(NS_SUCCEEDED(rv));
00293         if (NS_SUCCEEDED(rv))
00294         {
00295             rv = profileService->RenameProfile(T2CW(dialog.m_CurrentName), T2CW(dialog.m_NewName));
00296             ASSERT(NS_SUCCEEDED(rv));
00297         }
00298     }    
00299 }
00300 
00301 void CProfilesDlg::OnDeleteProfile() 
00302 {
00303     int itemIndex = m_ProfileList.GetCurSel();
00304     ASSERT(itemIndex != LB_ERR);
00305     if (itemIndex == LB_ERR)
00306         return;
00307 
00308     CString selectedProfile;
00309     m_ProfileList.GetText(itemIndex, selectedProfile);
00310     
00311     nsresult rv;
00312     nsCOMPtr<nsIProfile> profileService = 
00313              do_GetService(NS_PROFILE_CONTRACTID, &rv);
00314     ASSERT(NS_SUCCEEDED(rv));
00315     if (NS_SUCCEEDED(rv))
00316     {
00317         USES_CONVERSION;
00318 
00319         rv = profileService->DeleteProfile(T2CW(selectedProfile), PR_TRUE);
00320         ASSERT(NS_SUCCEEDED(rv));
00321         if (NS_SUCCEEDED(rv))
00322         {
00323             int itemCount = m_ProfileList.DeleteString(itemIndex);
00324             if (itemCount == 0)
00325                 GetDlgItem(IDOK)->EnableWindow(FALSE);
00326         }
00327     }    
00328 }
00329 
00330 void CProfilesDlg::OnDblclkProfile() 
00331 {
00332     OnOK();
00333 }