Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / source / core / tool / printopt.cxx
blobe9b3e1516147658c9652d876a270cd1c2cf03ff7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/Any.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
22 #include <osl/diagnose.h>
24 #include <printopt.hxx>
25 #include <sc.hrc>
27 using namespace utl;
28 using namespace com::sun::star::uno;
31 ScPrintOptions::ScPrintOptions()
33 SetDefaults();
36 void ScPrintOptions::SetDefaults()
38 bSkipEmpty = true;
39 bAllSheets = false;
40 bForceBreaks = false;
43 bool ScPrintOptions::operator==( const ScPrintOptions& rOpt ) const
45 return bSkipEmpty == rOpt.bSkipEmpty
46 && bAllSheets == rOpt.bAllSheets
47 && bForceBreaks == rOpt.bForceBreaks;
50 ScTpPrintItem::ScTpPrintItem( const ScPrintOptions& rOpt ) :
51 SfxPoolItem ( SID_SCPRINTOPTIONS ),
52 theOptions ( rOpt )
56 ScTpPrintItem::~ScTpPrintItem()
60 bool ScTpPrintItem::operator==( const SfxPoolItem& rItem ) const
62 assert(SfxPoolItem::operator==(rItem));
64 const ScTpPrintItem& rPItem = static_cast<const ScTpPrintItem&>(rItem);
65 return ( theOptions == rPItem.theOptions );
68 ScTpPrintItem* ScTpPrintItem::Clone( SfxItemPool * ) const
70 return new ScTpPrintItem( *this );
73 constexpr OUStringLiteral CFGPATH_PRINT = u"Office.Calc/Print";
75 #define SCPRINTOPT_EMPTYPAGES 0
76 #define SCPRINTOPT_ALLSHEETS 1
77 #define SCPRINTOPT_FORCEBREAKS 2
79 Sequence<OUString> ScPrintCfg::GetPropertyNames()
81 return {"Page/EmptyPages", // SCPRINTOPT_EMPTYPAGES
82 "Other/AllSheets", // SCPRINTOPT_ALLSHEETS
83 "Page/ForceBreaks"}; // SCPRINTOPT_FORCEBREAKS;
86 ScPrintCfg::ScPrintCfg() :
87 ConfigItem( CFGPATH_PRINT )
89 Sequence<OUString> aNames = GetPropertyNames();
90 EnableNotification(aNames);
91 ReadCfg();
94 void ScPrintCfg::ReadCfg()
96 const Sequence<OUString> aNames = GetPropertyNames();
97 const Sequence<Any> aValues = GetProperties(aNames);
98 OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
99 if(aValues.getLength() != aNames.getLength())
100 return;
102 if (bool bVal; aValues[SCPRINTOPT_EMPTYPAGES] >>= bVal)
103 SetSkipEmpty(!bVal); // reversed
104 if (bool bVal; aValues[SCPRINTOPT_ALLSHEETS] >>= bVal)
105 SetAllSheets(bVal);
106 if (bool bVal; aValues[SCPRINTOPT_FORCEBREAKS] >>= bVal)
107 SetForceBreaks(bVal);
110 void ScPrintCfg::ImplCommit()
112 Sequence<OUString> aNames = GetPropertyNames();
113 Sequence<Any> aValues(aNames.getLength());
114 Any* pValues = aValues.getArray();
116 pValues[SCPRINTOPT_EMPTYPAGES] <<= !GetSkipEmpty(); // reversed
117 pValues[SCPRINTOPT_ALLSHEETS] <<= GetAllSheets();
118 pValues[SCPRINTOPT_FORCEBREAKS] <<= GetForceBreaks();
119 PutProperties(aNames, aValues);
122 void ScPrintCfg::SetOptions( const ScPrintOptions& rNew )
124 *static_cast<ScPrintOptions*>(this) = rNew;
125 SetModified();
126 Commit();
129 void ScPrintCfg::Notify( const css::uno::Sequence< OUString >& ) { ReadCfg(); }
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */