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/string.hxx>
21 : #include <tools/urlobj.hxx>
22 : #include <ucbhelper/content.hxx>
23 : #include <tools/debug.hxx>
24 : #include <unotools/pathoptions.hxx>
25 : #include <comphelper/processfactory.hxx>
26 : #include <unotools/localfilehelper.hxx>
27 : #include <unotools/localedatawrapper.hxx>
28 : #include <unotools/ucbhelper.hxx>
29 : #include <com/sun/star/beans/XPropertySet.hpp>
30 : #include <com/sun/star/beans/XFastPropertySet.hpp>
31 : #include <com/sun/star/beans/PropertyValues.hpp>
32 : #include <com/sun/star/uno/Sequence.hxx>
33 : #include <com/sun/star/uno/Reference.h>
34 : #include <com/sun/star/util/PathSettings.hpp>
35 :
36 : #include "linguistic/misc.hxx"
37 :
38 : using namespace com::sun::star;
39 :
40 : namespace linguistic
41 : {
42 :
43 :
44 60 : sal_Bool FileExists( const String &rMainURL )
45 : {
46 60 : sal_Bool bExists = sal_False;
47 60 : if (rMainURL.Len())
48 : {
49 : try
50 : {
51 : ::ucbhelper::Content aContent( rMainURL,
52 : uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
53 60 : comphelper::getProcessComponentContext());
54 60 : bExists = aContent.isDocument();
55 : }
56 0 : catch (uno::Exception &)
57 : {
58 : }
59 : }
60 60 : return bExists;
61 : }
62 :
63 71 : static uno::Sequence< OUString > GetMultiPaths_Impl(
64 : const OUString &rPathPrefix,
65 : sal_Int16 nPathFlags )
66 : {
67 71 : uno::Sequence< OUString > aRes;
68 142 : uno::Sequence< OUString > aInternalPaths;
69 142 : uno::Sequence< OUString > aUserPaths;
70 142 : OUString aWritablePath;
71 :
72 71 : bool bSuccess = true;
73 142 : uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
74 : try
75 : {
76 71 : OUString aInternal( rPathPrefix + "_internal" );
77 142 : OUString aUser( rPathPrefix + "_user" );
78 142 : OUString aWriteable( rPathPrefix + "_writable" );
79 :
80 : uno::Reference< util::XPathSettings > xPathSettings =
81 142 : util::PathSettings::create( xContext );
82 71 : xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
83 71 : xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
84 142 : xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
85 : }
86 0 : catch (uno::Exception &)
87 : {
88 0 : bSuccess = false;
89 : }
90 71 : if (bSuccess)
91 : {
92 : // build resulting sequence by adding the paths in the following order:
93 : // 1. writable path
94 : // 2. all user paths
95 : // 3. all internal paths
96 71 : sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
97 71 : if (!aWritablePath.isEmpty())
98 71 : ++nMaxEntries;
99 71 : aRes.realloc( nMaxEntries );
100 71 : OUString *pRes = aRes.getArray();
101 71 : sal_Int32 nCount = 0; // number of actually added entries
102 71 : if ((nPathFlags & PATH_FLAG_WRITABLE) && !aWritablePath.isEmpty())
103 71 : pRes[ nCount++ ] = aWritablePath;
104 213 : for (int i = 0; i < 2; ++i)
105 : {
106 142 : const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
107 142 : const OUString *pPathSeq = rPathSeq.getConstArray();
108 213 : for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
109 : {
110 71 : const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & PATH_FLAG_USER);
111 71 : const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & PATH_FLAG_INTERNAL);
112 71 : if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
113 20 : pRes[ nCount++ ] = pPathSeq[k];
114 : }
115 : }
116 71 : aRes.realloc( nCount );
117 : }
118 :
119 142 : return aRes;
120 : }
121 :
122 51 : OUString GetDictionaryWriteablePath()
123 : {
124 51 : uno::Sequence< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", PATH_FLAG_WRITABLE ) );
125 : DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
126 102 : String aRes;
127 51 : if (aPaths.getLength() > 0)
128 51 : aRes = aPaths[0];
129 102 : return aRes;
130 : }
131 :
132 20 : uno::Sequence< OUString > GetDictionaryPaths( sal_Int16 nPathFlags )
133 : {
134 20 : return GetMultiPaths_Impl( "Dictionary", nPathFlags );
135 : }
136 :
137 0 : String GetWritableDictionaryURL( const String &rDicName )
138 : {
139 : // new user writable dictionaries should be created in the 'writable' path
140 0 : String aDirName( GetDictionaryWriteablePath() );
141 :
142 : // build URL to use for a new (persistent) dictionary
143 0 : INetURLObject aURLObj;
144 0 : aURLObj.SetSmartProtocol( INET_PROT_FILE );
145 0 : aURLObj.SetSmartURL( aDirName );
146 : DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
147 0 : aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
148 : DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
149 :
150 : // NO_DECODE preserves the escape sequences that might be included in aDirName
151 : // depending on the characters used in the path string. (Needed when comparing
152 : // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
153 0 : return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
154 : }
155 :
156 : } // namespace linguistic
157 :
158 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|