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.
16 #include "BufferView.h"
18 #include "GuiClipboard.h"
19 #include "qt_helpers.h"
21 #include "boost/assert.hpp"
23 #include <QApplication>
26 #include <QDataStream>
29 #include <QMacPasteboardMime>
32 #include <QStringList>
34 #include "support/convert.h"
35 #include "support/debug.h"
36 #include "support/filetools.h"
37 #include "support/FileFilterList.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include "frontends/alert.h"
42 #include "frontends/FileDialog.h"
47 #include "support/linkback/LinkBackProxy.h"
51 using namespace lyx::support;
53 static char const * const lyx_mime_type = "application/x-lyx";
54 static char const * const pdf_mime_type = "application/pdf";
62 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
64 QMacPasteboardMimeGraphics()
65 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL) {}
66 ~QMacPasteboardMimeGraphics() {}
67 QString convertorName();
68 QString flavorFor(const QString &mime);
69 QString mimeFor(QString flav);
70 bool canConvert(const QString &mime, QString flav);
71 QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav);
72 QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
76 QString QMacPasteboardMimeGraphics::convertorName()
82 QString QMacPasteboardMimeGraphics::flavorFor(const QString &mime)
84 LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
85 if (mime == QLatin1String(pdf_mime_type))
86 return QLatin1String("com.adobe.pdf");
91 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
93 LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
94 if (flav == QLatin1String("com.adobe.pdf"))
95 return QLatin1String(pdf_mime_type);
100 bool QMacPasteboardMimeGraphics::canConvert(const QString &mime, QString flav)
102 return mimeFor(flav) == mime;
106 QVariant QMacPasteboardMimeGraphics::convertToMime(const QString &mime, QList<QByteArray> data, QString)
109 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
114 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
116 QList<QByteArray> ret;
117 ret.append(data.toByteArray());
121 static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
126 GuiClipboard::GuiClipboard()
128 connect(qApp->clipboard(), SIGNAL(dataChanged()),
129 this, SLOT(on_dataChanged()));
130 // initialize clipboard status.
134 if (!graphicsPasteboardMime)
135 graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
140 GuiClipboard::~GuiClipboard()
143 closeAllLinkBackLinks();
148 string const GuiClipboard::getAsLyX() const
150 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
151 // We don't convert encodings here since the encoding of the
152 // clipboard contents is specified in the data itself
153 QMimeData const * source =
154 qApp->clipboard()->mimeData(QClipboard::Clipboard);
156 LYXERR(Debug::ACTION, "' (no QMimeData)");
160 if (source->hasFormat(lyx_mime_type)) {
161 // data from ourself or some other LyX instance
162 QByteArray const ar = source->data(lyx_mime_type);
163 string const s(ar.data(), ar.count());
164 LYXERR(Debug::ACTION, s << "'");
167 LYXERR(Debug::ACTION, "'");
172 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
173 Clipboard::GraphicsType & type) const
175 // create file dialog filter according to the existing types in the clipboard
176 vector<Clipboard::GraphicsType> types;
177 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
178 types.push_back(Clipboard::LinkBackGraphicsType);
179 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
180 types.push_back(Clipboard::PdfGraphicsType);
181 if (hasGraphicsContents(Clipboard::PngGraphicsType))
182 types.push_back(Clipboard::PngGraphicsType);
183 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
184 types.push_back(Clipboard::JpegGraphicsType);
186 BOOST_ASSERT(!types.empty());
188 // select prefered type if AnyGraphicsType was passed
189 if (type == Clipboard::AnyGraphicsType)
190 type = types.front();
193 map<Clipboard::GraphicsType, string> extensions;
194 map<Clipboard::GraphicsType, docstring> typeNames;
196 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
197 extensions[Clipboard::PdfGraphicsType] = "pdf";
198 extensions[Clipboard::PngGraphicsType] = "png";
199 extensions[Clipboard::JpegGraphicsType] = "jpeg";
201 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
202 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
203 typeNames[Clipboard::PngGraphicsType] = _("PNG");
204 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
206 // find unused filename with primary extension
207 string document_path = cur.buffer().fileName().onlyPath().absFilename();
208 unsigned newfile_number = 0;
213 = FileName(addName(document_path,
215 + convert<string>(newfile_number) + "."
216 + extensions[type]));
217 } while (filename.isReadableFile());
220 // create file type filter, putting the prefered on to the front
221 docstring filterSpec;
222 for (unsigned i = 0; i < types.size(); ++i) {
223 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
224 + " (*." + from_ascii(extensions[types[i]]) + ")";
225 if (types[i] == type)
226 filterSpec = s + filterSpec;
228 filterSpec += ";;" + s;
230 FileFilterList const filter(filterSpec);
232 // show save dialog for the graphic
233 FileDialog dlg(_("Choose a filename to save the pasted graphic as"));
234 FileDialog::Result result =
235 dlg.save(from_utf8(filename.onlyPath().absFilename()), filter,
236 from_utf8(filename.onlyFileName()));
238 if (result.first == FileDialog::Later)
241 string newFilename = to_utf8(result.second);
242 if (newFilename.empty()) {
243 cur.bv().message(_("Canceled."));
246 filename.set(newFilename);
248 // check the extension (the user could have changed it)
249 if (!suffixIs(ascii_lowercase(filename.absFilename()),
250 "." + extensions[type])) {
251 // the user changed the extension. Check if the type is available
253 for (i = 1; i < types.size(); ++i) {
254 if (suffixIs(ascii_lowercase(filename.absFilename()),
255 "." + extensions[types[i]])) {
261 // invalid extension found, or none at all. In the latter
262 // case set the default extensions.
263 if (i == types.size()
264 && filename.onlyFileName().find('.') == string::npos) {
265 filename.changeExtension("." + extensions[type]);
269 // check whether the file exists and warn the user
270 if (!filename.exists())
272 int ret = frontend::Alert::prompt(
273 _("Overwrite external file?"),
274 bformat(_("File %1$s already exists, do you want to overwrite it"),
275 from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
277 // overwrite, hence break the dialog loop
280 // not overwrite, hence show the dialog again (i.e. loop)
287 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
289 // get the filename from the user
290 FileName filename = getPastedGraphicsFileName(cur, type);
291 if (filename.empty())
294 // handle image cases first
295 if (type == PngGraphicsType || type == JpegGraphicsType) {
296 // get image from QImage from clipboard
297 QImage image = qApp->clipboard()->image();
298 if (image.isNull()) {
299 LYXERR(Debug::ACTION, "No image in clipboard");
303 // convert into graphics format
306 buffer.open(QIODevice::WriteOnly);
307 if (type == PngGraphicsType)
308 image.save(toqstr(filename.absFilename()), "PNG");
309 else if (type == JpegGraphicsType)
310 image.save(toqstr(filename.absFilename()), "JPEG");
318 QMimeData const * source =
319 qApp->clipboard()->mimeData(QClipboard::Clipboard);
321 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
328 case PdfGraphicsType: mime = pdf_mime_type; break;
329 case LinkBackGraphicsType: mime = pdf_mime_type; break;
330 default: BOOST_ASSERT(false);
334 if (!source->hasFormat(mime))
336 // data from ourself or some other LyX instance
337 QByteArray const ar = source->data(mime);
338 LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
339 << "length = " << ar.count());
341 QFile f(toqstr(filename.absFilename()));
342 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
343 LYXERR(Debug::ACTION, "Error opening file "
344 << filename.absFilename() << " for writing");
348 // write the (LinkBack) PDF data
350 if (type == LinkBackGraphicsType) {
352 void const * linkBackData;
353 unsigned linkBackLen;
354 getLinkBackData(&linkBackData, &linkBackLen);
355 f.write((char *)linkBackData, linkBackLen);
356 quint32 pdfLen = ar.size();
358 ds << pdfLen; // big endian by default
360 // only non-Mac this should never happen
370 docstring const GuiClipboard::getAsText() const
372 // text data from other applications
373 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
374 .normalized(QString::NormalizationForm_C);
375 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
379 return internalLineEnding(qstring_to_ucs4(str));
383 void GuiClipboard::put(string const & lyx, docstring const & text)
385 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
386 << to_utf8(text) << "')");
387 // We don't convert the encoding of lyx since the encoding of the
388 // clipboard contents is specified in the data itself
389 QMimeData * data = new QMimeData;
391 QByteArray const qlyx(lyx.c_str(), lyx.size());
392 data->setData(lyx_mime_type, qlyx);
394 // Don't test for text.empty() since we want to be able to clear the
396 QString const qtext = toqstr(text);
397 data->setText(qtext);
398 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
402 bool GuiClipboard::hasLyXContents() const
404 QMimeData const * const source =
405 qApp->clipboard()->mimeData(QClipboard::Clipboard);
406 return source && source->hasFormat(lyx_mime_type);
410 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
412 if (type == AnyGraphicsType) {
413 return hasGraphicsContents(PdfGraphicsType)
414 || hasGraphicsContents(PngGraphicsType)
415 || hasGraphicsContents(JpegGraphicsType)
416 || hasGraphicsContents(LinkBackGraphicsType);
419 QMimeData const * const source =
420 qApp->clipboard()->mimeData(QClipboard::Clipboard);
422 // handle image cases first
423 if (type == PngGraphicsType || type == JpegGraphicsType)
424 return source->hasImage();
426 // handle LinkBack for Mac
428 if (type == LinkBackGraphicsType)
429 return isLinkBackDataInPasteboard();
431 if (type == LinkBackGraphicsType)
436 QStringList const & formats = source->formats();
437 LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
438 for (int i = 0; i < formats.size(); ++i) {
439 LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
442 // compute mime for type
445 case PdfGraphicsType: mime = pdf_mime_type; break;
446 default: BOOST_ASSERT(false);
449 return source && source->hasFormat(mime);
453 bool GuiClipboard::isInternal() const
455 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
456 // the preamble dialog
457 // FIXME: This does only work on X11, since ownsClipboard() is
458 // hardwired to return false on Windows and OS X.
459 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
463 bool GuiClipboard::hasInternal() const
465 // Windows and Mac OS X does not have the concept of ownership;
466 // the clipboard is a fully global resource so all applications
467 // are notified of changes.
468 #if (defined(Q_WS_X11))
476 void GuiClipboard::on_dataChanged()
478 QMimeData const * const source =
479 qApp->clipboard()->mimeData(QClipboard::Clipboard);
480 QStringList l = source->formats();
481 LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
482 for (int i = 0; i < l.count(); i++) {
483 LYXERR(Debug::ACTION, fromqstr(l.value(i)));
486 text_clipboard_empty_ = qApp->clipboard()->
487 text(QClipboard::Clipboard).isEmpty();
489 has_lyx_contents_ = hasLyXContents();
490 has_graphics_contents_ = hasGraphicsContents();
494 bool GuiClipboard::empty() const
496 // We need to check both the plaintext and the LyX version of the
497 // clipboard. The plaintext version is empty if the LyX version
498 // contains only one inset, and the LyX version is empty if the
499 // clipboard does not come from LyX.
500 if (!text_clipboard_empty_)
502 return !has_lyx_contents_ && !has_graphics_contents_;
505 } // namespace frontend
508 #include "GuiClipboard_moc.cpp"