Kaydet (Commit) 767af37a authored tarafından Krisztian Pinter's avatar Krisztian Pinter Kaydeden (comit) Petr Mladek

Remove duplicate code for reading thumbnails

Change-Id: I51de8d07a4dc51ba03d24a1a5805df051b6e09b4
Reviewed-on: https://gerrit.libreoffice.org/4779Reviewed-by: 's avatarPetr Mladek <pmladek@suse.cz>
Tested-by: 's avatarPetr Mladek <pmladek@suse.cz>
üst 3a5cc4e3
......@@ -15,9 +15,16 @@
#include <vector>
#include <boost/function.hpp>
#include <comphelper/processfactory.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <sfx2/thumbnailviewitem.hxx>
#include <vcl/ctrl.hxx>
#include <vcl/timer.hxx>
#include <vcl/pngread.hxx>
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/embed/StorageFactory.hpp>
class BitmapEx;
class MouseEvent;
......@@ -242,6 +249,8 @@ public:
virtual bool renameItem(ThumbnailViewItem* pItem, OUString sNewTitle);
static BitmapEx readThumbnail(const OUString &msURL);
protected:
virtual void KeyInput( const KeyEvent& rKEvt );
......
......@@ -25,6 +25,7 @@
#include <comphelper/processfactory.hxx>
#include <sfx2/app.hxx>
#include <sfx2/sfxsids.hrc>
#include <sfx2/thumbnailview.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <vcl/image.hxx>
#include <vcl/pngread.hxx>
......@@ -107,102 +108,7 @@ Image TemplatePreviewProvider::operator() (
(void)pPage;
(void)rRenderer;
// Load the thumbnail from a template document.
uno::Reference<io::XInputStream> xIStream;
uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
try
{
uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext);
uno::Sequence<uno::Any> aArgs (2);
aArgs[0] <<= msURL;
aArgs[1] <<= embed::ElementModes::READ;
uno::Reference<embed::XStorage> xDocStorage (
xStorageFactory->createInstanceWithArguments(aArgs),
uno::UNO_QUERY);
try
{
if (xDocStorage.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement(
"Thumbnails",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
try
{
// An (older) implementation had a bug - The storage
// name was "Thumbnail" instead of "Thumbnails". The
// old name is still used as fallback but this code can
// be removed soon.
if ( ! xIStream.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement( "Thumbnail",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access tuhmbnail of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
// Extract the image from the stream.
BitmapEx aThumbnail;
if (xIStream.is())
{
::std::auto_ptr<SvStream> pStream (
::utl::UcbStreamHelper::CreateStream (xIStream));
::vcl::PNGReader aReader (*pStream);
aThumbnail = aReader.Read ();
}
// Note that the preview is returned without scaling it to the desired
// width. This gives the caller the chance to take advantage of a
// possibly larger resolution then was asked for.
return aThumbnail;
return ThumbnailView::readThumbnail(msURL);
}
......
......@@ -26,6 +26,7 @@
#include <comphelper/processfactory.hxx>
#include <sfx2/app.hxx>
#include <sfx2/sfxsids.hrc>
#include <sfx2/thumbnailview.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <vcl/image.hxx>
#include <vcl/pngread.hxx>
......@@ -110,102 +111,7 @@ Image TemplatePreviewProvider::operator() (
(void)pPage;
(void)rRenderer;
// Load the thumbnail from a template document.
uno::Reference<io::XInputStream> xIStream;
uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
try
{
uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext);
uno::Sequence<uno::Any> aArgs (2);
aArgs[0] <<= msURL;
aArgs[1] <<= embed::ElementModes::READ;
uno::Reference<embed::XStorage> xDocStorage (
xStorageFactory->createInstanceWithArguments(aArgs),
uno::UNO_QUERY);
try
{
if (xDocStorage.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement(
"Thumbnails",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
try
{
// An (older) implementation had a bug - The storage
// name was "Thumbnail" instead of "Thumbnails". The
// old name is still used as fallback but this code can
// be removed soon.
if ( ! xIStream.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement( "Thumbnail",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access tuhmbnail of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
// Extract the image from the stream.
BitmapEx aThumbnail;
if (xIStream.is())
{
::std::auto_ptr<SvStream> pStream (
::utl::UcbStreamHelper::CreateStream (xIStream));
::vcl::PNGReader aReader (*pStream);
aThumbnail = aReader.Read ();
}
// Note that the preview is returned without scaling it to the desired
// width. This gives the caller the chance to take advantage of a
// possibly larger resolution then was asked for.
return aThumbnail;
return ThumbnailView::readThumbnail(msURL);
}
......
......@@ -285,103 +285,7 @@ BitmapEx TemplateAbstractView::getDefaultThumbnail( const OUString& rPath )
BitmapEx TemplateAbstractView::fetchThumbnail (const OUString &msURL, long width, long height)
{
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
// Load the thumbnail from a template document.
uno::Reference<io::XInputStream> xIStream;
uno::Reference< uno::XComponentContext > xContext (comphelper::getProcessComponentContext());
try
{
uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create( xContext );
uno::Sequence<uno::Any> aArgs (2);
aArgs[0] <<= msURL;
aArgs[1] <<= embed::ElementModes::READ;
uno::Reference<embed::XStorage> xDocStorage (
xStorageFactory->createInstanceWithArguments(aArgs),
uno::UNO_QUERY);
try
{
if (xDocStorage.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement(
"Thumbnails",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
try
{
// An (older) implementation had a bug - The storage
// name was "Thumbnail" instead of "Thumbnails". The
// old name is still used as fallback but this code can
// be removed soon.
if ( ! xIStream.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement( "Thumbnail",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access tuhmbnail of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
// Extract the image from the stream.
BitmapEx aThumbnail;
if (xIStream.is())
{
::std::auto_ptr<SvStream> pStream (
::utl::UcbStreamHelper::CreateStream (xIStream));
::vcl::PNGReader aReader (*pStream);
aThumbnail = aReader.Read ();
}
return TemplateAbstractView::scaleImg(aThumbnail,width,height);
return TemplateAbstractView::scaleImg(ThumbnailView::readThumbnail(msURL), width, height);
}
IMPL_LINK_NOARG(TemplateAbstractView, ShowRootRegionHdl)
......
......@@ -1285,6 +1285,109 @@ bool ThumbnailView::renameItem(ThumbnailViewItem*, OUString)
return false;
}
BitmapEx ThumbnailView::readThumbnail(const OUString &msURL)
{
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
// Load the thumbnail from a template document.
uno::Reference<io::XInputStream> xIStream;
uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
try
{
uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext);
uno::Sequence<uno::Any> aArgs (2);
aArgs[0] <<= msURL;
aArgs[1] <<= embed::ElementModes::READ;
uno::Reference<embed::XStorage> xDocStorage (
xStorageFactory->createInstanceWithArguments(aArgs),
uno::UNO_QUERY);
try
{
if (xDocStorage.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement(
"Thumbnails",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
try
{
// An (older) implementation had a bug - The storage
// name was "Thumbnail" instead of "Thumbnails". The
// old name is still used as fallback but this code can
// be removed soon.
if ( ! xIStream.is())
{
uno::Reference<embed::XStorage> xStorage (
xDocStorage->openStorageElement( "Thumbnail",
embed::ElementModes::READ));
if (xStorage.is())
{
uno::Reference<io::XStream> xThumbnailCopy (
xStorage->cloneStreamElement("thumbnail.png"));
if (xThumbnailCopy.is())
xIStream = xThumbnailCopy->getInputStream();
}
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
}
catch (const uno::Exception& rException)
{
OSL_TRACE (
"caught exception while trying to access tuhmbnail of %s: %s",
OUStringToOString(msURL,
RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(rException.Message,
RTL_TEXTENCODING_UTF8).getStr());
}
// Extract the image from the stream.
BitmapEx aThumbnail;
if (xIStream.is())
{
::std::auto_ptr<SvStream> pStream (
::utl::UcbStreamHelper::CreateStream (xIStream));
::vcl::PNGReader aReader (*pStream);
aThumbnail = aReader.Read ();
}
// Note that the preview is returned without scaling it to the desired
// width. This gives the caller the chance to take advantage of a
// possibly larger resolution then was asked for.
return aThumbnail;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment