Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include "vbasystem.hxx"
20 :
21 : #include <ooo/vba/word/WdCursorType.hpp>
22 : #include <tools/diagnose_ex.h>
23 : #include <tools/config.hxx>
24 : #include <osl/file.hxx>
25 : #include <tools/urlobj.hxx>
26 :
27 : #ifdef WNT
28 : #include <windows.h>
29 : #include <tchar.h>
30 : #endif
31 :
32 : using namespace ::ooo::vba;
33 : using namespace ::com::sun::star;
34 :
35 0 : PrivateProfileStringListener::~PrivateProfileStringListener()
36 : {
37 0 : }
38 :
39 0 : void PrivateProfileStringListener::Initialize( const rtl::OUString& rFileName, const rtl::OString& rGroupName, const rtl::OString& rKey )
40 : {
41 0 : maFileName = rFileName;
42 0 : maGroupName = rGroupName;
43 0 : maKey = rKey;
44 0 : }
45 : #ifdef WNT
46 : void lcl_getRegKeyInfo( const rtl::OString& sKeyInfo, HKEY& hBaseKey, rtl::OString& sSubKey )
47 : {
48 : sal_Int32 nBaseKeyIndex = sKeyInfo.indexOf('\\');
49 : if( nBaseKeyIndex > 0 )
50 : {
51 : rtl::OString sBaseKey = sKeyInfo.copy( 0, nBaseKeyIndex );
52 : sSubKey = sKeyInfo.copy( nBaseKeyIndex + 1 );
53 : if( sBaseKey.equalsL(RTL_CONSTASCII_STRINGPARAM("HKEY_CURRENT_USER")) )
54 : {
55 : hBaseKey = HKEY_CURRENT_USER;
56 : }
57 : else if( sBaseKey.equalsL(RTL_CONSTASCII_STRINGPARAM("HKEY_LOCAL_MACHINE")) )
58 : {
59 : hBaseKey = HKEY_LOCAL_MACHINE;
60 : }
61 : else if( sBaseKey.equalsL(RTL_CONSTASCII_STRINGPARAM("HKEY_CLASSES_ROOT")) )
62 : {
63 : hBaseKey = HKEY_CLASSES_ROOT;
64 : }
65 : else if( sBaseKey.equalsL(RTL_CONSTASCII_STRINGPARAM("HKEY_USERS")) )
66 : {
67 : hBaseKey = HKEY_USERS;
68 : }
69 : else if( sBaseKey.equalsL(RTL_CONSTASCII_STRINGPARAM("HKEY_CURRENT_CONFIG")) )
70 : {
71 : hBaseKey = HKEY_CURRENT_CONFIG;
72 : }
73 : }
74 : }
75 : #endif
76 :
77 0 : uno::Any PrivateProfileStringListener::getValueEvent()
78 : {
79 : // get the private profile string
80 0 : rtl::OUString sValue;
81 0 : if(!maFileName.isEmpty())
82 : {
83 : // get key/value from a file
84 0 : Config aCfg( maFileName );
85 0 : aCfg.SetGroup( maGroupName );
86 0 : sValue = rtl::OStringToOUString(aCfg.ReadKey(maKey), RTL_TEXTENCODING_DONTKNOW);
87 : }
88 : else
89 : {
90 : // get key/value from windows register
91 : #ifdef WNT
92 : HKEY hBaseKey = NULL;
93 : rtl::OString sSubKey;
94 : lcl_getRegKeyInfo( maGroupName, hBaseKey, sSubKey );
95 : if( hBaseKey != NULL )
96 : {
97 : HKEY hKey = NULL;
98 : LONG lResult;
99 : LPCTSTR lpSubKey = TEXT( sSubKey.getStr());
100 : TCHAR szBuffer[1024];
101 : DWORD cbData = sizeof( szBuffer );
102 : lResult = RegOpenKeyEx( hBaseKey, lpSubKey, 0, KEY_QUERY_VALUE, &hKey );
103 : if( ERROR_SUCCESS == lResult )
104 : {
105 : LPCTSTR lpValueName = TEXT(maKey.getStr());
106 : lResult = RegQueryValueEx( hKey, lpValueName, NULL, NULL, (LPBYTE)szBuffer, &cbData );
107 : RegCloseKey( hKey );
108 : sValue = rtl::OUString::createFromAscii(szBuffer);
109 : }
110 : }
111 :
112 : return uno::makeAny( sValue );
113 : #else
114 : throw uno::RuntimeException( rtl::OUString(
115 0 : RTL_CONSTASCII_USTRINGPARAM("Only support on Windows")), uno::Reference< uno::XInterface >() );
116 : #endif
117 : }
118 :
119 0 : return uno::makeAny( sValue );
120 : }
121 :
122 0 : void PrivateProfileStringListener::setValueEvent( const css::uno::Any& value )
123 : {
124 : // set the private profile string
125 0 : rtl::OUString aValue;
126 0 : value >>= aValue;
127 0 : if(!maFileName.isEmpty())
128 : {
129 : // set value into a file
130 0 : Config aCfg( maFileName );
131 0 : aCfg.SetGroup( maGroupName );
132 0 : aCfg.WriteKey( maKey, rtl::OUStringToOString(aValue, RTL_TEXTENCODING_DONTKNOW) );
133 : }
134 : else
135 : {
136 : //set value into windows register
137 : #ifdef WNT
138 : HKEY hBaseKey = NULL;
139 : rtl::OString sSubKey;
140 : lcl_getRegKeyInfo( maGroupName, hBaseKey, sSubKey );
141 : if( hBaseKey != NULL )
142 : {
143 : HKEY hKey = NULL;
144 : LONG lResult;
145 : LPCTSTR lpSubKey = TEXT( sSubKey.getStr());
146 : lResult = RegCreateKeyEx( hBaseKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL );
147 : if( ERROR_SUCCESS == lResult )
148 : {
149 : LPCTSTR szValue = TEXT( rtl::OUStringToOString( aValue, RTL_TEXTENCODING_UTF8 ).getStr() );
150 : DWORD cbData = sizeof(TCHAR) * (_tcslen(szValue) + 1);
151 : LPCTSTR lpValueName = TEXT(maKey.getStr());
152 : lResult = RegSetValueEx( hKey, lpValueName, 0 /* Reserved */, REG_SZ, (LPBYTE)szValue, cbData );
153 : RegCloseKey( hKey );
154 : }
155 : }
156 : return;
157 : #else
158 : throw uno::RuntimeException( rtl::OUString(
159 0 : RTL_CONSTASCII_USTRINGPARAM("Not implemented")), uno::Reference< uno::XInterface >() );
160 : #endif
161 0 : }
162 :
163 0 : }
164 :
165 0 : SwVbaSystem::SwVbaSystem( uno::Reference<uno::XComponentContext >& xContext ): SwVbaSystem_BASE( uno::Reference< XHelperInterface >(), xContext )
166 : {
167 0 : }
168 :
169 0 : SwVbaSystem::~SwVbaSystem()
170 : {
171 0 : }
172 :
173 : sal_Int32 SAL_CALL
174 0 : SwVbaSystem::getCursor() throw (uno::RuntimeException)
175 : {
176 0 : sal_Int32 nPointerStyle = getPointerStyle( getCurrentWordDoc(mxContext) );
177 :
178 0 : switch( nPointerStyle )
179 : {
180 : case POINTER_ARROW:
181 0 : return word::WdCursorType::wdCursorNorthwestArrow;
182 : case POINTER_NULL:
183 0 : return word::WdCursorType::wdCursorNormal;
184 : case POINTER_WAIT:
185 0 : return word::WdCursorType::wdCursorWait;
186 : case POINTER_TEXT:
187 0 : return word::WdCursorType::wdCursorIBeam;
188 : default:
189 0 : return word::WdCursorType::wdCursorNormal;
190 : }
191 : }
192 :
193 : void SAL_CALL
194 0 : SwVbaSystem::setCursor( sal_Int32 _cursor ) throw (uno::RuntimeException)
195 : {
196 : try
197 : {
198 0 : switch( _cursor )
199 : {
200 : case word::WdCursorType::wdCursorNorthwestArrow:
201 : {
202 0 : const Pointer& rPointer( POINTER_ARROW );
203 0 : setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_False );
204 0 : break;
205 : }
206 : case word::WdCursorType::wdCursorWait:
207 : {
208 0 : const Pointer& rPointer( static_cast< PointerStyle >( POINTER_WAIT ) );
209 : //It will set the edit window, toobar and statusbar's mouse pointer.
210 0 : setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_True );
211 0 : break;
212 : }
213 : case word::WdCursorType::wdCursorIBeam:
214 : {
215 0 : const Pointer& rPointer( static_cast< PointerStyle >( POINTER_TEXT ) );
216 : //It will set the edit window, toobar and statusbar's mouse pointer.
217 0 : setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_True );
218 0 : break;
219 : }
220 : case word::WdCursorType::wdCursorNormal:
221 : {
222 0 : const Pointer& rPointer( POINTER_NULL );
223 0 : setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_False );
224 0 : break;
225 : }
226 : default:
227 : throw uno::RuntimeException( rtl::OUString(
228 0 : RTL_CONSTASCII_USTRINGPARAM("Unknown value for Cursor pointer")), uno::Reference< uno::XInterface >() );
229 : // TODO: isn't this a flaw in the API? It should be allowed to throw an
230 : // IllegalArgumentException, or so
231 : }
232 : }
233 0 : catch( const uno::Exception& )
234 : {
235 : DBG_UNHANDLED_EXCEPTION();
236 : }
237 0 : }
238 :
239 : uno::Any SAL_CALL
240 0 : SwVbaSystem::PrivateProfileString( const rtl::OUString& rFilename, const rtl::OUString& rSection, const rtl::OUString& rKey ) throw ( uno::RuntimeException )
241 : {
242 : // FIXME: need to detect whether it is a relative file path
243 : // we need to detect if this is a URL, if not then assume its a file path
244 0 : rtl::OUString sFileUrl;
245 0 : if( !rFilename.isEmpty() )
246 : {
247 0 : INetURLObject aObj;
248 0 : aObj.SetURL( rFilename );
249 0 : bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
250 0 : if ( bIsURL )
251 0 : sFileUrl = rFilename;
252 : else
253 0 : osl::FileBase::getFileURLFromSystemPath( rFilename, sFileUrl);
254 : }
255 :
256 0 : rtl::OString aGroupName(rtl::OUStringToOString(rSection, RTL_TEXTENCODING_DONTKNOW));
257 0 : rtl::OString aKey(rtl::OUStringToOString(rKey, RTL_TEXTENCODING_DONTKNOW));
258 0 : maPrivateProfileStringListener.Initialize( sFileUrl, aGroupName, aKey );
259 :
260 0 : return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( &maPrivateProfileStringListener ) ) );
261 : }
262 :
263 : rtl::OUString
264 0 : SwVbaSystem::getServiceImplName()
265 : {
266 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SwVbaSystem"));
267 : }
268 :
269 : uno::Sequence< rtl::OUString >
270 0 : SwVbaSystem::getServiceNames()
271 : {
272 0 : static uno::Sequence< rtl::OUString > aServiceNames;
273 0 : if ( aServiceNames.getLength() == 0 )
274 : {
275 0 : aServiceNames.realloc( 1 );
276 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.System" ) );
277 : }
278 0 : return aServiceNames;
279 : }
280 :
281 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|