LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/doc - doctempl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 64 773 8.3 %
Date: 2012-12-17 Functions: 17 83 20.5 %
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             : 
      21             : #include <limits.h>
      22             : #include <com/sun/star/uno/Any.h>
      23             : #include <osl/mutex.hxx>
      24             : #include <osl/thread.hxx>
      25             : 
      26             : #include <vcl/svapp.hxx>
      27             : #include <vcl/settings.hxx>
      28             : #include <unotools/localedatawrapper.hxx>
      29             : #include <unotools/pathoptions.hxx>
      30             : #include <tools/resary.hxx>
      31             : #include <tools/string.hxx>
      32             : #include <tools/urlobj.hxx>
      33             : #include <svtools/ehdl.hxx>
      34             : #include <svtools/sfxecode.hxx>
      35             : #include <comphelper/processfactory.hxx>
      36             : #include <ucbhelper/content.hxx>
      37             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      38             : #include <com/sun/star/beans/PropertyValue.hpp>
      39             : #include <com/sun/star/beans/XPropertyContainer.hpp>
      40             : #include <com/sun/star/beans/XPropertySet.hpp>
      41             : #include <com/sun/star/beans/XPropertySetInfo.hpp>
      42             : #include <com/sun/star/document/XTypeDetection.hpp>
      43             : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
      44             : #include <com/sun/star/frame/XComponentLoader.hpp>
      45             : #include <com/sun/star/frame/DocumentTemplates.hpp>
      46             : #include <com/sun/star/frame/XDocumentTemplates.hpp>
      47             : #include <com/sun/star/io/XInputStream.hpp>
      48             : #include <com/sun/star/io/XPersist.hpp>
      49             : #include <com/sun/star/lang/XLocalizable.hpp>
      50             : #include <com/sun/star/sdbc/XResultSet.hpp>
      51             : #include <com/sun/star/sdbc/XRow.hpp>
      52             : #include <com/sun/star/ucb/ContentInfo.hpp>
      53             : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
      54             : #include <com/sun/star/ucb/NameClash.hpp>
      55             : #include <com/sun/star/ucb/TransferInfo.hpp>
      56             : #include <com/sun/star/ucb/XCommandProcessor.hpp>
      57             : #include <com/sun/star/ucb/XContent.hpp>
      58             : #include <com/sun/star/ucb/XContentAccess.hpp>
      59             : #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
      60             : #include <com/sun/star/ucb/XAnyCompare.hpp>
      61             : #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
      62             : #include <com/sun/star/embed/ElementModes.hpp>
      63             : #include <com/sun/star/embed/XTransactedObject.hpp>
      64             : 
      65             : #include "sfxurlrelocator.hxx"
      66             : 
      67             : using namespace ::com::sun::star;
      68             : using namespace ::com::sun::star::beans;
      69             : using namespace ::com::sun::star::frame;
      70             : using namespace ::com::sun::star::io;
      71             : using namespace ::com::sun::star::lang;
      72             : using namespace ::com::sun::star::sdbc;
      73             : using namespace ::com::sun::star::uno;
      74             : using namespace ::com::sun::star::ucb;
      75             : using namespace ::com::sun::star::document;
      76             : using namespace ::rtl;
      77             : using namespace ::ucbhelper;
      78             : 
      79             : 
      80             : #include <sfx2/doctempl.hxx>
      81             : #include <sfx2/docfac.hxx>
      82             : #include <sfx2/docfile.hxx>
      83             : #include <sfx2/objsh.hxx>
      84             : #include "sfxtypes.hxx"
      85             : #include <sfx2/app.hxx>
      86             : #include "sfx2/sfxresid.hxx"
      87             : #include <sfx2/templatelocnames.hrc>
      88             : #include "doc.hrc"
      89             : #include <sfx2/fcontnr.hxx>
      90             : #include <svtools/templatefoldercache.hxx>
      91             : 
      92             : #include <comphelper/storagehelper.hxx>
      93             : #include <unotools/ucbhelper.hxx>
      94             : 
      95             : #include <vector>
      96             : using ::std::vector;
      97             : using ::std::advance;
      98             : 
      99             : //========================================================================
     100             : 
     101             : #define TITLE                   "Title"
     102             : #define TARGET_URL              "TargetURL"
     103             : 
     104             : #define TARGET_DIR_URL          "TargetDirURL"
     105             : #define COMMAND_TRANSFER        "transfer"
     106             : 
     107             : #define SERVICENAME_DOCINFO             "com.sun.star.document.DocumentProperties"
     108             : #define SERVICENAME_DESKTOP             "com.sun.star.frame.Desktop"
     109             : 
     110             : //========================================================================
     111             : 
     112             : class RegionData_Impl;
     113             : 
     114             : namespace DocTempl {
     115             : 
     116           0 : class DocTempl_EntryData_Impl
     117             : {
     118             :     RegionData_Impl*    mpParent;
     119             : 
     120             :     // the following member must be SfxObjectShellLock since it controlls that SfxObjectShell lifetime by design
     121             :     // and users of this class expect it to be so.
     122             :     SfxObjectShellLock  mxObjShell;
     123             : 
     124             :     OUString            maTitle;
     125             :     OUString            maOwnURL;
     126             :     OUString            maTargetURL;
     127             :     sal_Bool            mbIsOwner   : 1;
     128             :     sal_Bool            mbDidConvert: 1;
     129             : 
     130             : private:
     131           0 :     RegionData_Impl*    GetParent() const { return mpParent; }
     132             : 
     133             : public:
     134             :                         DocTempl_EntryData_Impl( RegionData_Impl* pParent,
     135             :                                         const OUString& rTitle );
     136             : 
     137           0 :     const OUString&     GetTitle() const { return maTitle; }
     138             :     const OUString&     GetTargetURL();
     139             :     const OUString&     GetHierarchyURL();
     140             : 
     141           0 :     void                SetTitle( const OUString& rTitle ) { maTitle = rTitle; }
     142           0 :     void                SetTargetURL( const OUString& rURL ) { maTargetURL = rURL; }
     143           0 :     void                SetHierarchyURL( const OUString& rURL) { maOwnURL = rURL; }
     144             : 
     145             :     int                 Compare( const OUString& rTitle ) const;
     146             : 
     147             :     SfxObjectShellRef   CreateObjectShell();
     148             :     sal_Bool                DeleteObjectShell();
     149             : };
     150             : 
     151             : }
     152             : 
     153             : using namespace ::DocTempl;
     154             : 
     155             : // ------------------------------------------------------------------------
     156             : 
     157             : class RegionData_Impl
     158             : {
     159             :     const SfxDocTemplate_Impl*  mpParent;
     160             :     vector< DocTempl_EntryData_Impl* > maEntries;
     161             :     OUString                    maTitle;
     162             :     OUString                    maOwnURL;
     163             :     OUString                    maTargetURL;
     164             : 
     165             : private:
     166             :     size_t                      GetEntryPos( const OUString& rTitle,
     167             :                                              sal_Bool& rFound ) const;
     168           0 :     const SfxDocTemplate_Impl*  GetParent() const { return mpParent; }
     169             : 
     170             : public:
     171             :                         RegionData_Impl( const SfxDocTemplate_Impl* pParent,
     172             :                                          const OUString& rTitle );
     173             :                         ~RegionData_Impl();
     174             : 
     175           0 :     void                SetTargetURL( const OUString& rURL ) { maTargetURL = rURL; }
     176           0 :     void                SetHierarchyURL( const OUString& rURL) { maOwnURL = rURL; }
     177             : 
     178             :     DocTempl_EntryData_Impl*     GetEntry( size_t nIndex ) const;
     179             :     DocTempl_EntryData_Impl*     GetEntry( const OUString& rName ) const;
     180             : 
     181           0 :     const OUString&     GetTitle() const { return maTitle; }
     182             :     const OUString&     GetTargetURL();
     183             :     const OUString&     GetHierarchyURL();
     184             : 
     185             :     size_t              GetCount() const;
     186             : 
     187           0 :     void                SetTitle( const OUString& rTitle ) { maTitle = rTitle; }
     188             : 
     189             :     void                AddEntry( const OUString& rTitle,
     190             :                                   const OUString& rTargetURL,
     191             :                                   size_t *pPos = NULL );
     192             :     void                DeleteEntry( size_t nIndex );
     193             : 
     194             :     int                 Compare( const OUString& rTitle ) const
     195             :                             { return maTitle.compareTo( rTitle ); }
     196             :     int                 Compare( RegionData_Impl* pCompareWith ) const;
     197             : };
     198             : 
     199             : typedef vector< RegionData_Impl* > RegionList_Impl;
     200             : 
     201             : // ------------------------------------------------------------------------
     202             : 
     203             : class SfxDocTemplate_Impl : public SvRefBase
     204             : {
     205             :     uno::Reference< XPersist >               mxInfo;
     206             :     uno::Reference< XDocumentTemplates >     mxTemplates;
     207             : 
     208             :     ::osl::Mutex        maMutex;
     209             :     OUString            maRootURL;
     210             :     OUString            maStandardGroup;
     211             :     RegionList_Impl     maRegions;
     212             :     sal_Bool            mbConstructed;
     213             : 
     214             :     uno::Reference< XAnyCompareFactory > m_rCompareFactory;
     215             : 
     216             :     // the following member is intended to prevent clearing of the global data when it is in use
     217             :     // TODO/LATER: it still does not make the implementation complete thread-safe
     218             :     sal_Int32           mnLockCounter;
     219             : 
     220             : private:
     221             :     void                Clear();
     222             : 
     223             : public:
     224             :                         SfxDocTemplate_Impl();
     225             :                         ~SfxDocTemplate_Impl();
     226             : 
     227             :     void                IncrementLock();
     228             :     void                DecrementLock();
     229             : 
     230             :     sal_Bool            Construct( );
     231             :     void                CreateFromHierarchy( Content &rTemplRoot );
     232             :     void                ReInitFromComponent();
     233             :     void                AddRegion( const OUString& rTitle,
     234             :                                    Content& rContent );
     235             : 
     236             :     void                Rescan();
     237             : 
     238             :     void                DeleteRegion( size_t nIndex );
     239             : 
     240           0 :     size_t              GetRegionCount() const
     241           0 :                             { return maRegions.size(); }
     242             :     RegionData_Impl*    GetRegion( const OUString& rName ) const;
     243             :     RegionData_Impl*    GetRegion( size_t nIndex ) const;
     244             : 
     245             :     sal_Bool            GetTitleFromURL( const OUString& rURL, OUString& aTitle );
     246             :     sal_Bool            InsertRegion( RegionData_Impl *pData, size_t nPos = size_t(-1) );
     247           0 :     OUString            GetRootURL() const { return maRootURL; }
     248             : 
     249           0 :     uno::Reference< XDocumentTemplates >     getDocTemplates() { return mxTemplates; }
     250             : };
     251             : 
     252             : // ------------------------------------------------------------------------
     253             : 
     254             : class DocTemplLocker_Impl
     255             : {
     256             :     SfxDocTemplate_Impl& m_aDocTempl;
     257             : public:
     258           8 :     DocTemplLocker_Impl( SfxDocTemplate_Impl& aDocTempl )
     259           8 :     : m_aDocTempl( aDocTempl )
     260             :     {
     261           8 :         m_aDocTempl.IncrementLock();
     262           8 :     }
     263             : 
     264           8 :     ~DocTemplLocker_Impl()
     265             :     {
     266           8 :         m_aDocTempl.DecrementLock();
     267           8 :     }
     268             : };
     269             : 
     270             : // ------------------------------------------------------------------------
     271             : 
     272             : #ifndef SFX_DECL_DOCTEMPLATES_DEFINED
     273             : #define SFX_DECL_DOCTEMPLATES_DEFINED
     274             : SV_DECL_REF(SfxDocTemplate_Impl)
     275             : #endif
     276             : 
     277          62 : SV_IMPL_REF(SfxDocTemplate_Impl)
     278             : 
     279             : // ------------------------------------------------------------------------
     280             : 
     281             : SfxDocTemplate_Impl *gpTemplateData = 0;
     282             : 
     283             : // -----------------------------------------------------------------------
     284             : 
     285             : static sal_Bool getTextProperty_Impl( Content& rContent,
     286             :                                       const OUString& rPropName,
     287             :                                       OUString& rPropValue );
     288             : 
     289             : //========================================================================
     290             : 
     291           0 : String SfxDocumentTemplates::GetFullRegionName
     292             : (
     293             :     sal_uInt16 nIdx                     // Region Index
     294             : )   const
     295             : 
     296             : /*  [Description]
     297             : 
     298             :     Returns the logical name of a region and its path
     299             : 
     300             :     [Return value]                 Reference to the Region name
     301             : 
     302             : */
     303             : 
     304             : {
     305             :     // First: find the RegionData for the index
     306           0 :     String aName;
     307             : 
     308           0 :     DocTemplLocker_Impl aLocker( *pImp );
     309             : 
     310           0 :     if ( pImp->Construct() )
     311             :     {
     312           0 :         RegionData_Impl *pData1 = pImp->GetRegion( nIdx );
     313             : 
     314           0 :         if ( pData1 )
     315           0 :             aName = pData1->GetTitle();
     316             : 
     317             :         // --**-- here was some code which appended the path to the
     318             :         //      group if there was more than one with the same name.
     319             :         //      this should not happen anymore
     320             :     }
     321             : 
     322           0 :     return aName;
     323             : }
     324             : 
     325             : //------------------------------------------------------------------------
     326             : 
     327           0 : const String& SfxDocumentTemplates::GetRegionName
     328             : (
     329             :     sal_uInt16 nIdx                 // Region Index
     330             : )   const
     331             : 
     332             : /*  [Description]
     333             : 
     334             :     Returns the logical name of a region
     335             : 
     336             :     [Return value]
     337             : 
     338             :     const String&                   Reference to the Region name
     339             : 
     340             : */
     341             : {
     342           0 :     static String maTmpString;
     343             : 
     344           0 :     DocTemplLocker_Impl aLocker( *pImp );
     345             : 
     346           0 :     if ( pImp->Construct() )
     347             :     {
     348           0 :         RegionData_Impl *pData = pImp->GetRegion( nIdx );
     349             : 
     350           0 :         if ( pData )
     351           0 :             maTmpString = pData->GetTitle();
     352             :         else
     353           0 :             maTmpString.Erase();
     354             :     }
     355             :     else
     356           0 :         maTmpString.Erase();
     357             : 
     358           0 :     return maTmpString;
     359             : }
     360             : 
     361             : //------------------------------------------------------------------------
     362             : 
     363           0 : sal_uInt16 SfxDocumentTemplates::GetRegionCount() const
     364             : 
     365             : /*  [Description]
     366             : 
     367             :     Returns the number of Regions
     368             : 
     369             :     [Return value]
     370             : 
     371             :     sal_uInt16                  Number of Regions
     372             : */
     373             : {
     374           0 :     DocTemplLocker_Impl aLocker( *pImp );
     375             : 
     376           0 :     if ( !pImp->Construct() )
     377           0 :         return 0;
     378             : 
     379           0 :     sal_uIntPtr nCount = pImp->GetRegionCount();
     380             : 
     381           0 :     return (sal_uInt16) nCount;
     382             : }
     383             : 
     384             : //------------------------------------------------------------------------
     385             : 
     386           0 : sal_Bool SfxDocumentTemplates::IsRegionLoaded( sal_uInt16 nIdx ) const
     387             : {
     388           0 :     DocTemplLocker_Impl aLocker( *pImp );
     389             : 
     390           0 :     if ( !pImp->Construct() )
     391           0 :         return sal_False;
     392             : 
     393           0 :     RegionData_Impl *pData = pImp->GetRegion( nIdx );
     394             : 
     395           0 :     if ( pData )
     396           0 :         return sal_True;
     397             :     else
     398           0 :         return sal_False;
     399             : }
     400             : 
     401             : //------------------------------------------------------------------------
     402             : 
     403           0 : sal_uInt16 SfxDocumentTemplates::GetCount
     404             : (
     405             :     sal_uInt16 nRegion              /* Region index whose number is
     406             :                                    to be determined */
     407             : 
     408             : )   const
     409             : 
     410             : /*  [Description]
     411             : 
     412             :     Number of entries in Region
     413             : 
     414             :     [Return value]                 Number of entries
     415             : */
     416             : 
     417             : {
     418           0 :     DocTemplLocker_Impl aLocker( *pImp );
     419             : 
     420           0 :     if ( !pImp->Construct() )
     421           0 :         return 0;
     422             : 
     423           0 :     RegionData_Impl *pData = pImp->GetRegion( nRegion );
     424           0 :     sal_uIntPtr            nCount = 0;
     425             : 
     426           0 :     if ( pData )
     427           0 :         nCount = pData->GetCount();
     428             : 
     429           0 :     return (sal_uInt16) nCount;
     430             : }
     431             : 
     432             : //------------------------------------------------------------------------
     433             : 
     434           0 : const String& SfxDocumentTemplates::GetName
     435             : (
     436             :     sal_uInt16 nRegion,     //  Region Index, in which the entry lies
     437             :     sal_uInt16 nIdx         //  Index of the entry
     438             : )   const
     439             : 
     440             : /*  [Description]
     441             : 
     442             :     Returns the logical name of an entry in Region
     443             : 
     444             :     [Return value]
     445             : 
     446             :     const String&           Entry Name
     447             : */
     448             : 
     449             : {
     450           0 :     DocTemplLocker_Impl aLocker( *pImp );
     451             : 
     452           0 :     static String maTmpString;
     453             : 
     454           0 :     if ( pImp->Construct() )
     455             :     {
     456           0 :         DocTempl_EntryData_Impl  *pEntry = NULL;
     457           0 :         RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
     458             : 
     459           0 :         if ( pRegion )
     460           0 :             pEntry = pRegion->GetEntry( nIdx );
     461             : 
     462           0 :         if ( pEntry )
     463           0 :             maTmpString = pEntry->GetTitle();
     464             :         else
     465           0 :             maTmpString.Erase();
     466             :     }
     467             :     else
     468           0 :         maTmpString.Erase();
     469             : 
     470           0 :     return maTmpString;
     471             : }
     472             : 
     473             : //------------------------------------------------------------------------
     474             : 
     475           0 : String SfxDocumentTemplates::GetFileName
     476             : (
     477             :     sal_uInt16 nRegion,     //  Region Index, in which the entry lies
     478             :     sal_uInt16 nIdx         //  Index of the entry
     479             : )   const
     480             : 
     481             : /*  [Description]
     482             : 
     483             :     Returns the file name of an entry in Region
     484             : 
     485             :     [Return value]                 File name of the entry
     486             : */
     487             : {
     488           0 :     DocTemplLocker_Impl aLocker( *pImp );
     489             : 
     490           0 :     if ( !pImp->Construct() )
     491           0 :         return String();
     492             : 
     493           0 :     DocTempl_EntryData_Impl  *pEntry = NULL;
     494           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
     495             : 
     496           0 :     if ( pRegion )
     497           0 :         pEntry = pRegion->GetEntry( nIdx );
     498             : 
     499           0 :     if ( pEntry )
     500             :     {
     501           0 :         INetURLObject aURLObj( pEntry->GetTargetURL() );
     502             :         return aURLObj.getName( INetURLObject::LAST_SEGMENT, true,
     503           0 :                                 INetURLObject::DECODE_WITH_CHARSET );
     504             :     }
     505             :     else
     506           0 :         return String();
     507             : }
     508             : 
     509             : //------------------------------------------------------------------------
     510             : 
     511           0 : String SfxDocumentTemplates::GetPath
     512             : (
     513             :     sal_uInt16  nRegion,    //  Region Index, in which the entry lies
     514             :     sal_uInt16  nIdx        //  Index of the entry
     515             : )   const
     516             : 
     517             : /*  [Description]
     518             : 
     519             :     Returns the file name with full path to the file assigned to an entry
     520             : 
     521             :     [Return value]
     522             : 
     523             :     String                  File name with full path
     524             : */
     525             : {
     526           0 :     DocTemplLocker_Impl aLocker( *pImp );
     527             : 
     528           0 :     if ( !pImp->Construct() )
     529           0 :         return String();
     530             : 
     531           0 :     DocTempl_EntryData_Impl  *pEntry = NULL;
     532           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
     533             : 
     534           0 :     if ( pRegion )
     535           0 :         pEntry = pRegion->GetEntry( nIdx );
     536             : 
     537           0 :     if ( pEntry )
     538           0 :         return pEntry->GetTargetURL();
     539             :     else
     540           0 :         return String();
     541             : }
     542             : 
     543             : //------------------------------------------------------------------------
     544             : 
     545           0 : ::rtl::OUString SfxDocumentTemplates::GetTemplateTargetURLFromComponent( const ::rtl::OUString& aGroupName,
     546             :                                                                          const ::rtl::OUString& aTitle )
     547             : {
     548           0 :     DocTemplLocker_Impl aLocker( *pImp );
     549             : 
     550           0 :     INetURLObject aTemplateObj( pImp->GetRootURL() );
     551             : 
     552             :     aTemplateObj.insertName( aGroupName, false,
     553             :                         INetURLObject::LAST_SEGMENT, true,
     554           0 :                         INetURLObject::ENCODE_ALL );
     555             : 
     556             :     aTemplateObj.insertName( aTitle, false,
     557             :                         INetURLObject::LAST_SEGMENT, true,
     558           0 :                         INetURLObject::ENCODE_ALL );
     559             : 
     560             : 
     561           0 :     ::rtl::OUString aResult;
     562           0 :     Content aTemplate;
     563           0 :     uno::Reference< XCommandEnvironment > aCmdEnv;
     564           0 :     if ( Content::create( aTemplateObj.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv, comphelper::getProcessComponentContext(), aTemplate ) )
     565             :     {
     566           0 :         OUString aPropName( TARGET_URL  );
     567           0 :         getTextProperty_Impl( aTemplate, aPropName, aResult );
     568           0 :         aResult = SvtPathOptions().SubstituteVariable( aResult );
     569             :     }
     570             : 
     571           0 :     return aResult;
     572             : }
     573             : 
     574             : //------------------------------------------------------------------------
     575             : 
     576             : /** Convert a resource string - a template name - to its localised pair if it exists.
     577             :     @param nSourceResIds
     578             :         Resource ID where the list of original en-US template names begin.
     579             :     @param nDestResIds
     580             :         Resource ID where the list of localised template names begin.
     581             :     @param nCount
     582             :         The number of names that have been localised.
     583             :     @param rString
     584             :         Name to be translated.
     585             :     @return
     586             :         The localised pair of rString or rString if the former does not exist.
     587             : */
     588           0 : OUString SfxDocumentTemplates::ConvertResourceString (
     589             :     int nSourceResIds, int nDestResIds, int nCount, const OUString& rString )
     590             : {
     591           0 :     for( int i = 0; i < nCount; ++i )
     592             :     {
     593           0 :         if( rString == SFX2_RESSTR(nSourceResIds + i))
     594           0 :             return SFX2_RESSTR(nDestResIds + i);
     595             :     }
     596           0 :     return rString;
     597             : }
     598             : 
     599             : //------------------------------------------------------------------------
     600             : 
     601           0 : sal_Bool SfxDocumentTemplates::CopyOrMove
     602             : (
     603             :     sal_uInt16  nTargetRegion,      //  Target Region Index
     604             :     sal_uInt16  nTargetIdx,         //  Target position Index
     605             :     sal_uInt16  nSourceRegion,      //  Source Region Index
     606             :     sal_uInt16  nSourceIdx,         /*  Index to be copied / to moved template */
     607             :     sal_Bool    bMove           //  Copy / Move
     608             : )
     609             : 
     610             : /*  [Description]
     611             : 
     612             :     Copy or move a document template
     613             : 
     614             :     [Return value]
     615             : 
     616             :     sal_Bool            sal_True,   Action could be performed
     617             :                         sal_False,  Action could not be performed
     618             : 
     619             :     [Cross-references]
     620             : 
     621             :     <SfxDocumentTemplates::Move(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16)>
     622             :     <SfxDocumentTemplates::Copy(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16)>
     623             : */
     624             : 
     625             : {
     626             :     /* to perform a copy or move, we need to send a transfer command to
     627             :        the destination folder with the URL of the source as parameter.
     628             :        ( If the destination content doesn't support the transfer command,
     629             :        we could try a copy ( and delete ) instead. )
     630             :        We need two transfers ( one for the real template and one for its
     631             :        representation in the hierarchy )
     632             :        ...
     633             :     */
     634             : 
     635           0 :     DocTemplLocker_Impl aLocker( *pImp );
     636             : 
     637           0 :     if ( !pImp->Construct() )
     638           0 :         return sal_False;
     639             : 
     640             :     // Don't copy or move any folders
     641           0 :     if( nSourceIdx == USHRT_MAX )
     642           0 :         return sal_False ;
     643             : 
     644           0 :     if ( nSourceRegion == nTargetRegion )
     645             :     {
     646             :         SAL_WARN( "sfx2.doc", "Don't know, what to do!" );
     647           0 :         return sal_False;
     648             :     }
     649             : 
     650           0 :     RegionData_Impl *pSourceRgn = pImp->GetRegion( nSourceRegion );
     651           0 :     if ( !pSourceRgn )
     652           0 :         return sal_False;
     653             : 
     654           0 :     DocTempl_EntryData_Impl *pSource = pSourceRgn->GetEntry( nSourceIdx );
     655           0 :     if ( !pSource )
     656           0 :         return sal_False;
     657             : 
     658           0 :     RegionData_Impl *pTargetRgn = pImp->GetRegion( nTargetRegion );
     659           0 :     if ( !pTargetRgn )
     660           0 :         return sal_False;
     661             : 
     662           0 :     OUString aTitle = pSource->GetTitle();
     663             : 
     664           0 :     uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
     665             : 
     666           0 :     if ( xTemplates->addTemplate( pTargetRgn->GetTitle(),
     667             :                                   aTitle,
     668           0 :                                   pSource->GetTargetURL() ) )
     669             :     {
     670             : 
     671           0 :         INetURLObject aSourceObj( pSource->GetTargetURL() );
     672             : 
     673           0 :         ::rtl::OUString aNewTargetURL = GetTemplateTargetURLFromComponent( pTargetRgn->GetTitle(), aTitle );
     674           0 :         if ( aNewTargetURL.isEmpty() )
     675           0 :             return sal_False;
     676             : 
     677           0 :         if ( bMove )
     678             :         {
     679             :             // --**-- delete the original file
     680           0 :             sal_Bool bDeleted = xTemplates->removeTemplate( pSourceRgn->GetTitle(),
     681           0 :                                                             pSource->GetTitle() );
     682           0 :             if ( bDeleted )
     683           0 :                 pSourceRgn->DeleteEntry( nSourceIdx );
     684             :             else
     685             :             {
     686           0 :                 if ( xTemplates->removeTemplate( pTargetRgn->GetTitle(), aTitle ) )
     687           0 :                     return sal_False; // will trigger tetry with copy instead of move
     688             : 
     689             :                 // if it is not possible to remove just created template ( must be possible! )
     690             :                 // it is better to report success here, since at least the copy has succeeded
     691             :                 // TODO/LATER: solve it more gracefully in future
     692             :             }
     693             :         }
     694             : 
     695             :         // todo: fix SfxDocumentTemplates to handle size_t instead of sal_uInt16
     696           0 :         size_t temp_nTargetIdx = nTargetIdx;
     697           0 :         pTargetRgn->AddEntry( aTitle, aNewTargetURL, &temp_nTargetIdx );
     698             : 
     699           0 :         return sal_True;
     700             :     }
     701             : 
     702             :     // --**-- if the current file is opened,
     703             :     // it must be re-opened afterwards.
     704             : 
     705           0 :     return sal_False;
     706             : }
     707             : 
     708             : //------------------------------------------------------------------------
     709             : 
     710           0 : sal_Bool SfxDocumentTemplates::Move
     711             : (
     712             :     sal_uInt16 nTargetRegion,       //  Target Region Index
     713             :     sal_uInt16 nTargetIdx,          //  Target position Index
     714             :     sal_uInt16 nSourceRegion,       //  Source Region Index
     715             :     sal_uInt16 nSourceIdx           /*  Index to be copied / to moved template */
     716             : )
     717             : 
     718             : /*  [Description]
     719             : 
     720             :     Moving a template
     721             : 
     722             :     [Return value]
     723             : 
     724             :     sal_Bool            sal_True,   Action could be performed
     725             :                         sal_False,  Action could not be performed
     726             : 
     727             :     [Cross-references]
     728             : 
     729             :     <SfxDocumentTemplates::CopyOrMove(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16,sal_Bool)>
     730             : */
     731             : {
     732           0 :     DocTemplLocker_Impl aLocker( *pImp );
     733             : 
     734             :     return CopyOrMove( nTargetRegion, nTargetIdx,
     735           0 :                        nSourceRegion, nSourceIdx, sal_True );
     736             : }
     737             : 
     738             : //------------------------------------------------------------------------
     739             : 
     740           0 : sal_Bool SfxDocumentTemplates::Copy
     741             : (
     742             :     sal_uInt16 nTargetRegion,       //  Target Region Index
     743             :     sal_uInt16 nTargetIdx,          //  Target position Index
     744             :     sal_uInt16 nSourceRegion,       //  Source Region Index
     745             :     sal_uInt16 nSourceIdx           /*  Index to be copied / to moved template */
     746             : )
     747             : 
     748             : /*  [Description]
     749             : 
     750             :     Copying a template
     751             : 
     752             :     [Return value]
     753             : 
     754             :     sal_Bool            sal_True,   Action could be performed
     755             :                         sal_False,  Action could not be performed
     756             : 
     757             :     [Cross-references]
     758             : 
     759             :     <SfxDocumentTemplates::CopyOrMove(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16,sal_Bool)>
     760             : */
     761             : 
     762             : {
     763           0 :     DocTemplLocker_Impl aLocker( *pImp );
     764             : 
     765             :     return CopyOrMove( nTargetRegion, nTargetIdx,
     766           0 :                        nSourceRegion, nSourceIdx, sal_False );
     767             : }
     768             : 
     769             : //------------------------------------------------------------------------
     770             : 
     771           0 : sal_Bool SfxDocumentTemplates::CopyTo
     772             : (
     773             :     sal_uInt16          nRegion,    //  Region of the template to be exported
     774             :     sal_uInt16          nIdx,       //  Index of the template to be exported
     775             :     const String&   rName       /*  File name under which the template is to
     776             :                                     be created */
     777             : )   const
     778             : 
     779             : /*  [Description]
     780             : 
     781             :     Exporting a template into the file system
     782             : 
     783             :     [Return value]
     784             : 
     785             :     sal_Bool            sal_True,   Action could be performed
     786             :                         sal_False,  Action could not be performed
     787             : 
     788             :     [Cross-references]
     789             : 
     790             :     <SfxDocumentTemplates::CopyFrom(sal_uInt16,sal_uInt16,String&)>
     791             : */
     792             : 
     793             : {
     794           0 :     DocTemplLocker_Impl aLocker( *pImp );
     795             : 
     796           0 :     if ( ! pImp->Construct() )
     797           0 :         return sal_False;
     798             : 
     799           0 :     RegionData_Impl *pSourceRgn = pImp->GetRegion( nRegion );
     800           0 :     if ( !pSourceRgn )
     801           0 :         return sal_False;
     802             : 
     803           0 :     DocTempl_EntryData_Impl *pSource = pSourceRgn->GetEntry( nIdx );
     804           0 :     if ( !pSource )
     805           0 :         return sal_False;
     806             : 
     807           0 :     INetURLObject aTargetURL( rName );
     808             : 
     809             :     OUString aTitle( aTargetURL.getName( INetURLObject::LAST_SEGMENT, true,
     810           0 :                                          INetURLObject::DECODE_WITH_CHARSET ) );
     811           0 :     aTargetURL.removeSegment();
     812             : 
     813           0 :     OUString aParentURL = aTargetURL.GetMainURL( INetURLObject::NO_DECODE );
     814             : 
     815           0 :     uno::Reference< XCommandEnvironment > aCmdEnv;
     816           0 :     Content aTarget;
     817             : 
     818             :     try
     819             :     {
     820           0 :         aTarget = Content( aParentURL, aCmdEnv, comphelper::getProcessComponentContext() );
     821             : 
     822           0 :         TransferInfo aTransferInfo;
     823           0 :         aTransferInfo.MoveData = sal_False;
     824           0 :         aTransferInfo.SourceURL = pSource->GetTargetURL();
     825           0 :         aTransferInfo.NewTitle = aTitle;
     826           0 :         aTransferInfo.NameClash = NameClash::OVERWRITE;
     827             : 
     828           0 :         Any aArg = makeAny( aTransferInfo );
     829           0 :         OUString aCmd( COMMAND_TRANSFER  );
     830             : 
     831           0 :         aTarget.executeCommand( aCmd, aArg );
     832             :     }
     833           0 :     catch ( ContentCreationException& )
     834           0 :     { return sal_False; }
     835           0 :     catch ( Exception& )
     836           0 :     { return sal_False; }
     837             : 
     838           0 :     return sal_True;
     839             : }
     840             : 
     841             : //------------------------------------------------------------------------
     842             : 
     843           0 : sal_Bool SfxDocumentTemplates::CopyFrom
     844             : (
     845             :     sal_uInt16      nRegion,        /*  Region in which the template is to be
     846             :                                     imported */
     847             :     sal_uInt16      nIdx,           //  Index of the new template in this Region
     848             :     String&     rName           /*  File name of the template to be imported
     849             :                                     as an out parameter of the (automatically
     850             :                                     generated from the file name) logical name
     851             :                                     of the template */
     852             : )
     853             : 
     854             : /*  [Description]
     855             : 
     856             :     Import a template from the file system
     857             : 
     858             :     [Return value]                 Sucess (sal_True) or serfpTargetDirectory->GetContent());
     859             : 
     860             :     sal_Bool            sal_True,   Action could be performed
     861             :                         sal_False,  Action could not be performed
     862             : 
     863             :     [Cross-references]
     864             : 
     865             :     <SfxDocumentTemplates::CopyTo(sal_uInt16,sal_uInt16,const String&)>
     866             : */
     867             : 
     868             : {
     869           0 :     DocTemplLocker_Impl aLocker( *pImp );
     870             : 
     871           0 :     if ( ! pImp->Construct() )
     872           0 :         return sal_False;
     873             : 
     874           0 :     RegionData_Impl *pTargetRgn = pImp->GetRegion( nRegion );
     875             : 
     876           0 :     if ( !pTargetRgn )
     877           0 :         return sal_False;
     878             : 
     879           0 :     uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
     880           0 :     if ( !xTemplates.is() )
     881           0 :         return sal_False;
     882             : 
     883           0 :     OUString aTitle;
     884           0 :     sal_Bool bTemplateAdded = sal_False;
     885             : 
     886           0 :     if( pImp->GetTitleFromURL( rName, aTitle ) )
     887             :     {
     888           0 :         bTemplateAdded = xTemplates->addTemplate( pTargetRgn->GetTitle(), aTitle, rName );
     889             :     }
     890             :     else
     891             :     {
     892           0 :         OUString aService( SERVICENAME_DESKTOP  );
     893           0 :         uno::Reference< XComponentLoader > xDesktop( ::comphelper::getProcessServiceFactory()->createInstance( aService ),
     894           0 :                                                 UNO_QUERY );
     895             : 
     896           0 :         Sequence< PropertyValue > aArgs( 1 );
     897           0 :         aArgs[0].Name = ::rtl::OUString("Hidden");
     898           0 :         aArgs[0].Value <<= sal_True;
     899             : 
     900           0 :         INetURLObject   aTemplURL( rName );
     901           0 :         uno::Reference< XDocumentPropertiesSupplier > xDocPropsSupplier;
     902           0 :         uno::Reference< XStorable > xStorable;
     903             :         try
     904             :         {
     905             :             xStorable = uno::Reference< XStorable >(
     906           0 :                 xDesktop->loadComponentFromURL( aTemplURL.GetMainURL(INetURLObject::NO_DECODE),
     907             :                                                 OUString("_blank"),
     908             :                                                 0,
     909           0 :                                                 aArgs ),
     910           0 :                 UNO_QUERY );
     911             : 
     912             :             xDocPropsSupplier = uno::Reference< XDocumentPropertiesSupplier >(
     913           0 :                 xStorable, UNO_QUERY );
     914             :         }
     915           0 :         catch( Exception& )
     916             :         {
     917             :         }
     918             : 
     919           0 :         if( xStorable.is() )
     920             :         {
     921             :             // get Title from XDocumentPropertiesSupplier
     922           0 :             if( xDocPropsSupplier.is() )
     923             :             {
     924             :                 uno::Reference< XDocumentProperties > xDocProps
     925           0 :                     = xDocPropsSupplier->getDocumentProperties();
     926           0 :                 if (xDocProps.is() ) {
     927           0 :                     aTitle = xDocProps->getTitle();
     928           0 :                 }
     929             :             }
     930             : 
     931           0 :             if( aTitle.isEmpty() )
     932             :             {
     933           0 :                 INetURLObject aURL( aTemplURL );
     934           0 :                 aURL.CutExtension();
     935             :                 aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true,
     936           0 :                                         INetURLObject::DECODE_WITH_CHARSET );
     937             :             }
     938             : 
     939             :             // write a template using XStorable interface
     940           0 :             bTemplateAdded = xTemplates->storeTemplate( pTargetRgn->GetTitle(), aTitle, xStorable );
     941           0 :         }
     942             :     }
     943             : 
     944             : 
     945           0 :     if( bTemplateAdded )
     946             :     {
     947           0 :         INetURLObject aTemplObj( pTargetRgn->GetHierarchyURL() );
     948             :         aTemplObj.insertName( aTitle, false,
     949             :                               INetURLObject::LAST_SEGMENT, true,
     950           0 :                               INetURLObject::ENCODE_ALL );
     951           0 :         OUString aTemplURL = aTemplObj.GetMainURL( INetURLObject::NO_DECODE );
     952             : 
     953           0 :         uno::Reference< XCommandEnvironment > aCmdEnv;
     954           0 :         Content aTemplCont;
     955             : 
     956           0 :         if( Content::create( aTemplURL, aCmdEnv, comphelper::getProcessComponentContext(), aTemplCont ) )
     957             :         {
     958           0 :             OUString aTemplName;
     959           0 :             OUString aPropName( TARGET_URL  );
     960             : 
     961           0 :             if( getTextProperty_Impl( aTemplCont, aPropName, aTemplName ) )
     962             :             {
     963           0 :                 if ( nIdx == USHRT_MAX )
     964           0 :                     nIdx = 0;
     965             :                 else
     966           0 :                     nIdx += 1;
     967             : 
     968             :                 // todo: fix SfxDocumentTemplates to handle size_t instead of sal_uInt16
     969           0 :                 size_t temp_nIdx = nIdx;
     970           0 :                 pTargetRgn->AddEntry( aTitle, aTemplName, &temp_nIdx );
     971           0 :                 rName = aTitle;
     972           0 :                 return sal_True;
     973             :             }
     974             :             else
     975             :             {
     976             :                 DBG_ASSERT( sal_False, "CopyFrom(): The content should contain target URL!" );
     977           0 :             }
     978             :         }
     979             :         else
     980             :         {
     981             :             DBG_ASSERT( sal_False, "CopyFrom(): The content just was created!" );
     982           0 :         }
     983             :     }
     984             : 
     985           0 :     return sal_False;
     986             : }
     987             : 
     988             : //------------------------------------------------------------------------
     989             : 
     990           0 : sal_Bool SfxDocumentTemplates::Delete
     991             : (
     992             :     sal_uInt16 nRegion,             //  Region Index
     993             :     sal_uInt16 nIdx                 /*  Index of the entry or USHRT_MAX,
     994             :                                     if a directory is meant. */
     995             : )
     996             : 
     997             : /*  [Description]
     998             : 
     999             :     Deleting an entry or a directory
    1000             : 
    1001             :     [Return value]
    1002             : 
    1003             :     sal_Bool            sal_True,   Action could be performed
    1004             :                         sal_False,  Action could not be performed
    1005             : 
    1006             :     [Cross-references]
    1007             : 
    1008             :     <SfxDocumentTemplates::InsertDir(const String&,sal_uInt16)>
    1009             :     <SfxDocumentTemplates::KillDir(SfxTemplateDir&)>
    1010             : */
    1011             : 
    1012             : {
    1013           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1014             : 
    1015             :     /* delete the template or folder in the hierarchy and in the
    1016             :        template folder by sending a delete command to the content.
    1017             :        Then remove the data from the lists
    1018             :     */
    1019           0 :     if ( ! pImp->Construct() )
    1020           0 :         return sal_False;
    1021             : 
    1022           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
    1023             : 
    1024           0 :     if ( !pRegion )
    1025           0 :         return sal_False;
    1026             : 
    1027             :     sal_Bool bRet;
    1028           0 :     uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
    1029             : 
    1030           0 :     if ( nIdx == USHRT_MAX )
    1031             :     {
    1032           0 :         bRet = xTemplates->removeGroup( pRegion->GetTitle() );
    1033           0 :         if ( bRet )
    1034           0 :             pImp->DeleteRegion( nRegion );
    1035             :     }
    1036             :     else
    1037             :     {
    1038           0 :         DocTempl_EntryData_Impl *pEntry = pRegion->GetEntry( nIdx );
    1039             : 
    1040           0 :         if ( !pEntry )
    1041           0 :             return sal_False;
    1042             : 
    1043           0 :         bRet = xTemplates->removeTemplate( pRegion->GetTitle(),
    1044           0 :                                            pEntry->GetTitle() );
    1045           0 :         if( bRet )
    1046           0 :             pRegion->DeleteEntry( nIdx );
    1047             :     }
    1048             : 
    1049           0 :     return bRet;
    1050             : }
    1051             : 
    1052             : //------------------------------------------------------------------------
    1053             : 
    1054           0 : sal_Bool SfxDocumentTemplates::InsertDir
    1055             : (
    1056             :     const String&   rText,      //  the logical name of the new Region
    1057             :     sal_uInt16          nRegion     //  Region Index
    1058             : )
    1059             : 
    1060             : /*  [Description]
    1061             : 
    1062             :     Insert an index
    1063             : 
    1064             :     [Return value]
    1065             : 
    1066             :     sal_Bool            sal_True,   Action could be performed
    1067             :                         sal_False,  Action could not be performed
    1068             : 
    1069             :     [Cross-references]
    1070             : 
    1071             :     <SfxDocumentTemplates::KillDir(SfxTemplateDir&)>
    1072             : */
    1073             : {
    1074           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1075             : 
    1076           0 :     if ( ! pImp->Construct() )
    1077           0 :         return sal_False;
    1078             : 
    1079           0 :     RegionData_Impl *pRegion = pImp->GetRegion( rText );
    1080             : 
    1081           0 :     if ( pRegion )
    1082           0 :         return sal_False;
    1083             : 
    1084           0 :     uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
    1085             : 
    1086           0 :     if ( xTemplates->addGroup( rText ) )
    1087             :     {
    1088           0 :         RegionData_Impl* pNewRegion = new RegionData_Impl( pImp, rText );
    1089             : 
    1090           0 :         if ( ! pImp->InsertRegion( pNewRegion, nRegion ) )
    1091             :         {
    1092           0 :             delete pNewRegion;
    1093           0 :             return sal_False;
    1094             :         }
    1095           0 :         return sal_True;
    1096             :     }
    1097             : 
    1098           0 :     return sal_False;
    1099             : }
    1100             : 
    1101             : //------------------------------------------------------------------------
    1102             : 
    1103           0 : sal_Bool SfxDocumentTemplates::SetName
    1104             : (
    1105             :     const String&   rName,      //  Der zu setzende Name
    1106             :     sal_uInt16          nRegion,    //  Region Index
    1107             :     sal_uInt16          nIdx        /*  Index of the entry oder USHRT_MAX,
    1108             :                                     if a directory is meant. */
    1109             : )
    1110             : 
    1111             : /*  [Description]
    1112             : 
    1113             :     Change the name of an entry or a directory
    1114             : 
    1115             :     [Return value]
    1116             : 
    1117             :     sal_Bool            sal_True,   Action could be performed
    1118             :                         sal_False,  Action could not be performed
    1119             : 
    1120             : */
    1121             : 
    1122             : {
    1123           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1124             : 
    1125           0 :     if ( ! pImp->Construct() )
    1126           0 :         return sal_False;
    1127             : 
    1128           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
    1129           0 :     DocTempl_EntryData_Impl *pEntry = NULL;
    1130             : 
    1131           0 :     if ( !pRegion )
    1132           0 :         return sal_False;
    1133             : 
    1134           0 :     uno::Reference< XDocumentTemplates > xTemplates = pImp->getDocTemplates();
    1135           0 :     OUString aEmpty;
    1136             : 
    1137           0 :     if ( nIdx == USHRT_MAX )
    1138             :     {
    1139           0 :         if ( pRegion->GetTitle() == OUString( rName ) )
    1140           0 :             return sal_True;
    1141             : 
    1142             :         // we have to rename a region
    1143           0 :         if ( xTemplates->renameGroup( pRegion->GetTitle(), rName ) )
    1144             :         {
    1145           0 :             pRegion->SetTitle( rName );
    1146           0 :             pRegion->SetTargetURL( aEmpty );
    1147           0 :             pRegion->SetHierarchyURL( aEmpty );
    1148           0 :             return sal_True;
    1149             :         }
    1150             :     }
    1151             :     else
    1152             :     {
    1153           0 :         pEntry = pRegion->GetEntry( nIdx );
    1154             : 
    1155           0 :         if ( !pEntry )
    1156           0 :             return sal_False;
    1157             : 
    1158           0 :         if ( pEntry->GetTitle() == OUString( rName ) )
    1159           0 :             return sal_True;
    1160             : 
    1161           0 :         if ( xTemplates->renameTemplate( pRegion->GetTitle(),
    1162           0 :                                          pEntry->GetTitle(),
    1163           0 :                                          rName ) )
    1164             :         {
    1165           0 :             pEntry->SetTitle( rName );
    1166           0 :             pEntry->SetTargetURL( aEmpty );
    1167           0 :             pEntry->SetHierarchyURL( aEmpty );
    1168           0 :             return sal_True;
    1169             :         }
    1170             :     }
    1171             : 
    1172           0 :     return sal_False;
    1173             : }
    1174             : 
    1175             : //------------------------------------------------------------------------
    1176             : 
    1177           0 : sal_Bool SfxDocumentTemplates::Rescan()
    1178             : 
    1179             : /*  [Description]
    1180             : 
    1181             :     Comparison of administrative data with the current state on disk.
    1182             :     The logical name for which no file exists, will be removed from the
    1183             :     administrative structure. Files for which no record exists will be included.
    1184             : 
    1185             :     [Return value]
    1186             : 
    1187             :     sal_Bool            sal_True,   Action could be performed
    1188             :                         sal_False,  Action could not be performed
    1189             : 
    1190             :     [Cross-references]
    1191             : 
    1192             :     <SfxTemplateDir::Scan(sal_Bool bDirectory, sal_Bool bSave)>
    1193             :     <SfxTemplateDir::Freshen(const SfxTemplateDir &rNew)>
    1194             : */
    1195             : {
    1196           0 :     if ( !pImp->Construct() )
    1197           0 :         return sal_False;
    1198             : 
    1199           0 :     pImp->Rescan();
    1200             : 
    1201           0 :     return sal_True;
    1202             : }
    1203             : 
    1204             : //------------------------------------------------------------------------
    1205             : 
    1206           0 : SfxObjectShellRef SfxDocumentTemplates::CreateObjectShell
    1207             : (
    1208             :     sal_uInt16 nRegion,         //  Region Index
    1209             :     sal_uInt16 nIdx             //  Index of the entry
    1210             : )
    1211             : 
    1212             : /*  [Description]
    1213             : 
    1214             :     Access to the document shell of an entry
    1215             : 
    1216             :     [Return value]
    1217             : 
    1218             :     SfxObjectShellRef         Referece to the ObjectShell
    1219             : 
    1220             :     [Cross-references]
    1221             : 
    1222             :     <SfxTemplateDirEntry::CreateObjectShell()>
    1223             :     <SfxDocumentTemplates::DeleteObjectShell(sal_uInt16, sal_uInt16)>
    1224             : */
    1225             : 
    1226             : {
    1227           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1228             : 
    1229           0 :     if ( !pImp->Construct() )
    1230           0 :         return NULL;
    1231             : 
    1232           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
    1233           0 :     DocTempl_EntryData_Impl *pEntry = NULL;
    1234             : 
    1235           0 :     if ( pRegion )
    1236           0 :         pEntry = pRegion->GetEntry( nIdx );
    1237             : 
    1238           0 :     if ( pEntry )
    1239           0 :         return pEntry->CreateObjectShell();
    1240             :     else
    1241           0 :         return NULL;
    1242             : }
    1243             : 
    1244             : //------------------------------------------------------------------------
    1245             : 
    1246           0 : sal_Bool SfxDocumentTemplates::DeleteObjectShell
    1247             : (
    1248             :     sal_uInt16 nRegion,         //  Region Index
    1249             :     sal_uInt16 nIdx             //  Index of the entry
    1250             : )
    1251             : 
    1252             : /*  [Description]
    1253             : 
    1254             :     Releasing the ObjectShell of an entry
    1255             : 
    1256             :     [Return value]
    1257             : 
    1258             :     sal_Bool            sal_True,   Action could be performed
    1259             :                         sal_False,  Action could not be performed
    1260             : 
    1261             :     [Cross-references]
    1262             : 
    1263             :     <SfxTemplateDirEntry::DeleteObjectShell()>
    1264             :     <SfxDocumentTemplates::CreateObjectShell(sal_uInt16, sal_uInt16)>
    1265             : */
    1266             : 
    1267             : {
    1268           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1269             : 
    1270           0 :     if ( ! pImp->Construct() )
    1271           0 :         return sal_True;
    1272             : 
    1273           0 :     RegionData_Impl *pRegion = pImp->GetRegion( nRegion );
    1274           0 :     DocTempl_EntryData_Impl *pEntry = NULL;
    1275             : 
    1276           0 :     if ( pRegion )
    1277           0 :         pEntry = pRegion->GetEntry( nIdx );
    1278             : 
    1279           0 :     if ( pEntry )
    1280           0 :         return pEntry->DeleteObjectShell();
    1281             :     else
    1282           0 :         return sal_True;
    1283             : }
    1284             : 
    1285             : //------------------------------------------------------------------------
    1286             : 
    1287           8 : sal_Bool SfxDocumentTemplates::GetFull
    1288             : (
    1289             :     const String &rRegion,      // Region Name
    1290             :     const String &rName,        // Template Name
    1291             :     String &rPath               // Out: Path + File name
    1292             : )
    1293             : 
    1294             : /*  [Description]
    1295             : 
    1296             :     Returns Path + File name of the template specified by rRegion and rName.
    1297             : 
    1298             :     [Return value]
    1299             : 
    1300             :     sal_Bool            sal_True,   Action could be performed
    1301             :                         sal_False,  Action could not be performed
    1302             : 
    1303             :     [Cross-references]
    1304             : 
    1305             :     <SfxDocumentTemplates::GetLogicNames(const String&,String&,String&)>
    1306             : */
    1307             : 
    1308             : {
    1309           8 :     DocTemplLocker_Impl aLocker( *pImp );
    1310             : 
    1311             :     // We don't search for empty names!
    1312           8 :     if ( ! rName.Len() )
    1313           0 :         return sal_False;
    1314             : 
    1315           8 :     if ( ! pImp->Construct() )
    1316           8 :         return sal_False;
    1317             : 
    1318           0 :     DocTempl_EntryData_Impl* pEntry = NULL;
    1319           0 :     const sal_uInt16 nCount = GetRegionCount();
    1320             : 
    1321           0 :     for ( sal_uInt16 i = 0; i < nCount; ++i )
    1322             :     {
    1323           0 :         RegionData_Impl *pRegion = pImp->GetRegion( i );
    1324             : 
    1325           0 :         if( pRegion &&
    1326           0 :             ( !rRegion.Len() || ( rRegion == String( pRegion->GetTitle() ) ) ) )
    1327             :         {
    1328           0 :             pEntry = pRegion->GetEntry( rName );
    1329             : 
    1330           0 :             if ( pEntry )
    1331             :             {
    1332           0 :                 rPath = pEntry->GetTargetURL();
    1333           0 :                 break;
    1334             :             }
    1335             :         }
    1336             :     }
    1337             : 
    1338           0 :     return ( pEntry != NULL );
    1339             : }
    1340             : 
    1341             : //------------------------------------------------------------------------
    1342             : 
    1343           0 : sal_Bool SfxDocumentTemplates::GetLogicNames
    1344             : (
    1345             :     const String &rPath,            // Full Path to the template
    1346             :     String &rRegion,                // Out: Region name
    1347             :     String &rName                   // Out: Template name
    1348             : ) const
    1349             : 
    1350             : /*  [Description]
    1351             : 
    1352             :     Returns and logical path name to the template specified by rPath
    1353             : 
    1354             :     [Return value]
    1355             : 
    1356             :     sal_Bool            sal_True,   Action could be performed
    1357             :                         sal_False,  Action could not be performed
    1358             : 
    1359             :     [Cross-references]
    1360             : 
    1361             :     <SfxDocumentTemplates::GetFull(const String&,const String&,DirEntry&)>
    1362             : */
    1363             : 
    1364             : {
    1365           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1366             : 
    1367           0 :     if ( ! pImp->Construct() )
    1368           0 :         return sal_False;
    1369             : 
    1370           0 :     INetURLObject aFullPath;
    1371             : 
    1372           0 :     aFullPath.SetSmartProtocol( INET_PROT_FILE );
    1373           0 :     aFullPath.SetURL( rPath );
    1374           0 :     OUString aPath( aFullPath.GetMainURL( INetURLObject::NO_DECODE ) );
    1375             : 
    1376           0 :     RegionData_Impl *pData = NULL;
    1377           0 :     DocTempl_EntryData_Impl  *pEntry = NULL;
    1378           0 :     sal_Bool         bFound = sal_False;
    1379             : 
    1380           0 :     sal_uIntPtr nCount = GetRegionCount();
    1381             : 
    1382           0 :     for ( sal_uIntPtr i=0; !bFound && (i<nCount); i++ )
    1383             :     {
    1384           0 :         pData = pImp->GetRegion( i );
    1385           0 :         if ( pData )
    1386             :         {
    1387           0 :             sal_uIntPtr nChildCount = pData->GetCount();
    1388             : 
    1389           0 :             for ( sal_uIntPtr j=0; !bFound && (j<nChildCount); j++ )
    1390             :             {
    1391           0 :                 pEntry = pData->GetEntry( j );
    1392           0 :                 if ( pEntry->GetTargetURL() == aPath )
    1393             :                 {
    1394           0 :                     bFound = sal_True;
    1395             :                 }
    1396             :             }
    1397             :         }
    1398             :     }
    1399             : 
    1400           0 :     if ( bFound )
    1401             :     {
    1402           0 :         rRegion = pData->GetTitle();
    1403           0 :         rName = pEntry->GetTitle();
    1404             :     }
    1405             : 
    1406           0 :     return bFound;
    1407             : }
    1408             : 
    1409             : //------------------------------------------------------------------------
    1410             : 
    1411           8 : SfxDocumentTemplates::SfxDocumentTemplates()
    1412             : 
    1413             : /*  [Description]
    1414             : 
    1415             :     Constructor
    1416             : */
    1417             : {
    1418           8 :     if ( !gpTemplateData )
    1419           8 :         gpTemplateData = new SfxDocTemplate_Impl;
    1420             : 
    1421           8 :     pImp = gpTemplateData;
    1422           8 : }
    1423             : 
    1424             : //-------------------------------------------------------------------------
    1425             : 
    1426           6 : void SfxDocumentTemplates::Construct()
    1427             : 
    1428             : //  Delayed build-up of administrative data
    1429             : 
    1430             : {
    1431           6 : }
    1432             : 
    1433             : //------------------------------------------------------------------------
    1434             : 
    1435          12 : SfxDocumentTemplates::~SfxDocumentTemplates()
    1436             : 
    1437             : /*  [Description]
    1438             : 
    1439             :     Destructor
    1440             :     Release of administrative data
    1441             : */
    1442             : 
    1443             : {
    1444           6 :     pImp = NULL;
    1445           6 : }
    1446             : 
    1447           0 : void SfxDocumentTemplates::Update( sal_Bool _bSmart )
    1448             : {
    1449           0 :     if  (   !_bSmart                                                // don't be smart
    1450           0 :         ||  ::svt::TemplateFolderCache( sal_True ).needsUpdate()    // update is really necessary
    1451             :         )
    1452             :     {
    1453           0 :         if ( pImp->Construct() )
    1454           0 :             pImp->Rescan();
    1455             :     }
    1456           0 : }
    1457             : 
    1458           0 : void SfxDocumentTemplates::ReInitFromComponent()
    1459             : {
    1460           0 :     pImp->ReInitFromComponent();
    1461           0 : }
    1462             : 
    1463             : 
    1464           0 : sal_Bool SfxDocumentTemplates::HasUserContents( sal_uInt16 nRegion, sal_uInt16 nIdx ) const
    1465             : {
    1466           0 :     DocTemplLocker_Impl aLocker( *pImp );
    1467             : 
    1468           0 :     sal_Bool bResult = sal_False;
    1469             : 
    1470           0 :     RegionData_Impl* pRegion = pImp->GetRegion( nRegion );
    1471             : 
    1472           0 :     if ( pRegion )
    1473             :     {
    1474           0 :         ::rtl::OUString aRegionTargetURL = pRegion->GetTargetURL();
    1475           0 :         if ( !aRegionTargetURL.isEmpty() )
    1476             :         {
    1477           0 :             sal_uInt16 nLen = 0;
    1478           0 :             sal_uInt16 nStartInd = 0;
    1479             : 
    1480           0 :             if( nIdx == USHRT_MAX )
    1481             :             {
    1482             :                 // this is a folder
    1483             :                 // check whether there is at least one editable template
    1484           0 :                 nLen = ( sal_uInt16 )pRegion->GetCount();
    1485           0 :                 nStartInd = 0;
    1486           0 :                 if ( nLen == 0 )
    1487           0 :                     bResult = sal_True; // the writing part of empty folder with writing URL can be removed
    1488             :             }
    1489             :             else
    1490             :             {
    1491             :                 // this is a template
    1492             :                 // check whether the template is inserted by user
    1493           0 :                 nLen = 1;
    1494           0 :                 nStartInd = nIdx;
    1495             :             }
    1496             : 
    1497           0 :             for ( sal_uInt16 nInd = nStartInd; nInd < nStartInd + nLen; nInd++ )
    1498             :             {
    1499           0 :                 DocTempl_EntryData_Impl* pEntryData = pRegion->GetEntry( nInd );
    1500           0 :                 if ( pEntryData )
    1501             :                 {
    1502           0 :                     ::rtl::OUString aEntryTargetURL = pEntryData->GetTargetURL();
    1503           0 :                     if ( !aEntryTargetURL.isEmpty()
    1504           0 :                       && ::utl::UCBContentHelper::IsSubPath( aRegionTargetURL, aEntryTargetURL ) )
    1505             :                     {
    1506           0 :                         bResult = sal_True;
    1507             :                         break;
    1508           0 :                     }
    1509             :                 }
    1510             :             }
    1511           0 :         }
    1512             :     }
    1513             : 
    1514           0 :     return bResult;
    1515             : }
    1516             : 
    1517             : // -----------------------------------------------------------------------
    1518           0 : DocTempl_EntryData_Impl::DocTempl_EntryData_Impl( RegionData_Impl* pParent,
    1519           0 :                                 const OUString& rTitle )
    1520             : {
    1521           0 :     mpParent    = pParent;
    1522             :     maTitle     = SfxDocumentTemplates::ConvertResourceString(
    1523           0 :                   STR_TEMPLATE_NAME1_DEF, STR_TEMPLATE_NAME1, NUM_TEMPLATE_NAMES, rTitle );
    1524           0 :     mbIsOwner   = sal_False;
    1525           0 :     mbDidConvert= sal_False;
    1526           0 : }
    1527             : 
    1528             : // -----------------------------------------------------------------------
    1529           0 : int DocTempl_EntryData_Impl::Compare( const OUString& rTitle ) const
    1530             : {
    1531           0 :     return maTitle.compareTo( rTitle );
    1532             : }
    1533             : 
    1534             : // -----------------------------------------------------------------------
    1535           0 : SfxObjectShellRef DocTempl_EntryData_Impl::CreateObjectShell()
    1536             : {
    1537           0 :     if( ! mxObjShell.Is() )
    1538             :     {
    1539           0 :         mbIsOwner = sal_False;
    1540           0 :         sal_Bool bDum = sal_False;
    1541           0 :         SfxApplication *pSfxApp = SFX_APP();
    1542           0 :         String          aTargetURL = GetTargetURL();
    1543             : 
    1544           0 :         mxObjShell = pSfxApp->DocAlreadyLoaded( aTargetURL, sal_True, bDum );
    1545             : 
    1546           0 :         if( ! mxObjShell.Is() )
    1547             :         {
    1548           0 :             mbIsOwner = sal_True;
    1549             :             SfxMedium *pMed=new SfxMedium(
    1550           0 :                 aTargetURL,(STREAM_STD_READWRITE | STREAM_SHARE_DENYALL) );
    1551           0 :             const SfxFilter* pFilter = NULL;
    1552           0 :             pMed->UseInteractionHandler(sal_True);
    1553           0 :             if( pSfxApp->GetFilterMatcher().GuessFilter(
    1554           0 :                 *pMed, &pFilter, SFX_FILTER_TEMPLATE, 0 ) ||
    1555           0 :                 (pFilter && !pFilter->IsOwnFormat()) ||
    1556           0 :                 (pFilter && !pFilter->UsesStorage()) )
    1557             :             {
    1558             :                 SfxErrorContext aEc( ERRCTX_SFX_LOADTEMPLATE,
    1559           0 :                                      aTargetURL );
    1560           0 :                 delete pMed;
    1561           0 :                 mbDidConvert=sal_True;
    1562             :                 sal_uIntPtr lErr;
    1563           0 :                 if ( mxObjShell.Is() ) {
    1564           0 :                     lErr = pSfxApp->LoadTemplate( mxObjShell,aTargetURL);
    1565           0 :                     if( lErr != ERRCODE_NONE )
    1566           0 :                         ErrorHandler::HandleError(lErr);
    1567           0 :                 }
    1568             : 
    1569             :             }
    1570           0 :             else if (pFilter)
    1571             :             {
    1572           0 :                 mbDidConvert=sal_False;
    1573           0 :                 mxObjShell = SfxObjectShell::CreateObject( pFilter->GetServiceName(), SFX_CREATE_MODE_ORGANIZER );
    1574           0 :                 if ( mxObjShell.Is() )
    1575             :                 {
    1576           0 :                     mxObjShell->DoInitNew(0);
    1577             :                     // TODO/LATER: make sure that we don't use binary templates!
    1578           0 :                     if( mxObjShell->LoadFrom( *pMed ) )
    1579             :                     {
    1580           0 :                         mxObjShell->DoSaveCompleted( pMed );
    1581             :                     }
    1582             :                     else
    1583           0 :                         mxObjShell.Clear();
    1584             :                 }
    1585             :             }
    1586           0 :         }
    1587             :     }
    1588             : 
    1589           0 :     return (SfxObjectShellRef)(SfxObjectShell*) mxObjShell;
    1590             : }
    1591             : 
    1592             : //------------------------------------------------------------------------
    1593           0 : sal_Bool DocTempl_EntryData_Impl::DeleteObjectShell()
    1594             : {
    1595           0 :     sal_Bool bRet = sal_True;
    1596             : 
    1597           0 :     if ( mxObjShell.Is() )
    1598             :     {
    1599           0 :         if( mxObjShell->IsModified() )
    1600             :         {
    1601             :             // Here we also save, if the Template is being processed ...
    1602           0 :             bRet = sal_False;
    1603             : 
    1604           0 :             if ( mbIsOwner )
    1605             :             {
    1606           0 :                 if( mbDidConvert )
    1607             :                 {
    1608             :                     bRet=mxObjShell->PreDoSaveAs_Impl(
    1609           0 :                         GetTargetURL(),
    1610           0 :                         mxObjShell->GetFactory().GetFilterContainer()->GetAnyFilter( SFX_FILTER_EXPORT | SFX_FILTER_IMPORT, SFX_FILTER_INTERNAL )->GetFilterName(), 0 );
    1611             :                 }
    1612             :                 else
    1613             :                 {
    1614           0 :                     if( mxObjShell->Save() )
    1615             :                     {
    1616           0 :                         uno::Reference< embed::XTransactedObject > xTransacted( mxObjShell->GetStorage(), uno::UNO_QUERY );
    1617             :                         DBG_ASSERT( xTransacted.is(), "Storage must implement XTransactedObject!\n" );
    1618           0 :                         if ( xTransacted.is() )
    1619             :                         {
    1620             :                             try
    1621             :                             {
    1622           0 :                                 xTransacted->commit();
    1623           0 :                                 bRet = sal_True;
    1624             :                             }
    1625           0 :                             catch( uno::Exception& )
    1626             :                             {
    1627             :                             }
    1628           0 :                         }
    1629             :                     }
    1630             :                 }
    1631             :             }
    1632             :         }
    1633             : 
    1634           0 :         if( bRet )
    1635             :         {
    1636           0 :             mxObjShell.Clear();
    1637             :         }
    1638             :     }
    1639           0 :     return bRet;
    1640             : }
    1641             : 
    1642             : // -----------------------------------------------------------------------
    1643           0 : const OUString& DocTempl_EntryData_Impl::GetHierarchyURL()
    1644             : {
    1645           0 :     if ( maOwnURL.isEmpty() )
    1646             :     {
    1647           0 :         INetURLObject aTemplateObj( GetParent()->GetHierarchyURL() );
    1648             : 
    1649           0 :         aTemplateObj.insertName( GetTitle(), false,
    1650             :                      INetURLObject::LAST_SEGMENT, true,
    1651           0 :                      INetURLObject::ENCODE_ALL );
    1652             : 
    1653           0 :         maOwnURL = aTemplateObj.GetMainURL( INetURLObject::NO_DECODE );
    1654           0 :         DBG_ASSERT( !maOwnURL.isEmpty(), "GetHierarchyURL(): Could not create URL!" );
    1655             :     }
    1656             : 
    1657           0 :     return maOwnURL;
    1658             : }
    1659             : 
    1660             : // -----------------------------------------------------------------------
    1661           0 : const OUString& DocTempl_EntryData_Impl::GetTargetURL()
    1662             : {
    1663           0 :     if ( maTargetURL.isEmpty() )
    1664             :     {
    1665           0 :         uno::Reference< XCommandEnvironment > aCmdEnv;
    1666           0 :         Content aRegion;
    1667             : 
    1668           0 :         if ( Content::create( GetHierarchyURL(), aCmdEnv, comphelper::getProcessComponentContext(), aRegion ) )
    1669             :         {
    1670           0 :             OUString aPropName( TARGET_URL  );
    1671             : 
    1672           0 :             getTextProperty_Impl( aRegion, aPropName, maTargetURL );
    1673             :         }
    1674             :         else
    1675             :         {
    1676             :             SAL_WARN( "sfx2.doc", "GetTargetURL(): Could not create hierarchy content!" );
    1677           0 :         }
    1678             :     }
    1679             : 
    1680           0 :     return maTargetURL;
    1681             : }
    1682             : 
    1683             : // -----------------------------------------------------------------------
    1684           0 : RegionData_Impl::RegionData_Impl( const SfxDocTemplate_Impl* pParent,
    1685           0 :                                   const OUString& rTitle )
    1686             : {
    1687           0 :     maTitle     = rTitle;
    1688           0 :     mpParent    = pParent;
    1689           0 : }
    1690             : 
    1691             : // -----------------------------------------------------------------------
    1692           0 : RegionData_Impl::~RegionData_Impl()
    1693             : {
    1694           0 :     for ( size_t i = 0, n = maEntries.size(); i < n; ++i )
    1695           0 :         delete maEntries[ i ];
    1696           0 :     maEntries.clear();
    1697           0 : }
    1698             : 
    1699             : // -----------------------------------------------------------------------
    1700           0 : size_t RegionData_Impl::GetEntryPos( const OUString& rTitle, sal_Bool& rFound ) const
    1701             : {
    1702             : #if 1   // Don't use binary search today
    1703             :     size_t i;
    1704           0 :     size_t nCount = maEntries.size();
    1705             : 
    1706           0 :     for ( i=0; i<nCount; i++ )
    1707             :     {
    1708           0 :         DocTempl_EntryData_Impl *pData = maEntries[ i ];
    1709             : 
    1710           0 :         if ( pData->Compare( rTitle ) == 0 )
    1711             :         {
    1712           0 :             rFound = sal_True;
    1713           0 :             return i;
    1714             :         }
    1715             :     }
    1716             : 
    1717           0 :     rFound = sal_False;
    1718           0 :     return i;
    1719             : 
    1720             : #else
    1721             :     // use binary search to find the correct position
    1722             :     // in the maEntries list
    1723             : 
    1724             :     int     nCompVal = 1;
    1725             :     size_t  nStart = 0;
    1726             :     size_t  nEnd = maEntries.size() - 1;
    1727             :     size_t  nMid;
    1728             : 
    1729             :     DocTempl_EntryData_Impl* pMid;
    1730             : 
    1731             :     rFound = sal_False;
    1732             : 
    1733             :     while ( nCompVal && ( nStart <= nEnd ) )
    1734             :     {
    1735             :         nMid = ( nEnd - nStart ) / 2 + nStart;
    1736             :         pMid = maEntries[ nMid ];
    1737             : 
    1738             :         nCompVal = pMid->Compare( rTitle );
    1739             : 
    1740             :         if ( nCompVal < 0 )     // pMid < pData
    1741             :             nStart = nMid + 1;
    1742             :         else
    1743             :             nEnd = nMid - 1;
    1744             :     }
    1745             : 
    1746             :     if ( nCompVal == 0 )
    1747             :     {
    1748             :         rFound = sal_True;
    1749             :     }
    1750             :     else
    1751             :     {
    1752             :         if ( nCompVal < 0 )     // pMid < pData
    1753             :             nMid++;
    1754             :     }
    1755             : 
    1756             :     return nMid;
    1757             : #endif
    1758             : }
    1759             : 
    1760             : // -----------------------------------------------------------------------
    1761           0 : void RegionData_Impl::AddEntry( const OUString& rTitle,
    1762             :                                 const OUString& rTargetURL,
    1763             :                                 size_t *pPos )
    1764             : {
    1765           0 :     INetURLObject aLinkObj( GetHierarchyURL() );
    1766             :     aLinkObj.insertName( rTitle, false,
    1767             :                       INetURLObject::LAST_SEGMENT, true,
    1768           0 :                       INetURLObject::ENCODE_ALL );
    1769           0 :     OUString aLinkURL = aLinkObj.GetMainURL( INetURLObject::NO_DECODE );
    1770             : 
    1771             :     DocTempl_EntryData_Impl* pEntry;
    1772           0 :     sal_Bool        bFound = sal_False;
    1773           0 :     size_t          nPos = GetEntryPos( rTitle, bFound );
    1774             : 
    1775           0 :     if ( bFound )
    1776             :     {
    1777           0 :         pEntry = maEntries[ nPos ];
    1778             :     }
    1779             :     else
    1780             :     {
    1781           0 :         if ( pPos )
    1782           0 :             nPos = *pPos;
    1783             : 
    1784           0 :         pEntry = new DocTempl_EntryData_Impl( this, rTitle );
    1785           0 :         pEntry->SetTargetURL( rTargetURL );
    1786           0 :         pEntry->SetHierarchyURL( aLinkURL );
    1787           0 :         if ( nPos < maEntries.size() ) {
    1788           0 :             vector< DocTempl_EntryData_Impl* >::iterator it = maEntries.begin();
    1789           0 :             advance( it, nPos );
    1790           0 :             maEntries.insert( it, pEntry );
    1791             :         }
    1792             :         else
    1793           0 :             maEntries.push_back( pEntry );
    1794           0 :     }
    1795           0 : }
    1796             : 
    1797             : // -----------------------------------------------------------------------
    1798           0 : size_t RegionData_Impl::GetCount() const
    1799             : {
    1800           0 :     return maEntries.size();
    1801             : }
    1802             : 
    1803             : // -----------------------------------------------------------------------
    1804           0 : const OUString& RegionData_Impl::GetHierarchyURL()
    1805             : {
    1806           0 :     if ( maOwnURL.isEmpty() )
    1807             :     {
    1808           0 :         INetURLObject aRegionObj( GetParent()->GetRootURL() );
    1809             : 
    1810           0 :         aRegionObj.insertName( GetTitle(), false,
    1811             :                      INetURLObject::LAST_SEGMENT, true,
    1812           0 :                      INetURLObject::ENCODE_ALL );
    1813             : 
    1814           0 :         maOwnURL = aRegionObj.GetMainURL( INetURLObject::NO_DECODE );
    1815           0 :         DBG_ASSERT( !maOwnURL.isEmpty(), "GetHierarchyURL(): Could not create URL!" );
    1816             :     }
    1817             : 
    1818           0 :     return maOwnURL;
    1819             : }
    1820             : 
    1821             : // -----------------------------------------------------------------------
    1822           0 : const OUString& RegionData_Impl::GetTargetURL()
    1823             : {
    1824           0 :     if ( maTargetURL.isEmpty() )
    1825             :     {
    1826           0 :         uno::Reference< XCommandEnvironment > aCmdEnv;
    1827           0 :         Content aRegion;
    1828             : 
    1829           0 :         if ( Content::create( GetHierarchyURL(), aCmdEnv, comphelper::getProcessComponentContext(), aRegion ) )
    1830             :         {
    1831           0 :             OUString aPropName( TARGET_DIR_URL  );
    1832             : 
    1833           0 :             getTextProperty_Impl( aRegion, aPropName, maTargetURL );
    1834             :             // The targeturl must be substituted: $(baseinsturl) (#i32656#)
    1835           0 :             maTargetURL = SvtPathOptions().SubstituteVariable( maTargetURL );
    1836             :         }
    1837             :         else
    1838             :         {
    1839             :             SAL_WARN( "sfx2.doc", "GetTargetURL(): Could not create hierarchy content!" );
    1840           0 :         }
    1841             :     }
    1842             : 
    1843           0 :     return maTargetURL;
    1844             : }
    1845             : 
    1846             : // -----------------------------------------------------------------------
    1847           0 : DocTempl_EntryData_Impl* RegionData_Impl::GetEntry( const OUString& rName ) const
    1848             : {
    1849           0 :     sal_Bool    bFound = sal_False;
    1850           0 :     long        nPos = GetEntryPos( rName, bFound );
    1851             : 
    1852           0 :     if ( bFound )
    1853           0 :         return maEntries[ nPos ];
    1854             :     else
    1855           0 :         return NULL;
    1856             : }
    1857             : 
    1858             : // -----------------------------------------------------------------------
    1859           0 : DocTempl_EntryData_Impl* RegionData_Impl::GetEntry( size_t nIndex ) const
    1860             : {
    1861           0 :     if ( nIndex < maEntries.size() )
    1862           0 :         return maEntries[ nIndex ];
    1863           0 :     return NULL;
    1864             : }
    1865             : 
    1866             : // -----------------------------------------------------------------------
    1867           0 : void RegionData_Impl::DeleteEntry( size_t nIndex )
    1868             : {
    1869           0 :     if ( nIndex < maEntries.size() )
    1870             :     {
    1871           0 :         delete maEntries[ nIndex ];
    1872           0 :         vector< DocTempl_EntryData_Impl*>::iterator it = maEntries.begin();
    1873           0 :         advance( it, nIndex );
    1874           0 :         maEntries.erase( it );
    1875             :     }
    1876           0 : }
    1877             : 
    1878             : // -----------------------------------------------------------------------
    1879           0 : int RegionData_Impl::Compare( RegionData_Impl* pCompare ) const
    1880             : {
    1881           0 :     int nCompare = maTitle.compareTo( pCompare->maTitle );
    1882             : 
    1883           0 :     return nCompare;
    1884             : }
    1885             : 
    1886             : // -----------------------------------------------------------------------
    1887             : 
    1888           8 : SfxDocTemplate_Impl::SfxDocTemplate_Impl()
    1889             : : mbConstructed( sal_False )
    1890           8 : , mnLockCounter( 0 )
    1891             : {
    1892           8 : }
    1893             : 
    1894             : // -----------------------------------------------------------------------
    1895          18 : SfxDocTemplate_Impl::~SfxDocTemplate_Impl()
    1896             : {
    1897           6 :     Clear();
    1898             : 
    1899           6 :     gpTemplateData = NULL;
    1900          12 : }
    1901             : 
    1902             : // -----------------------------------------------------------------------
    1903           8 : void SfxDocTemplate_Impl::IncrementLock()
    1904             : {
    1905           8 :     ::osl::MutexGuard aGuard( maMutex );
    1906           8 :     mnLockCounter++;
    1907           8 : }
    1908             : 
    1909             : // -----------------------------------------------------------------------
    1910           8 : void SfxDocTemplate_Impl::DecrementLock()
    1911             : {
    1912           8 :     ::osl::MutexGuard aGuard( maMutex );
    1913           8 :     if ( mnLockCounter )
    1914           8 :         mnLockCounter--;
    1915           8 : }
    1916             : 
    1917             : // -----------------------------------------------------------------------
    1918           0 : RegionData_Impl* SfxDocTemplate_Impl::GetRegion( size_t nIndex ) const
    1919             : {
    1920           0 :     if ( nIndex < maRegions.size() )
    1921           0 :         return maRegions[ nIndex ];
    1922           0 :     return NULL;
    1923             : }
    1924             : 
    1925             : // -----------------------------------------------------------------------
    1926           0 : RegionData_Impl* SfxDocTemplate_Impl::GetRegion( const OUString& rName )
    1927             :     const
    1928             : {
    1929           0 :     for ( size_t i = 0, n = maRegions.size(); i < n; ++i )
    1930             :     {
    1931           0 :         RegionData_Impl* pData = maRegions[ i ];
    1932           0 :         if( pData->GetTitle() == rName )
    1933           0 :             return pData;
    1934             :     }
    1935           0 :     return NULL;
    1936             : }
    1937             : 
    1938             : // -----------------------------------------------------------------------
    1939           0 : void SfxDocTemplate_Impl::DeleteRegion( size_t nIndex )
    1940             : {
    1941           0 :     if ( nIndex < maRegions.size() )
    1942             :     {
    1943           0 :         delete maRegions[ nIndex ];
    1944           0 :         RegionList_Impl::iterator it = maRegions.begin();
    1945           0 :         advance( it, nIndex );
    1946           0 :         maRegions.erase( it );
    1947             :     }
    1948           0 : }
    1949             : 
    1950             : // -----------------------------------------------------------------------
    1951             : /*  AddRegion adds a Region to the RegionList
    1952             : */
    1953           0 : void SfxDocTemplate_Impl::AddRegion( const OUString& rTitle,
    1954             :                                      Content& rContent )
    1955             : {
    1956             :     RegionData_Impl* pRegion;
    1957           0 :     pRegion = new RegionData_Impl( this, rTitle );
    1958             : 
    1959           0 :     if ( ! InsertRegion( pRegion ) )
    1960             :     {
    1961           0 :         delete pRegion;
    1962           0 :         return;
    1963             :     }
    1964             : 
    1965             :     // now get the content of the region
    1966           0 :     uno::Reference< XResultSet > xResultSet;
    1967           0 :     Sequence< OUString > aProps(2);
    1968           0 :     aProps[0] = OUString(TITLE );
    1969           0 :     aProps[1] = OUString(TARGET_URL );
    1970             : 
    1971             :     try
    1972             :     {
    1973           0 :         ResultSetInclude eInclude = INCLUDE_DOCUMENTS_ONLY;
    1974           0 :         Sequence< NumberedSortingInfo >     aSortingInfo(1);
    1975           0 :         aSortingInfo.getArray()->ColumnIndex = 1;
    1976           0 :         aSortingInfo.getArray()->Ascending = sal_True;
    1977           0 :         xResultSet = rContent.createSortedCursor( aProps, aSortingInfo, m_rCompareFactory, eInclude );
    1978             :     }
    1979           0 :     catch ( Exception& ) {}
    1980             : 
    1981           0 :     if ( xResultSet.is() )
    1982             :     {
    1983           0 :         uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
    1984           0 :         uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
    1985             : 
    1986             :         try
    1987             :         {
    1988           0 :             while ( xResultSet->next() )
    1989             :             {
    1990           0 :                 OUString aTitle( xRow->getString( 1 ) );
    1991           0 :                 OUString aTargetDir( xRow->getString( 2 ) );
    1992             : 
    1993           0 :                 pRegion->AddEntry( aTitle, aTargetDir );
    1994           0 :             }
    1995             :         }
    1996           0 :         catch ( Exception& ) {}
    1997           0 :     }
    1998             : }
    1999             : 
    2000             : // -----------------------------------------------------------------------
    2001           0 : void SfxDocTemplate_Impl::CreateFromHierarchy( Content &rTemplRoot )
    2002             : {
    2003           0 :     uno::Reference< XResultSet > xResultSet;
    2004           0 :     Sequence< OUString > aProps(1);
    2005           0 :     aProps[0] = OUString(TITLE );
    2006             : 
    2007             :     try
    2008             :     {
    2009           0 :         ResultSetInclude eInclude = INCLUDE_FOLDERS_ONLY;
    2010           0 :         Sequence< NumberedSortingInfo >     aSortingInfo(1);
    2011           0 :         aSortingInfo.getArray()->ColumnIndex = 1;
    2012           0 :         aSortingInfo.getArray()->Ascending = sal_True;
    2013           0 :         xResultSet = rTemplRoot.createSortedCursor( aProps, aSortingInfo, m_rCompareFactory, eInclude );
    2014             :     }
    2015           0 :     catch ( Exception& ) {}
    2016             : 
    2017           0 :     if ( xResultSet.is() )
    2018             :     {
    2019           0 :         uno::Reference< XCommandEnvironment > aCmdEnv;
    2020           0 :         uno::Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
    2021           0 :         uno::Reference< XRow > xRow( xResultSet, UNO_QUERY );
    2022             : 
    2023             :         try
    2024             :         {
    2025           0 :             while ( xResultSet->next() )
    2026             :             {
    2027           0 :                 OUString aTitle( xRow->getString( 1 ) );
    2028             : 
    2029           0 :                 OUString aId = xContentAccess->queryContentIdentifierString();
    2030           0 :                 Content  aContent = Content( aId, aCmdEnv, comphelper::getProcessComponentContext() );
    2031             : 
    2032           0 :                 AddRegion( aTitle, aContent );
    2033           0 :             }
    2034             :         }
    2035           0 :         catch ( Exception& ) {}
    2036           0 :     }
    2037           0 : }
    2038             : 
    2039             : // ------------------------------------------------------------------------
    2040           8 : sal_Bool SfxDocTemplate_Impl::Construct( )
    2041             : {
    2042           8 :     ::osl::MutexGuard aGuard( maMutex );
    2043             : 
    2044           8 :     if ( mbConstructed )
    2045           0 :         return sal_True;
    2046             : 
    2047           8 :     uno::Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
    2048           8 :     uno::Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
    2049             : 
    2050           8 :     OUString aService( SERVICENAME_DOCINFO  );
    2051           8 :     uno::Reference< XPersist > xInfo( xFactory->createInstance( aService ), UNO_QUERY );
    2052           8 :     mxInfo = xInfo;
    2053             : 
    2054           8 :     mxTemplates = frame::DocumentTemplates::create(xContext);
    2055             : 
    2056           8 :     uno::Reference< XLocalizable > xLocalizable( mxTemplates, UNO_QUERY );
    2057             : 
    2058           8 :     Sequence< Any > aCompareArg(1);
    2059           8 :     *(aCompareArg.getArray()) <<= xLocalizable->getLocale();
    2060             :     m_rCompareFactory = uno::Reference< XAnyCompareFactory >(
    2061           8 :                     xFactory->createInstanceWithArguments( OUString("com.sun.star.ucb.AnyCompareFactory"),
    2062           8 :                                                            aCompareArg ),
    2063           8 :                     UNO_QUERY );
    2064             : 
    2065           8 :     uno::Reference < XContent > aRootContent = mxTemplates->getContent();
    2066           8 :     uno::Reference < XCommandEnvironment > aCmdEnv;
    2067             : 
    2068           8 :     if ( ! aRootContent.is() )
    2069           8 :         return sal_False;
    2070             : 
    2071           0 :     mbConstructed = sal_True;
    2072           0 :     maRootURL = aRootContent->getIdentifier()->getContentIdentifier();
    2073             : 
    2074           0 :     ResStringArray  aLongNames( SfxResId( TEMPLATE_LONG_NAMES_ARY ) );
    2075             : 
    2076           0 :     if ( aLongNames.Count() )
    2077           0 :         maStandardGroup = aLongNames.GetString( 0 );
    2078             : 
    2079           0 :     Content aTemplRoot( aRootContent, aCmdEnv, comphelper::getProcessComponentContext() );
    2080           0 :     CreateFromHierarchy( aTemplRoot );
    2081             : 
    2082           0 :     return sal_True;
    2083             : }
    2084             : 
    2085             : // -----------------------------------------------------------------------
    2086           0 : void SfxDocTemplate_Impl::ReInitFromComponent()
    2087             : {
    2088           0 :     uno::Reference< XDocumentTemplates > xTemplates = getDocTemplates();
    2089           0 :     if ( xTemplates.is() )
    2090             :     {
    2091           0 :         uno::Reference < XContent > aRootContent = xTemplates->getContent();
    2092           0 :         uno::Reference < XCommandEnvironment > aCmdEnv;
    2093           0 :         Content aTemplRoot( aRootContent, aCmdEnv, comphelper::getProcessComponentContext() );
    2094           0 :         Clear();
    2095           0 :         CreateFromHierarchy( aTemplRoot );
    2096           0 :     }
    2097           0 : }
    2098             : 
    2099             : // -----------------------------------------------------------------------
    2100           0 : sal_Bool SfxDocTemplate_Impl::InsertRegion( RegionData_Impl *pNew, size_t nPos )
    2101             : {
    2102           0 :     ::osl::MutexGuard   aGuard( maMutex );
    2103             : 
    2104             :     // return false (not inserted) if the entry already exists
    2105           0 :     for ( size_t i = 0, n = maRegions.size(); i < n; ++i )
    2106           0 :         if ( maRegions[ i ]->Compare( pNew ) == 0 )
    2107           0 :             return sal_False;
    2108             : 
    2109           0 :     size_t newPos = nPos;
    2110           0 :     if ( pNew->GetTitle() == maStandardGroup )
    2111           0 :         newPos = 0;
    2112             : 
    2113           0 :     if ( newPos < maRegions.size() )
    2114             :     {
    2115           0 :         RegionList_Impl::iterator it = maRegions.begin();
    2116           0 :         advance( it, newPos );
    2117           0 :         maRegions.insert( it, pNew );
    2118             :     }
    2119             :     else
    2120           0 :         maRegions.push_back( pNew );
    2121             : 
    2122           0 :     return sal_True;
    2123             : }
    2124             : 
    2125             : // -----------------------------------------------------------------------
    2126           0 : void SfxDocTemplate_Impl::Rescan()
    2127             : {
    2128           0 :     Clear();
    2129             : 
    2130             :     try
    2131             :     {
    2132           0 :         uno::Reference< XDocumentTemplates > xTemplates = getDocTemplates();
    2133             :         DBG_ASSERT( xTemplates.is(), "SfxDocTemplate_Impl::Rescan:invalid template instance!" );
    2134           0 :         if ( xTemplates.is() )
    2135             :         {
    2136           0 :             xTemplates->update();
    2137             : 
    2138           0 :             uno::Reference < XContent > aRootContent = xTemplates->getContent();
    2139           0 :             uno::Reference < XCommandEnvironment > aCmdEnv;
    2140             : 
    2141           0 :             Content aTemplRoot( aRootContent, aCmdEnv, comphelper::getProcessComponentContext() );
    2142           0 :             CreateFromHierarchy( aTemplRoot );
    2143           0 :         }
    2144             :     }
    2145           0 :     catch( const Exception& )
    2146             :     {
    2147             :         SAL_WARN( "sfx2.doc", "SfxDocTemplate_Impl::Rescan: caught an exception while doing the update!" );
    2148             :     }
    2149           0 : }
    2150             : 
    2151             : // -----------------------------------------------------------------------
    2152           0 : sal_Bool SfxDocTemplate_Impl::GetTitleFromURL( const OUString& rURL,
    2153             :                                            OUString& aTitle )
    2154             : {
    2155           0 :     if ( mxInfo.is() )
    2156             :     {
    2157             :         try
    2158             :         {
    2159           0 :             mxInfo->read( rURL );
    2160             :         }
    2161           0 :         catch ( Exception& )
    2162             :         {
    2163             :             // the document is not a StarOffice document
    2164           0 :             return sal_False;
    2165             :         }
    2166             : 
    2167             : 
    2168             :         try
    2169             :         {
    2170           0 :             uno::Reference< XPropertySet > aPropSet( mxInfo, UNO_QUERY );
    2171           0 :             if ( aPropSet.is() )
    2172             :             {
    2173           0 :                 OUString aPropName( TITLE  );
    2174           0 :                 Any aValue = aPropSet->getPropertyValue( aPropName );
    2175           0 :                 aValue >>= aTitle;
    2176           0 :             }
    2177             :         }
    2178           0 :         catch ( IOException& ) {}
    2179           0 :         catch ( UnknownPropertyException& ) {}
    2180           0 :         catch ( Exception& ) {}
    2181             :     }
    2182             : 
    2183           0 :     if ( aTitle.isEmpty() )
    2184             :     {
    2185           0 :         INetURLObject aURL( rURL );
    2186           0 :         aURL.CutExtension();
    2187             :         aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true,
    2188           0 :                                INetURLObject::DECODE_WITH_CHARSET );
    2189             :     }
    2190             : 
    2191           0 :     return sal_True;
    2192             : }
    2193             : 
    2194             : 
    2195             : // -----------------------------------------------------------------------
    2196           6 : void SfxDocTemplate_Impl::Clear()
    2197             : {
    2198           6 :     ::osl::MutexGuard   aGuard( maMutex );
    2199           6 :     if ( mnLockCounter )
    2200           6 :         return;
    2201             : 
    2202           6 :     for ( size_t i = 0, n = maRegions.size(); i < n; ++i )
    2203           0 :         delete maRegions[ i ];
    2204           6 :     maRegions.clear();
    2205             : }
    2206             : 
    2207             : // -----------------------------------------------------------------------
    2208           0 : sal_Bool getTextProperty_Impl( Content& rContent,
    2209             :                                const OUString& rPropName,
    2210             :                                OUString& rPropValue )
    2211             : {
    2212           0 :     sal_Bool bGotProperty = sal_False;
    2213             : 
    2214             :     // Get the property
    2215             :     try
    2216             :     {
    2217           0 :         uno::Reference< XPropertySetInfo > aPropInfo = rContent.getProperties();
    2218             : 
    2219             :         // check, whether or not the property exists
    2220           0 :         if ( !aPropInfo.is() || !aPropInfo->hasPropertyByName( rPropName ) )
    2221             :         {
    2222           0 :             return sal_False;
    2223             :         }
    2224             : 
    2225             :         // now get the property
    2226           0 :         Any aAnyValue;
    2227             : 
    2228           0 :         aAnyValue = rContent.getPropertyValue( rPropName );
    2229           0 :         aAnyValue >>= rPropValue;
    2230             : 
    2231           0 :         if ( SfxURLRelocator_Impl::propertyCanContainOfficeDir( rPropName ) )
    2232             :         {
    2233           0 :             SfxURLRelocator_Impl aRelocImpl( ::comphelper::getProcessServiceFactory() );
    2234           0 :             aRelocImpl.makeAbsoluteURL( rPropValue );
    2235             :         }
    2236             : 
    2237           0 :         bGotProperty = sal_True;
    2238             :     }
    2239           0 :     catch ( RuntimeException& ) {}
    2240           0 :     catch ( Exception& ) {}
    2241             : 
    2242           0 :     return bGotProperty;
    2243             : }
    2244             : 
    2245             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10