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 #ifndef _DESKTOP_MIGRATION_IMPL_HXX_
28 #define _DESKTOP_MIGRATION_IMPL_HXX_
29 
30 #include <vector>
31 #include <algorithm>
32 #include <memory>
33 #include <hash_map>
34 
35 #include "migration.hxx"
36 
37 #include <sal/types.h>
38 #include <rtl/string.hxx>
39 #include <rtl/ustring.hxx>
40 
41 #include <com/sun/star/uno/Reference.hxx>
42 
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/container/XNameAccess.hpp>
45 #include <com/sun/star/container/XIndexAccess.hpp>
46 #include <com/sun/star/container/XIndexContainer.hpp>
47 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
48 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
49 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
50 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
51 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
52 
53 #define NS_CSS com::sun::star
54 #define NS_UNO com::sun::star::uno
55 
56 namespace desktop
57 {
58 
59 struct install_info
60 {
61     rtl::OUString productname;  // human readeable product name
62     rtl::OUString userdata;     // file: url for user installation
63 };
64 
65 typedef std::vector< rtl::OUString > strings_v;
66 typedef std::auto_ptr< strings_v > strings_vr;
67 
68 struct migration_step
69 {
70     rtl::OUString name;
71     strings_v includeFiles;
72     strings_v excludeFiles;
73     strings_v includeConfig;
74     strings_v excludeConfig;
75     strings_v includeExtensions;
76     strings_v excludeExtensions;
77     rtl::OUString service;
78 };
79 
80 struct supported_migration
81 {
82     rtl::OUString name;
83     sal_Int32     nPriority;
84     strings_v     supported_versions;
85 };
86 
87 typedef std::vector< migration_step > migrations_v;
88 typedef std::auto_ptr< migrations_v > migrations_vr;
89 typedef std::vector< supported_migration > migrations_available;
90 
91 //__________________________________________
92 /**
93 	define the item, e.g.:menuitem, toolbaritem, to be migrated. we keep the information
94 	of the command URL, the previous sibling node and the parent node of a item
95 */
96 struct MigrationItem
97 {
98 	::rtl::OUString m_sParentNodeName;
99 	::rtl::OUString m_sPrevSibling;
100 	::rtl::OUString m_sCommandURL;
101 	NS_UNO::Reference< NS_CSS::container::XIndexContainer > m_xPopupMenu;
102 
103 	MigrationItem()
104 		:m_xPopupMenu(0)
105 	{
106 	}
107 
108 	MigrationItem(const ::rtl::OUString& sParentNodeName,
109 		const ::rtl::OUString& sPrevSibling,
110 		const ::rtl::OUString& sCommandURL,
111 		const NS_UNO::Reference< NS_CSS::container::XIndexContainer > xPopupMenu)
112 	{
113 		m_sParentNodeName = sParentNodeName;
114 		m_sPrevSibling	  = sPrevSibling;
115 		m_sCommandURL	  = sCommandURL;
116 		m_xPopupMenu	  = xPopupMenu;
117 	}
118 
119 	MigrationItem& operator=(const MigrationItem& aMigrationItem)
120 	{
121 		m_sParentNodeName = aMigrationItem.m_sParentNodeName;
122 		m_sPrevSibling	  = aMigrationItem.m_sPrevSibling;
123 		m_sCommandURL     = aMigrationItem.m_sCommandURL;
124 		m_xPopupMenu      = aMigrationItem.m_xPopupMenu;
125 
126 		return *this;
127 	}
128 
129 	sal_Bool operator==(const MigrationItem& aMigrationItem)
130 	{
131 		return ( aMigrationItem.m_sParentNodeName == m_sParentNodeName &&
132 			aMigrationItem.m_sPrevSibling    == m_sPrevSibling	   &&
133 			aMigrationItem.m_sCommandURL     == m_sCommandURL	   &&
134 			aMigrationItem.m_xPopupMenu.is() == m_xPopupMenu.is()    );
135 	}
136 
137 	::rtl::OUString GetPrevSibling() const { return m_sPrevSibling; }
138 };
139 
140 typedef ::std::hash_map< ::rtl::OUString,
141 						 ::std::vector< MigrationItem >,
142 						 ::rtl::OUStringHash,
143 						 ::std::equal_to< ::rtl::OUString > > MigrationHashMap;
144 
145 struct MigrationItemInfo
146 {
147 	::rtl::OUString m_sResourceURL;
148 	MigrationItem m_aMigrationItem;
149 
150 	MigrationItemInfo(){}
151 
152 	MigrationItemInfo(const ::rtl::OUString& sResourceURL, const MigrationItem& aMigratiionItem)
153 	{
154 		m_sResourceURL = sResourceURL;
155 		m_aMigrationItem = aMigratiionItem;
156 	}
157 };
158 
159 //__________________________________________
160 /**
161 	information for the UI elements to be migrated for one module
162 */
163 struct MigrationModuleInfo
164 {
165 	::rtl::OUString sModuleShortName;
166 	sal_Bool		bHasMenubar;
167 	::std::vector< ::rtl::OUString > m_vToolbars;
168 
169 	MigrationModuleInfo():bHasMenubar(sal_False){};
170 };
171 
172 //__________________________________________
173 /**
174 	get the information before copying the ui configuration files of old version to new version
175 */
176 class NewVersionUIInfo
177 {
178 public:
179 
180 	NS_UNO::Reference< NS_CSS::ui::XUIConfigurationManager > getConfigManager(const ::rtl::OUString& sModuleShortName) const;
181 	NS_UNO::Reference< NS_CSS::container::XIndexContainer > getNewMenubarSettings(const ::rtl::OUString& sModuleShortName) const;
182 	NS_UNO::Reference< NS_CSS::container::XIndexContainer > getNewToolbarSettings(const ::rtl::OUString& sModuleShortName, const ::rtl::OUString& sToolbarName) const;
183 	void init(const ::std::vector< MigrationModuleInfo >& vModulesInfo);
184 
185 private:
186 
187 	NS_UNO::Sequence< NS_CSS::beans::PropertyValue > m_lCfgManagerSeq;
188 	NS_UNO::Sequence< NS_CSS::beans::PropertyValue > m_lNewVersionMenubarSettingsSeq;
189 	NS_UNO::Sequence< NS_CSS::beans::PropertyValue > m_lNewVersionToolbarSettingsSeq;
190 };
191 
192 class MigrationImpl
193 {
194 
195 private:
196     strings_vr m_vrVersions;
197     NS_UNO::Reference< NS_CSS::lang::XMultiServiceFactory > m_xFactory;
198 
199     migrations_available m_vMigrationsAvailable; // list of all available migrations
200     migrations_vr        m_vrMigrations;         // list of all migration specs from config
201     install_info         m_aInfo;                // info about the version being migrated
202     strings_vr           m_vrFileList;           // final list of files to be copied
203  	MigrationHashMap     m_aOldVersionItemsHashMap;
204  	MigrationHashMap     m_aNewVersionItemsHashMap;
205  	::rtl::OUString      m_sModuleIdentifier;
206 
207     // functions to control the migration process
208     bool          readAvailableMigrations(migrations_available&);
209     migrations_vr readMigrationSteps(const ::rtl::OUString& rMigrationName);
210     sal_Int32     findPreferedMigrationProcess(const migrations_available&);
211     install_info  findInstallation(const strings_v& rVersions);
212     strings_vr    compileFileList();
213 
214     // helpers
215     void substract(strings_v& va, const strings_v& vb_c) const;
216     strings_vr getAllFiles(const rtl::OUString& baseURL) const;
217     strings_vr applyPatterns(const strings_v& vSet, const strings_v& vPatterns) const;
218     NS_UNO::Reference< NS_CSS::container::XNameAccess > getConfigAccess(const sal_Char* path, sal_Bool rw=sal_False);
219 
220 	::std::vector< MigrationModuleInfo > dectectUIChangesForAllModules() const;
221 	void compareOldAndNewConfig(const ::rtl::OUString& sParentNodeName,
222 		const NS_UNO::Reference< NS_CSS::container::XIndexContainer >& xOldIndexContainer,
223 		const NS_UNO::Reference< NS_CSS::container::XIndexContainer >& xNewIndexContainer,
224 		const ::rtl::OUString& sToolbarName);
225 	void mergeOldToNewVersion(const NS_UNO::Reference< NS_CSS::ui::XUIConfigurationManager >& xCfgManager,
226 		const NS_UNO::Reference< NS_CSS::container::XIndexContainer>& xIndexContainer,
227 		const ::rtl::OUString& sModuleIdentifier,
228 		const ::rtl::OUString& sResourceURL);
229 
230     // actual processing function that perform the migration steps
231     void copyFiles();
232     void copyConfig();
233     void runServices();
234     void refresh();
235 
236     void setMigrationCompleted();
237     sal_Bool checkMigrationCompleted();
238 
239 public:
240     MigrationImpl(const NS_UNO::Reference< NS_CSS::lang::XMultiServiceFactory >&);
241     ~MigrationImpl();
242     sal_Bool doMigration();
243     sal_Bool checkMigration();
244     rtl::OUString getOldVersionName();
245 
246 
247 };
248 }
249 #undef NS_CSS
250 #undef NS_UNO
251 
252 #endif
253