LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / appl / sfxpicklist.cxx
blob83834597aff094f24cffd9555503e823621f64a8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/pngwrite.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;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::util;
48 class SfxPickListImpl : public SfxListener
50 /**
51 * Adds the given document to the pick list (recent documents) if it satisfies
52 certain requirements, e.g. being writable. Check implementation for requirement
53 details.
55 static void AddDocumentToPickList( const SfxObjectShell* pDocShell );
57 public:
58 SfxPickListImpl(SfxApplication& rApp);
59 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
62 void SfxPickListImpl::AddDocumentToPickList( const SfxObjectShell* pDocSh )
64 if (pDocSh->IsAvoidRecentDocs() || comphelper::LibreOfficeKit::isActive())
65 return;
67 SfxMedium *pMed = pDocSh->GetMedium();
68 if( !pMed )
69 return;
71 // Unnamed Documents and embedded-Documents not in Picklist
72 if ( !pDocSh->HasName() ||
73 SfxObjectCreateMode::STANDARD != pDocSh->GetCreateMode() )
74 return;
76 // Help not in History
77 INetURLObject aURL( pDocSh->IsDocShared() ? pDocSh->GetSharedFileURL() : pMed->GetOrigURL() );
78 if ( aURL.GetProtocol() == INetProtocol::VndSunStarHelp )
79 return;
81 // add no document that forbids this
82 if ( !pMed->IsUpdatePickList() )
83 return;
85 // ignore hidden documents
86 if ( !SfxViewFrame::GetFirst( pDocSh ) )
87 return;
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();
95 std::optional<OUString> aThumbnail;
97 // generate the thumbnail
98 //fdo#74834: only generate thumbnail for history if the corresponding option is not disabled in the configuration
99 if (!pDocSh->IsModified() && !Application::IsHeadlessModeEnabled() &&
100 officecfg::Office::Common::History::RecentDocsThumbnail::get())
102 // not modified => the document matches what is in the shell
103 const SfxUnoAnyItem* pEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(pMed->GetItemSet(), SID_ENCRYPTIONDATA, false);
104 if ( pEncryptionDataItem )
106 // encrypted document, will show a generic document icon instead
107 aThumbnail = OUString();
109 else
111 BitmapEx aResultBitmap = pDocSh->GetPreviewBitmap();
112 if (!aResultBitmap.IsEmpty())
114 SvMemoryStream aStream(65535, 65535);
115 vcl::PNGWriter aWriter(aResultBitmap);
116 if (aWriter.Write(aStream))
118 Sequence<sal_Int8> aSequence(static_cast<const sal_Int8*>(aStream.GetData()), aStream.Tell());
119 OUStringBuffer aBuffer;
120 ::comphelper::Base64::encode(aBuffer, aSequence);
121 aThumbnail = aBuffer.makeStringAndClear();
127 // add to svtool history options
128 SvtHistoryOptions::AppendItem( EHistoryType::PickList,
129 aURL.GetURLNoPass( INetURLObject::DecodeMechanism::NONE ),
130 aFilter,
131 aTitle,
132 aThumbnail);
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 const SfxOpenUrlHint* pOpenUrlHint = dynamic_cast<const SfxOpenUrlHint*>(&rHint);
157 if ( pOpenUrlHint )
159 INetURLHistory::GetOrCreate()->PutUrl( INetURLObject( pOpenUrlHint->GetDocumentURL() ));
162 const SfxEventHint* pEventHint = dynamic_cast<const SfxEventHint*>(&rHint);
163 if ( !pEventHint )
164 return;
166 // only ObjectShell-related events with media interest
167 SfxObjectShell* pDocSh = pEventHint->GetObjShell();
168 if( !pDocSh )
169 return;
171 switch ( pEventHint->GetEventId() )
173 case SfxEventHintId::CreateDoc:
175 bool bAllowModif = pDocSh->IsEnableSetModified();
176 if ( bAllowModif )
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() );
188 if ( bAllowModif )
189 pDocSh->EnableSetModified( bAllowModif );
191 break;
193 case SfxEventHintId::OpenDoc:
194 case SfxEventHintId::SaveDocDone:
195 case SfxEventHintId::SaveAsDocDone:
196 case SfxEventHintId::SaveToDocDone:
197 case SfxEventHintId::CloseDoc:
199 AddDocumentToPickList(pDocSh);
201 break;
203 case SfxEventHintId::SaveAsDoc:
205 SfxMedium *pMedium = pDocSh->GetMedium();
206 if (!pMedium)
207 return;
209 // We're starting a "Save As". Add the current document (if it's
210 // not a "new" document) to the "Recent Documents" list before we
211 // switch to the new path.
212 // If the current document is new, path will be empty.
213 OUString path = pMedium->GetOrigURL();
214 if (!path.isEmpty())
216 AddDocumentToPickList(pDocSh);
219 break;
220 default: break;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */