Files
libreoffice/svtools/source/config/useroptions.cxx

634 lines
22 KiB
C++
Raw Normal View History

2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* $RCSfile: useroptions.cxx,v $
*
2000-10-26 11:58:47 +00:00
* $Revision: 1.6 $
2000-09-18 16:07:07 +00:00
*
2000-10-26 11:58:47 +00:00
* last change: $Author: pb $ $Date: 2000-10-26 12:56:39 $
2000-09-18 16:07:07 +00:00
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (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.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#pragma hdrstop
#include "useroptions.hxx"
#ifndef _UTL_CONFIGMGR_HXX_
#include <unotools/configmgr.hxx>
#endif
#ifndef _TOOLS_DEBUG_HXX
#include <tools/debug.hxx>
#endif
#ifndef _COM_SUN_STAR_UNO_ANY_HXX_
#include <com/sun/star/uno/Any.hxx>
#endif
#ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
#include <com/sun/star/uno/Sequence.hxx>
#endif
using namespace utl;
using namespace rtl;
using namespace com::sun::star::uno;
2000-09-25 09:00:42 +00:00
// define ----------------------------------------------------------------
#define USER_CITY 0
#define USER_COMPANY 1
#define USER_COUNTRY 2
#define USER_CUSTOMERNUMBER 3
#define USER_EMAIL 4
#define USER_FAX 5
#define USER_FIRSTNAME 6
#define USER_LASTNAME 7
#define USER_POSITION 8
#define USER_STATE 9
#define USER_STREET 10
#define USER_TELEPHONEHOME 11
#define USER_TELEPHONEWORK 12
#define USER_TITLE 13
#define USER_ID 14
#define USER_ZIP 15
// class SvtUserOptions_Impl ---------------------------------------------
class SvtUserOptions_Impl : public utl::ConfigItem
{
private:
String m_aCompany;
String m_aFirstName;
String m_aLastName;
String m_aID;
String m_aStreet;
String m_aCity;
String m_aState;
String m_aZip;
String m_aCountry;
String m_aPosition;
String m_aTitle;
String m_aTelephoneHome;
String m_aTelephoneWork;
String m_aFax;
String m_aEmail;
String m_aCustomerNumber;
String m_aEmptyString;
2000-10-09 05:30:33 +00:00
String m_aFullName;
2000-09-25 09:00:42 +00:00
::osl::Mutex m_aMutex;
2000-10-09 05:30:33 +00:00
typedef String SvtUserOptions_Impl:: *StrPtr;
2000-09-25 09:00:42 +00:00
// not const because of using a mutex
2000-10-09 05:30:33 +00:00
const String& GetToken( StrPtr pPtr );
void SetToken( StrPtr pPtr, const String& rNewToken );
void InitFullName();
2000-09-25 09:00:42 +00:00
public:
SvtUserOptions_Impl();
virtual void Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
virtual void Commit();
// get the user token, not const because of using a mutex
2000-10-09 05:30:33 +00:00
const String& GetCompany() { return GetToken( &SvtUserOptions_Impl::m_aCompany ); }
const String& GetFirstName() { return GetToken( &SvtUserOptions_Impl::m_aFirstName ); }
const String& GetLastName() { return GetToken( &SvtUserOptions_Impl::m_aLastName ); }
const String& GetID() { return GetToken( &SvtUserOptions_Impl::m_aID ); }
const String& GetStreet() { return GetToken( &SvtUserOptions_Impl::m_aStreet ); }
const String& GetCity() { return GetToken( &SvtUserOptions_Impl::m_aCity ); }
const String& GetState() { return GetToken( &SvtUserOptions_Impl::m_aState ); }
const String& GetZip() { return GetToken( &SvtUserOptions_Impl::m_aZip ); }
const String& GetCountry() { return GetToken( &SvtUserOptions_Impl::m_aCountry ); }
const String& GetPosition() { return GetToken( &SvtUserOptions_Impl::m_aPosition ); }
const String& GetTitle() { return GetToken( &SvtUserOptions_Impl::m_aTitle ); }
const String& GetTelephoneHome() { return GetToken( &SvtUserOptions_Impl::m_aTelephoneHome ); }
const String& GetTelephoneWork() { return GetToken( &SvtUserOptions_Impl::m_aTelephoneWork ); }
const String& GetFax() { return GetToken( &SvtUserOptions_Impl::m_aFax ); }
const String& GetEmail() { return GetToken( &SvtUserOptions_Impl::m_aEmail ); }
const String& GetCustomerNumber() { return GetToken( &SvtUserOptions_Impl::m_aCustomerNumber ); }
const String& GetFullName();
2000-09-25 09:00:42 +00:00
// set the address token
2000-10-09 05:30:33 +00:00
void SetCompany( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aCompany, rNewToken ); }
void SetFirstName( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aFirstName, rNewToken ); }
void SetLastName( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aLastName, rNewToken ); }
void SetID( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aID, rNewToken ); }
void SetStreet( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aStreet, rNewToken ); }
void SetCity( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aCity, rNewToken ); }
void SetState( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aState, rNewToken ); }
void SetZip( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aZip, rNewToken ); }
void SetCountry( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aCountry, rNewToken ); }
void SetPosition( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aPosition, rNewToken ); }
void SetTitle( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aTitle, rNewToken ); }
2000-09-25 09:00:42 +00:00
void SetTelephoneHome( const String& rNewToken )
2000-10-09 05:30:33 +00:00
{ SetToken( &SvtUserOptions_Impl::m_aTelephoneHome, rNewToken ); }
2000-09-25 09:00:42 +00:00
void SetTelephoneWork( const String& rNewToken )
2000-10-09 05:30:33 +00:00
{ SetToken( &SvtUserOptions_Impl::m_aTelephoneWork, rNewToken ); }
void SetFax( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aFax, rNewToken ); }
void SetEmail( const String& rNewToken )
{ SetToken( &SvtUserOptions_Impl::m_aEmail, rNewToken ); }
2000-09-25 09:00:42 +00:00
void SetCustomerNumber( const String& rNewToken )
2000-10-09 05:30:33 +00:00
{ SetToken( &SvtUserOptions_Impl::m_aCustomerNumber, rNewToken ); }
2000-09-25 09:00:42 +00:00
};
// global ----------------------------------------------------------------
static SvtUserOptions_Impl* pOptions = NULL;
static sal_Int32 nRefCount = 0;
// functions -------------------------------------------------------------
Sequence< OUString > GetUserPropertyNames()
2000-09-18 16:07:07 +00:00
{
static const char* aPropNames[] =
{
2000-09-25 09:00:42 +00:00
"Data/City", // USER_CITY
"Data/Company", // USER_COMPANY
"Data/Country", // USER_COUNTRY
"Data/CustomerNumber", // USER_CUSTOMERNUMBER
"Data/EMail", // USER_EMAIL
"Data/Fax", // USER_FAX
"Data/FirstName", // USER_FIRSTNAME
"Data/LastName", // USER_LASTNAME
"Data/Position", // USER_POSITION
"Data/State", // USER_STATE
"Data/Street", // USER_STREET
"Data/TelephoneHome", // USER_TELEPHONEHOME
"Data/TelephoneWork", // USER_TELEPHONEWORK
"Data/Title", // USER_TITLE
"Data/UserID", // USER_ID
"Data/Zip", // USER_ZIP
2000-09-18 16:07:07 +00:00
};
2000-09-25 09:00:42 +00:00
const int nCount = sizeof( aPropNames ) / sizeof( const char* );
2000-09-18 16:07:07 +00:00
Sequence< OUString > aNames( nCount );
OUString* pNames = aNames.getArray();
for ( int i = 0; i < nCount; i++ )
pNames[i] = OUString::createFromAscii( aPropNames[i] );
return aNames;
}
2000-09-25 09:00:42 +00:00
// class SvtUserOptions_Impl ---------------------------------------------
2000-09-18 16:07:07 +00:00
2000-10-09 05:30:33 +00:00
const String& SvtUserOptions_Impl::GetToken( StrPtr pPtr )
2000-09-18 16:07:07 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
2000-10-09 05:30:33 +00:00
return this->*pPtr;
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
2000-10-09 05:30:33 +00:00
void SvtUserOptions_Impl::SetToken( StrPtr pPtr, const String& rNewToken )
2000-09-18 16:07:07 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
2000-10-09 05:30:33 +00:00
this->*pPtr = rNewToken;
2000-09-18 16:07:07 +00:00
SetModified();
}
// -----------------------------------------------------------------------
2000-10-09 05:30:33 +00:00
void SvtUserOptions_Impl::InitFullName()
{
m_aFullName = GetFirstName();
m_aFullName.EraseLeadingAndTrailingChars();
if ( m_aFullName.Len() )
m_aFullName += ' ';
m_aFullName += GetLastName();
m_aFullName.EraseTrailingChars();
}
// -----------------------------------------------------------------------
2000-09-25 09:00:42 +00:00
SvtUserOptions_Impl::SvtUserOptions_Impl() :
ConfigItem( OUString::createFromAscii("UserProfile") )
2000-09-18 16:07:07 +00:00
{
2000-09-25 09:00:42 +00:00
Sequence< OUString > aNames = GetUserPropertyNames();
2000-09-18 16:07:07 +00:00
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() )
{
OUString aTempStr;
for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
{
DBG_ASSERT( pValues[nProp].hasValue(), "property value missing" );
if ( pValues[nProp].hasValue() )
{
2000-09-25 09:00:42 +00:00
if ( pValues[nProp] >>= aTempStr )
{
switch ( nProp )
{
case USER_COMPANY: m_aCompany = String( aTempStr ); break;
case USER_FIRSTNAME: m_aFirstName = String( aTempStr ); break;
case USER_LASTNAME: m_aLastName = String( aTempStr ); break;
case USER_ID: m_aID = String( aTempStr ); break;
case USER_STREET: m_aStreet = String( aTempStr ); break;
case USER_CITY: m_aCity = String( aTempStr ); break;
case USER_STATE: m_aState = String( aTempStr ); break;
case USER_ZIP: m_aZip = String( aTempStr ); break;
case USER_COUNTRY: m_aCountry = String( aTempStr ); break;
case USER_POSITION: m_aPosition = String( aTempStr ); break;
case USER_TITLE: m_aTitle = String( aTempStr ); break;
case USER_TELEPHONEHOME: m_aTelephoneHome = String( aTempStr ); break;
case USER_TELEPHONEWORK: m_aTelephoneWork = String( aTempStr ); break;
case USER_FAX: m_aFax = String( aTempStr ); break;
case USER_EMAIL: m_aEmail = String( aTempStr ); break;
case USER_CUSTOMERNUMBER: m_aCustomerNumber = String( aTempStr ); break;
default:
DBG_ERRORFILE( "invalid index to load a user token" );
}
}
else
2000-09-18 16:07:07 +00:00
{
2000-09-25 09:00:42 +00:00
DBG_ERRORFILE( "Wrong any type" );
2000-09-18 16:07:07 +00:00
}
}
}
}
2000-10-09 05:30:33 +00:00
InitFullName();
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
2000-09-25 09:00:42 +00:00
void SvtUserOptions_Impl::Commit()
2000-09-18 16:07:07 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
2000-09-25 09:00:42 +00:00
Sequence< OUString > aNames = GetUserPropertyNames();
2000-09-18 16:07:07 +00:00
OUString* pNames = aNames.getArray();
Sequence< Any > aValues( aNames.getLength() );
Any* pValues = aValues.getArray();
const Type& rType = ::getBooleanCppuType();
OUString aTempStr;
for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
{
switch ( nProp )
{
case USER_COMPANY: aTempStr = OUString( m_aCompany ); break;
case USER_FIRSTNAME: aTempStr = OUString( m_aFirstName ); break;
case USER_LASTNAME: aTempStr = OUString( m_aLastName ); break;
case USER_ID: aTempStr = OUString( m_aID ); break;
case USER_STREET: aTempStr = OUString( m_aStreet ); break;
case USER_CITY: aTempStr = OUString( m_aCity ); break;
case USER_STATE: aTempStr = OUString( m_aState ); break;
case USER_ZIP: aTempStr = OUString( m_aZip ); break;
case USER_COUNTRY: aTempStr = OUString( m_aCountry ); break;
case USER_POSITION: aTempStr = OUString( m_aPosition ); break;
case USER_TITLE: aTempStr = OUString( m_aTitle ); break;
case USER_TELEPHONEHOME: aTempStr = OUString( m_aTelephoneHome ); break;
case USER_TELEPHONEWORK: aTempStr = OUString( m_aTelephoneWork ); break;
case USER_FAX: aTempStr = OUString( m_aFax ); break;
case USER_EMAIL: aTempStr = OUString( m_aEmail ); break;
case USER_CUSTOMERNUMBER: aTempStr = OUString( m_aCustomerNumber ); break;
default:
DBG_ERRORFILE( "invalid index to save a user token" );
}
pValues[nProp] <<= aTempStr;
}
PutProperties( aNames, aValues );
}
// -----------------------------------------------------------------------
2000-10-09 05:30:33 +00:00
const String& SvtUserOptions_Impl::GetFullName()
{
if ( IsModified() )
InitFullName();
return m_aFullName;
}
// -----------------------------------------------------------------------
2000-09-25 09:00:42 +00:00
void SvtUserOptions_Impl::Notify( const Sequence<rtl::OUString>& aPropertyNames )
2000-09-18 16:07:07 +00:00
{
DBG_ERRORFILE( "properties have been changed" );
}
2000-09-25 09:00:42 +00:00
// class SvtUserOptions --------------------------------------------------
SvtUserOptions::SvtUserOptions()
{
// Global access, must be guarded (multithreading)
::osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
if ( !pOptions )
pOptions = new SvtUserOptions_Impl;
++nRefCount;
pImp = pOptions;
}
// -----------------------------------------------------------------------
SvtUserOptions::~SvtUserOptions()
{
// Global access, must be guarded (multithreading)
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if ( !--nRefCount )
2000-10-26 11:58:47 +00:00
{
if ( pOptions->IsModified() )
pOptions->Commit();
2000-09-25 09:00:42 +00:00
DELETEZ( pOptions );
2000-10-26 11:58:47 +00:00
}
2000-09-25 09:00:42 +00:00
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetCompany() const
{
return pImp->GetCompany();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetFirstName() const
{
return pImp->GetFirstName();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetLastName() const
{
return pImp->GetLastName();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetID() const
{
return pImp->GetID();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetStreet() const
{
return pImp->GetStreet();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetCity() const
{
return pImp->GetCity();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetState() const
{
return pImp->GetState();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetZip() const
{
return pImp->GetZip();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetCountry() const
{
return pImp->GetCountry();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetPosition() const
{
return pImp->GetPosition();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetTitle() const
{
return pImp->GetTitle();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetTelephoneHome() const
{
return pImp->GetTelephoneHome();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetTelephoneWork() const
{
return pImp->GetTelephoneWork();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetFax() const
{
return pImp->GetFax();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetEmail() const
{
return pImp->GetEmail();
}
// -----------------------------------------------------------------------
const String& SvtUserOptions::GetCustomerNumber() const
{
return pImp->GetCustomerNumber();
}
// -----------------------------------------------------------------------
2000-10-09 05:30:33 +00:00
const String& SvtUserOptions::GetFullName() const
2000-10-06 06:51:48 +00:00
{
2000-10-09 05:30:33 +00:00
return pImp->GetFullName();
2000-10-06 06:51:48 +00:00
}
// -----------------------------------------------------------------------
2000-09-25 09:00:42 +00:00
void SvtUserOptions::SetCompany( const String& rNewToken )
{
pImp->SetCompany( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetFirstName( const String& rNewToken )
{
pImp->SetFirstName( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetLastName( const String& rNewToken )
{
pImp->SetLastName( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetID( const String& rNewToken )
{
pImp->SetID( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetStreet( const String& rNewToken )
{
pImp->SetStreet( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetCity( const String& rNewToken )
{
pImp->SetCity( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetState( const String& rNewToken )
{
pImp->SetState( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetZip( const String& rNewToken )
{
pImp->SetZip( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetCountry( const String& rNewToken )
{
pImp->SetCountry( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetPosition( const String& rNewToken )
{
pImp->SetPosition( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetTitle( const String& rNewToken )
{
pImp->SetTitle( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetTelephoneHome( const String& rNewToken )
{
pImp->SetTelephoneHome( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetTelephoneWork( const String& rNewToken )
{
pImp->SetTelephoneWork( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetFax( const String& rNewToken )
{
pImp->SetFax( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetEmail( const String& rNewToken )
{
pImp->SetEmail( rNewToken );
}
// -----------------------------------------------------------------------
void SvtUserOptions::SetCustomerNumber( const String& rNewToken )
{
pImp->SetCustomerNumber( rNewToken );
}