2010-10-12 15:53:47 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-12 22:04:38 +01:00
/*
* 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 .
*/
2007-09-13 16:53:20 +00:00
# include "MacabRecord.hxx"
# include "macabutilities.hxx"
# include <com/sun/star/util/DateTime.hpp>
# include <premac.h>
# include <Carbon/Carbon.h>
# include <AddressBook/ABAddressBookC.h>
# include <postmac.h>
# include <connectivity/dbconversion.hxx>
using namespace connectivity : : macab ;
using namespace com : : sun : : star : : util ;
using namespace : : dbtools ;
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
MacabRecord : : MacabRecord ( )
{
size = 0 ;
2015-11-10 16:18:56 +01:00
fields = nullptr ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
MacabRecord : : MacabRecord ( const sal_Int32 _size )
{
size = _size ;
fields = new macabfield * [ size ] ;
sal_Int32 i ;
for ( i = 0 ; i < size ; i + + )
2015-11-10 16:18:56 +01:00
fields [ i ] = nullptr ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
MacabRecord : : ~ MacabRecord ( )
{
if ( size > 0 )
{
int i ;
for ( i = 0 ; i < size ; i + + )
{
delete fields [ i ] ;
2015-11-10 16:18:56 +01:00
fields [ i ] = nullptr ;
2007-09-13 16:53:20 +00:00
}
}
delete [ ] fields ;
2015-11-10 16:18:56 +01:00
fields = nullptr ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
void MacabRecord : : insertAtColumn ( CFTypeRef _value , ABPropertyType _type , const sal_Int32 _column )
{
if ( _column < size )
{
2015-11-10 16:18:56 +01:00
if ( fields [ _column ] = = nullptr )
2007-09-13 16:53:20 +00:00
fields [ _column ] = new macabfield ;
fields [ _column ] - > value = _value ;
2008-03-05 15:38:50 +00:00
if ( fields [ _column ] - > value )
CFRetain ( fields [ _column ] - > value ) ;
2007-09-13 16:53:20 +00:00
fields [ _column ] - > type = _type ;
}
}
2014-02-22 21:20:15 +01:00
2014-09-15 22:28:10 +02:00
bool MacabRecord : : contains ( const macabfield * _field ) const
2007-09-13 16:53:20 +00:00
{
2015-11-10 16:18:56 +01:00
if ( _field = = nullptr )
2014-09-15 22:28:10 +02:00
return false ;
2007-09-13 16:53:20 +00:00
else
return contains ( _field - > value ) ;
}
2014-02-22 21:20:15 +01:00
2014-09-15 22:28:10 +02:00
bool MacabRecord : : contains ( const CFTypeRef _value ) const
2007-09-13 16:53:20 +00:00
{
sal_Int32 i ;
for ( i = 0 ; i < size ; i + + )
{
2015-11-10 16:18:56 +01:00
if ( fields [ i ] ! = nullptr )
2007-09-13 16:53:20 +00:00
{
if ( CFEqual ( fields [ i ] - > value , _value ) )
{
2014-09-15 22:28:10 +02:00
return true ;
2007-09-13 16:53:20 +00:00
}
}
}
2014-09-15 22:28:10 +02:00
return false ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
sal_Int32 MacabRecord : : getSize ( ) const
{
return size ;
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
macabfield * MacabRecord : : copy ( const sal_Int32 i ) const
{
/* Note: copy(i) creates a new macabfield identical to that at
* location i , whereas get ( i ) returns a pointer to the macabfield
* at location i .
*/
if ( i < size )
{
macabfield * _copy = new macabfield ;
_copy - > type = fields [ i ] - > type ;
_copy - > value = fields [ i ] - > value ;
2008-03-05 15:38:50 +00:00
if ( _copy - > value )
CFRetain ( _copy - > value ) ;
2007-09-13 16:53:20 +00:00
return _copy ;
}
2015-11-10 16:18:56 +01:00
return nullptr ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
macabfield * MacabRecord : : get ( const sal_Int32 i ) const
{
/* Note: copy(i) creates a new macabfield identical to that at
* location i , whereas get ( i ) returns a pointer to the macabfield
* at location i .
*/
if ( i < size )
{
return fields [ i ] ;
}
2015-11-10 16:18:56 +01:00
return nullptr ;
2007-09-13 16:53:20 +00:00
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
void MacabRecord : : releaseFields ( )
{
/* This method is, at the moment, only used in MacabHeader.cxx, but
* the idea is simple : if you are not destroying this object but want
* to clear it of its macabfields , you should release each field ' s
* value .
*/
sal_Int32 i ;
for ( i = 0 ; i < size ; i + + )
CFRelease ( fields [ i ] - > value ) ;
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
sal_Int32 MacabRecord : : compareFields ( const macabfield * _field1 , const macabfield * _field2 )
{
/* When comparing records, if either field is NULL (and the other is
* not ) , that field is considered " greater than " the other , so that it
* shows up later in the list when fields are ordered .
*/
if ( _field1 = = _field2 )
return 0 ;
2015-11-10 16:18:56 +01:00
if ( _field1 = = nullptr )
2007-09-13 16:53:20 +00:00
return 1 ;
2015-11-10 16:18:56 +01:00
if ( _field2 = = nullptr )
2007-09-13 16:53:20 +00:00
return - 1 ;
/* If they aren't the same type, for now, return the one with
* the smaller type ID . . . I don ' t know of a better way to compare
* two different data types .
*/
if ( _field1 - > type ! = _field2 - > type )
return ( _field1 - > type - _field2 - > type ) ;
CFComparisonResult result ;
/* Carbon has a unique compare function for each data type: */
switch ( _field1 - > type )
{
case kABStringProperty :
result = CFStringCompare (
2015-03-29 21:28:27 +02:00
static_cast < CFStringRef > ( _field1 - > value ) ,
static_cast < CFStringRef > ( _field2 - > value ) ,
2011-01-03 13:56:33 +01:00
kCFCompareLocalized ) ; // Specifies that the comparison should take into account differences related to locale, such as the thousands separator character.
2007-09-13 16:53:20 +00:00
break ;
case kABDateProperty :
result = CFDateCompare (
2015-03-29 21:28:27 +02:00
static_cast < CFDateRef > ( _field1 - > value ) ,
static_cast < CFDateRef > ( _field2 - > value ) ,
2015-11-10 16:18:56 +01:00
nullptr ) ; // NULL = unused variable
2007-09-13 16:53:20 +00:00
break ;
case kABIntegerProperty :
case kABRealProperty :
result = CFNumberCompare (
2015-03-29 21:28:27 +02:00
static_cast < CFNumberRef > ( _field1 - > value ) ,
static_cast < CFNumberRef > ( _field2 - > value ) ,
2015-11-10 16:18:56 +01:00
nullptr ) ; // NULL = unused variable
2007-09-13 16:53:20 +00:00
break ;
default :
result = kCFCompareEqualTo ; // can't compare
}
return ( sal_Int32 ) result ;
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
/* Create a macabfield out of an OUString and type. Together with the
* method fieldToString ( ) ( below ) , it is possible to switch conveniently
* between an OUString and a macabfield ( for use when creating and handling
* SQL statement ) .
*/
2014-03-19 22:49:09 +01:00
macabfield * MacabRecord : : createMacabField ( const OUString & _newFieldString , const ABPropertyType _abType )
2007-09-13 16:53:20 +00:00
{
2015-11-10 16:18:56 +01:00
macabfield * newField = nullptr ;
2007-09-13 16:53:20 +00:00
switch ( _abType )
{
case kABStringProperty :
newField = new macabfield ;
newField - > value = OUStringToCFString ( _newFieldString ) ;
newField - > type = _abType ;
break ;
case kABDateProperty :
{
DateTime aDateTime = DBTypeConversion : : toDateTime ( _newFieldString ) ;
// bad format...
if ( aDateTime . Year = = 0 & & aDateTime . Month = = 0 & & aDateTime . Day = = 0 )
{
}
else
{
double nTime = DBTypeConversion : : toDouble ( aDateTime , DBTypeConversion : : getStandardDate ( ) ) ;
nTime - = kCFAbsoluteTimeIntervalSince1970 ;
newField = new macabfield ;
2015-11-10 16:18:56 +01:00
newField - > value = CFDateCreate ( nullptr , ( CFAbsoluteTime ) nTime ) ;
2007-09-13 16:53:20 +00:00
newField - > type = _abType ;
}
}
break ;
case kABIntegerProperty :
try
{
sal_Int64 nVal = _newFieldString . toInt64 ( ) ;
newField = new macabfield ;
2015-11-10 16:18:56 +01:00
newField - > value = CFNumberCreate ( nullptr , kCFNumberLongType , & nVal ) ;
2007-09-13 16:53:20 +00:00
newField - > type = _abType ;
}
// bad format...
catch ( . . . )
{
}
break ;
case kABRealProperty :
try
{
double nVal = _newFieldString . toDouble ( ) ;
newField = new macabfield ;
2015-11-10 16:18:56 +01:00
newField - > value = CFNumberCreate ( nullptr , kCFNumberDoubleType , & nVal ) ;
2007-09-13 16:53:20 +00:00
newField - > type = _abType ;
}
// bad format...
catch ( . . . )
{
}
break ;
default :
;
}
return newField ;
}
2014-02-22 21:20:15 +01:00
2007-09-13 16:53:20 +00:00
/* Create an OUString out of a macabfield. Together with the method
* createMacabField ( ) ( above ) , it is possible to switch conveniently
* between an OUString and a macabfield ( for use when creating and handling
* SQL statement ) .
*/
2013-04-07 12:06:47 +02:00
OUString MacabRecord : : fieldToString ( const macabfield * _aField )
2007-09-13 16:53:20 +00:00
{
2015-11-10 16:18:56 +01:00
if ( _aField = = nullptr )
2013-04-07 12:06:47 +02:00
return OUString ( ) ;
2007-09-13 16:53:20 +00:00
2013-04-07 12:06:47 +02:00
OUString fieldString ;
2007-09-13 16:53:20 +00:00
switch ( _aField - > type )
{
case kABStringProperty :
2015-03-29 21:28:27 +02:00
fieldString = CFStringToOUString ( static_cast < CFStringRef > ( _aField - > value ) ) ;
2007-09-13 16:53:20 +00:00
break ;
case kABDateProperty :
{
2015-03-29 21:28:27 +02:00
DateTime aTime = CFDateToDateTime ( static_cast < CFDateRef > ( _aField - > value ) ) ;
2007-09-13 16:53:20 +00:00
fieldString = DBTypeConversion : : toDateTimeString ( aTime ) ;
}
break ;
case kABIntegerProperty :
{
2015-03-29 21:28:27 +02:00
CFNumberType numberType = CFNumberGetType ( static_cast < CFNumberRef > ( _aField - > value ) ) ;
2007-09-13 16:53:20 +00:00
sal_Int64 nVal ;
// Should we check for the wrong type here, e.g., a float?
2015-03-29 21:28:27 +02:00
bool m_bSuccess = ! CFNumberGetValue ( static_cast < CFNumberRef > ( _aField - > value ) , numberType , & nVal ) ;
2014-09-15 22:28:10 +02:00
if ( m_bSuccess )
2013-08-21 16:48:17 +03:00
fieldString = OUString : : number ( nVal ) ;
2007-09-13 16:53:20 +00:00
}
break ;
case kABRealProperty :
{
2015-03-29 21:28:27 +02:00
CFNumberType numberType = CFNumberGetType ( static_cast < CFNumberRef > ( _aField - > value ) ) ;
2007-09-13 16:53:20 +00:00
double nVal ;
// Should we check for the wrong type here, e.g., an int?
2015-03-29 21:28:27 +02:00
bool m_bSuccess = ! CFNumberGetValue ( static_cast < CFNumberRef > ( _aField - > value ) , numberType , & nVal ) ;
2014-09-15 22:28:10 +02:00
if ( m_bSuccess )
2013-08-21 16:48:17 +03:00
fieldString = OUString : : number ( nVal ) ;
2007-09-13 16:53:20 +00:00
}
break ;
default :
;
}
return fieldString ;
}
2010-10-12 15:53:47 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */