/src/libreoffice/sc/source/ui/inc/dataprovider.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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 | | */ |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #include <memory> |
13 | | #include <string_view> |
14 | | #include <salhelper/thread.hxx> |
15 | | #include <rtl/ustring.hxx> |
16 | | #include <rtl/ref.hxx> |
17 | | #include <osl/mutex.hxx> |
18 | | #include <document.hxx> |
19 | | |
20 | | #include <rtl/strbuf.hxx> |
21 | | |
22 | | #include <atomic> |
23 | | #include <vector> |
24 | | //#include <map> |
25 | | |
26 | | #include <orcus/csv_parser.hpp> |
27 | | |
28 | | class SvStream; |
29 | | class ScDBData; |
30 | | |
31 | | namespace sc { |
32 | | |
33 | | class DataTransformation; |
34 | | class ExternalDataSource; |
35 | | |
36 | | class CSVFetchThread : public salhelper::Thread |
37 | | { |
38 | | ScDocument& mrDocument; |
39 | | OUString maURL; |
40 | | |
41 | | std::atomic<bool> mbTerminate; |
42 | | |
43 | | orcus::csv::parser_config maConfig; |
44 | | |
45 | | std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations; |
46 | | |
47 | | std::function<void()> maImportFinishedHdl; |
48 | | |
49 | | std::exception_ptr mpLastException; |
50 | | bool mbIsParseError; |
51 | | |
52 | | |
53 | | public: |
54 | | CSVFetchThread(ScDocument& rDoc, OUString , std::function<void()> aImportFinishedHdl, |
55 | | std::vector<std::shared_ptr<sc::DataTransformation>>&& mrDataTransformations); |
56 | | virtual ~CSVFetchThread() override; |
57 | | |
58 | | void RequestTerminate(); |
59 | | bool IsRequestedTerminate(); |
60 | | void Terminate(); |
61 | | void EndThread(); |
62 | 0 | bool IsParseError() { return mbIsParseError; } |
63 | 0 | auto GetLastException() { return mpLastException; } |
64 | | |
65 | | virtual void execute() override; |
66 | | }; |
67 | | |
68 | | /** |
69 | | * Abstract class for all data provider. |
70 | | * |
71 | | */ |
72 | | class DataProvider |
73 | | { |
74 | | protected: |
75 | | /** |
76 | | * If true make the threaded import deterministic for the tests. |
77 | | */ |
78 | | bool mbDeterministic; |
79 | | sc::ExternalDataSource& mrDataSource; |
80 | | |
81 | | public: |
82 | | DataProvider(sc::ExternalDataSource& rDataSource); |
83 | | |
84 | | virtual ~DataProvider(); |
85 | | |
86 | | virtual void Import() = 0; |
87 | | |
88 | | virtual const OUString& GetURL() const = 0; |
89 | | |
90 | | static std::unique_ptr<SvStream> FetchStreamFromURL(const OUString&, OStringBuffer& rBuffer); |
91 | | |
92 | | void setDeterministic(); |
93 | | }; |
94 | | |
95 | | class CSVDataProvider : public DataProvider |
96 | | { |
97 | | rtl::Reference<CSVFetchThread> mxCSVFetchThread; |
98 | | ScDocument* mpDocument; |
99 | | ScDocumentUniquePtr mpDoc; |
100 | | |
101 | | void Refresh(); |
102 | | |
103 | | public: |
104 | | CSVDataProvider (ScDocument* pDoc, sc::ExternalDataSource& rDataSource); |
105 | | virtual ~CSVDataProvider() override; |
106 | | |
107 | | virtual void Import() override; |
108 | | |
109 | | const OUString& GetURL() const override; |
110 | | void ImportFinished(); |
111 | | }; |
112 | | |
113 | | /** |
114 | | * This class handles the copying of the data from the imported |
115 | | * temporary document to the actual document. Additionally, in the future |
116 | | * we may decide to store data transformations in this class. |
117 | | * |
118 | | * In addition this class also handles how to deal with excess data by for example extending the ScDBData or by only showing the first or last entries. |
119 | | * |
120 | | * TODO: move the DataProvider::WriteToDoc here |
121 | | * |
122 | | */ |
123 | | class ScDBDataManager |
124 | | { |
125 | | OUString maDBName; |
126 | | ScDocument* mpDoc; |
127 | | |
128 | | public: |
129 | | ScDBDataManager(OUString aDBName, ScDocument* pDoc); |
130 | | ~ScDBDataManager(); |
131 | | |
132 | | void SetDatabase(const OUString& rDBName); |
133 | | |
134 | | ScDBData* getDBData(); |
135 | | |
136 | | void WriteToDoc(ScDocument& rDoc); |
137 | | }; |
138 | | |
139 | | class DataProviderFactory |
140 | | { |
141 | | private: |
142 | | |
143 | | static bool isInternalDataProvider(std::u16string_view rProvider); |
144 | | |
145 | | public: |
146 | | |
147 | | static std::shared_ptr<DataProvider> getDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource); |
148 | | |
149 | | }; |
150 | | |
151 | | } |
152 | | |
153 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |