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/.
10 #include "filterdetect.hxx"
12 #include "tools/urlobj.hxx"
13 #include "ucbhelper/content.hxx"
15 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
16 #include <com/sun/star/io/XInputStream.hpp>
18 #define WRITER_TEXT_FILTER "Text"
19 #define CALC_TEXT_FILTER "Text - txt - csv (StarCalc)"
21 using namespace ::com::sun::star
;
26 void setPropValue(uno::Sequence
<beans::PropertyValue
>& rProps
, sal_Int32 nPos
, const char* pName
, const T
& rValue
)
29 rProps
[nPos
].Value
<<= rValue
;
32 sal_Int32 n
= rProps
.getLength();
34 rProps
[n
].Name
= OUString::createFromAscii(pName
);
35 rProps
[n
].Value
<<= rValue
;
41 PlainTextFilterDetect::PlainTextFilterDetect(const uno::Reference
<uno::XComponentContext
>& xCxt
) :
44 PlainTextFilterDetect::~PlainTextFilterDetect() {}
46 OUString SAL_CALL
PlainTextFilterDetect::detect(uno::Sequence
<beans::PropertyValue
>& lDescriptor
) throw (uno::RuntimeException
)
53 sal_Int32 nFilter
= -1;
55 for (sal_Int32 i
= 0, n
= lDescriptor
.getLength(); i
< n
; ++i
)
57 if (lDescriptor
[i
].Name
== "TypeName")
58 lDescriptor
[i
].Value
>>= aType
;
59 else if (lDescriptor
[i
].Name
== "FilterName")
61 else if (lDescriptor
[i
].Name
== "DocumentService")
62 lDescriptor
[i
].Value
>>= aDocService
;
63 else if (lDescriptor
[i
].Name
== "URL")
65 lDescriptor
[i
].Value
>>= aUrl
;
67 // Get the file name extension.
68 INetURLObject
aParser(aUrl
);
69 aExt
= aParser
.getExtension(
70 INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
);
71 aExt
= aExt
.toAsciiLowerCase();
75 if (aType
== "generic_Text")
79 // Decide which filter to use based on the document service first,
80 // then on extension if that's not available.
82 if (aDocService
== "com.sun.star.sheet.SpreadsheetDocument")
84 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(CALC_TEXT_FILTER
));
85 else if (aDocService
== "com.sun.star.text.TextDocument")
87 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(WRITER_TEXT_FILTER
));
88 else if (aExt
== "csv")
89 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(CALC_TEXT_FILTER
));
90 else if (aExt
== "tsv")
91 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(CALC_TEXT_FILTER
));
92 else if (aExt
== "xls")
93 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(CALC_TEXT_FILTER
));
94 else if (aExt
== "txt")
95 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(WRITER_TEXT_FILTER
));
97 // No clue. Open it in Writer by default.
98 setPropValue(lDescriptor
, nFilter
, "FilterName", OUString(WRITER_TEXT_FILTER
));
109 void SAL_CALL
PlainTextFilterDetect::initialize(const uno::Sequence
<uno::Any
>& /*aArguments*/)
110 throw (uno::Exception
, uno::RuntimeException
)
114 OUString
PlainTextFilterDetect_getImplementationName()
116 return OUString("com.sun.star.comp.filters.PlainTextFilterDetect");
119 sal_Bool
PlainTextFilterDetect_supportsService(const OUString
& ServiceName
)
121 return ServiceName
== "com.sun.star.document.ExtendedTypeDetection" ||
122 ServiceName
== "com.sun.star.comp.filters.PlainTextFilterDetect";
125 uno::Sequence
<OUString
> PlainTextFilterDetect_getSupportedServiceNames()
127 uno::Sequence
<OUString
> aRet(2);
128 OUString
* pArray
= aRet
.getArray();
129 pArray
[0] = "com.sun.star.document.ExtendedTypeDetection";
130 pArray
[1] = "com.sun.star.comp.filters.PlainTextFilterDetect";
134 uno::Reference
<uno::XInterface
> PlainTextFilterDetect_createInstance(
135 const uno::Reference
<uno::XComponentContext
> & rCxt
)
137 return (cppu::OWeakObject
*) new PlainTextFilterDetect(rCxt
);
141 OUString SAL_CALL
PlainTextFilterDetect::getImplementationName()
142 throw (uno::RuntimeException
)
144 return PlainTextFilterDetect_getImplementationName();
147 sal_Bool SAL_CALL
PlainTextFilterDetect::supportsService(const OUString
& rServiceName
)
148 throw (uno::RuntimeException
)
150 return PlainTextFilterDetect_supportsService(rServiceName
);
153 uno::Sequence
<OUString
> SAL_CALL
PlainTextFilterDetect::getSupportedServiceNames()
154 throw (uno::RuntimeException
)
156 return PlainTextFilterDetect_getSupportedServiceNames();
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */