Debian Codegraph @

src/file/ApvlvLibreOffice.cc

raw
1 /*
2 * This file is part of the apvlv package
3 * Copyright (C) <2010> <Alf>
4 *
5 * Contact: Alf <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22 /* @CPPFILE ApvlvOffice.cc
23 *
24 * Author: Alf <[email protected]>
25 */
26
27 #include <LibreOfficeKit/LibreOfficeKit.hxx>
28 #include <QDebug>
29 #include <QDir>
30 #include <QTemporaryFile>
31 #include <filesystem>
32
33 #include "ApvlvLibreOffice.h"
34
35 namespace apvlv
36 {
37 FILE_TYPE_DEFINITION ("libreOffice", ApvlvOFFICE,
38 { ".doc", ".docx", ".xls", ".xlsx", "ppt", "pptx" });
39
40 using namespace std;
41
42 mutex ApvlvOFFICE::mLokMutex;
43 TokenDispatcher ApvlvOFFICE::mDispatcher{ 1, true };
44 unique_ptr<lok::Office> ApvlvOFFICE::mOffice;
45
46 bool
47 ApvlvOFFICE::load (const string &filename)
48 {
49 auto filepath = filesystem::path (filename);
50 if (filepath.filename ().string ().starts_with ("~"))
51 {
52 qWarning () << "filename: " << QString::fromLocal8Bit (filename)
53 << " maybe a temporary file, skip.";
54 return false;
55 }
56
57 auto token = mDispatcher.getToken (true);
58
59 initLokInstance ();
60
61 lock_guard<mutex> lk (mLokMutex);
62 mDoc
63 = unique_ptr<lok::Document>{ mOffice->documentLoad (filename.c_str ()) };
64 if (mDoc == nullptr)
65 return false;
66
67 mDoc->initializeForRendering ();
68 return true;
69 }
70
71 ApvlvOFFICE::~ApvlvOFFICE ()
72 {
73 auto token = mDispatcher.getToken (true);
74 lock_guard<mutex> lk (mLokMutex);
75 mDoc = nullptr;
76 }
77
78 int
79 ApvlvOFFICE::sum ()
80 {
81 auto token = mDispatcher.getToken (true);
82 lock_guard<mutex> lk (mLokMutex);
83 auto num = mDoc->getParts ();
84 return num;
85 }
86
87 bool
88 ApvlvOFFICE::pageText (int pn, const Rectangle &rect, string &text)
89 {
90 auto tmpname = QString ("%1/apvlv.%2.txt")
91 .arg (QDir::temp ().path ())
92 .arg (random ());
93
94 auto token = mDispatcher.getToken (false);
95 unique_lock<mutex> lk (mLokMutex);
96 mDoc->setPart (pn);
97 mDoc->saveAs (tmpname.toStdString ().c_str (), "txt");
98 lk.unlock ();
99 token.reset ();
100
101 QFile file (tmpname);
102 if (file.open (QIODeviceBase::ReadOnly) == false)
103 return false;
104
105 auto bytes = file.readAll ();
106 text.append (bytes.toStdString ());
107 file.close ();
108 file.remove ();
109 return true;
110 }
111
112 bool
113 ApvlvOFFICE::pageRenderToImage (int pn, double zm, int rot, QImage *pix)
114 {
115 auto token = mDispatcher.getToken (true);
116 lock_guard<mutex> lk (mLokMutex);
117 mDoc->setPart (pn);
118 QTemporaryFile file;
119 if (file.open ())
120 {
121 mDoc->saveAs (file.fileName ().toStdString ().c_str (), "png");
122 *pix = QImage (file.fileName (), "png");
123 file.close ();
124 }
125 return true;
126 }
127
128 void
129 ApvlvOFFICE::initLokInstance ()
130 {
131 lock_guard<mutex> lk (mLokMutex);
132 if (mOffice == nullptr)
133 {
134 auto lok_path = ApvlvParams::instance ()->getStringOrDefault (
135 "lok_path", DEFAULT_LOK_PATH);
136 mOffice
137 = unique_ptr<lok::Office>{ lok::lok_cpp_init (lok_path.c_str ()) };
138 }
139 }
140
141 }
142
143 // Local Variables:
144 // mode: c++
145 // End: