b53c5113fc3631f718fa5a632deb3bac862d345e
[vlc.git] / projects / activex / persistpropbag.cpp
1 /*****************************************************************************
2  * persistpropbag.cpp: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #include "plugin.h"
24 #include "persistpropbag.h"
25
26 #include "utils.h"
27 #include "oleobject.h"
28
29 using namespace std;
30
31 STDMETHODIMP VLCPersistPropertyBag::GetClassID(LPCLSID pClsID)
32 {
33     if( NULL == pClsID )
34         return E_POINTER;
35
36     *pClsID = _p_instance->getClassID();
37
38     return S_OK;
39 };
40
41 STDMETHODIMP VLCPersistPropertyBag::InitNew(void)
42 {
43     return _p_instance->onInit();
44 };
45
46 STDMETHODIMP VLCPersistPropertyBag::Load(LPPROPERTYBAG pPropBag, LPERRORLOG pErrorLog)
47 {
48     HRESULT hr = _p_instance->onInit();
49     if( FAILED(hr) )
50         return hr;
51
52     if( NULL == pPropBag )
53         return E_INVALIDARG;
54
55     VARIANT value;
56
57     V_VT(&value) = VT_BSTR;
58     if( S_OK == pPropBag->Read(OLESTR("mrl"), &value, pErrorLog) )
59     {
60         _p_instance->setMRL(V_BSTR(&value));
61         VariantClear(&value);
62     }
63     else
64     {
65         /*
66         ** try alternative syntax
67         */
68         V_VT(&value) = VT_BSTR;
69         if( S_OK == pPropBag->Read(OLESTR("src"), &value, pErrorLog) )
70         {
71             _p_instance->setMRL(V_BSTR(&value));
72             VariantClear(&value);
73         }
74         else
75         {
76             V_VT(&value) = VT_BSTR;
77             if( S_OK == pPropBag->Read(OLESTR("filename"), &value, pErrorLog) )
78             {
79                 _p_instance->setMRL(V_BSTR(&value));
80                 VariantClear(&value);
81             }
82         }
83     }
84
85     V_VT(&value) = VT_BOOL;
86     if( S_OK == pPropBag->Read(OLESTR("autoplay"), &value, pErrorLog) )
87     {
88         _p_instance->setAutoPlay(V_BOOL(&value) != VARIANT_FALSE);
89         VariantClear(&value);
90     }
91     else
92     {
93         /*
94         ** try alternative syntax
95         */
96         V_VT(&value) = VT_BOOL;
97         if( S_OK == pPropBag->Read(OLESTR("autostart"), &value, pErrorLog) )
98         {
99             _p_instance->setAutoPlay(V_BOOL(&value) != VARIANT_FALSE);
100             VariantClear(&value);
101         }
102     }
103
104     SIZEL size = _p_instance->getExtent();
105     V_VT(&value) = VT_I4;
106     if( S_OK == pPropBag->Read(OLESTR("extentwidth"), &value, pErrorLog) )
107     {
108          size.cx = V_I4(&value);
109         VariantClear(&value);
110     }
111     V_VT(&value) = VT_I4;
112     if( S_OK == pPropBag->Read(OLESTR("extentheight"), &value, pErrorLog) )
113     {
114          size.cy = V_I4(&value);
115         VariantClear(&value);
116     }
117     _p_instance->setExtent(size);
118
119     V_VT(&value) = VT_BOOL;
120     if( S_OK == pPropBag->Read(OLESTR("autoloop"), &value, pErrorLog) )
121     {
122         _p_instance->setAutoLoop(V_BOOL(&value) != VARIANT_FALSE);
123         VariantClear(&value);
124     }
125     else
126     {
127         /*
128         ** try alternative syntax
129         */
130         V_VT(&value) = VT_BOOL;
131         if( S_OK == pPropBag->Read(OLESTR("loop"), &value, pErrorLog) )
132         {
133             _p_instance->setAutoLoop(V_BOOL(&value) != VARIANT_FALSE);
134             VariantClear(&value);
135         }
136     }
137
138     V_VT(&value) = VT_BOOL;
139     if( S_OK == pPropBag->Read(OLESTR("mute"), &value, pErrorLog) )
140     {
141         _p_instance->setMute(V_BOOL(&value) != VARIANT_FALSE);
142         VariantClear(&value);
143     }
144
145     V_VT(&value) = VT_BOOL;
146     if( S_OK == pPropBag->Read(OLESTR("visible"), &value, pErrorLog) )
147     {
148         _p_instance->setVisible(V_BOOL(&value) != VARIANT_FALSE);
149         VariantClear(&value);
150     }
151     else
152     {
153         /*
154         ** try alternative syntax
155         */
156         V_VT(&value) = VT_BOOL;
157         if( S_OK == pPropBag->Read(OLESTR("showdisplay"), &value, pErrorLog) )
158         {
159             _p_instance->setVisible(V_BOOL(&value) != VARIANT_FALSE);
160             VariantClear(&value);
161         }
162     }
163
164     V_VT(&value) = VT_I4;
165     if( S_OK == pPropBag->Read(OLESTR("volume"), &value, pErrorLog) )
166     {
167         _p_instance->setVolume(V_I4(&value));
168         VariantClear(&value);
169     }
170
171     V_VT(&value) = VT_I4;
172     if( S_OK == pPropBag->Read(OLESTR("starttime"), &value, pErrorLog) )
173     {
174         _p_instance->setStartTime(V_I4(&value));
175         VariantClear(&value);
176     }
177
178     V_VT(&value) = VT_BSTR;
179     if( S_OK == pPropBag->Read(OLESTR("baseurl"), &value, pErrorLog) )
180     {
181         _p_instance->setBaseURL(V_BSTR(&value));
182         VariantClear(&value);
183     }
184
185     V_VT(&value) = VT_I4;
186     if( S_OK == pPropBag->Read(OLESTR("backcolor"), &value, pErrorLog) )
187     {
188         _p_instance->setBackColor(V_I4(&value));
189         VariantClear(&value);
190     }
191     else
192     {
193         /*
194         ** try alternative syntax
195         */
196         V_VT(&value) = VT_BSTR;
197         if( S_OK == pPropBag->Read(OLESTR("bgcolor"), &value, pErrorLog) )
198         {
199             long backcolor;
200             if( swscanf(V_BSTR(&value), L"#%lX", &backcolor) )
201             {
202                 _p_instance->setBackColor(backcolor);
203             }
204             VariantClear(&value);
205         }
206     }
207
208     return _p_instance->onLoad();
209 };
210
211 STDMETHODIMP VLCPersistPropertyBag::Save(LPPROPERTYBAG pPropBag, BOOL fClearDirty, BOOL fSaveAllProperties)
212 {
213     if( NULL == pPropBag )
214         return E_INVALIDARG;
215
216     VARIANT value;
217
218     VariantInit(&value);
219
220     V_VT(&value) = VT_BOOL;
221     V_BOOL(&value) = _p_instance->getAutoLoop()? VARIANT_TRUE : VARIANT_FALSE;
222     pPropBag->Write(OLESTR("AutoLoop"), &value);
223     VariantClear(&value);
224
225     V_VT(&value) = VT_BOOL;
226     V_BOOL(&value) = _p_instance->getAutoPlay()? VARIANT_TRUE : VARIANT_FALSE;
227     pPropBag->Write(OLESTR("AutoPlay"), &value);
228     VariantClear(&value);
229
230     SIZEL size = _p_instance->getExtent();
231     V_VT(&value) = VT_I4;
232     V_I4(&value) = size.cx;
233     pPropBag->Write(OLESTR("ExtentWidth"), &value);
234     V_I4(&value) = size.cy;
235     pPropBag->Write(OLESTR("ExtentHeight"), &value);
236
237     V_VT(&value) = VT_BSTR;
238     V_BSTR(&value) = SysAllocStringLen(_p_instance->getMRL(),
239                             SysStringLen(_p_instance->getMRL()));
240     pPropBag->Write(OLESTR("MRL"), &value);
241     VariantClear(&value);
242
243     V_VT(&value) = VT_BOOL;
244     V_BOOL(&value) = _p_instance->getVisible()? VARIANT_TRUE : VARIANT_FALSE;
245     pPropBag->Write(OLESTR("Visible"), &value);
246     VariantClear(&value);
247
248     V_VT(&value) = VT_I4;
249     V_I4(&value) = _p_instance->getVolume();
250     pPropBag->Write(OLESTR("Volume"), &value);
251     VariantClear(&value);
252
253     V_VT(&value) = VT_I4;
254     V_I4(&value) = _p_instance->getStartTime();
255     pPropBag->Write(OLESTR("StartTime"), &value);
256     VariantClear(&value);
257
258     V_VT(&value) = VT_BSTR;
259     V_BSTR(&value) = SysAllocStringLen(_p_instance->getBaseURL(),
260                             SysStringLen(_p_instance->getBaseURL()));
261     pPropBag->Write(OLESTR("BaseURL"), &value);
262     VariantClear(&value);
263
264     V_VT(&value) = VT_I4;
265     V_I4(&value) = _p_instance->getBackColor();
266     pPropBag->Write(OLESTR("BackColor"), &value);
267     VariantClear(&value);
268
269     if( fClearDirty )
270         _p_instance->setDirty(FALSE);
271
272     return S_OK;
273 };