3 * \file qt4/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 "FileDialog.h"
17 #include "support/FileName.h"
18 #include "GuiClipboard.h"
19 #include "qt_helpers.h"
22 #include "BufferView.h"
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/debug.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 <QApplication>
42 #include <QDataStream>
47 #include <QStringList>
48 #include <QTextDocument>
50 #include <boost/crc.hpp>
57 using namespace lyx::support;
64 static QMimeData const * read_clipboard()
66 LYXERR(Debug::CLIPBOARD, "Getting Clipboard");
67 QMimeData const * source =
68 qApp->clipboard()->mimeData(QClipboard::Clipboard);
70 LYXERR0("0 bytes (no QMimeData)");
73 // It appears that doing IO between getting a mimeData object
74 // and using it can cause a crash (maybe Qt used IO
75 // as an excuse to free() it? Anyway let's not introduce
76 // any new IO here, so e.g. leave the following line commented.
77 // lyxerr << "Got Clipboard (" << (long) source << ")\n" ;
82 void CacheMimeData::update()
84 time_t const start_time = current_time();
85 LYXERR(Debug::CLIPBOARD, "Creating CacheMimeData object");
86 cached_formats_ = read_clipboard()->formats();
88 // Qt times out after 5 seconds if it does not recieve a response.
89 if (current_time() - start_time > 3) {
90 LYXERR0("No timely response from clipboard, perhaps process "
91 << "holding clipboard is frozen?");
96 QByteArray CacheMimeData::data(QString const & mimeType) const
98 return read_clipboard()->data(mimeType);
102 QString const lyxMimeType(){ return "application/x-lyx"; }
103 QString const texMimeType(){ return "text/x-tex"; }
104 QString const latexMimeType(){ return "application/x-latex"; }
105 QString const pdfMimeType(){ return "application/pdf"; }
106 QString const emfMimeType(){ return "image/x-emf"; }
107 QString const wmfMimeType(){ return "image/x-wmf"; }
110 GuiClipboard::GuiClipboard()
112 connect(qApp->clipboard(), SIGNAL(dataChanged()),
113 this, SLOT(on_dataChanged()));
114 // initialize clipboard status.
119 string const GuiClipboard::getAsLyX() const
121 LYXERR(Debug::CLIPBOARD, "GuiClipboard::getAsLyX(): `");
122 // We don't convert encodings here since the encoding of the
123 // clipboard contents is specified in the data itself
124 if (cache_.hasFormat(lyxMimeType())) {
125 // data from ourself or some other LyX instance
126 QByteArray const ar = cache_.data(lyxMimeType());
127 string const s(ar.data(), ar.count());
128 LYXERR(Debug::CLIPBOARD, s << "'");
131 LYXERR(Debug::CLIPBOARD, "'");
136 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
137 Clipboard::GraphicsType & type) const
139 // create file dialog filter according to the existing types in the clipboard
140 vector<Clipboard::GraphicsType> types;
141 if (hasGraphicsContents(Clipboard::EmfGraphicsType))
142 types.push_back(Clipboard::EmfGraphicsType);
143 if (hasGraphicsContents(Clipboard::WmfGraphicsType))
144 types.push_back(Clipboard::WmfGraphicsType);
145 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
146 types.push_back(Clipboard::LinkBackGraphicsType);
147 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
148 types.push_back(Clipboard::PdfGraphicsType);
149 if (hasGraphicsContents(Clipboard::PngGraphicsType))
150 types.push_back(Clipboard::PngGraphicsType);
151 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
152 types.push_back(Clipboard::JpegGraphicsType);
154 LASSERT(!types.empty(), return FileName());
156 // select prefered type if AnyGraphicsType was passed
157 if (type == Clipboard::AnyGraphicsType)
158 type = types.front();
161 map<Clipboard::GraphicsType, string> extensions;
162 map<Clipboard::GraphicsType, docstring> typeNames;
164 extensions[Clipboard::EmfGraphicsType] = "emf";
165 extensions[Clipboard::WmfGraphicsType] = "wmf";
166 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
167 extensions[Clipboard::PdfGraphicsType] = "pdf";
168 extensions[Clipboard::PngGraphicsType] = "png";
169 extensions[Clipboard::JpegGraphicsType] = "jpeg";
171 typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
172 typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
173 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
174 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
175 typeNames[Clipboard::PngGraphicsType] = _("PNG");
176 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
178 // find unused filename with primary extension
179 string document_path = cur.buffer()->fileName().onlyPath().absFileName();
180 unsigned newfile_number = 0;
184 filename = FileName(addName(document_path,
186 + convert<string>(newfile_number) + "."
187 + extensions[type]));
188 } while (filename.isReadableFile());
191 // create file type filter, putting the prefered on to the front
193 for (size_t i = 0; i != types.size(); ++i) {
194 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
195 + " (*." + from_ascii(extensions[types[i]]) + ")";
196 if (types[i] == type)
197 filter.prepend(toqstr(s));
199 filter.append(toqstr(s));
201 filter = fileFilters(filter.join(";;"));
203 // show save dialog for the graphic
204 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
205 FileDialog::Result result =
206 dlg.save(toqstr(filename.onlyPath().absFileName()), filter,
207 toqstr(filename.onlyFileName()));
209 if (result.first == FileDialog::Later)
212 string newFilename = fromqstr(result.second);
213 if (newFilename.empty()) {
214 cur.bv().message(_("Canceled."));
217 filename.set(newFilename);
219 // check the extension (the user could have changed it)
220 if (!suffixIs(ascii_lowercase(filename.absFileName()),
221 "." + extensions[type])) {
222 // the user changed the extension. Check if the type is available
224 for (i = 1; i != types.size(); ++i) {
225 if (suffixIs(ascii_lowercase(filename.absFileName()),
226 "." + extensions[types[i]])) {
232 // invalid extension found, or none at all. In the latter
233 // case set the default extensions.
234 if (i == types.size()
235 && filename.onlyFileName().find('.') == string::npos) {
236 filename.changeExtension("." + extensions[type]);
240 // check whether the file exists and warn the user
241 if (!filename.exists())
243 int ret = frontend::Alert::prompt(
244 _("Overwrite external file?"),
245 bformat(_("File %1$s already exists, do you want to overwrite it?"),
246 from_utf8(filename.absFileName())), 1, 1, _("&Overwrite"), _("&Cancel"));
248 // overwrite, hence break the dialog loop
251 // not overwrite, hence show the dialog again (i.e. loop)
258 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
260 // get the filename from the user
261 FileName filename = getPastedGraphicsFileName(cur, type);
262 if (filename.empty())
265 // handle image cases first
266 if (type == PngGraphicsType || type == JpegGraphicsType) {
267 // get image from QImage from clipboard
268 QImage image = qApp->clipboard()->image();
269 if (image.isNull()) {
270 LYXERR(Debug::CLIPBOARD, "No image in clipboard");
274 // convert into graphics format
277 buffer.open(QIODevice::WriteOnly);
278 if (type == PngGraphicsType)
279 image.save(toqstr(filename.absFileName()), "PNG");
280 else if (type == JpegGraphicsType)
281 image.save(toqstr(filename.absFileName()), "JPEG");
291 case PdfGraphicsType: mime = pdfMimeType(); break;
292 case LinkBackGraphicsType: mime = pdfMimeType(); break;
293 case EmfGraphicsType: mime = emfMimeType(); break;
294 case WmfGraphicsType: mime = wmfMimeType(); break;
295 default: LASSERT(false, return FileName());
299 if (!cache_.hasFormat(mime))
301 // data from ourself or some other LyX instance
302 QByteArray const ar = cache_.data(mime);
303 LYXERR(Debug::CLIPBOARD, "Getting from clipboard: mime = " << mime.constData()
304 << "length = " << ar.count());
306 QFile f(toqstr(filename.absFileName()));
307 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
308 LYXERR(Debug::CLIPBOARD, "Error opening file "
309 << filename.absFileName() << " for writing");
313 // write the (LinkBack) PDF data
315 if (type == LinkBackGraphicsType) {
317 void const * linkBackData;
318 unsigned linkBackLen;
319 getLinkBackData(&linkBackData, &linkBackLen);
320 f.write((char *)linkBackData, linkBackLen);
321 quint32 pdfLen = ar.size();
323 ds << pdfLen; // big endian by default
325 // only non-Mac this should never happen
337 * Tidy up a HTML chunk coming from the clipboard.
338 * This is needed since different applications put different kinds of HTML
340 * - With or without the <?xml> tag
341 * - With or without the <!DOCTYPE> tag
342 * - With or without the <html> tag
343 * - With or without the <body> tag
344 * - With or without the <p> tag
345 * Since we are going to write a HTML file for external converters we need
346 * to ensure that it is a well formed HTML file, including all the mentioned tags.
348 QString tidyHtml(QString input)
350 // Misuse QTextDocument to cleanup the HTML.
351 // As a side effect, all visual markup like <tt> is converted to CSS,
352 // which is ignored by gnuhtml2latex.
353 // While this may be seen as a bug by some people it is actually a
354 // good thing, since we do import structure, but ignore all visual
356 QTextDocument converter;
357 converter.setHtml(input);
358 return converter.toHtml("utf-8");
363 docstring const GuiClipboard::getAsText(TextType type) const
365 // text data from other applications
366 if ((type == AnyTextType || type == LyXOrPlainTextType) && hasTextContents(LyXTextType))
368 if (type == AnyTextType && hasTextContents(LaTeXTextType))
369 type = LaTeXTextType;
370 if (type == AnyTextType && hasTextContents(HtmlTextType))
375 // must not convert to docstring, since file can contain
376 // mixed encodings (use getAsLyX() instead)
379 case LyXOrPlainTextType:
381 str = qApp->clipboard()->text(QClipboard::Clipboard)
382 .normalized(QString::NormalizationForm_C);
384 case LaTeXTextType: {
385 QMimeData const * source =
386 qApp->clipboard()->mimeData(QClipboard::Clipboard);
388 // First try LaTeX, then TeX (we do not distinguish
389 // for clipboard purposes)
390 if (source->hasFormat(latexMimeType())) {
391 str = source->data(latexMimeType());
392 str = str.normalized(QString::NormalizationForm_C);
393 } else if (source->hasFormat(texMimeType())) {
394 str = source->data(texMimeType());
395 str = str.normalized(QString::NormalizationForm_C);
401 QString subtype = "html";
402 str = qApp->clipboard()->text(subtype, QClipboard::Clipboard)
403 .normalized(QString::NormalizationForm_C);
408 LYXERR(Debug::CLIPBOARD, "GuiClipboard::getAsText(" << type << "): `" << str << "'");
412 return internalLineEnding(str);
416 void GuiClipboard::put(string const & text) const
418 qApp->clipboard()->setText(toqstr(text));
422 void GuiClipboard::put(string const & lyx, docstring const & html, docstring const & text)
424 LYXERR(Debug::CLIPBOARD, "GuiClipboard::put(`" << lyx << "' `"
425 << to_utf8(html) << "' `" << to_utf8(text) << "')");
426 // We don't convert the encoding of lyx since the encoding of the
427 // clipboard contents is specified in the data itself
428 QMimeData * data = new QMimeData;
430 QByteArray const qlyx(lyx.c_str(), lyx.size());
431 data->setData(lyxMimeType(), qlyx);
432 // If the OS has not the concept of clipboard ownership,
433 // we recognize internal data through its checksum.
434 if (!hasInternal()) {
435 boost::crc_32_type crc32;
436 crc32.process_bytes(lyx.c_str(), lyx.size());
437 checksum = crc32.checksum();
440 // Don't test for text.empty() since we want to be able to clear the
442 QString const qtext = toqstr(text);
443 data->setText(qtext);
444 QString const qhtml = toqstr(html);
445 data->setHtml(qhtml);
446 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
450 bool GuiClipboard::hasTextContents(Clipboard::TextType type) const
454 return cache_.hasFormat(lyxMimeType()) || cache_.hasText() ||
455 cache_.hasHtml() || cache_.hasFormat(latexMimeType()) ||
456 cache_.hasFormat(texMimeType());
457 case LyXOrPlainTextType:
458 return cache_.hasFormat(lyxMimeType()) || cache_.hasText();
460 return cache_.hasFormat(lyxMimeType());
462 return cache_.hasText();
464 return cache_.hasHtml();
466 return cache_.hasFormat(latexMimeType()) ||
467 cache_.hasFormat(texMimeType());
474 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
476 if (type == AnyGraphicsType) {
477 return hasGraphicsContents(PdfGraphicsType)
478 || hasGraphicsContents(PngGraphicsType)
479 || hasGraphicsContents(JpegGraphicsType)
480 || hasGraphicsContents(EmfGraphicsType)
481 || hasGraphicsContents(WmfGraphicsType)
482 || hasGraphicsContents(LinkBackGraphicsType);
485 // handle image cases first
486 if (type == PngGraphicsType || type == JpegGraphicsType)
487 return cache_.hasImage();
489 // handle LinkBack for Mac
490 if (type == LinkBackGraphicsType)
492 return isLinkBackDataInPasteboard();
498 QStringList const & formats = cache_.formats();
499 LYXERR(Debug::CLIPBOARD, "We found " << formats.size() << " formats");
500 for (int i = 0; i < formats.size(); ++i)
501 LYXERR(Debug::CLIPBOARD, "Found format " << formats[i]);
503 // compute mime for type
506 case EmfGraphicsType: mime = emfMimeType(); break;
507 case WmfGraphicsType: mime = wmfMimeType(); break;
508 case PdfGraphicsType: mime = pdfMimeType(); break;
509 default: LASSERT(false, return false);
512 return cache_.hasFormat(mime);
516 bool GuiClipboard::isInternal() const
518 if (!hasTextContents(LyXTextType))
521 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
522 // the preamble dialog. This does only work on X11 and Windows, since
523 // ownsClipboard() is hardwired to return false on OS X.
525 return qApp->clipboard()->ownsClipboard();
527 // We are running on OS X: Check whether clipboard data is from
528 // ourself by comparing its checksum with the stored one.
529 QByteArray const ar = cache_.data(lyxMimeType());
530 string const data(ar.data(), ar.count());
531 boost::crc_32_type crc32;
532 crc32.process_bytes(data.c_str(), data.size());
533 return checksum == crc32.checksum();
537 bool GuiClipboard::hasInternal() const
539 // Windows and Mac OS X does not have the concept of ownership;
540 // the clipboard is a fully global resource so all applications
541 // are notified of changes. However, on Windows ownership is
542 // emulated by Qt through the OleIsCurrentClipboard() API, while
543 // on Mac OS X we deal with this issue by ourself.
552 void GuiClipboard::on_dataChanged()
554 //Note: we do not really need to run cache_.update() unless the
555 //data has been changed *and* the GuiClipboard has been queried.
556 //However if run cache_.update() the moment a process grabs the
557 //clipboard, the process holding the clipboard presumably won't
558 //yet be frozen, and so we won't need to wait 5 seconds for Qt
559 //to time-out waiting for the clipboard.
561 QStringList l = cache_.formats();
562 LYXERR(Debug::CLIPBOARD, "Qt Clipboard changed. We found the following mime types:");
563 for (int i = 0; i < l.count(); i++)
564 LYXERR(Debug::CLIPBOARD, l.value(i));
566 plaintext_clipboard_empty_ = qApp->clipboard()->
567 text(QClipboard::Clipboard).isEmpty();
569 has_text_contents_ = hasTextContents();
570 has_graphics_contents_ = hasGraphicsContents();
574 bool GuiClipboard::empty() const
576 // We need to check both the plaintext and the LyX version of the
577 // clipboard. The plaintext version is empty if the LyX version
578 // contains only one inset, and the LyX version is empty if the
579 // clipboard does not come from LyX.
580 if (!plaintext_clipboard_empty_)
582 return !has_text_contents_ && !has_graphics_contents_;
585 } // namespace frontend
588 #include "moc_GuiClipboard.cpp"