1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
30 
31 #include "dx_config.hxx"
32 
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <comphelper/anytostring.hxx>
36 #include <basegfx/vector/b2ivector.hxx>
37 #include <cppuhelper/exc_hlp.hxx>
38 
39 using namespace com::sun::star;
40 
41 namespace dxcanvas
42 {
43     DXCanvasItem::DXCanvasItem() :
44         ConfigItem(
45             ::rtl::OUString(
46                 RTL_CONSTASCII_USTRINGPARAM( "Office.Canvas/DXCanvas" )),
47             CONFIG_MODE_IMMEDIATE_UPDATE ),
48         maValues(),
49         maMaxTextureSize(),
50         mbBlacklistCurrentDevice(false),
51         mbValuesDirty(false)
52     {
53         try
54         {
55             uno::Sequence< ::rtl::OUString > aName(1);
56             aName[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DeviceBlacklist" ));
57 
58             uno::Sequence< uno::Any > aProps( GetProperties( aName ));
59             uno::Sequence< sal_Int32 > aValues;
60 
61             if( aProps.getLength() > 0 &&
62                 (aProps[0] >>= aValues) )
63             {
64                 const sal_Int32* pValues = aValues.getConstArray();
65                 const sal_Int32 nNumEntries( aValues.getLength()*sizeof(sal_Int32)/sizeof(DeviceInfo) );
66                 for( sal_Int32 i=0; i<nNumEntries; ++i )
67                 {
68                     DeviceInfo aInfo;
69                     aInfo.nVendorId         = *pValues++;
70                     aInfo.nDeviceId         = *pValues++;
71                     aInfo.nDeviceSubSysId   = *pValues++;
72                     aInfo.nDeviceRevision   = *pValues++;
73                     aInfo.nDriverId         = *pValues++;
74                     aInfo.nDriverVersion    = *pValues++;
75                     aInfo.nDriverSubVersion = *pValues++;
76                     aInfo.nDriverBuildId    = *pValues++;
77                     maValues.insert(aInfo);
78                 }
79             }
80 
81             aName[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BlacklistCurrentDevice" ));
82             aProps = GetProperties( aName );
83             if( aProps.getLength() > 0 )
84                 aProps[0] >>= mbBlacklistCurrentDevice;
85 
86             aName[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MaxTextureSize" ));
87             aProps = GetProperties( aName );
88             if( aProps.getLength() > 0 )
89                 maMaxTextureSize.reset( aProps[0].get<sal_Int32>() );
90             else
91                 maMaxTextureSize.reset();
92         }
93         catch( uno::Exception& )
94         {
95             OSL_ENSURE( false,
96                         rtl::OUStringToOString(
97                             comphelper::anyToString( cppu::getCaughtException() ),
98                             RTL_TEXTENCODING_UTF8 ).getStr() );
99         }
100     }
101 
102     DXCanvasItem::~DXCanvasItem()
103     {
104         if( !mbValuesDirty )
105             return;
106 
107         try
108         {
109             uno::Sequence< ::rtl::OUString > aName(1);
110             aName[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DeviceBlacklist" ));
111 
112             uno::Sequence< sal_Int32 > aValues( sizeof(DeviceInfo)/sizeof(sal_Int32)*maValues.size() );
113 
114             sal_Int32* pValues = aValues.getArray();
115             ValueSet::const_iterator aIter( maValues.begin() );
116             const ValueSet::const_iterator aEnd( maValues.end() );
117             while( aIter != aEnd )
118             {
119                 const DeviceInfo& rInfo( *aIter );
120                 *pValues++ = rInfo.nVendorId;
121                 *pValues++ = rInfo.nDeviceId;
122                 *pValues++ = rInfo.nDeviceSubSysId;
123                 *pValues++ = rInfo.nDeviceRevision;
124                 *pValues++ = rInfo.nDriverId;
125                 *pValues++ = rInfo.nDriverVersion;
126                 *pValues++ = rInfo.nDriverSubVersion;
127                 *pValues++ = rInfo.nDriverBuildId;
128                 ++aIter;
129             }
130 
131             uno::Sequence< uno::Any > aValue(1);
132             aValue[0] <<= aValues;
133             PutProperties( aName, aValue );
134         }
135         catch( uno::Exception& )
136         {
137             OSL_ENSURE( false,
138                         rtl::OUStringToOString(
139                             comphelper::anyToString( cppu::getCaughtException() ),
140                             RTL_TEXTENCODING_UTF8 ).getStr() );
141         }
142     }
143 
144 	void DXCanvasItem::Notify( const com::sun::star::uno::Sequence<rtl::OUString>& ) {}
145 	void DXCanvasItem::Commit() {}
146 
147     bool DXCanvasItem::isDeviceUsable( const DeviceInfo& rDeviceInfo ) const
148     {
149         return maValues.find(rDeviceInfo) == maValues.end();
150     }
151 
152     bool DXCanvasItem::isBlacklistCurrentDevice() const
153     {
154         return mbBlacklistCurrentDevice;
155     }
156 
157     void DXCanvasItem::blacklistDevice( const DeviceInfo& rDeviceInfo )
158     {
159         mbValuesDirty = true;
160         maValues.insert(rDeviceInfo);
161     }
162 
163     void DXCanvasItem::adaptMaxTextureSize( basegfx::B2IVector& io_maxTextureSize ) const
164     {
165         if( maMaxTextureSize )
166         {
167             io_maxTextureSize.setX(
168                 std::min( *maMaxTextureSize,
169                           io_maxTextureSize.getX() ));
170             io_maxTextureSize.setY(
171                 std::min( *maMaxTextureSize,
172                           io_maxTextureSize.getY() ));
173         }
174     }
175 
176 }
177