2 * Copyright (c) 2005 Alexander Gottwald
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name(s) of the above copyright
23 * holders shall not be used in advertising or otherwise to promote the sale,
24 * use or other dealings in this Software without prior written authorization.
27 #include "window/util.h"
31 const CLSID CLSID_DOMDocument40
= {0x88d969c0,0xf192,0x11d4,0xa6,0x5f,0x00,0x40,0x96,0x32,0x51,0xe5};
32 const CLSID CLSID_DOMDocument30
= {0xf5078f32,0xc551,0x11d3,0x89,0xb9,0x00,0x00,0xf8,0x1f,0xe2,0x21};
33 const IID IID_IXMLDOMDocument2
= {0x2933BF95,0x7B36,0x11d2,0xB2,0x0E,0x00,0xC0,0x4F,0x98,0x3E,0x60};
35 #define HRCALL(x, msg) if (FAILED(x)) { throw std::runtime_error("OLE Error:" msg " failed"); };
37 char *wcconvert(const wchar_t *wstr
)
39 int chars
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
41 throw win32_error("WideCharToMultiByte");
42 char *mbstr
= new char[chars
];
43 chars
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, mbstr
, chars
, NULL
, NULL
);
45 throw win32_error("WideCharToMultiByte");
49 wchar_t *mbconvert(const char *mbstr
)
51 int chars
= MultiByteToWideChar(CP_ACP
, 0, mbstr
, -1, NULL
, 0);
53 throw win32_error("MultiByteToWideChar");
54 wchar_t *wstr
= new wchar_t[chars
];
55 chars
= MultiByteToWideChar(CP_ACP
, 0, mbstr
, -1, wstr
, chars
);
57 throw win32_error("MultiByteToWideChar");
61 VARIANT
VariantString(const char *filename
)
64 wchar_t *str
= mbconvert(filename
);
68 V_BSTR(&var
) = SysAllocString(str
);
75 VARIANT
VariantString(const wchar_t *str
)
79 V_BSTR(&var
) = SysAllocString(str
);
84 IXMLDOMDocument2
*CreateDocument()
86 IXMLDOMDocument2
*doc
= NULL
;
90 HRCALL(CoCreateInstance(CLSID_DOMDocument40
, NULL
, CLSCTX_INPROC_SERVER
,
91 IID_IXMLDOMDocument2
, (void**)&doc
), "CoCreateInstance");
94 HRCALL(doc
->put_async(VARIANT_FALSE
), "put_async");
95 HRCALL(doc
->put_validateOnParse(VARIANT_FALSE
), "put_validateOnParse");
96 HRCALL(doc
->put_resolveExternals(VARIANT_FALSE
), "put_resolveExternals");
98 IXMLDOMProcessingInstruction
*pi
= NULL
;
99 IXMLDOMElement
*root
= NULL
;
100 BSTR xml
= SysAllocString(L
"xml");
101 BSTR ver
= SysAllocString(L
"version='1.0'");
102 HRCALL(doc
->createProcessingInstruction(xml
,ver
, &pi
),
103 "createProcessingInstruction");
104 HRCALL(doc
->appendChild(pi
, NULL
),
110 BSTR elemname
= SysAllocString(L
"XLaunch");
111 HRCALL(doc
->createElement(elemname
, &root
), "createElement");
112 HRCALL(doc
->appendChild(root
, NULL
), "appendChild");
113 SysFreeString(elemname
);
122 void setAttribute(IXMLDOMElement
*elem
, const wchar_t *name
, const wchar_t *value
)
124 BSTR str
= SysAllocString(name
);
125 VARIANT var
= VariantString(value
);
126 HRCALL(elem
->setAttribute(str
, var
), "setAttribute");
131 void setAttribute(IXMLDOMElement
*elem
, const wchar_t *name
, const char *value
)
133 wchar_t *wstr
= mbconvert(value
);
134 setAttribute(elem
, name
, wstr
);
139 void CConfig::Save(const char *filename
)
141 IXMLDOMDocument2
*doc
= CreateDocument();
142 IXMLDOMElement
*root
= NULL
;
144 HRCALL(doc
->get_documentElement(&root
), "get_documentElement");
149 setAttribute(root
, L
"WindowMode", L
"MultiWindow");
152 setAttribute(root
, L
"WindowMode", L
"Fullscreen");
156 setAttribute(root
, L
"WindowMode", L
"Windowed");
159 setAttribute(root
, L
"WindowMode", L
"Nodecoration");
166 setAttribute(root
, L
"ClientMode", L
"NoClient");
169 setAttribute(root
, L
"ClientMode", L
"StartProgram");
172 setAttribute(root
, L
"ClientMode", L
"XDMCP");
175 setAttribute(root
, L
"LocalClient", local
?L
"True":L
"False");
176 setAttribute(root
, L
"Display", display
.c_str());
177 setAttribute(root
, L
"Program", program
.c_str());
178 setAttribute(root
, L
"RemoteProtocol", protocol
.c_str());
179 setAttribute(root
, L
"RemoteHost", host
.c_str());
180 setAttribute(root
, L
"RemoteUser", user
.c_str());
181 setAttribute(root
, L
"XDMCPHost", xdmcp_host
.c_str());
182 setAttribute(root
, L
"XDMCPBroadcast", broadcast
?L
"True":L
"False");
183 setAttribute(root
, L
"XDMCPIndirect", indirect
?L
"True":L
"False");
184 setAttribute(root
, L
"Clipboard", clipboard
?L
"True":L
"False");
185 setAttribute(root
, L
"ExtraParams", extra_params
.c_str());
187 VARIANT var
= VariantString(filename
);
188 HRCALL(doc
->save(var
), "save");
196 BOOL
getAttribute(IXMLDOMElement
*elem
, const wchar_t *name
, std::string
&ret
)
199 HRCALL(elem
->getAttribute((OLECHAR
*)name
, &var
), "getAttribute");
200 if (V_VT(&var
) != VT_NULL
&& V_VT(&var
) == VT_BSTR
)
202 char *str
= wcconvert(V_BSTR(&var
));
210 BOOL
getAttributeBool(IXMLDOMElement
*elem
, const wchar_t *name
, bool &ret
)
213 if (getAttribute(elem
, name
, str
))
225 void CConfig::Load(const char *filename
)
227 IXMLDOMDocument2
*doc
= CreateDocument();
228 IXMLDOMElement
*root
= NULL
;
230 VARIANT var
= VariantString(filename
);
232 HRCALL(doc
->load(var
, &status
), "load");
235 if (status
== VARIANT_FALSE
)
241 HRCALL(doc
->get_documentElement(&root
), "get_documentElement");
243 std::string windowMode
;
244 std::string clientMode
;
246 if (getAttribute(root
, L
"WindowMode", windowMode
))
248 if (windowMode
== "MultiWindow")
249 window
= MultiWindow
;
250 else if (windowMode
== "Fullscreen")
252 else if (windowMode
== "Windowed")
254 else if (windowMode
== "Nodecoration")
255 window
= Nodecoration
;
257 if (getAttribute(root
, L
"ClientMode", clientMode
))
259 if (clientMode
== "NoClient")
261 else if (clientMode
== "StartProgram")
262 client
= StartProgram
;
263 else if (clientMode
== "XDMCP")
267 getAttributeBool(root
, L
"LocalClient", local
);
268 getAttribute(root
, L
"Display", display
);
269 getAttribute(root
, L
"Program", program
);
270 getAttribute(root
, L
"RemoteProtocol", protocol
);
271 getAttribute(root
, L
"RemoteHost", host
);
272 getAttribute(root
, L
"RemoteUser", user
);
273 getAttribute(root
, L
"XDMCPHost", xdmcp_host
);
274 getAttributeBool(root
, L
"XDMCPBroadcast", broadcast
);
275 getAttributeBool(root
, L
"XDMCPIndirect", indirect
);
276 getAttributeBool(root
, L
"Clipboard", clipboard
);
277 getAttribute(root
, L
"ExtraParams", extra_params
);