LibreOffice Module cui (master)  1
QrCodeGenDialog.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 #include <QrCodeGenDialog.hxx>
11 
13 #include <tools/stream.hxx>
14 #include <dialmgr.hxx>
15 #include <strings.hrc>
16 #include <unotools/streamwrap.hxx>
17 #include <utility>
18 #include <vcl/svapp.hxx>
19 
20 #if defined(SYSTEM_QRCODEGEN)
21 #include <qrcodegen/QrCode.hpp>
22 #else
23 #include <QrCode.hpp>
24 #endif
25 
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 #include <com/sun/star/drawing/XShape.hpp>
29 #include <com/sun/star/graphic/GraphicProvider.hpp>
30 #include <com/sun/star/graphic/XGraphic.hpp>
31 #include <com/sun/star/drawing/QRCode.hpp>
32 #include <com/sun/star/drawing/QRCodeErrorCorrection.hpp>
33 #include <com/sun/star/graphic/XGraphicProvider.hpp>
34 #include <com/sun/star/io/XInputStream.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/sheet/XSpreadsheet.hpp>
38 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
39 #include <com/sun/star/text/TextContentAnchorType.hpp>
40 #include <com/sun/star/text/XTextContent.hpp>
41 #include <com/sun/star/text/XTextViewCursor.hpp>
42 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
43 #include <config_fuzzers.h>
44 #include <com/sun/star/drawing/XDrawView.hpp>
45 #include <com/sun/star/drawing/XDrawPage.hpp>
46 
47 using namespace css;
48 using namespace css::uno;
49 using namespace css::beans;
50 using namespace css::container;
51 using namespace css::frame;
52 using namespace css::io;
53 using namespace css::lang;
54 using namespace css::frame;
55 using namespace css::sheet;
56 using namespace css::text;
57 using namespace css::drawing;
58 using namespace css::graphic;
59 using namespace qrcodegen;
60 
61 QrCodeGenDialog::QrCodeGenDialog(weld::Widget* pParent, Reference<XModel> xModel,
62  bool bEditExisting)
63  : GenericDialogController(pParent, "cui/ui/qrcodegen.ui", "QrCodeGenDialog")
64  , m_xModel(std::move(xModel))
65  , m_xEdittext(m_xBuilder->weld_entry("edit_text"))
66  , m_xECC{ m_xBuilder->weld_radio_button("button_low"),
67  m_xBuilder->weld_radio_button("button_medium"),
68  m_xBuilder->weld_radio_button("button_quartile"),
69  m_xBuilder->weld_radio_button("button_high") }
70  , m_xSpinBorder(m_xBuilder->weld_spin_button("edit_border"))
71  , mpParent(pParent)
72 {
73  if (!bEditExisting)
74  {
75  // TODO: This only works in Writer doc. Should also work in shapes
76  Reference<XIndexAccess> xSelections(m_xModel->getCurrentSelection(), UNO_QUERY);
77  if (xSelections.is())
78  {
79  Reference<XTextRange> xSelection(xSelections->getByIndex(0), UNO_QUERY);
80  if (xSelection.is())
81  m_xEdittext->set_text(xSelection->getString());
82  }
83  return;
84  }
85 
86  Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
87  UNO_QUERY_THROW);
88  Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
89 
90  // Read properties from selected QR Code
91  css::drawing::QRCode aQRCode;
92  xProps->getPropertyValue("QRCodeProperties") >>= aQRCode;
93 
94  m_xEdittext->set_text(aQRCode.Payload);
95 
96  //Get Error Correction Constant from selected QR Code
97  GetErrorCorrection(aQRCode.ErrorCorrection);
98 
99  m_xSpinBorder->set_value(aQRCode.Border);
100 
101  // Mark this as existing shape
102  m_xExistingShapeProperties = xProps;
103 }
104 
106 {
107  short nRet;
108  while (true)
109  {
110  nRet = GenericDialogController::run();
111  if (nRet == RET_OK)
112  {
113  try
114  {
115  Apply();
116  break;
117  }
118  catch (const qrcodegen::data_too_long&)
119  {
120  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
121  mpParent, VclMessageType::Warning, VclButtonsType::Ok,
122  CuiResId(RID_SVXSTR_QRCODEDATALONG)));
123  xBox->run();
124  }
125  }
126  else
127  break;
128  }
129  return nRet;
130 }
131 
133 {
134  css::drawing::QRCode aQRCode;
135  aQRCode.Payload = m_xEdittext->get_text();
136 
137  bool bLowECCActive(m_xECC[0]->get_active());
138  bool bMediumECCActive(m_xECC[1]->get_active());
139  bool bQuartileECCActive(m_xECC[2]->get_active());
140 
141  if (bLowECCActive)
142  {
143  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::LOW;
144  }
145  else if (bMediumECCActive)
146  {
147  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::MEDIUM;
148  }
149  else if (bQuartileECCActive)
150  {
151  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::QUARTILE;
152  }
153  else
154  {
155  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::HIGH;
156  }
157 
158  aQRCode.Border = m_xSpinBorder->get_value();
159 
160  // Read svg and replace placeholder texts
161  OUString aSvgImage = GenerateQRCode(aQRCode.Payload, aQRCode.ErrorCorrection, aQRCode.Border);
162 
163  // Insert/Update graphic
164  SvMemoryStream aSvgStream(4096, 4096);
165  aSvgStream.WriteOString(OUStringToOString(aSvgImage, RTL_TEXTENCODING_UTF8));
166  Reference<XInputStream> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream));
168  Reference<XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(xContext);
169 
170  Sequence<PropertyValue> aMediaProperties(1);
171  aMediaProperties[0].Name = "InputStream";
172  aMediaProperties[0].Value <<= xInputStream;
173  Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
174 
175  bool bIsExistingQRCode = m_xExistingShapeProperties.is();
176  Reference<XPropertySet> xShapeProps;
177  if (bIsExistingQRCode)
178  xShapeProps = m_xExistingShapeProperties;
179  else
180  xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
181  ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
182  UNO_QUERY);
183 
184  xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
185 
186  // Set QRCode properties
187  xShapeProps->setPropertyValue("QRCodeProperties", Any(aQRCode));
188 
189  if (!bIsExistingQRCode)
190  {
191  // Default size
192  Reference<XShape> xShape(xShapeProps, UNO_QUERY);
193  awt::Size aShapeSize;
194  aShapeSize.Height = 4000;
195  aShapeSize.Width = 4000;
196  xShape->setSize(aShapeSize);
197 
198  // Default anchoring
199  xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));
200 
201  const Reference<XServiceInfo> xServiceInfo(m_xModel, UNO_QUERY_THROW);
202 
203  // Writer
204  if (xServiceInfo->supportsService("com.sun.star.text.TextDocument"))
205  {
206  Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
207  Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
208  UNO_QUERY_THROW);
209  Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
210  // use cursor's XText - it might be in table cell, frame, ...
211  Reference<XText> const xText(xCursor->getText());
212  assert(xText.is());
213  xText->insertTextContent(xCursor, xTextContent, true);
214  return;
215  }
216 
217  // Calc
218  else if (xServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
219  {
220  Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
221  awt::Point aCellPosition;
222  xSheetCell->getPropertyValue("Position") >>= aCellPosition;
223  xShape->setPosition(aCellPosition);
224 
225  Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
226  Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
227  Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
228  Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
229  Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
230 
231  xShapes->add(xShape);
232  return;
233  }
234 
235  //Impress and Draw
236  else if (xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument")
237  || xServiceInfo->supportsService("com.sun.star.drawing.DrawingDocument"))
238  {
239  Reference<XDrawView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
240  Reference<XDrawPage> xPage(xView->getCurrentPage(), UNO_SET_THROW);
241  Reference<XShapes> xShapes(xPage, UNO_QUERY_THROW);
242 
243  xShapes->add(xShape);
244  return;
245  }
246 
247  else
248  {
249  //Not implemented for math,base and other apps.
250  throw uno::RuntimeException("Not implemented");
251  }
252  }
253 }
254 
255 OUString QrCodeGenDialog::GenerateQRCode(OUString aQRText, long aQRECC, int aQRBorder)
256 {
257 #if ENABLE_FUZZERS
258  return OUString();
259 #else
260  //Select ECC:: value from aQrECC
261  qrcodegen::QrCode::Ecc bqrEcc = qrcodegen::QrCode::Ecc::LOW;
262 
263  switch (aQRECC)
264  {
265  case 1:
266  {
267  bqrEcc = qrcodegen::QrCode::Ecc::LOW;
268  break;
269  }
270  case 2:
271  {
272  bqrEcc = qrcodegen::QrCode::Ecc::MEDIUM;
273  break;
274  }
275  case 3:
276  {
277  bqrEcc = qrcodegen::QrCode::Ecc::QUARTILE;
278  break;
279  }
280  case 4:
281  {
282  bqrEcc = qrcodegen::QrCode::Ecc::HIGH;
283  break;
284  }
285  }
286 
287  //OuString to char* qrtext
288  OString o = OUStringToOString(aQRText, RTL_TEXTENCODING_UTF8);
289  const char* qrtext = o.pData->buffer;
290 
291  // From QR Code library
292  qrcodegen::QrCode qr0 = qrcodegen::QrCode::encodeText(qrtext, bqrEcc);
293  std::string svg = qr0.toSvgString(aQRBorder);
294  //cstring to OUString
295  return OUString::createFromAscii(svg.c_str());
296 #endif
297 }
298 
299 void QrCodeGenDialog::GetErrorCorrection(long ErrorCorrection)
300 {
301  switch (ErrorCorrection)
302  {
303  case css::drawing::QRCodeErrorCorrection::LOW:
304  {
305  m_xECC[0]->set_active(true);
306  break;
307  }
308  case css::drawing::QRCodeErrorCorrection::MEDIUM:
309  {
310  m_xECC[1]->set_active(true);
311  break;
312  }
313  case css::drawing::QRCodeErrorCorrection::QUARTILE:
314  {
315  m_xECC[2]->set_active(true);
316  break;
317  }
318  case css::drawing::QRCodeErrorCorrection::HIGH:
319  {
320  m_xECC[3]->set_active(true);
321  break;
322  }
323  }
324 }
325 
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Reference< frame::XModel > m_xModel
std::unique_ptr< weld::Builder > m_xBuilder
static OUString GenerateQRCode(OUString aQrText, long aQrECC, int aQrBorder)
std::unique_ptr< weld::RadioButton > m_xECC[4]
SvStream & WriteOString(const OString &rStr)
weld::Widget * mpParent
QrCodeGenDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel, bool bEditExisting)
std::unique_ptr< weld::Entry > m_xEdittext
css::uno::Reference< css::beans::XPropertySet > m_xExistingShapeProperties
std::unique_ptr< weld::SpinButton > m_xSpinBorder
css::uno::Reference< css::frame::XModel > m_xModel
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
virtual short run() override
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
void GetErrorCorrection(long)
RegionData_Impl * mpParent
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
RET_OK
Reference< XComponentContext > getProcessComponentContext()
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
Reference< XGraphic > xGraphic