1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/lok.hxx>
21 #include <comphelper/base64.hxx>
22 #include <com/sun/star/document/XDocumentProperties.hpp>
23 #include <unotools/historyoptions.hxx>
24 #include <unotools/useroptions.hxx>
25 #include <tools/datetime.hxx>
26 #include <tools/urlobj.hxx>
27 #include <svl/inethist.hxx>
28 #include <vcl/filter/PngImageWriter.hxx>
29 #include <vcl/svapp.hxx>
30 #include <officecfg/Office/Common.hxx>
33 #include <sfx2/app.hxx>
34 #include <sfxpicklist.hxx>
35 #include <sfx2/sfxsids.hrc>
36 #include <sfx2/event.hxx>
37 #include <sfx2/objsh.hxx>
38 #include <sfx2/docfile.hxx>
39 #include <openurlhint.hxx>
40 #include <sfx2/docfilt.hxx>
41 #include <sfx2/viewfrm.hxx>
44 using namespace ::com::sun::star::uno
;
46 class SfxPickListImpl
: public SfxListener
49 * Adds the given document to the pick list (recent documents) if it satisfies
50 certain requirements, e.g. being writable. Check implementation for requirement
53 static void AddDocumentToPickList(const SfxObjectShell
* pDocShell
, bool bNoThumbnail
= false);
56 SfxPickListImpl(SfxApplication
& rApp
);
57 virtual void Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
) override
;
60 void SfxPickListImpl::AddDocumentToPickList(const SfxObjectShell
* pDocSh
, bool bNoThumbnail
)
62 if (pDocSh
->IsAvoidRecentDocs() || comphelper::LibreOfficeKit::isActive())
65 SfxMedium
*pMed
= pDocSh
->GetMedium();
69 // Unnamed Documents and embedded-Documents not in Picklist
70 if ( !pDocSh
->HasName() ||
71 SfxObjectCreateMode::STANDARD
!= pDocSh
->GetCreateMode() )
74 // Help not in History
75 INetURLObject
aURL( pDocSh
->IsDocShared() ? pDocSh
->GetSharedFileURL() : pMed
->GetOrigURL() );
76 if ( aURL
.GetProtocol() == INetProtocol::VndSunStarHelp
)
79 // add no document that forbids this
80 if ( !pMed
->IsUpdatePickList() )
83 // ignore hidden documents
84 if ( !SfxViewFrame::GetFirst( pDocSh
) )
87 OUString aTitle
= pDocSh
->GetTitle(SFX_TITLE_PICKLIST
);
89 std::shared_ptr
<const SfxFilter
> pFilter
= pMed
->GetFilter();
91 aFilter
= pFilter
->GetFilterName();
93 std::optional
<OUString
> aThumbnail
;
95 // generate the thumbnail
96 //fdo#74834: only generate thumbnail for history if the corresponding option is not disabled in the configuration
97 if (!bNoThumbnail
&& !pDocSh
->IsModified() && !Application::IsHeadlessModeEnabled() &&
98 officecfg::Office::Common::History::RecentDocsThumbnail::get())
100 // not modified => the document matches what is in the shell
101 const SfxUnoAnyItem
* pEncryptionDataItem
= pMed
->GetItemSet().GetItem(SID_ENCRYPTIONDATA
, false);
102 if ( pEncryptionDataItem
)
104 // encrypted document, will show a generic document icon instead
105 aThumbnail
= OUString();
109 BitmapEx aResultBitmap
= pDocSh
->GetPreviewBitmap();
110 if (!aResultBitmap
.IsEmpty())
112 SvMemoryStream
aStream(65535, 65535);
113 vcl::PngImageWriter
aWriter(aStream
);
114 if (aWriter
.write(aResultBitmap
))
116 Sequence
<sal_Int8
> aSequence(static_cast<const sal_Int8
*>(aStream
.GetData()), aStream
.Tell());
117 OUStringBuffer aBuffer
;
118 ::comphelper::Base64::encode(aBuffer
, aSequence
);
119 aThumbnail
= aBuffer
.makeStringAndClear();
124 ::std::optional
<bool> const oIsReadOnly(pMed
->IsOriginallyLoadedReadOnly());
126 // add to svtool history options
127 SvtHistoryOptions::AppendItem( EHistoryType::PickList
,
128 aURL
.GetURLNoPass( INetURLObject::DecodeMechanism::NONE
),
134 if ( aURL
.GetProtocol() == INetProtocol::File
)
135 Application::AddToRecentDocumentList( aURL
.GetURLNoPass( INetURLObject::DecodeMechanism::NONE
),
136 pFilter
? pFilter
->GetMimeType() : OUString(),
137 pFilter
? pFilter
->GetServiceName() : OUString() );
140 SfxPickList::SfxPickList(SfxApplication
& rApp
)
141 : mxImpl(new SfxPickListImpl(rApp
))
145 SfxPickList::~SfxPickList()
149 SfxPickListImpl::SfxPickListImpl(SfxApplication
& rApp
)
151 StartListening(rApp
);
154 void SfxPickListImpl::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
156 if (rHint
.GetId() == SfxHintId::SfxOpenUrl
)
158 const SfxOpenUrlHint
* pOpenUrlHint
= static_cast<const SfxOpenUrlHint
*>(&rHint
);
159 INetURLHistory::GetOrCreate()->PutUrl( INetURLObject( pOpenUrlHint
->GetDocumentURL() ));
162 if (rHint
.GetId() != SfxHintId::ThisIsAnSfxEventHint
)
165 const SfxEventHint
& rEventHint
= static_cast<const SfxEventHint
&>(rHint
);
166 // only ObjectShell-related events with media interest
167 rtl::Reference
<SfxObjectShell
> pDocSh
= rEventHint
.GetObjShell();
171 switch (rEventHint
.GetEventId())
173 case SfxEventHintId::CreateDoc
:
175 bool bAllowModif
= pDocSh
->IsEnableSetModified();
177 pDocSh
->EnableSetModified( false );
179 using namespace ::com::sun::star
;
180 uno::Reference
<document::XDocumentProperties
> xDocProps(
181 pDocSh
->getDocProperties());
182 if (xDocProps
.is()) {
183 xDocProps
->setAuthor( SvtUserOptions().GetFullName() );
184 ::DateTime
now( ::DateTime::SYSTEM
);
185 xDocProps
->setCreationDate( now
.GetUNODateTime() );
189 pDocSh
->EnableSetModified( bAllowModif
);
193 case SfxEventHintId::OpenDoc
:
194 case SfxEventHintId::SaveDocDone
:
195 case SfxEventHintId::SaveAsDocDone
:
196 case SfxEventHintId::SaveToDocDone
:
197 case SfxEventHintId::CloseDoc
:
199 const bool bNoThumbnail
= rEventHint
.GetEventId() == SfxEventHintId::CloseDoc
;
200 AddDocumentToPickList(pDocSh
.get(), bNoThumbnail
);
204 case SfxEventHintId::SaveAsDoc
:
206 SfxMedium
*pMedium
= pDocSh
->GetMedium();
210 // We're starting a "Save As". Add the current document (if it's
211 // not a "new" document) to the "Recent Documents" list before we
212 // switch to the new path.
213 // If the current document is new, path will be empty.
214 OUString path
= pMedium
->GetOrigURL();
217 AddDocumentToPickList(pDocSh
.get());
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */