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));
109 void ODocumentInfoPreview::insertEntry(
110 std::u16string_view title
, OUString
const & value
)
112 if (m_xEditEngine
->HasText()) {
113 m_xEditEngine
->QuickInsertText(u
"\n\n"_ustr
, ESelection::AtEnd());
116 OUString
caption(OUString::Concat(title
) + ":\n");
117 m_xEditEngine
->QuickInsertText(caption
, ESelection::AtEnd());
119 SfxItemSet
aSet(m_xEditEngine
->GetEmptyItemSet());
120 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT
));
121 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT_CJK
));
122 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, EE_CHAR_WEIGHT_CTL
));
123 int nCaptionPara
= m_xEditEngine
->GetParagraphCount() - 2;
124 m_xEditEngine
->QuickSetAttribs(aSet
, ESelection(nCaptionPara
, 0, nCaptionPara
, caption
.getLength() - 1));
126 m_xEditEngine
->QuickInsertText(value
, ESelection::AtEnd());
129 void ODocumentInfoPreview::insertNonempty(tools::Long id
, OUString
const & value
)
131 if (!value
.isEmpty()) {
132 insertEntry(SvtDocInfoTable_Impl::GetString(id
), value
);
136 void ODocumentInfoPreview::insertDateTime(
137 tools::Long id
, css::util::DateTime
const & value
)
140 Date(value
.Day
, value
.Month
, value
.Year
),
142 value
.Hours
, value
.Minutes
, value
.Seconds
, value
.NanoSeconds
));
143 if (aToolsDT
.IsValidAndGregorian()) {
144 const LocaleDataWrapper
& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
145 OUString buf
= rLocaleWrapper
.getDate(aToolsDT
) +
147 rLocaleWrapper
.getTime(aToolsDT
);
148 insertEntry(SvtDocInfoTable_Impl::GetString(id
), buf
);
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */