android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / filter / xml / xmlmeta.cxx
blob925fea5d9aeac3e6d3282634c1efa6922565a5e3
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 <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
21 #include <com/sun/star/frame/XModel.hpp>
22 #include <osl/diagnose.h>
23 #include <xmloff/xmlmetai.hxx>
24 #include <xmloff/ProgressBarHelper.hxx>
25 #include <xmloff/xmltkmap.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <docstat.hxx>
29 #include <doc.hxx>
30 #include <IDocumentStatistics.hxx>
31 #include "xmlimp.hxx"
32 #include "xmlexp.hxx"
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::text;
38 using namespace ::xmloff::token;
40 uno::Reference<document::XDocumentProperties>
41 SwXMLImport::GetDocumentProperties() const
43 if (m_bOrganizerMode || IsStylesOnlyMode() ||
44 IsBlockMode() || IsInsertMode())
46 return nullptr;
48 uno::Reference<document::XDocumentPropertiesSupplier> const xDPS(
49 GetModel(), UNO_QUERY_THROW);
50 return xDPS->getDocumentProperties();
53 SvXMLImportContext *SwXMLImport::CreateMetaContext(
54 const sal_Int32 /*nElement*/ )
56 SvXMLImportContext *pContext = nullptr;
58 if (getImportFlags() & SvXMLImportFlags::META)
60 uno::Reference<document::XDocumentProperties> const xDocProps(
61 GetDocumentProperties());
62 pContext = new SvXMLMetaDocumentContext(*this, xDocProps);
65 return pContext;
68 namespace {
70 enum SvXMLTokenMapAttrs
72 XML_TOK_META_STAT_TABLE = 1,
73 XML_TOK_META_STAT_IMAGE = 2,
74 XML_TOK_META_STAT_OLE = 4,
75 XML_TOK_META_STAT_PAGE = 8,
76 XML_TOK_META_STAT_PARA = 16,
77 XML_TOK_META_STAT_WORD = 32,
78 XML_TOK_META_STAT_CHAR = 64,
79 XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR = 128,
80 XML_TOK_META_STAT_END=XML_TOK_UNKNOWN
83 struct statistic {
84 SvXMLTokenMapAttrs token;
85 const char* name;
86 sal_uInt16 SwDocStat::* target16;
87 sal_uLong SwDocStat::* target32; /* or 64, on LP64 platforms */
92 const struct statistic s_stats [] = {
93 { XML_TOK_META_STAT_TABLE, "TableCount", &SwDocStat::nTable, nullptr },
94 { XML_TOK_META_STAT_IMAGE, "ImageCount", &SwDocStat::nGrf, nullptr },
95 { XML_TOK_META_STAT_OLE, "ObjectCount", &SwDocStat::nOLE, nullptr },
96 { XML_TOK_META_STAT_PAGE, "PageCount", nullptr, &SwDocStat::nPage },
97 { XML_TOK_META_STAT_PARA, "ParagraphCount", nullptr, &SwDocStat::nPara },
98 { XML_TOK_META_STAT_WORD, "WordCount", nullptr, &SwDocStat::nWord },
99 { XML_TOK_META_STAT_CHAR, "CharacterCount", nullptr, &SwDocStat::nChar },
100 { XML_TOK_META_STAT_NON_WHITE_SPACE_CHAR, "NonWhitespaceCharacterCount", nullptr, &SwDocStat::nCharExcludingSpaces },
101 { XML_TOK_META_STAT_END, nullptr, nullptr, nullptr }
104 void SwXMLImport::SetStatistics(
105 const Sequence< beans::NamedValue > & i_rStats)
107 if( IsStylesOnlyMode() || IsInsertMode() )
108 return;
110 SvXMLImport::SetStatistics(i_rStats);
112 SwDoc *pDoc = getDoc();
113 SwDocStat aDocStat( pDoc->getIDocumentStatistics().GetDocStat() );
115 sal_uInt32 nTokens = 0;
117 for (const auto& rStat : i_rStats) {
118 for (struct statistic const* pStat = s_stats; pStat->name != nullptr;
119 ++pStat) {
120 if (rStat.Name.equalsAscii(pStat->name)) {
121 sal_Int32 val = 0;
122 if (rStat.Value >>= val) {
123 if (pStat->target16 != nullptr) {
124 aDocStat.*(pStat->target16)
125 = o3tl::narrowing<sal_uInt16> (val);
126 } else {
127 aDocStat.*(pStat->target32)
128 = static_cast<sal_uInt32> (val);
130 nTokens |= pStat->token;
131 } else {
132 OSL_FAIL("SwXMLImport::SetStatistics: invalid entry");
138 if( nTokens )
139 pDoc->getIDocumentStatistics().SetDocStat( aDocStat );
141 // set progress bar reference to #paragraphs. If not available,
142 // use #pages*10, or guesstimate 250 paragraphs. Additionally
143 // guesstimate PROGRESS_BAR_STEPS each for meta+settings, styles,
144 // and autostyles.
145 bool bSetFallback = true;
146 sal_Int32 nProgressReference = sal_Int32(); // silence C4701
147 const sal_Int32 nProgressReferenceWriggleRoom = 3 * PROGRESS_BAR_STEP;
148 if (nTokens & XML_TOK_META_STAT_PARA)
150 nProgressReference = static_cast<sal_Int32>(aDocStat.nPara);
151 bSetFallback = false;
153 else if (nTokens & XML_TOK_META_STAT_PAGE)
154 bSetFallback = o3tl::checked_multiply<sal_Int32>(aDocStat.nPage, 10, nProgressReference);
155 if (!bSetFallback)
156 bSetFallback = o3tl::checked_add(nProgressReference, nProgressReferenceWriggleRoom, nProgressReference);
157 if (bSetFallback)
158 nProgressReference = 250 + nProgressReferenceWriggleRoom;
159 ProgressBarHelper* pProgress = GetProgressBarHelper();
160 pProgress->SetReference(nProgressReference);
161 pProgress->SetValue( 0 );
164 void SwXMLExport::ExportMeta_()
166 SvXMLExport::ExportMeta_();
168 if( !m_bBlock && IsShowProgress() )
170 ProgressBarHelper *pProgress = GetProgressBarHelper();
171 pProgress->SetValue( pProgress->GetValue() + 2 );
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */