#include "GuiClipboard.h"
#include "qt_helpers.h"
-#include "frontends/alert.h"
-
#include "Buffer.h"
#include "BufferView.h"
#include "Cursor.h"
+#include "support/lassert.h"
#include "support/convert.h"
#include "support/debug.h"
#include "support/filetools.h"
-#include "support/FileFilterList.h"
#include "support/gettext.h"
#include "support/lstrings.h"
+#include "support/lyxtime.h"
+
+#ifdef Q_WS_MACX
+#include "support/linkback/LinkBackProxy.h"
+#endif // Q_WS_MACX
+
+#include "frontends/alert.h"
#include <QApplication>
#include <QBuffer>
#include <QDataStream>
#include <QFile>
#include <QImage>
-#include <QMacPasteboardMime>
#include <QMimeData>
#include <QString>
#include <QStringList>
-#ifdef Q_WS_WIN
-#include <QWindowsMime>
-#ifdef Q_CYGWIN_WIN
-#include <wtypes.h>
-#endif
-#include <objidl.h>
-#endif // Q_WS_WIN
-
-#include "boost/assert.hpp"
+#include <boost/crc.hpp>
+#include <memory>
#include <map>
-
-#ifdef Q_WS_MACX
-#include "support/linkback/LinkBackProxy.h"
-#endif // Q_WS_MACX
+#include <iostream>
using namespace std;
using namespace lyx::support;
-static char const * const lyx_mime_type = "application/x-lyx";
-static char const * const pdf_mime_type = "application/pdf";
-static char const * const emf_mime_type = "image/x-emf";
-static char const * const wmf_mime_type = "image/x-wmf";
namespace lyx {
namespace frontend {
-#ifdef Q_WS_WIN
-
-static FORMATETC cfFromMime(QString const & mimetype)
+static QMimeData const * read_clipboard()
{
- FORMATETC formatetc;
- if (mimetype == emf_mime_type) {
- formatetc.cfFormat = CF_ENHMETAFILE;
- formatetc.tymed = TYMED_ENHMF;
- }
- else if (mimetype == wmf_mime_type) {
- formatetc.cfFormat = CF_METAFILEPICT;
- formatetc.tymed = TYMED_MFPICT;
+ LYXERR(Debug::ACTION, "Getting Clipboard");
+ QMimeData const * source =
+ qApp->clipboard()->mimeData(QClipboard::Clipboard);
+ if (!source) {
+ LYXERR0("0 bytes (no QMimeData)");
+ return new QMimeData();
}
- else return formatetc;
- formatetc.ptd = NULL;
- formatetc.dwAspect = DVASPECT_CONTENT;
- formatetc.lindex = -1;
- return formatetc;
-}
-
-
-class QWindowsMimeMetafile : public QWindowsMime {
-public:
- bool canConvertFromMime(FORMATETC const & formatetc, QMimeData const * mimedata) const;
- bool canConvertToMime(QString const & mimetype, IDataObject * pDataObj) const;
- bool convertFromMime(FORMATETC const & formatetc, const QMimeData * mimedata, STGMEDIUM * pmedium) const;
- QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj, QVariant::Type preferredType) const;
- QVector<FORMATETC> formatsForMime(QString const & mimeType, QMimeData const * mimeData) const;
- QString mimeForFormat(FORMATETC const &) const;
-};
-
-
-QString QWindowsMimeMetafile::mimeForFormat(FORMATETC const & formatetc) const
-{
- QString f;
- if (formatetc.cfFormat == CF_ENHMETAFILE)
- f = emf_mime_type;
- else if (formatetc.cfFormat == CF_METAFILEPICT)
- f = wmf_mime_type;
- return f;
-}
-
-
-bool QWindowsMimeMetafile::canConvertFromMime(
- FORMATETC const & formatetc, QMimeData const * mimedata) const
-{
- return false;
-}
-
-
-bool QWindowsMimeMetafile::canConvertToMime(
- QString const & mimetype, IDataObject * pDataObj) const
-{
- FORMATETC formatetc = cfFromMime(mimetype);
- return pDataObj->QueryGetData(&formatetc) == S_OK;
-}
-
-
-bool QWindowsMimeMetafile::convertFromMime(
- FORMATETC const & formatetc, QMimeData const * mimedata,
- STGMEDIUM * pmedium) const
-{
- return false;
+ // It appears that doing IO between getting a mimeData object
+ // and using it can cause a crash (maybe Qt used IO
+ // as an excuse to free() it? Anyway let's not introduce
+ // any new IO here, so e.g. leave the following line commented.
+ // lyxerr << "Got Clipboard (" << (long) source << ")\n" ;
+ return source;
}
-QVariant QWindowsMimeMetafile::convertToMime(QString const & mimetype,
- IDataObject * pDataObj, QVariant::Type preferredType) const
+void CacheMimeData::update()
{
- QByteArray data;
- if (!canConvertToMime(mimetype, pDataObj))
- return data;
-
- FORMATETC formatetc = cfFromMime(mimetype);
- STGMEDIUM s;
- if (pDataObj->GetData(&formatetc, &s) != S_OK)
- return data;
-
- int dataSize;
- if (s.tymed == TYMED_ENHMF) {
- dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, NULL);
- data.resize(dataSize);
- dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize, (LPBYTE)data.data());
- } else if (s.tymed == TYMED_MFPICT) {
- dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, NULL);
- data.resize(dataSize);
- dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize, (LPBYTE)data.data());
+ time_t const start_time = current_time();
+ LYXERR(Debug::ACTION, "Creating CacheMimeData object");
+ cached_formats_ = read_clipboard()->formats();
+
+ // Qt times out after 5 seconds if it does not recieve a response.
+ if (current_time() - start_time > 3) {
+ LYXERR0("No timely response from clipboard, perhaps process "
+ << "holding clipboard is frozen?");
}
- data.detach();
- ReleaseStgMedium(&s);
-
- return data;
}
-QVector<FORMATETC> QWindowsMimeMetafile::formatsForMime(
- QString const & mimetype, QMimeData const * mimedata) const
+QByteArray CacheMimeData::data(QString const & mimeType) const
{
- QVector<FORMATETC> formats;
- formats += cfFromMime(mimetype);
- return formats;
+ return read_clipboard()->data(mimeType);
}
-static QWindowsMimeMetafile * metafileWindowsMime;
-
-#endif // Q_WS_WIN
-
-#ifdef Q_WS_MACX
-
-class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
-public:
- QMacPasteboardMimeGraphics()
- : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
- {}
- ~QMacPasteboardMimeGraphics() {}
- QString convertorName();
- QString flavorFor(const QString & mime);
- QString mimeFor(QString flav);
- bool canConvert(const QString & mime, QString flav);
- QVariant convertToMime(const QString & mime, QList<QByteArray> data, QString flav);
- QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
-};
-
-
-QString QMacPasteboardMimeGraphics::convertorName()
-{
- return "Graphics";
-}
-
-QString QMacPasteboardMimeGraphics::flavorFor(const QString & mime)
-{
- LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
- if (mime == QLatin1String(pdf_mime_type))
- return QLatin1String("com.adobe.pdf");
- return QString();
-}
-
-
-QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
-{
- LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
- if (flav == QLatin1String("com.adobe.pdf"))
- return QLatin1String(pdf_mime_type);
- return QString();
-}
-
-
-bool QMacPasteboardMimeGraphics::canConvert(const QString & mime, QString flav)
-{
- return mimeFor(flav) == mime;
-}
-
-
-QVariant QMacPasteboardMimeGraphics::convertToMime(const QString & mime, QList<QByteArray> data, QString)
-{
- if(data.count() > 1)
- qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
- return data.first();
-}
-
-
-QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
-{
- QList<QByteArray> ret;
- ret.append(data.toByteArray());
- return ret;
-}
-
-static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
-
-#endif // Q_WS_MACX
+QString const lyxMimeType(){ return "application/x-lyx"; }
+QString const pdfMimeType(){ return "application/pdf"; }
+QString const emfMimeType(){ return "image/x-emf"; }
+QString const wmfMimeType(){ return "image/x-wmf"; }
GuiClipboard::GuiClipboard()
this, SLOT(on_dataChanged()));
// initialize clipboard status.
on_dataChanged();
-
-#ifdef Q_WS_MACX
- if (!graphicsPasteboardMime)
- graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
-#endif // Q_WS_MACX
-
-#ifdef Q_WS_WIN
- if (!metafileWindowsMime)
- metafileWindowsMime = new QWindowsMimeMetafile();
-#endif // Q_WS_WIN
-}
-
-
-GuiClipboard::~GuiClipboard()
-{
-#ifdef Q_WS_MACX
- closeAllLinkBackLinks();
-#endif // Q_WS_MACX
}
LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
// We don't convert encodings here since the encoding of the
// clipboard contents is specified in the data itself
- QMimeData const * source =
- qApp->clipboard()->mimeData(QClipboard::Clipboard);
- if (!source) {
- LYXERR(Debug::ACTION, "' (no QMimeData)");
- return string();
- }
-
- if (source->hasFormat(lyx_mime_type)) {
+ if (cache_.hasFormat(lyxMimeType())) {
// data from ourself or some other LyX instance
- QByteArray const ar = source->data(lyx_mime_type);
+ QByteArray const ar = cache_.data(lyxMimeType());
string const s(ar.data(), ar.count());
LYXERR(Debug::ACTION, s << "'");
return s;
if (hasGraphicsContents(Clipboard::JpegGraphicsType))
types.push_back(Clipboard::JpegGraphicsType);
- BOOST_ASSERT(!types.empty());
+ LASSERT(!types.empty(), /**/);
// select prefered type if AnyGraphicsType was passed
if (type == Clipboard::AnyGraphicsType)
typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
// find unused filename with primary extension
- string document_path = cur.buffer().fileName().onlyPath().absFilename();
+ string document_path = cur.buffer()->fileName().onlyPath().absFileName();
unsigned newfile_number = 0;
FileName filename;
do {
while (true) {
// create file type filter, putting the prefered on to the front
- docstring filterSpec;
+ QStringList filter;
for (size_t i = 0; i != types.size(); ++i) {
docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
+ " (*." + from_ascii(extensions[types[i]]) + ")";
if (types[i] == type)
- filterSpec = s + filterSpec;
+ filter.prepend(toqstr(s));
else
- filterSpec += ";;" + s;
+ filter.append(toqstr(s));
}
- FileFilterList const filter(filterSpec);
+ filter = fileFilters(filter.join(";;"));
// show save dialog for the graphic
FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
FileDialog::Result result =
- dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
+ dlg.save(toqstr(filename.onlyPath().absFileName()), filter,
toqstr(filename.onlyFileName()));
if (result.first == FileDialog::Later)
filename.set(newFilename);
// check the extension (the user could have changed it)
- if (!suffixIs(ascii_lowercase(filename.absFilename()),
+ if (!suffixIs(ascii_lowercase(filename.absFileName()),
"." + extensions[type])) {
// the user changed the extension. Check if the type is available
size_t i;
for (i = 1; i != types.size(); ++i) {
- if (suffixIs(ascii_lowercase(filename.absFilename()),
+ if (suffixIs(ascii_lowercase(filename.absFileName()),
"." + extensions[types[i]])) {
type = types[i];
break;
int ret = frontend::Alert::prompt(
_("Overwrite external file?"),
bformat(_("File %1$s already exists, do you want to overwrite it?"),
- from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
+ from_utf8(filename.absFileName())), 1, 1, _("&Overwrite"), _("&Cancel"));
if (ret == 0)
// overwrite, hence break the dialog loop
break;
QBuffer buffer(&ar);
buffer.open(QIODevice::WriteOnly);
if (type == PngGraphicsType)
- image.save(toqstr(filename.absFilename()), "PNG");
+ image.save(toqstr(filename.absFileName()), "PNG");
else if (type == JpegGraphicsType)
- image.save(toqstr(filename.absFilename()), "JPEG");
+ image.save(toqstr(filename.absFileName()), "JPEG");
else
- BOOST_ASSERT(false);
+ LASSERT(false, /**/);
return filename;
}
- // get mime data
- QMimeData const * source =
- qApp->clipboard()->mimeData(QClipboard::Clipboard);
- if (!source) {
- LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
- return FileName();
- }
-
// get mime for type
QString mime;
switch (type) {
- case PdfGraphicsType: mime = pdf_mime_type; break;
- case LinkBackGraphicsType: mime = pdf_mime_type; break;
- case EmfGraphicsType: mime = emf_mime_type; break;
- case WmfGraphicsType: mime = wmf_mime_type; break;
- default: BOOST_ASSERT(false);
+ case PdfGraphicsType: mime = pdfMimeType(); break;
+ case LinkBackGraphicsType: mime = pdfMimeType(); break;
+ case EmfGraphicsType: mime = emfMimeType(); break;
+ case WmfGraphicsType: mime = wmfMimeType(); break;
+ default: LASSERT(false, /**/);
}
// get data
- if (!source->hasFormat(mime))
+ if (!cache_.hasFormat(mime))
return FileName();
// data from ourself or some other LyX instance
- QByteArray const ar = source->data(mime);
+ QByteArray const ar = cache_.data(mime);
LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
<< "length = " << ar.count());
- QFile f(toqstr(filename.absFilename()));
+ QFile f(toqstr(filename.absFileName()));
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
LYXERR(Debug::ACTION, "Error opening file "
- << filename.absFilename() << " for writing");
+ << filename.absFileName() << " for writing");
return FileName();
}
ds << pdfLen; // big endian by default
#else
// only non-Mac this should never happen
- BOOST_ASSERT(false);
+ LASSERT(false, /**/);
#endif // Q_WS_MACX
}
// text data from other applications
QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
.normalized(QString::NormalizationForm_C);
- LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
+ LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << str << "'");
if (str.isNull())
return docstring();
- return internalLineEnding(qstring_to_ucs4(str));
+ return internalLineEnding(str);
}
QMimeData * data = new QMimeData;
if (!lyx.empty()) {
QByteArray const qlyx(lyx.c_str(), lyx.size());
- data->setData(lyx_mime_type, qlyx);
+ data->setData(lyxMimeType(), qlyx);
+ // If the OS has not the concept of clipboard ownership,
+ // we recognize internal data through its checksum.
+ if (!hasInternal()) {
+ boost::crc_32_type crc32;
+ crc32.process_bytes(lyx.c_str(), lyx.size());
+ checksum = crc32.checksum();
+ }
}
// Don't test for text.empty() since we want to be able to clear the
// clipboard.
bool GuiClipboard::hasLyXContents() const
{
- QMimeData const * const source =
- qApp->clipboard()->mimeData(QClipboard::Clipboard);
- return source && source->hasFormat(lyx_mime_type);
+ return cache_.hasFormat(lyxMimeType());
+}
+
+
+bool GuiClipboard::hasTextContents() const
+{
+ return cache_.hasText();
}
|| hasGraphicsContents(LinkBackGraphicsType);
}
- QMimeData const * const source =
- qApp->clipboard()->mimeData(QClipboard::Clipboard);
-
// handle image cases first
if (type == PngGraphicsType || type == JpegGraphicsType)
- return source->hasImage();
+ return cache_.hasImage();
// handle LinkBack for Mac
-#ifdef Q_WS_MACX
if (type == LinkBackGraphicsType)
+#ifdef Q_WS_MACX
return isLinkBackDataInPasteboard();
#else
- if (type == LinkBackGraphicsType)
return false;
#endif // Q_WS_MACX
// get mime data
- QStringList const & formats = source->formats();
+ QStringList const & formats = cache_.formats();
LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
- for (int i = 0; i < formats.size(); ++i) {
- LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
- }
+ for (int i = 0; i < formats.size(); ++i)
+ LYXERR(Debug::ACTION, "Found format " << formats[i]);
// compute mime for type
QString mime;
switch (type) {
- case EmfGraphicsType: mime = emf_mime_type; break;
- case WmfGraphicsType: mime = wmf_mime_type; break;
- case PdfGraphicsType: mime = pdf_mime_type; break;
- default: BOOST_ASSERT(false);
+ case EmfGraphicsType: mime = emfMimeType(); break;
+ case WmfGraphicsType: mime = wmfMimeType(); break;
+ case PdfGraphicsType: mime = pdfMimeType(); break;
+ default: LASSERT(false, /**/);
}
- return source && source->hasFormat(mime);
+ return cache_.hasFormat(mime);
}
bool GuiClipboard::isInternal() const
{
+ if (!hasLyXContents())
+ return false;
+
// ownsClipboard() is also true for stuff coming from dialogs, e.g.
- // the preamble dialog
- // FIXME: This does only work on X11, since ownsClipboard() is
- // hardwired to return false on Windows and OS X.
- return qApp->clipboard()->ownsClipboard() && hasLyXContents();
+ // the preamble dialog. This does only work on X11 and Windows, since
+ // ownsClipboard() is hardwired to return false on OS X.
+ if (hasInternal())
+ return qApp->clipboard()->ownsClipboard();
+
+ // We are running on OS X: Check whether clipboard data is from
+ // ourself by comparing its checksum with the stored one.
+ QByteArray const ar = cache_.data(lyxMimeType());
+ string const data(ar.data(), ar.count());
+ boost::crc_32_type crc32;
+ crc32.process_bytes(data.c_str(), data.size());
+ return checksum == crc32.checksum();
}
{
// Windows and Mac OS X does not have the concept of ownership;
// the clipboard is a fully global resource so all applications
- // are notified of changes.
-#if (defined(Q_WS_X11))
+ // are notified of changes. However, on Windows ownership is
+ // emulated by Qt through the OleIsCurrentClipboard() API, while
+ // on Mac OS X we deal with this issue by ourself.
+#if (defined(Q_WS_X11) || defined(Q_WS_WIN))
return true;
#else
return false;
void GuiClipboard::on_dataChanged()
{
- QMimeData const * const source =
- qApp->clipboard()->mimeData(QClipboard::Clipboard);
- QStringList l = source->formats();
+ //Note: we do not really need to run cache_.update() unless the
+ //data has been changed *and* the GuiClipboard has been queried.
+ //However if run cache_.update() the moment a process grabs the
+ //clipboard, the process holding the clipboard presumably won't
+ //yet be frozen, and so we won't need to wait 5 seconds for Qt
+ //to time-out waiting for the clipboard.
+ cache_.update();
+ QStringList l = cache_.formats();
LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
- for (int i = 0; i < l.count(); i++) {
- LYXERR(Debug::ACTION, fromqstr(l.value(i)));
- }
+ for (int i = 0; i < l.count(); i++)
+ LYXERR(Debug::ACTION, l.value(i));
text_clipboard_empty_ = qApp->clipboard()->
text(QClipboard::Clipboard).isEmpty();
} // namespace frontend
} // namespace lyx
-#include "GuiClipboard_moc.cpp"
+#include "moc_GuiClipboard.cpp"