diff options
author | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-07-27 22:14:39 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@gmail.com> | 2012-09-07 09:10:34 -0400 |
commit | a6639312005763d19e877bc61e6a844c09f5fb23 (patch) | |
tree | a955fe459e1eb82957100dd1c397ee3a31c32fdd /sc/source | |
parent | 948350d60c031904c7daffc7be95ef9c146e700c (diff) |
First cut on integrating liborcus into libreoffice tree....
It introduces the 'FilterProvider' property in the media descriptor
to optionally bypass the normal loading process and let the external
filter provider to handle the loading.
For now I'm overwriting the csv import filter just to see how this
could work just as an experiment.
Orcus still needs a lot of work, and it crashes very often at the
moment.
Change-Id: I11b34572c71073144804a7d0dd5176c8067d8deb
Diffstat (limited to 'sc/source')
-rw-r--r-- | sc/source/core/tool/orcushandler.cxx | 166 | ||||
-rw-r--r-- | sc/source/ui/docshell/docsh.cxx | 16 | ||||
-rw-r--r-- | sc/source/ui/inc/docsh.hxx | 1 |
3 files changed, 183 insertions, 0 deletions
diff --git a/sc/source/core/tool/orcushandler.cxx b/sc/source/core/tool/orcushandler.cxx new file mode 100644 index 000000000000..2fa5e890d180 --- /dev/null +++ b/sc/source/core/tool/orcushandler.cxx @@ -0,0 +1,166 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Major Contributor(s): + * Copyright (C) 2012 Kohei Yoshida <kohei.yoshida@suse.com> + * + * All Rights Reserved. + * + * 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. + */ + +#include "orcushandler.hxx" +#include "document.hxx" + +#include "tools/urlobj.hxx" + +#include <orcus/model/interface.hpp> +#include <orcus/orcus_csv.hpp> + +#include <boost/ptr_container/ptr_vector.hpp> + +using orcus::model::row_t; +using orcus::model::col_t; +using orcus::model::formula_grammar_t; + +namespace { + +class ScOrcusSheet; + +class ScOrcusFactory : public orcus::model::iface::factory +{ + ScDocument& mrDoc; + boost::ptr_vector<ScOrcusSheet> maSheets; + +public: + ScOrcusFactory(ScDocument& rDoc); + + virtual orcus::model::iface::sheet* append_sheet(const char *sheet_name, size_t sheet_name_length); + virtual orcus::model::iface::shared_strings* get_shared_strings(); + virtual orcus::model::iface::styles* get_styles(); +}; + +class ScOrcusSheet : public orcus::model::iface::sheet +{ + ScDocument& mrDoc; + SCTAB mnTab; +public: + ScOrcusSheet(ScDocument& rDoc, SCTAB nTab); + + virtual void set_auto(row_t row, col_t col, const char* p, size_t n); + virtual void set_format(row_t row, col_t col, size_t xf_index); + virtual void set_formula(row_t row, col_t col, formula_grammar_t grammar, const char* p, size_t n); + virtual void set_formula_result(row_t row, col_t col, const char* p, size_t n); + virtual void set_shared_formula( + row_t row, col_t col, formula_grammar_t grammar, size_t sindex, + const char* p_formula, size_t n_formula, const char* p_range, size_t n_range); + virtual void set_shared_formula(row_t row, col_t col, size_t sindex); + virtual void set_string(row_t row, col_t col, size_t sindex); + virtual void set_value(row_t row, col_t col, double value); +}; + +ScOrcusFactory::ScOrcusFactory(ScDocument& rDoc) : mrDoc(rDoc) {} + +orcus::model::iface::sheet* ScOrcusFactory::append_sheet(const char* sheet_name, size_t sheet_name_length) +{ + OUString aTabName(sheet_name, sheet_name_length, RTL_TEXTENCODING_UTF8); + if (!mrDoc.InsertTab(SC_TAB_APPEND, aTabName)) + return NULL; + + SCTAB nTab = mrDoc.GetTableCount() - 1; + maSheets.push_back(new ScOrcusSheet(mrDoc, nTab)); + return &maSheets.back(); +} + +orcus::model::iface::shared_strings* ScOrcusFactory::get_shared_strings() +{ + // We don't support it yet. + return NULL; +} + +orcus::model::iface::styles* ScOrcusFactory::get_styles() +{ + // We don't support it yet. + return NULL; +} + +ScOrcusSheet::ScOrcusSheet(ScDocument& rDoc, SCTAB nTab) : + mrDoc(rDoc), mnTab(nTab) {} + +void ScOrcusSheet::set_auto(row_t row, col_t col, const char* p, size_t n) +{ + OUString aVal(p, n, RTL_TEXTENCODING_UTF8); + mrDoc.SetString(col, row, mnTab, aVal); +} + +void ScOrcusSheet::set_format(row_t /*row*/, col_t /*col*/, size_t /*xf_index*/) +{ +} + +void ScOrcusSheet::set_formula( + row_t /*row*/, col_t /*col*/, formula_grammar_t /*grammar*/, const char* /*p*/, size_t /*n*/) +{ +} + +void ScOrcusSheet::set_formula_result(row_t /*row*/, col_t /*col*/, const char* /*p*/, size_t /*n*/) +{ +} + +void ScOrcusSheet::set_shared_formula( + row_t /*row*/, col_t /*col*/, formula_grammar_t /*grammar*/, size_t /*sindex*/, + const char* /*p_formula*/, size_t /*n_formula*/, const char* /*p_range*/, size_t /*n_range*/) +{ +} + +void ScOrcusSheet::set_shared_formula(row_t /*row*/, col_t /*col*/, size_t /*sindex*/) +{ +} + +void ScOrcusSheet::set_string(row_t /*row*/, col_t /*col*/, size_t /*sindex*/) +{ +} + +void ScOrcusSheet::set_value(row_t /*row*/, col_t /*col*/, double /*value*/) +{ +} + +} // anonymous namespace + +bool ScOrcusFilters::importCSV(ScDocument& rDoc, const OUString& rPath) +{ + ScOrcusFactory aFactory(rDoc); + INetURLObject aURL(rPath); + const char* path = rtl::OUStringToOString(aURL.getFSysPath(INetURLObject::FSYS_UNX), RTL_TEXTENCODING_UTF8).getStr(); + + try + { + orcus::orcus_csv filter(&aFactory); + filter.read_file(path); + } + catch (const std::exception&) + { + rDoc.InsertTab(SC_TAB_APPEND, OUString("Foo")); + rDoc.SetString(0, 0, 0, "Failed to load!!!"); + return false; + } + return true; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx index 184800397abf..94e51030b299 100644 --- a/sc/source/ui/docshell/docsh.cxx +++ b/sc/source/ui/docshell/docsh.cxx @@ -112,6 +112,8 @@ #include "cellsuno.hxx" #include "dpobject.hxx" #include "markdata.hxx" +#include "orcushandler.hxx" + #ifdef ENABLE_TELEPATHY #include "sccollaboration.hxx" #endif @@ -1497,6 +1499,20 @@ sal_Bool ScDocShell::ConvertFrom( SfxMedium& rMedium ) return bRet; } +bool ScDocShell::LoadExternal(SfxMedium& rMed, const OUString& rProvider) +{ + if (rProvider == "orcus") + { + if (!ScOrcusFilters::importCSV(aDocument, rMed.GetName())) + return false; + + FinishedLoading(SFX_LOADED_MAINDOCUMENT | SFX_LOADED_IMAGES); + return true; + } + + return false; +} + ScDocShell::PrepareSaveGuard::PrepareSaveGuard( ScDocShell& rDocShell ) : mrDocShell( rDocShell) diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx index 6b0f926403be..1efff933c57f 100644 --- a/sc/source/ui/inc/docsh.hxx +++ b/sc/source/ui/inc/docsh.hxx @@ -212,6 +212,7 @@ public: virtual sal_Bool Load( SfxMedium& rMedium ); virtual sal_Bool LoadFrom( SfxMedium& rMedium ); virtual sal_Bool ConvertFrom( SfxMedium &rMedium ); + virtual bool LoadExternal(SfxMedium& rMedium, const rtl::OUString& rProvider); virtual sal_Bool Save(); virtual sal_Bool SaveAs( SfxMedium& rMedium ); virtual sal_Bool ConvertTo( SfxMedium &rMedium ); |