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 157 : bool FileExists( const OUString &rMainURL )
44 : {
45 157 : bool bExists = false;
46 157 : if (!rMainURL.isEmpty())
47 : {
48 : try
49 : {
50 : ::ucbhelper::Content aContent( rMainURL,
51 : uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
52 157 : comphelper::getProcessComponentContext());
53 158 : bExists = aContent.isDocument();
54 : }
55 1 : catch (uno::Exception &)
56 : {
57 : }
58 : }
59 157 : return bExists;
60 : }
61 :
62 158 : static uno::Sequence< OUString > GetMultiPaths_Impl(
63 : const OUString &rPathPrefix,
64 : sal_Int16 nPathFlags )
65 : {
66 158 : uno::Sequence< OUString > aRes;
67 316 : uno::Sequence< OUString > aInternalPaths;
68 316 : uno::Sequence< OUString > aUserPaths;
69 316 : OUString aWritablePath;
70 :
71 158 : bool bSuccess = true;
72 316 : uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
73 : try
74 : {
75 158 : OUString aInternal( rPathPrefix + "_internal" );
76 316 : OUString aUser( rPathPrefix + "_user" );
77 316 : OUString aWriteable( rPathPrefix + "_writable" );
78 :
79 : uno::Reference< util::XPathSettings > xPathSettings =
80 316 : util::thePathSettings::get( xContext );
81 158 : xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
82 158 : xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
83 316 : xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
84 : }
85 0 : catch (uno::Exception &)
86 : {
87 0 : bSuccess = false;
88 : }
89 158 : 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 158 : sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
96 158 : if (!aWritablePath.isEmpty())
97 158 : ++nMaxEntries;
98 158 : aRes.realloc( nMaxEntries );
99 158 : OUString *pRes = aRes.getArray();
100 158 : sal_Int32 nCount = 0; // number of actually added entries
101 158 : if ((nPathFlags & PATH_FLAG_WRITABLE) && !aWritablePath.isEmpty())
102 158 : pRes[ nCount++ ] = aWritablePath;
103 474 : for (int i = 0; i < 2; ++i)
104 : {
105 316 : const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
106 316 : const OUString *pPathSeq = rPathSeq.getConstArray();
107 474 : for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
108 : {
109 158 : const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & PATH_FLAG_USER);
110 158 : const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & PATH_FLAG_INTERNAL);
111 158 : if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
112 38 : pRes[ nCount++ ] = pPathSeq[k];
113 : }
114 : }
115 158 : aRes.realloc( nCount );
116 : }
117 :
118 316 : return aRes;
119 : }
120 :
121 120 : OUString GetDictionaryWriteablePath()
122 : {
123 120 : uno::Sequence< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", PATH_FLAG_WRITABLE ) );
124 : DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
125 120 : OUString aRes;
126 120 : if (aPaths.getLength() > 0)
127 120 : aRes = aPaths[0];
128 120 : return aRes;
129 : }
130 :
131 38 : uno::Sequence< OUString > GetDictionaryPaths( sal_Int16 nPathFlags )
132 : {
133 38 : return GetMultiPaths_Impl( "Dictionary", nPathFlags );
134 : }
135 :
136 1 : OUString GetWritableDictionaryURL( const OUString &rDicName )
137 : {
138 : // new user writable dictionaries should be created in the 'writable' path
139 1 : OUString aDirName( GetDictionaryWriteablePath() );
140 :
141 : // build URL to use for a new (persistent) dictionary
142 2 : INetURLObject aURLObj;
143 1 : aURLObj.SetSmartProtocol( INET_PROT_FILE );
144 1 : aURLObj.SetSmartURL( aDirName );
145 : DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
146 1 : 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 2 : return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
153 : }
154 :
155 : } // namespace linguistic
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|