LCOV - code coverage report
Current view: top level - desktop/source/migration - migration.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 112 731 15.3 %
Date: 2014-11-03 Functions: 12 38 31.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <sal/config.h>
      21             : 
      22             : #include <algorithm>
      23             : #include <iterator>
      24             : #include <map>
      25             : #include <new>
      26             : #include <set>
      27             : 
      28             : #include "migration.hxx"
      29             : #include "migration_impl.hxx"
      30             : 
      31             : #include <unotools/textsearch.hxx>
      32             : #include <comphelper/processfactory.hxx>
      33             : #include <comphelper/sequence.hxx>
      34             : #include <unotools/bootstrap.hxx>
      35             : #include <rtl/bootstrap.hxx>
      36             : #include <rtl/uri.hxx>
      37             : #include <i18nlangtag/lang.h>
      38             : #include <tools/urlobj.hxx>
      39             : #include <osl/file.hxx>
      40             : #include <osl/mutex.hxx>
      41             : #include <osl/security.hxx>
      42             : #include <unotools/configmgr.hxx>
      43             : 
      44             : #include <com/sun/star/configuration/Update.hpp>
      45             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      46             : #include <com/sun/star/lang/XInitialization.hpp>
      47             : #include <com/sun/star/task/XJob.hpp>
      48             : #include <com/sun/star/beans/NamedValue.hpp>
      49             : #include <com/sun/star/beans/XPropertySet.hpp>
      50             : #include <com/sun/star/util/XRefreshable.hpp>
      51             : #include <com/sun/star/util/XChangesBatch.hpp>
      52             : #include <com/sun/star/util/XStringSubstitution.hpp>
      53             : #include <com/sun/star/embed/ElementModes.hpp>
      54             : #include <com/sun/star/embed/FileSystemStorageFactory.hpp>
      55             : #include <com/sun/star/embed/XStorage.hpp>
      56             : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
      57             : #include <com/sun/star/frame/theUICommandDescription.hpp>
      58             : #include <com/sun/star/ui/UIConfigurationManager.hpp>
      59             : #include <com/sun/star/ui/XUIConfiguration.hpp>
      60             : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
      61             : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
      62             : 
      63             : using namespace osl;
      64             : using namespace std;
      65             : using namespace com::sun::star::task;
      66             : using namespace com::sun::star::lang;
      67             : using namespace com::sun::star::beans;
      68             : using namespace com::sun::star::util;
      69             : using namespace com::sun::star::container;
      70             : using com::sun::star::uno::Exception;
      71             : using namespace com::sun::star;
      72             : 
      73             : 
      74             : namespace desktop {
      75             : 
      76             : static const char ITEM_DESCRIPTOR_COMMANDURL[] = "CommandURL";
      77             : static const char ITEM_DESCRIPTOR_CONTAINER[] = "ItemDescriptorContainer";
      78             : static const char ITEM_DESCRIPTOR_LABEL[] = "Label";
      79             : 
      80           0 : OUString retrieveLabelFromCommand(const OUString& sCommand, const OUString& sModuleIdentifier)
      81             : {
      82           0 :     OUString sLabel;
      83             : 
      84           0 :     uno::Reference< container::XNameAccess > xUICommands;
      85             :     uno::Reference< container::XNameAccess > const xNameAccess(
      86             :             frame::theUICommandDescription::get(
      87           0 :                 ::comphelper::getProcessComponentContext()) );
      88           0 :     xNameAccess->getByName( sModuleIdentifier ) >>= xUICommands;
      89           0 :     if (xUICommands.is())
      90             :     {
      91           0 :         if ( !sCommand.isEmpty() )
      92             :         {
      93           0 :             OUString aStr;
      94           0 :             ::uno::Sequence< beans::PropertyValue > aPropSeq;
      95             :             try
      96             :             {
      97           0 :                 uno::Any a( xUICommands->getByName( sCommand ));
      98           0 :                 if ( a >>= aPropSeq )
      99             :                 {
     100           0 :                     for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
     101             :                     {
     102           0 :                         if ( aPropSeq[i].Name == "Label" )
     103             :                         {
     104           0 :                             aPropSeq[i].Value >>= aStr;
     105           0 :                             break;
     106             :                         }
     107             :                     }
     108             :                 }
     109             : 
     110           0 :                 sLabel = aStr;
     111             :             }
     112           0 :             catch (const container::NoSuchElementException&)
     113             :             {
     114           0 :                 sLabel = sCommand;
     115           0 :                 sal_Int32 nIndex = sLabel.indexOf(':');
     116           0 :                 if (nIndex>=0 && nIndex <= sLabel.getLength()-1)
     117           0 :                     sLabel = sLabel.copy(nIndex+1);
     118           0 :             }
     119             : 
     120             :         }
     121             :     }
     122             : 
     123           0 :     return sLabel;
     124             : }
     125             : 
     126           0 : OUString mapModuleShortNameToIdentifier(const OUString& sShortName)
     127             : {
     128           0 :     OUString sIdentifier;
     129             : 
     130           0 :     if ( sShortName == "StartModule" )
     131           0 :         sIdentifier = "com.sun.star.frame.StartModule";
     132             : 
     133           0 :     else if ( sShortName == "swriter" )
     134           0 :         sIdentifier = "com.sun.star.text.TextDocument";
     135             : 
     136           0 :     else if ( sShortName == "scalc" )
     137           0 :         sIdentifier = "com.sun.star.sheet.SpreadsheetDocument";
     138             : 
     139           0 :     else if ( sShortName == "sdraw" )
     140           0 :         sIdentifier = "com.sun.star.drawing.DrawingDocument";
     141             : 
     142           0 :     else if ( sShortName == "simpress" )
     143           0 :         sIdentifier = "com.sun.star.presentation.PresentationDocument";
     144             : 
     145           0 :     else if ( sShortName == "smath" )
     146           0 :         sIdentifier = "com.sun.star.formula.FormulaProperties";
     147             : 
     148           0 :     else if ( sShortName == "schart" )
     149           0 :         sIdentifier = "com.sun.star.chart2.ChartDocument";
     150             : 
     151           0 :     else if ( sShortName == "BasicIDE" )
     152           0 :         sIdentifier = "com.sun.star.script.BasicIDE";
     153             : 
     154           0 :     else if ( sShortName == "dbapp" )
     155           0 :         sIdentifier = "com.sun.star.sdb.OfficeDatabaseDocument";
     156             : 
     157           0 :     else if ( sShortName == "sglobal" )
     158           0 :         sIdentifier = "com.sun.star.text.GlobalDocument";
     159             : 
     160           0 :     else if ( sShortName == "sweb" )
     161           0 :         sIdentifier = "com.sun.star.text.WebDocument";
     162             : 
     163           0 :     else if ( sShortName == "swxform" )
     164           0 :         sIdentifier = "com.sun.star.xforms.XMLFormDocument";
     165             : 
     166           0 :     else if ( sShortName == "sbibliography" )
     167           0 :         sIdentifier = "com.sun.star.frame.Bibliography";
     168             : 
     169           0 :     return sIdentifier;
     170             : }
     171             : 
     172           0 : bool MigrationImpl::alreadyMigrated()
     173             : {
     174           0 :     OUString MIGRATION_STAMP_NAME("/MIGRATED4");
     175           0 :     OUString aStr = m_aInfo.userdata + MIGRATION_STAMP_NAME;
     176           0 :     File aFile(aStr);
     177             :     // create migration stamp, and/or check its existence
     178           0 :     bool bRet = aFile.open (osl_File_OpenFlag_Write | osl_File_OpenFlag_Create | osl_File_OpenFlag_NoLock) == FileBase::E_EXIST;
     179             :     OSL_TRACE( "File '%s' exists? %d\n",
     180             :              OUStringToOString(aStr, RTL_TEXTENCODING_ASCII_US).getStr(),
     181             :              bRet );
     182           0 :     return bRet;
     183             : }
     184             : 
     185          64 : bool MigrationImpl::initializeMigration()
     186             : {
     187          64 :     bool bRet = false;
     188             : 
     189          64 :     if (!checkMigrationCompleted()) {
     190          64 :         readAvailableMigrations(m_vMigrationsAvailable);
     191          64 :         sal_Int32 nIndex = findPreferredMigrationProcess(m_vMigrationsAvailable);
     192             :         // m_aInfo is now set to the preferred migration source
     193          64 :         if ( nIndex >= 0 ) {
     194           0 :             if (alreadyMigrated())
     195           0 :                 return false;
     196           0 :             m_vrMigrations = readMigrationSteps(m_vMigrationsAvailable[nIndex].name);
     197             :         }
     198             : 
     199          64 :         bRet = !m_aInfo.userdata.isEmpty();
     200             :     }
     201             : 
     202             :     OSL_TRACE( "Migration %s", bRet ? "needed" : "not required" );
     203             : 
     204          64 :     return bRet;
     205             : }
     206             : 
     207          64 : void Migration::migrateSettingsIfNecessary()
     208             : {
     209          64 :     MigrationImpl aImpl;
     210             : 
     211          64 :     if (! aImpl.initializeMigration() )
     212         128 :         return;
     213             : 
     214           0 :     bool bResult = false;
     215             :     try
     216             :     {
     217           0 :         bResult = aImpl.doMigration();
     218             :     }
     219           0 :     catch (const Exception& e)
     220             :     {
     221           0 :         OString aMsg("doMigration() exception: ");
     222           0 :         aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
     223           0 :         OSL_FAIL(aMsg.getStr());
     224             :     }
     225             :     OSL_ENSURE(bResult, "Migration has not been successful");
     226           0 :     (void)bResult;
     227             : }
     228             : 
     229          64 : MigrationImpl::MigrationImpl()
     230          64 :     : m_vrVersions(new strings_v)
     231             : {
     232          64 : }
     233             : 
     234          64 : MigrationImpl::~MigrationImpl()
     235             : {
     236          64 : }
     237             : 
     238             : // The main entry point for migrating settings
     239           0 : bool MigrationImpl::doMigration()
     240             : {
     241             :     // compile file list for migration
     242           0 :     m_vrFileList = compileFileList();
     243             : 
     244           0 :     bool result = false;
     245             :     try
     246             :     {
     247           0 :         NewVersionUIInfo aNewVersionUIInfo;
     248           0 :         ::std::vector< MigrationModuleInfo > vModulesInfo = dectectUIChangesForAllModules();
     249           0 :         aNewVersionUIInfo.init(vModulesInfo);
     250             : 
     251           0 :         copyFiles();
     252             : 
     253           0 :         const OUString sMenubarResourceURL("private:resource/menubar/menubar");
     254           0 :         const OUString sToolbarResourcePre("private:resource/toolbar/");
     255           0 :         for (sal_uInt32 i=0; i<vModulesInfo.size(); ++i)
     256             :         {
     257           0 :             OUString sModuleIdentifier = mapModuleShortNameToIdentifier(vModulesInfo[i].sModuleShortName);
     258           0 :             if (sModuleIdentifier.isEmpty())
     259           0 :                 continue;
     260             : 
     261           0 :             uno::Sequence< uno::Any > lArgs(2);
     262           0 :             OUString aOldCfgDataPath = m_aInfo.userdata + "/user/config/soffice.cfg/modules/";
     263           0 :             lArgs[0] <<= aOldCfgDataPath + vModulesInfo[i].sModuleShortName;
     264           0 :             lArgs[1] <<= embed::ElementModes::READ;
     265             : 
     266           0 :             uno::Reference< uno::XComponentContext > xContext(comphelper::getProcessComponentContext());
     267           0 :             uno::Reference< lang::XSingleServiceFactory > xStorageFactory(embed::FileSystemStorageFactory::create(xContext));
     268           0 :             uno::Reference< embed::XStorage >             xModules(xStorageFactory->createInstanceWithArguments(lArgs), uno::UNO_QUERY);
     269           0 :             uno::Reference< ui::XUIConfigurationManager2 > xOldCfgManager = ui::UIConfigurationManager::create(xContext);
     270             : 
     271           0 :             if ( xModules.is() )
     272             :             {
     273           0 :                     xOldCfgManager->setStorage( xModules );
     274           0 :                     xOldCfgManager->reload();
     275             :             }
     276             : 
     277           0 :             uno::Reference< ui::XUIConfigurationManager > xCfgManager = aNewVersionUIInfo.getConfigManager(vModulesInfo[i].sModuleShortName);
     278             : 
     279           0 :             if (vModulesInfo[i].bHasMenubar)
     280             :             {
     281           0 :                 uno::Reference< container::XIndexContainer > xOldVersionMenuSettings = uno::Reference< container::XIndexContainer >(xOldCfgManager->getSettings(sMenubarResourceURL, sal_True), uno::UNO_QUERY);
     282           0 :                 uno::Reference< container::XIndexContainer > xNewVersionMenuSettings = aNewVersionUIInfo.getNewMenubarSettings(vModulesInfo[i].sModuleShortName);
     283           0 :                 OUString sParent;
     284           0 :                 compareOldAndNewConfig(sParent, xOldVersionMenuSettings, xNewVersionMenuSettings, sMenubarResourceURL);
     285           0 :                 mergeOldToNewVersion(xCfgManager, xNewVersionMenuSettings, sModuleIdentifier, sMenubarResourceURL);
     286             :             }
     287             : 
     288           0 :             sal_Int32 nToolbars = vModulesInfo[i].m_vToolbars.size();
     289           0 :             if (nToolbars >0)
     290             :             {
     291           0 :                 for (sal_Int32 j=0; j<nToolbars; ++j)
     292             :                 {
     293           0 :                     OUString sToolbarName = vModulesInfo[i].m_vToolbars[j];
     294           0 :                     OUString sToolbarResourceURL = sToolbarResourcePre + sToolbarName;
     295             : 
     296           0 :                     uno::Reference< container::XIndexContainer > xOldVersionToolbarSettings = uno::Reference< container::XIndexContainer >(xOldCfgManager->getSettings(sToolbarResourceURL, sal_True), uno::UNO_QUERY);
     297           0 :                     uno::Reference< container::XIndexContainer > xNewVersionToolbarSettings = aNewVersionUIInfo.getNewToolbarSettings(vModulesInfo[i].sModuleShortName, sToolbarName);
     298           0 :                     OUString sParent;
     299           0 :                     compareOldAndNewConfig(sParent, xOldVersionToolbarSettings, xNewVersionToolbarSettings, sToolbarResourceURL);
     300           0 :                     mergeOldToNewVersion(xCfgManager, xNewVersionToolbarSettings, sModuleIdentifier, sToolbarResourceURL);
     301           0 :                 }
     302             :             }
     303             : 
     304           0 :             m_aOldVersionItemsHashMap.clear();
     305           0 :             m_aNewVersionItemsHashMap.clear();
     306           0 :         }
     307             : 
     308             :         // execute the migration items from Setup.xcu
     309           0 :         copyConfig();
     310             : 
     311             :         // execute custom migration services from Setup.xcu
     312             :         // and refresh the cache
     313           0 :         runServices();
     314           0 :         refresh();
     315             : 
     316           0 :         result = true;
     317             :     }
     318           0 :     catch (css::uno::Exception & e)
     319             :     {
     320             :         SAL_WARN(
     321             :             "desktop.migration",
     322             :             "ignored Exception \"" << e.Message
     323             :                 << "\" while migrating from version \"" << m_aInfo.productname
     324             :                 << "\" data \"" << m_aInfo.userdata << "\"");
     325             :     }
     326             : 
     327             :     // prevent running the migration multiple times
     328           0 :     setMigrationCompleted();
     329           0 :     return result;
     330             : }
     331             : 
     332           0 : void MigrationImpl::refresh()
     333             : {
     334             :     uno::Reference< XRefreshable >(
     335             :         configuration::theDefaultProvider::get(comphelper::getProcessComponentContext()),
     336           0 :         uno::UNO_QUERY_THROW)->refresh();
     337           0 : }
     338             : 
     339           0 : void MigrationImpl::setMigrationCompleted()
     340             : {
     341             :     try
     342             :     {
     343           0 :         uno::Reference< XPropertySet > aPropertySet(getConfigAccess("org.openoffice.Setup/Office", true), uno::UNO_QUERY_THROW);
     344           0 :         aPropertySet->setPropertyValue("MigrationCompleted", uno::makeAny(sal_True));
     345           0 :         uno::Reference< XChangesBatch >(aPropertySet, uno::UNO_QUERY_THROW)->commitChanges();
     346             :     }
     347           0 :     catch (...)
     348             :     {
     349             :         // fail silently
     350             :     }
     351           0 : }
     352             : 
     353          64 : bool MigrationImpl::checkMigrationCompleted()
     354             : {
     355          64 :     bool bMigrationCompleted = false;
     356             :     try {
     357             :         uno::Reference< XPropertySet > aPropertySet(
     358          64 :             getConfigAccess("org.openoffice.Setup/Office"), uno::UNO_QUERY_THROW);
     359          64 :         aPropertySet->getPropertyValue("MigrationCompleted") >>= bMigrationCompleted;
     360             : 
     361          64 :         if( !bMigrationCompleted && getenv("SAL_DISABLE_USERMIGRATION" ) )
     362             :         {
     363             :             // migration prevented - fake it's success
     364           0 :             setMigrationCompleted();
     365           0 :             bMigrationCompleted = true;
     366          64 :         }
     367             :     }
     368           0 :     catch (const Exception&)
     369             :     {
     370             :         // just return false...
     371             :     }
     372             :     OSL_TRACE( "Migration %s", bMigrationCompleted ? "already completed" : "not done" );
     373             : 
     374          64 :     return bMigrationCompleted;
     375             : }
     376             : 
     377          64 : static void insertSorted(migrations_available& rAvailableMigrations, supported_migration& aSupportedMigration)
     378             : {
     379          64 :     bool                           bInserted( false );
     380          64 :     migrations_available::iterator pIter = rAvailableMigrations.begin();
     381         128 :     while ( !bInserted && pIter != rAvailableMigrations.end())
     382             :     {
     383           0 :         if ( pIter->nPriority < aSupportedMigration.nPriority )
     384             :         {
     385           0 :             rAvailableMigrations.insert(pIter, aSupportedMigration );
     386           0 :             bInserted = true;
     387           0 :             break; // i111193: insert invalidates iterator!
     388             :         }
     389           0 :         ++pIter;
     390             :     }
     391          64 :     if ( !bInserted )
     392          64 :         rAvailableMigrations.push_back( aSupportedMigration );
     393          64 : }
     394             : 
     395          64 : bool MigrationImpl::readAvailableMigrations(migrations_available& rAvailableMigrations)
     396             : {
     397             :     // get supported version names
     398          64 :     uno::Reference< XNameAccess > aMigrationAccess(getConfigAccess("org.openoffice.Setup/Migration/SupportedVersions"), uno::UNO_QUERY_THROW);
     399         128 :     uno::Sequence< OUString > seqSupportedVersions = aMigrationAccess->getElementNames();
     400             : 
     401         128 :     const OUString aVersionIdentifiers( "VersionIdentifiers" );
     402         128 :     const OUString aPriorityIdentifier( "Priority" );
     403             : 
     404         128 :     for (sal_Int32 i=0; i<seqSupportedVersions.getLength(); i++)
     405             :     {
     406          64 :         sal_Int32                 nPriority( 0 );
     407          64 :         uno::Sequence< OUString > seqVersions;
     408         128 :         uno::Reference< XNameAccess > xMigrationData( aMigrationAccess->getByName(seqSupportedVersions[i]), uno::UNO_QUERY_THROW );
     409          64 :         xMigrationData->getByName( aVersionIdentifiers ) >>= seqVersions;
     410          64 :         xMigrationData->getByName( aPriorityIdentifier ) >>= nPriority;
     411             : 
     412         128 :         supported_migration aSupportedMigration;
     413          64 :         aSupportedMigration.name      = seqSupportedVersions[i];
     414          64 :         aSupportedMigration.nPriority = nPriority;
     415         384 :         for (sal_Int32 j=0; j<seqVersions.getLength(); j++)
     416         320 :             aSupportedMigration.supported_versions.push_back(seqVersions[j].trim());
     417          64 :         insertSorted( rAvailableMigrations, aSupportedMigration );
     418             :         OSL_TRACE( " available migration '%s'\n",
     419             :                    OUStringToOString( aSupportedMigration.name, RTL_TEXTENCODING_ASCII_US ).getStr() );
     420          64 :     }
     421             : 
     422         128 :     return true;
     423             : }
     424             : 
     425           0 : migrations_vr MigrationImpl::readMigrationSteps(const OUString& rMigrationName)
     426             : {
     427             :     // get migration access
     428           0 :     uno::Reference< XNameAccess > aMigrationAccess(getConfigAccess("org.openoffice.Setup/Migration/SupportedVersions"), uno::UNO_QUERY_THROW);
     429           0 :     uno::Reference< XNameAccess > xMigrationData( aMigrationAccess->getByName(rMigrationName), uno::UNO_QUERY_THROW );
     430             : 
     431             :     // get migration description from from org.openoffice.Setup/Migration
     432             :     // and build vector of migration steps
     433           0 :     OUString aMigrationSteps( "MigrationSteps" );
     434           0 :     uno::Reference< XNameAccess > theNameAccess(xMigrationData->getByName(aMigrationSteps), uno::UNO_QUERY_THROW);
     435           0 :     uno::Sequence< OUString > seqMigrations = theNameAccess->getElementNames();
     436           0 :     uno::Reference< XNameAccess > tmpAccess;
     437           0 :     uno::Reference< XNameAccess > tmpAccess2;
     438           0 :     uno::Sequence< OUString > tmpSeq;
     439           0 :     migrations_vr vrMigrations(new migrations_v);
     440           0 :     for (sal_Int32 i = 0; i < seqMigrations.getLength(); i++)
     441             :     {
     442             :         // get current migration step
     443           0 :         theNameAccess->getByName(seqMigrations[i]) >>= tmpAccess;
     444           0 :         migration_step tmpStep;
     445           0 :         tmpStep.name = seqMigrations[i];
     446             : 
     447             :         // read included files from current step description
     448           0 :         OUString aSeqEntry;
     449           0 :         if (tmpAccess->getByName("IncludedFiles") >>= tmpSeq)
     450             :         {
     451           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     452             :             {
     453           0 :                 aSeqEntry = tmpSeq[j];
     454           0 :                 tmpStep.includeFiles.push_back(aSeqEntry);
     455             :             }
     456             :         }
     457             : 
     458             :         // exluded files...
     459           0 :         if (tmpAccess->getByName("ExcludedFiles") >>= tmpSeq)
     460             :         {
     461           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     462           0 :                 tmpStep.excludeFiles.push_back(tmpSeq[j]);
     463             :         }
     464             : 
     465             :         // included nodes...
     466           0 :         if (tmpAccess->getByName("IncludedNodes") >>= tmpSeq)
     467             :         {
     468           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     469           0 :                 tmpStep.includeConfig.push_back(tmpSeq[j]);
     470             :         }
     471             : 
     472             :         // excluded nodes...
     473           0 :         if (tmpAccess->getByName("ExcludedNodes") >>= tmpSeq)
     474             :         {
     475           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     476           0 :                 tmpStep.excludeConfig.push_back(tmpSeq[j]);
     477             :         }
     478             : 
     479             :         // included extensions...
     480           0 :         if (tmpAccess->getByName("IncludedExtensions") >>= tmpSeq)
     481             :         {
     482           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     483           0 :                 tmpStep.includeExtensions.push_back(tmpSeq[j]);
     484             :         }
     485             : 
     486             :         // excluded extensions...
     487           0 :         if (tmpAccess->getByName("ExcludedExtensions") >>= tmpSeq)
     488             :         {
     489           0 :             for (sal_Int32 j=0; j<tmpSeq.getLength(); j++)
     490             :             {
     491           0 :                 aSeqEntry = tmpSeq[j];
     492           0 :                 tmpStep.excludeExtensions.push_back(aSeqEntry);
     493             :             }
     494             :         }
     495             : 
     496             :         // generic service
     497           0 :         tmpAccess->getByName("MigrationService") >>= tmpStep.service;
     498             : 
     499           0 :         vrMigrations->push_back(tmpStep);
     500           0 :     }
     501           0 :     return vrMigrations;
     502             : }
     503             : 
     504           0 : static FileBase::RC _checkAndCreateDirectory(INetURLObject& dirURL)
     505             : {
     506           0 :     FileBase::RC result = Directory::create(dirURL.GetMainURL(INetURLObject::DECODE_TO_IURI));
     507           0 :     if (result == FileBase::E_NOENT)
     508             :     {
     509           0 :         INetURLObject baseURL(dirURL);
     510           0 :         baseURL.removeSegment();
     511           0 :         _checkAndCreateDirectory(baseURL);
     512           0 :         return Directory::create(dirURL.GetMainURL(INetURLObject::DECODE_TO_IURI));
     513             :     } else
     514           0 :         return result;
     515             : }
     516             : 
     517             : #if defined UNX && ! defined MACOSX
     518             : 
     519             : static const char XDG_CONFIG_PART[] = "/.config/";
     520             : 
     521          64 : OUString MigrationImpl::preXDGConfigDir(const OUString& rConfigDir)
     522             : {
     523          64 :     OUString aPreXDGConfigPath;
     524          64 :     const char* pXDGCfgHome = getenv("XDG_CONFIG_HOME");
     525             : 
     526             :     // cater for XDG_CONFIG_HOME change
     527             :     // If XDG_CONFIG_HOME is set then we;
     528             :     // assume the user knows what they are doing ( room for improvement here, we could
     529             :     // of course search the default config dir etc. also  - but this is more complex,
     530             :     // we would need to weigh results from the current config dir against matches in
     531             :     // the 'old' config dir etc. ) - currently we just use the returned config dir.
     532             :     // If XDG_CONFIG_HOME is NOT set;
     533             :     // assume then we should now using the default $HOME/,config config location for
     534             :     // our user profiles, however *all* previous libreoffice and openoffice.org
     535             :     // configurations will be in the 'old' config directory and that's where we need
     536             :     // to search - we convert the returned config dir to the 'old' dir
     537          64 :     if ( !pXDGCfgHome && rConfigDir.endsWithAsciiL( XDG_CONFIG_PART, sizeof( XDG_CONFIG_PART ) - 1 )  )
     538             :         // remove trailing '.config/' but leave the terminating '/'
     539          64 :         aPreXDGConfigPath = rConfigDir.copy( 0, rConfigDir.getLength() - sizeof(  XDG_CONFIG_PART ) + 2 );
     540             :     else
     541           0 :         aPreXDGConfigPath = rConfigDir;
     542             : 
     543             :     // the application-specific config dir is not longer prefixed by '.' because it is hidden under ".config"
     544             :     // we have to add the '.' for the pre-XDG directory names
     545          64 :     aPreXDGConfigPath += ".";
     546             : 
     547          64 :    return aPreXDGConfigPath;
     548             : }
     549             : #endif
     550             : 
     551         640 : void MigrationImpl::setInstallInfoIfExist(
     552             :     install_info& aInfo,
     553             :     const OUString& rConfigDir,
     554             :     const OUString& rVersion)
     555             : {
     556         640 :     OUString url(INetURLObject(rConfigDir).GetMainURL(INetURLObject::NO_DECODE));
     557        1280 :     osl::DirectoryItem item;
     558        1280 :     osl::FileStatus stat(osl_FileStatus_Mask_Type);
     559             : 
     560        1280 :     if (osl::DirectoryItem::get(url, item) == osl::FileBase::E_None
     561           0 :             && item.getFileStatus(stat) == osl::FileBase::E_None
     562         640 :             && stat.getFileType() == osl::FileStatus::Directory)
     563             :     {
     564           0 :         aInfo.userdata = url;
     565           0 :         aInfo.productname = rVersion;
     566         640 :     }
     567         640 : }
     568             : 
     569          64 : install_info MigrationImpl::findInstallation(const strings_v& rVersions)
     570             : {
     571             : 
     572          64 :     OUString aTopConfigDir;
     573          64 :     osl::Security().getConfigDir( aTopConfigDir );
     574          64 :     if ( !aTopConfigDir.isEmpty() && aTopConfigDir[ aTopConfigDir.getLength()-1 ] != '/' )
     575          64 :         aTopConfigDir += "/";
     576             : 
     577             : #if defined UNX && ! defined MACOSX
     578         128 :     OUString aPreXDGTopConfigDir = preXDGConfigDir(aTopConfigDir);
     579             : #endif
     580             : 
     581          64 :     install_info aInfo;
     582          64 :     strings_v::const_iterator i_ver = rVersions.begin();
     583         448 :     while (i_ver != rVersions.end())
     584             :     {
     585         640 :         OUString aVersion, aProfileName;
     586         320 :         sal_Int32 nSeparatorIndex = (*i_ver).indexOf('=');
     587         320 :         if ( nSeparatorIndex != -1 )
     588             :         {
     589         320 :             aVersion = (*i_ver).copy( 0, nSeparatorIndex );
     590         320 :             aProfileName = (*i_ver).copy( nSeparatorIndex+1 );
     591             :         }
     592             : 
     593        1600 :         if ( !aVersion.isEmpty() && !aProfileName.isEmpty() &&
     594         320 :              ( aInfo.userdata.isEmpty() ||
     595             :                aProfileName.equalsIgnoreAsciiCase(
     596         320 :                    utl::ConfigManager::getProductName() ) ) )
     597             :         {
     598         320 :             setInstallInfoIfExist(aInfo, aTopConfigDir + aProfileName, aVersion);
     599             : #if defined UNX && ! defined MACOSX
     600             :             //try preXDG path if the new one does not exist
     601         320 :             if ( aInfo.userdata.isEmpty())
     602         320 :                 setInstallInfoIfExist(aInfo, aPreXDGTopConfigDir + aProfileName, aVersion);
     603             : #endif
     604             :         }
     605         320 :         ++i_ver;
     606         320 :     }
     607             : 
     608         128 :     return aInfo;
     609             : }
     610             : 
     611          64 : sal_Int32 MigrationImpl::findPreferredMigrationProcess(const migrations_available& rAvailableMigrations)
     612             : {
     613          64 :     sal_Int32    nIndex( -1 );
     614          64 :     sal_Int32    i( 0 );
     615             : 
     616          64 :     migrations_available::const_iterator rIter = rAvailableMigrations.begin();
     617          64 :     while ( rIter != rAvailableMigrations.end() )
     618             :     {
     619          64 :         install_info aInstallInfo = findInstallation(rIter->supported_versions);
     620          64 :         if (!aInstallInfo.productname.isEmpty() )
     621             :         {
     622           0 :             m_aInfo = aInstallInfo;
     623           0 :             nIndex  = i;
     624           0 :             break;
     625             :         }
     626          64 :         ++i;
     627          64 :         ++rIter;
     628          64 :     }
     629             : 
     630             :     OSL_TRACE( " preferred migration is from product '%s'\n",
     631             :                OUStringToOString( m_aInfo.productname, RTL_TEXTENCODING_ASCII_US ).getStr() );
     632             :     OSL_TRACE( " and settings directory '%s'\n",
     633             :                OUStringToOString( m_aInfo.userdata, RTL_TEXTENCODING_ASCII_US ).getStr() );
     634             : 
     635          64 :     return nIndex;
     636             : }
     637             : 
     638           0 : strings_vr MigrationImpl::applyPatterns(const strings_v& vSet, const strings_v& vPatterns) const
     639             : {
     640             :     using namespace utl;
     641           0 :     strings_vr vrResult(new strings_v);
     642           0 :     strings_v::const_iterator i_set;
     643           0 :     strings_v::const_iterator i_pat = vPatterns.begin();
     644           0 :     while (i_pat != vPatterns.end())
     645             :     {
     646             :         // find matches for this pattern in input set
     647             :         // and copy them to the result
     648           0 :         SearchParam param(*i_pat, SearchParam::SRCH_REGEXP);
     649           0 :         TextSearch ts(param, LANGUAGE_DONTKNOW);
     650           0 :         i_set = vSet.begin();
     651           0 :         sal_Int32 start = 0;
     652           0 :         sal_Int32 end = 0;
     653           0 :         while (i_set != vSet.end())
     654             :         {
     655           0 :             end = i_set->getLength();
     656           0 :             if (ts.SearchForward(*i_set, &start, &end))
     657           0 :                 vrResult->push_back(*i_set);
     658           0 :             ++i_set;
     659             :         }
     660           0 :         ++i_pat;
     661           0 :     }
     662           0 :     return vrResult;
     663             : }
     664             : 
     665           0 : strings_vr MigrationImpl::getAllFiles(const OUString& baseURL) const
     666             : {
     667             :     using namespace osl;
     668           0 :     strings_vr vrResult(new strings_v);
     669             : 
     670             :     // get sub dirs
     671           0 :     Directory dir(baseURL);
     672           0 :     if (dir.open() == FileBase::E_None)
     673             :     {
     674           0 :         strings_v vSubDirs;
     675           0 :         strings_vr vrSubResult;
     676             : 
     677             :         // work through directory contents...
     678           0 :         DirectoryItem item;
     679           0 :         FileStatus fs(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL);
     680           0 :         while (dir.getNextItem(item) == FileBase::E_None)
     681             :         {
     682           0 :             if (item.getFileStatus(fs) == FileBase::E_None)
     683             :             {
     684           0 :                 if (fs.getFileType() == FileStatus::Directory)
     685           0 :                     vSubDirs.push_back(fs.getFileURL());
     686             :                 else
     687           0 :                     vrResult->push_back(fs.getFileURL());
     688             :             }
     689             :         }
     690             : 
     691             :         // recurse subfolders
     692           0 :         strings_v::const_iterator i = vSubDirs.begin();
     693           0 :         while (i != vSubDirs.end())
     694             :         {
     695           0 :             vrSubResult = getAllFiles(*i);
     696           0 :             vrResult->insert(vrResult->end(), vrSubResult->begin(), vrSubResult->end());
     697           0 :             ++i;
     698           0 :         }
     699             :     }
     700           0 :     return vrResult;
     701             : }
     702             : 
     703             : namespace {
     704             : 
     705             : // removes elements of vector 2 in vector 1
     706           0 : strings_v subtract(strings_v const & va, strings_v const & vb) {
     707           0 :     strings_v a(va);
     708           0 :     std::sort(a.begin(), a.end());
     709           0 :     strings_v::iterator ae(std::unique(a.begin(), a.end()));
     710           0 :     strings_v b(vb);
     711           0 :     std::sort(b.begin(), b.end());
     712           0 :     strings_v::iterator be(std::unique(b.begin(), b.end()));
     713           0 :     strings_v c;
     714           0 :     std::set_difference(a.begin(), ae, b.begin(), be, std::back_inserter(c));
     715           0 :     return c;
     716             : }
     717             : 
     718             : }
     719             : 
     720           0 : strings_vr MigrationImpl::compileFileList()
     721             : {
     722             : 
     723           0 :     strings_vr vrResult(new strings_v);
     724           0 :     strings_vr vrInclude;
     725           0 :     strings_vr vrExclude;
     726             : 
     727             :     // get a list of all files:
     728           0 :     strings_vr vrFiles = getAllFiles(m_aInfo.userdata);
     729             : 
     730             :     // get a file list result for each migration step
     731           0 :     migrations_v::const_iterator i_migr = m_vrMigrations->begin();
     732           0 :     while (i_migr != m_vrMigrations->end())
     733             :     {
     734           0 :         vrInclude = applyPatterns(*vrFiles, i_migr->includeFiles);
     735           0 :         vrExclude = applyPatterns(*vrFiles, i_migr->excludeFiles);
     736           0 :         strings_v sub(subtract(*vrInclude, *vrExclude));
     737           0 :         vrResult->insert(vrResult->end(), sub.begin(), sub.end());
     738           0 :         ++i_migr;
     739           0 :     }
     740           0 :     return vrResult;
     741             : }
     742             : 
     743             : namespace {
     744             : 
     745           0 : struct componentParts {
     746             :     std::set< OUString > includedPaths;
     747             :     std::set< OUString > excludedPaths;
     748             : };
     749             : 
     750             : typedef std::map< OUString, componentParts > Components;
     751             : 
     752           0 : bool getComponent(OUString const & path, OUString * component) {
     753             :     OSL_ASSERT(component != 0);
     754           0 :     if (path.isEmpty() || path[0] != '/') {
     755             :         OSL_TRACE(
     756             :             ("configuration migration in/exclude path %s ignored (does not"
     757             :              " start with slash)"),
     758             :             OUStringToOString(path, RTL_TEXTENCODING_UTF8).getStr());
     759           0 :         return false;
     760             :     }
     761           0 :     sal_Int32 i = path.indexOf('/', 1);
     762           0 :     *component = i < 0 ? path.copy(1) : path.copy(1, i - 1);
     763           0 :     return true;
     764             : }
     765             : 
     766           0 : uno::Sequence< OUString > setToSeq(std::set< OUString > const & set) {
     767           0 :     std::set< OUString >::size_type n = set.size();
     768           0 :     if (n > SAL_MAX_INT32) {
     769           0 :         throw std::bad_alloc();
     770             :     }
     771           0 :     uno::Sequence< OUString > seq(static_cast< sal_Int32 >(n));
     772           0 :     sal_Int32 i = 0;
     773           0 :     for (std::set< OUString >::const_iterator j(set.begin());
     774           0 :          j != set.end(); ++j)
     775             :     {
     776           0 :         seq[i++] = *j;
     777             :     }
     778           0 :     return seq;
     779             : }
     780             : 
     781             : }
     782             : 
     783           0 : void MigrationImpl::copyConfig() {
     784           0 :     Components comps;
     785           0 :     for (migrations_v::const_iterator i(m_vrMigrations->begin());
     786           0 :          i != m_vrMigrations->end(); ++i)
     787             :     {
     788           0 :         for (strings_v::const_iterator j(i->includeConfig.begin());
     789           0 :              j != i->includeConfig.end(); ++j)
     790             :         {
     791           0 :             OUString comp;
     792           0 :             if (getComponent(*j, &comp)) {
     793           0 :                 comps[comp].includedPaths.insert(*j);
     794             :             }
     795           0 :         }
     796           0 :         for (strings_v::const_iterator j(i->excludeConfig.begin());
     797           0 :              j != i->excludeConfig.end(); ++j)
     798             :         {
     799           0 :             OUString comp;
     800           0 :             if (getComponent(*j, &comp)) {
     801           0 :                 comps[comp].excludedPaths.insert(*j);
     802             :             }
     803           0 :         }
     804             :     }
     805             : 
     806             :     // check if the shared registrymodifications.xcu file exists
     807           0 :     bool bRegistryModificationsXcuExists = false;
     808           0 :     OUString regFilePath(m_aInfo.userdata);
     809           0 :     regFilePath += "/user/registrymodifications.xcu";
     810           0 :     File regFile(regFilePath);
     811           0 :     ::osl::FileBase::RC nError = regFile.open(osl_File_OpenFlag_Read);
     812           0 :     if ( nError == ::osl::FileBase::E_None ) {
     813           0 :         bRegistryModificationsXcuExists = true;
     814           0 :         regFile.close();
     815             :     }
     816             : 
     817           0 :     for (Components::const_iterator i(comps.begin()); i != comps.end(); ++i) {
     818           0 :         if (!i->second.includedPaths.empty()) {
     819           0 :             if (!bRegistryModificationsXcuExists) {
     820             :                 // shared registrymodifications.xcu does not exists
     821             :                 // the configuration is split in many registry files
     822             :                 // determine the file names from the first element in included paths
     823           0 :                 OUStringBuffer buf(m_aInfo.userdata);
     824           0 :                 buf.appendAscii("/user/registry/data");
     825           0 :                 sal_Int32 n = 0;
     826           0 :                 do {
     827           0 :                     OUString seg(i->first.getToken(0, '.', n));
     828             :                     OUString enc(
     829             :                         rtl::Uri::encode(
     830             :                             seg, rtl_UriCharClassPchar, rtl_UriEncodeStrict,
     831           0 :                             RTL_TEXTENCODING_UTF8));
     832           0 :                     if (enc.isEmpty() && !seg.isEmpty()) {
     833             :                         OSL_TRACE(
     834             :                             ("configuration migration component %s ignored (cannot"
     835             :                             " be encoded as file path)"),
     836             :                             OUStringToOString(
     837             :                                 i->first, RTL_TEXTENCODING_UTF8).getStr());
     838           0 :                         goto next;
     839             :                     }
     840           0 :                     buf.append('/');
     841           0 :                     buf.append(enc);
     842           0 :                 } while (n >= 0);
     843           0 :                 buf.appendAscii(".xcu");
     844           0 :                 regFilePath = buf.toString();
     845             :             }
     846             :             configuration::Update::get(
     847           0 :                 comphelper::getProcessComponentContext())->
     848             :                 insertModificationXcuFile(
     849           0 :                     regFilePath, setToSeq(i->second.includedPaths),
     850           0 :                     setToSeq(i->second.excludedPaths));
     851             :         } else {
     852             :             OSL_TRACE(
     853             :                 ("configuration migration component %s ignored (only excludes,"
     854             :                  " no includes)"),
     855             :                 OUStringToOString(
     856             :                     i->first, RTL_TEXTENCODING_UTF8).getStr());
     857             :         }
     858             :     next:;
     859           0 :     }
     860           0 : }
     861             : 
     862         128 : uno::Reference< XNameAccess > MigrationImpl::getConfigAccess(const sal_Char* pPath, bool bUpdate)
     863             : {
     864         128 :     uno::Reference< XNameAccess > xNameAccess;
     865             :     try{
     866         128 :         OUString sAccessSrvc;
     867         128 :         if (bUpdate)
     868           0 :             sAccessSrvc = "com.sun.star.configuration.ConfigurationUpdateAccess";
     869             :         else
     870         128 :             sAccessSrvc = "com.sun.star.configuration.ConfigurationAccess";
     871             : 
     872         256 :         OUString sConfigURL = OUString::createFromAscii(pPath);
     873             : 
     874             :         uno::Reference< XMultiServiceFactory > theConfigProvider(
     875             :             configuration::theDefaultProvider::get(
     876         256 :                 comphelper::getProcessComponentContext()));
     877             : 
     878             :         // access the provider
     879         256 :         uno::Sequence< uno::Any > theArgs(1);
     880         128 :         theArgs[ 0 ] <<= sConfigURL;
     881         256 :         xNameAccess = uno::Reference< XNameAccess > (
     882         128 :                 theConfigProvider->createInstanceWithArguments(
     883         384 :                 sAccessSrvc, theArgs ), uno::UNO_QUERY_THROW );
     884             :     }
     885           0 :     catch (const com::sun::star::uno::Exception& e)
     886             :     {
     887             :         SAL_WARN(
     888             :             "desktop.migration", "ignoring Exception \"" << e.Message << "\"");
     889             :     }
     890         128 :     return xNameAccess;
     891             : }
     892             : 
     893           0 : void MigrationImpl::copyFiles()
     894             : {
     895           0 :     strings_v::const_iterator i_file = m_vrFileList->begin();
     896           0 :     OUString localName;
     897           0 :     OUString destName;
     898           0 :     OUString userInstall;
     899             :     utl::Bootstrap::PathStatus aStatus;
     900           0 :     aStatus = utl::Bootstrap::locateUserInstallation(userInstall);
     901           0 :     if (aStatus == utl::Bootstrap::PATH_EXISTS)
     902             :     {
     903           0 :         while (i_file != m_vrFileList->end())
     904             :         {
     905             :             // remove installation prefix from file
     906           0 :             localName = i_file->copy(m_aInfo.userdata.getLength());
     907           0 :             if (localName.endsWith( "/autocorr/acor_.dat"))
     908             :             {
     909             :                 // Previous versions used an empty language tag for
     910             :                 // LANGUAGE_DONTKNOW with the "[All]" autocorrection entry.
     911             :                 // As of LibreOffice 4.0 it is 'und' for LANGUAGE_UNDETERMINED
     912             :                 // so the file name is "acor_und.dat".
     913           0 :                 localName = localName.copy( 0, localName.getLength() - 4) + "und.dat";
     914             :             }
     915           0 :             destName = userInstall + localName;
     916           0 :             INetURLObject aURL(destName);
     917             :             // check whether destination directory exists
     918           0 :             aURL.removeSegment();
     919           0 :             _checkAndCreateDirectory(aURL);
     920           0 :             FileBase::RC copyResult = File::copy(*i_file, destName);
     921           0 :             if (copyResult != FileBase::E_None)
     922             :             {
     923           0 :                 OString msg("Cannot copy ");
     924           0 :                 msg += OUStringToOString(*i_file, RTL_TEXTENCODING_UTF8) + " to "
     925           0 :                     +  OUStringToOString(destName, RTL_TEXTENCODING_UTF8);
     926           0 :                 OSL_FAIL(msg.getStr());
     927             :             }
     928           0 :             ++i_file;
     929           0 :         }
     930             :     }
     931             :     else
     932             :     {
     933             :         OSL_FAIL("copyFiles: UserInstall does not exist");
     934           0 :     }
     935           0 : }
     936             : 
     937           0 : void MigrationImpl::runServices()
     938             : {
     939             :     // Build argument array
     940           0 :     uno::Sequence< uno::Any > seqArguments(3);
     941           0 :     seqArguments[0] = uno::makeAny(NamedValue("Productname",
     942           0 :         uno::makeAny(m_aInfo.productname)));
     943           0 :     seqArguments[1] = uno::makeAny(NamedValue("UserData",
     944           0 :         uno::makeAny(m_aInfo.userdata)));
     945             : 
     946             : 
     947             :     // create an instance of every migration service
     948             :     // and execute the migration job
     949           0 :     uno::Reference< XJob > xMigrationJob;
     950             : 
     951           0 :     uno::Reference< uno::XComponentContext > xContext(comphelper::getProcessComponentContext());
     952           0 :     migrations_v::const_iterator i_mig  = m_vrMigrations->begin();
     953           0 :     while (i_mig != m_vrMigrations->end())
     954             :     {
     955           0 :         if( !i_mig->service.isEmpty())
     956             :         {
     957             : 
     958             :             try
     959             :             {
     960             :                 // set black list for extension migration
     961           0 :                 uno::Sequence< OUString > seqExtBlackList;
     962           0 :                 sal_uInt32 nSize = i_mig->excludeExtensions.size();
     963           0 :                 if ( nSize > 0 )
     964           0 :                     seqExtBlackList = comphelper::arrayToSequence< OUString >(
     965           0 :                         &i_mig->excludeExtensions[0], nSize );
     966           0 :                 seqArguments[2] = uno::makeAny(NamedValue("ExtensionBlackList",
     967           0 :                     uno::makeAny( seqExtBlackList )));
     968             : 
     969           0 :                 xMigrationJob = uno::Reference< XJob >(
     970           0 :                     xContext->getServiceManager()->createInstanceWithArgumentsAndContext(i_mig->service, seqArguments, xContext),
     971           0 :                     uno::UNO_QUERY_THROW);
     972             : 
     973           0 :                 xMigrationJob->execute(uno::Sequence< NamedValue >());
     974             : 
     975             : 
     976             :             }
     977           0 :             catch (const Exception& e)
     978             :             {
     979           0 :                 OString aMsg("Execution of migration service failed (Exception caught).\nService: ");
     980           0 :                 aMsg += OUStringToOString(i_mig->service, RTL_TEXTENCODING_ASCII_US) + "\nMessage: ";
     981           0 :                 aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
     982           0 :                 OSL_FAIL(aMsg.getStr());
     983             :             }
     984           0 :             catch (...)
     985             :             {
     986           0 :                 OString aMsg("Execution of migration service failed (Exception caught).\nService: ");
     987           0 :                 aMsg += OUStringToOString(i_mig->service, RTL_TEXTENCODING_ASCII_US) +
     988           0 :                     "\nNo message available";
     989           0 :                 OSL_FAIL(aMsg.getStr());
     990             :             }
     991             : 
     992             :         }
     993           0 :         ++i_mig;
     994           0 :     }
     995           0 : }
     996             : 
     997           0 : ::std::vector< MigrationModuleInfo > MigrationImpl::dectectUIChangesForAllModules() const
     998             : {
     999           0 :     ::std::vector< MigrationModuleInfo > vModulesInfo;
    1000           0 :     const OUString MENUBAR("menubar");
    1001           0 :     const OUString TOOLBAR("toolbar");
    1002             : 
    1003           0 :     uno::Sequence< uno::Any > lArgs(2);
    1004           0 :     lArgs[0] <<= m_aInfo.userdata + "/user/config/soffice.cfg/modules";
    1005           0 :     lArgs[1] <<= embed::ElementModes::READ;
    1006             : 
    1007             :     uno::Reference< lang::XSingleServiceFactory > xStorageFactory(
    1008           0 :                      embed::FileSystemStorageFactory::create(comphelper::getProcessComponentContext()));
    1009           0 :     uno::Reference< embed::XStorage >             xModules;
    1010             : 
    1011           0 :     xModules = uno::Reference< embed::XStorage >(xStorageFactory->createInstanceWithArguments(lArgs), uno::UNO_QUERY);
    1012           0 :     if (!xModules.is())
    1013           0 :         return vModulesInfo;
    1014             : 
    1015           0 :     uno::Reference< container::XNameAccess > xAccess = uno::Reference< container::XNameAccess >(xModules, uno::UNO_QUERY);
    1016           0 :     uno::Sequence< OUString > lNames = xAccess->getElementNames();
    1017           0 :     sal_Int32 nLength = lNames.getLength();
    1018           0 :     for (sal_Int32 i=0; i<nLength; ++i)
    1019             :     {
    1020           0 :         OUString sModuleShortName = lNames[i];
    1021           0 :         uno::Reference< embed::XStorage > xModule = xModules->openStorageElement(sModuleShortName, embed::ElementModes::READ);
    1022           0 :         if (xModule.is())
    1023             :         {
    1024           0 :             MigrationModuleInfo aModuleInfo;
    1025             : 
    1026           0 :             uno::Reference< embed::XStorage > xMenubar = xModule->openStorageElement(MENUBAR, embed::ElementModes::READ);
    1027           0 :             if (xMenubar.is())
    1028             :             {
    1029           0 :                 uno::Reference< container::XNameAccess > xNameAccess = uno::Reference< container::XNameAccess >(xMenubar, uno::UNO_QUERY);
    1030           0 :                 if (xNameAccess->getElementNames().getLength() > 0)
    1031             :                 {
    1032           0 :                     aModuleInfo.sModuleShortName = sModuleShortName;
    1033           0 :                     aModuleInfo.bHasMenubar = true;
    1034           0 :                 }
    1035             :             }
    1036             : 
    1037           0 :             uno::Reference< embed::XStorage > xToolbar = xModule->openStorageElement(TOOLBAR, embed::ElementModes::READ);
    1038           0 :             if (xToolbar.is())
    1039             :             {
    1040           0 :                 const OUString RESOURCEURL_CUSTOM_ELEMENT("custom_");
    1041           0 :                 sal_Int32 nCustomLen = 7;
    1042             : 
    1043           0 :                 uno::Reference< container::XNameAccess > xNameAccess = uno::Reference< container::XNameAccess >(xToolbar, uno::UNO_QUERY);
    1044           0 :                 ::uno::Sequence< OUString > lToolbars = xNameAccess->getElementNames();
    1045           0 :                 for (sal_Int32 j=0; j<lToolbars.getLength(); ++j)
    1046             :                 {
    1047           0 :                     OUString sToolbarName = lToolbars[j];
    1048           0 :                     if (sToolbarName.getLength()>=nCustomLen &&
    1049           0 :                         sToolbarName.copy(0, nCustomLen).equals(RESOURCEURL_CUSTOM_ELEMENT))
    1050           0 :                         continue;
    1051             : 
    1052           0 :                     aModuleInfo.sModuleShortName = sModuleShortName;
    1053           0 :                     sal_Int32 nIndex = sToolbarName.lastIndexOf('.');
    1054           0 :                     if (nIndex > 0)
    1055             :                     {
    1056           0 :                         OUString sExtension(sToolbarName.copy(nIndex));
    1057           0 :                         OUString sToolbarResourceName(sToolbarName.copy(0, nIndex));
    1058           0 :                         if (!sToolbarResourceName.isEmpty() && sExtension == ".xml")
    1059           0 :                             aModuleInfo.m_vToolbars.push_back(sToolbarResourceName);
    1060             :                     }
    1061           0 :                 }
    1062             :             }
    1063             : 
    1064           0 :             if (!aModuleInfo.sModuleShortName.isEmpty())
    1065           0 :                 vModulesInfo.push_back(aModuleInfo);
    1066             :         }
    1067           0 :     }
    1068             : 
    1069           0 :     return vModulesInfo;
    1070             : }
    1071             : 
    1072           0 : void MigrationImpl::compareOldAndNewConfig(const OUString& sParent,
    1073             :                                            const uno::Reference< container::XIndexContainer >& xIndexOld,
    1074             :                                            const uno::Reference< container::XIndexContainer >& xIndexNew,
    1075             :                                            const OUString& sResourceURL)
    1076             : {
    1077           0 :     const OUString MENU_SEPARATOR(" | ");
    1078             : 
    1079           0 :     ::std::vector< MigrationItem > vOldItems;
    1080           0 :     ::std::vector< MigrationItem > vNewItems;
    1081           0 :     uno::Sequence< beans::PropertyValue > aProp;
    1082           0 :     sal_Int32 nOldCount = xIndexOld->getCount();
    1083           0 :     sal_Int32 nNewCount = xIndexNew->getCount();
    1084             : 
    1085           0 :     for (int n=0; n<nOldCount; ++n)
    1086             :     {
    1087           0 :         MigrationItem aMigrationItem;
    1088           0 :         if (xIndexOld->getByIndex(n) >>= aProp)
    1089             :         {
    1090           0 :             for(int i=0; i<aProp.getLength(); ++i)
    1091             :             {
    1092           0 :                 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
    1093           0 :                     aProp[i].Value >>= aMigrationItem.m_sCommandURL;
    1094           0 :                 else if ( aProp[i].Name == ITEM_DESCRIPTOR_CONTAINER )
    1095           0 :                     aProp[i].Value >>= aMigrationItem.m_xPopupMenu;
    1096             :             }
    1097             : 
    1098           0 :             if (!aMigrationItem.m_sCommandURL.isEmpty())
    1099           0 :                 vOldItems.push_back(aMigrationItem);
    1100             :         }
    1101           0 :     }
    1102             : 
    1103           0 :     for (int n=0; n<nNewCount; ++n)
    1104             :     {
    1105           0 :         MigrationItem aMigrationItem;
    1106           0 :         if (xIndexNew->getByIndex(n) >>= aProp)
    1107             :         {
    1108           0 :             for(int i=0; i<aProp.getLength(); ++i)
    1109             :             {
    1110           0 :                 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
    1111           0 :                     aProp[i].Value >>= aMigrationItem.m_sCommandURL;
    1112           0 :                 else if ( aProp[i].Name == ITEM_DESCRIPTOR_CONTAINER )
    1113           0 :                     aProp[i].Value >>= aMigrationItem.m_xPopupMenu;
    1114             :             }
    1115             : 
    1116           0 :             if (!aMigrationItem.m_sCommandURL.isEmpty())
    1117           0 :                 vNewItems.push_back(aMigrationItem);
    1118             :         }
    1119           0 :     }
    1120             : 
    1121           0 :     ::std::vector< MigrationItem >::iterator it;
    1122             : 
    1123           0 :     OUString sSibling;
    1124           0 :     for (it = vOldItems.begin(); it!=vOldItems.end(); ++it)
    1125             :     {
    1126           0 :         ::std::vector< MigrationItem >::iterator pFound = ::std::find(vNewItems.begin(), vNewItems.end(), *it);
    1127           0 :         if (pFound != vNewItems.end() && it->m_xPopupMenu.is())
    1128             :         {
    1129           0 :             OUString sName;
    1130           0 :             if (!sParent.isEmpty())
    1131           0 :                 sName = sParent + MENU_SEPARATOR + it->m_sCommandURL;
    1132             :             else
    1133           0 :                 sName = it->m_sCommandURL;
    1134           0 :             compareOldAndNewConfig(sName, it->m_xPopupMenu, pFound->m_xPopupMenu, sResourceURL);
    1135             :         }
    1136           0 :         else if (pFound == vNewItems.end())
    1137             :         {
    1138           0 :             MigrationItem aMigrationItem(sParent, sSibling, it->m_sCommandURL, it->m_xPopupMenu);
    1139           0 :             if (m_aOldVersionItemsHashMap.find(sResourceURL)==m_aOldVersionItemsHashMap.end())
    1140             :             {
    1141           0 :                 ::std::vector< MigrationItem > vMigrationItems;
    1142           0 :                 m_aOldVersionItemsHashMap.insert(MigrationHashMap::value_type(sResourceURL, vMigrationItems));
    1143           0 :                 m_aOldVersionItemsHashMap[sResourceURL].push_back(aMigrationItem);
    1144             :             }
    1145             :             else
    1146             :             {
    1147           0 :                 if (::std::find(m_aOldVersionItemsHashMap[sResourceURL].begin(), m_aOldVersionItemsHashMap[sResourceURL].end(), aMigrationItem)==m_aOldVersionItemsHashMap[sResourceURL].end())
    1148           0 :                     m_aOldVersionItemsHashMap[sResourceURL].push_back(aMigrationItem);
    1149           0 :             }
    1150             :         }
    1151             : 
    1152           0 :         sSibling = it->m_sCommandURL;
    1153             :     }
    1154             : 
    1155           0 :     uno::Reference< container::XIndexContainer > xPopup;
    1156           0 :     for (it = vNewItems.begin(); it!=vNewItems.end(); ++it)
    1157             :     {
    1158           0 :         ::std::vector< MigrationItem >::iterator pFound = ::std::find(vOldItems.begin(), vOldItems.end(), *it);
    1159           0 :         if (pFound != vOldItems.end() && it->m_xPopupMenu.is())
    1160             :         {
    1161           0 :             OUString sName;
    1162           0 :             if (!sParent.isEmpty())
    1163           0 :                 sName = sParent + MENU_SEPARATOR + it->m_sCommandURL;
    1164             :             else
    1165           0 :                 sName = it->m_sCommandURL;
    1166           0 :             compareOldAndNewConfig(sName, pFound->m_xPopupMenu, it->m_xPopupMenu, sResourceURL);
    1167             :         }
    1168           0 :         else if (::std::find(vOldItems.begin(), vOldItems.end(), *it) == vOldItems.end())
    1169             :         {
    1170           0 :             MigrationItem aMigrationItem(sParent, sSibling, it->m_sCommandURL, it->m_xPopupMenu);
    1171           0 :             if (m_aNewVersionItemsHashMap.find(sResourceURL)==m_aNewVersionItemsHashMap.end())
    1172             :             {
    1173           0 :                 ::std::vector< MigrationItem > vMigrationItems;
    1174           0 :                 m_aNewVersionItemsHashMap.insert(MigrationHashMap::value_type(sResourceURL, vMigrationItems));
    1175           0 :                 m_aNewVersionItemsHashMap[sResourceURL].push_back(aMigrationItem);
    1176             :             }
    1177             :             else
    1178             :             {
    1179           0 :                 if (::std::find(m_aNewVersionItemsHashMap[sResourceURL].begin(), m_aNewVersionItemsHashMap[sResourceURL].end(), aMigrationItem)==m_aNewVersionItemsHashMap[sResourceURL].end())
    1180           0 :                     m_aNewVersionItemsHashMap[sResourceURL].push_back(aMigrationItem);
    1181           0 :             }
    1182             :         }
    1183           0 :     }
    1184           0 : }
    1185             : 
    1186           0 : void MigrationImpl::mergeOldToNewVersion(const uno::Reference< ui::XUIConfigurationManager >& xCfgManager,
    1187             :                                          const uno::Reference< container::XIndexContainer>& xIndexContainer,
    1188             :                                          const OUString& sModuleIdentifier,
    1189             :                                          const OUString& sResourceURL)
    1190             : {
    1191           0 :     MigrationHashMap::iterator pFound = m_aOldVersionItemsHashMap.find(sResourceURL);
    1192           0 :     if (pFound==m_aOldVersionItemsHashMap.end())
    1193           0 :         return;
    1194             : 
    1195           0 :     ::std::vector< MigrationItem >::iterator it;
    1196           0 :     for (it=pFound->second.begin(); it!=pFound->second.end(); ++it)
    1197             :     {
    1198           0 :         uno::Reference< container::XIndexContainer > xTemp = xIndexContainer;
    1199             : 
    1200           0 :         OUString sParentNodeName = it->m_sParentNodeName;
    1201           0 :         sal_Int32 nIndex = 0;
    1202           0 :         do
    1203             :         {
    1204           0 :             OUString sToken = sParentNodeName.getToken(0, '|', nIndex).trim();
    1205           0 :             if (sToken.isEmpty())
    1206           0 :                 break;
    1207             : 
    1208           0 :             sal_Int32 nCount = xTemp->getCount();
    1209           0 :             for (sal_Int32 i=0; i<nCount; ++i)
    1210             :             {
    1211           0 :                 OUString sCommandURL;
    1212           0 :                 OUString sLabel;
    1213           0 :                 uno::Reference< container::XIndexContainer > xChild;
    1214             : 
    1215           0 :                 uno::Sequence< beans::PropertyValue > aPropSeq;
    1216           0 :                 xTemp->getByIndex(i) >>= aPropSeq;
    1217           0 :                 for (sal_Int32 j=0; j<aPropSeq.getLength(); ++j)
    1218             :                 {
    1219           0 :                     OUString sPropName = aPropSeq[j].Name;
    1220           0 :                     if ( sPropName == ITEM_DESCRIPTOR_COMMANDURL )
    1221           0 :                         aPropSeq[j].Value >>= sCommandURL;
    1222           0 :                     else if ( sPropName == ITEM_DESCRIPTOR_LABEL )
    1223           0 :                         aPropSeq[j].Value >>= sLabel;
    1224           0 :                     else if ( sPropName == ITEM_DESCRIPTOR_CONTAINER )
    1225           0 :                         aPropSeq[j].Value >>= xChild;
    1226           0 :                 }
    1227             : 
    1228           0 :                 if (sCommandURL == sToken)
    1229             :                 {
    1230           0 :                     xTemp = xChild;
    1231           0 :                     break;
    1232             :                 }
    1233           0 :             }
    1234             : 
    1235           0 :         } while (nIndex>=0);
    1236             : 
    1237           0 :         if (nIndex == -1)
    1238             :         {
    1239           0 :             uno::Sequence< beans::PropertyValue > aPropSeq(3);
    1240             : 
    1241           0 :             aPropSeq[0].Name = OUString(ITEM_DESCRIPTOR_COMMANDURL);
    1242           0 :             aPropSeq[0].Value <<= it->m_sCommandURL;
    1243           0 :             aPropSeq[1].Name = OUString(ITEM_DESCRIPTOR_LABEL);
    1244           0 :             aPropSeq[1].Value <<= retrieveLabelFromCommand(it->m_sCommandURL, sModuleIdentifier);
    1245           0 :             aPropSeq[2].Name = OUString(ITEM_DESCRIPTOR_CONTAINER);
    1246           0 :             aPropSeq[2].Value <<= it->m_xPopupMenu;
    1247             : 
    1248           0 :             if (it->m_sPrevSibling.isEmpty())
    1249           0 :                 xTemp->insertByIndex(0, uno::makeAny(aPropSeq));
    1250           0 :             else if (!it->m_sPrevSibling.isEmpty())
    1251             :             {
    1252           0 :                 sal_Int32 nCount = xTemp->getCount();
    1253           0 :                 sal_Int32 i = 0;
    1254           0 :                 for (; i<nCount; ++i)
    1255             :                 {
    1256           0 :                     OUString sCmd;
    1257           0 :                     uno::Sequence< beans::PropertyValue > aTempPropSeq;
    1258           0 :                     xTemp->getByIndex(i) >>= aTempPropSeq;
    1259           0 :                     for (sal_Int32 j=0; j<aTempPropSeq.getLength(); ++j)
    1260             :                     {
    1261           0 :                         if ( aTempPropSeq[j].Name == ITEM_DESCRIPTOR_COMMANDURL )
    1262             :                         {
    1263           0 :                             aTempPropSeq[j].Value >>= sCmd;
    1264           0 :                             break;
    1265             :                         }
    1266             :                     }
    1267             : 
    1268           0 :                     if (sCmd.equals(it->m_sPrevSibling))
    1269           0 :                         break;
    1270           0 :                 }
    1271             : 
    1272           0 :                 xTemp->insertByIndex(i+1, uno::makeAny(aPropSeq));
    1273           0 :             }
    1274             :         }
    1275           0 :     }
    1276             : 
    1277           0 :     uno::Reference< container::XIndexAccess > xIndexAccess(xIndexContainer, uno::UNO_QUERY);
    1278           0 :     if (xIndexAccess.is())
    1279           0 :         xCfgManager->replaceSettings(sResourceURL, xIndexAccess);
    1280             : 
    1281           0 :     uno::Reference< ui::XUIConfigurationPersistence > xUIConfigurationPersistence(xCfgManager, uno::UNO_QUERY);
    1282           0 :     if (xUIConfigurationPersistence.is())
    1283           0 :         xUIConfigurationPersistence->store();
    1284             : }
    1285             : 
    1286           0 : uno::Reference< ui::XUIConfigurationManager > NewVersionUIInfo::getConfigManager(const OUString& sModuleShortName) const
    1287             : {
    1288           0 :     uno::Reference< ui::XUIConfigurationManager > xCfgManager;
    1289             : 
    1290           0 :     for (sal_Int32 i=0; i<m_lCfgManagerSeq.getLength(); ++i)
    1291             :     {
    1292           0 :         if (m_lCfgManagerSeq[i].Name.equals(sModuleShortName))
    1293             :         {
    1294           0 :             m_lCfgManagerSeq[i].Value >>= xCfgManager;
    1295           0 :             break;
    1296             :         }
    1297             :     }
    1298             : 
    1299           0 :     return xCfgManager;
    1300             : }
    1301             : 
    1302           0 : uno::Reference< container::XIndexContainer > NewVersionUIInfo::getNewMenubarSettings(const OUString& sModuleShortName) const
    1303             : {
    1304           0 :     uno::Reference< container::XIndexContainer > xNewMenuSettings;
    1305             : 
    1306           0 :     for (sal_Int32 i=0; i<m_lNewVersionMenubarSettingsSeq.getLength(); ++i)
    1307             :     {
    1308           0 :         if (m_lNewVersionMenubarSettingsSeq[i].Name.equals(sModuleShortName))
    1309             :         {
    1310           0 :             m_lNewVersionMenubarSettingsSeq[i].Value >>= xNewMenuSettings;
    1311           0 :             break;
    1312             :         }
    1313             :     }
    1314             : 
    1315           0 :     return xNewMenuSettings;
    1316             : }
    1317             : 
    1318           0 : uno::Reference< container::XIndexContainer > NewVersionUIInfo::getNewToolbarSettings(const OUString& sModuleShortName, const OUString& sToolbarName) const
    1319             : {
    1320           0 :     uno::Reference< container::XIndexContainer > xNewToolbarSettings;
    1321             : 
    1322           0 :     for (sal_Int32 i=0; i<m_lNewVersionToolbarSettingsSeq.getLength(); ++i)
    1323             :     {
    1324           0 :         if (m_lNewVersionToolbarSettingsSeq[i].Name.equals(sModuleShortName))
    1325             :         {
    1326           0 :             uno::Sequence< beans::PropertyValue > lToolbarSettingsSeq;
    1327           0 :             m_lNewVersionToolbarSettingsSeq[i].Value >>= lToolbarSettingsSeq;
    1328           0 :             for (sal_Int32 j=0; j<lToolbarSettingsSeq.getLength(); ++j)
    1329             :             {
    1330           0 :                 if (lToolbarSettingsSeq[j].Name.equals(sToolbarName))
    1331             :                 {
    1332           0 :                     lToolbarSettingsSeq[j].Value >>= xNewToolbarSettings;
    1333           0 :                     break;
    1334             :                 }
    1335             :             }
    1336             : 
    1337           0 :             break;
    1338             :         }
    1339             :     }
    1340             : 
    1341           0 :     return xNewToolbarSettings;
    1342             : }
    1343             : 
    1344           0 : void NewVersionUIInfo::init(const ::std::vector< MigrationModuleInfo >& vModulesInfo)
    1345             : {
    1346           0 :     m_lCfgManagerSeq.realloc(vModulesInfo.size());
    1347           0 :     m_lNewVersionMenubarSettingsSeq.realloc(vModulesInfo.size());
    1348           0 :     m_lNewVersionToolbarSettingsSeq.realloc(vModulesInfo.size());
    1349             : 
    1350           0 :     const OUString sMenubarResourceURL("private:resource/menubar/menubar");
    1351           0 :     const OUString sToolbarResourcePre("private:resource/toolbar/");
    1352             : 
    1353           0 :     uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xModuleCfgSupplier = ui::theModuleUIConfigurationManagerSupplier::get( ::comphelper::getProcessComponentContext() );
    1354             : 
    1355           0 :     for (sal_uInt32 i=0; i<vModulesInfo.size(); ++i)
    1356             :     {
    1357           0 :         OUString sModuleIdentifier = mapModuleShortNameToIdentifier(vModulesInfo[i].sModuleShortName);
    1358           0 :         if (!sModuleIdentifier.isEmpty())
    1359             :         {
    1360           0 :             uno::Reference< ui::XUIConfigurationManager > xCfgManager = xModuleCfgSupplier->getUIConfigurationManager(sModuleIdentifier);
    1361           0 :             m_lCfgManagerSeq[i].Name = vModulesInfo[i].sModuleShortName;
    1362           0 :             m_lCfgManagerSeq[i].Value <<= xCfgManager;
    1363             : 
    1364           0 :             if (vModulesInfo[i].bHasMenubar)
    1365             :             {
    1366           0 :                 m_lNewVersionMenubarSettingsSeq[i].Name = vModulesInfo[i].sModuleShortName;
    1367           0 :                 m_lNewVersionMenubarSettingsSeq[i].Value <<= xCfgManager->getSettings(sMenubarResourceURL, sal_True);
    1368             :             }
    1369             : 
    1370           0 :             sal_Int32 nToolbars = vModulesInfo[i].m_vToolbars.size();
    1371           0 :             if (nToolbars > 0)
    1372             :             {
    1373           0 :                 uno::Sequence< beans::PropertyValue > lPropSeq(nToolbars);
    1374           0 :                 for (sal_Int32 j=0; j<nToolbars; ++j)
    1375             :                 {
    1376           0 :                     OUString sToolbarName = vModulesInfo[i].m_vToolbars[j];
    1377           0 :                     OUString sToolbarResourceURL = sToolbarResourcePre + sToolbarName;
    1378             : 
    1379           0 :                     lPropSeq[j].Name = sToolbarName;
    1380           0 :                     lPropSeq[j].Value <<= xCfgManager->getSettings(sToolbarResourceURL, sal_True);
    1381           0 :                 }
    1382             : 
    1383           0 :                 m_lNewVersionToolbarSettingsSeq[i].Name = vModulesInfo[i].sModuleShortName;
    1384           0 :                 m_lNewVersionToolbarSettingsSeq[i].Value <<= lPropSeq;
    1385           0 :             }
    1386             :         }
    1387           0 :     }
    1388           0 : }
    1389             : 
    1390             : } // namespace desktop
    1391             : 
    1392             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10