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 <sal/config.h>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/document/XDocumentProperties.hpp>
24 #include <com/sun/star/script/CannotConvertException.hpp>
25 #include <com/sun/star/script/Converter.hpp>
26 #include <com/sun/star/script/XTypeConverter.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/string.hxx>
29 #include <editeng/eeitem.hxx>
30 #include <editeng/wghtitem.hxx>
31 #include <rtl/ustring.hxx>
32 #include "DocumentInfoPreview.hxx"
33 #include <vcl/settings.hxx>
34 #include <vcl/svapp.hxx>
35 #include <svl/itemset.hxx>
36 #include <tools/datetime.hxx>
37 #include <comphelper/diagnose_ex.hxx>
38 #include <unotools/localedatawrapper.hxx>
40 #include <templwin.hrc>
41 #include "templwin.hxx"
45 ODocumentInfoPreview::ODocumentInfoPreview()
49 void ODocumentInfoPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
51 WeldEditView::SetDrawingArea(pDrawingArea
);
52 m_xEditView
->HideCursor();
53 m_xEditView
->SetReadOnly(true);
56 ODocumentInfoPreview::~ODocumentInfoPreview()
60 void ODocumentInfoPreview::clear() {
61 m_xEditEngine
->SetText(OUString());
64 void ODocumentInfoPreview::fill(
65 css::uno::Reference
< css::document::XDocumentProperties
> const & xDocProps
)
67 assert(xDocProps
.is());
69 insertNonempty(DI_TITLE
, xDocProps
->getTitle());
70 insertNonempty(DI_FROM
, xDocProps
->getAuthor());
71 insertDateTime(DI_DATE
, xDocProps
->getCreationDate());
72 insertNonempty(DI_MODIFIEDBY
, xDocProps
->getModifiedBy());
73 insertDateTime(DI_MODIFIEDDATE
, xDocProps
->getModificationDate());
74 insertNonempty(DI_PRINTBY
, xDocProps
->getPrintedBy());
75 insertDateTime(DI_PRINTDATE
, xDocProps
->getPrintDate());
76 insertNonempty(DI_THEME
, xDocProps
->getSubject());
79 comphelper::string::convertCommaSeparated(xDocProps
->getKeywords()));
80 insertNonempty(DI_DESCRIPTION
, xDocProps
->getDescription());
82 // User-defined (custom) properties:
83 css::uno::Reference
< css::beans::XPropertySet
> user(
84 xDocProps
->getUserDefinedProperties(), css::uno::UNO_QUERY_THROW
);
85 css::uno::Reference
< css::beans::XPropertySetInfo
> info(
86 user
->getPropertySetInfo());
87 const css::uno::Sequence
< css::beans::Property
> props(info
->getProperties());
88 for (const auto& rProp
: props
) {
89 OUString
name(rProp
.Name
);
90 css::uno::Any
aAny(user
->getPropertyValue(name
));
91 css::uno::Reference
< css::script::XTypeConverter
> conv(
92 css::script::Converter::create(
93 comphelper::getProcessComponentContext()));
96 value
= conv
->convertToSimpleType(aAny
, css::uno::TypeClass_STRING
).
98 } catch (css::script::CannotConvertException
&) {
99 TOOLS_INFO_EXCEPTION("svtools.contnr", "ignored");
101 if (!value
.isEmpty()) {
102 insertEntry(name
, value
);
106 m_xEditView
->SetSelection(ESelection(0, 0, 0, 0));
111 ESelection
InsertAtEnd(const EditEngine
& rEditEngine
)
113 const sal_uInt32 nPara
= rEditEngine
.GetParagraphCount() -1;
114 sal_Int32 nLastLen
= rEditEngine
.GetText(nPara
).getLength();
115 return ESelection(nPara
, nLastLen
, nPara
, nLastLen
);
119 void ODocumentInfoPreview::insertEntry(
120 std::u16string_view title
, OUString
const & value
)
122 if (!m_xEditEngine
->GetText().isEmpty()) {
123 m_xEditEngine
->QuickInsertText("\n\n", InsertAtEnd(*m_xEditEngine
));
126 OUString
caption(OUString::Concat(title
) + ":\n");
127 m_xEditEngine
->QuickInsertText(caption
, InsertAtEnd(*m_xEditEngine
));
129 SfxItemSet
aSet(m_xEditEngine
->GetEmptyItemSet());
130 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT
));
131 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT_CJK
));
132 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT_CTL
));
133 int nCaptionPara
= m_xEditEngine
->GetParagraphCount() - 2;
134 m_xEditEngine
->QuickSetAttribs(aSet
, ESelection(nCaptionPara
, 0, nCaptionPara
, caption
.getLength() - 1));
136 m_xEditEngine
->QuickInsertText(value
, InsertAtEnd(*m_xEditEngine
));
139 void ODocumentInfoPreview::insertNonempty(tools::Long id
, OUString
const & value
)
141 if (!value
.isEmpty()) {
142 insertEntry(SvtDocInfoTable_Impl::GetString(id
), value
);
146 void ODocumentInfoPreview::insertDateTime(
147 tools::Long id
, css::util::DateTime
const & value
)
150 Date(value
.Day
, value
.Month
, value
.Year
),
152 value
.Hours
, value
.Minutes
, value
.Seconds
, value
.NanoSeconds
));
153 if (aToolsDT
.IsValidAndGregorian()) {
154 const LocaleDataWrapper
& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
155 OUString buf
= rLocaleWrapper
.getDate(aToolsDT
) +
157 rLocaleWrapper
.getTime(aToolsDT
);
158 insertEntry(SvtDocInfoTable_Impl::GetString(id
), buf
);
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */