Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <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 :
35 : #include "linguistic/misc.hxx"
36 :
37 : using namespace com::sun::star;
38 :
39 : namespace linguistic
40 : {
41 :
42 :
43 0 : bool FileExists( const OUString &rMainURL )
44 : {
45 0 : bool bExists = false;
46 0 : if (!rMainURL.isEmpty())
47 : {
48 : try
49 : {
50 : ::ucbhelper::Content aContent( rMainURL,
51 : uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
52 0 : comphelper::getProcessComponentContext());
53 0 : bExists = aContent.isDocument();
54 : }
55 0 : catch (uno::Exception &)
56 : {
57 : }
58 : }
59 0 : return bExists;
60 : }
61 :
62 0 : static uno::Sequence< OUString > GetMultiPaths_Impl(
63 : const OUString &rPathPrefix,
64 : sal_Int16 nPathFlags )
65 : {
66 0 : uno::Sequence< OUString > aRes;
67 0 : uno::Sequence< OUString > aInternalPaths;
68 0 : uno::Sequence< OUString > aUserPaths;
69 0 : OUString aWritablePath;
70 :
71 0 : bool bSuccess = true;
72 0 : uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
73 : try
74 : {
75 0 : OUString aInternal( rPathPrefix + "_internal" );
76 0 : OUString aUser( rPathPrefix + "_user" );
77 0 : OUString aWriteable( rPathPrefix + "_writable" );
78 :
79 : uno::Reference< util::XPathSettings > xPathSettings =
80 0 : util::thePathSettings::get( xContext );
81 0 : xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
82 0 : xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
83 0 : xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
84 : }
85 0 : catch (uno::Exception &)
86 : {
87 0 : bSuccess = false;
88 : }
89 0 : if (bSuccess)
90 : {
91 : // build resulting sequence by adding the paths in the following order:
92 : // 1. writable path
93 : // 2. all user paths
94 : // 3. all internal paths
95 0 : sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
96 0 : if (!aWritablePath.isEmpty())
97 0 : ++nMaxEntries;
98 0 : aRes.realloc( nMaxEntries );
99 0 : OUString *pRes = aRes.getArray();
100 0 : sal_Int32 nCount = 0; // number of actually added entries
101 0 : if ((nPathFlags & PATH_FLAG_WRITABLE) && !aWritablePath.isEmpty())
102 0 : pRes[ nCount++ ] = aWritablePath;
103 0 : for (int i = 0; i < 2; ++i)
104 : {
105 0 : const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
106 0 : const OUString *pPathSeq = rPathSeq.getConstArray();
107 0 : for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
108 : {
109 0 : const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & PATH_FLAG_USER);
110 0 : const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & PATH_FLAG_INTERNAL);
111 0 : if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
112 0 : pRes[ nCount++ ] = pPathSeq[k];
113 : }
114 : }
115 0 : aRes.realloc( nCount );
116 : }
117 :
118 0 : return aRes;
119 : }
120 :
121 0 : OUString GetDictionaryWriteablePath()
122 : {
123 0 : uno::Sequence< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", PATH_FLAG_WRITABLE ) );
124 : DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
125 0 : OUString aRes;
126 0 : if (aPaths.getLength() > 0)
127 0 : aRes = aPaths[0];
128 0 : return aRes;
129 : }
130 :
131 0 : uno::Sequence< OUString > GetDictionaryPaths( sal_Int16 nPathFlags )
132 : {
133 0 : return GetMultiPaths_Impl( "Dictionary", nPathFlags );
134 : }
135 :
136 0 : OUString GetWritableDictionaryURL( const OUString &rDicName )
137 : {
138 : // new user writable dictionaries should be created in the 'writable' path
139 0 : OUString aDirName( GetDictionaryWriteablePath() );
140 :
141 : // build URL to use for a new (persistent) dictionary
142 0 : INetURLObject aURLObj;
143 0 : aURLObj.SetSmartProtocol( INET_PROT_FILE );
144 0 : aURLObj.SetSmartURL( aDirName );
145 : DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
146 0 : aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
147 : DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
148 :
149 : // NO_DECODE preserves the escape sequences that might be included in aDirName
150 : // depending on the characters used in the path string. (Needed when comparing
151 : // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
152 0 : return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
153 : }
154 :
155 : } // namespace linguistic
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|