fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / filter / source / textfilterdetect / filterdetect.cxx
blob360d8d460730f0342a9bd371c61a5ff48a02daf1
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/.
8 */
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;
23 namespace {
25 template<typename T>
26 void setPropValue(uno::Sequence<beans::PropertyValue>& rProps, sal_Int32 nPos, const char* pName, const T& rValue)
28 if (nPos >= 0)
29 rProps[nPos].Value <<= rValue;
30 else
32 sal_Int32 n = rProps.getLength();
33 rProps.realloc(n+1);
34 rProps[n].Name = OUString::createFromAscii(pName);
35 rProps[n].Value <<= rValue;
41 PlainTextFilterDetect::PlainTextFilterDetect(const uno::Reference<uno::XComponentContext>& xCxt) :
42 mxCxt(xCxt) {}
44 PlainTextFilterDetect::~PlainTextFilterDetect() {}
46 OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyValue>& lDescriptor) throw (uno::RuntimeException)
48 OUString aType;
49 OUString aDocService;
50 OUString aExt;
51 OUString aUrl;
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")
60 nFilter = i;
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")
77 // Generic text type.
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")
83 // Open it in Calc.
84 setPropValue(lDescriptor, nFilter, "FilterName", OUString(CALC_TEXT_FILTER));
85 else if (aDocService == "com.sun.star.text.TextDocument")
86 // Open it in Writer.
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));
96 else
97 // No clue. Open it in Writer by default.
98 setPropValue(lDescriptor, nFilter, "FilterName", OUString(WRITER_TEXT_FILTER));
100 return aType;
103 // failed!
104 return OUString();
107 // XInitialization
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";
131 return aRet;
134 uno::Reference<uno::XInterface> PlainTextFilterDetect_createInstance(
135 const uno::Reference<uno::XComponentContext> & rCxt)
137 return (cppu::OWeakObject*) new PlainTextFilterDetect(rCxt);
140 // XServiceInfo
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: */