LCOV - code coverage report
Current view: top level - sfx2/source/control - recentdocsview.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 57 164 34.8 %
Date: 2014-11-03 Functions: 10 20 50.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             : #include <sax/tools/converter.hxx>
      21             : #include <sfx2/recentdocsview.hxx>
      22             : #include <sfx2/templateabstractview.hxx>
      23             : #include <sfx2/app.hxx>
      24             : #include <sfx2/sfx.hrc>
      25             : #include <sfx2/sfxresid.hxx>
      26             : #include <unotools/historyoptions.hxx>
      27             : #include <vcl/builder.hxx>
      28             : #include <vcl/pngread.hxx>
      29             : #include <tools/urlobj.hxx>
      30             : #include <com/sun/star/util/URLTransformer.hpp>
      31             : #include <com/sun/star/frame/Desktop.hpp>
      32             : #include <com/sun/star/frame/XFrame.hpp>
      33             : #include <templateview.hrc>
      34             : 
      35             : using namespace ::com::sun::star;
      36             : using namespace com::sun::star::uno;
      37             : using namespace com::sun::star::lang;
      38             : using namespace com::sun::star::frame;
      39             : using namespace com::sun::star::beans;
      40             : 
      41           2 : RecentDocsView::RecentDocsView( vcl::Window* pParent )
      42             :     : ThumbnailView(pParent)
      43             :     , mnFileTypes(TYPE_NONE)
      44             :     , mnTextHeight(30)
      45             :     , mnItemPadding(5)
      46             :     , mnItemMaxTextLength(30)
      47             :     , mnLastMouseDownItem(THUMBNAILVIEW_ITEM_NOTFOUND)
      48             :     , maWelcomeImage(SfxResId(IMG_WELCOME))
      49             :     , maWelcomeLine1(SfxResId(STR_WELCOME_LINE1))
      50           2 :     , maWelcomeLine2(SfxResId(STR_WELCOME_LINE2))
      51             : {
      52           2 :     Rectangle aScreen = Application::GetScreenPosSizePixel(Application::GetDisplayBuiltInScreen());
      53           2 :     mnItemMaxSize = std::min(aScreen.GetWidth(),aScreen.GetHeight()) > 800 ? 256 : 192;
      54             : 
      55           2 :     SetStyle(GetStyle() | WB_VSCROLL);
      56           2 :     setItemMaxTextLength( mnItemMaxTextLength );
      57           2 :     setItemDimensions( mnItemMaxSize, mnItemMaxSize, mnTextHeight, mnItemPadding );
      58           2 : }
      59             : 
      60           2 : extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeRecentDocsView(vcl::Window *pParent, VclBuilder::stringmap &)
      61             : {
      62           2 :     return new RecentDocsView(pParent);
      63             : }
      64             : 
      65           4 : RecentDocsView::~RecentDocsView()
      66             : {
      67           4 : }
      68             : 
      69           0 : bool RecentDocsView::typeMatchesExtension(ApplicationType type, const OUString &rExt)
      70             : {
      71           0 :     bool bRet = false;
      72             : 
      73           0 :     if (rExt == "odt" || rExt == "doc" || rExt == "docx" ||
      74           0 :         rExt == "rtf" || rExt == "txt" || rExt == "odm" || rExt == "otm")
      75             :     {
      76           0 :         bRet = type & TYPE_WRITER;
      77             :     }
      78           0 :     else if (rExt == "ods" || rExt == "xls" || rExt == "xlsx")
      79             :     {
      80           0 :         bRet = type & TYPE_CALC;
      81             :     }
      82           0 :     else if (rExt == "odp" || rExt == "pps" || rExt == "ppt" ||
      83           0 :             rExt == "pptx")
      84             :     {
      85           0 :         bRet = type & TYPE_IMPRESS;
      86             :     }
      87           0 :     else if (rExt == "odg")
      88             :     {
      89           0 :         bRet = type & TYPE_DRAW;
      90             :     }
      91           0 :     else if (rExt == "odb")
      92             :     {
      93           0 :         bRet = type & TYPE_DATABASE;
      94             :     }
      95           0 :     else if (rExt == "odf")
      96             :     {
      97           0 :         bRet = type & TYPE_MATH;
      98             :     }
      99             :     else
     100             :     {
     101           0 :         bRet = type & TYPE_OTHER;
     102             :     }
     103             : 
     104           0 :     return bRet;
     105             : }
     106             : 
     107           0 : bool RecentDocsView::isAcceptedFile(const OUString &rURL) const
     108             : {
     109           0 :     INetURLObject aUrl(rURL);
     110           0 :     OUString aExt = aUrl.getExtension();
     111           0 :     return (mnFileTypes & TYPE_WRITER   && typeMatchesExtension(TYPE_WRITER,  aExt)) ||
     112           0 :            (mnFileTypes & TYPE_CALC     && typeMatchesExtension(TYPE_CALC,    aExt)) ||
     113           0 :            (mnFileTypes & TYPE_IMPRESS  && typeMatchesExtension(TYPE_IMPRESS, aExt)) ||
     114           0 :            (mnFileTypes & TYPE_DRAW     && typeMatchesExtension(TYPE_DRAW,    aExt)) ||
     115           0 :            (mnFileTypes & TYPE_DATABASE && typeMatchesExtension(TYPE_DATABASE,aExt)) ||
     116           0 :            (mnFileTypes & TYPE_MATH     && typeMatchesExtension(TYPE_MATH,    aExt)) ||
     117           0 :            (mnFileTypes & TYPE_OTHER    && typeMatchesExtension(TYPE_OTHER,   aExt));
     118             : }
     119             : 
     120           6 : void RecentDocsView::SetMessageFont()
     121             : {
     122           6 :     vcl::Font aFont(GetFont());
     123           6 :     aFont.SetHeight(aFont.GetHeight()*1.3);
     124           6 :     SetFont(aFont);
     125           6 : }
     126             : 
     127           0 : BitmapEx RecentDocsView::getDefaultThumbnail(const OUString &rURL)
     128             : {
     129           0 :     BitmapEx aImg;
     130           0 :     INetURLObject aUrl(rURL);
     131           0 :     OUString aExt = aUrl.getExtension();
     132             : 
     133           0 :     if ( typeMatchesExtension( TYPE_WRITER, aExt) )
     134           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_TEXT ) );
     135           0 :     else if ( typeMatchesExtension( TYPE_CALC, aExt) )
     136           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_SHEET ) );
     137           0 :     else if ( typeMatchesExtension( TYPE_IMPRESS, aExt) )
     138           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_PRESENTATION ) );
     139           0 :     else if ( typeMatchesExtension( TYPE_DRAW, aExt) )
     140           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_DRAWING ) );
     141           0 :     else if ( typeMatchesExtension( TYPE_DATABASE, aExt) )
     142           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_DATABASE ) );
     143           0 :     else if ( typeMatchesExtension( TYPE_MATH, aExt) )
     144           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_MATH ) );
     145             :     else
     146           0 :         aImg = BitmapEx ( SfxResId( SFX_FILE_THUMBNAIL_DEFAULT ) );
     147             : 
     148           0 :     return aImg;
     149             : }
     150             : 
     151           0 : void RecentDocsView::insertItem(const OUString &rURL, const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId)
     152             : {
     153           0 :     RecentDocsViewItem *pChild = new RecentDocsViewItem(*this, rURL, rTitle, rThumbnail, nId);
     154             : 
     155           0 :     AppendItem(pChild);
     156           0 : }
     157             : 
     158           2 : void RecentDocsView::Reload()
     159             : {
     160           2 :     Clear();
     161             : 
     162           2 :     Sequence< Sequence< PropertyValue > > aHistoryList = SvtHistoryOptions().GetList( ePICKLIST );
     163           2 :     for ( int i = 0; i < aHistoryList.getLength(); i++ )
     164             :     {
     165           0 :         Sequence< PropertyValue >& rRecentEntry = aHistoryList[i];
     166             : 
     167           0 :         OUString aURL;
     168           0 :         OUString aTitle;
     169           0 :         BitmapEx aThumbnail;
     170             : 
     171           0 :         for ( int j = 0; j < rRecentEntry.getLength(); j++ )
     172             :         {
     173           0 :             Any a = rRecentEntry[j].Value;
     174             : 
     175           0 :             if (rRecentEntry[j].Name == "URL")
     176           0 :                 a >>= aURL;
     177           0 :             else if (rRecentEntry[j].Name == "Title")
     178           0 :                 a >>= aTitle;
     179           0 :             else if (rRecentEntry[j].Name == "Thumbnail")
     180             :             {
     181           0 :                 OUString aBase64;
     182           0 :                 a >>= aBase64;
     183           0 :                 if (!aBase64.isEmpty())
     184             :                 {
     185           0 :                     Sequence<sal_Int8> aDecoded;
     186           0 :                     sax::Converter::decodeBase64(aDecoded, aBase64);
     187             : 
     188           0 :                     SvMemoryStream aStream(aDecoded.getArray(), aDecoded.getLength(), STREAM_READ);
     189           0 :                     vcl::PNGReader aReader(aStream);
     190           0 :                     aThumbnail = aReader.Read();
     191           0 :                 }
     192             :             }
     193           0 :         }
     194             : 
     195           0 :         if (isAcceptedFile(aURL))
     196             :         {
     197           0 :             insertItem(aURL, aTitle, aThumbnail, i+1);
     198             :         }
     199           0 :     }
     200             : 
     201           2 :     CalculateItemPositions();
     202           2 :     Invalidate();
     203             : 
     204             :     // Set preferred width
     205           2 :     if (mFilteredItemList.empty())
     206             :     {
     207           2 :         vcl::Font aOldFont(GetFont());
     208           2 :         SetMessageFont();
     209           2 :         set_width_request(std::max(GetTextWidth(maWelcomeLine1), GetTextWidth(maWelcomeLine2)));
     210           2 :         SetFont(aOldFont);
     211             :     }
     212             :     else
     213             :     {
     214           0 :         set_width_request(mnTextHeight + mnItemMaxSize + 2*mnItemPadding);
     215           2 :     }
     216           2 : }
     217             : 
     218           0 : void RecentDocsView::MouseButtonDown( const MouseEvent& rMEvt )
     219             : {
     220           0 :     if (rMEvt.IsLeft())
     221             :     {
     222           0 :         mnLastMouseDownItem = ImplGetItem(rMEvt.GetPosPixel());
     223             : 
     224             :         // ignore to avoid stuff done in ThumbnailView; we don't do selections etc.
     225           0 :         return;
     226             :     }
     227             : 
     228           0 :     ThumbnailView::MouseButtonDown(rMEvt);
     229             : }
     230             : 
     231           0 : void RecentDocsView::MouseButtonUp(const MouseEvent& rMEvt)
     232             : {
     233           0 :     if (rMEvt.IsLeft())
     234             :     {
     235           0 :         if( rMEvt.GetClicks() > 1 )
     236           0 :             return;
     237             : 
     238           0 :         size_t nPos = ImplGetItem(rMEvt.GetPosPixel());
     239           0 :         ThumbnailViewItem* pItem = ImplGetItem(nPos);
     240             : 
     241           0 :         if (pItem && nPos == mnLastMouseDownItem)
     242           0 :             pItem->MouseButtonUp(rMEvt);
     243             : 
     244           0 :         mnLastMouseDownItem = THUMBNAILVIEW_ITEM_NOTFOUND;
     245             : 
     246           0 :         if (pItem)
     247           0 :             return;
     248             :     }
     249           0 :     ThumbnailView::MouseButtonUp(rMEvt);
     250             : }
     251             : 
     252           0 : void RecentDocsView::OnItemDblClicked(ThumbnailViewItem *pItem)
     253             : {
     254           0 :     RecentDocsViewItem* pRecentItem = dynamic_cast< RecentDocsViewItem* >(pItem);
     255           0 :     if (pRecentItem)
     256           0 :         pRecentItem->OpenDocument();
     257           0 : }
     258             : 
     259           2 : void RecentDocsView::Paint( const Rectangle &aRect )
     260             : {
     261           2 :     if ( mItemList.size() == 0 )
     262             :     {
     263             :         // No recent files to be shown yet. Show a welcome screen.
     264           2 :         vcl::Font aOldFont(GetFont());
     265           2 :         SetMessageFont();
     266             : 
     267           2 :         long nTextHeight = GetTextHeight();
     268             : 
     269           2 :         long nTextWidth1 = GetTextWidth(maWelcomeLine1);
     270           2 :         long nTextWidth2 = GetTextWidth(maWelcomeLine2);
     271             : 
     272           2 :         const Size & rImgSize = maWelcomeImage.GetSizePixel();
     273           2 :         const Size & rSize = GetSizePixel();
     274             : 
     275           2 :         const int nX = (rSize.Width() - rImgSize.Width())/2;
     276           2 :         const int nY = (rSize.Height() - 3 * nTextHeight - rImgSize.Height())/2;
     277             : 
     278           2 :         Point aImgPoint(nX, nY);
     279           2 :         Point aStr1Point((rSize.Width() - nTextWidth1)/2, nY + rImgSize.Height() + 0.7 * nTextHeight);
     280           2 :         Point aStr2Point((rSize.Width() - nTextWidth2)/2, nY + rImgSize.Height() + 1.7 * nTextHeight);
     281             : 
     282           2 :         DrawImage(aImgPoint, rImgSize, maWelcomeImage, IMAGE_DRAW_SEMITRANSPARENT);
     283           2 :         DrawText(aStr1Point, maWelcomeLine1);
     284           2 :         DrawText(aStr2Point, maWelcomeLine2);
     285             : 
     286           2 :         SetFont(aOldFont);
     287             :     }
     288             :     else
     289           0 :         ThumbnailView::Paint(aRect);
     290           2 : }
     291             : 
     292           0 : void RecentDocsView::LoseFocus()
     293             : {
     294           0 :     deselectItems();
     295             : 
     296           0 :     ThumbnailView::LoseFocus();
     297           0 : }
     298             : 
     299             : 
     300           2 : void RecentDocsView::Clear()
     301             : {
     302           2 :     vcl::Font aOldFont(GetFont());
     303           2 :     SetMessageFont();
     304           2 :     set_width_request(std::max(GetTextWidth(maWelcomeLine1), GetTextWidth(maWelcomeLine2)));
     305           2 :     SetFont(aOldFont);
     306             : 
     307           2 :     ThumbnailView::Clear();
     308           2 : }
     309             : 
     310           0 : IMPL_STATIC_LINK_NOINSTANCE( RecentDocsView, ExecuteHdl_Impl, LoadRecentFile*, pLoadRecentFile )
     311             : {
     312             :     try
     313             :     {
     314             :         // Asynchronous execution as this can lead to our own destruction!
     315             :         // Framework can recycle our current frame and the layout manager disposes all user interface
     316             :         // elements if a component gets detached from its frame!
     317           0 :         pLoadRecentFile->xDispatch->dispatch( pLoadRecentFile->aTargetURL, pLoadRecentFile->aArgSeq );
     318             :     }
     319           0 :     catch ( const Exception& )
     320             :     {
     321             :     }
     322             : 
     323           0 :     delete pLoadRecentFile;
     324           0 :     return 0;
     325         951 : }
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10