~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/embedding/tests/mfcembed/ProfilesDlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: Mozilla-sample-code 1.0
 
3
 *
 
4
 * Copyright (c) 2002 Netscape Communications Corporation and
 
5
 * other contributors
 
6
 *
 
7
 * Permission is hereby granted, free of charge, to any person obtaining a
 
8
 * copy of this Mozilla sample software and associated documentation files
 
9
 * (the "Software"), to deal in the Software without restriction, including
 
10
 * without limitation the rights to use, copy, modify, merge, publish,
 
11
 * distribute, sublicense, and/or sell copies of the Software, and to permit
 
12
 * persons to whom the Software is furnished to do so, subject to the
 
13
 * following conditions:
 
14
 *
 
15
 * The above copyright notice and this permission notice shall be included
 
16
 * in all copies or substantial portions of the Software.
 
17
 *
 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
19
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
21
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
24
 * DEALINGS IN THE SOFTWARE.
 
25
 *
 
26
 * Contributor(s):
 
27
 *
 
28
 * ***** END LICENSE BLOCK ***** */
 
29
 
 
30
// ProfilesDlg.cpp : implementation file
 
31
//
 
32
 
 
33
#include "stdafx.h"
 
34
#include <afxpriv.h>
 
35
#include "mfcembed.h"
 
36
#include "ProfilesDlg.h"
 
37
 
 
38
// Mozilla
 
39
#include "nsIProfile.h"
 
40
#include "nsIServiceManager.h"
 
41
 
 
42
#ifdef _DEBUG
 
43
#define new DEBUG_NEW
 
44
#undef THIS_FILE
 
45
static char THIS_FILE[] = __FILE__;
 
46
#endif
 
47
 
 
48
// Static Routines
 
49
static void ValidateProfileName(const CString& profileName, CDataExchange* pDX)
 
50
{
 
51
    USES_CONVERSION;
 
52
 
 
53
    nsresult rv;
 
54
    PRBool exists = FALSE;
 
55
 
 
56
    {
 
57
        nsCOMPtr<nsIProfile> profileService = 
 
58
                 do_GetService(NS_PROFILE_CONTRACTID, &rv);
 
59
        rv = profileService->ProfileExists(T2CW(profileName), &exists);
 
60
    }
 
61
 
 
62
    if (NS_SUCCEEDED(rv) && exists)
 
63
    {
 
64
        CString errMsg;
 
65
        errMsg.Format(_T("Error: A profile named \"%s\" already exists."), profileName);
 
66
        AfxMessageBox( errMsg, MB_ICONEXCLAMATION );
 
67
        errMsg.Empty();
 
68
        pDX->Fail();
 
69
    }
 
70
 
 
71
    if (profileName.FindOneOf(_T("\\/")) != -1)
 
72
    {
 
73
        AfxMessageBox( _T("Error: A profile name cannot contain the characters \"\\\" or \"/\"."), MB_ICONEXCLAMATION );
 
74
        pDX->Fail();
 
75
    }
 
76
}
 
77
 
 
78
/////////////////////////////////////////////////////////////////////////////
 
79
// CNewProfileDlg dialog
 
80
 
 
81
 
 
82
CNewProfileDlg::CNewProfileDlg(CWnd* pParent /*=NULL*/)
 
83
    : CDialog(CNewProfileDlg::IDD, pParent)
 
84
{
 
85
    //{{AFX_DATA_INIT(CNewProfileDlg)
 
86
    m_LocaleIndex = -1;
 
87
    m_Name = _T("");
 
88
    //}}AFX_DATA_INIT
 
89
}
 
90
 
 
91
 
 
92
void CNewProfileDlg::DoDataExchange(CDataExchange* pDX)
 
93
{
 
94
    CDialog::DoDataExchange(pDX);
 
95
    //{{AFX_DATA_MAP(CNewProfileDlg)
 
96
    DDX_CBIndex(pDX, IDC_LOCALE_COMBO, m_LocaleIndex);
 
97
    DDX_Text(pDX, IDC_NEW_PROF_NAME, m_Name);
 
98
    //}}AFX_DATA_MAP
 
99
 
 
100
    pDX->PrepareEditCtrl(IDC_NEW_PROF_NAME);
 
101
    if (pDX->m_bSaveAndValidate)
 
102
    {
 
103
        ValidateProfileName(m_Name, pDX);
 
104
    }
 
105
}
 
106
 
 
107
 
 
108
BEGIN_MESSAGE_MAP(CNewProfileDlg, CDialog)
 
109
    //{{AFX_MSG_MAP(CNewProfileDlg)
 
110
        // NOTE: the ClassWizard will add message map macros here
 
111
    //}}AFX_MSG_MAP
 
112
END_MESSAGE_MAP()
 
113
 
 
114
/////////////////////////////////////////////////////////////////////////////
 
