Debian Codegraph @

src/file/ApvlvAxOffice.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 <Ole2.h>
28 #include <OleCtl.h>
29 #include <QPdfDocument>
30 #include <Shlwapi.h>
31 #include <Windows.h>
32 #include <ocidl.h>
33
34 #include "ApvlvAxOffice.h"
35 #include "ApvlvUtil.h"
36 #include "ApvlvWebViewWidget.h"
37
38 namespace apvlv
39 {
40 using namespace std;
41
42 FILE_TYPE_DEFINITION ("MSOffice", ApvlvOfficeWord, { ".doc", ".docx" });
43 FILE_TYPE_DEFINITION ("MSOffice", ApvlvPowerPoint, { ".ppt", ".pptx" });
44 FILE_TYPE_DEFINITION ("MSOffice", ApvlvExcel, { ".xls", ".xlsx" });
45
46 bool
47 ApvlvOfficeWord::load (const string &filename)
48 {
49 auto qname = QString::fromLocal8Bit (filename);
50 mApp = new QAxWidget ("Word.Application");
51 mApp->setProperty ("Visible", false);
52 mDocs = mApp->querySubObject ("Documents");
53 mDoc = mDocs->querySubObject (
54 "OpenNoRepairDialog(const QString&, bool, bool, bool)", qname, false,
55 true, false);
56 if (mDoc == nullptr)
57 {
58 mApp->dynamicCall ("Quit()");
59 delete mApp;
60 return false;
61 }
62 return true;
63 }
64
65 int
66 ApvlvOfficeWord::sum ()
67 {
68 auto content = mDoc->querySubObject ("Content");
69 auto pages = content->dynamicCall ("Information(wdNumberOfPagesInDocument)");
70 return pages.toInt ();
71 }
72
73 SizeF
74 ApvlvOfficeWord::pageSizeF (int pn, int rot)
75 {
76 auto pnstr = QString ("Pages(%1)").arg (pn + 1);
77 auto win = mDoc->querySubObject ("ActiveWindow");
78 auto pane = win->querySubObject ("ActivePane");
79 auto page = pane->querySubObject (pnstr.toStdString ().c_str ());
80 auto width = page->property ("Width");
81 auto height = page->property ("Height");
82 return { width.toDouble (), height.toDouble () };
83 }
84
85 bool
86 ApvlvOfficeWord::pageText (int pn, const Rectangle &rect, string &text)
87 {
88 auto content = mDoc->querySubObject ("Content");
89 return false;
90 }
91
92 bool
93 ApvlvOfficeWord::pageRenderToImage (int pn, double zm, int rot, QImage *pix)
94 {
95 char szFormatName[1024];
96 const char *lpFormatName;
97 static UINT auPriorityList[] = { CF_TEXT, CF_BITMAP };
98
99 auto gf = GetPriorityClipboardFormat (auPriorityList, 2);
100
101 auto pnstr = QString ("Pages(%1)").arg (pn + 1);
102 auto win = mDoc->querySubObject ("ActiveWindow");
103 auto pane = win->querySubObject ("ActivePane");
104 auto page = pane->querySubObject (pnstr.toStdString ().c_str ());
105
106 auto selection = mApp->querySubObject ("Selection");
107 selection->dynamicCall ("GoTo(int, int, int, const QVariant&)", 1, 1, 1,
108 page->property ("Start"));
109 selection->dynamicCall ("MoveDown(int, int, int)", 5, 1, 0);
110 selection->dynamicCall ("EndKey(int, int)", 6, 1);
111 selection->dynamicCall ("CopyAsPicture()");
112
113 Sleep (1000);
114 auto clip = QApplication::clipboard ();
115 auto mime = clip->mimeData ();
116 for (auto const &t : mime->formats ())
117 {
118 qDebug () << "clipboard contains: " << t;
119 }
120
121 // HWND hWnd = (HWND)mApp->winId ();
122 if (OpenClipboard (NULL) == FALSE)
123 {
124 qDebug ("open clipboard error: %d\n", GetLastError ());
125 return false;
126 }
127
128 auto uFormat = EnumClipboardFormats (0);
129 while (uFormat)
130 {
131 if (GetClipboardFormatNameA (uFormat, szFormatName,
132 sizeof (szFormatName)))
133 lpFormatName = szFormatName;
134 else
135 lpFormatName = "(unknown)";
136 qDebug ("get file: %s\n", lpFormatName);
137 uFormat = EnumClipboardFormats (uFormat);
138 }
139
140 if (IsClipboardFormatAvailable (CF_BITMAP) == FALSE)
141 {
142 qDebug ("no picture: %d\n", GetLastError ());
143 CloseClipboard ();
144 return false;
145 }
146
147 HBITMAP hBitmap = static_cast<HBITMAP> (GetClipboardData (CF_BITMAP));
148 if (hBitmap == nullptr)
149 {
150 qDebug ("no image: %d\n", GetLastError ());
151 CloseClipboard ();
152 return false;
153 }
154 IPicture *iPicture = nullptr;
155 auto hr = OleCreatePictureIndirect (reinterpret_cast<LPPICTDESC> (hBitmap),
156 IID_IPicture, TRUE, (void **)&iPicture);
157 if (FAILED (hr))
158 {
159 qDebug ("failed create: %d\n", GetLastError ());
160 CloseClipboard ();
161 return false;
162 }
163 LPSTREAM stream = NULL;
164 hr = SHCreateStreamOnFileA ("z:\\a.bmp", STGM_CREATE | STGM_WRITE, &stream);
165 if (FAILED (hr))
166 {
167 iPicture->Release ();
168 CloseClipboard ();
169 return false;
170 }
171
172 hr = iPicture->SaveAsFile (stream, FALSE, NULL);
173 iPicture->Release ();
174 CloseClipboard ();
175 if (FAILED (hr))
176 {
177 qDebug ("save failed\n");
178 return false;
179 }
180
181 return true;
182 }
183
184 bool
185 ApvlvOfficeWord::pageRenderToWebView (int pn, double zm, int rot,
186 WebView *webview)
187 {
188 webview->setZoomFactor (zm);
189 QUrl url = QString ("apvlv:///%1").arg (pn);
190 webview->load (url);
191 return true;
192 }
193
194 optional<QByteArray>
195 ApvlvOfficeWord::pathContent (const string &path)
196 {
197 auto pn = QString::fromLocal8Bit (path).toInt ();
198 auto pageRange = mDoc->querySubObject (
199 "GoTo(int, int, int, const QVariant&)", 1, 1, pn + 1);
200 auto endRange = mDoc->querySubObject ("GoTo(int, int, int, const QVariant&)",
201 1, 1, pn + 2);
202 if (pageRange && endRange)
203 {
204 int endPosition;
205 if (endRange->property ("Start").toInt () == 1)
206 {
207 auto content = mDoc->querySubObject ("Content");
208 endPosition = content->property ("End").toInt ();
209 delete content;
210 }
211 else
212 {
213 endPosition = endRange->property ("Start").toInt () - 1;
214 }
215
216 pageRange->setProperty ("End", endPosition);
217
218 pageRange->dynamicCall ("Copy()");
219 }
220 else
221 {
222 qWarning () << "Failed to get page range";
223 return nullptr;
224 }
225
226 auto clip = QApplication::clipboard ();
227 auto mime = clip->mimeData ();
228 return QByteArray::fromStdString (mime->html ().toStdString ());
229 }
230
231 bool
232 ApvlvPowerPoint::load (const string &filename)
233 {
234 auto qname = QString::fromLocal8Bit (filename);
235 mApp = new QAxWidget ("PowerPoint.Application");
236 mApp->setProperty ("Visible", false);
237 mDocs = mApp->querySubObject ("Presentations");
238 mDoc = mDocs->querySubObject ("Open(const QString&, bool, bool, bool)",
239 qname, true, false, false);
240 if (mDoc == nullptr)
241 {
242 mApp->dynamicCall ("Quit()");
243 delete mApp;
244 return false;
245 }
246 return true;
247 }
248
249 int
250 ApvlvPowerPoint::sum ()
251 {
252 auto slides = mDoc->querySubObject ("Slides");
253 auto count = slides->property ("Count");
254 return count.toInt ();
255 }
256
257 SizeF
258 ApvlvPowerPoint::pageSizeF (int pn, int rot)
259 {
260 auto page = mDoc->querySubObject ("PageSetup");
261 auto width = page->property ("SlideWidth");
262 auto height = page->property ("SlideHeight");
263 return { width.toDouble (), height.toDouble () };
264 }
265
266 bool
267 ApvlvPowerPoint::pageText (int pn, const Rectangle &rect, string &text)
268 {
269 auto content = mDoc->querySubObject ("Content");
270 return false;
271 }
272
273 bool
274 ApvlvPowerPoint::pageRenderToImage (int pn, double zm, int rot, QImage *pix)
275 {
276 auto slides = mDoc->querySubObject ("Slides");
277 auto slide = slides->querySubObject ("Item(int)", pn + 1);
278 auto temppath
279 = QString ("%1/apvlv_%2.png").arg (QDir::tempPath ()).arg (rand ());
280 temppath.replace ("/", "\\");
281 slide->dynamicCall ("Export(const QString &, const QString &)", temppath,
282 QString ("PNG"));
283
284 if (QFile::exists (temppath) == false)
285 {
286 return false;
287 }
288 *pix = QImage (temppath);
289 QFile::remove (temppath);
290 return true;
291 }
292
293 ExcelWidget *
294 ApvlvExcel::getWidget ()
295 {
296 auto wid = new ExcelWidget ();
297 wid->setFile (this);
298 return wid;
299 }
300
301 void
302 ExcelWidget::setFile (File *file)
303 {
304 mFile = file;
305 auto qname = QString::fromLocal8Bit (mFile->getFilename ());
306 mAxWidget.setControl (qname);
307 }
308
309 void
310 ExcelWidget::showPage (int p, double s)
311 {
312 auto sheets = mAxWidget.querySubObject ("Sheets");
313 auto sheet = sheets->querySubObject ("Item(int)", p + 1);
314 sheet->dynamicCall ("Activate()");
315 mPageNumber = p;
316 mScrollValue = s;
317 }
318
319 void
320 ExcelWidget::showPage (int p, const string &anchor)
321 {
322 showPage (p, 0.0);
323 mPageNumber = p;
324 mScrollValue = 0;
325 }
326
327 bool
328 ApvlvExcel::load (const string &filename)
329 {
330 auto qname = QString::fromLocal8Bit (filename);
331 mApp = new QAxWidget ("Excel.Workbook");
332 mApp->setProperty ("Visible", false);
333 mApp->setProperty ("ReadOnly", true);
334 mApp->setControl (qname);
335 mDoc = nullptr;
336 return true;
337 }
338
339 int
340 ApvlvExcel::sum ()
341 {
342 auto sheets = mApp->querySubObject ("Sheets");
343 auto count = sheets->property ("Count");
344 return count.toInt ();
345 }
346
347 bool
348 ApvlvExcel::pageText (int pn, const Rectangle &rect, string &text)
349 {
350 return false;
351 // auto content = mDoc->querySubObject ("Content");
352 }
353
354 }
355
356 // Local Variables:
357 // mode: c++
358 // End: