20#include <com/sun/star/beans/PropertyValue.hpp>
21#include <com/sun/star/beans/XPropertyAccess.hpp>
22#include <com/sun/star/container/XContainerQuery.hpp>
23#include <com/sun/star/document/XExporter.hpp>
24#include <com/sun/star/embed/XStorage.hpp>
25#include <com/sun/star/frame/XDispatchProvider.hpp>
26#include <com/sun/star/frame/XDispatch.hpp>
27#include <com/sun/star/frame/XStatusListener.hpp>
28#include <com/sun/star/frame/XFrame.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/frame/ModuleManager.hpp>
31#include <com/sun/star/frame/XStorable.hpp>
32#include <com/sun/star/io/IOException.hpp>
33#include <com/sun/star/lang/XMultiServiceFactory.hpp>
34#include <com/sun/star/system/SimpleSystemMail.hpp>
35#include <com/sun/star/system/SimpleCommandMail.hpp>
36#include <com/sun/star/system/XSimpleMailClientSupplier.hpp>
37#include <com/sun/star/system/SimpleMailClientFlags.hpp>
38#include <com/sun/star/ucb/CommandAbortedException.hpp>
39#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
40#include <com/sun/star/uno/Reference.h>
41#include <com/sun/star/util/URLTransformer.hpp>
42#include <com/sun/star/util/XURLTransformer.hpp>
43#include <com/sun/star/util/XModifiable.hpp>
45#include <osl/diagnose.h>
49#include <sfx2/strings.hrc>
70using namespace ::com::sun::star::system;
75class PrepareListener_Impl :
public ::cppu::WeakImplHelper< css::frame::XStatusListener >
79 PrepareListener_Impl();
82 virtual void SAL_CALL statusChanged(
const css::frame::FeatureStateEvent& aEvent)
override;
85 virtual void SAL_CALL disposing(
const css::lang::EventObject& aEvent)
override;
87 bool IsSet()
const {
return m_bState;}
92PrepareListener_Impl::PrepareListener_Impl() :
97void PrepareListener_Impl::statusChanged(
const css::frame::FeatureStateEvent& rEvent)
99 if( rEvent.IsEnabled )
100 rEvent.State >>= m_bState;
105void PrepareListener_Impl::disposing(
const css::lang::EventObject& )
114 const uno::Reference< lang::XMultiServiceFactory >& xSMGR,
115 const uno::Reference< frame::XModel >& xModel,
116 const OUString& rFilterName,
117 std::u16string_view rType,
120 css::uno::Sequence< css::beans::PropertyValue >& rArgs )
126 uno::Sequence < beans::PropertyValue > aProps;
127 css::uno::Reference< css::container::XNameAccess > xFilterCFG(
128 xSMGR->createInstance(
"com.sun.star.document.FilterFactory" ), uno::UNO_QUERY );
129 css::uno::Reference< css::util::XModifiable > xModifiable(
xModel, css::uno::UNO_QUERY );
131 if ( !xFilterCFG.is() )
134 uno::Any aAny = xFilterCFG->getByName( rFilterName );
136 if ( aAny >>= aProps )
138 for(
const auto& rProp : std::as_const(aProps) )
140 if( rProp.Name ==
"UIComponent" )
142 OUString aServiceName;
143 rProp.
Value >>= aServiceName;
144 if( !aServiceName.isEmpty() )
146 uno::Reference< ui::dialogs::XExecutableDialog > xFilterDialog(
147 xSMGR->createInstance( aServiceName ), uno::UNO_QUERY );
148 uno::Reference< beans::XPropertyAccess > xFilterProperties(
149 xFilterDialog, uno::UNO_QUERY );
151 if( xFilterDialog.is() && xFilterProperties.is() )
153 uno::Reference< document::XExporter > xExporter( xFilterDialog, uno::UNO_QUERY );
160 uno::Sequence< beans::PropertyValue > aFilterDataValue{
166 uno::Sequence< beans::PropertyValue > aPropsForDialog{
171 xFilterProperties->setPropertyValues( aPropsForDialog );
175 xExporter->setSourceDocument(
xModel );
177 if( xFilterDialog->execute() )
180 const uno::Sequence< beans::PropertyValue > aPropsFromDialog = xFilterProperties->getPropertyValues();
183 auto pProp = std::find_if(aPropsFromDialog.begin(), aPropsFromDialog.end(),
184 [](
const beans::PropertyValue& rDialogProp) { return rDialogProp.Name ==
"FilterData"; });
185 if (pProp != aPropsFromDialog.end())
188 rArgs.realloc( ++rNumArgs );
189 auto pArgs = rArgs.getArray();
190 pArgs[rNumArgs-1].Name = pProp->Name;
191 pArgs[rNumArgs-1].Value = pProp->Value;
205 xModifiable->setModified(
false );
207 catch( css::beans::PropertyVetoException& )
220 catch( css::uno::RuntimeException& )
224 catch( uno::Exception& )
237 const OUString& aSaveFileName,
238 const css::uno::Reference< css::uno::XInterface >& xFrameOrModel,
239 const OUString& rType,
240 OUString& rFileNamePath )
245 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
246 css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
250 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager( css::frame::ModuleManager::create(xContext) );
255 aModule = xModuleManager->identify( xFrameOrModel );
257 catch ( css::uno::RuntimeException& )
261 catch ( css::uno::Exception& )
265 css::uno::Reference< css::frame::XFrame >
xFrame( xFrameOrModel, css::uno::UNO_QUERY );
266 css::uno::Reference< css::frame::XModel >
xModel( xFrameOrModel, css::uno::UNO_QUERY );
269 css::uno::Reference< css::frame::XController >
xController =
xFrame->getController();
275 if ( !aModule.isEmpty() &&
xModel.is() )
277 bool bModified(
false );
278 bool bHasLocation(
false );
279 bool bStoreTo(
false );
281 css::uno::Reference< css::util::XModifiable > xModifiable(
xModel, css::uno::UNO_QUERY );
282 css::uno::Reference< css::frame::XStorable > xStorable(
xModel, css::uno::UNO_QUERY );
284 if ( xModifiable.is() )
285 bModified = xModifiable->isModified();
286 if ( xStorable.is() )
288 OUString aLocation = xStorable->getLocation();
291 bool bPrivateProtocol = ( aFileObj.
GetProtocol() == INetProtocol::PrivSoffice );
293 bHasLocation = !aLocation.isEmpty() && !bPrivateProtocol;
294 OSL_ASSERT( !bPrivateProtocol );
296 if ( !rType.isEmpty() )
299 if ( xStorable.is() )
301 OUString aFilterName;
302 OUString aTypeName( rType );
306 css::uno::Reference< css::container::XContainerQuery > xContainerQuery(
307 xSMGR->createInstance(
"com.sun.star.document.FilterFactory" ),
308 css::uno::UNO_QUERY );
313 css::uno::Sequence< css::beans::NamedValue > aQuery( bSendAsPDF ? 3 : 2 );
314 auto pQuery = aQuery.getArray();
315 pQuery[0].Name =
"Type";
316 pQuery[0].Value <<= aTypeName;
317 pQuery[1].Name =
"DocumentService";
318 pQuery[1].Value <<= aModule;
326 pQuery[2].Name =
"Flags";
327 pQuery[2].Value <<= sal_Int32(0x80042);
330 css::uno::Reference< css::container::XEnumeration > xEnumeration =
331 xContainerQuery->createSubSetEnumerationByProperties( aQuery );
333 if ( xEnumeration->hasMoreElements() )
348 if ( aOrgFilterName == aFilterName )
368 if ( !bHasLocation || aFilterName.isEmpty())
375 "ooSetupFactoryDefaultFilter",
377 css::uno::Reference< css::container::XNameAccess > xNameAccess(
378 xContainerQuery, css::uno::UNO_QUERY );
379 if ( xNameAccess.is() )
387 catch ( css::container::NoSuchElementException& )
390 catch ( css::beans::UnknownPropertyException& )
398 if (( aFilterName.isEmpty() ) ||
399 ( aTypeName.isEmpty() && !bHasLocation ))
403 if ( bHasLocation && !bStoreTo )
410 css::uno::Reference< container::XNameAccess > xTypeDetection(
411 xSMGR->createInstance(
"com.sun.star.document.TypeDetection" ),
412 css::uno::UNO_QUERY );
415 if ( xTypeDetection.is() )
422 ::uno::Sequence< OUString >() );
423 if ( aExtensions.hasElements() )
424 aExtension = aExtensions[0];
426 catch ( css::container::NoSuchElementException& )
433 aFileName = aSaveFileName;
434 if ( aFileName.isEmpty() )
439 aFileName =
"noname";
450 if ( aFileName.isEmpty() )
453 OSL_ASSERT( !aFilterName.isEmpty() );
454 OSL_ASSERT( !aFileName.isEmpty() );
460 INetURLObject aFilePathObj( ::utl::CreateTempURL(
nullptr,
true) );
466 sal_Int32 nNumArgs(1);
467 static constexpr OUStringLiteral aPasswordPropName(
u"Password" );
469 "FilterName", aFilterName) };
475 if ( !aPassword.isEmpty() )
477 aArgs.realloc( ++nNumArgs );
478 auto pArgs = aArgs.getArray();
479 pArgs[nNumArgs-1].Name = aPasswordPropName;
480 pArgs[nNumArgs-1].Value <<= aPassword;
483 bool bNeedsPreparation =
false;
484 css::util::URL aPrepareURL;
485 css::uno::Reference< css::frame::XDispatch > xPrepareDispatch;
486 css::uno::Reference< css::frame::XDispatchProvider > xDispatchProvider(
xFrame, css::uno::UNO_QUERY );
487 css::uno::Reference< css::util::XURLTransformer > xURLTransformer( css::util::URLTransformer::create( xContext ) );
494 aPrepareURL.Complete =
".uno:PrepareMailExport";
495 xURLTransformer->parseStrict( aPrepareURL );
497 if ( xDispatchProvider.is() )
499 xPrepareDispatch.set( xDispatchProvider->queryDispatch( aPrepareURL, OUString(), 0 ));
500 if ( xPrepareDispatch.is() )
503 xPrepareDispatch->addStatusListener( pPrepareListener, aPrepareURL );
504 bNeedsPreparation = pPrepareListener->IsSet();
505 xPrepareDispatch->removeStatusListener( pPrepareListener, aPrepareURL );
509 catch ( css::uno::RuntimeException& )
513 catch ( css::uno::Exception& )
518 if ( bModified || !bHasLocation || bStoreTo || bNeedsPreparation )
523 if( bNeedsPreparation && xPrepareDispatch.is() )
527 css::uno::Sequence< css::beans::PropertyValue > aDispatchArgs;
528 xPrepareDispatch->dispatch( aPrepareURL, aDispatchArgs );
530 catch ( css::uno::RuntimeException& )
534 catch ( css::uno::Exception& )
543 xSMGR,
xModel, aFilterName, rType, bModified, nNumArgs, aArgs );
547 return eShowPDFFilterDialog;
550 xStorable->storeToURL( aFileURL, aArgs );
551 rFileNamePath = aFileURL;
558 aURL.Complete =
".uno:MailExportFinished";
559 xURLTransformer->parseStrict(
aURL );
561 if ( xDispatchProvider.is() )
563 css::uno::Reference< css::frame::XDispatch >
xDispatch(
564 xDispatchProvider->queryDispatch(
aURL, OUString(), 0 ));
569 css::uno::Sequence< css::beans::PropertyValue > aDispatchArgs;
572 catch ( css::uno::RuntimeException& )
576 catch ( css::uno::Exception& )
589 xModifiable->setModified(
false );
591 catch( css::beans::PropertyVetoException& )
596 catch ( css::io::IOException& )
604 aArgs.realloc( ++nNumArgs );
605 auto pArgs = aArgs.getArray();
606 pArgs[nNumArgs-1].Name =
"CopyStreamIfPossible";
607 pArgs[nNumArgs-1].Value <<=
true;
611 xStorable->storeToURL( aFileURL, aArgs );
612 rFileNamePath = aFileURL;
615 catch ( css::io::IOException& )
637 if ( !rAddress.isEmpty() )
649 const css::uno::Reference< css::uno::XInterface >& xFrameOrModel,
650 const OUString& sAttachmentTitle )
666 css::uno::Reference < XComponentContext > xContext = ::comphelper::getProcessComponentContext();
668 css::uno::Reference< XSimpleMailClientSupplier > xSimpleMailClientSupplier;
672 xSimpleMailClientSupplier = SimpleSystemMail::create( xContext );
674 catch (
const uno::Exception & )
677 if ( ! xSimpleMailClientSupplier.is() )
680 xSimpleMailClientSupplier = SimpleCommandMail::create( xContext );
682 catch (
const uno::Exception & )
686 if ( xSimpleMailClientSupplier.is() )
688 css::uno::Reference< XSimpleMailClient > xSimpleMailClient = xSimpleMailClientSupplier->querySimpleMailClient();
690 if ( !xSimpleMailClient.is() )
697 css::uno::Reference< XSimpleMailMessage > xSimpleMailMessage = xSimpleMailClient->createSimpleMailMessage();
698 if ( xSimpleMailMessage.is() )
700 sal_Int32 nSendFlags = SimpleMailClientFlags::DEFAULTS;
713 xSimpleMailMessage->setRecipient(
mpToList->at( 0 ) );
714 nSendFlags = SimpleMailClientFlags::NO_USER_INTERFACE;
720 Sequence< OUString > aCcRecipientSeq( nToCount - 1 );
721 std::copy_n(std::next(
mpToList->begin()), aCcRecipientSeq.getLength(),
722 aCcRecipientSeq.getArray());
723 xSimpleMailMessage->setCcRecipient( aCcRecipientSeq );
728 if ( xSimpleMailMessage->getSubject().isEmpty() ) {
735 if (subject.isEmpty()) {
740 xSimpleMailMessage->setSubject( subject );
742 xSimpleMailMessage->setAttachement( aAttachmentSeq );
747 xSimpleMailClient->sendSimpleMailMessage( xSimpleMailMessage, nSendFlags );
750 catch ( IllegalArgumentException& )
759 css::uno::Reference< css::awt::XWindow > xParentWindow =
xFrame->getContainerWindow();
764 std::unique_ptr<weld::MessageDialog> xBox(xBuilder->weld_message_dialog(
"ErrorFindEmailDialog"));
818 if ( !aFirstName.isEmpty() || !
aName.isEmpty() )
820 if ( !aFirstName.isEmpty() )
824 if ( !
aName.isEmpty() )
829 rFrom = rFrom.replaceAll(
"<",
"").replaceAll(
">",
"").replaceAll(
"@",
"");
831 OUString aEmailName = aUserCFG.
GetEmail();
834 aEmailName = aEmailName.replaceAll(
"<",
"").replaceAll(
">",
"");
836 if ( !aEmailName.isEmpty() )
838 if ( !rFrom.isEmpty() )
844 return !rFrom.isEmpty();
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
static std::unique_ptr< weld::Builder > CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false, sal_uInt64 nLOKWindowId=0)
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString getExtension(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString getBase(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool insertName(std::u16string_view rTheName, bool bAppendFinalSlash=false, sal_Int32 nIndex=LAST_SEGMENT, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
INetProtocol GetProtocol() const
bool setExtension(std::u16string_view rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
SendMailResult AttachDocument(const css::uno::Reference< css::uno::XInterface > &xFrameOrModel, const OUString &sAttachmentTitle)
attaches a document to the current attachment list, can be called more than once.
void AddToAddress(const OUString &rAddress)
std::unique_ptr< AddressList_Impl > mpToList
::std::vector< OUString > maAttachedDocuments
static SaveResult ShowFilterOptionsDialog(const css::uno::Reference< css::lang::XMultiServiceFactory > &xSMGR, const css::uno::Reference< css::frame::XModel > &xModel, const OUString &rFilterName, std::u16string_view rType, bool bModified, sal_Int32 &rNumArgs, css::uno::Sequence< css::beans::PropertyValue > &rArgs)
SendMailResult Send(const css::uno::Reference< css::frame::XFrame > &xFrame)
SendMailResult SaveAndSend(const css::uno::Reference< css::frame::XFrame > &xFrame, const OUString &rType)
static SaveResult SaveDocumentAsFormat(const OUString &aSaveFileName, const css::uno::Reference< css::uno::XInterface > &xFrameOrModel, const OUString &rType, OUString &rFileNamePath)
OUString GetFirstName() const
OUString GetEmail() const
OUString GetLastName() const
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
Reference< XDispatch > xDispatch
const char16_t PDF_DOCUMENT_TYPE[]
bool CreateFromAddress_Impl(OUString &rFrom)
::std::vector< OUString > AddressList_Impl
OString strip(const OString &rIn, char c)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
OUString SfxResId(TranslateId aId)
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel