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

Generated by: LCOV version 1.10