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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include "evaluation.hxx"
28 #include <com/sun/star/beans/NamedValue.hpp>
29 #include <com/sun/star/registry/XRegistryKey.hpp>
30 #include <com/sun/star/util/Date.hpp>
31 #include <rtl/ustrbuf.hxx>
32 #include <uno/environment.h>
33 #include <cppuhelper/factory.hxx>
34 #include <unotools/configmgr.hxx>
35 #include <vcl/msgbox.hxx>
36 #include <tools/resmgr.hxx>
37 #include <tools/resid.hxx>
38 #include "../app/desktop.hrc"
39 
40 
41 using namespace rtl;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::registry;
46 
47 namespace desktop {
48 
49 static SOEvaluation*	pSOEval=0;
50 
51 const char* SOEvaluation::interfaces[] =
52 {
53     "com.sun.star.beans.XExactName",
54     "com.sun.star.beans.XMaterialHolder",
55     "com.sun.star.lang.XComponent",
56     "com.sun.star.lang.XServiceInfo",
57     NULL,
58 };
59 
60 const char* SOEvaluation::implementationName = "com.sun.star.comp.desktop.Evaluation";
61 const char* SOEvaluation::serviceName = "com.sun.star.office.Evaluation";
62 
GetImplementationName()63 OUString SOEvaluation::GetImplementationName()
64 {
65 	return OUString( RTL_CONSTASCII_USTRINGPARAM( implementationName));
66 }
67 
GetSupportedServiceNames()68 Sequence< OUString > SOEvaluation::GetSupportedServiceNames()
69 {
70 	sal_Int32 nSize = (sizeof( interfaces ) / sizeof( const char *)) - 1;
71 	Sequence< OUString > aResult( nSize );
72 
73 	for( sal_Int32 i = 0; i < nSize; i++ )
74 		aResult[i] = OUString::createFromAscii( interfaces[i] );
75 	return aResult;
76 }
77 
CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)78 Reference< XInterface >  SAL_CALL SOEvaluation::CreateInstance(
79     const Reference< XMultiServiceFactory >& rSMgr )
80 {
81 	static osl::Mutex	aMutex;
82 	if ( pSOEval == 0 )
83 	{
84 		osl::MutexGuard guard( aMutex );
85 		if ( pSOEval == 0 )
86 			return (XComponent*) ( new SOEvaluation( rSMgr ) );
87 	}
88 	return (XComponent*)0;
89 }
90 
SOEvaluation(const Reference<XMultiServiceFactory> & xFactory)91 SOEvaluation::SOEvaluation( const Reference< XMultiServiceFactory >& xFactory ) :
92 	m_aListeners( m_aMutex ),
93 	m_xServiceManager( xFactory )
94 {
95 }
96 
~SOEvaluation()97 SOEvaluation::~SOEvaluation()
98 {
99 }
100 
101 // XComponent
dispose()102 void SAL_CALL SOEvaluation::dispose() throw ( RuntimeException )
103 {
104     EventObject aObject;
105     aObject.Source = (XComponent*)this;
106     m_aListeners.disposeAndClear( aObject );
107 }
108 
addEventListener(const Reference<XEventListener> & aListener)109 void SAL_CALL SOEvaluation::addEventListener( const Reference< XEventListener > & aListener) throw ( RuntimeException )
110 {
111     m_aListeners.addInterface( aListener );
112 }
113 
removeEventListener(const Reference<XEventListener> & aListener)114 void SAL_CALL SOEvaluation::removeEventListener( const Reference< XEventListener > & aListener ) throw ( RuntimeException )
115 {
116     m_aListeners.removeInterface( aListener );
117 }
118 
119 // XExactName
getExactName(const rtl::OUString & rApproximateName)120 rtl::OUString SAL_CALL SOEvaluation::getExactName( const rtl::OUString& rApproximateName ) throw ( RuntimeException )
121 {
122     // get the tabreg service for an evaluation version
123     // without this service office shouldn't run at all
124 	OUString aTitle = rApproximateName;
125     OUString aEval;
126     sal_Bool bExpired = sal_True;
127     Reference < XMaterialHolder > xHolder( m_xServiceManager->createInstance(
128             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.tab.tabreg" ) ) ), UNO_QUERY );
129     if ( xHolder.is() )
130     {
131         // get a sequence of strings for the defined locales
132         // a registered version doesn't provide data
133         bExpired = sal_False;
134         Any aData = xHolder->getMaterial();
135         Sequence < NamedValue > aSeq;
136         if ( aData >>= aSeq )
137         {
138             // this is an evaluation version, because it provides "material"
139             bExpired = sal_True;
140             for (int i=0; i<aSeq.getLength(); i++ )
141             {
142                 NamedValue& rValue = aSeq[i];
143                 if ( rValue.Name.equalsAscii("expired") )
144                     rValue.Value >>= bExpired;
145                 else if (rValue.Name.equalsAscii("title") )
146                     rValue.Value >>= aEval;
147             }
148             // append eval string to title
149             aTitle += OUString::createFromAscii(" ") + aEval;
150             if ( bExpired )
151                 throw RuntimeException();
152         }
153     }
154 
155 	return aTitle;
156 }
157 
158 // XMaterialHolder
getMaterial()159 Any SAL_CALL SOEvaluation::getMaterial() throw( RuntimeException )
160 {
161 	// Time bomb implementation. Return empty Any to do nothing or
162 	// provide a com::sun::star::util::Date with the time bomb date.
163 	Any a;
164 
165     // change here to force recompile 00002
166 #ifdef TIMEBOMB
167 	// Code for extracting/providing time bomb date!
168 	int nDay   = TIMEBOMB % 100;
169 	int nMonth = ( TIMEBOMB % 10000 ) / 100;
170     int nYear  = TIMEBOMB / 10000;
171 	com::sun::star::util::Date	aDate( nDay, nMonth, nYear );
172 	a <<= aDate;
173 #endif
174 	return a;
175 }
176 
177 // XServiceInfo
getImplementationName()178 ::rtl::OUString SAL_CALL SOEvaluation::getImplementationName()
179 throw ( RuntimeException )
180 {
181 	return SOEvaluation::GetImplementationName();
182 }
183 
supportsService(const::rtl::OUString & rServiceName)184 sal_Bool SAL_CALL SOEvaluation::supportsService( const ::rtl::OUString& rServiceName )
185 throw ( RuntimeException )
186 {
187 	sal_Int32 nSize = (sizeof( interfaces ) / sizeof( const char *))-1;
188 
189 	for( sal_Int32 i = 0; i < nSize; i++ )
190 		if ( rServiceName.equalsAscii( interfaces[i] ))
191 			return sal_True;
192 	return sal_False;
193 }
194 
getSupportedServiceNames()195 Sequence< ::rtl::OUString > SAL_CALL SOEvaluation::getSupportedServiceNames()
196 throw ( RuntimeException )
197 {
198 	return SOEvaluation::GetSupportedServiceNames();
199 }
200 
201 }
202