summaryrefslogtreecommitdiff
path: root/unotools/source/config/useroptions.cxx
blob: 2f216d7af2e323a07832c16f10be1d25b76d27d8 (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
358
359
360
361
362
363
364
365
366
367
/* -*- 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 <unotools/useroptions.hxx>

#include <unotools/configmgr.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <tools/solar.h>
#include <osl/mutex.hxx>
#include <rtl/instance.hxx>
#include <rtl/logfile.hxx>
#include "itemholder1.hxx"

#include <com/sun/star/beans/Property.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/util/XChangesListener.hpp>
#include <com/sun/star/util/XChangesNotifier.hpp>
#include <com/sun/star/util/ChangesEvent.hpp>
#include <comphelper/configurationhelper.hxx>
#include <comphelper/processfactory.hxx>

using namespace utl;
using namespace com::sun::star;
//using namespace com::sun::star::uno;
using rtl::OUString;

//namespace css = ::com::sun::star;

namespace
{

OUString const sData = "org.openoffice.UserProfile/Data";

// vOptionNames[] -- names of the user option entries
// The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
OUString const vOptionNames[] = {
    OUString("l"),                         // USER_OPT_CITY
    OUString("o"),                         // USER_OPT_COMPANY
    OUString("c"),                         // USER_OPT_COUNTRY
    OUString("mail"),                      // USER_OPT_EMAIL
    OUString("facsimiletelephonenumber"),  // USER_OPT_FAX
    OUString("givenname"),                 // USER_OPT_FIRSTNAME
    OUString("sn"),                        // USER_OPT_LASTNAME
    OUString("position"),                  // USER_OPT_POSITION
    OUString("st"),                        // USER_OPT_STATE
    OUString("street"),                    // USER_OPT_STREET
    OUString("homephone"),                 // USER_OPT_TELEPHONEHOME
    OUString("telephonenumber"),           // USER_OPT_TELEPHONEWORK
    OUString("title"),                     // USER_OPT_TITLE
    OUString("initials"),                  // USER_OPT_ID
    OUString("postalcode"),                // USER_OPT_ZIP
    OUString("fathersname"),               // USER_OPT_FATHERSNAME
    OUString("apartment"),                 // USER_OPT_APARTMENT
    OUString("customernumber")             // USER_OPT_CUSTOMERNUMBER
};
const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);

} // namespace

boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;

// class ChangeListener --------------------------------------------------

class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
{
public:
    ChangeListener (Impl& rParent): m_rParent(rParent) { }

    // XChangesListener
    virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException);
    // XEventListener
    virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException);

private:
    Impl& m_rParent;
};

// class Impl ------------------------------------------------------------

class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
{
public:
    Impl ();

    OUString GetFullName () const;

    sal_Bool IsTokenReadonly (sal_uInt16 nToken) const;
    OUString GetToken (sal_uInt16 nToken) const;
    void     SetToken (sal_uInt16 nToken, OUString const& rNewToken);
    void     Notify ();

private:
    uno::Reference<util::XChangesListener> m_xChangeListener;
    uno::Reference<container::XNameAccess> m_xCfg;
    uno::Reference<beans::XPropertySet>    m_xData;
};

// class SvtUserOptions::ChangeListener ----------------------------------

void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException)
{
    if (rEvent.Changes.getLength())
        m_rParent.Notify();
}

void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException)
{
    try
    {
        uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
        xChgNot->removeChangesListener(this);
    }
    catch (uno::Exception&)
    {
    }
}

// class SvtUserOptions::Impl --------------------------------------------

SvtUserOptions::Impl::Impl() :
    m_xChangeListener( new ChangeListener(*this) )
{
    try
    {
        m_xCfg = uno::Reference<container::XNameAccess>(
            comphelper::ConfigurationHelper::openConfig(
                comphelper::getProcessServiceFactory(),
                sData,
                comphelper::ConfigurationHelper::E_STANDARD
            ),
            uno::UNO_QUERY
        );

        m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
        uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
        try
        {
            xChgNot->addChangesListener(m_xChangeListener);
        }
        catch (uno::RuntimeException&)
        {
        }
    }
    catch (uno::Exception const& ex)
    {
        m_xCfg.clear();
        SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
    }
}

// -----------------------------------------------------------------------

OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
{
    OUString sToken;
    if (nToken < nOptionNameCount)
    {
        try
        {
            if (m_xData.is())
                m_xData->getPropertyValue(vOptionNames[nToken]) >>= sToken;
        }
        catch (uno::Exception const& ex)
        {
            SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
        }
    }
    else
        SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
    return sToken;
}

// -----------------------------------------------------------------------

void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
{
    if (nToken < nOptionNameCount)
    {
        try
        {
            if (m_xData.is())
                m_xData->setPropertyValue(vOptionNames[nToken], uno::makeAny(sToken));
            comphelper::ConfigurationHelper::flush(m_xCfg);
        }
        catch (uno::Exception const& ex)
        {
            SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
        }
    }
    else
        SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
}

// -----------------------------------------------------------------------

OUString SvtUserOptions::Impl::GetFullName () const
{
    // TODO international name
    OUString sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
    if (!sFullName.isEmpty())
        sFullName += " ";
    sFullName += GetToken(USER_OPT_LASTNAME).trim();
    return sFullName;
}

// -----------------------------------------------------------------------

void SvtUserOptions::Impl::Notify ()
{
    NotifyListeners(0);
}

// -----------------------------------------------------------------------

sal_Bool SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken) const
{
    if (nToken < nOptionNameCount)
    {
        uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
        uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
        beans::Property aProp = xInfo->getPropertyByName(vOptionNames[nToken]);
        return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
            beans::PropertyAttribute::READONLY);
    }
    else
    {
        SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
        return sal_False;
    }
}

// class SvtUserOptions --------------------------------------------------

SvtUserOptions::SvtUserOptions ()
{
    // Global access, must be guarded (multithreading)
    osl::MutexGuard aGuard(GetInitMutex());

    if (pSharedImpl.expired())
    {
        RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SvtUserOptions::Impl::ctor()");
        pImpl.reset(new Impl);
        pSharedImpl = pImpl;
        ItemHolder1::holdConfigItem(E_USEROPTIONS);
    }
    pImpl = pSharedImpl.lock();
    pImpl->AddListener(this);
}

// -----------------------------------------------------------------------

SvtUserOptions::~SvtUserOptions()
{
    // Global access, must be guarded (multithreading)
    osl::MutexGuard aGuard( GetInitMutex() );
    pImpl->RemoveListener(this);
}

// -----------------------------------------------------------------------

namespace
{
    class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
}

osl::Mutex& SvtUserOptions::GetInitMutex()
{
    return theUserOptionsMutex::get();
}

// -----------------------------------------------------------------------

//
// getters
//
OUString SvtUserOptions::GetCompany        () const { return GetToken(USER_OPT_COMPANY); }
OUString SvtUserOptions::GetFirstName      () const { return GetToken(USER_OPT_FIRSTNAME); }
OUString SvtUserOptions::GetLastName       () const { return GetToken(USER_OPT_LASTNAME); }
OUString SvtUserOptions::GetID             () const { return GetToken(USER_OPT_ID); }
OUString SvtUserOptions::GetStreet         () const { return GetToken(USER_OPT_STREET); }
OUString SvtUserOptions::GetCity           () const { return GetToken(USER_OPT_CITY); }
OUString SvtUserOptions::GetState          () const { return GetToken(USER_OPT_STATE); }
OUString SvtUserOptions::GetZip            () const { return GetToken(USER_OPT_ZIP); }
OUString SvtUserOptions::GetCountry        () const { return GetToken(USER_OPT_COUNTRY); }
OUString SvtUserOptions::GetPosition       () const { return GetToken(USER_OPT_POSITION); }
OUString SvtUserOptions::GetTitle          () const { return GetToken(USER_OPT_TITLE); }
OUString SvtUserOptions::GetTelephoneHome  () const { return GetToken(USER_OPT_TELEPHONEHOME); }
OUString SvtUserOptions::GetTelephoneWork  () const { return GetToken(USER_OPT_TELEPHONEWORK); }
OUString SvtUserOptions::GetFax            () const { return GetToken(USER_OPT_FAX); }
OUString SvtUserOptions::GetEmail          () const { return GetToken(USER_OPT_EMAIL); }
OUString SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER); }

// -----------------------------------------------------------------------

//
// setters
//
void SvtUserOptions::SetCompany   (OUString const& sToken) { SetToken(USER_OPT_COMPANY,   sToken); }
void SvtUserOptions::SetFirstName (OUString const& sToken) { SetToken(USER_OPT_FIRSTNAME, sToken); }
void SvtUserOptions::SetLastName  (OUString const& sToken) { SetToken(USER_OPT_LASTNAME,  sToken); }
void SvtUserOptions::SetID        (OUString const& sToken) { SetToken(USER_OPT_ID,        sToken); }
void SvtUserOptions::SetStreet    (OUString const& sToken) { SetToken(USER_OPT_STREET,    sToken); }
void SvtUserOptions::SetState     (OUString const& sToken) { SetToken(USER_OPT_STATE,     sToken); }
void SvtUserOptions::SetZip       (OUString const& sToken) { SetToken(USER_OPT_ZIP,       sToken); }
void SvtUserOptions::SetCountry   (OUString const& sToken) { SetToken(USER_OPT_COUNTRY,   sToken); }
void SvtUserOptions::SetPosition  (OUString const& sToken) { SetToken(USER_OPT_POSITION,  sToken); }
void SvtUserOptions::SetTitle     (OUString const& sToken) { SetToken(USER_OPT_TITLE,     sToken); }
void SvtUserOptions::SetFax       (OUString const& sToken) { SetToken(USER_OPT_FAX,       sToken); }
void SvtUserOptions::SetTelephoneHome  (OUString const& sToken) { SetToken(USER_OPT_TELEPHONEHOME,  sToken); }
void SvtUserOptions::SetTelephoneWork  (OUString const& sToken) { SetToken(USER_OPT_TELEPHONEWORK,  sToken); }
void SvtUserOptions::SetCustomerNumber (OUString const& sToken) { SetToken(USER_OPT_CUSTOMERNUMBER, sToken); }

// -----------------------------------------------------------------------

sal_Bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
{
    osl::MutexGuard aGuard(GetInitMutex());
    return pImpl->IsTokenReadonly(nToken);
}

// -----------------------------------------------------------------------

OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
{
    osl::MutexGuard aGuard(GetInitMutex());
    return pImpl->GetToken(nToken);
}

// -----------------------------------------------------------------------

void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
{
    osl::MutexGuard aGuard(GetInitMutex());
    pImpl->SetToken(nToken, rNewToken);
}

// -----------------------------------------------------------------------

OUString SvtUserOptions::GetFullName () const
{
    osl::MutexGuard aGuard(GetInitMutex());
    return pImpl->GetFullName();
}

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