LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/ucb/source/ucp/ftp - ftpcontentprovider.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 55 74 74.3 %
Date: 2013-07-09 Functions: 17 21 81.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             : 
      21             : /**************************************************************************
      22             :                                 TODO
      23             :  **************************************************************************
      24             : 
      25             :  *************************************************************************/
      26             : 
      27             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      28             : #include <comphelper/processfactory.hxx>
      29             : #include <osl/socket.hxx>
      30             : #include "ftpcontentprovider.hxx"
      31             : #include "ftpcontent.hxx"
      32             : #include "ftploaderthread.hxx"
      33             : 
      34             : 
      35             : using namespace ftp;
      36             : using namespace com::sun::star::lang;
      37             : using namespace com::sun::star::container;
      38             : using namespace com::sun::star::uno;
      39             : using namespace com::sun::star::ucb;
      40             : using namespace com::sun::star::beans;
      41             : 
      42             : 
      43             : 
      44             : //=========================================================================
      45             : //=========================================================================
      46             : //
      47             : // ContentProvider Implementation.
      48             : //
      49             : //=========================================================================
      50             : //=========================================================================
      51             : 
      52           2 : FTPContentProvider::FTPContentProvider(
      53             :     const Reference< XComponentContext >& rxContext)
      54             : : ::ucbhelper::ContentProviderImplHelper(rxContext),
      55             :   m_ftpLoaderThread(0),
      56           2 :   m_pProxyDecider(0)
      57             : {
      58           2 : }
      59             : 
      60             : //=========================================================================
      61             : // virtual
      62           6 : FTPContentProvider::~FTPContentProvider()
      63             : {
      64           2 :     delete m_ftpLoaderThread;
      65           2 :     delete m_pProxyDecider;
      66           4 : }
      67             : 
      68             : //=========================================================================
      69             : //
      70             : // XInterface methods.
      71             : //
      72             : //=========================================================================
      73             : 
      74         115 : XINTERFACE_IMPL_3(FTPContentProvider,
      75             :                   XTypeProvider,
      76             :                   XServiceInfo,
      77             :                   XContentProvider)
      78             : 
      79             : //=========================================================================
      80             : //
      81             : // XTypeProvider methods.
      82             : //
      83             : //=========================================================================
      84             : 
      85           1 : XTYPEPROVIDER_IMPL_3(FTPContentProvider,
      86             :                      XTypeProvider,
      87             :                      XServiceInfo,
      88             :                      XContentProvider)
      89             : 
      90             : //=========================================================================
      91             : //
      92             : // XServiceInfo methods.
      93             : //
      94             : //=========================================================================
      95             : 
      96          10 : XSERVICEINFO_IMPL_1_CTX(
      97             :     FTPContentProvider,
      98             :     OUString("com.sun.star.comp.FTPContentProvider"),
      99             :     OUString(FTP_CONTENT_PROVIDER_SERVICE_NAME));
     100             : 
     101             : //=========================================================================
     102             : //
     103             : // Service factory implementation.
     104             : //
     105             : //=========================================================================
     106             : 
     107           2 : ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
     108             : 
     109             : 
     110             : //=========================================================================
     111             : //
     112             : // XContentProvider methods.
     113             : //
     114             : //=========================================================================
     115             : 
     116             : // virtual
     117             : Reference<XContent> SAL_CALL
     118           2 : FTPContentProvider::queryContent(
     119             :     const Reference< XContentIdentifier >& xCanonicId
     120             : )
     121             :     throw(
     122             :         IllegalIdentifierException,
     123             :         RuntimeException
     124             :     )
     125             : {
     126             :     // Check, if a content with given id already exists...
     127           2 :     Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
     128           2 :     if(xContent.is())
     129           0 :         return xContent;
     130             : 
     131             :     // A new content has to be returned:
     132             :     {
     133             :         // Initialize
     134           2 :         osl::MutexGuard aGuard( m_aMutex );
     135           2 :         if(!m_ftpLoaderThread || !m_pProxyDecider)
     136             :         {
     137             :             try {
     138           2 :                 init();
     139           0 :             } catch( ... ) {
     140           0 :                 throw RuntimeException();
     141             :             }
     142             : 
     143           2 :             if(!m_ftpLoaderThread || !m_pProxyDecider)
     144           0 :                 throw RuntimeException();
     145           2 :         }
     146             :     }
     147             : 
     148             :     try {
     149           2 :         FTPURL aURL(xCanonicId->getContentIdentifier(),
     150           4 :                     this);
     151             : 
     152           4 :         if(!m_pProxyDecider->shouldUseProxy(
     153             :             OUString("ftp"),
     154             :             aURL.host(),
     155           4 :             aURL.port().toInt32()))
     156             :         {
     157           2 :             xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
     158           2 :             registerNewContent(xContent);
     159             :         }
     160             :         else {
     161             :             Reference<XContentProvider>
     162           0 :                 xProvider(getHttpProvider());
     163           0 :             if(xProvider.is())
     164           0 :                 return xProvider->queryContent(xCanonicId);
     165             :             else
     166           0 :                 throw RuntimeException();
     167           2 :         }
     168           0 :     } catch(const malformed_exception&) {
     169           0 :         throw IllegalIdentifierException();
     170             :     }
     171             : 
     172             :     // may throw IllegalIdentifierException
     173           2 :     return xContent;
     174             : }
     175             : 
     176             : 
     177             : 
     178             : 
     179           2 : void FTPContentProvider::init() {
     180           2 :     m_ftpLoaderThread = new FTPLoaderThread();
     181           2 :     m_pProxyDecider = new ucbhelper::InternetProxyDecider( m_xContext );
     182           2 : }
     183             : 
     184             : 
     185             : 
     186           1 : CURL* FTPContentProvider::handle() {
     187             :     // Cannot be zero if called from here;
     188           1 :     return m_ftpLoaderThread->handle();
     189             : }
     190             : 
     191             : 
     192           1 : bool FTPContentProvider::forHost(
     193             :     const OUString& host,
     194             :     const OUString& port,
     195             :     const OUString& username,
     196             :     OUString& password,
     197             :     OUString& account)
     198             : {
     199           1 :     osl::MutexGuard aGuard(m_aMutex);
     200           1 :     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
     201           3 :         if(host == m_ServerInfo[i].host &&
     202           2 :            port == m_ServerInfo[i].port &&
     203           1 :            username == m_ServerInfo[i].username )
     204             :         {
     205           1 :             password = m_ServerInfo[i].password;
     206           1 :             account = m_ServerInfo[i].account;
     207           1 :             return true;
     208             :         }
     209             : 
     210           0 :     return false;
     211             : }
     212             : 
     213             : 
     214           1 : bool  FTPContentProvider::setHost(
     215             :     const OUString& host,
     216             :     const OUString& port,
     217             :     const OUString& username,
     218             :     const OUString& password,
     219             :     const OUString& account)
     220             : {
     221           1 :     ServerInfo inf;
     222           1 :     inf.host = host;
     223           1 :     inf.port = port;
     224           1 :     inf.username = username;
     225           1 :     inf.password = password;
     226           1 :     inf.account = account;
     227             : 
     228           1 :     bool present(false);
     229           2 :     osl::MutexGuard aGuard(m_aMutex);
     230           1 :     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
     231           0 :         if(host == m_ServerInfo[i].host &&
     232           0 :            port == m_ServerInfo[i].port &&
     233           0 :            username == m_ServerInfo[i].username)
     234             :         {
     235           0 :             present = true;
     236           0 :             m_ServerInfo[i].password = password;
     237           0 :             m_ServerInfo[i].account = account;
     238             :         }
     239             : 
     240           1 :     if(!present)
     241           1 :         m_ServerInfo.push_back(inf);
     242             : 
     243           2 :     return !present;
     244             : }
     245             : 
     246             : 
     247             : 
     248             : Reference<XContentProvider>
     249           0 : FTPContentProvider::getHttpProvider()
     250             :     throw(RuntimeException)
     251             : {
     252             :     // used for access to ftp-proxy
     253           0 :     return UniversalContentBroker::create( m_xContext )->queryContentProvider("http:");
     254             : }
     255             : 
     256             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10