kbrown / rpms / libreoffice

Forked from rpms/libreoffice 2 years ago
Clone

Blame SOURCES/0001-Related-rhbz-1065807-rework-i66157-for-multiple-writ.patch

f085be
From 2fc88c27a7c329753f2c58ec5ee1caa3678200ae Mon Sep 17 00:00:00 2001
f085be
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
f085be
Date: Mon, 24 Feb 2014 15:27:36 +0000
f085be
Subject: [PATCH] Related: rhbz#1065807 rework #i66157# for multiple writable
f085be
 template dirs
f085be
f085be
if there are multiple user-level template dirs then we should be able to
f085be
remove/rename content in all of them, not just the default writable target.
f085be
f085be
The target scenario here is to default to ~/Templates when it exists as the
f085be
template dir, but to retain ~/.config/libreoffice/user/template in the dir for
f085be
any pre-existing templates and to treat both as equivalent in terms of removing
f085be
their content etc.
f085be
f085be
i#66157# wanted to avoid remove extensions templates and other internal
f085be
ones, so rework that logic to instead just be hands off internal templates
f085be
and allow modification of the remainder
f085be
f085be
Change-Id: I56afe991d4297ba692e914ae95ea02d68553f60a
f085be
---
f085be
 sfx2/source/doc/doctemplates.cxx | 82 ++++++++++++++++++++++++++++++++--------
f085be
 1 file changed, 67 insertions(+), 15 deletions(-)
f085be
f085be
diff --git a/sfx2/source/doc/doctemplates.cxx b/sfx2/source/doc/doctemplates.cxx
f085be
index ff07ef3..fe43a40 100644
f085be
--- a/sfx2/source/doc/doctemplates.cxx
f085be
+++ b/sfx2/source/doc/doctemplates.cxx
f085be
@@ -54,6 +54,7 @@
f085be
 #include <com/sun/star/ucb/XContentAccess.hpp>
f085be
 #include <com/sun/star/frame/ModuleManager.hpp>
f085be
 #include <com/sun/star/uno/Exception.hpp>
f085be
+#include <com/sun/star/util/PathSettings.hpp>
f085be
 
f085be
 #include <svtools/templatefoldercache.hxx>
f085be
 #include <unotools/configmgr.hxx>
f085be
@@ -180,6 +181,7 @@ class SfxDocTplService_Impl
f085be
 
f085be
     ::osl::Mutex                maMutex;
f085be
     Sequence< OUString >        maTemplateDirs;
f085be
+    Sequence< OUString >        maInternalTemplateDirs;
f085be
     OUString                    maRootURL;
f085be
     NameList_Impl               maNames;
f085be
     Locale                      maLocale;
f085be
@@ -264,8 +266,16 @@ class SfxDocTplService_Impl
f085be
 
f085be
     void                        updateData( DocTemplates_EntryData_Impl *pData );
f085be
 
f085be
+    //See: #i66157# and rhbz#1065807
f085be
+    //return which template dir the rURL is a subpath of
f085be
+    OUString                    findParentTemplateDir(const OUString& rURL) const;
f085be
+
f085be
+    //See: #i66157# and rhbz#1065807
f085be
+    //return true if rURL is a path (or subpath of) a dir which is not a user path
f085be
+    //which implies neither it or its contents can be removed
f085be
+    bool                        isInternalTemplateDir(const OUString& rURL) const;
f085be
 public:
f085be
-                                 SfxDocTplService_Impl( const uno::Reference< XComponentContext > & xContext );
f085be
+                                SfxDocTplService_Impl( const uno::Reference< XComponentContext > & xContext );
f085be
                                 ~SfxDocTplService_Impl();
f085be
 
f085be
     sal_Bool                    init() { if ( !mbIsInitialized ) init_Impl(); return mbIsInitialized; }
f085be
@@ -545,7 +555,7 @@ void SfxDocTplService_Impl::getDirList()
f085be
     // TODO/LATER: let use service, register listener
f085be
     INetURLObject   aURL;
f085be
     OUString    aDirs = SvtPathOptions().GetTemplatePath();
