Bump for 4.0-15
[LibreOffice.git] / filter / source / textfilterdetect / filterdetect.cxx
blob7dd2ddd76cf07c5d2460aa7a0a3c7afdb2293bc7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * Major Contributor(s):
16 * Copyright (C) 2012 Kohei Yoshida <kohei.yoshida@suse.com>
18 * All Rights Reserved.
20 * For minor contributions see the git repository.
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 * instead of those above.
29 #include "filterdetect.hxx"
31 #include "tools/urlobj.hxx"
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #define WRITER_TEXT_FILTER "Text"
36 #define CALC_TEXT_FILTER "Text - txt - csv (StarCalc)"
38 using namespace ::com::sun::star;
40 namespace {
42 void setFilter(uno::Sequence<beans::PropertyValue>& rProps, sal_Int32 nPos, const rtl::OUString& rFilter)
44 if (nPos >= 0)
45 rProps[nPos].Value <<= rFilter;
46 else
48 sal_Int32 n = rProps.getLength();
49 rProps.realloc(n+1);
50 rProps[n].Name = "FilterName";
51 rProps[n].Value <<= rFilter;
57 PlainTextFilterDetect::PlainTextFilterDetect(const uno::Reference<lang::XMultiServiceFactory> &xMSF) :
58 mxMSF(xMSF) {}
60 PlainTextFilterDetect::~PlainTextFilterDetect() {}
62 rtl::OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyValue>& lDescriptor) throw (uno::RuntimeException)
64 rtl::OUString aType;
65 rtl::OUString aDocService;
66 rtl::OUString aExt;
68 sal_Int32 nFilter = -1;
70 for (sal_Int32 i = 0, n = lDescriptor.getLength(); i < n; ++i)
72 if (lDescriptor[i].Name == "TypeName")
73 lDescriptor[i].Value >>= aType;
74 else if (lDescriptor[i].Name == "FilterName")
75 nFilter = i;
76 else if (lDescriptor[i].Name == "DocumentService")
77 lDescriptor[i].Value >>= aDocService;
78 else if (lDescriptor[i].Name == "URL")
80 rtl::OUString aURL;
81 lDescriptor[i].Value >>= aURL;
83 // Get the file name extension.
84 INetURLObject aParser(aURL);
85 aExt = aParser.getExtension(
86 INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET);
87 aExt = aExt.toAsciiLowerCase();
91 if (aType == "generic_Text")
93 // Generic text type. Decide which filter to use based on the
94 // document service first, then on extension if that's not available.
96 if (aDocService == "com.sun.star.sheet.SpreadsheetDocument")
97 // Open it in Calc.
98 setFilter(lDescriptor, nFilter, CALC_TEXT_FILTER);
99 else if (aDocService == "com.sun.star.text.TextDocument")
100 // Open it in Writer.
101 setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER);
102 else if (aExt == "csv")
103 setFilter(lDescriptor, nFilter, CALC_TEXT_FILTER);
104 else if (aExt == "txt")
105 setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER);
106 else
107 // No clue. Open it in Writer by default.
108 setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER);
110 return aType;
113 // failed!
114 return rtl::OUString();
117 // XInitialization
119 void SAL_CALL PlainTextFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
120 throw (uno::Exception, uno::RuntimeException)
124 rtl::OUString PlainTextFilterDetect_getImplementationName()
126 return rtl::OUString("com.sun.star.comp.filters.PlainTextFilterDetect");
129 sal_Bool PlainTextFilterDetect_supportsService(const rtl::OUString& ServiceName)
131 return ServiceName == "com.sun.star.document.ExtendedTypeDetection" ||
132 ServiceName == "com.sun.star.comp.filters.PlainTextFilterDetect";
135 uno::Sequence<rtl::OUString> PlainTextFilterDetect_getSupportedServiceNames()
137 uno::Sequence<rtl::OUString> aRet(2);
138 rtl::OUString* pArray = aRet.getArray();
139 pArray[0] = "com.sun.star.document.ExtendedTypeDetection";
140 pArray[1] = "com.sun.star.comp.filters.PlainTextFilterDetect";
141 return aRet;
144 uno::Reference<uno::XInterface> PlainTextFilterDetect_createInstance(
145 const uno::Reference<lang::XMultiServiceFactory> & rSMgr)
147 return (cppu::OWeakObject*) new PlainTextFilterDetect(rSMgr);
150 // XServiceInfo
151 rtl::OUString SAL_CALL PlainTextFilterDetect::getImplementationName()
152 throw (uno::RuntimeException)
154 return PlainTextFilterDetect_getImplementationName();
157 sal_Bool SAL_CALL PlainTextFilterDetect::supportsService(const rtl::OUString& rServiceName)
158 throw (uno::RuntimeException)
160 return PlainTextFilterDetect_supportsService(rServiceName);
163 uno::Sequence<rtl::OUString> SAL_CALL PlainTextFilterDetect::getSupportedServiceNames()
164 throw (uno::RuntimeException)
166 return PlainTextFilterDetect_getSupportedServiceNames();
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */