summaryrefslogtreecommitdiffstats
path: root/unotools/source/config/defaultoptions.cxx
blob: 80a597abcdbfacce44a800790dc81fe2b9be931c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <osl/file.hxx>
#include <sal/log.hxx>
#include <unotools/defaultoptions.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/configitem.hxx>
#include <tools/debug.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <osl/mutex.hxx>

#include <rtl/instance.hxx>
#include <rtl/ustrbuf.hxx>

#include "itemholder1.hxx"

using namespace osl;
using namespace utl;
using namespace com::sun::star::uno;

#define DEFAULTPATH_ADDIN          0
#define DEFAULTPATH_AUTOCORRECT    1
#define DEFAULTPATH_AUTOTEXT       2
#define DEFAULTPATH_BACKUP         3
#define DEFAULTPATH_BASIC          4
#define DEFAULTPATH_BITMAP         5
#define DEFAULTPATH_CONFIG         6
#define DEFAULTPATH_DICTIONARY     7
#define DEFAULTPATH_FAVORITES      8
#define DEFAULTPATH_FILTER         9
#define DEFAULTPATH_GALLERY        10
#define DEFAULTPATH_GRAPHIC        11
#define DEFAULTPATH_HELP           12
#define DEFAULTPATH_LINGUISTIC     13
#define DEFAULTPATH_MODULE         14
#define DEFAULTPATH_PALETTE        15
#define DEFAULTPATH_PLUGIN         16
#define DEFAULTPATH_TEMP           17
#define DEFAULTPATH_TEMPLATE       18
#define DEFAULTPATH_USERCONFIG     19
#define DEFAULTPATH_WORK           20
#define DEFAULTPATH_CLASSIFICATION 21
#define DEFAULTPATH_USERDICTIONARY 22

// class SvtDefaultOptions_Impl ------------------------------------------

class SvtDefaultOptions_Impl : public utl::ConfigItem
{
public:
    OUString         m_aAddinPath;
    OUString         m_aAutoCorrectPath;
    OUString         m_aAutoTextPath;
    OUString         m_aBackupPath;
    OUString         m_aBasicPath;
    OUString         m_aBitmapPath;
    OUString         m_aConfigPath;
    OUString         m_aDictionaryPath;
    OUString         m_aFavoritesPath;
    OUString         m_aFilterPath;
    OUString         m_aGalleryPath;
    OUString         m_aGraphicPath;
    OUString         m_aHelpPath;
    OUString         m_aLinguisticPath;
    OUString         m_aModulePath;
    OUString         m_aPalettePath;
    OUString         m_aPluginPath;
    OUString         m_aTempPath;
    OUString         m_aTemplatePath;
    OUString         m_aUserConfigPath;
    OUString         m_aWorkPath;
    OUString         m_aClassificationPath;

                    SvtDefaultOptions_Impl();
                    virtual ~SvtDefaultOptions_Impl() override;

    OUString         GetDefaultPath( sal_uInt16 nId ) const;
    virtual void    Notify( const css::uno::Sequence<OUString>& aPropertyNames) override;

private:
    virtual void    ImplCommit() final override;
};

// global ----------------------------------------------------------------

namespace {

std::weak_ptr<SvtDefaultOptions_Impl> g_pOptions;

}

typedef OUString SvtDefaultOptions_Impl:: *PathStrPtr;

namespace {

struct PathToDefaultMapping_Impl
{
    SvtPathOptions::Paths   _ePath;
    PathStrPtr              _pDefaultPath;
};

}

static PathToDefaultMapping_Impl const PathMap_Impl[] =
{
    { SvtPathOptions::PATH_ADDIN,           &SvtDefaultOptions_Impl::m_aAddinPath },
    { SvtPathOptions::PATH_AUTOCORRECT,     &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
    { SvtPathOptions::PATH_AUTOTEXT,        &SvtDefaultOptions_Impl::m_aAutoTextPath },
    { SvtPathOptions::PATH_BACKUP,          &SvtDefaultOptions_Impl::m_aBackupPath },
    { SvtPathOptions::PATH_BASIC,           &SvtDefaultOptions_Impl::m_aBasicPath },
    { SvtPathOptions::PATH_BITMAP,          &SvtDefaultOptions_Impl::m_aBitmapPath },
    { SvtPathOptions::PATH_CONFIG,          &SvtDefaultOptions_Impl::m_aConfigPath },
    { SvtPathOptions::PATH_DICTIONARY,      &SvtDefaultOptions_Impl::m_aDictionaryPath },
    { SvtPathOptions::PATH_FAVORITES,       &SvtDefaultOptions_Impl::m_aFavoritesPath },
    { SvtPathOptions::PATH_FILTER,          &SvtDefaultOptions_Impl::m_aFilterPath },
    { SvtPathOptions::PATH_GALLERY,         &SvtDefaultOptions_Impl::m_aGalleryPath },
    { SvtPathOptions::PATH_GRAPHIC,         &SvtDefaultOptions_Impl::m_aGraphicPath },
    { SvtPathOptions::PATH_HELP,            &SvtDefaultOptions_Impl::m_aHelpPath },
    { SvtPathOptions::PATH_LINGUISTIC,      &SvtDefaultOptions_Impl::m_aLinguisticPath },
    { SvtPathOptions::PATH_MODULE,          &SvtDefaultOptions_Impl::m_aModulePath },
    { SvtPathOptions::PATH_PALETTE,         &SvtDefaultOptions_Impl::m_aPalettePath },
    { SvtPathOptions::PATH_PLUGIN,          &SvtDefaultOptions_Impl::m_aPluginPath },
    { SvtPathOptions::PATH_TEMP,            &SvtDefaultOptions_Impl::m_aTempPath },
    { SvtPathOptions::PATH_TEMPLATE,        &SvtDefaultOptions_Impl::m_aTemplatePath },
    { SvtPathOptions::PATH_USERCONFIG,      &SvtDefaultOptions_Impl::m_aUserConfigPath },
    { SvtPathOptions::PATH_WORK,            &SvtDefaultOptions_Impl::m_aWorkPath },
    { SvtPathOptions::PATH_CLASSIFICATION,  &SvtDefaultOptions_Impl::m_aClassificationPath }
};

// functions -------------------------------------------------------------

static Sequence< OUString > GetDefaultPropertyNames()
{
    static const char* aPropNames[] =
    {
        "Addin",            // PATH_ADDIN
        "AutoCorrect",      // PATH_AUTOCORRECT
        "AutoText",         // PATH_AUTOTEXT
        "Backup",           // PATH_BACKUP
        "Basic",            // PATH_BASIC
        "Bitmap",           // PATH_BITMAP
        "Config",           // PATH_CONFIG
        "Dictionary",       // PATH_DICTIONARY
        "Favorite",         // PATH_FAVORITES
        "Filter",           // PATH_FILTER
        "Gallery",          // PATH_GALLERY
        "Graphic",          // PATH_GRAPHIC
        "Help",             // PATH_HELP
        "Linguistic",       // PATH_LINGUISTIC
        "Module",           // PATH_MODULE
        "Palette",          // PATH_PALETTE
        "Plugin",           // PATH_PLUGIN
        "Temp",             // PATH_TEMP
        "Template",         // PATH_TEMPLATE
        "UserConfig",       // PATH_USERCONFIG
        "Work",             // PATH_WORK
        "Classification"    // PATH_CLASSIFICATION
    };

    const int nCount = SAL_N_ELEMENTS( aPropNames );
    Sequence< OUString > aNames( nCount );
    OUString* pNames = aNames.getArray();
    for ( int i = 0; i < nCount; i++ )
        pNames[i] = OUString::createFromAscii( aPropNames[i] );

    return aNames;
}

void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >&  )
{
    // no notification, will never be changed
}

void SvtDefaultOptions_Impl::ImplCommit()
{
    // will never be changed
}

// class SvtDefaultOptions_Impl ------------------------------------------

OUString SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
{
    OUString aRet;
    sal_uInt16 nIdx = 0;

    while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_CLASSIFICATION )
    {
        if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
        {
            aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
            if ( nId == SvtPathOptions::PATH_ADDIN ||
                 nId == SvtPathOptions::PATH_FILTER ||
                 nId == SvtPathOptions::PATH_HELP ||
                 nId == SvtPathOptions::PATH_MODULE ||
                 nId == SvtPathOptions::PATH_PLUGIN )
            {
                OUString aTmp;
                osl::FileBase::getFileURLFromSystemPath( aRet, aTmp );
                aRet = aTmp;
            }

            break;
        }
        ++nIdx;
    }

    return aRet;
}

SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( "Office.Common/Path/Default" )
{
    Sequence< OUString > aNames = GetDefaultPropertyNames();
    Sequence< Any > aValues = GetProperties( aNames );
    EnableNotification( aNames );
    const Any* pValues = aValues.getConstArray();
    DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
    if ( aValues.getLength() == aNames.getLength() )
    {
        SvtPathOptions aPathOpt;
        OUString aTempStr;
        OUStringBuffer aFullPathBuf;

        for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
        {
            if ( pValues[nProp].hasValue() )
            {
                switch ( pValues[nProp].getValueTypeClass() )
                {
                    case css::uno::TypeClass_STRING :
                    {
                        // multi paths
                        if ( pValues[nProp] >>= aTempStr )
                            aFullPathBuf = aPathOpt.SubstituteVariable( aTempStr );
                        else
                        {
                            SAL_WARN( "unotools.config", "any operator >>= failed" );
                        }
                        break;
                    }

                    case css::uno::TypeClass_SEQUENCE :
                    {
                        // single paths
                        aFullPathBuf.setLength(0);
                        Sequence < OUString > aList;
                        if ( pValues[nProp] >>= aList )
                        {
                            sal_Int32 nCount = aList.getLength();
                            for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
                            {
                                aFullPathBuf.append(aPathOpt.SubstituteVariable( aList[ nPosition ] ));
                                if ( nPosition < nCount-1 )
                                    aFullPathBuf.append(";");
                            }
                        }
                        else
                        {
                            SAL_WARN( "unotools.config", "any operator >>= failed" );
                        }
                        break;
                    }

                    default:
                    {
                        SAL_WARN( "unotools.config", "Wrong any type" );
                    }
                }

                auto aFullPath = aFullPathBuf.makeStringAndClear();
                switch ( nProp )
                {
                    case DEFAULTPATH_ADDIN:            m_aAddinPath = aFullPath;         break;
                    case DEFAULTPATH_AUTOCORRECT:      m_aAutoCorrectPath = aFullPath;   break;
                    case DEFAULTPATH_AUTOTEXT:         m_aAutoTextPath = aFullPath;      break;
                    case DEFAULTPATH_BACKUP:           m_aBackupPath = aFullPath;        break;
                    case DEFAULTPATH_BASIC:            m_aBasicPath = aFullPath;         break;
                    case DEFAULTPATH_BITMAP:           m_aBitmapPath = aFullPath;        break;
                    case DEFAULTPATH_CONFIG:           m_aConfigPath = aFullPath;        break;
                    case DEFAULTPATH_DICTIONARY:       m_aDictionaryPath = aFullPath;    break;
                    case DEFAULTPATH_FAVORITES:        m_aFavoritesPath = aFullPath;     break;
                    case DEFAULTPATH_FILTER:           m_aFilterPath = aFullPath;        break;
                    case DEFAULTPATH_GALLERY:          m_aGalleryPath = aFullPath;       break;
                    case DEFAULTPATH_GRAPHIC:          m_aGraphicPath = aFullPath;       break;
                    case DEFAULTPATH_HELP:             m_aHelpPath = aFullPath;          break;
                    case DEFAULTPATH_LINGUISTIC:       m_aLinguisticPath = aFullPath;    break;
                    case DEFAULTPATH_MODULE:           m_aModulePath = aFullPath;        break;
                    case DEFAULTPATH_PALETTE:          m_aPalettePath = aFullPath;       break;
                    case DEFAULTPATH_PLUGIN:           m_aPluginPath = aFullPath;        break;
                    case DEFAULTPATH_TEMP:             m_aTempPath = aFullPath;          break;
                    case DEFAULTPATH_TEMPLATE:         m_aTemplatePath = aFullPath;      break;
                    case DEFAULTPATH_USERCONFIG:       m_aUserConfigPath = aFullPath;    break;
                    case DEFAULTPATH_WORK:             m_aWorkPath = aFullPath;          break;
                    case DEFAULTPATH_CLASSIFICATION:   m_aClassificationPath = aFullPath;break;
                    case DEFAULTPATH_USERDICTIONARY:   break;

                    default:
                        SAL_WARN( "unotools.config", "invalid index to load a default path" );
                }
            }
        }
    }
}

SvtDefaultOptions_Impl::~SvtDefaultOptions_Impl()
{
    if ( IsModified() )
        Commit();
}

// class SvtDefaultOptions -----------------------------------------------
namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }

SvtDefaultOptions::SvtDefaultOptions()
{
    // Global access, must be guarded (multithreading)
    ::osl::MutexGuard aGuard( lclMutex::get() );
    pImpl = g_pOptions.lock();
    if ( !pImpl )
    {
        pImpl = std::make_shared<SvtDefaultOptions_Impl>();
        g_pOptions = pImpl;
        ItemHolder1::holdConfigItem(EItem::DefaultOptions);
    }
}

SvtDefaultOptions::~SvtDefaultOptions()
{
    // Global access, must be guarded (multithreading)
    ::osl::MutexGuard aGuard( lclMutex::get() );
    pImpl.reset();
}

OUString SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
{
    return pImpl->GetDefaultPath( nId );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */