LibreOffice Module sfx2 (master) 1
sfxpicklist.cxx
Go to the documentation of this file.
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 <comphelper/lok.hxx>
21#include <comphelper/base64.hxx>
22#include <com/sun/star/document/XDocumentProperties.hpp>
25#include <tools/datetime.hxx>
26#include <tools/urlobj.hxx>
27#include <svl/inethist.hxx>
29#include <vcl/svapp.hxx>
30#include <officecfg/Office/Common.hxx>
31
32
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>
42
43
44using namespace ::com::sun::star::uno;
45using namespace ::com::sun::star::beans;
46using namespace ::com::sun::star::util;
47
49{
55 static void AddDocumentToPickList( const SfxObjectShell* pDocShell );
56
57public:
59 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
60};
61
63{
65 return;
66
67 SfxMedium *pMed = pDocSh->GetMedium();
68 if( !pMed )
69 return;
70
71 // Unnamed Documents and embedded-Documents not in Picklist
72 if ( !pDocSh->HasName() ||
74 return;
75
76 // Help not in History
77 INetURLObject aURL( pDocSh->IsDocShared() ? pDocSh->GetSharedFileURL() : pMed->GetOrigURL() );
78 if ( aURL.GetProtocol() == INetProtocol::VndSunStarHelp )
79 return;
80
81 // add no document that forbids this
82 if ( !pMed->IsUpdatePickList() )
83 return;
84
85 // ignore hidden documents
86 if ( !SfxViewFrame::GetFirst( pDocSh ) )
87 return;
88
89 OUString aTitle = pDocSh->GetTitle(SFX_TITLE_PICKLIST);
90 OUString aFilter;
91 std::shared_ptr<const SfxFilter> pFilter = pMed->GetFilter();
92 if ( pFilter )
93 aFilter = pFilter->GetFilterName();
94
95 std::optional<OUString> aThumbnail;
96
97 // generate the thumbnail
98 //fdo#74834: only generate thumbnail for history if the corresponding option is not disabled in the configuration
100 officecfg::Office::Common::History::RecentDocsThumbnail::get())
101 {
102 // not modified => the document matches what is in the shell
103 const SfxUnoAnyItem* pEncryptionDataItem = pMed->GetItemSet().GetItem(SID_ENCRYPTIONDATA, false);
104 if ( pEncryptionDataItem )
105 {
106 // encrypted document, will show a generic document icon instead
107 aThumbnail = OUString();
108 }
109 else
110 {
111 BitmapEx aResultBitmap = pDocSh->GetPreviewBitmap();
112 if (!aResultBitmap.IsEmpty())
113 {
114 SvMemoryStream aStream(65535, 65535);
115 vcl::PngImageWriter aWriter(aStream);
116 if (aWriter.write(aResultBitmap))
117 {
118 Sequence<sal_Int8> aSequence(static_cast<const sal_Int8*>(aStream.GetData()), aStream.Tell());
119 OUStringBuffer aBuffer;
121 aThumbnail = aBuffer.makeStringAndClear();
122 }
123 }
124 }
125 }
126 ::std::optional<bool> const oIsReadOnly(pMed->IsOriginallyLoadedReadOnly());
127
128 // add to svtool history options
129 SvtHistoryOptions::AppendItem( EHistoryType::PickList,
131 aFilter,
132 aTitle,
133 aThumbnail,
134 oIsReadOnly);
135
136 if ( aURL.GetProtocol() == INetProtocol::File )
138 pFilter ? pFilter->GetMimeType() : OUString(),
139 pFilter ? pFilter->GetServiceName() : OUString() );
140}
141
143 : mxImpl(new SfxPickListImpl(rApp))
144{
145}
146
148{
149}
150
152{
153 StartListening(rApp);
154}
155
157{
158 const SfxOpenUrlHint* pOpenUrlHint = dynamic_cast<const SfxOpenUrlHint*>(&rHint);
159 if ( pOpenUrlHint )
160 {
162 }
163
164 const SfxEventHint* pEventHint = dynamic_cast<const SfxEventHint*>(&rHint);
165 if ( !pEventHint )
166 return;
167
168 // only ObjectShell-related events with media interest
169 SfxObjectShell* pDocSh = pEventHint->GetObjShell();
170 if( !pDocSh )
171 return;
172
173 switch ( pEventHint->GetEventId() )
174 {
176 {
177 bool bAllowModif = pDocSh->IsEnableSetModified();
178 if ( bAllowModif )
179 pDocSh->EnableSetModified( false );
180
181 using namespace ::com::sun::star;
182 uno::Reference<document::XDocumentProperties> xDocProps(
183 pDocSh->getDocProperties());
184 if (xDocProps.is()) {
185 xDocProps->setAuthor( SvtUserOptions().GetFullName() );
187 xDocProps->setCreationDate( now.GetUNODateTime() );
188 }
189
190 if ( bAllowModif )
191 pDocSh->EnableSetModified( bAllowModif );
192 }
193 break;
194
200 {
201 AddDocumentToPickList(pDocSh);
202 }
203 break;
204
206 {
207 SfxMedium *pMedium = pDocSh->GetMedium();
208 if (!pMedium)
209 return;
210
211 // We're starting a "Save As". Add the current document (if it's
212 // not a "new" document) to the "Recent Documents" list before we
213 // switch to the new path.
214 // If the current document is new, path will be empty.
215 OUString path = pMedium->GetOrigURL();
216 if (!path.isEmpty())
217 {
218 AddDocumentToPickList(pDocSh);
219 }
220 }
221 break;
222 default: break;
223 }
224}
225
226/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void AddToRecentDocumentList(const OUString &rFileUrl, const OUString &rMimeType, const OUString &rDocumentService)
static bool IsHeadlessModeEnabled()
bool IsEmpty() const
css::util::DateTime GetUNODateTime() const
void PutUrl(const INetURLObject &rUrl)
static INetURLHistory * GetOrCreate()
SfxObjectShell * GetObjShell() const
Definition: event.hxx:211
SfxEventHintId GetEventId() const
Definition: event.hxx:205
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
const std::shared_ptr< const SfxFilter > & GetFilter() const
Definition: docfile.cxx:3111
bool IsOriginallyLoadedReadOnly() const
Definition: docfile.cxx:3829
SAL_DLLPRIVATE bool IsUpdatePickList() const
Definition: docfile.cxx:2924
SfxItemSet & GetItemSet() const
Definition: docfile.cxx:3647
const OUString & GetOrigURL() const
Definition: docfile.cxx:3364
css::uno::Reference< css::document::XDocumentProperties > getDocProperties() const
Definition: objmisc.cxx:161
OUString GetSharedFileURL() const
Definition: objmisc.cxx:644
bool IsAvoidRecentDocs() const
Don't add to the recent documents - it's an expensive operation, sometimes it is not wanted.
Definition: objsh.hxx:464
bool HasName() const
Definition: objsh.hxx:266
bool IsEnableSetModified() const
Definition: objmisc.cxx:248
bool IsDocShared() const
Definition: objmisc.cxx:634
bool IsModified() const
Definition: objmisc.cxx:259
SfxMedium * GetMedium() const
Definition: objsh.hxx:261
OUString GetTitle(sal_uInt16 nMaxLen=0) const
Definition: objmisc.cxx:710
BitmapEx GetPreviewBitmap() const
Definition: objcont.cxx:105
void EnableSetModified(bool bEnable=true)
Definition: objmisc.cxx:241
SfxObjectCreateMode GetCreateMode() const
Definition: objsh.hxx:487
const OUString & GetDocumentURL() const
Definition: openurlhint.cxx:26
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
static void AddDocumentToPickList(const SfxObjectShell *pDocShell)
Adds the given document to the pick list (recent documents) if it satisfies certain requirements,...
Definition: sfxpicklist.cxx:62
SfxPickListImpl(SfxApplication &rApp)
SfxPickList(SfxApplication &rApp)
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetFirst(const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
Definition: viewfrm.cxx:1983
const void * GetData()
sal_uInt64 Tell() const
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
bool write(const BitmapEx &rBitmap)
URL aURL
DateTime now
void AppendItem(EHistoryType eHistory, const OUString &sURL, const OUString &sFilter, const OUString &sTitle, const std::optional< OUString > &sThumbnail, ::std::optional< bool > const oIsReadOnly)
#define SFX_TITLE_PICKLIST
Definition: objsh.hxx:118
signed char sal_Int8
std::unique_ptr< char[]> aBuffer