bump product version to 5.0.4.1
[LibreOffice.git] / linguistic / source / misc2.cxx
blobeeee5cab3eca97e59965b91172020376db0f6fda
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 INTERNAL = 0x01,
45 USER = 0x02,
46 WRITABLE = 0x04
48 namespace o3tl
50 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x07> {};
52 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER | DictionaryPathFlags::WRITABLE)
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< ::com::sun::star::ucb::XCommandEnvironment >(),
67 comphelper::getProcessComponentContext());
68 bExists = aContent.isDocument();
70 catch (uno::Exception &)
74 return bExists;
77 static uno::Sequence< OUString > GetMultiPaths_Impl(
78 const OUString &rPathPrefix,
79 DictionaryPathFlags nPathFlags )
81 uno::Sequence< 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.realloc( nMaxEntries );
114 OUString *pRes = aRes.getArray();
115 sal_Int32 nCount = 0; // number of actually added entries
116 if ((nPathFlags & DictionaryPathFlags::WRITABLE) && !aWritablePath.isEmpty())
117 pRes[ nCount++ ] = aWritablePath;
118 for (int i = 0; i < 2; ++i)
120 const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
121 const OUString *pPathSeq = rPathSeq.getConstArray();
122 for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
124 const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & DictionaryPathFlags::USER);
125 const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & DictionaryPathFlags::INTERNAL);
126 if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
127 pRes[ nCount++ ] = pPathSeq[k];
130 aRes.realloc( nCount );
133 return aRes;
136 OUString GetDictionaryWriteablePath()
138 uno::Sequence< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", DictionaryPathFlags::WRITABLE ) );
139 DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
140 OUString aRes;
141 if (aPaths.getLength() > 0)
142 aRes = aPaths[0];
143 return aRes;
146 uno::Sequence< OUString > GetDictionaryPaths()
148 return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL );
151 OUString GetWritableDictionaryURL( const OUString &rDicName )
153 // new user writable dictionaries should be created in the 'writable' path
154 OUString aDirName( GetDictionaryWriteablePath() );
156 // build URL to use for a new (persistent) dictionary
157 INetURLObject aURLObj;
158 aURLObj.SetSmartProtocol( INetProtocol::File );
159 aURLObj.SetSmartURL( aDirName );
160 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
161 aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
162 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
164 // NO_DECODE preserves the escape sequences that might be included in aDirName
165 // depending on the characters used in the path string. (Needed when comparing
166 // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
167 return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
170 } // namespace linguistic
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */