LibreOffice Module writerperfect (master) 1
MSWorksImportFilter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* MSWorksImportFilter: Sets up the filter, and calls DocumentCollector
3 * to do the actual filtering
4 *
5 * This file is part of the LibreOffice project.
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
14
15#include <libwps/libwps.h>
16
18#include <WPFTResMgr.hxx>
20#include <strings.hrc>
21
22static bool handleEmbeddedWKSObject(const librevenge::RVNGBinaryData& data,
23 OdfDocumentHandler* pHandler, const OdfStreamType streamType)
24{
25 OdsGenerator exporter;
26 exporter.addDocumentHandler(pHandler, streamType);
27 return libwps::WPSDocument::parse(data.getDataStream(), &exporter) == libwps::WPS_OK;
28}
29
31 librevenge::RVNGInputStream& rInput,
32 OdtGenerator& rGenerator,
33 utl::MediaDescriptor& mediaDescriptor)
34{
35 libwps::WPSKind kind = libwps::WPS_TEXT;
36 libwps::WPSCreator creator;
37 bool needEncoding = false;
38 const libwps::WPSConfidence confidence
39 = libwps::WPSDocument::isFileFormatSupported(&rInput, kind, creator, needEncoding);
40
41 std::string fileEncoding;
42 if ((kind == libwps::WPS_TEXT) && (confidence == libwps::WPS_CONFIDENCE_EXCELLENT)
43 && needEncoding)
44 {
45 OUString encoding;
46 // first check if we can find the encoding in the filter options (headless mode)
47 mediaDescriptor[utl::MediaDescriptor::PROP_FILTEROPTIONS] >>= encoding;
48 if (!encoding.isEmpty()) // TODO: check if the encoding string is valid
49 fileEncoding = encoding.toUtf8().getStr();
50 else
51 {
52 OUString title;
53
54 switch (creator)
55 {
56 case libwps::WPS_MSWORKS:
57 title = WpResId(STR_ENCODING_DIALOG_TITLE_MSWORKS);
58 encoding = "CP850";
59 break;
60 case libwps::WPS_RESERVED_0: // MS Write
61 title = WpResId(STR_ENCODING_DIALOG_TITLE_MSWRITE);
62 encoding = "CP1252";
63 break;
64 case libwps::WPS_RESERVED_1: // DosWord
65 title = WpResId(STR_ENCODING_DIALOG_TITLE_DOSWORD);
66 encoding = "CP850";
67 break;
68 default:
69 title = WpResId(STR_ENCODING_DIALOG_TITLE);
70 encoding = "CP850";
71 break;
72 }
73
74 fileEncoding = encoding.toUtf8().getStr(); // set default to the proposed encoding
75 try
76 {
77 writerperfect::WPFTEncodingDialog aDlg(pParent, title, encoding);
78 if (aDlg.run() == RET_OK)
79 {
80 if (!aDlg.GetEncoding().isEmpty())
81 fileEncoding = aDlg.GetEncoding().toUtf8().getStr();
82 }
83 // we can fail because we are in headless mode, the user has cancelled conversion, ...
84 else if (aDlg.hasUserCalledCancel())
85 return false;
86 }
87 catch (css::uno::Exception&)
88 {
89 TOOLS_WARN_EXCEPTION("writerperfect", "ignoring");
90 }
91 }
92 }
93 return libwps::WPS_OK
94 == libwps::WPSDocument::parse(&rInput, &rGenerator, "", fileEncoding.c_str());
95}
96
97bool MSWorksImportFilter::doDetectFormat(librevenge::RVNGInputStream& rInput, OUString& rTypeName)
98{
99 libwps::WPSKind kind = libwps::WPS_TEXT;
100 libwps::WPSCreator creator;
101 bool needEncoding;
102 const libwps::WPSConfidence confidence
103 = libwps::WPSDocument::isFileFormatSupported(&rInput, kind, creator, needEncoding);
104
105 if ((kind == libwps::WPS_TEXT) && (confidence == libwps::WPS_CONFIDENCE_EXCELLENT))
106 {
107 switch (creator)
108 {
109 case libwps::WPS_MSWORKS:
110 rTypeName = "writer_MS_Works_Document";
111 break;
112 case libwps::WPS_RESERVED_0:
113 rTypeName = "writer_MS_Write";
114 break;
115 case libwps::WPS_RESERVED_1:
116 rTypeName = "writer_DosWord";
117 break;
118 case libwps::WPS_RESERVED_4:
119 rTypeName = "writer_PocketWord_File";
120 break;
121 default:
122 break;
123 }
124 }
125
126 return !rTypeName.isEmpty();
127}
128
129void MSWorksImportFilter::doRegisterHandlers(OdtGenerator& rGenerator)
130{
131 rGenerator.registerEmbeddedObjectHandler("image/wks-ods", &handleEmbeddedWKSObject);
132}
133
134// XServiceInfo
136{
137 return "com.sun.star.comp.Writer.MSWorksImportFilter";
138}
139
140sal_Bool SAL_CALL MSWorksImportFilter::supportsService(const OUString& rServiceName)
141{
142 return cppu::supportsService(this, rServiceName);
143}
144
145css::uno::Sequence<OUString> SAL_CALL MSWorksImportFilter::getSupportedServiceNames()
146{
147 return { "com.sun.star.document.ImportFilter", "com.sun.star.document.ExtendedTypeDetection" };
148}
149
150extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
152 css::uno::XComponentContext* const context, const css::uno::Sequence<css::uno::Any>&)
153{
154 return cppu::acquire(new MSWorksImportFilter(context));
155}
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_Writer_MSWorksImportFilter_get_implementation(css::uno::XComponentContext *const context, const css::uno::Sequence< css::uno::Any > &)
static bool handleEmbeddedWKSObject(const librevenge::RVNGBinaryData &data, OdfDocumentHandler *pHandler, const OdfStreamType streamType)
OUString WpResId(TranslateId aId)
Definition: WPFTResMgr.hxx:7
virtual bool doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual bool doImportDocument(weld::Window *pParent, librevenge::RVNGInputStream &rInput, OdtGenerator &rGenerator, utl::MediaDescriptor &) override
virtual OUString SAL_CALL getImplementationName() override
virtual void doRegisterHandlers(OdtGenerator &rGenerator) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static constexpr OUStringLiteral PROP_FILTEROPTIONS
virtual short run()
#define TOOLS_WARN_EXCEPTION(area, stream)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
RET_OK