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