f085be
-    sal_uInt16  nCount = comphelper::string::getTokenCount(aDirs, C_DELIM);
f085be
+    sal_Int32 nCount = comphelper::string::getTokenCount(aDirs, C_DELIM);
f085be
 
f085be
     maTemplateDirs = Sequence< OUString >( nCount );
f085be
 
f085be
@@ -553,7 +563,7 @@ void SfxDocTplService_Impl::getDirList()
f085be
     const OUString aPrefix(
f085be
         "vnd.sun.star.expand:"  );
f085be
 
f085be
-    for ( sal_uInt16 i=0; i
f085be
+    for (sal_Int32 i = 0; i < nCount; ++i)
f085be
     {
f085be
         aURL.SetSmartProtocol( INET_PROT_FILE );
f085be
         aURL.SetURL( aDirs.getToken( i, C_DELIM ) );
f085be
@@ -571,6 +581,23 @@ void SfxDocTplService_Impl::getDirList()
f085be
 
f085be
     aValue <<= maTemplateDirs;
f085be
 
f085be
+    css::uno::Reference< css::util::XPathSettings > xPathSettings =
f085be
+        css::util::PathSettings::create( mxContext );
f085be
+
f085be
+    // load internal paths
f085be
+    OUString sProp( "Template_internal" );
f085be
+    Any aAny = xPathSettings->getPropertyValue( sProp );
f085be
+    aAny >>= maInternalTemplateDirs;
f085be
+
f085be
+    nCount = maInternalTemplateDirs.getLength();
f085be
+    for (sal_Int32 i = 0; i < nCount; ++i)
f085be
+    {
f085be
+        //expand vnd.sun.star.expand: and remove "..." from them
f085be
+        //to normalize into the expected url patterns
f085be
+        maRelocator.makeRelocatableURL(maInternalTemplateDirs[i]);
f085be
+        maRelocator.makeAbsoluteURL(maInternalTemplateDirs[i]);
f085be
+    }
f085be
+
f085be
     // Store the template dir list
f085be
     setProperty( maRootContent, aPropName, aValue );
f085be
 }
f085be
@@ -1547,13 +1574,16 @@ sal_Bool SfxDocTplService_Impl::removeGroup( const OUString& rGroupName )
f085be
 
f085be
         if ( !maTemplateDirs.getLength() )
f085be
             return sal_False;
f085be
-        OUString aGeneralTempPath = maTemplateDirs[ maTemplateDirs.getLength() - 1 ];
f085be
 
f085be
         // check that the fs location is in writeble folder and this is not a "My templates" folder
f085be
         INetURLObject aGroupParentFolder( aGroupTargetURL );
f085be
-        if ( !aGroupParentFolder.removeSegment()
f085be
-          || !::utl::UCBContentHelper::IsSubPath( aGeneralTempPath,
f085be
-                                                      aGroupParentFolder.GetMainURL( INetURLObject::NO_DECODE ) ) )
f085be
+        if (!aGroupParentFolder.removeSegment())
f085be
+            return sal_False;
f085be
+
f085be
+        OUString aGeneralTempPath = findParentTemplateDir(
f085be
+            aGroupParentFolder.GetMainURL(INetURLObject::NO_DECODE));
f085be
+
f085be
+        if (aGeneralTempPath.isEmpty())
f085be
             return sal_False;
f085be
 
f085be
         // now get the content of the Group
f085be
@@ -1661,14 +1691,14 @@ sal_Bool SfxDocTplService_Impl::renameGroup( const OUString& rOldName,
f085be
 
f085be
     if ( !maTemplateDirs.getLength() )
f085be
         return sal_False;
f085be
-    OUString aGeneralTempPath = maTemplateDirs[ maTemplateDirs.getLength() - 1 ];
f085be
 
f085be
     // check that the fs location is in writeble folder and this is not a "My templates" folder
f085be
     INetURLObject aGroupParentFolder( aGroupTargetURL );
f085be
-    if ( !aGroupParentFolder.removeSegment()
f085be
-      || !::utl::UCBContentHelper::IsSubPath( aGeneralTempPath,
f085be
-                                                  aGroupParentFolder.GetMainURL( INetURLObject::NO_DECODE ) ) )
f085be
+    if (!aGroupParentFolder.removeSegment() ||
f085be
+        isInternalTemplateDir(aGroupParentFolder.GetMainURL(INetURLObject::NO_DECODE)))
f085be
+    {
f085be
         return sal_False;
f085be
+    }
f085be
 
f085be
     // check that the group can be renamed ( all the contents must be in target location )
f085be
     sal_Bool bCanBeRenamed = sal_False;
f085be
@@ -1770,7 +1800,7 @@ sal_Bool SfxDocTplService_Impl::storeTemplate( const OUString& rGroupName,
f085be
             aValue >>= aTemplateToRemoveTargetURL;
f085be
 
f085be
         if ( aGroupTargetURL.isEmpty() || !maTemplateDirs.getLength()
f085be
-          || (!aTemplateToRemoveTargetURL.isEmpty() && !::utl::UCBContentHelper::IsSubPath( maTemplateDirs[ maTemplateDirs.getLength() - 1 ], aTemplateToRemoveTargetURL )) )
f085be
+          || (!aTemplateToRemoveTargetURL.isEmpty() && isInternalTemplateDir(aTemplateToRemoveTargetURL)) )
f085be
             return sal_False; // it is not allowed to remove the template
f085be
     }
f085be
 
f085be
@@ -1909,6 +1939,30 @@ sal_Bool SfxDocTplService_Impl::storeTemplate( const OUString& rGroupName,
f085be
     }
f085be
 }
