xref: /trunk/main/sw/source/ui/vba/vbasystem.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbasystem.hxx"
28 #include <vbahelper/vbahelper.hxx>
29 #include <ooo/vba/word/WdCursorType.hpp>
30 #include <tools/diagnose_ex.h>
31 #include <tools/config.hxx>
32 #include <tools/string.hxx>
33 #include <osl/file.hxx>
34 #include <tools/urlobj.hxx>
35 #include <tools/string.hxx>
36 
37 using namespace ::ooo::vba;
38 using namespace ::com::sun::star;
39 
40 PrivateProfileStringListener::~PrivateProfileStringListener()
41 {
42 }
43 
44 void PrivateProfileStringListener::Initialize( const rtl::OUString& rFileName, const ByteString& rGroupName, const ByteString& rKey )
45 {
46     maFileName = rFileName;
47     maGroupName = rGroupName;
48     maKey = rKey;
49 }
50 
51 uno::Any PrivateProfileStringListener::getValueEvent()
52 {
53     // get the private profile string
54     Config aCfg( maFileName );
55     aCfg.SetGroup( maGroupName );
56     rtl::OUString sValue = String( aCfg.ReadKey( maKey ), RTL_TEXTENCODING_DONTKNOW );
57 
58     return uno::makeAny( sValue );
59 }
60 
61 void PrivateProfileStringListener::setValueEvent( const css::uno::Any& value )
62 {
63     // set the private profile string
64     Config aCfg( maFileName );
65     aCfg.SetGroup( maGroupName );
66 
67     rtl::OUString aValue;
68     value >>= aValue;
69     aCfg.WriteKey( maKey, ByteString( aValue.getStr(), RTL_TEXTENCODING_DONTKNOW ) );
70 }
71 
72 SwVbaSystem::SwVbaSystem( uno::Reference<uno::XComponentContext >& xContext ): SwVbaSystem_BASE( uno::Reference< XHelperInterface >(), xContext )
73 {
74 }
75 
76 SwVbaSystem::~SwVbaSystem()
77 {
78 }
79 
80 sal_Int32 SAL_CALL
81 SwVbaSystem::getCursor() throw (uno::RuntimeException)
82 {
83     sal_Int32 nPointerStyle =  getPointerStyle( getCurrentWordDoc(mxContext) );
84 
85     switch( nPointerStyle )
86     {
87         case POINTER_ARROW:
88             return word::WdCursorType::wdCursorNorthwestArrow;
89         case POINTER_NULL:
90             return word::WdCursorType::wdCursorNormal;
91         case POINTER_WAIT:
92             return word::WdCursorType::wdCursorWait;
93         case POINTER_TEXT:
94             return word::WdCursorType::wdCursorIBeam;
95         default:
96             return word::WdCursorType::wdCursorNormal;
97     }
98 }
99 
100 void SAL_CALL
101 SwVbaSystem::setCursor( sal_Int32 _cursor ) throw (uno::RuntimeException)
102 {
103     try
104     {
105         switch( _cursor )
106         {
107             case word::WdCursorType::wdCursorNorthwestArrow:
108             {
109                 const Pointer& rPointer( POINTER_ARROW );
110                 setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_False );
111                 break;
112             }
113             case word::WdCursorType::wdCursorWait:
114             {
115                 const Pointer& rPointer( static_cast< PointerStyle >( POINTER_WAIT ) );
116                 //It will set the edit window, toobar and statusbar's mouse pointer.
117                 setCursorHelper( getCurrentWordDoc(mxContext), rPointer, sal_True );
118                 break;
119             }
120             case word::WdCursorType::wdCursorIBeam:
121             {
122                 const Pointer& rPointer( static_cast< PointerStyle >( POINTER_TEXT ) );
123                 //It will set the edit window, toobar and statusbar's mouse pointer.
124                 setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_True );
125                 break;
126             }
127             case word::WdCursorType::wdCursorNormal:
128             {
129                 const Pointer& rPointer( POINTER_NULL );
130                 setCursorHelper( getCurrentWordDoc( mxContext ), rPointer, sal_False );
131                 break;
132             }
133             default:
134                 throw uno::RuntimeException( rtl::OUString(
135                         RTL_CONSTASCII_USTRINGPARAM("Unknown value for Cursor pointer")), uno::Reference< uno::XInterface >() );
136                 // TODO: isn't this a flaw in the API? It should be allowed to throw an
137                 // IllegalArgumentException, or so
138         }
139     }
140     catch( const uno::Exception& )
141     {
142     	DBG_UNHANDLED_EXCEPTION();
143     }
144 }
145 
146 uno::Any SAL_CALL
147 SwVbaSystem::PrivateProfileString( const rtl::OUString& rFilename, const rtl::OUString& rSection, const rtl::OUString& rKey ) throw ( uno::RuntimeException )
148 {
149     if( rFilename.getLength() == 0 )
150         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Failed to access document from shell" ) ), uno::Reference< uno::XInterface >() );
151 
152     // FIXME: need to detect whether it is a relative file path
153     // we need to detect if this is a URL, if not then assume its a file path
154     rtl::OUString sFileUrl;
155     INetURLObject aObj;
156 	aObj.SetURL( rFilename );
157 	bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
158 	if ( bIsURL )
159 		sFileUrl = rFilename;
160 	else
161 		osl::FileBase::getFileURLFromSystemPath( rFilename, sFileUrl);
162 
163     ByteString aGroupName = ByteString( rSection.getStr(), RTL_TEXTENCODING_DONTKNOW);
164     ByteString aKey = ByteString( rKey.getStr(), RTL_TEXTENCODING_DONTKNOW);
165     maPrivateProfileStringListener.Initialize( sFileUrl, aGroupName, aKey );
166 
167     return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( &maPrivateProfileStringListener ) ) );
168 }
169 
170 rtl::OUString&
171 SwVbaSystem::getServiceImplName()
172 {
173 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSystem") );
174 	return sImplName;
175 }
176 
177 uno::Sequence< rtl::OUString >
178 SwVbaSystem::getServiceNames()
179 {
180 	static uno::Sequence< rtl::OUString > aServiceNames;
181 	if ( aServiceNames.getLength() == 0 )
182 	{
183 		aServiceNames.realloc( 1 );
184 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.System" ) );
185 	}
186 	return aServiceNames;
187 }
188