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