1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23 #include "vbasystem.hxx"
24 #include <vbahelper/vbahelper.hxx>
25 #include <ooo/vba/word/WdCursorType.hpp>
26 #include <tools/diagnose_ex.h>
27 #include <tools/config.hxx>
28 #include <tools/string.hxx>
29 #include <osl/file.hxx>
30 #include <tools/urlobj.hxx>
31 #include <tools/string.hxx>
32
33 using namespace ::ooo::vba;
34 using namespace ::com::sun::star;
35
~PrivateProfileStringListener()36 PrivateProfileStringListener::~PrivateProfileStringListener()
37 {
38 }
39
Initialize(const rtl::OUString & rFileName,const ByteString & rGroupName,const ByteString & rKey)40 void PrivateProfileStringListener::Initialize( const rtl::OUString& rFileName, const ByteString& rGroupName, const ByteString& rKey )
41 {
42 maFileName = rFileName;
43 maGroupName = rGroupName;
44 maKey = rKey;
45 }
46
getValueEvent()47 uno::Any PrivateProfileStringListener::getValueEvent()
48 {
49 // get the private profile string
50 Config aCfg( maFileName );
51 aCfg.SetGroup( maGroupName );
52 rtl::OUString sValue = String( aCfg.ReadKey( maKey ), RTL_TEXTENCODING_DONTKNOW );
53
54 return uno::makeAny( sValue );
55 }
56
setValueEvent(const css::uno::Any & value)57 void PrivateProfileStringListener::setValueEvent( const css::uno::Any& value )
58 {
59 // set the private profile string
60 Config aCfg( maFileName );
61 aCfg.SetGroup( maGroupName );
62
63 rtl::OUString aValue;
64 value >>= aValue;
65 aCfg.WriteKey( maKey, ByteString( aValue.getStr(), RTL_TEXTENCODING_DONTKNOW ) );
66 }
67
SwVbaSystem(uno::Reference<uno::XComponentContext> & xContext)68 SwVbaSystem::SwVbaSystem( uno::Reference<uno::XComponentContext >& xContext ): SwVbaSystem_BASE( uno::Reference< XHelperInterface >(), xContext )
69 {
70 }
71
~SwVbaSystem()72 SwVbaSystem::~SwVbaSystem()
73 {
74 }
75
76 sal_Int32 SAL_CALL
getCursor()77 SwVbaSystem::getCursor() throw (uno::RuntimeException)
78 {
79 sal_Int32 nPointerStyle = getPointerStyle( getCurrentWordDoc(mxContext) );
80
81 switch( nPointerStyle )
82 {
83 case POINTER_ARROW:
84 return word::WdCursorType::wdCursorNorthwestArrow;
85 case POINTER_NULL:
86 return word::WdCursorType::wdCursorNormal;
87 case POINTER_WAIT:
88 return word::WdCursorType::wdCursorWait;
89 case POINTER_TEXT:
90 return word::WdCursorType::wdCursorIBeam;
91 default:
92 return word::WdCursorType::wdCursorNormal;
93 }
94 }
95
96 void SAL_CALL
setCursor(sal_Int32 _cursor)97 SwVbaSystem::setCursor( sal_Int32 _cursor ) throw (uno::RuntimeException)
98 {
99 try
100 {
101 switch( _cursor )
102 {
103 case word::WdCursorType::wdCursorNorthwestArrow:
104 {
105 const Pointer& rPointer( POINTER_ARROW );
106 setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_False );
107 break;
108 }
109 case word::WdCursorType::wdCursorWait:
110 {
111 const Pointer& rPointer( static_cast< PointerStyle >( POINTER_WAIT ) );
112 //It will set the edit window, toobar and statusbar's mouse pointer.
113 setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_True );
114 break;
115 }
116 case word::WdCursorType::wdCursorIBeam:
117 {
118 const Pointer& rPointer( static_cast< PointerStyle >( POINTER_TEXT ) );
119 //It will set the edit window, toobar and statusbar's mouse pointer.
120 setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_True );
121 break;
122 }
123 case word::WdCursorType::wdCursorNormal:
124 {
125 const Pointer& rPointer( POINTER_NULL );
126 setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_False );
127 break;
128 }
129 default:
130 throw uno::RuntimeException( rtl::OUString(
131 RTL_CONSTASCII_USTRINGPARAM("Unknown value for Cursor pointer")), uno::Reference< uno::XInterface >() );
132 // TODO: isn't this a flaw in the API? It should be allowed to throw an
133 // IllegalArgumentException, or so
134 }
135 }
136 catch( const uno::Exception& )
137 {
138 DBG_UNHANDLED_EXCEPTION();
139 }
140 }
141
142 uno::Any SAL_CALL
PrivateProfileString(const rtl::OUString & rFilename,const rtl::OUString & rSection,const rtl::OUString & rKey)143 SwVbaSystem::PrivateProfileString( const rtl::OUString& rFilename, const rtl::OUString& rSection, const rtl::OUString& rKey ) throw ( uno::RuntimeException )
144 {
145 if( rFilename.getLength() == 0 )
146 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Failed to access document from shell" ) ), uno::Reference< uno::XInterface >() );
147
148 // FIXME: need to detect whether it is a relative file path
149 // we need to detect if this is a URL, if not then assume its a file path
150 rtl::OUString sFileUrl;
151 INetURLObject aObj;
152 aObj.SetURL( rFilename );
153 bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
154 if ( bIsURL )
155 sFileUrl = rFilename;
156 else
157 osl::FileBase::getFileURLFromSystemPath( rFilename, sFileUrl);
158
159 ByteString aGroupName = ByteString( rSection.getStr(), RTL_TEXTENCODING_DONTKNOW);
160 ByteString aKey = ByteString( rKey.getStr(), RTL_TEXTENCODING_DONTKNOW);
161 maPrivateProfileStringListener.Initialize( sFileUrl, aGroupName, aKey );
162
163 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( &maPrivateProfileStringListener ) ) );
164 }
165
166 rtl::OUString&
getServiceImplName()167 SwVbaSystem::getServiceImplName()
168 {
169 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSystem") );
170 return sImplName;
171 }
172
173 uno::Sequence< rtl::OUString >
getServiceNames()174 SwVbaSystem::getServiceNames()
175 {
176 static uno::Sequence< rtl::OUString > aServiceNames;
177 if ( aServiceNames.getLength() == 0 )
178 {
179 aServiceNames.realloc( 1 );
180 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.System" ) );
181 }
182 return aServiceNames;
183 }
184