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 #ifndef _DATABASES_HXX_
25 #define _DATABASES_HXX_
26 
27 #ifndef INCLUDED_STL_SET
28 #include <set>
29 #define INCLUDED_STL_SET
30 #endif
31 #ifndef INCLUDED_STL_VECTOR
32 #include <vector>
33 #define INCLUDED_STL_VECTOR
34 #endif
35 #include <hash_map>
36 #include <hash_set>
37 #include <osl/mutex.hxx>
38 #include <rtl/ustring.hxx>
39 #include <rtl/string.hxx>
40 #include <com/sun/star/uno/Sequence.hxx>
41 #include <com/sun/star/ucb/XContent.hpp>
42 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
43 #include <com/sun/star/io/XInputStream.hpp>
44 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
45 #include <com/sun/star/uno/XComponentContext.hpp>
46 #include <com/sun/star/i18n/XCollator.hpp>
47 #include <com/sun/star/deployment/XPackage.hpp>
48 #include "com/sun/star/ucb/XSimpleFileAccess.hpp"
49 
50 // Forward declaration
51 
52 
53 namespace helpdatafileproxy {
54 
55     class Hdf;
56 
57 }
58 
59 
60 namespace chelp {
61 
62 
63 	class Databases;
64 	class URLParameter;
65 
66 	class StaticModuleInformation
67 	{
68 	private:
69 
70 		rtl::OUString m_aStartId;
71 		rtl::OUString m_aProgramSwitch;
72 		rtl::OUString m_aTitle;
73 		rtl::OUString m_aHeading;
74 		rtl::OUString m_aFulltext;
75 		int m_nOrder;
76 
77 
78 	public:
79 
StaticModuleInformation(rtl::OUString aTitle,rtl::OUString aStartId,rtl::OUString aProgramSwitch,rtl::OUString aHeading,rtl::OUString aFulltext,rtl::OUString aOrder)80 		StaticModuleInformation( rtl::OUString aTitle,
81 								 rtl::OUString aStartId,
82 								 rtl::OUString aProgramSwitch,
83 								 rtl::OUString aHeading,
84 								 rtl::OUString aFulltext,
85                                  rtl::OUString aOrder )
86 			: m_aStartId( aStartId ),
87 			  m_aProgramSwitch( aProgramSwitch ),
88 			  m_aTitle( aTitle ),
89 			  m_aHeading( aHeading ),
90 			  m_aFulltext( aFulltext ),
91               m_nOrder( aOrder.toInt32() )
92 		{
93 		}
94 
~StaticModuleInformation()95 		~StaticModuleInformation() { }
96 
get_title()97 		rtl::OUString get_title()     { return m_aTitle; }
get_id()98 		rtl::OUString get_id()        { return m_aStartId; }
get_program()99 		rtl::OUString get_program()   { return m_aProgramSwitch; }
get_heading()100 		rtl::OUString get_heading()   { return m_aHeading; }
get_fulltext()101 		rtl::OUString get_fulltext()  { return m_aFulltext; }
get_order()102         int get_order() { return m_nOrder; }
103 	};  // end class StaticModuleInformation
104 
105 
106 
107 	class KeywordInfo
108 	{
109 	public:
110 
111 		class KeywordElement
112 		{
113             friend struct KeywordElementComparator;
114             friend class KeywordInfo;
115 
116 		public:
117 
118 			KeywordElement( Databases* pDatabases,
119 							helpdatafileproxy::Hdf* pHdf,
120 							rtl::OUString& key,
121 							rtl::OUString& ids );
122 
123 		private:
124 
125 			rtl::OUString key;
126 			com::sun::star::uno::Sequence< rtl::OUString > listId;
127 			com::sun::star::uno::Sequence< rtl::OUString > listAnchor;
128 			com::sun::star::uno::Sequence< rtl::OUString > listTitle;
129 
130 			void init( Databases *pDatabases,helpdatafileproxy::Hdf* pHdf,const rtl::OUString& ids );
131 		};
132 
133 
134 		KeywordInfo( const std::vector< KeywordElement >& aVector );
135 
~KeywordInfo()136         ~KeywordInfo() { };
137 
138 		com::sun::star::uno::Sequence< rtl::OUString >&
getKeywordList()139 		getKeywordList() { return listKey; }
140 
141 		com::sun::star::uno::Sequence< com::sun::star::uno::Sequence< rtl::OUString > >&
getIdList()142 		getIdList() { return listId; }
143 
144 		com::sun::star::uno::Sequence< com::sun::star::uno::Sequence< rtl::OUString > >&
getAnchorList()145 		getAnchorList() { return listAnchor; }
146 
147 		com::sun::star::uno::Sequence< com::sun::star::uno::Sequence< rtl::OUString > >&
getTitleList()148 		getTitleList() { return listTitle; }
149 
150 	private:
151 
152 		com::sun::star::uno::Sequence< rtl::OUString > listKey;
153 		com::sun::star::uno::Sequence< com::sun::star::uno::Sequence< rtl::OUString > > listId,listAnchor,listTitle;
154 	};  // end class KeywordInfo
155 
156 
157 
158 	class Databases
159 	{
160 	public:
161 
162 		struct eq
163 		{
operator ()chelp::Databases::eq164 			bool operator()( const rtl::OUString& rKey1, const rtl::OUString& rKey2 ) const
165 			{
166 				return rKey1.compareTo( rKey2 ) == 0;
167 			}
168 		};
169 
170 		struct ha
171 		{
operator ()chelp::Databases::ha172 			size_t operator()( const rtl::OUString& rName ) const
173 			{
174 				return rName.hashCode();
175 			}
176 		};
177 
178 
179 		/**
180 		 *  Input is the installdirectory in system dependent notation
181 		 */
182 
183 		Databases( sal_Bool showBasic,
184 				 const rtl::OUString& instPath,
185 				 const com::sun::star::uno::Sequence< rtl::OUString >& imagesZipPaths,
186 				 const rtl::OUString& productName,
187 				 const rtl::OUString& productVersion,
188 				 const rtl::OUString& styleSheet,
189 				 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext );
190 
191 		~Databases();
192 
193         rtl::OString getImagesZipFileURL();
194 
195 		rtl::OUString getInstallPathAsSystemPath();
196 
197 		rtl::OUString getInstallPathAsURL();
198 
199 		const std::vector< rtl::OUString >& getModuleList( const rtl::OUString& Language );
200 
201 		StaticModuleInformation* getStaticInformationForModule( const rtl::OUString& Module,
202 																const rtl::OUString& Language );
203 
204 		bool checkModuleMatchForExtension( const rtl::OUString& Database, const rtl::OUString& doclist );
205 		KeywordInfo* getKeyword( const rtl::OUString& Module,
206 								 const rtl::OUString& Language );
207 
208 		helpdatafileproxy::Hdf* getHelpDataFile( const rtl::OUString& Module,
209 						 const rtl::OUString& Language, bool helpText = false,
210 						 const rtl::OUString* pExtensionPath = NULL );
211 
212 
213 		/**
214 		 *  The following method returns the Collator for the given language-country combination
215 		 */
216 
217 		com::sun::star::uno::Reference< com::sun::star::i18n::XCollator >
218 		getCollator( const rtl::OUString& Language,
219 					 const rtl::OUString& System );   // System not used by current implementation
220 		//                                            // of XCollator
221 
222 		/**
223 		 *  Returns a copy of the popupfile
224 		 */
225 
226 		void popupDocument( URLParameter* urlPar,char **buffer,int *byteCount );
227 
228 
229 		/**
230 		 *  Returns the cascading style sheet used to format the HTML-output.
231 		 *  First try is language directory, second try is main installation directory.
232 		 */
233 
234 		void cascadingStylesheet( const rtl::OUString& Language,
235 								  char** buffer,
236 								  int* byteCount );
237 
238 
239 		/**
240 		 *  Changes the stylesheet for further reads.
241 		 */
242 
243 		void changeCSS(const rtl::OUString& newStyleSheet);
244 
245 
246 		/**
247 		 *  Returns the active help text for the given module, language and id.
248 		 */
249 
250 		void setActiveText( const rtl::OUString& Module,
251 							const rtl::OUString& Language,
252 							const rtl::OUString& Id,
253 							char** buffer,
254 							int* byteCount );
255 
256 		/**
257 		 *  Has the purpose of forcing the jarfile to stay open
258 		 */
259 
260 		com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess >
261 		jarFile( const rtl::OUString& jar,
262 				 const rtl::OUString& Language );
263 
264 		com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess >
265 		findJarFileForPath( const rtl::OUString& jar, const rtl::OUString& Language,
266 			const rtl::OUString& path, rtl::OUString* o_pExtensionPath = NULL,
267             rtl::OUString* o_pExtensionRegistryPath = NULL );
268 
269 		/**
270 		 *  Maps a given language-locale combination to language.
271 		 */
272 
273 		rtl::OUString processLang( const rtl::OUString& Language );
274 
275 
276 		/**
277 		 *  Maps a given language-locale combination to locale.
278 		 *  The returned string maybe empty
279 		 */
280 
281 		rtl::OUString country( const rtl::OUString& Language );
282 
283 
284 		void replaceName( rtl::OUString& oustring ) const;
285 
getProductName() const286 		rtl::OUString getProductName() const { return m_vReplacement[0]; }
getProductVersion() const287 		rtl::OUString getProductVersion() const { return m_vReplacement[1]; }
288 
289 		rtl::OUString expandURL( const rtl::OUString& aURL );
290 
291 		static rtl::OUString expandURL( const rtl::OUString& aURL,
292 			com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext );
293 
294 	private:
295 
296 		osl::Mutex                                                                     m_aMutex;
297 		com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >       m_xContext;
298 		com::sun::star::uno::Reference< com::sun::star::lang::XMultiComponentFactory > m_xSMgr;
299 		com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess >	   m_xSFA;
300 
301 	    sal_Bool m_bShowBasic;
302 		int    m_nErrorDocLength;
303 		char*  m_pErrorDoc;
304 
305 		int    m_nCustomCSSDocLength;
306 		char*  m_pCustomCSSDoc;
307 		rtl::OUString m_aCSS;
308 
309 #define PRODUCTNAME    0
310 #define PRODUCTVERSION 1
311 #define VENDORNAME     2
312 #define VENDORVERSION  3
313 #define VENDORSHORT    4
314 #define NEWPRODUCTNAME    5
315 #define NEWPRODUCTVERSION 6
316 
317 		int                    m_vAdd[7];
318 		rtl::OUString          m_vReplacement[7];
319 		rtl::OUString          newProdName,newProdVersion,
320             prodName,prodVersion,vendName,vendVersion,vendShort;
321 
322 		rtl::OUString          m_aInstallDirectory;                // Installation directory
323 		com::sun::star::uno::Sequence< rtl::OUString > m_aImagesZipPaths;
324         rtl::OString           m_aImagesZipFileURL;
325 		sal_Int16              m_nSymbolsStyle;
326 		rtl::OUString          m_aInstallDirectoryWithoutEncoding; // a work around for a Sablot bug.
327 		rtl::OUString          m_aInstallDirectoryAsSystemPath;    // Installation directory
328 
329 		std::vector< rtl::OUString >	m_avModules;
330 
331 		typedef std::hash_map< rtl::OUString,helpdatafileproxy::Hdf*,ha,eq >   DatabasesTable;
332 		DatabasesTable m_aDatabases;         // Language and module dependent databases
333 
334 		typedef  std::hash_map< rtl::OUString,rtl::OUString,ha,eq > LangSetTable;
335 		LangSetTable m_aLangSet;   // Mapping to of lang-country to lang
336 
337 		typedef std::hash_map< rtl::OUString,StaticModuleInformation*,ha,eq > ModInfoTable;
338 		ModInfoTable m_aModInfo;   // Module information
339 
340 		typedef std::hash_map< rtl::OUString,KeywordInfo*,ha,eq > KeywordInfoTable;
341 		KeywordInfoTable m_aKeywordInfo;   // Module information
342 
343 		typedef
344 		std::hash_map<
345 		rtl::OUString,
346 			 ::com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess >,
347 			ha,
348 			eq >         ZipFileTable;
349 		ZipFileTable m_aZipFileTable;   // No closing of an once opened jarfile
350 
351 		typedef
352 		std::hash_map<
353 		rtl::OUString,
354 			 ::com::sun::star::uno::Reference< com::sun::star::i18n::XCollator >,
355 			ha,
356 			eq >      CollatorTable;
357 		CollatorTable    m_aCollatorTable;
358 
359 
360 		struct ostring_eq
361 		{
operator ()chelp::Databases::ostring_eq362 			bool operator()( const rtl::OString& rKey1, const rtl::OString& rKey2 ) const
363 			{
364 				return rKey1.compareTo( rKey2 ) == 0;
365 			}
366 		};
367 
368 		struct ostring_ha
369 		{
operator ()chelp::Databases::ostring_ha370 			size_t operator()( const rtl::OString& rName ) const
371 			{
372 				return rName.hashCode();
373 			}
374 		};
375 
376 		typedef
377 		std::hash_set<
378 			rtl::OString,
379 			ostring_ha,
380 			ostring_eq >      EmptyActiveTextSet;
381 		EmptyActiveTextSet	m_aEmptyActiveTextSet;
382 
383 
384 		// methods
385 
386 		void setInstallPath( const rtl::OUString& aInstallDirectory );
387 
388 	}; // end class Databases
389 
390 
391 	//===================================================================
392 	enum IteratorState
393 	{
394 		INITIAL_MODULE,
395 		//SHARED_MODULE,		// Later, avoids redundancies in help compiling
396 		USER_EXTENSIONS,
397 		SHARED_EXTENSIONS,
398         BUNDLED_EXTENSIONS,
399 		END_REACHED
400 	};
401 
402 	// Hashtable to cache extension help status
403 	typedef std::hash_map
404 	<
405 		::rtl::OUString,
406 		bool,
407 		Databases::ha,
408 		Databases::eq
409 	>
410 	ExtensionHelpExistanceMap;
411 
412 
413 	class ExtensionIteratorBase
414 	{
415 		static ExtensionHelpExistanceMap	aHelpExistanceMap;
416 
417 	public:
418 		ExtensionIteratorBase( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext,
419 			Databases& rDatabases, const rtl::OUString& aInitialModule, const rtl::OUString& aLanguage );
420 		ExtensionIteratorBase( Databases& rDatabases, const rtl::OUString& aInitialModule,
421 			const rtl::OUString& aLanguage );
422 		void init( void );
423 
424 	private:
425 		com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > implGetHelpPackageFromPackage
426 			( const com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage,
427 			  com::sun::star::uno::Reference< com::sun::star::deployment::XPackage >& o_xParentPackageBundle );
428 
429 	protected:
430 		com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > implGetNextUserHelpPackage
431 			( com::sun::star::uno::Reference< com::sun::star::deployment::XPackage >& o_xParentPackageBundle );
432 		com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > implGetNextSharedHelpPackage
433 			( com::sun::star::uno::Reference< com::sun::star::deployment::XPackage >& o_xParentPackageBundle );
434         com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > implGetNextBundledHelpPackage
435         ( com::sun::star::uno::Reference< com::sun::star::deployment::XPackage >& o_xParentPackageBundle );
436 		rtl::OUString implGetFileFromPackage( const rtl::OUString& rFileExtension,
437 			com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage );
438 		void implGetLanguageVectorFromPackage( ::std::vector< ::rtl::OUString > &rv,
439 			com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage );
440 
441 		com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >	m_xContext;
442 		com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess >	m_xSFA;
443 		Databases&																	m_rDatabases;
444 
445 		IteratorState																m_eState;
446 		rtl::OUString																m_aExtensionPath;
447 
448 		rtl::OUString																m_aInitialModule;
449 		rtl::OUString																m_aLanguage;
450 
451 		com::sun::star::uno::Sequence< com::sun::star::uno::Reference
452 			< com::sun::star::deployment::XPackage > >								m_aUserPackagesSeq;
453 		bool																		m_bUserPackagesLoaded;
454 
455 		com::sun::star::uno::Sequence< com::sun::star::uno::Reference
456 			< com::sun::star::deployment::XPackage > >								m_aSharedPackagesSeq;
457 		bool																		m_bSharedPackagesLoaded;
458 
459         com::sun::star::uno::Sequence< com::sun::star::uno::Reference
460             < com::sun::star::deployment::XPackage > >                              m_aBundledPackagesSeq;
461 		bool																		m_bBundledPackagesLoaded;
462 
463 		int																			m_iUserPackage;
464 		int																			m_iSharedPackage;
465         int																			m_iBundledPackage;
466 
467 	}; // end class ExtensionIteratorBase
468 
469 
470 	//===================================================================
471 	class DataBaseIterator : public ExtensionIteratorBase
472 	{
473 	public:
DataBaseIterator(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> xContext,Databases & rDatabases,const rtl::OUString & aInitialModule,const rtl::OUString & aLanguage,bool bHelpText)474 		DataBaseIterator( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext,
475 			Databases& rDatabases, const rtl::OUString& aInitialModule, const rtl::OUString& aLanguage, bool bHelpText )
476 				: ExtensionIteratorBase( xContext, rDatabases, aInitialModule, aLanguage )
477 				, m_bHelpText( bHelpText )
478 		{}
DataBaseIterator(Databases & rDatabases,const rtl::OUString & aInitialModule,const rtl::OUString & aLanguage,bool bHelpText)479 		DataBaseIterator( Databases& rDatabases, const rtl::OUString& aInitialModule,
480 			const rtl::OUString& aLanguage, bool bHelpText )
481 				: ExtensionIteratorBase( rDatabases, aInitialModule, aLanguage )
482 				, m_bHelpText( bHelpText )
483 		{}
484 
485 		helpdatafileproxy::Hdf* nextHdf( rtl::OUString* o_pExtensionPath = NULL, rtl::OUString* o_pExtensionRegistryPath = NULL );
486 
487 
488 	private:
489 		helpdatafileproxy::Hdf* implGetHdfFromPackage(
490 			com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage,
491 			rtl::OUString* o_pExtensionPath, rtl::OUString* o_pExtensionRegistryPath );
492 
493 		bool																		m_bHelpText;
494 
495 	}; // end class DataBaseIterator
496 
497 	//===================================================================
498 	class KeyDataBaseFileIterator : public ExtensionIteratorBase
499 	{
500 	public:
KeyDataBaseFileIterator(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> xContext,Databases & rDatabases,const rtl::OUString & aInitialModule,const rtl::OUString & aLanguage)501 		KeyDataBaseFileIterator( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext,
502 			Databases& rDatabases, const rtl::OUString& aInitialModule, const rtl::OUString& aLanguage )
503 				: ExtensionIteratorBase( xContext, rDatabases, aInitialModule, aLanguage )
504 		{}
505         //Returns a file URL
506 		rtl::OUString nextDbFile( bool& o_rbExtension );
507 
508 	private:
509 		rtl::OUString implGetDbFileFromPackage(
510 			com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage );
511 
512 	}; // end class KeyDataBaseFileIterator
513 
514 	//===================================================================
515 	class JarFileIterator : public ExtensionIteratorBase
516 	{
517 	public:
JarFileIterator(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> xContext,Databases & rDatabases,const rtl::OUString & aInitialModule,const rtl::OUString & aLanguage)518 		JarFileIterator( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext,
519 			Databases& rDatabases, const rtl::OUString& aInitialModule, const rtl::OUString& aLanguage )
520 				: ExtensionIteratorBase( xContext, rDatabases, aInitialModule, aLanguage )
521 		{}
522 
523 		com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess >
524 			nextJarFile( com::sun::star::uno::Reference< com::sun::star::deployment::XPackage >& o_xParentPackageBundle,
525 							rtl::OUString* o_pExtensionPath = NULL, rtl::OUString* o_pExtensionRegistryPath = NULL );
526 
527 	private:
528 		com::sun::star::uno::Reference< com::sun::star::container::XHierarchicalNameAccess >
529 			implGetJarFromPackage(com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage,
530 				rtl::OUString* o_pExtensionPath = NULL, rtl::OUString* o_pExtensionRegistryPath = NULL );
531 
532 	}; // end class JarFileIterator
533 
534 	//===================================================================
535 	class IndexFolderIterator : public ExtensionIteratorBase
536 	{
537 	public:
IndexFolderIterator(Databases & rDatabases,const rtl::OUString & aInitialModule,const rtl::OUString & aLanguage)538 		IndexFolderIterator( Databases& rDatabases, const rtl::OUString& aInitialModule, const rtl::OUString& aLanguage )
539 			: ExtensionIteratorBase( rDatabases, aInitialModule, aLanguage )
540 		{}
541 
542 		rtl::OUString nextIndexFolder( bool& o_rbExtension, bool& o_rbTemporary );
543 		void deleteTempIndexFolder( const rtl::OUString& aIndexFolder );
544 
545 	private:
546 		rtl::OUString implGetIndexFolderFromPackage( bool& o_rbTemporary,
547 			com::sun::star::uno::Reference< com::sun::star::deployment::XPackage > xPackage );
548 
549 	}; // end class KeyDataBaseFileIterator
550 
551 	//===================================================================
552 
553 }      // end namespace chelp
554 
555 
556 #endif
557