diff options
author | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-05-25 00:22:58 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-05-25 02:04:18 -0400 |
commit | 8b8e24544793f4ec6f61edb7ce8ac10bf6bb0981 (patch) | |
tree | f66cc554cb9857438f42954d992fbd52dd674efb | |
parent | 427b50c05f18de474dd5b88b705951240335beac (diff) |
Do the real detection work for plain text & forgot to add a makefile.
Change-Id: I95edb869b00764464d91e1f396da1bb648b74515
-rw-r--r-- | filter/Library_textfd.mk | 58 | ||||
-rw-r--r-- | filter/source/textfilterdetect/filterdetect.cxx | 101 |
2 files changed, 133 insertions, 26 deletions
diff --git a/filter/Library_textfd.mk b/filter/Library_textfd.mk new file mode 100644 index 000000000000..ac7303aa1905 --- /dev/null +++ b/filter/Library_textfd.mk @@ -0,0 +1,58 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +#************************************************************************* +# Version: MPL 1.1 / GPLv3+ / LGPLv3+ +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License or as specified alternatively below. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Initial Developer of the Original Code is +# Kohei Yoshida <kohei.yoshida@suse.com> +# Portions created by the Initial Developer are Copyright (C) 2011 the +# Initial Developer. All Rights Reserved. +# +# Major Contributor(s): +# +# For minor contributions see the git repository. +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 3 or later (the "GPLv3+"), or +# the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), +# in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable +# instead of those above. +#************************************************************************* + +$(eval $(call gb_Library_Library,textfd)) + +$(eval $(call gb_Library_set_componentfile,textfd,filter/source/textfilterdetect/textfd)) + +$(eval $(call gb_Library_use_api,textfd,\ + udkapi \ + offapi \ +)) + +$(eval $(call gb_Library_set_include,textfd,\ + $$(INCLUDE) \ +)) + +$(eval $(call gb_Library_use_libraries,textfd,\ + ucbhelper \ + cppuhelper \ + cppu \ + sal \ + tl \ + $(gb_STDLIBS) \ +)) + +$(eval $(call gb_Library_add_exception_objects,textfd,\ + filter/source/textfilterdetect/fdcomp \ + filter/source/textfilterdetect/filterdetect \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/filter/source/textfilterdetect/filterdetect.cxx b/filter/source/textfilterdetect/filterdetect.cxx index 55bfd0a5b223..9cb9405438f0 100644 --- a/filter/source/textfilterdetect/filterdetect.cxx +++ b/filter/source/textfilterdetect/filterdetect.cxx @@ -27,49 +27,98 @@ */ #include "filterdetect.hxx" + +#include "tools/urlobj.hxx" + #include <com/sun/star/lang/XMultiServiceFactory.hpp> +#define WRITER_TEXT_FILTER "Text" +#define CALC_TEXT_FILTER "Text - txt - csv (StarCalc)" + using namespace ::com::sun::star; +namespace { + +void setFilter(uno::Sequence<beans::PropertyValue>& rProps, sal_Int32 nPos, const rtl::OUString& rFilter) +{ + if (nPos >= 0) + rProps[nPos].Value <<= rFilter; + else + { + sal_Int32 n = rProps.getLength(); + rProps.realloc(n+1); + rProps[n].Name = "FilterName"; + rProps[n].Value <<= rFilter; + } +} + +} + PlainTextFilterDetect::PlainTextFilterDetect(const uno::Reference<lang::XMultiServiceFactory> &xMSF) : mxMSF(xMSF) {} PlainTextFilterDetect::~PlainTextFilterDetect() {} -rtl::OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyValue>& aArguments) throw (uno::RuntimeException) +rtl::OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyValue>& lDescriptor) throw (uno::RuntimeException) { + rtl::OUString aType; + rtl::OUString aDocService; + rtl::OUString aExt; + + sal_Int32 nFilter = -1; + + for (sal_Int32 i = 0, n = lDescriptor.getLength(); i < n; ++i) + { + if (lDescriptor[i].Name == "TypeName") + lDescriptor[i].Value >>= aType; + else if (lDescriptor[i].Name == "FilterName") + nFilter = i; + else if (lDescriptor[i].Name == "DocumentService") + lDescriptor[i].Value >>= aDocService; + else if (lDescriptor[i].Name == "URL") + { + rtl::OUString aURL; + lDescriptor[i].Value >>= aURL; + + // Get the file name extension. + INetURLObject aParser(aURL); + aExt = aParser.getExtension( + INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET); + aExt = aExt.toAsciiLowerCase(); + } + } + + if (aType == "calc_Text_txt_csv_StarCalc") + { + // Generic text type. Decide which filter to use based on the + // document service first, then on extension if that's not available. + + if (aDocService == "com.sun.star.sheet.SpreadsheetDocument") + // Open it in Calc. + setFilter(lDescriptor, nFilter, CALC_TEXT_FILTER); + else if (aDocService == "com.sun.star.text.TextDocument") + // Open it in Writer. + setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER); + else if (aExt == "csv") + setFilter(lDescriptor, nFilter, CALC_TEXT_FILTER); + else if (aExt == "txt") + setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER); + else + // No clue. Open it in Writer by default. + setFilter(lDescriptor, nFilter, WRITER_TEXT_FILTER); + + return aType; + } + + // failed! return rtl::OUString(); } // XInitialization -void SAL_CALL PlainTextFilterDetect::initialize(const uno::Sequence<uno::Any>& aArguments) +void SAL_CALL PlainTextFilterDetect::initialize(const uno::Sequence<uno::Any>& /*aArguments*/) throw (uno::Exception, uno::RuntimeException) { - uno::Sequence<beans::PropertyValue> aAnySeq; - sal_Int32 nLength = aArguments.getLength(); - if (nLength && (aArguments[0] >>= aAnySeq)) - { - const beans::PropertyValue * pValue = aAnySeq.getConstArray(); - for (sal_Int32 i = 0, n = aAnySeq.getLength(); i < n; ++i) - { - if (pValue[i].Name == "Type") - { - fprintf(stdout, "PlainTextFilterDetect::initialize: type = '%s'\n", - rtl::OUStringToOString(pValue[i].Value.get<rtl::OUString>(), RTL_TEXTENCODING_UTF8).getStr()); - } - else if (pValue[i].Name == "UserData") - { - fprintf(stdout, "PlainTextFilterDetect::initialize: user data = '%s'\n", - rtl::OUStringToOString(pValue[i].Value.get<rtl::OUString>(), RTL_TEXTENCODING_UTF8).getStr()); - } - else if (pValue[i].Name == "TemplateName") - { - fprintf(stdout, "PlainTextFilterDetect::initialize: template name = '%s'\n", - rtl::OUStringToOString(pValue[i].Value.get<rtl::OUString>(), RTL_TEXTENCODING_UTF8).getStr()); - } - } - } } rtl::OUString PlainTextFilterDetect_getImplementationName() |