1 // ProfilesDlg.cpp : implementation file
7 #include "ProfilesDlg.h"
10 #include "nsIProfile.h"
11 #include "nsIServiceManager.h"
16 static char THIS_FILE
[] = __FILE__
;
20 static void ValidateProfileName(const CString
& profileName
, CDataExchange
* pDX
)
25 PRBool exists
= FALSE
;
28 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
29 nsCOMPtr
<nsIProfile
> profileService(do_GetService(NS_PROFILE_CONTRACTID
));
30 rv
= profileService
->ProfileExists(T2W(profileName
), &exists
);
33 if (NS_SUCCEEDED(rv
) && exists
)
37 errMsg
.Format(_T("Error: A profile named \"%s\" already exists."), (const char *)profileName
);
38 AfxMessageBox( errMsg
, MB_ICONEXCLAMATION
);
43 if (profileName
.FindOneOf("\\/") != -1)
45 AfxMessageBox( _T("Error: A profile name cannot contain the characters \"\\\" or \"/\"."), MB_ICONEXCLAMATION
);
50 /////////////////////////////////////////////////////////////////////////////
51 // CNewProfileDlg dialog
54 CNewProfileDlg::CNewProfileDlg(CWnd
* pParent
/*=NULL*/)
55 : CDialog(CNewProfileDlg::IDD
, pParent
)
57 //{{AFX_DATA_INIT(CNewProfileDlg)
64 void CNewProfileDlg::DoDataExchange(CDataExchange
* pDX
)
66 CDialog::DoDataExchange(pDX
);
67 //{{AFX_DATA_MAP(CNewProfileDlg)
68 DDX_CBIndex(pDX
, IDC_LOCALE_COMBO
, m_LocaleIndex
);
69 DDX_Text(pDX
, IDC_NEW_PROF_NAME
, m_Name
);
72 pDX
->PrepareEditCtrl(IDC_NEW_PROF_NAME
);
73 if (pDX
->m_bSaveAndValidate
)
75 ValidateProfileName(m_Name
, pDX
);
80 BEGIN_MESSAGE_MAP(CNewProfileDlg
, CDialog
)
81 //{{AFX_MSG_MAP(CNewProfileDlg)
82 // NOTE: the ClassWizard will add message map macros here
86 /////////////////////////////////////////////////////////////////////////////
87 // CNewProfileDlg message handlers
90 /////////////////////////////////////////////////////////////////////////////
91 // CRenameProfileDlg dialog
94 CRenameProfileDlg::CRenameProfileDlg(CWnd
* pParent
/*=NULL*/)
95 : CDialog(CRenameProfileDlg::IDD
, pParent
)
97 //{{AFX_DATA_INIT(CRenameProfileDlg)
103 void CRenameProfileDlg::DoDataExchange(CDataExchange
* pDX
)
105 CDialog::DoDataExchange(pDX
);
106 //{{AFX_DATA_MAP(CRenameProfileDlg)
107 DDX_Text(pDX
, IDC_NEW_NAME
, m_NewName
);
110 pDX
->PrepareEditCtrl(IDC_NEW_NAME
);
111 if (pDX
->m_bSaveAndValidate
)
113 ValidateProfileName(m_NewName
, pDX
);
118 BEGIN_MESSAGE_MAP(CRenameProfileDlg
, CDialog
)
119 //{{AFX_MSG_MAP(CRenameProfileDlg)
120 // NOTE: the ClassWizard will add message map macros here
124 /////////////////////////////////////////////////////////////////////////////
125 // CRenameProfileDlg message handlers
128 /////////////////////////////////////////////////////////////////////////////
129 // CProfilesDlg dialog
132 CProfilesDlg::CProfilesDlg(CWnd
* pParent
/*=NULL*/)
133 : CDialog(CProfilesDlg::IDD
, pParent
)
135 //{{AFX_DATA_INIT(CProfilesDlg)
137 m_bAskAtStartUp
= TRUE
;
142 void CProfilesDlg::DoDataExchange(CDataExchange
* pDX
)
144 CDialog::DoDataExchange(pDX
);
145 //{{AFX_DATA_MAP(CProfilesDlg)
146 DDX_Control(pDX
, IDC_LIST1
, m_ProfileList
);
147 DDX_Check(pDX
, IDC_CHECK_ASK_AT_START
, m_bAskAtStartUp
);
150 if (pDX
->m_bSaveAndValidate
)
154 int itemIndex
= m_ProfileList
.GetCurSel();
155 if (itemIndex
!= LB_ERR
)
158 m_ProfileList
.GetText(itemIndex
, itemText
);
159 m_SelectedProfile
.Assign(T2W(itemText
));
165 BEGIN_MESSAGE_MAP(CProfilesDlg
, CDialog
)
166 //{{AFX_MSG_MAP(CProfilesDlg)
167 ON_BN_CLICKED(IDC_PROF_NEW
, OnNewProfile
)
168 ON_BN_CLICKED(IDC_PROF_RENAME
, OnRenameProfile
)
169 ON_BN_CLICKED(IDC_PROF_DELETE
, OnDeleteProfile
)
173 /////////////////////////////////////////////////////////////////////////////
174 // CProfilesDlg message handlers
176 BOOL
CProfilesDlg::OnInitDialog()
180 CDialog::OnInitDialog();
183 nsXPIDLString curProfileName
;
185 // Fill the list of profiles
187 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
188 nsCOMPtr
<nsIProfile
> profileService(do_GetService(NS_PROFILE_CONTRACTID
));
189 profileService
->GetCurrentProfile(getter_Copies(curProfileName
));
191 PRInt32 selectedRow
= 0;
193 PRUnichar
**profileList
;
194 rv
= profileService
->GetProfileList(&listLen
, &profileList
);
196 for (PRUint32 index
= 0; index
< listLen
; index
++)
198 CString
tmpStr(W2T(profileList
[index
]));
199 m_ProfileList
.AddString(tmpStr
);
200 if (nsCRT::strcmp(profileList
[index
], curProfileName
.get()) == 0)
204 m_ProfileList
.SetCurSel(selectedRow
);
208 GetDlgItem(IDCANCEL
)->EnableWindow(FALSE
);
211 return TRUE
; // return TRUE unless you set the focus to a control
212 // EXCEPTION: OCX Property Pages should return FALSE
215 void CProfilesDlg::OnNewProfile()
217 CNewProfileDlg dialog
;
219 if (dialog
.DoModal() == IDOK
)
223 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
224 nsCOMPtr
<nsIProfile
> profileService(do_GetService(NS_PROFILE_CONTRACTID
,&rv
));
225 ASSERT(NS_SUCCEEDED(rv
));
226 if (NS_SUCCEEDED(rv
))
230 rv
= profileService
->CreateNewProfile(T2W(dialog
.m_Name
), nsnull
, nsnull
, PR_FALSE
);
231 ASSERT(NS_SUCCEEDED(rv
));
232 if (NS_SUCCEEDED(rv
))
234 int item
= m_ProfileList
.AddString(dialog
.m_Name
);
235 m_ProfileList
.SetCurSel(item
);
236 GetDlgItem(IDOK
)->EnableWindow(TRUE
);
242 void CProfilesDlg::OnRenameProfile()
244 CRenameProfileDlg dialog
;
246 int itemIndex
= m_ProfileList
.GetCurSel();
247 ASSERT(itemIndex
!= LB_ERR
);
248 if (itemIndex
== LB_ERR
)
251 m_ProfileList
.GetText(itemIndex
, dialog
.m_CurrentName
);
253 if (dialog
.DoModal() == IDOK
)
259 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
260 nsCOMPtr
<nsIProfile
> profileService(do_GetService(NS_PROFILE_CONTRACTID
,&rv
));
261 ASSERT(NS_SUCCEEDED(rv
));
262 if (NS_SUCCEEDED(rv
))
264 rv
= profileService
->RenameProfile(T2W(dialog
.m_CurrentName
), T2W(dialog
.m_NewName
));
265 ASSERT(NS_SUCCEEDED(rv
));
270 void CProfilesDlg::OnDeleteProfile()
272 int itemIndex
= m_ProfileList
.GetCurSel();
274 ASSERT(itemIndex
!= LB_ERR
);
275 if (itemIndex
== LB_ERR
)
278 CString selectedProfile
;
279 m_ProfileList
.GetText(itemIndex
, selectedProfile
);
282 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
283 nsCOMPtr
<nsIProfile
> profileService(do_GetService(NS_PROFILE_CONTRACTID
,&rv
));
285 ASSERT(NS_SUCCEEDED(rv
));
286 if (NS_SUCCEEDED(rv
))
290 rv
= profileService
->DeleteProfile(T2W(selectedProfile
), PR_TRUE
);
291 ASSERT(NS_SUCCEEDED(rv
));
292 if (NS_SUCCEEDED(rv
))
294 int itemCount
= m_ProfileList
.DeleteString(itemIndex
);
296 GetDlgItem(IDOK
)->EnableWindow(FALSE
);