LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sfx2/source/appl - sfxpicklist.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 88 206 42.7 %
Date: 2013-07-09 Functions: 11 19 57.9 %
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             : #include <com/sun/star/document/XDocumentProperties.hpp>
      22             : #include <unotools/historyoptions.hxx>
      23             : #include <unotools/useroptions.hxx>
      24             : #include <tools/urlobj.hxx>
      25             : #include <framework/menuconfiguration.hxx>
      26             : #include <svl/inethist.hxx>
      27             : #include <svl/stritem.hxx>
      28             : #include <svl/eitem.hxx>
      29             : #include <osl/file.hxx>
      30             : #include <unotools/localfilehelper.hxx>
      31             : #include <cppuhelper/implbase1.hxx>
      32             : 
      33             : // ----------------------------------------------------------------------------
      34             : 
      35             : #include <sfx2/app.hxx>
      36             : #include "sfxpicklist.hxx"
      37             : #include <sfx2/sfxuno.hxx>
      38             : #include "sfxtypes.hxx"
      39             : #include <sfx2/request.hxx>
      40             : #include <sfx2/sfxsids.hrc>
      41             : #include <sfx2/sfx.hrc>
      42             : #include <sfx2/event.hxx>
      43             : #include <sfx2/objsh.hxx>
      44             : #include <sfx2/bindings.hxx>
      45             : #include "referers.hxx"
      46             : #include <sfx2/docfile.hxx>
      47             : #include "objshimp.hxx"
      48             : #include <sfx2/docfilt.hxx>
      49             : 
      50             : #include <rtl/instance.hxx>
      51             : 
      52             : #include <algorithm>
      53             : 
      54             : // ----------------------------------------------------------------------------
      55             : 
      56             : using namespace ::com::sun::star::uno;
      57             : using namespace ::com::sun::star::beans;
      58             : using namespace ::com::sun::star::util;
      59             : 
      60             : // ----------------------------------------------------------------------------
      61             : 
      62             : class StringLength : public ::cppu::WeakImplHelper1< XStringWidth >
      63             : {
      64             :     public:
      65         133 :         StringLength() {}
      66         266 :         virtual ~StringLength() {}
      67             : 
      68             :         // XStringWidth
      69           0 :         sal_Int32 SAL_CALL queryStringWidth( const OUString& aString )
      70             :             throw (::com::sun::star::uno::RuntimeException)
      71             :         {
      72           0 :             return aString.getLength();
      73             :         }
      74             : };
      75             : 
      76           0 : void SfxPickList::CreatePicklistMenuTitle( Menu* pMenu, sal_uInt16 nItemId, const OUString& aURLString, sal_uInt32 nNo )
      77             : {
      78           0 :     OUStringBuffer aPickEntry;
      79             : 
      80           0 :     if ( nNo < 9 )
      81             :     {
      82           0 :         aPickEntry.append('~');
      83           0 :         aPickEntry.append(OUString::valueOf(static_cast<sal_Int32>(nNo + 1)));
      84             :     }
      85           0 :     else if ( nNo == 9 )
      86           0 :         aPickEntry.appendAscii(RTL_CONSTASCII_STRINGPARAM("1~0"));
      87             :     else
      88           0 :         aPickEntry.append(OUString::valueOf(static_cast<sal_Int32>(nNo + 1)));
      89           0 :     aPickEntry.appendAscii(RTL_CONSTASCII_STRINGPARAM(": "));
      90             : 
      91           0 :     INetURLObject   aURL( aURLString );
      92           0 :     OUString   aTipHelpText;
      93           0 :     OUString   aAccessibleName = aPickEntry.toString();
      94             : 
      95           0 :     if ( aURL.GetProtocol() == INET_PROT_FILE )
      96             :     {
      97             :         // Do handle file URL differently => convert it to a system
      98             :         // path and abbreviate it with a special function:
      99           0 :         OUString aFileSystemPath( aURL.getFSysPath( INetURLObject::FSYS_DETECT ) );
     100             : 
     101           0 :         OUString aSystemPath( aFileSystemPath );
     102           0 :         OUString aCompactedSystemPath;
     103             : 
     104           0 :         aTipHelpText = aSystemPath;
     105           0 :         aAccessibleName += aSystemPath;
     106           0 :         oslFileError nError = osl_abbreviateSystemPath( aSystemPath.pData, &aCompactedSystemPath.pData, 46, NULL );
     107           0 :         if ( !nError )
     108           0 :             aPickEntry.append( aCompactedSystemPath );
     109             :         else
     110           0 :             aPickEntry.append( aFileSystemPath );
     111             : 
     112           0 :         if ( aPickEntry.getLength() > 50 )
     113             :         {
     114           0 :             aPickEntry.setLength( 47 );
     115           0 :             aPickEntry.appendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
     116           0 :         }
     117             :     }
     118             :     else
     119             :     {
     120             :         // Use INetURLObject to abbreviate all other URLs
     121           0 :         OUString aShortURL;
     122           0 :         aShortURL = aURL.getAbbreviated( m_xStringLength, 46, INetURLObject::DECODE_UNAMBIGUOUS );
     123           0 :         aPickEntry.append(aShortURL);
     124           0 :         aTipHelpText = aURLString;
     125           0 :         aAccessibleName += aURLString;
     126             :     }
     127             : 
     128             :     // Set menu item text, tip help and accessible name
     129           0 :     pMenu->SetItemText( nItemId, aPickEntry.toString() );
     130           0 :     pMenu->SetTipHelpText( nItemId, aTipHelpText );
     131           0 :     pMenu->SetAccessibleName( nItemId, aAccessibleName );
     132           0 : }
     133             : 
     134             : namespace
     135             : {
     136             :     class thePickListMutex
     137             :         : public rtl::Static<osl::Mutex, thePickListMutex> {};
     138             : }
     139             : 
     140         133 : void SfxPickList::RemovePickListEntries()
     141             : {
     142         133 :     ::osl::MutexGuard aGuard( thePickListMutex::get() );
     143         133 :     for ( sal_uInt32 i = 0; i < m_aPicklistVector.size(); i++ )
     144           0 :         delete m_aPicklistVector[i];
     145         133 :     m_aPicklistVector.clear();
     146         133 : }
     147             : 
     148           0 : SfxPickList::PickListEntry* SfxPickList::GetPickListEntry( sal_uInt32 nIndex )
     149             : {
     150             :     OSL_ASSERT( m_aPicklistVector.size() > nIndex );
     151             : 
     152           0 :     if ( nIndex < m_aPicklistVector.size() )
     153           0 :         return m_aPicklistVector[ nIndex ];
     154             :     else
     155           0 :         return 0;
     156             : }
     157             : 
     158        1201 : void SfxPickList::AddDocumentToPickList( SfxObjectShell* pDocSh )
     159             : {
     160        1201 :     SfxMedium *pMed = pDocSh->GetMedium();
     161        1201 :     if( !pMed )
     162         628 :         return;
     163             : 
     164             :     // Unnamed Documents and embedded-Documents not in Picklist
     165        1784 :     if ( !pDocSh->HasName() ||
     166         583 :             SFX_CREATE_MODE_STANDARD != pDocSh->GetCreateMode() )
     167         618 :         return;
     168             : 
     169             :     // Help not in History
     170         583 :     INetURLObject aURL( pDocSh->IsDocShared() ? pDocSh->GetSharedFileURL() : OUString( pMed->GetOrigURL() ) );
     171         583 :     if ( aURL.GetProtocol() == INET_PROT_VND_SUN_STAR_HELP )
     172           0 :         return;
     173             : 
     174         583 :     if ( !pMed->IsUpdatePickList() )
     175          10 :         return;
     176             : 
     177             :     // add no document that forbids this (for example Message-Body)
     178         573 :     SFX_ITEMSET_ARG( pMed->GetItemSet(), pPicklistItem, SfxBoolItem, SID_PICKLIST, sal_False );
     179         573 :     if ( pPicklistItem && !pPicklistItem->GetValue() )
     180           0 :         return;
     181             : 
     182             :     // ignore hidden documents
     183         573 :     if ( !SfxViewFrame::GetFirst( pDocSh, sal_True ) )
     184           0 :         return;
     185             : 
     186        1146 :     OUString  aTitle = pDocSh->GetTitle(SFX_TITLE_PICKLIST);
     187        1146 :     OUString  aFilter;
     188         573 :     const SfxFilter* pFilter = pMed->GetOrigFilter();
     189         573 :     if ( pFilter )
     190         573 :         aFilter = pFilter->GetFilterName();
     191             : 
     192             :     // add to svtool history options
     193             :     SvtHistoryOptions().AppendItem( ePICKLIST,
     194             :             aURL.GetURLNoPass( INetURLObject::NO_DECODE ),
     195             :             aFilter,
     196             :             aTitle,
     197         573 :             OUString() );
     198             : 
     199         573 :     if ( aURL.GetProtocol() == INET_PROT_FILE )
     200        1146 :         Application::AddToRecentDocumentList( aURL.GetURLNoPass( INetURLObject::NO_DECODE ), (pFilter) ? pFilter->GetMimeType() : OUString() );
     201             : }
     202             : 
     203         133 : SfxPickList& SfxPickList::Get()
     204             : {
     205         133 :     static SfxPickList aUniqueInstance(SvtHistoryOptions().GetSize(ePICKLIST));
     206         133 :     return aUniqueInstance;
     207             : }
     208             : 
     209         133 : SfxPickList::SfxPickList( sal_uInt32 nAllowedMenuSize ) :
     210         133 :     m_nAllowedMenuSize( nAllowedMenuSize )
     211             : {
     212         133 :     m_xStringLength = new StringLength;
     213         133 :     m_nAllowedMenuSize = ::std::min( m_nAllowedMenuSize, (sal_uInt32)PICKLIST_MAXSIZE );
     214         133 :     StartListening( *SFX_APP() );
     215         133 : }
     216             : 
     217         266 : SfxPickList::~SfxPickList()
     218             : {
     219         133 :     RemovePickListEntries();
     220         133 : }
     221             : 
     222           0 : void SfxPickList::CreatePickListEntries()
     223             : {
     224           0 :     RemovePickListEntries();
     225             : 
     226             :     // Reading the pick list
     227           0 :     Sequence< Sequence< PropertyValue > > seqPicklist = SvtHistoryOptions().GetList( ePICKLIST );
     228             : 
     229           0 :     sal_uInt32 nCount   = seqPicklist.getLength();
     230           0 :     sal_uInt32 nEntries = ::std::min( m_nAllowedMenuSize, nCount );
     231             : 
     232           0 :     for( sal_uInt32 nItem=0; nItem < nEntries; ++nItem )
     233             :     {
     234           0 :         Sequence< PropertyValue > seqPropertySet = seqPicklist[ nItem ];
     235             : 
     236           0 :         INetURLObject   aURL;
     237           0 :         OUString sURL;
     238           0 :         OUString sFilter;
     239           0 :         OUString sTitle;
     240             : 
     241           0 :         sal_uInt32 nPropertyCount = seqPropertySet.getLength();
     242           0 :         for( sal_uInt32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
     243             :         {
     244           0 :             if( seqPropertySet[nProperty].Name == HISTORY_PROPERTYNAME_URL )
     245             :             {
     246           0 :                 seqPropertySet[nProperty].Value >>= sURL;
     247             :             }
     248           0 :             else if( seqPropertySet[nProperty].Name == HISTORY_PROPERTYNAME_FILTER )
     249             :             {
     250           0 :                 seqPropertySet[nProperty].Value >>= sFilter;
     251             :             }
     252           0 :             else if( seqPropertySet[nProperty].Name == HISTORY_PROPERTYNAME_TITLE )
     253             :             {
     254           0 :                 seqPropertySet[nProperty].Value >>= sTitle;
     255             :             }
     256             :         }
     257             : 
     258           0 :         aURL.SetSmartURL( sURL );
     259           0 :         aURL.SetPass( OUString() );
     260             : 
     261           0 :         PickListEntry *pPick = new PickListEntry( aURL.GetMainURL( INetURLObject::NO_DECODE ), sFilter, sTitle );
     262           0 :         m_aPicklistVector.push_back( pPick );
     263           0 :     }
     264           0 : }
     265             : 
     266           0 : void SfxPickList::CreateMenuEntries( Menu* pMenu )
     267             : {
     268           0 :     ::osl::MutexGuard aGuard( thePickListMutex::get() );
     269             : 
     270             :     static sal_Bool bPickListMenuInitializing = sal_False;
     271             : 
     272           0 :     if ( bPickListMenuInitializing ) // method is not reentrant!
     273           0 :         return;
     274             : 
     275           0 :     bPickListMenuInitializing = sal_True;
     276           0 :     CreatePickListEntries();
     277             : 
     278           0 :     for ( sal_uInt16 nId = START_ITEMID_PICKLIST; nId <= END_ITEMID_PICKLIST; ++nId )
     279           0 :         pMenu->RemoveItem( pMenu->GetItemPos( nId ) );
     280             : 
     281           0 :     if ( pMenu->GetItemType( pMenu->GetItemCount()-1 ) == MENUITEM_SEPARATOR )
     282           0 :         pMenu->RemoveItem( pMenu->GetItemCount()-1 );
     283             : 
     284           0 :     if ( m_aPicklistVector.size() > 0 &&
     285           0 :          pMenu->GetItemType( pMenu->GetItemCount()-1 )
     286           0 :             != MENUITEM_SEPARATOR && m_nAllowedMenuSize )
     287           0 :         pMenu->InsertSeparator();
     288             : 
     289           0 :     OUString aEmptyString;
     290           0 :     for ( sal_uInt32 i = 0; i < m_aPicklistVector.size(); i++ )
     291             :     {
     292           0 :         PickListEntry* pEntry = GetPickListEntry( i );
     293             : 
     294           0 :         pMenu->InsertItem( (sal_uInt16)(START_ITEMID_PICKLIST + i), aEmptyString );
     295           0 :         CreatePicklistMenuTitle( pMenu, (sal_uInt16)(START_ITEMID_PICKLIST + i), pEntry->aName, i );
     296             :     }
     297             : 
     298           0 :     bPickListMenuInitializing = sal_False;
     299             : }
     300             : 
     301           0 : void SfxPickList::ExecuteEntry( sal_uInt32 nIndex )
     302             : {
     303           0 :     ::osl::ClearableMutexGuard aGuard( thePickListMutex::get() );
     304             : 
     305           0 :     PickListEntry *pPick = SfxPickList::Get().GetPickListEntry( nIndex );
     306             : 
     307           0 :     if ( pPick )
     308             :     {
     309           0 :         SfxRequest aReq( SID_OPENDOC, SFX_CALLMODE_ASYNCHRON, SFX_APP()->GetPool() );
     310           0 :         aReq.AppendItem( SfxStringItem( SID_FILE_NAME, pPick->aName ));
     311           0 :         aReq.AppendItem( SfxStringItem( SID_REFERER, SFX_REFERER_USER ) );
     312           0 :         aReq.AppendItem( SfxStringItem( SID_TARGETNAME, "_default" ) );
     313           0 :         String aFilter( pPick->aFilter );
     314           0 :         aGuard.clear();
     315             : 
     316           0 :         sal_uInt16 nPos=aFilter.Search('|');
     317           0 :         if( nPos != STRING_NOTFOUND )
     318             :         {
     319           0 :             OUString aOptions(aFilter.Copy(nPos).GetBuffer()+1);
     320           0 :             aFilter.Erase( nPos );
     321           0 :             aReq.AppendItem( SfxStringItem(SID_FILE_FILTEROPTIONS, aOptions));
     322             :         }
     323             : 
     324           0 :         aReq.AppendItem(SfxStringItem( SID_FILTER_NAME, aFilter ));
     325           0 :         aReq.AppendItem( SfxBoolItem( SID_TEMPLATE, sal_False ) );
     326           0 :         SFX_APP()->ExecuteSlot( aReq );
     327           0 :     }
     328           0 : }
     329             : 
     330           0 : void SfxPickList::ExecuteMenuEntry( sal_uInt16 nId )
     331             : {
     332           0 :     ExecuteEntry( (sal_uInt32)( nId - START_ITEMID_PICKLIST ) );
     333           0 : }
     334             : 
     335       23197 : void SfxPickList::Notify( SfxBroadcaster&, const SfxHint& rHint )
     336             : {
     337       23197 :     if ( rHint.IsA( TYPE( SfxStringHint )))
     338             :     {
     339         456 :         SfxStringHint* pStringHint = (SfxStringHint*) &rHint;
     340             : 
     341         456 :         if ( pStringHint->GetId() == SID_OPENURL )
     342         456 :             INetURLHistory::GetOrCreate()->PutUrl( INetURLObject( pStringHint->GetObject() ));
     343             :     }
     344             : 
     345       23197 :     if ( rHint.IsA( TYPE( SfxEventHint )))
     346             :     {
     347       11383 :         SfxEventHint* pEventHint = PTR_CAST(SfxEventHint,&rHint);
     348             :         // only ObjectShell-related events with media interest
     349       11383 :         SfxObjectShell* pDocSh = pEventHint->GetObjShell();
     350       11383 :         if( !pDocSh )
     351           0 :             return;
     352             : 
     353       11383 :         switch ( pEventHint->GetEventId() )
     354             :         {
     355             :             case SFX_EVENT_CREATEDOC:
     356             :             {
     357         600 :                 sal_Bool bAllowModif = pDocSh->IsEnableSetModified();
     358         600 :                 if ( bAllowModif )
     359         600 :                     pDocSh->EnableSetModified( sal_False );
     360             : 
     361             :                 using namespace ::com::sun::star;
     362             :                 uno::Reference<document::XDocumentProperties> xDocProps(
     363         600 :                     pDocSh->getDocProperties());
     364         600 :                 if (xDocProps.is()) {
     365         600 :                     xDocProps->setAuthor( SvtUserOptions().GetFullName() );
     366         600 :                     ::DateTime now( ::DateTime::SYSTEM );
     367         600 :                     xDocProps->setCreationDate( util::DateTime(
     368        1800 :                         now.GetNanoSec(), now.GetSec(), now.GetMin(),
     369        1800 :                         now.GetHour(), now.GetDay(), now.GetMonth(),
     370        4200 :                         now.GetYear() ) );
     371             :                 }
     372             : 
     373         600 :                 if ( bAllowModif )
     374         600 :                     pDocSh->EnableSetModified( bAllowModif );
     375             :             }
     376         600 :             break;
     377             : 
     378             :             case SFX_EVENT_OPENDOC:
     379             :             {
     380          41 :                 SfxMedium *pMed = pDocSh->GetMedium();
     381          41 :                 if( !pMed )
     382           0 :                     return;
     383             : 
     384             :                 // Unnamed Documents and embedded-Documents not in History
     385          82 :                 if ( !pDocSh->HasName() ||
     386          41 :                      SFX_CREATE_MODE_STANDARD != pDocSh->GetCreateMode() )
     387           0 :                     return;
     388             : 
     389             :                 // Help not in History
     390          41 :                 INetURLObject aURL( pDocSh->IsDocShared() ? pDocSh->GetSharedFileURL() : OUString( pMed->GetOrigURL() ) );
     391          41 :                 if ( aURL.GetProtocol() == INET_PROT_VND_SUN_STAR_HELP )
     392           0 :                     return;
     393             : 
     394          82 :                 OUString  aTitle = pDocSh->GetTitle(SFX_TITLE_PICKLIST);
     395          82 :                 OUString  aFilter;
     396          41 :                 const SfxFilter* pFilter = pMed->GetOrigFilter();
     397          41 :                 if ( pFilter )
     398          41 :                     aFilter = pFilter->GetFilterName();
     399             : 
     400             :                 // add to svtool history options
     401             :                 SvtHistoryOptions().AppendItem( eHISTORY,
     402             :                         aURL.GetURLNoPass( INetURLObject::NO_DECODE ),
     403             :                         aFilter,
     404             :                         aTitle,
     405          82 :                         OUString() );
     406             :             }
     407          41 :             break;
     408             : 
     409             :             case SFX_EVENT_SAVEDOCDONE:
     410             :             case SFX_EVENT_SAVEASDOCDONE:
     411             :             case SFX_EVENT_SAVETODOCDONE:
     412             :             case SFX_EVENT_CLOSEDOC:
     413             :             {
     414        1197 :                 AddDocumentToPickList(pDocSh);
     415             :             }
     416        1197 :             break;
     417             : 
     418             :             case SFX_EVENT_SAVEASDOC:
     419             :             {
     420          10 :                 SfxMedium *pMedium = pDocSh->GetMedium();
     421          10 :                 if (!pMedium)
     422           0 :                     return;
     423             : 
     424             :                 // We're starting a "Save As". Add the current document (if it's
     425             :                 // not a "new" document) to the "Recent Documents" list before we
     426             :                 // switch to the new path.
     427             :                 // If the current document is new, path will be empty.
     428          10 :                 OUString path = pMedium->GetOrigURL();
     429          10 :                 if (!path.isEmpty())
     430             :                 {
     431           4 :                     AddDocumentToPickList(pDocSh);
     432          10 :                 }
     433             :             }
     434          10 :             break;
     435             :         }
     436             :     }
     437         408 : }
     438             : 
     439             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10