LibreOffice Module sw (master)  1
swunohelper.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <com/sun/star/uno/Sequence.h>
21 #include <com/sun/star/uno/Exception.hpp>
22 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
23 #include <com/sun/star/ucb/XContentIdentifier.hpp>
24 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
25 #include <com/sun/star/ucb/TransferInfo.hpp>
26 #include <com/sun/star/ucb/NameClash.hpp>
27 #include <com/sun/star/sdbc/XResultSet.hpp>
28 #include <com/sun/star/sdbc/XRow.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <comphelper/extract.hxx>
32 #include <o3tl/any.hxx>
33 #include <tools/urlobj.hxx>
34 #include <tools/datetime.hxx>
35 #include <rtl/ustring.hxx>
36 #include <osl/diagnose.h>
38 #include <ucbhelper/content.hxx>
39 #include <swunohelper.hxx>
40 #include <svx/xfillit0.hxx>
41 #include <editeng/memberids.h>
42 #include <svl/itemset.hxx>
43 
44 using namespace com::sun::star;
45 
46 namespace SWUnoHelper
47 {
48 
49 sal_Int32 GetEnumAsInt32( const css::uno::Any& rVal )
50 {
51  sal_Int32 nReturn = 0;
52  if (! ::cppu::enum2int(nReturn,rVal) )
53  OSL_FAIL( "can't get EnumAsInt32" );
54  return nReturn;
55 }
56 
57 // methods for UCB actions
58 bool UCB_DeleteFile( const OUString& rURL )
59 {
60  bool bRemoved;
61  try
62  {
63  ucbhelper::Content aTempContent( rURL,
64  css::uno::Reference< css::ucb::XCommandEnvironment >(),
66  aTempContent.executeCommand("delete", css::uno::makeAny( true ) );
67  bRemoved = true;
68  }
69  catch( css::uno::Exception& )
70  {
71  bRemoved = false;
72  OSL_FAIL( "Exception from executeCommand( delete )" );
73  }
74  return bRemoved;
75 }
76 
77 bool UCB_MoveFile( const OUString& rURL, const OUString& rNewURL )
78 {
79  bool bCopyCompleted = true;
80  try
81  {
82  INetURLObject aURL( rNewURL );
83  const OUString sName( aURL.GetName() );
84  aURL.removeSegment();
85  const OUString sMainURL( aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE) );
86 
87  ucbhelper::Content aTempContent( sMainURL,
88  css::uno::Reference< css::ucb::XCommandEnvironment >(),
90 
91  css::ucb::TransferInfo aInfo;
92  aInfo.NameClash = css::ucb::NameClash::ERROR;
93  aInfo.NewTitle = sName;
94  aInfo.SourceURL = rURL;
95  aInfo.MoveData = true;
96  aTempContent.executeCommand( "transfer", uno::Any(aInfo) );
97  }
98  catch( css::uno::Exception& )
99  {
100  OSL_FAIL( "Exception from executeCommand( transfer )" );
101  bCopyCompleted = false;
102  }
103  return bCopyCompleted;
104 }
105 
106 bool UCB_IsCaseSensitiveFileName( const OUString& rURL )
107 {
108  bool bCaseSensitive;
109  try
110  {
111  INetURLObject aTempObj( rURL );
112  aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
113  css::uno::Reference< css::ucb::XContentIdentifier > xRef1 = new
115 
116  aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
117  css::uno::Reference< css::ucb::XContentIdentifier > xRef2 = new
119 
120  css::uno::Reference< css::ucb::XUniversalContentBroker > xUcb =
121  css::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());
122 
123  sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
124  bCaseSensitive = 0 != nCompare;
125  }
126  catch( css::uno::Exception& )
127  {
128  bCaseSensitive = false;
129  OSL_FAIL( "Exception from compareContentIds()" );
130  }
131  return bCaseSensitive;
132 }
133 
134 bool UCB_IsReadOnlyFileName( const OUString& rURL )
135 {
136  bool bIsReadOnly = false;
137  try
138  {
139  ucbhelper::Content aCnt( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
140  css::uno::Any aAny = aCnt.getPropertyValue("IsReadOnly");
141  if(aAny.hasValue())
142  bIsReadOnly = *o3tl::doAccess<bool>(aAny);
143  }
144  catch( css::uno::Exception& )
145  {
146  bIsReadOnly = false;
147  }
148  return bIsReadOnly;
149 }
150 
151 bool UCB_IsFile( const OUString& rURL )
152 {
153  bool bExists = false;
154  try
155  {
156  ::ucbhelper::Content aContent( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
157  bExists = aContent.isDocument();
158  }
159  catch (css::uno::Exception &)
160  {
161  }
162  return bExists;
163 }
164 
165 bool UCB_IsDirectory( const OUString& rURL )
166 {
167  bool bExists = false;
168  try
169  {
170  ::ucbhelper::Content aContent( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
171  bExists = aContent.isFolder();
172  }
173  catch (css::uno::Exception &)
174  {
175  }
176  return bExists;
177 }
178 
179  // get a list of files from the folder of the URL
180  // options: pExtension = 0 -> all, else this specific extension
181  // pDateTime != 0 -> returns also the modified date/time of
182  // the files in a std::vector<OUString> -->
183  // !! objects must be deleted from the caller!!
184 bool UCB_GetFileListOfFolder( const OUString& rURL,
185  std::vector<OUString>& rList,
186  const OUString* pExtension,
187  std::vector< ::DateTime >* pDateTimeList )
188 {
189  bool bOk = false;
190  try
191  {
192  ucbhelper::Content aCnt( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
193  css::uno::Reference< css::sdbc::XResultSet > xResultSet;
194 
195  const sal_Int32 nSeqSize = pDateTimeList ? 2 : 1;
196  css::uno::Sequence < OUString > aProps( nSeqSize );
197  OUString* pProps = aProps.getArray();
198  pProps[ 0 ] = "Title";
199  if( pDateTimeList )
200  pProps[ 1 ] = "DateModified";
201 
202  try
203  {
204  xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
205  }
206  catch( css::uno::Exception& )
207  {
208  OSL_FAIL( "create cursor failed!" );
209  }
210 
211  if( xResultSet.is() )
212  {
213  css::uno::Reference< css::sdbc::XRow > xRow( xResultSet, css::uno::UNO_QUERY );
214  const sal_Int32 nExtLen = pExtension ? pExtension->getLength() : 0;
215  try
216  {
217  if( xResultSet->first() )
218  {
219  do {
220  const OUString sTitle( xRow->getString( 1 ) );
221  if( !nExtLen ||
222  ( sTitle.getLength() > nExtLen &&
223  sTitle.endsWith( *pExtension )) )
224  {
225  rList.push_back( sTitle );
226 
227  if( pDateTimeList )
228  {
229  css::util::DateTime aStamp = xRow->getTimestamp(2);
230  ::DateTime aDateTime(
231  ::Date( aStamp.Day,
232  aStamp.Month,
233  aStamp.Year ),
234  ::tools::Time( aStamp.Hours,
235  aStamp.Minutes,
236  aStamp.Seconds,
237  aStamp.NanoSeconds ));
238  pDateTimeList->push_back( aDateTime );
239  }
240  }
241 
242  } while( xResultSet->next() );
243  }
244  bOk = true;
245  }
246  catch( css::uno::Exception& )
247  {
248  OSL_FAIL( "Exception caught!" );
249  }
250  }
251  }
252  catch( css::uno::Exception& )
253  {
254  OSL_FAIL( "Exception caught!" );
255  bOk = false;
256  }
257  return bOk;
258 }
259 
261  sal_uInt16 const nMID)
262 {
263  const XFillStyleItem* pXFillStyleItem(rSet.GetItem<XFillStyleItem>(XATTR_FILLSTYLE, false));
264 
265  if(!pXFillStyleItem)
266  {
267  return false;
268  }
269 
270  // here different FillStyles can be excluded for export; it will depend on the
271  // quality these fallbacks can reach. That again is done in getSvxBrushItemFromSourceSet,
272  // take a look there how the superset of DrawObject FillStyles is mapped to SvxBrushItem.
273  const drawing::FillStyle eFill = pXFillStyleItem->GetValue();
274  switch (eFill)
275  {
276  case drawing::FillStyle_NONE:
277  // claim that BackColor and BackTransparent are available so that
278  // fo:background="transparent" attribute is exported to override
279  // the parent style in case it is != NONE
280  switch (nMID)
281  {
282  case MID_BACK_COLOR:
284  case MID_GRAPHIC_TRANSPARENT: // this is *BackTransparent
285  return true;
286  default:
287  return false;
288  }
289  break;
290  case drawing::FillStyle_SOLID:
291  case drawing::FillStyle_GRADIENT: // gradient and hatch don't exist in
292  case drawing::FillStyle_HATCH: // SvxBrushItem so average color is emulated
293  switch (nMID)
294  {
295  case MID_BACK_COLOR:
296  case MID_GRAPHIC_TRANSPARENT: // this is *BackTransparent
297  // Gradient/Hatch always have emulated color
298  return (drawing::FillStyle_SOLID != eFill)
299  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLCOLOR)
300  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
301  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
303  // Gradient/Hatch always have emulated color
304  return (drawing::FillStyle_SOLID != eFill)
305  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLCOLOR);
307  return SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
308  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
309  }
310  break;
311  case drawing::FillStyle_BITMAP:
312  switch (nMID)
313  {
314  case MID_GRAPHIC:
315  return SfxItemState::SET == rSet.GetItemState(XATTR_FILLBITMAP);
317  return SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_STRETCH)
318  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_TILE)
319  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_POS);
322  return SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
323  || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
324  }
325  break;
326  default:
327  assert(false);
328  }
329 
330 
331  return false;
332 }
333 
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetBase(OUString const &rTheBase)
bool UCB_IsReadOnlyFileName(const OUString &rURL)
css::uno::Any getPropertyValue(const OUString &rPropertyName)
#define MID_GRAPHIC_TRANSPARENT
#define XATTR_FILLFLOATTRANSPARENCE
#define MID_BACK_COLOR
bool UCB_DeleteFile(const OUString &rURL)
Definition: swunohelper.cxx:58
#define MID_GRAPHIC_TRANSPARENCY
#define XATTR_FILLBMP_STRETCH
#define XATTR_FILLCOLOR
bool UCB_IsFile(const OUString &rURL)
OUString GetBase() const
bool UCB_MoveFile(const OUString &rURL, const OUString &rNewURL)
Definition: swunohelper.cxx:77
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
bool UCB_IsCaseSensitiveFileName(const OUString &rURL)
#define XATTR_FILLBMP_POS
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
OUString sName
bool needToMapFillItemsToSvxBrushItemTypes(const SfxItemSet &rSet, sal_uInt16 const nMID)
helper to check if fill style is set to color or bitmap and thus formerly used SvxBrushItem parts nee...
#define MID_BACK_COLOR_R_G_B
#define MID_BACK_COLOR_TRANSPARENCY
#define MID_GRAPHIC_POSITION
css::uno::Reference< css::sdbc::XResultSet > createCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
#define XATTR_FILLSTYLE
bool UCB_IsDirectory(const OUString &rURL)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
#define XATTR_FILLBMP_TILE
bool UCB_GetFileListOfFolder(const OUString &rURL, std::vector< OUString > &rList, const OUString *pExtension, std::vector< ::DateTime > *pDateTimeList)
Reference< XComponentContext > getProcessComponentContext()
#define XATTR_FILLBITMAP
css::uno::Any executeCommand(const OUString &rCommandName, const css::uno::Any &rCommandArgument)
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
#define MID_GRAPHIC
OUString GetName(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INCLUDE_DOCUMENTS_ONLY
sal_Int32 GetEnumAsInt32(const css::uno::Any &rVal)
Definition: swunohelper.cxx:49
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
#define XATTR_FILLTRANSPARENCE