]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiClipboard.cpp
Guard against possible referencing null.
[features.git] / src / frontends / qt / GuiClipboard.cpp
1 // -*- C++ -*-
2 /**
3  * \file qt/GuiClipboard.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiApplication.h"
16 #include "GuiClipboard.h"
17
18 #include "Buffer.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "FileDialog.h"
22 #include "qt_helpers.h"
23
24 #include "support/lassert.h"
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/lyxtime.h"
32
33 #ifdef Q_OS_MAC
34 #include "support/linkback/LinkBackProxy.h"
35 #endif // Q_OS_MAC
36
37 #include "frontends/alert.h"
38
39 #include "support/checksum.h"
40
41 #include <QApplication>
42 #include <QBuffer>
43 #include <QClipboard>
44 #include <QDataStream>
45 #include <QFile>
46 #include <QImage>
47 #include <QMimeData>
48 #include <QString>
49 #include <QStringList>
50 #include <QTextDocument>
51 #include <QTimer>
52
53 #include <memory>
54 #include <map>
55 #include <iostream>
56
57 using namespace std;
58 using namespace lyx::support;
59
60
61 namespace lyx {
62
63 namespace frontend {
64
65 static QMimeData const * read_clipboard()
66 {
67         LYXERR(Debug::CLIPBOARD, "Getting Clipboard");
68         QMimeData const * source =
69                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
70         if (!source) {
71                 LYXERR0("0 bytes (no QMimeData)");
72                 return new QMimeData;
73         }
74         // It appears that doing IO between getting a mimeData object
75         // and using it can cause a crash (maybe Qt used IO
76         // as an excuse to free() it? Anyway let's not introduce
77         // any new IO here, so e.g. leave the following line commented.
78         // lyxerr << "Got Clipboard (" << (long) source << ")\n" ;
79         return source;
80 }
81
82
83 void CacheMimeData::update()
84 {
85         time_t const start_time = current_time();
86         LYXERR(Debug::CLIPBOARD, "Creating CacheMimeData object");
87         cached_formats_ = read_clipboard()->formats();
88
89         // Qt times out after 5 seconds if it does not receive a response.
90         if (current_time() - start_time > 3) {
91                 LYXERR0("No timely response from clipboard, perhaps process "
92                         << "holding clipboard is frozen?");
93         }
94 }
95
96
97 QByteArray CacheMimeData::data(QString const & mimeType) const
98 {
99         return read_clipboard()->data(mimeType);
100 }
101
102
103 QString const lyxMimeType(){ return "application/x-lyx"; }
104 QString const texMimeType(){ return "text/x-tex"; }
105 QString const latexMimeType(){ return "application/x-latex"; }
106 QString const pdfMimeType(){ return "application/pdf"; }
107 QString const emfMimeType(){ return "image/x-emf"; }
108 QString const wmfMimeType(){ return "image/x-wmf"; }
109
110
111 GuiClipboard::GuiClipboard()
112 {
113         connect(qApp->clipboard(), SIGNAL(dataChanged()),
114                 this, SLOT(on_dataChanged()));
115         if(qApp->clipboard()->supportsFindBuffer()) {
116                 connect(qApp->clipboard(), SIGNAL(findBufferChanged()),
117                         this, SLOT(on_findChanged()));
118                 on_findChanged();
119         }
120         // initialize clipboard status.
121         update();
122 }
123
124
125 string const GuiClipboard::getAsLyX() const
126 {
127         LYXERR(Debug::CLIPBOARD, "GuiClipboard::getAsLyX(): `");
128         // We don't convert encodings here since the encoding of the
129         // clipboard contents is specified in the data itself
130         if (cache_.hasFormat(lyxMimeType())) {
131                 // data from ourself or some other LyX instance
132                 QByteArray const ar = cache_.data(lyxMimeType());
133                 string const s(ar.data(), ar.count());
134                 LYXERR(Debug::CLIPBOARD, s << "'");
135                 return s;
136         }
137         LYXERR(Debug::CLIPBOARD, "'");
138         return string();
139 }
140
141
142 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
143         Clipboard::GraphicsType & type) const
144 {
145         // create file dialog filter according to the existing types in the clipboard
146         vector<Clipboard::GraphicsType> types;
147         if (hasGraphicsContents(Clipboard::EmfGraphicsType))
148                 types.push_back(Clipboard::EmfGraphicsType);
149         if (hasGraphicsContents(Clipboard::WmfGraphicsType))
150                 types.push_back(Clipboard::WmfGraphicsType);
151         if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
152                 types.push_back(Clipboard::LinkBackGraphicsType);
153         if (hasGraphicsContents(Clipboard::PdfGraphicsType))
154                 types.push_back(Clipboard::PdfGraphicsType);
155         if (hasGraphicsContents(Clipboard::PngGraphicsType))
156                 types.push_back(Clipboard::PngGraphicsType);
157         if (hasGraphicsContents(Clipboard::JpegGraphicsType))
158                 types.push_back(Clipboard::JpegGraphicsType);
159
160         LASSERT(!types.empty(), return FileName());
161
162         // select preferred type if AnyGraphicsType was passed
163         if (type == Clipboard::AnyGraphicsType)
164                 type = types.front();
165
166         // which extension?
167         map<Clipboard::GraphicsType, string> extensions;
168         map<Clipboard::GraphicsType, docstring> typeNames;
169
170         extensions[Clipboard::EmfGraphicsType] = "emf";
171         extensions[Clipboard::WmfGraphicsType] = "wmf";
172         extensions[Clipboard::LinkBackGraphicsType] = "linkback";
173         extensions[Clipboard::PdfGraphicsType] = "pdf";
174         extensions[Clipboard::PngGraphicsType] = "png";
175         extensions[Clipboard::JpegGraphicsType] = "jpeg";
176
177         typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
178         typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
179         typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
180         typeNames[Clipboard::PdfGraphicsType] = _("PDF");
181         typeNames[Clipboard::PngGraphicsType] = _("PNG");
182         typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
183
184         // find unused filename with primary extension
185         string document_path = cur.buffer()->fileName().onlyPath().absFileName();
186         unsigned newfile_number = 0;
187         FileName filename;
188         do {
189                 ++newfile_number;
190                 filename = FileName(addName(document_path,
191                         to_utf8(_("pasted"))
192                         + convert<string>(newfile_number) + "."
193                         + extensions[type]));
194         } while (filename.isReadableFile());
195
196         while (true) {
197                 // create file type filter, putting the preferred on to the front
198                 QStringList filter;
199                 for (size_t i = 0; i != types.size(); ++i) {
200                         docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
201                                 + " (*." + from_ascii(extensions[types[i]]) + ")";
202                         if (types[i] == type)
203                                 filter.prepend(toqstr(s));
204                         else
205                                 filter.append(toqstr(s));
206                 }
207                 filter = fileFilters(filter.join(";;"));
208
209                 // show save dialog for the graphic
210                 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
211                 FileDialog::Result result =
212                 dlg.save(toqstr(filename.onlyPath().absFileName()), filter,
213                          toqstr(filename.onlyFileName()));
214
215                 if (result.first == FileDialog::Later)
216                         return FileName();
217
218                 string newFilename = fromqstr(result.second);
219                 if (newFilename.empty()) {
220                         cur.bv().message(_("Canceled."));
221                         return FileName();
222                 }
223                 filename.set(newFilename);
224
225                 // check the extension (the user could have changed it)
226                 if (!suffixIs(ascii_lowercase(filename.absFileName()),
227                               "." + extensions[type])) {
228                         // the user changed the extension. Check if the type is available
229                         size_t i;
230                         for (i = 1; i != types.size(); ++i) {
231                                 if (suffixIs(ascii_lowercase(filename.absFileName()),
232                                              "." + extensions[types[i]])) {
233                                         type = types[i];
234                                         break;
235                                 }
236                         }
237
238                         // invalid extension found, or none at all. In the latter
239                         // case set the default extensions.
240                         if (i == types.size()
241                             && filename.onlyFileName().find('.') == string::npos) {
242                                 filename.changeExtension("." + extensions[type]);
243                         }
244                 }
245
246                 // check whether the file exists and warn the user
247                 if (!filename.exists())
248                         break;
249                 int ret = frontend::Alert::prompt(
250                         _("Overwrite external file?"),
251                         bformat(_("File %1$s already exists, do you want to overwrite it?"),
252                         from_utf8(filename.absFileName())), 1, 1, _("&Overwrite"), _("&Cancel"));
253                 if (ret == 0)
254                         // overwrite, hence break the dialog loop
255                         break;
256
257                 // not overwrite, hence show the dialog again (i.e. loop)
258         }
259
260         return filename;
261 }
262
263
264 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
265 {
266         // get the filename from the user
267         FileName filename = getPastedGraphicsFileName(cur, type);
268         if (filename.empty())
269                 return FileName();
270
271         // handle image cases first
272         if (type == PngGraphicsType || type == JpegGraphicsType) {
273                 // get image from QImage from clipboard
274                 QImage image = qApp->clipboard()->image();
275                 if (image.isNull()) {
276                         LYXERR(Debug::CLIPBOARD, "No image in clipboard");
277                         return FileName();
278                 }
279
280                 // convert into graphics format
281                 QByteArray ar;
282                 QBuffer buffer(&ar);
283                 buffer.open(QIODevice::WriteOnly);
284                 if (type == PngGraphicsType)
285                         image.save(toqstr(filename.absFileName()), "PNG");
286                 else if (type == JpegGraphicsType)
287                         image.save(toqstr(filename.absFileName()), "JPEG");
288                 else
289                         LATTEST(false);
290
291                 return filename;
292         }
293
294         // get mime for type
295         QString mime;
296         switch (type) {
297         case PdfGraphicsType: mime = pdfMimeType(); break;
298         case LinkBackGraphicsType: mime = pdfMimeType(); break;
299         case EmfGraphicsType: mime = emfMimeType(); break;
300         case WmfGraphicsType: mime = wmfMimeType(); break;
301         default: LASSERT(false, return FileName());
302         }
303
304         // get data
305         if (!cache_.hasFormat(mime))
306                 return FileName();
307         // data from ourself or some other LyX instance
308         QByteArray const ar = cache_.data(mime);
309         LYXERR(Debug::CLIPBOARD, "Getting from clipboard: mime = " << mime.constData()
310                << "length = " << ar.count());
311
312         QFile f(toqstr(filename.absFileName()));
313         if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
314                 LYXERR(Debug::CLIPBOARD, "Error opening file "
315                        << filename.absFileName() << " for writing");
316                 return FileName();
317         }
318
319         // write the (LinkBack) PDF data
320         f.write(ar);
321         if (type == LinkBackGraphicsType) {
322 #ifdef Q_OS_MAC
323                 void const * linkBackData;
324                 unsigned linkBackLen;
325                 getLinkBackData(&linkBackData, &linkBackLen);
326                 f.write((char *)linkBackData, linkBackLen);
327                 quint32 pdfLen = ar.size();
328                 QDataStream ds(&f);
329                 ds << pdfLen; // big endian by default
330 #else
331                 // only non-Mac this should never happen
332                 LATTEST(false);
333 #endif // Q_OS_MAC
334         }
335
336         f.close();
337         return filename;
338 }
339
340
341 namespace {
342 /**
343  * Tidy up a HTML chunk coming from the clipboard.
344  * This is needed since different applications put different kinds of HTML
345  * on the clipboard:
346  * - With or without the <?xml> tag
347  * - With or without the <!DOCTYPE> tag
348  * - With or without the <html> tag
349  * - With or without the <body> tag
350  * - With or without the <p> tag
351  * Since we are going to write a HTML file for external converters we need
352  * to ensure that it is a well formed HTML file, including all the mentioned tags.
353  */
354 QString tidyHtml(QString const & input)
355 {
356         // Misuse QTextDocument to cleanup the HTML.
357         // As a side effect, all visual markup like <tt> is converted to CSS,
358         // which is ignored by gnuhtml2latex.
359         // While this may be seen as a bug by some people it is actually a
360         // good thing, since we do import structure, but ignore all visual
361         // clutter.
362         QTextDocument converter;
363         converter.setHtml(input);
364 #if QT_VERSION < 0x060000
365         return converter.toHtml("utf-8");
366 #else
367         return converter.toHtml();
368 #endif
369 }
370 } // namespace
371
372
373 docstring const GuiClipboard::getAsText(TextType type) const
374 {
375         // text data from other applications
376         if ((type == AnyTextType || type == LyXOrPlainTextType) && hasTextContents(LyXTextType))
377                 type = LyXTextType;
378         if (type == AnyTextType && hasTextContents(LaTeXTextType))
379                 type = LaTeXTextType;
380         if (type == AnyTextType && hasTextContents(HtmlTextType))
381                 type = HtmlTextType;
382         QString str;
383         switch (type) {
384         case LyXTextType:
385                 // must not convert to docstring, since file can contain
386                 // mixed encodings (use getAsLyX() instead)
387                 break;
388         case AnyTextType:
389         case LyXOrPlainTextType:
390         case PlainTextType:
391                 str = qApp->clipboard()->text(QClipboard::Clipboard)
392                                 .normalized(QString::NormalizationForm_C);
393                 break;
394         case LaTeXTextType: {
395                 QMimeData const * source =
396                         qApp->clipboard()->mimeData(QClipboard::Clipboard);
397                 if (source) {
398                         // First try LaTeX, then TeX (we do not distinguish
399                         // for clipboard purposes)
400                         if (source->hasFormat(latexMimeType())) {
401                                 str = source->data(latexMimeType());
402                                 str = str.normalized(QString::NormalizationForm_C);
403                         } else if (source->hasFormat(texMimeType())) {
404                                 str = source->data(texMimeType());
405                                 str = str.normalized(QString::NormalizationForm_C);
406                         }
407                 }
408                 break;
409         }
410         case HtmlTextType: {
411                 QString subtype = "html";
412                 str = qApp->clipboard()->text(subtype, QClipboard::Clipboard)
413                                 .normalized(QString::NormalizationForm_C);
414                 str = tidyHtml(str);
415                 break;
416         }
417         }
418         LYXERR(Debug::CLIPBOARD, "GuiClipboard::getAsText(" << type << "): `" << str << "'");
419         if (str.isNull())
420                 return docstring();
421
422         return internalLineEnding(str);
423 }
424
425
426 void GuiClipboard::put(string const & text) const
427 {
428         qApp->clipboard()->setText(toqstr(text));
429 }
430
431
432 void GuiClipboard::put(string const & lyx, docstring const & html, docstring const & text)
433 {
434         LYXERR(Debug::CLIPBOARD, "GuiClipboard::put(`" << lyx << "' `"
435                               << to_utf8(html) << "' `" << to_utf8(text) << "')");
436         // We don't convert the encoding of lyx since the encoding of the
437         // clipboard contents is specified in the data itself
438         QMimeData * data = new QMimeData;
439         if (!lyx.empty()) {
440                 QByteArray const qlyx(lyx.c_str(), lyx.size());
441                 data->setData(lyxMimeType(), qlyx);
442                 // If the OS has not the concept of clipboard ownership,
443                 // we recognize internal data through its checksum.
444                 if (!hasInternal())
445                         checksum = support::checksum(lyx);
446         }
447         // Don't test for text.empty() since we want to be able to clear the
448         // clipboard.
449         QString const qtext = toqstr(text);
450         data->setText(qtext);
451         QString const qhtml = toqstr(html);
452         data->setHtml(qhtml);
453         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
454 }
455
456
457 bool GuiClipboard::hasTextContents(Clipboard::TextType type) const
458 {
459         switch (type) {
460         case AnyTextType:
461                 return cache_.hasFormat(lyxMimeType()) || cache_.hasText() ||
462                        cache_.hasHtml() || cache_.hasFormat(latexMimeType()) ||
463                        cache_.hasFormat(texMimeType());
464         case LyXOrPlainTextType:
465                 return cache_.hasFormat(lyxMimeType()) || cache_.hasText();
466         case LyXTextType:
467                 return cache_.hasFormat(lyxMimeType());
468         case PlainTextType:
469                 return cache_.hasText();
470         case HtmlTextType:
471                 return cache_.hasHtml();
472         case LaTeXTextType:
473                 return cache_.hasFormat(latexMimeType()) ||
474                        cache_.hasFormat(texMimeType());
475         }
476         // shut up compiler
477         return false;
478 }
479
480
481 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
482 {
483         if (type == AnyGraphicsType) {
484                 return hasGraphicsContents(PdfGraphicsType)
485                         || hasGraphicsContents(PngGraphicsType)
486                         || hasGraphicsContents(JpegGraphicsType)
487                         || hasGraphicsContents(EmfGraphicsType)
488                         || hasGraphicsContents(WmfGraphicsType)
489                         || hasGraphicsContents(LinkBackGraphicsType);
490         }
491
492         // handle image cases first
493         if (type == PngGraphicsType || type == JpegGraphicsType)
494                 return cache_.hasImage();
495
496         // handle LinkBack for Mac
497         if (type == LinkBackGraphicsType)
498 #ifdef Q_OS_MAC
499                 return isLinkBackDataInPasteboard();
500 #else
501                 return false;
502 #endif // Q_OS_MAC
503
504         // get mime data
505         QStringList const & formats = cache_.formats();
506         LYXERR(Debug::CLIPBOARD, "We found " << formats.size() << " formats");
507         for (int i = 0; i < formats.size(); ++i)
508                 LYXERR(Debug::CLIPBOARD, "Found format " << formats[i]);
509
510         // compute mime for type
511         QString mime;
512         switch (type) {
513         case EmfGraphicsType: mime = emfMimeType(); break;
514         case WmfGraphicsType: mime = wmfMimeType(); break;
515         case PdfGraphicsType: mime = pdfMimeType(); break;
516         default: LASSERT(false, return false);
517         }
518
519         return cache_.hasFormat(mime);
520 }
521
522
523 bool GuiClipboard::isInternal() const
524 {
525         if (!hasTextContents(LyXTextType))
526                 return false;
527
528         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
529         // the preamble dialog. This does only work on X11 and Windows, since
530         // ownsClipboard() is hardwired to return false on OS X.
531         if (hasInternal())
532                 return qApp->clipboard()->ownsClipboard();
533
534         // We are running on OS X: Check whether clipboard data is from
535         // ourself by comparing its checksum with the stored one.
536         QByteArray const ar = cache_.data(lyxMimeType());
537         string const data(ar.data(), ar.count());
538         return checksum == static_cast<std::uint32_t>(support::checksum(data));
539 }
540
541
542 bool GuiClipboard::hasInternal() const
543 {
544         // Windows and Mac OS X does not have the concept of ownership;
545         // the clipboard is a fully global resource so all applications
546         // are notified of changes. However, on Windows ownership is
547         // emulated by Qt through the OleIsCurrentClipboard() API, while
548         // on Mac OS X we deal with this issue by ourself.
549 #ifndef Q_OS_MAC
550         return true;
551 #else
552         return false;
553 #endif
554 }
555
556
557 void GuiClipboard::setFindBuffer(docstring const & text)
558 {
559         LYXERR(Debug::CLIPBOARD, "new findbuffer: " << text);
560         Clipboard::setFindBuffer(text);
561         if(qApp->clipboard()->supportsFindBuffer()) {
562                 qApp->clipboard()->setText(toqstr(text), QClipboard::FindBuffer);
563         }
564 }
565
566
567 void GuiClipboard::on_findChanged()
568 {
569         Clipboard::setFindBuffer(from_utf8(fromqstr(
570                 qApp->clipboard()->text(QClipboard::FindBuffer))));
571 }
572
573
574 void GuiClipboard::on_dataChanged()
575 {
576         update();
577 #if defined(Q_OS_WIN) || defined(Q_CYGWIN_WIN)
578         // Retry on Windows (#10109)
579         if (cache_.formats().count() == 0) {
580                 QTimer::singleShot(100, this, SLOT(update()));
581         }
582 #endif
583 }
584
585 void GuiClipboard::update()
586 {
587         //Note: we do not really need to run cache_.update() unless the
588         //data has been changed *and* the GuiClipboard has been queried.
589         //However if run cache_.update() the moment a process grabs the
590         //clipboard, the process holding the clipboard presumably won't
591         //yet be frozen, and so we won't need to wait 5 seconds for Qt
592         //to time-out waiting for the clipboard.
593         cache_.update();
594         QStringList l = cache_.formats();
595         LYXERR(Debug::CLIPBOARD, "Qt Clipboard changed. We found the following mime types:");
596         for (int i = 0; i < l.count(); i++)
597                 LYXERR(Debug::CLIPBOARD, l.value(i));
598
599         plaintext_clipboard_empty_ = qApp->clipboard()->
600                 text(QClipboard::Clipboard).isEmpty();
601
602         has_text_contents_ = hasTextContents();
603         has_graphics_contents_ = hasGraphicsContents();
604 }
605
606
607 bool GuiClipboard::empty() const
608 {
609         // We need to check both the plaintext and the LyX version of the
610         // clipboard. The plaintext version is empty if the LyX version
611         // contains only one inset, and the LyX version is empty if the
612         // clipboard does not come from LyX.
613         if (!plaintext_clipboard_empty_)
614                 return false;
615         return !has_text_contents_ && !has_graphics_contents_;
616 }
617
618 } // namespace frontend
619 } // namespace lyx
620
621 #include "moc_GuiClipboard.cpp"