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