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.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiApplication.h"
16 #include "GuiClipboard.h"
19 #include "BufferView.h"
21 #include "FileDialog.h"
22 #include "qt_helpers.h"
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"
34 #include "support/linkback/LinkBackProxy.h"
37 #include "frontends/alert.h"
39 #include "support/checksum.h"
41 #include <QApplication>
44 #include <QDataStream>
49 #include <QStringList>
50 #include <QTextDocument>
58 using namespace lyx::support;
65 static QMimeData const * read_clipboard()
67 LYXERR(Debug::CLIPBOARD, "Getting Clipboard");
68 QMimeData const * source =
69 qApp->clipboard()->mimeData(QClipboard::Clipboard);
71 LYXERR0("0 bytes (no QMimeData)");
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" ;
83 void CacheMimeData::update()
85 time_t const start_time = current_time();
86 LYXERR(Debug::CLIPBOARD, "Creating CacheMimeData object");
87 cached_formats_ = read_clipboard()->formats();
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?");
97 QByteArray CacheMimeData::data(QString const & mimeType) const
99 return read_clipboard()->data(mimeType);
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"; }
111 GuiClipboard::GuiClipboard()
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()));
120 // initialize clipboard status.
125 string const GuiClipboard::getAsLyX() const
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 << "'");
137 LYXERR(Debug::CLIPBOARD, "'");
142 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
143 Clipboard::GraphicsType & type) const
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);
160 LASSERT(!types.empty(), return FileName());
162 // select preferred type if AnyGraphicsType was passed
163 if (type == Clipboard::AnyGraphicsType)
164 type = types.front();
167 map<Clipboard::GraphicsType, string> extensions;
168 map<Clipboard::GraphicsType, docstring> typeNames;
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";
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");
184 // find unused filename with primary extension
185 string document_path = cur.buffer()->fileName().onlyPath().absFileName();
186 unsigned newfile_number = 0;
190 filename = FileName(addName(document_path,
192 + convert<string>(newfile_number) + "."
193 + extensions[type]));
194 } while (filename.isReadableFile());
197 // create file type filter, putting the preferred on to the front
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));
205 filter.append(toqstr(s));
207 filter = fileFilters(filter.join(";;"));
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()));
215 if (result.first == FileDialog::Later)
218 string newFilename = fromqstr(result.second);
219 if (newFilename.empty()) {
220 cur.bv().message(_("Canceled."));
223 filename.set(newFilename);
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
230 for (i = 1; i != types.size(); ++i) {
231 if (suffixIs(ascii_lowercase(filename.absFileName()),
232 "." + extensions[types[i]])) {
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]);
246 // check whether the file exists and warn the user
247 if (!filename.exists())
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"));
254 // overwrite, hence break the dialog loop
257 // not overwrite, hence show the dialog again (i.e. loop)
264 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
266 // get the filename from the user
267 FileName filename = getPastedGraphicsFileName(cur, type);
268 if (filename.empty())
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");
280 // convert into graphics format
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");
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());
305 if (!cache_.hasFormat(mime))
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());
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");
319 // write the (LinkBack) PDF data
321 if (type == LinkBackGraphicsType) {
323 void const * linkBackData;
324 unsigned linkBackLen;
325 getLinkBackData(&linkBackData, &linkBackLen);
326 f.write((char *)linkBackData, linkBackLen);
327 quint32 pdfLen = ar.size();
329 ds << pdfLen; // big endian by default
331 // only non-Mac this should never happen
343 * Tidy up a HTML chunk coming from the clipboard.
344 * This is needed since different applications put different kinds of HTML
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.
354 QString tidyHtml(QString const & input)
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
362 QTextDocument converter;
363 converter.setHtml(input);
364 #if QT_VERSION < 0x060000
365 return converter.toHtml("utf-8");
367 return converter.toHtml();
373 docstring const GuiClipboard::getAsText(TextType type) const
375 // text data from other applications
376 if ((type == AnyTextType || type == LyXOrPlainTextType) && hasTextContents(LyXTextType))
378 if (type == AnyTextType && hasTextContents(LaTeXTextType))
379 type = LaTeXTextType;
380 if (type == AnyTextType && hasTextContents(HtmlTextType))
385 // must not convert to docstring, since file can contain
386 // mixed encodings (use getAsLyX() instead)
389 case LyXOrPlainTextType:
391 str = qApp->clipboard()->text(QClipboard::Clipboard)
392 .normalized(QString::NormalizationForm_C);
394 case LaTeXTextType: {
395 QMimeData const * source =
396 qApp->clipboard()->mimeData(QClipboard::Clipboard);
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);
411 QString subtype = "html";
412 str = qApp->clipboard()->text(subtype, QClipboard::Clipboard)
413 .normalized(QString::NormalizationForm_C);
418 LYXERR(Debug::CLIPBOARD, "GuiClipboard::getAsText(" << type << "): `" << str << "'");
422 return internalLineEnding(str);
426 void GuiClipboard::put(string const & text) const
428 qApp->clipboard()->setText(toqstr(text));
432 void GuiClipboard::put(string const & lyx, docstring const & html, docstring const & text)
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;
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.
445 checksum = support::checksum(lyx);
447 // Don't test for text.empty() since we want to be able to clear the
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);
457 bool GuiClipboard::hasTextContents(Clipboard::TextType type) const
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();
467 return cache_.hasFormat(lyxMimeType());
469 return cache_.hasText();
471 return cache_.hasHtml();
473 return cache_.hasFormat(latexMimeType()) ||
474 cache_.hasFormat(texMimeType());
481 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
483 if (type == AnyGraphicsType) {
484 return hasGraphicsContents(PdfGraphicsType)
485 || hasGraphicsContents(PngGraphicsType)
486 || hasGraphicsContents(JpegGraphicsType)
487 || hasGraphicsContents(EmfGraphicsType)
488 || hasGraphicsContents(WmfGraphicsType)
489 || hasGraphicsContents(LinkBackGraphicsType);
492 // handle image cases first
493 if (type == PngGraphicsType || type == JpegGraphicsType)
494 return cache_.hasImage();
496 // handle LinkBack for Mac
497 if (type == LinkBackGraphicsType)
499 return isLinkBackDataInPasteboard();
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]);
510 // compute mime for 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);
519 return cache_.hasFormat(mime);
523 bool GuiClipboard::isInternal() const
525 if (!hasTextContents(LyXTextType))
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.
532 return qApp->clipboard()->ownsClipboard();
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));
542 bool GuiClipboard::hasInternal() const
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.
557 void GuiClipboard::setFindBuffer(docstring const & text)
559 LYXERR(Debug::CLIPBOARD, "new findbuffer: " << text);
560 Clipboard::setFindBuffer(text);
561 if(qApp->clipboard()->supportsFindBuffer()) {
562 qApp->clipboard()->setText(toqstr(text), QClipboard::FindBuffer);
567 void GuiClipboard::on_findChanged()
569 Clipboard::setFindBuffer(from_utf8(fromqstr(
570 qApp->clipboard()->text(QClipboard::FindBuffer))));
574 void GuiClipboard::on_dataChanged()
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()));
585 void GuiClipboard::update()
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.
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));
599 plaintext_clipboard_empty_ = qApp->clipboard()->
600 text(QClipboard::Clipboard).isEmpty();
602 has_text_contents_ = hasTextContents();
603 has_graphics_contents_ = hasGraphicsContents();
607 bool GuiClipboard::empty() const
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_)
615 return !has_text_contents_ && !has_graphics_contents_;
618 } // namespace frontend
621 #include "moc_GuiClipboard.cpp"