summaryrefslogtreecommitdiff
path: root/unotools/source/config/regoptions.cxx
blob: 9b0550a8cdc587e34a4bc6714b8a32c9ad7f67af (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_unotools.hxx"
#include <unotools/regoptions.hxx>
#include <unotools/confignode.hxx>
#include <tools/date.hxx>
#include <comphelper/processfactory.hxx>
#include <osl/mutex.hxx>
#include <unotools/bootstrap.hxx>
#include <rtl/ustring.hxx>
#include <rtl/instance.hxx>

//........................................................................
namespace utl
{
//........................................................................

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

    //====================================================================
    //= local helpers
    //====================================================================
    //--------------------------------------------------------------------
    /** converts a string representation of a date into a integer representation
        <p>No semantic check is made, i.e. if the string describes the 40.12, this is not recognized. In opposite,
        the string must have a valid structure (DD.MM.YYYY).</p>
        @return
            the integer representation of the date which can be used with the Date class,
            or 0 if the given string was no valid date representation
    */
    static sal_Int32 lcl_convertString2Date( const ::rtl::OUString& _rStringRep )
    {
        sal_Int32 nDateIntRep = 0;
        if ( _rStringRep.getLength() == 2 + 1 + 2 + 1 + 4 ) // DD.MM.YYYY
        {
            // validate the string
            sal_Bool bValid = sal_True;

            const sal_Unicode* pStringRep = _rStringRep.getStr();
            sal_Int32 nLen = _rStringRep.getLength();
            for (   sal_Int32 nPos = 0;
                    bValid && ( nPos < nLen );
                    ++nPos, ++pStringRep
                )
            {
                if ( ( 2 == nPos ) || ( 5 == nPos ) )
                    bValid = '.' == *pStringRep;        // the number separators
                else
                    bValid = ( *pStringRep >= '0' ) && ( *pStringRep <= '9' );
            }

            // passed the test?
            if ( bValid )
            {
                Date aDate;
                aDate.SetDay    ( (sal_uInt16)_rStringRep.copy( 0, 2 ).toInt32( ) );
                aDate.SetMonth  ( (sal_uInt16)_rStringRep.copy( 3, 2 ).toInt32( ) );
                aDate.SetYear   ( (sal_uInt16)_rStringRep.copy( 6, 4 ).toInt32( ) );
                nDateIntRep = aDate.GetDate();
            }
        }

        return nDateIntRep;
    }

    //--------------------------------------------------------------------
    static const ::rtl::OUString& lcl_fillToken( const sal_Int32 /* [in] */ _nToken, const sal_Int16 /* [in] */ _nDigits, ::rtl::OUString& /* [out] */ _rToken )
    {
        // convert into string
        ::rtl::OUString sLeanToken = ::rtl::OUString::valueOf( _nToken );
        // check length
        if ( sLeanToken.getLength() < _nDigits )
        {   // fill
            OSL_ENSURE( _nDigits <= 4, "lcl_fillToken: invalid digit number!" );
            _rToken = ::rtl::OUString( "0000", _nDigits - sLeanToken.getLength(), RTL_TEXTENCODING_ASCII_US );
            _rToken += sLeanToken;
        }
        else
            _rToken = sLeanToken;

        return _rToken;
    }

    //--------------------------------------------------------------------
    /** converts a integer representation of a date into a string representation
    */
    static ::rtl::OUString lcl_ConvertDate2String( const Date& _rDate )
    {
        OSL_ENSURE( _rDate.IsValid(), "lcl_ConvertDate2String: invalid integer representation!" );

        sal_Unicode cSeparator( '.' );
        ::rtl::OUString sSeparator( &cSeparator, 1 );

        ::rtl::OUString sStringRep;
        ::rtl::OUString sToken;
        sStringRep += lcl_fillToken( (sal_Int32)_rDate.GetDay(), 2, sToken );
        sStringRep += sSeparator;
        sStringRep += lcl_fillToken( (sal_Int32)_rDate.GetMonth(), 2, sToken );
        sStringRep += sSeparator;
        sStringRep += lcl_fillToken( (sal_Int32)_rDate.GetYear(), 4, sToken );

        return sStringRep;
    }

    //--------------------------------------------------------------------
    /// checks whether a given trigger date is reached (i.e. is _before_ the current date)
    static sal_Bool lcl_reachedTriggerDate( const Date& _rTriggerDate )
    {
        return _rTriggerDate <= Date();
    }

    //--------------------------------------------------------------------
    #define DECLARE_STATIC_LAZY_USTRING( name ) \
    static const ::rtl::OUString& lcl_get##name##Name() \
    {   \
        static const ::rtl::OUString sName(RTL_CONSTASCII_USTRINGPARAM( #name )); \
        return sName;   \
    }

    DECLARE_STATIC_LAZY_USTRING( ReminderDate );
    DECLARE_STATIC_LAZY_USTRING( RequestDialog );
    DECLARE_STATIC_LAZY_USTRING( ShowMenuItem );
    DECLARE_STATIC_LAZY_USTRING( Patch );

    //====================================================================
    //= RegOptionsImpl
    //====================================================================
    class RegOptionsImpl
    {
    private:
        OConfigurationTreeRoot  m_aRegistrationNode;        // the configuration node we need to access our persistent data

        String                  m_sRegistrationURL;         // the URL to use when doing an online registration
        Date                    m_aReminderDate;            // the reminder date as found in the configuration
        sal_Int32               m_nDialogCounter;           // the dialog counter - see getDialogPermission
        sal_Bool                m_bShowMenuItem;            // the flag indicating if the registration menu item is allowed

        static  RegOptionsImpl* s_pSingleInstance;          // the one and only instance of this class
        static  sal_Int32       s_nInstanceCount;           // reference counter for the instances
        static  sal_Bool        s_bThisSessionDone;         // the flag indicating if for this session, everything beeing relevant has already been done

    private:
        RegOptionsImpl( );

        static  ::osl::Mutex&   getStaticMutex();           // get the mutex used to protect the static members of this class

        void                    commit( );
        sal_Int32               getBuildId() const;

    private:
        RegOptions::DialogPermission    implGetDialogPermission( ) const;

    public:
        static RegOptionsImpl*  registerClient( );
        static void             revokeClient( );

        inline sal_Bool         hasURL( ) const             { return ( 0 != m_sRegistrationURL.Len() ); }
        inline sal_Bool         allowMenu( ) const          { return hasURL() && m_bShowMenuItem; }
        inline String           getRegistrationURL( ) const { return m_sRegistrationURL; }

        RegOptions::DialogPermission    getDialogPermission( ) const;
        void                            markSessionDone( );
        void                            activateReminder( sal_Int32 _nDaysFromNow );
        void                            removeReminder();
        bool                            hasReminderDateCome() const;
    };

    //--------------------------------------------------------------------
    RegOptionsImpl* RegOptionsImpl::s_pSingleInstance = NULL;
    sal_Bool        RegOptionsImpl::s_bThisSessionDone = sal_False;
    sal_Int32       RegOptionsImpl::s_nInstanceCount = 0;

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

    //--------------------------------------------------------------------
    ::osl::Mutex& RegOptionsImpl::getStaticMutex()
    {
        return theRegOptionsImplMutex::get();
    }

    //--------------------------------------------------------------------
    void RegOptionsImpl::commit( )
    {
        m_aRegistrationNode.commit( );
    }

    //--------------------------------------------------------------------
    RegOptionsImpl* RegOptionsImpl::registerClient( )
    {
        ::osl::MutexGuard aGuard( getStaticMutex() );

        if ( !s_pSingleInstance )
            s_pSingleInstance = new RegOptionsImpl;

        ++s_nInstanceCount;
        return s_pSingleInstance;
    }

    //--------------------------------------------------------------------
    void RegOptionsImpl::revokeClient( )
    {
        ::osl::MutexGuard aGuard( getStaticMutex() );
        OSL_ENSURE( s_nInstanceCount, "RegOptionsImpl::revokeClient: there are no clients alive!" );
        OSL_ENSURE( s_pSingleInstance || !s_nInstanceCount, "RegOptionsImpl::revokeClient: invalid instance pointer!" );

        if ( s_nInstanceCount )
        {
            if ( s_pSingleInstance )
                // commit the changes done by this client
                s_pSingleInstance->commit();

            if ( 0 == --s_nInstanceCount )
            {
                delete s_pSingleInstance;
                s_pSingleInstance = NULL;
            }
        }
    }

    //--------------------------------------------------------------------
    RegOptionsImpl::RegOptionsImpl( )
        :m_nDialogCounter   ( 0 )
        ,m_bShowMenuItem    ( sal_False )
    {
        // create the config node for all our registration information
        m_aRegistrationNode = OConfigurationTreeRoot::createWithServiceFactory(
            ::comphelper::getProcessServiceFactory(),
            ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Common/Help/Registration"))
        );

        // cache some data
        //the URL to use for online registration
        ::rtl::OUString sStringValue;
        m_aRegistrationNode.getNodeValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) ) >>= sStringValue;
        m_sRegistrationURL = sStringValue;

        // the state of the dialog
        m_aRegistrationNode.getNodeValue( lcl_getRequestDialogName() ) >>= m_nDialogCounter;

        // the flag for showing the menu item
        sal_Bool bBoolValue = sal_False;
        m_aRegistrationNode.getNodeValue( lcl_getShowMenuItemName() ) >>= bBoolValue;
        m_bShowMenuItem = bBoolValue;

        // the reminder date (if any)
        sal_Int32 nIntDate = 0;
        sStringValue = ::rtl::OUString();
        m_aRegistrationNode.getNodeValue( lcl_getReminderDateName() ) >>= sStringValue;
        bool bIsPatchDate = ( sStringValue.equals( lcl_getPatchName() ) != sal_False );
        if ( !bIsPatchDate && sStringValue.getLength() )
            nIntDate = lcl_convertString2Date( sStringValue );
        m_aReminderDate.SetDate( nIntDate );
    }

    //--------------------------------------------------------------------
    RegOptions::DialogPermission RegOptionsImpl::implGetDialogPermission( ) const
    {
        RegOptions::DialogPermission eResult = RegOptions::dpDisabled;
        // no URL or a counter already decreased to zero means the dialog is disabled
        if ( hasURL() && ( m_nDialogCounter > 0 ) )
        {
            // during every session, the counter will be decreased
            // If it reaches zero, the dialog shall be executed
            if ( 1 == m_nDialogCounter )
            {
                if ( m_aReminderDate.IsValid( ) )
                {   // valid reminder date
                    // assume remind later
                    eResult = RegOptions::dpRemindLater;
                    // and check if we reached the reminder date
                    if ( lcl_reachedTriggerDate( m_aReminderDate ) )
                        eResult = RegOptions::dpThisSession;
                }
                else
                    eResult = RegOptions::dpThisSession;    // execute in this session
            }
            else
                eResult = RegOptions::dpNotThisSession;     // first trigger session not reached

            // a last check ....
            if  (   ( s_bThisSessionDone )                  // this session is already marked as "done"
                &&  ( RegOptions::dpThisSession == eResult )// but without this, the state would be "now"
                )
                eResult = RegOptions::dpDisabled;           // -> change state to "disabled"
        }

        return eResult;
    }

    //--------------------------------------------------------------------
    RegOptions::DialogPermission RegOptionsImpl::getDialogPermission( ) const
    {
        OSL_ENSURE( !s_bThisSessionDone, "RegOptionsImpl::getDialogPermission: should never be asked in this session, again!" );
            // Somebody already marked this session as "everything relevant happened". So why sombody (else?) asks
            // me again?

        return implGetDialogPermission( );
    }

    //--------------------------------------------------------------------
    void RegOptionsImpl::activateReminder( sal_Int32 _nDaysFromNow )
    {
        OSL_ENSURE( s_bThisSessionDone ||  ( implGetDialogPermission( ) != RegOptions::dpDisabled ), "RegOptionsImpl::activateReminder: invalid call!" );
        OSL_ENSURE( _nDaysFromNow > 0, "RegOptionsImpl::activateReminder: invalid argument!" );

        // calc the reminder
        m_aReminderDate = Date() + _nDaysFromNow;   // today (default ctor) + days

        // remember the date
        m_aRegistrationNode.setNodeValue(
            lcl_getReminderDateName(),
            makeAny( lcl_ConvertDate2String( m_aReminderDate ) )
        );
        // to be on the save side, write the counter
        m_aRegistrationNode.setNodeValue(
            lcl_getRequestDialogName(),
            makeAny( (sal_Int32)1 )
        );

        // mark this session as done
        if ( !s_bThisSessionDone )
            markSessionDone( );
    }

    //--------------------------------------------------------------------
    void RegOptionsImpl::removeReminder()
    {
        ::rtl::OUString aDefault;
        ::rtl::OUString aReminderValue( lcl_getPatchName() );
        aReminderValue += ::rtl::OUString::valueOf(getBuildId());

        m_aRegistrationNode.setNodeValue(
            lcl_getReminderDateName(),
            Any( aReminderValue )
        );
    }

    //--------------------------------------------------------------------
    sal_Int32 RegOptionsImpl::getBuildId() const
    {
        sal_Int32 nBuildId( 0 );
        ::rtl::OUString aDefault;
        ::rtl::OUString aBuildIdData = utl::Bootstrap::getBuildIdData( aDefault );
        sal_Int32 nIndex1 = aBuildIdData.indexOf(':');
        sal_Int32 nIndex2 = aBuildIdData.indexOf(')');
        if (( nIndex1 > 0 ) && ( nIndex2 > 0 ) && ( nIndex2-1 > nIndex1+1 ))
        {
            ::rtl::OUString aBuildId = aBuildIdData.copy( nIndex1+1, nIndex2-nIndex1-1 );
            nBuildId = aBuildId.toInt32();
        }

        return nBuildId;
    }

    //--------------------------------------------------------------------
    bool RegOptionsImpl::hasReminderDateCome() const
    {
        bool bRet = false;
        sal_Int32 nDate = 0;
        ::rtl::OUString sDate;
        m_aRegistrationNode.getNodeValue( lcl_getReminderDateName() ) >>= sDate;
        if ( sDate.getLength() )
        {
            if ( sDate.indexOf( lcl_getPatchName() ) == 0)
            {
                if (sDate.equals( lcl_getPatchName() ))
                    bRet = true;
                else if (sDate.getLength() > lcl_getPatchName().getLength() )
                {
                    // Check the build ID to determine if the registration
                    // dialog needs to be shown.
                    sal_Int32 nBuildId = getBuildId();
                    ::rtl::OUString aStoredBuildId( sDate.copy(lcl_getPatchName().getLength()));

                    // remind if the current build ID is not the same as the stored one
                    if ( nBuildId != aStoredBuildId.toInt32() )
                        bRet = true;
                }
            }
            else
            {
                nDate = lcl_convertString2Date( sDate );
                if ( nDate > 0 )
                {
                    Date aReminderDate;
                    aReminderDate.SetDate( nDate );
                    bRet = aReminderDate <= Date();
                }
            }
        }
        else
            bRet = true;

        return bRet;
    }

    //--------------------------------------------------------------------
    void RegOptionsImpl::markSessionDone( )
    {
        OSL_ENSURE( !s_bThisSessionDone, "RegOptionsImpl::markSessionDone: already marked!" );
        OSL_ENSURE( implGetDialogPermission( ) != RegOptions::dpDisabled, "RegOptionsImpl::markSessionDone: invalid call!" );
        if ( !s_bThisSessionDone )
        {
            RegOptions::DialogPermission eOldPermission = implGetDialogPermission( );

            s_bThisSessionDone = sal_True;

            if ( RegOptions::dpRemindLater == eOldPermission )
            {   // no action required. If we shall remind later, the counter is already at 1, we should not change this,
                // as the next smaller number (which is 0 :) means that the dialog would be disabled
                OSL_ENSURE( 1 == m_nDialogCounter, "RegOptionsImpl::markSessionDone: invalid session counter (1)!" );
            }
            else
            {
                OSL_ENSURE( m_nDialogCounter > 0, "RegOptionsImpl::markSessionDone: invalid session counter (2)!" );
                --m_nDialogCounter;

                // decrease the session counter
                m_aRegistrationNode.setNodeValue(
                    lcl_getRequestDialogName(),
                    makeAny( (sal_Int32)m_nDialogCounter )
                );

                // and clear the reminder date
                removeReminder();
            }
        }
    }

    //====================================================================
    //= RegOptions
    //====================================================================
    //--------------------------------------------------------------------
    RegOptions::RegOptions()
        :m_pImpl( NULL )
    {
    }

    //--------------------------------------------------------------------
    void RegOptions::ensureImpl( )
    {
        if ( !m_pImpl )
            m_pImpl = RegOptionsImpl::registerClient();
    }

    //--------------------------------------------------------------------
    RegOptions::~RegOptions()
    {
        if ( m_pImpl )
        {
            RegOptionsImpl::revokeClient();
            m_pImpl = NULL;
        }
    }

    //--------------------------------------------------------------------
    String RegOptions::getRegistrationURL( ) const
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        return m_pImpl->getRegistrationURL();
    }

    //--------------------------------------------------------------------
    RegOptions::DialogPermission RegOptions::getDialogPermission( ) const
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        return m_pImpl->getDialogPermission();
    }

    //--------------------------------------------------------------------
    void RegOptions::markSessionDone( )
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        m_pImpl->markSessionDone();
    }

    //--------------------------------------------------------------------
    void RegOptions::activateReminder( sal_Int32 _nDaysFromNow )
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        m_pImpl->activateReminder( _nDaysFromNow );
    }

    //--------------------------------------------------------------------
    sal_Bool RegOptions::allowMenu( ) const
    {
        /// we cache this setting, 'cause it is needed very often
        static sal_Bool bKnowMenuPermission = sal_False;
        static sal_Bool bAllowMenu          = sal_False;

        if ( !bKnowMenuPermission )
        {
            const_cast< RegOptions* >( this )->ensureImpl( );
            bAllowMenu = m_pImpl->allowMenu();
            bKnowMenuPermission = sal_True;
        }
        return bAllowMenu;
    }

    //--------------------------------------------------------------------
    void RegOptions::removeReminder()
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        m_pImpl->removeReminder();
    }

    //--------------------------------------------------------------------
    bool RegOptions::hasReminderDateCome() const
    {
        const_cast< RegOptions* >( this )->ensureImpl( );
        return m_pImpl->hasReminderDateCome();
    }

//........................................................................
}   // namespace utl
//........................................................................

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