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