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 "GuiClipboard.h"
18 #include "qt_helpers.h"
20 #include "frontends/alert.h"
23 #include "BufferView.h"
26 #include "support/convert.h"
27 #include "support/debug.h"
28 #include "support/filetools.h"
29 #include "support/FileFilterList.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include <QApplication>
36 #include <QDataStream>
39 #include <QMacPasteboardMime>
42 #include <QStringList>
44 #include "boost/assert.hpp"
49 #include "support/linkback/LinkBackProxy.h"
53 using namespace lyx::support;
55 static char const * const lyx_mime_type = "application/x-lyx";
56 static char const * const pdf_mime_type = "application/pdf";
64 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
66 QMacPasteboardMimeGraphics()
67 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
69 ~QMacPasteboardMimeGraphics() {}
70 QString convertorName();
71 QString flavorFor(const QString & mime);
72 QString mimeFor(QString flav);
73 bool canConvert(const QString & mime, QString flav);
74 QVariant convertToMime(const QString & mime, QList<QByteArray> data, QString flav);
75 QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
79 QString QMacPasteboardMimeGraphics::convertorName()
85 QString QMacPasteboardMimeGraphics::flavorFor(const QString & mime)
87 LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
88 if (mime == QLatin1String(pdf_mime_type))
89 return QLatin1String("com.adobe.pdf");
94 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
96 LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
97 if (flav == QLatin1String("com.adobe.pdf"))
98 return QLatin1String(pdf_mime_type);
103 bool QMacPasteboardMimeGraphics::canConvert(const QString & mime, QString flav)
105 return mimeFor(flav) == mime;
109 QVariant QMacPasteboardMimeGraphics::convertToMime(const QString & mime, QList<QByteArray> data, QString)
112 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
117 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
119 QList<QByteArray> ret;
120 ret.append(data.toByteArray());
124 static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
129 GuiClipboard::GuiClipboard()
131 connect(qApp->clipboard(), SIGNAL(dataChanged()),
132 this, SLOT(on_dataChanged()));
133 // initialize clipboard status.
137 if (!graphicsPasteboardMime)
138 graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
143 GuiClipboard::~GuiClipboard()
146 closeAllLinkBackLinks();
151 string const GuiClipboard::getAsLyX() const
153 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
154 // We don't convert encodings here since the encoding of the
155 // clipboard contents is specified in the data itself
156 QMimeData const * source =
157 qApp->clipboard()->mimeData(QClipboard::Clipboard);
159 LYXERR(Debug::ACTION, "' (no QMimeData)");
163 if (source->hasFormat(lyx_mime_type)) {
164 // data from ourself or some other LyX instance
165 QByteArray const ar = source->data(lyx_mime_type);
166 string const s(ar.data(), ar.count());
167 LYXERR(Debug::ACTION, s << "'");
170 LYXERR(Debug::ACTION, "'");
175 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
176 Clipboard::GraphicsType & type) const
178 // create file dialog filter according to the existing types in the clipboard
179 vector<Clipboard::GraphicsType> types;
180 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
181 types.push_back(Clipboard::LinkBackGraphicsType);
182 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
183 types.push_back(Clipboard::PdfGraphicsType);
184 if (hasGraphicsContents(Clipboard::PngGraphicsType))
185 types.push_back(Clipboard::PngGraphicsType);
186 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
187 types.push_back(Clipboard::JpegGraphicsType);
189 BOOST_ASSERT(!types.empty());
191 // select prefered type if AnyGraphicsType was passed
192 if (type == Clipboard::AnyGraphicsType)
193 type = types.front();
196 map<Clipboard::GraphicsType, string> extensions;
197 map<Clipboard::GraphicsType, docstring> typeNames;
199 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
200 extensions[Clipboard::PdfGraphicsType] = "pdf";
201 extensions[Clipboard::PngGraphicsType] = "png";
202 extensions[Clipboard::JpegGraphicsType] = "jpeg";
204 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
205 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
206 typeNames[Clipboard::PngGraphicsType] = _("PNG");
207 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
209 // find unused filename with primary extension
210 string document_path = cur.buffer().fileName().onlyPath().absFilename();
211 unsigned newfile_number = 0;
215 filename = FileName(addName(document_path,
217 + convert<string>(newfile_number) + "."
218 + extensions[type]));
219 } while (filename.isReadableFile());
222 // create file type filter, putting the prefered on to the front
223 docstring filterSpec;
224 for (size_t i = 0; i != types.size(); ++i) {
225 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
226 + " (*." + from_ascii(extensions[types[i]]) + ")";
227 if (types[i] == type)
228 filterSpec = s + filterSpec;
230 filterSpec += ";;" + s;
232 FileFilterList const filter(filterSpec);
234 // show save dialog for the graphic
235 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
236 FileDialog::Result result =
237 dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
238 toqstr(filename.onlyFileName()));
240 if (result.first == FileDialog::Later)
243 string newFilename = fromqstr(result.second);
244 if (newFilename.empty()) {
245 cur.bv().message(_("Canceled."));
248 filename.set(newFilename);
250 // check the extension (the user could have changed it)
251 if (!suffixIs(ascii_lowercase(filename.absFilename()),
252 "." + extensions[type])) {
253 // the user changed the extension. Check if the type is available
255 for (i = 1; i != types.size(); ++i) {
256 if (suffixIs(ascii_lowercase(filename.absFilename()),
257 "." + extensions[types[i]])) {
263 // invalid extension found, or none at all. In the latter
264 // case set the default extensions.
265 if (i == types.size()
266 && filename.onlyFileName().find('.') == string::npos) {
267 filename.changeExtension("." + extensions[type]);
271 // check whether the file exists and warn the user
272 if (!filename.exists())
274 int ret = frontend::Alert::prompt(
275 _("Overwrite external file?"),
276 bformat(_("File %1$s already exists, do you want to overwrite it"),
277 from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
279 // overwrite, hence break the dialog loop
282 // not overwrite, hence show the dialog again (i.e. loop)
289 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
291 // get the filename from the user
292 FileName filename = getPastedGraphicsFileName(cur, type);
293 if (filename.empty())
296 // handle image cases first
297 if (type == PngGraphicsType || type == JpegGraphicsType) {
298 // get image from QImage from clipboard
299 QImage image = qApp->clipboard()->image();
300 if (image.isNull()) {
301 LYXERR(Debug::ACTION, "No image in clipboard");
305 // convert into graphics format
308 buffer.open(QIODevice::WriteOnly);
309 if (type == PngGraphicsType)
310 image.save(toqstr(filename.absFilename()), "PNG");
311 else if (type == JpegGraphicsType)
312 image.save(toqstr(filename.absFilename()), "JPEG");
320 QMimeData const * source =
321 qApp->clipboard()->mimeData(QClipboard::Clipboard);
323 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
330 case PdfGraphicsType: mime = pdf_mime_type; break;
331 case LinkBackGraphicsType: mime = pdf_mime_type; break;
332 default: BOOST_ASSERT(false);
336 if (!source->hasFormat(mime))
338 // data from ourself or some other LyX instance
339 QByteArray const ar = source->data(mime);
340 LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
341 << "length = " << ar.count());
343 QFile f(toqstr(filename.absFilename()));
344 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
345 LYXERR(Debug::ACTION, "Error opening file "
346 << filename.absFilename() << " for writing");
350 // write the (LinkBack) PDF data
352 if (type == LinkBackGraphicsType) {
354 void const * linkBackData;
355 unsigned linkBackLen;
356 getLinkBackData(&linkBackData, &linkBackLen);
357 f.write((char *)linkBackData, linkBackLen);
358 quint32 pdfLen = ar.size();
360 ds << pdfLen; // big endian by default
362 // only non-Mac this should never happen
372 docstring const GuiClipboard::getAsText() const
374 // text data from other applications
375 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
376 .normalized(QString::NormalizationForm_C);
377 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
381 return internalLineEnding(qstring_to_ucs4(str));
385 void GuiClipboard::put(string const & lyx, docstring const & text)
387 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
388 << to_utf8(text) << "')");
389 // We don't convert the encoding of lyx since the encoding of the
390 // clipboard contents is specified in the data itself
391 QMimeData * data = new QMimeData;
393 QByteArray const qlyx(lyx.c_str(), lyx.size());
394 data->setData(lyx_mime_type, qlyx);
396 // Don't test for text.empty() since we want to be able to clear the
398 QString const qtext = toqstr(text);
399 data->setText(qtext);
400 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
404 bool GuiClipboard::hasLyXContents() const
406 QMimeData const * const source =
407 qApp->clipboard()->mimeData(QClipboard::Clipboard);
408 return source && source->hasFormat(lyx_mime_type);
412 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
414 if (type == AnyGraphicsType) {
415 return hasGraphicsContents(PdfGraphicsType)
416 || hasGraphicsContents(PngGraphicsType)
417 || hasGraphicsContents(JpegGraphicsType)
418 || hasGraphicsContents(LinkBackGraphicsType);
421 QMimeData const * const source =
422 qApp->clipboard()->mimeData(QClipboard::Clipboard);
424 // handle image cases first
425 if (type == PngGraphicsType || type == JpegGraphicsType)
426 return source->hasImage();
428 // handle LinkBack for Mac
430 if (type == LinkBackGraphicsType)
431 return isLinkBackDataInPasteboard();
433 if (type == LinkBackGraphicsType)
438 QStringList const & formats = source->formats();
439 LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
440 for (int i = 0; i < formats.size(); ++i) {
441 LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
444 // compute mime for type
447 case PdfGraphicsType: mime = pdf_mime_type; break;
448 default: BOOST_ASSERT(false);
451 return source && source->hasFormat(mime);
455 bool GuiClipboard::isInternal() const
457 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
458 // the preamble dialog
459 // FIXME: This does only work on X11, since ownsClipboard() is
460 // hardwired to return false on Windows and OS X.
461 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
465 bool GuiClipboard::hasInternal() const
467 // Windows and Mac OS X does not have the concept of ownership;
468 // the clipboard is a fully global resource so all applications
469 // are notified of changes.
470 #if (defined(Q_WS_X11))
478 void GuiClipboard::on_dataChanged()
480 QMimeData const * const source =
481 qApp->clipboard()->mimeData(QClipboard::Clipboard);
482 QStringList l = source->formats();
483 LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
484 for (int i = 0; i < l.count(); i++) {
485 LYXERR(Debug::ACTION, fromqstr(l.value(i)));
488 text_clipboard_empty_ = qApp->clipboard()->
489 text(QClipboard::Clipboard).isEmpty();
491 has_lyx_contents_ = hasLyXContents();
492 has_graphics_contents_ = hasGraphicsContents();
496 bool GuiClipboard::empty() const
498 // We need to check both the plaintext and the LyX version of the
499 // clipboard. The plaintext version is empty if the LyX version
500 // contains only one inset, and the LyX version is empty if the
501 // clipboard does not come from LyX.
502 if (!text_clipboard_empty_)
504 return !has_lyx_contents_ && !has_graphics_contents_;
507 } // namespace frontend
510 #include "GuiClipboard_moc.cpp"