/
ImportXMLdlg.cpp
266 lines (213 loc) · 6.97 KB
/
ImportXMLdlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// ImportXMLdlg.cpp : implementation file
//
#include "stdafx.h"
#include "..\mushclient.h"
#include "plugins\PluginsDlg.h"
#include "ImportXMLdlg.h"
#include "..\doc.h"
#ifdef _DEBUG
//#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImportXMLdlg dialog
CImportXMLdlg::CImportXMLdlg(CWnd* pParent /*=NULL*/)
: CDialog(CImportXMLdlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CImportXMLdlg)
m_bGeneral = FALSE;
m_bTriggers = FALSE;
m_bAliases = FALSE;
m_bTimers = FALSE;
m_bMacros = FALSE;
m_bVariables = FALSE;
m_bColours = FALSE;
m_bKeypad = FALSE;
m_bPrinting = FALSE;
//}}AFX_DATA_INIT
m_pDoc = NULL;
}
void CImportXMLdlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CImportXMLdlg)
DDX_Control(pDX, IDC_PRINTING, m_ctlPrinting);
DDX_Control(pDX, IDC_KEYPAD, m_ctlKeypad);
DDX_Control(pDX, IDC_COLOURS, m_ctlColours);
DDX_Control(pDX, IDC_VARIABLES, m_ctlVariables);
DDX_Control(pDX, IDC_MACROS, m_ctlMacros);
DDX_Control(pDX, IDC_TIMERS, m_ctlTimers);
DDX_Control(pDX, IDC_ALIASES, m_ctlAliases);
DDX_Control(pDX, IDC_TRIGGERS, m_ctlTriggers);
DDX_Control(pDX, IDC_GENERAL, m_ctlGeneral);
DDX_Check(pDX, IDC_GENERAL, m_bGeneral);
DDX_Check(pDX, IDC_TRIGGERS, m_bTriggers);
DDX_Check(pDX, IDC_ALIASES, m_bAliases);
DDX_Check(pDX, IDC_TIMERS, m_bTimers);
DDX_Check(pDX, IDC_MACROS, m_bMacros);
DDX_Check(pDX, IDC_VARIABLES, m_bVariables);
DDX_Check(pDX, IDC_COLOURS, m_bColours);
DDX_Check(pDX, IDC_KEYPAD, m_bKeypad);
DDX_Check(pDX, IDC_PRINTING, m_bPrinting);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CImportXMLdlg, CDialog)
//{{AFX_MSG_MAP(CImportXMLdlg)
ON_BN_CLICKED(IDOK, OnOk)
ON_BN_CLICKED(IDC_CLIPBOARD, OnClipboard)
ON_BN_CLICKED(IDC_PLUGINS_LIST, OnPluginsList)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
ON_UPDATE_COMMAND_UI(IDC_CLIPBOARD, OnUpdateNeedXMLClipboard)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImportXMLdlg message handlers
void CImportXMLdlg::OnOk()
{
CFileDialog filedlg (TRUE, // loading the file
"", // default extension
"", // suggested name
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
"All files (*.*)|*.*|"
"XML files (*.xml)|*.xml|"
"Text files (*.txt)|*.txt|"
"MUSHclient worlds (*.mcl)|*.mcl|"
"MUSHclient triggers (*.mct)|*.mct|"
"MUSHclient aliases (*.mca)|*.mca|"
"MUSHclient colours (*.mcc)|*.mcc|"
"MUSHclient macros (*.mcm)|*.mcm|"
"MUSHclient timers (*.mci)|*.mci|"
"|", // filter
this); // parent window
filedlg.m_ofn.lpstrTitle = "Import";
ChangeToFileBrowsingDirectory ();
int nResult = filedlg.DoModal();
ChangeToStartupDirectory ();
if (nResult != IDOK)
return; // cancelled dialog
CFile * f = NULL;
CArchive * ar = NULL;
try
{
f = new CFile (filedlg.GetPathName (), CFile::modeRead | CFile::shareDenyWrite);
ar = new CArchive(f, CArchive::load);
ImportArchive (*ar);
} // end of try block
catch (CFileException * e)
{
::UMessageBox (TFormat ("Unable to open or read %s",
(LPCTSTR) filedlg.GetPathName ()), MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a file exception
delete ar; // delete archive
delete f; // delete file
}
void CImportXMLdlg::OnClipboard()
{
CString strContents;
if (!GetClipboardContents (strContents, CF_TEXT, false))
return; // can't do it
CMemFile f ((unsigned char *) (const char *) strContents, strContents.GetLength ());
CArchive ar (&f, CArchive::load);
ImportArchive (ar);
}
#define ADDCOUNT(count, name, plural) \
if (count) \
{ \
if (!strCounts.IsEmpty ()) \
strCounts += ", "; \
strCounts += CFormat ("%lu " name "%s", plural (count)); \
}
void CImportXMLdlg::ImportArchive (CArchive & ar)
{
try
{
if (IsArchiveXML (ar))
{
unsigned long iMask = 0;
// work out what to import
if (m_ctlGeneral.GetCheck ())
iMask |= XML_GENERAL;
if (m_ctlTriggers.GetCheck ())
iMask |= XML_TRIGGERS;
if (m_ctlAliases.GetCheck ())
iMask |= XML_ALIASES;
if (m_ctlTimers.GetCheck ())
iMask |= XML_TIMERS;
if (m_ctlMacros.GetCheck ())
iMask |= XML_MACROS;
if (m_ctlVariables.GetCheck ())
iMask |= XML_VARIABLES;
if (m_ctlColours.GetCheck ())
iMask |= XML_COLOURS;
if (m_ctlKeypad.GetCheck ())
iMask |= XML_KEYPAD;
if (m_ctlPrinting.GetCheck ())
iMask |= XML_PRINTING;
UINT iTriggers = 0;
UINT iAliases = 0;
UINT iTimers = 0;
UINT iMacros = 0;
UINT iVariables = 0;
UINT iColours = 0;
UINT iKeypad = 0;
UINT iPrinting = 0;
// do it
m_pDoc->Load_World_XML (ar,
iMask | XML_NO_PLUGINS,
0, // load flags
&iTriggers,
&iAliases,
&iTimers,
&iMacros,
&iVariables,
&iColours,
&iKeypad,
&iPrinting);
CString strCounts = TFormat (
"%lu trigger%s, "
"%lu alias%s, "
"%lu timer%s, "
"%lu macro%s, "
"%lu variable%s, "
"%lu colour%s, "
"%lu keypad%s, "
"%lu printing style%s loaded. ",
PLURAL (iTriggers),
PLURALES (iAliases),
PLURAL (iTimers),
PLURAL (iMacros),
PLURAL (iVariables),
PLURAL (iColours),
PLURAL (iKeypad),
PLURAL (iPrinting));
::UMessageBox (strCounts, MB_ICONINFORMATION);
m_pDoc->SetModifiedFlag (TRUE); // document has now changed
CDialog::OnOK();
}
else
::TMessageBox ("Not in XML format");
} // end of try block
catch (CArchiveException* e)
{
::TMessageBox ("There was a problem parsing the XML. "
"See the error window for more details");
e->Delete ();
}
} // end of CImportXMLdlg::ImportArchive
void CImportXMLdlg::OnUpdateNeedXMLClipboard(CCmdUI* pCmdUI)
{
pCmdUI->Enable (IsClipboardXML ());
} // end of CImportXMLdlg::OnUpdateNeedXMLClipboard
LRESULT CImportXMLdlg::OnKickIdle(WPARAM, LPARAM)
{
UpdateDialogControls (AfxGetApp()->m_pMainWnd, false);
return 0;
} // end of CImportXMLdlg::OnKickIdle
void CImportXMLdlg::OnPluginsList()
{
CPluginsDlg dlg;
dlg.m_pDoc = m_pDoc;
dlg.DoModal ();
}