tdf#121561 sw DOCX: output toc-end inside last toc paragraph
[LibreOffice.git] / linguistic / source / misc2.cxx
blobf416a1739fd194670d771da179065b1de7f8d96b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/urlobj.hxx>
21 #include <ucbhelper/content.hxx>
22 #include <tools/debug.hxx>
23 #include <unotools/pathoptions.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <unotools/localfilehelper.hxx>
26 #include <unotools/localedatawrapper.hxx>
27 #include <unotools/ucbhelper.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/XFastPropertySet.hpp>
30 #include <com/sun/star/beans/PropertyValues.hpp>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/uno/Reference.h>
33 #include <com/sun/star/util/thePathSettings.hpp>
34 #include <o3tl/typed_flags_set.hxx>
36 #include <linguistic/misc.hxx>
38 using namespace com::sun::star;
40 /// Flags to be used with the multi-path related functions
41 /// @see GetDictionaryPaths
42 enum class DictionaryPathFlags
44 NONE = 0x00,
45 INTERNAL = 0x01,
46 USER = 0x02,
48 namespace o3tl
50 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
52 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
54 namespace linguistic
58 bool FileExists( const OUString &rMainURL )
60 bool bExists = false;
61 if (!rMainURL.isEmpty())
63 try
65 ::ucbhelper::Content aContent( rMainURL,
66 uno::Reference< css::ucb::XCommandEnvironment >(),
67 comphelper::getProcessComponentContext());
68 bExists = aContent.isDocument();
70 catch (uno::Exception &)
74 return bExists;
77 static std::vector< OUString > GetMultiPaths_Impl(
78 const OUString &rPathPrefix,
79 DictionaryPathFlags nPathFlags )
81 std::vector< OUString > aRes;
82 uno::Sequence< OUString > aInternalPaths;
83 uno::Sequence< OUString > aUserPaths;
84 OUString aWritablePath;
86 bool bSuccess = true;
87 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
88 try
90 OUString aInternal( rPathPrefix + "_internal" );
91 OUString aUser( rPathPrefix + "_user" );
92 OUString aWriteable( rPathPrefix + "_writable" );
94 uno::Reference< util::XPathSettings > xPathSettings =
95 util::thePathSettings::get( xContext );
96 xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
97 xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
98 xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
100 catch (uno::Exception &)
102 bSuccess = false;
104 if (bSuccess)
106 // build resulting sequence by adding the paths in the following order:
107 // 1. writable path
108 // 2. all user paths
109 // 3. all internal paths
110 sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
111 if (!aWritablePath.isEmpty())
112 ++nMaxEntries;
113 aRes.resize( nMaxEntries );
114 sal_Int32 nCount = 0; // number of actually added entries
115 if (!aWritablePath.isEmpty())
116 aRes[ nCount++ ] = aWritablePath;
117 for (int i = 0; i < 2; ++i)
119 const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
120 const OUString *pPathSeq = rPathSeq.getConstArray();
121 for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
123 const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & DictionaryPathFlags::USER);
124 const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & DictionaryPathFlags::INTERNAL);
125 if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
126 aRes[ nCount++ ] = pPathSeq[k];
129 aRes.resize( nCount );
132 return aRes;
135 OUString GetDictionaryWriteablePath()
137 std::vector< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", DictionaryPathFlags::NONE ) );
138 DBG_ASSERT( aPaths.size() == 1, "Dictionary_writable path corrupted?" );
139 OUString aRes;
140 if (!aPaths.empty())
141 aRes = aPaths[0];
142 return aRes;
145 std::vector< OUString > GetDictionaryPaths()
147 return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL );
150 OUString GetWritableDictionaryURL( const OUString &rDicName )
152 // new user writable dictionaries should be created in the 'writable' path
153 OUString aDirName( GetDictionaryWriteablePath() );
155 // build URL to use for a new (persistent) dictionary
156 INetURLObject aURLObj;
157 aURLObj.SetSmartProtocol( INetProtocol::File );
158 aURLObj.SetSmartURL( aDirName );
159 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
160 aURLObj.Append( rDicName, INetURLObject::EncodeMechanism::All );
161 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
163 // DecodeMechanism::NONE preserves the escape sequences that might be included in aDirName
164 // depending on the characters used in the path string. (Needed when comparing
165 // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
166 return aURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
169 } // namespace linguistic
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */