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