f085be
 
f085be
+bool SfxDocTplService_Impl::isInternalTemplateDir(const OUString& rURL) const
f085be
+{
f085be
+    const sal_Int32 nDirs = maInternalTemplateDirs.getLength();
f085be
+    const OUString* pDirs = maInternalTemplateDirs.getConstArray();
f085be
+    for (sal_Int32 i = 0; i < nDirs; ++i, ++pDirs)
f085be
+    {
f085be
+        if (::utl::UCBContentHelper::IsSubPath(*pDirs, rURL))
f085be
+            return true;
f085be
+    }
f085be
+    return false;
f085be
+}
f085be
+
f085be
+OUString SfxDocTplService_Impl::findParentTemplateDir(const OUString& rURL) const
f085be
+{
f085be
+    const sal_Int32 nDirs = maTemplateDirs.getLength();
f085be
+    const OUString* pDirs = maTemplateDirs.getConstArray();
f085be
+    for (sal_Int32 i = 0; i < nDirs; ++i, ++pDirs)
f085be
+    {
f085be
+        if (::utl::UCBContentHelper::IsSubPath(*pDirs, rURL))
f085be
+            return *pDirs;
f085be
+    }
f085be
+    return OUString();
f085be
+}
f085be
+
f085be
 //-----------------------------------------------------------------------------
f085be
 sal_Bool SfxDocTplService_Impl::addTemplate( const OUString& rGroupName,
f085be
                                              const OUString& rTemplateName,
f085be
@@ -2032,7 +2086,6 @@ sal_Bool SfxDocTplService_Impl::addTemplate( const OUString& rGroupName,
f085be
     catch ( Exception& )
f085be
     { return sal_False; }
f085be
 
f085be
-
f085be
     // either the document has title and it is the same as requested, or we have to set it
f085be
     sal_Bool bCorrectTitle = ( bDocHasTitle && aTitle.equals( rTemplateName ) );
f085be
     if ( !bCorrectTitle )
f085be
@@ -2101,8 +2154,7 @@ sal_Bool SfxDocTplService_Impl::removeTemplate( const OUString& rGroupName,
f085be
     // delete the target template
f085be
     if ( !aTargetURL.isEmpty() )
f085be
     {
f085be
-        if ( !maTemplateDirs.getLength()
f085be
-          || !::utl::UCBContentHelper::IsSubPath( maTemplateDirs[ maTemplateDirs.getLength() - 1 ], aTargetURL ) )
f085be
+        if (isInternalTemplateDir(aTargetURL))
f085be
             return sal_False;
f085be
 
f085be
         removeContent( aTargetURL );
f085be
-- 
f085be
1.8.5.3
f085be