Back to index

lightning-sunbird  0.9+nobinonly
ProfilesDlg.cpp
Go to the documentation of this file.
00001 // ProfilesDlg.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include <afxpriv.h>
00006 #include "Testembed.h"
00007 #include "ProfilesDlg.h"
00008 
00009 // Mozilla
00010 #include "nsIProfile.h"
00011 #include "nsIServiceManager.h"
00012 
00013 #ifdef _DEBUG
00014 #define new DEBUG_NEW
00015 #undef THIS_FILE
00016 static char THIS_FILE[] = __FILE__;
00017 #endif
00018 
00019 // Static Routines
00020 static void ValidateProfileName(const CString& profileName, CDataExchange* pDX)
00021 {
00022     USES_CONVERSION;
00023 
00024     nsresult rv;
00025     PRBool exists = FALSE;
00026 
00027     {
00028         //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00029            nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID));
00030         rv = profileService->ProfileExists(T2W(profileName), &exists);
00031     }
00032 
00033     if (NS_SUCCEEDED(rv) && exists)
00034     {
00035         CString errMsg;
00036 
00037         errMsg.Format(_T("Error: A profile named \"%s\" already exists."), (const char *)profileName);
00038         AfxMessageBox( errMsg, MB_ICONEXCLAMATION );
00039         errMsg.Empty();
00040         pDX->Fail();
00041     }
00042 
00043     if (profileName.FindOneOf("\\/") != -1)
00044     {
00045         AfxMessageBox( _T("Error: A profile name cannot contain the characters \"\\\" or \"/\"."), MB_ICONEXCLAMATION );
00046         pDX->Fail();
00047     }
00048 }
00049 
00051 // CNewProfileDlg dialog
00052 
00053 
00054 CNewProfileDlg::CNewProfileDlg(CWnd* pParent /*=NULL*/)
00055        : CDialog(CNewProfileDlg::IDD, pParent)
00056 {
00057        //{{AFX_DATA_INIT(CNewProfileDlg)
00058        m_LocaleIndex = -1;
00059        m_Name = _T("");
00060        //}}AFX_DATA_INIT
00061 }
00062 
00063 
00064 void CNewProfileDlg::DoDataExchange(CDataExchange* pDX)
00065 {
00066        CDialog::DoDataExchange(pDX);
00067        //{{AFX_DATA_MAP(CNewProfileDlg)
00068        DDX_CBIndex(pDX, IDC_LOCALE_COMBO, m_LocaleIndex);
00069        DDX_Text(pDX, IDC_NEW_PROF_NAME, m_Name);
00070        //}}AFX_DATA_MAP
00071 
00072     pDX->PrepareEditCtrl(IDC_NEW_PROF_NAME);
00073     if (pDX->m_bSaveAndValidate)
00074     {
00075         ValidateProfileName(m_Name, pDX);
00076     }
00077 }
00078 
00079 
00080 BEGIN_MESSAGE_MAP(CNewProfileDlg, CDialog)
00081        //{{AFX_MSG_MAP(CNewProfileDlg)
00082               // NOTE: the ClassWizard will add message map macros here
00083        //}}AFX_MSG_MAP
00084 END_MESSAGE_MAP()
00085 
00087 // CNewProfileDlg message handlers
00088 
00089 
00091 // CRenameProfileDlg dialog
00092 
00093 
00094 CRenameProfileDlg::CRenameProfileDlg(CWnd* pParent /*=NULL*/)
00095        : CDialog(CRenameProfileDlg::IDD, pParent)
00096 {
00097        //{{AFX_DATA_INIT(CRenameProfileDlg)
00098        m_NewName = _T("");
00099        //}}AFX_DATA_INIT
00100 }
00101 
00102 
00103 void CRenameProfileDlg::DoDataExchange(CDataExchange* pDX)
00104 {
00105        CDialog::DoDataExchange(pDX);
00106        //{{AFX_DATA_MAP(CRenameProfileDlg)
00107        DDX_Text(pDX, IDC_NEW_NAME, m_NewName);
00108        //}}AFX_DATA_MAP
00109 
00110     pDX->PrepareEditCtrl(IDC_NEW_NAME);
00111     if (pDX->m_bSaveAndValidate)
00112     {
00113         ValidateProfileName(m_NewName, pDX);
00114     }
00115 }
00116 
00117 
00118 BEGIN_MESSAGE_MAP(CRenameProfileDlg, CDialog)
00119        //{{AFX_MSG_MAP(CRenameProfileDlg)
00120               // NOTE: the ClassWizard will add message map macros here
00121        //}}AFX_MSG_MAP
00122 END_MESSAGE_MAP()
00123 
00125 // CRenameProfileDlg message handlers
00126 
00127 
00129 // CProfilesDlg dialog
00130 
00131 
00132 CProfilesDlg::CProfilesDlg(CWnd* pParent /*=NULL*/)
00133        : CDialog(CProfilesDlg::IDD, pParent)
00134 {
00135        //{{AFX_DATA_INIT(CProfilesDlg)
00136     m_bAtStartUp = TRUE;
00137        m_bAskAtStartUp = TRUE;
00138        //}}AFX_DATA_INIT
00139 }
00140 
00141 
00142 void CProfilesDlg::DoDataExchange(CDataExchange* pDX)
00143 {
00144        CDialog::DoDataExchange(pDX);
00145        //{{AFX_DATA_MAP(CProfilesDlg)
00146        DDX_Control(pDX, IDC_LIST1, m_ProfileList);
00147        DDX_Check(pDX, IDC_CHECK_ASK_AT_START, m_bAskAtStartUp);
00148        //}}AFX_DATA_MAP
00149 
00150     if (pDX->m_bSaveAndValidate)
00151     {
00152         USES_CONVERSION;
00153 
00154         int itemIndex = m_ProfileList.GetCurSel();
00155         if (itemIndex != LB_ERR)
00156         {
00157             CString itemText;
00158             m_ProfileList.GetText(itemIndex, itemText);
00159             m_SelectedProfile.Assign(T2W(itemText));
00160         }
00161     }
00162 }
00163 
00164 
00165 BEGIN_MESSAGE_MAP(CProfilesDlg, CDialog)
00166        //{{AFX_MSG_MAP(CProfilesDlg)
00167        ON_BN_CLICKED(IDC_PROF_NEW, OnNewProfile)
00168        ON_BN_CLICKED(IDC_PROF_RENAME, OnRenameProfile)
00169        ON_BN_CLICKED(IDC_PROF_DELETE, OnDeleteProfile)
00170        //}}AFX_MSG_MAP
00171 END_MESSAGE_MAP()
00172 
00174 // CProfilesDlg message handlers
00175 
00176 BOOL CProfilesDlg::OnInitDialog() 
00177 {
00178     USES_CONVERSION;
00179 
00180        CDialog::OnInitDialog();
00181        
00182     nsCAutoString   cStr;        
00183     nsXPIDLString   curProfileName;
00184 
00185     // Fill the list of profiles
00186     nsresult rv;
00187     //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00188     nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID));
00189     profileService->GetCurrentProfile(getter_Copies(curProfileName));
00190 
00191     PRInt32     selectedRow = 0;
00192     PRUint32    listLen;
00193     PRUnichar   **profileList;
00194     rv = profileService->GetProfileList(&listLen, &profileList);
00195 
00196     for (PRUint32 index = 0; index < listLen; index++)
00197     {
00198         CString tmpStr(W2T(profileList[index]));
00199         m_ProfileList.AddString(tmpStr);
00200         if (nsCRT::strcmp(profileList[index], curProfileName.get()) == 0)
00201             selectedRow = index;
00202     }
00203 
00204     m_ProfileList.SetCurSel(selectedRow);
00205 
00206        if (m_bAtStartUp)
00207     {
00208         GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
00209     }
00210        
00211        return TRUE;  // return TRUE unless you set the focus to a control
00212                      // EXCEPTION: OCX Property Pages should return FALSE
00213 }
00214 
00215 void CProfilesDlg::OnNewProfile() 
00216 {
00217        CNewProfileDlg dialog;
00218 
00219     if (dialog.DoModal() == IDOK)
00220     {
00221         nsresult rv;
00222 
00223         //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00224            nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
00225         ASSERT(NS_SUCCEEDED(rv));
00226         if (NS_SUCCEEDED(rv))
00227         {
00228             USES_CONVERSION;
00229 
00230                   rv = profileService->CreateNewProfile(T2W(dialog.m_Name), nsnull, nsnull, PR_FALSE);
00231             ASSERT(NS_SUCCEEDED(rv));
00232             if (NS_SUCCEEDED(rv))
00233             {
00234                 int item = m_ProfileList.AddString(dialog.m_Name);
00235                 m_ProfileList.SetCurSel(item);
00236                 GetDlgItem(IDOK)->EnableWindow(TRUE);
00237             }
00238         }
00239     }
00240 }
00241 
00242 void CProfilesDlg::OnRenameProfile() 
00243 {
00244        CRenameProfileDlg dialog;
00245 
00246     int itemIndex = m_ProfileList.GetCurSel();
00247     ASSERT(itemIndex != LB_ERR);
00248     if (itemIndex == LB_ERR)
00249         return;
00250 
00251     m_ProfileList.GetText(itemIndex, dialog.m_CurrentName);
00252 
00253     if (dialog.DoModal() == IDOK)
00254     {
00255         USES_CONVERSION;
00256 
00257         nsresult rv;
00258 
00259         //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00260            nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
00261         ASSERT(NS_SUCCEEDED(rv));
00262         if (NS_SUCCEEDED(rv))
00263         {
00264             rv = profileService->RenameProfile(T2W(dialog.m_CurrentName), T2W(dialog.m_NewName));
00265             ASSERT(NS_SUCCEEDED(rv));
00266         }
00267     }  
00268 }
00269 
00270 void CProfilesDlg::OnDeleteProfile() 
00271 {
00272     int itemIndex = m_ProfileList.GetCurSel();
00273 
00274     ASSERT(itemIndex != LB_ERR);
00275     if (itemIndex == LB_ERR)
00276         return;
00277 
00278     CString selectedProfile;
00279     m_ProfileList.GetText(itemIndex, selectedProfile);
00280     
00281     nsresult rv;
00282     //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00283     nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
00284 
00285        ASSERT(NS_SUCCEEDED(rv));
00286     if (NS_SUCCEEDED(rv))
00287     {
00288         USES_CONVERSION;
00289 
00290            rv = profileService->DeleteProfile(T2W(selectedProfile), PR_TRUE);
00291         ASSERT(NS_SUCCEEDED(rv));
00292         if (NS_SUCCEEDED(rv))
00293         {
00294             int itemCount = m_ProfileList.DeleteString(itemIndex);
00295             if (itemCount == 0)
00296                 GetDlgItem(IDOK)->EnableWindow(FALSE);
00297         }
00298     }  
00299 }