LCOV - code coverage report
Current view: top level - scripting/source/inc/util - MiscUtils.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 22 35 62.9 %
Date: 2015-06-13 12:38:46 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #ifndef INCLUDED_SCRIPTING_SOURCE_INC_UTIL_MISCUTILS_HXX
      21             : #define INCLUDED_SCRIPTING_SOURCE_INC_UTIL_MISCUTILS_HXX
      22             : 
      23             : #include <rtl/ustring.hxx>
      24             : #include <osl/diagnose.h>
      25             : #include <tools/urlobj.hxx>
      26             : 
      27             : #include <ucbhelper/content.hxx>
      28             : #include <com/sun/star/uno/XComponentContext.hpp>
      29             : #include <com/sun/star/frame/XModel.hpp>
      30             : #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
      31             : #include <com/sun/star/beans/XPropertySet.hpp>
      32             : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      33             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      34             : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
      35             : #include <com/sun/star/ucb/XContentAccess.hpp>
      36             : #include <com/sun/star/sdbc/XResultSet.hpp>
      37             : #include <com/sun/star/sdbc/XRow.hpp>
      38             : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
      39             : #include <comphelper/processfactory.hxx>
      40             : 
      41             : namespace sf_misc
      42             : {
      43             : 
      44             : class MiscUtils
      45             : {
      46             : public:
      47           0 :     static css::uno::Sequence< OUString > allOpenTDocUrls( const  css::uno::Reference< css::uno::XComponentContext >& xCtx)
      48             : {
      49           0 :     css::uno::Sequence< OUString > result;
      50             :     try
      51             :     {
      52           0 :         if ( !xCtx.is() )
      53             :         {
      54           0 :             return result;
      55             :         }
      56           0 :         css::uno::Reference < css::ucb::XSimpleFileAccess3 > xSFA( css::ucb::SimpleFileAccess::create(xCtx) );
      57           0 :         result = xSFA->getFolderContents( "vnd.sun.star.tdoc:/", true );
      58             :     }
      59           0 :     catch ( css::uno::Exception& )
      60             :     {
      61             :     }
      62           0 :     return result;
      63             : }
      64             : 
      65         140 :     static OUString xModelToTdocUrl( const css::uno::Reference< css::frame::XModel >& xModel,
      66             :                                             const css::uno::Reference< css::uno::XComponentContext >& xContext )
      67             : {
      68             :     css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
      69         140 :         xContext->getServiceManager() );
      70             :     css::uno::Reference<
      71         280 :             css::frame::XTransientDocumentsDocumentContentFactory > xDocFac;
      72             :     try
      73             :     {
      74         280 :         xDocFac =
      75             :             css::uno::Reference<
      76             :                 css::frame::XTransientDocumentsDocumentContentFactory >(
      77         140 :                     xMCF->createInstanceWithContext(
      78             :                         OUString(
      79             :                                 "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ),
      80         140 :                         xContext ),
      81         140 :                 css::uno::UNO_QUERY );
      82             :     }
      83           0 :     catch ( css::uno::Exception const & )
      84             :     {
      85             :         // handled below
      86             :     }
      87             : 
      88         140 :     if ( xDocFac.is() )
      89             :     {
      90             :         try
      91             :         {
      92             :             css::uno::Reference< css::ucb::XContent > xContent(
      93         140 :                 xDocFac->createDocumentContent( xModel ) );
      94         140 :             return xContent->getIdentifier()->getContentIdentifier();
      95             :         }
      96           0 :         catch ( css::lang::IllegalArgumentException const & )
      97             :         {
      98             :             OSL_FAIL( "Invalid document model!" );
      99             :         }
     100             :     }
     101             : 
     102             :     OSL_FAIL( "Unable to obtain URL for document model!" );
     103         140 :     return OUString();
     104             : }
     105         171 :     static css::uno::Reference< css::frame::XModel > tDocUrlToModel( const OUString& url )
     106             : {
     107         171 :     css::uno::Any result;
     108             : 
     109             :     try
     110             :     {
     111         171 :         ::ucbhelper::Content root( url, NULL, comphelper::getProcessComponentContext() );
     112         342 :         OUString propName =  "DocumentModel";
     113         342 :         result = getUCBProperty( root, propName );
     114             :     }
     115           0 :     catch ( css::ucb::ContentCreationException& )
     116             :     {
     117             :         // carry on, empty value will be returned
     118             :     }
     119           0 :     catch ( css::uno::RuntimeException& )
     120             :     {
     121             :         // carry on, empty value will be returned
     122             :     }
     123             : 
     124             :     css::uno::Reference< css::frame::XModel > xModel(
     125         171 :         result, css::uno::UNO_QUERY );
     126             : 
     127         171 :     return xModel;
     128             : }
     129             : 
     130             : 
     131         171 :     static css::uno::Any getUCBProperty( ::ucbhelper::Content& content, OUString& prop )
     132             : {
     133         171 :     css::uno::Any result;
     134             :     try
     135             :     {
     136         171 :         result = content.getPropertyValue( prop );
     137             :     }
     138           0 :     catch ( css::uno::Exception& )
     139             :     {
     140             :     }
     141         171 :     return result;
     142             : }
     143             : 
     144             : private:
     145             : static OUString parseLocationName( const OUString& location )
     146             : {
     147             :     // strip out the last leaf of location name
     148             :     // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
     149             :     OUString temp = location;
     150             :     INetURLObject aURLObj( temp );
     151             :     if ( !aURLObj.HasError() )
     152             :         temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     153             :     return temp;
     154             : }
     155             : 
     156             : };
     157             : } // namespace sf_misc
     158             : #endif
     159             : 
     160             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11