Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include "filterdetect.hxx"
11 :
12 : #include "tools/urlobj.hxx"
13 : #include "ucbhelper/content.hxx"
14 :
15 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
16 : #include <com/sun/star/io/XInputStream.hpp>
17 :
18 : #define WRITER_TEXT_FILTER "Text"
19 : #define CALC_TEXT_FILTER "Text - txt - csv (StarCalc)"
20 :
21 : using namespace ::com::sun::star;
22 :
23 : namespace {
24 :
25 : template<typename T>
26 0 : void setPropValue(uno::Sequence<beans::PropertyValue>& rProps, sal_Int32 nPos, const char* pName, const T& rValue)
27 : {
28 0 : if (nPos >= 0)
29 0 : rProps[nPos].Value <<= rValue;
30 : else
31 : {
32 0 : sal_Int32 n = rProps.getLength();
33 0 : rProps.realloc(n+1);
34 0 : rProps[n].Name = OUString::createFromAscii(pName);
35 0 : rProps[n].Value <<= rValue;
36 : }
37 0 : }
38 :
39 : }
40 :
41 0 : PlainTextFilterDetect::PlainTextFilterDetect(const uno::Reference<uno::XComponentContext>& xCxt) :
42 0 : mxCxt(xCxt) {}
43 :
44 0 : PlainTextFilterDetect::~PlainTextFilterDetect() {}
45 :
46 0 : OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyValue>& lDescriptor) throw (uno::RuntimeException)
47 : {
48 0 : OUString aType;
49 0 : OUString aDocService;
50 0 : OUString aExt;
51 0 : OUString aUrl;
52 :
53 0 : sal_Int32 nFilter = -1;
54 :
55 0 : for (sal_Int32 i = 0, n = lDescriptor.getLength(); i < n; ++i)
56 : {
57 0 : if (lDescriptor[i].Name == "TypeName")
58 0 : lDescriptor[i].Value >>= aType;
59 0 : else if (lDescriptor[i].Name == "FilterName")
60 0 : nFilter = i;
61 0 : else if (lDescriptor[i].Name == "DocumentService")
62 0 : lDescriptor[i].Value >>= aDocService;
63 0 : else if (lDescriptor[i].Name == "URL")
64 : {
65 0 : lDescriptor[i].Value >>= aUrl;
66 :
67 : // Get the file name extension.
68 0 : INetURLObject aParser(aUrl);
69 0 : aExt = aParser.getExtension(
70 0 : INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET);
71 0 : aExt = aExt.toAsciiLowerCase();
72 : }
73 : }
74 :
75 0 : if (aType == "generic_Text")
76 : {
77 : // Generic text type.
78 :
79 : // Decide which filter to use based on the document service first,
80 : // then on extension if that's not available.
81 :
82 0 : if (aDocService == "com.sun.star.sheet.SpreadsheetDocument")
83 : // Open it in Calc.
84 0 : setPropValue(lDescriptor, nFilter, "FilterName", OUString(CALC_TEXT_FILTER));
85 0 : else if (aDocService == "com.sun.star.text.TextDocument")
86 : // Open it in Writer.
87 0 : setPropValue(lDescriptor, nFilter, "FilterName", OUString(WRITER_TEXT_FILTER));
88 0 : else if (aExt == "csv")
89 0 : setPropValue(lDescriptor, nFilter, "FilterName", OUString(CALC_TEXT_FILTER));
90 0 : else if (aExt == "txt")
91 0 : setPropValue(lDescriptor, nFilter, "FilterName", OUString(WRITER_TEXT_FILTER));
92 : else
93 : // No clue. Open it in Writer by default.
94 0 : setPropValue(lDescriptor, nFilter, "FilterName", OUString(WRITER_TEXT_FILTER));
95 :
96 0 : return aType;
97 : }
98 :
99 : // failed!
100 0 : return OUString();
101 : }
102 :
103 : // XInitialization
104 :
105 0 : void SAL_CALL PlainTextFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
106 : throw (uno::Exception, uno::RuntimeException)
107 : {
108 0 : }
109 :
110 0 : OUString PlainTextFilterDetect_getImplementationName()
111 : {
112 0 : return OUString("com.sun.star.comp.filters.PlainTextFilterDetect");
113 : }
114 :
115 0 : sal_Bool PlainTextFilterDetect_supportsService(const OUString& ServiceName)
116 : {
117 0 : return ServiceName == "com.sun.star.document.ExtendedTypeDetection" ||
118 0 : ServiceName == "com.sun.star.comp.filters.PlainTextFilterDetect";
119 : }
120 :
121 0 : uno::Sequence<OUString> PlainTextFilterDetect_getSupportedServiceNames()
122 : {
123 0 : uno::Sequence<OUString> aRet(2);
124 0 : OUString* pArray = aRet.getArray();
125 0 : pArray[0] = "com.sun.star.document.ExtendedTypeDetection";
126 0 : pArray[1] = "com.sun.star.comp.filters.PlainTextFilterDetect";
127 0 : return aRet;
128 : }
129 :
130 0 : uno::Reference<uno::XInterface> PlainTextFilterDetect_createInstance(
131 : const uno::Reference<uno::XComponentContext> & rCxt)
132 : {
133 0 : return (cppu::OWeakObject*) new PlainTextFilterDetect(rCxt);
134 : }
135 :
136 : // XServiceInfo
137 0 : OUString SAL_CALL PlainTextFilterDetect::getImplementationName()
138 : throw (uno::RuntimeException)
139 : {
140 0 : return PlainTextFilterDetect_getImplementationName();
141 : }
142 :
143 0 : sal_Bool SAL_CALL PlainTextFilterDetect::supportsService(const OUString& rServiceName)
144 : throw (uno::RuntimeException)
145 : {
146 0 : return PlainTextFilterDetect_supportsService(rServiceName);
147 : }
148 :
149 0 : uno::Sequence<OUString> SAL_CALL PlainTextFilterDetect::getSupportedServiceNames()
150 : throw (uno::RuntimeException)
151 : {
152 0 : return PlainTextFilterDetect_getSupportedServiceNames();
153 : }
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|