115
// CNewProfileDlg message handlers
 
116
 
 
117
 
 
118
/////////////////////////////////////////////////////////////////////////////
 
119
// CRenameProfileDlg dialog
 
120
 
 
121
 
 
122
CRenameProfileDlg::CRenameProfileDlg(CWnd* pParent /*=NULL*/)
 
123
    : CDialog(CRenameProfileDlg::IDD, pParent)
 
124
{
 
125
    //{{AFX_DATA_INIT(CRenameProfileDlg)
 
126
    m_NewName = _T("");
 
127
    //}}AFX_DATA_INIT
 
128
}
 
129
 
 
130
 
 
131
void CRenameProfileDlg::DoDataExchange(CDataExchange* pDX)
 
132
{
 
133
    CDialog::DoDataExchange(pDX);
 
134
    //{{AFX_DATA_MAP(CRenameProfileDlg)
 
135
    DDX_Text(pDX, IDC_NEW_NAME, m_NewName);
 
136
    //}}AFX_DATA_MAP
 
137
 
 
138
    pDX->PrepareEditCtrl(IDC_NEW_NAME);
 
139
    if (pDX->m_bSaveAndValidate)
 
140
    {
 
141
        ValidateProfileName(m_NewName, pDX);
 
142
    }
 
143
}
 
144
 
 
145
 
 
146
BEGIN_MESSAGE_MAP(CRenameProfileDlg, CDialog)
 
147
    //{{AFX_MSG_MAP(CRenameProfileDlg)
 
148
        // NOTE: the ClassWizard will add message map macros here
 
149
    //}}AFX_MSG_MAP
 
150
END_MESSAGE_MAP()
 
151
 
 
152
/////////////////////////////////////////////////////////////////////////////
 
153
// CRenameProfileDlg message handlers
 
154
 
 
155
 
 
156
/////////////////////////////////////////////////////////////////////////////
 
157
// CProfilesDlg dialog
 
158
 
 
159
 
 
160
CProfilesDlg::CProfilesDlg(CWnd* pParent /*=NULL*/)
 
161
    : CDialog(CProfilesDlg::IDD, pParent)
 
162
{
 
163
    //{{AFX_DATA_INIT(CProfilesDlg)
 
164
    m_bAtStartUp = FALSE;
 
165
    m_bAskAtStartUp = FALSE;
 
166
    //}}AFX_DATA_INIT
 
167
}
 
168
 
 
169
 
 
170
void CProfilesDlg::DoDataExchange(CDataExchange* pDX)
 
171
{
 
172
    CDialog::DoDataExchange(pDX);
 
173
    //{{AFX_DATA_MAP(CProfilesDlg)
 
174
    DDX_Control(pDX, IDC_LIST1, m_ProfileList);
 
175
    DDX_Check(pDX, IDC_CHECK_ASK_AT_START, m_bAskAtStartUp);
 
176
    //}}AFX_DATA_MAP
 
177
 
 
178
    if (pDX->m_bSaveAndValidate)
 
179
    {
 
180
        USES_CONVERSION;
 
181
 
 
182
        int itemIndex = m_ProfileList.GetCurSel();
 
183
        if (itemIndex != LB_ERR)
 
184
        {
 
185
            CString itemText;
 
186
            m_ProfileList.GetText(itemIndex, itemText);
 
187
            m_SelectedProfile.Assign(T2CW(itemText));
 
188
        }
 
189
    }
 
190
}
 
191
 
 
192
 
 
193
BEGIN_MESSAGE_MAP(CProfilesDlg, CDialog)
 
194
    //{{AFX_MSG_MAP(CProfilesDlg)
 
195
    ON_BN_CLICKED(IDC_PROF_NEW, OnNewProfile)
 
196
    ON_BN_CLICKED(IDC_PROF_RENAME, OnRenameProfile)
 
197
    ON_BN_CLICKED(IDC_PROF_DELETE, OnDeleteProfile)
 
198
    ON_LBN_DBLCLK(IDC_LIST1, OnDblclkProfile)
 
199
    //}}AFX_MSG_MAP
 
200
END_MESSAGE_MAP()
 
201
 
 
202
/////////////////////////////////////////////////////////////////////////////
 
203
// CProfilesDlg message handlers
 
204
 
 
205
BOOL CProfilesDlg::OnInitDialog() 
 
206
{
 
207
    USES_CONVERSION;
 
208
 
 
209
    CDialog::OnInitDialog();
 
210
    
 
211
    nsCAutoString   cStr;        
 
212
    nsXPIDLString   curProfileName;
 
213
 
 
214
    // Fill the list of profiles
 
215
    nsresult rv;
 
216
    nsCOMPtr<nsIProfile> profileService = 
 
217
             do_GetService(NS_PROFILE_CONTRACTID, &rv);
 
218
    profileService->GetCurrentProfile(getter_Copies(curProfileName));
 
219
 
 
220
    PRInt32     selectedRow = 0;
 
221
    PRUint32    listLen;
 
222
    PRUnichar   **profileList;
 
223
    rv = profileService->GetProfileList(&listLen, &profileList);
 
224
 
 
225
    for (PRUint32 index = 0; index < listLen; index++)
 
226
    {
 
227
        CString tmpStr(W2T(profileList[index]));
 
228
        m_ProfileList.AddString(tmpStr);
 
229
        if (wcscmp(profileList[index], curProfileName.get()) == 0)
 
230
            selectedRow = index;
 
231
    }
 
232
 
 
233
    m_ProfileList.SetCurSel(selectedRow);
 
234
 
 
235
    if (m_bAtStartUp)
 
236
    {
 
237
        GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
 
238
    }
 
239
    
 
240
    return TRUE;  // return TRUE unless you set the focus to a control
 
241
                  // EXCEPTION: OCX Property Pages should return FALSE
 
242
}
 
243
 
 
244
void CProfilesDlg::OnNewProfile() 
 
245
{
 
246
    CNewProfileDlg dialog;
 
247
 
 
248
    if (dialog.DoModal() == IDOK)
 
249
    {
 
250
        nsresult rv;
 
251
 
 
252
        nsCOMPtr<nsIProfile> profileService = 
 
253
                 do_GetService(NS_PROFILE_CONTRACTID, &rv);
 
254
        ASSERT(NS_SUCCEEDED(rv));
 
255
        if (NS_SUCCEEDED(rv))
 
256
        {
 
257
            USES_CONVERSION;
 
258
 
 
259
            rv = profileService->CreateNewProfile(T2CW(dialog.m_Name), nsnull, nsnull, PR_FALSE);
 
260
            ASSERT(NS_SUCCEEDED(rv));
 
261
            if (NS_SUCCEEDED(rv))
 
262
            {
 
263
                int item = m_ProfileList.AddString(dialog.m_Name);
 
264
                m_ProfileList.SetCurSel(item);
 
265
                GetDlgItem(IDOK)->EnableWindow(TRUE);
 
266
            }
 
267
        }
 
268
    }
 
269
}
 
270
 
 
271
void CProfilesDlg::OnRenameProfile() 
 
272
{
 
273
    CRenameProfileDlg dialog;
 
274
 
 
275
    int itemIndex = m_ProfileList.GetCurSel();
 
276
    ASSERT(itemIndex != LB_ERR);
 
277
    if (itemIndex == LB_ERR)
 
278
        return;
 
279
 
 
280
    m_ProfileList.GetText(itemIndex, dialog.m_CurrentName);
 
281
 
 
282
    if (dialog.DoModal() == IDOK)
 
283
    {
 
284
        USES_CONVERSION;
 
285
 
 
286
        nsresult rv;
 
287
 
 
288
        nsCOMPtr<nsIProfile> profileService = 
 
289
                 do_GetService(NS_PROFILE_CONTRACTID, &rv);
 
290
        ASSERT(NS_SUCCEEDED(rv));
 
291
        if (NS_SUCCEEDED(rv))
 
292
        {
 
293
            rv = profileService->RenameProfile(T2CW(dialog.m_CurrentName), T2CW(dialog.m_NewName));
 
294
            ASSERT(NS_SUCCEEDED(rv));
 
295
        }
 
296
    }    
 
297
}
 
298
 
 
299
void CProfilesDlg::OnDeleteProfile() 
 
300
{
 
301
    int itemIndex = m_ProfileList.GetCurSel();
 
302
    ASSERT(itemIndex != LB_ERR);
 
303
    if (itemIndex == LB_ERR)
 
304
        return;
 
305
 
 
306
    CString selectedProfile;
 
307
    m_ProfileList.GetText(itemIndex, selectedProfile);
 
308
    
 
309
    nsresult rv;
 
310
    nsCOMPtr<nsIProfile> profileService = 
 
311
             do_GetService(NS_PROFILE_CONTRACTID, &rv);
 
312
    ASSERT(NS_SUCCEEDED(rv));
 
313
    if (NS_SUCCEEDED(rv))
 
314
    {
 
315
        USES_CONVERSION;
 
316
 
 
317
        rv = profileService->DeleteProfile(T2CW(selectedProfile), PR_TRUE);
 
318
        ASSERT(NS_SUCCEEDED(rv));
 
319
        if (NS_SUCCEEDED(rv))
 
320
        {
 
321
            int itemCount = m_ProfileList.DeleteString(itemIndex);
 
322
            if (itemCount == 0)
 
323
                GetDlgItem(IDOK)->EnableWindow(FALSE);
 
324
        }
 
325
    }    
 
326
}
 
327
 
 
328
void CProfilesDlg::OnDblclkProfile() 
 
329
{
 
330
    OnOK();
 
331
}