X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiClipboard.cpp;h=297b828948e45612d0a50456fcfe8ddfe70f4526;hb=8e7d7e42952f0119db1c192386a63d1d9ede3b7f;hp=c56b1f44626c13fb7d65479f90c914c537a2a401;hpb=4d188198ba395743b6cd829e925bcda69243d223;p=lyx.git diff --git a/src/frontends/qt4/GuiClipboard.cpp b/src/frontends/qt4/GuiClipboard.cpp index c56b1f4462..297b828948 100644 --- a/src/frontends/qt4/GuiClipboard.cpp +++ b/src/frontends/qt4/GuiClipboard.cpp @@ -27,6 +27,7 @@ #include "support/filetools.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/lyxtime.h" #ifdef Q_WS_MACX #include "support/linkback/LinkBackProxy.h" @@ -46,6 +47,7 @@ #include #include +#include using namespace std; using namespace lyx::support; @@ -55,10 +57,48 @@ namespace lyx { namespace frontend { -char const * lyx_mime_type = "application/x-lyx"; -char const * pdf_mime_type = "application/pdf"; -char const * emf_mime_type = "image/x-emf"; -char const * wmf_mime_type = "image/x-wmf"; +static QMimeData const * read_clipboard() +{ + LYXERR(Debug::ACTION, "Getting Clipboard"); + QMimeData const * source = + qApp->clipboard()->mimeData(QClipboard::Clipboard); + if (!source) { + LYXERR0("0 bytes (no QMimeData)"); + return new QMimeData(); + } + // 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; +} + + +void CacheMimeData::update() +{ + 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?"); + } +} + + +QByteArray CacheMimeData::data(QString const & mimeType) const +{ + return read_clipboard()->data(mimeType); +} + + +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() @@ -75,16 +115,9 @@ string const GuiClipboard::getAsLyX() const 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; @@ -137,7 +170,7 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, 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 { @@ -164,7 +197,7 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, // 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) @@ -178,12 +211,12 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, 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; @@ -204,7 +237,7 @@ FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur, 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; @@ -237,45 +270,37 @@ FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) cons 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 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; + 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(); } @@ -310,7 +335,7 @@ docstring const GuiClipboard::getAsText() const if (str.isNull()) return docstring(); - return internalLineEnding(qstring_to_ucs4(str)); + return internalLineEnding(str); } @@ -323,7 +348,7 @@ void GuiClipboard::put(string const & lyx, docstring const & text) 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); } // Don't test for text.empty() since we want to be able to clear the // clipboard. @@ -335,9 +360,13 @@ void GuiClipboard::put(string const & lyx, docstring const & text) 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(); } @@ -352,12 +381,9 @@ bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const || 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 if (type == LinkBackGraphicsType) @@ -368,7 +394,7 @@ bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const #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 " << formats[i]); @@ -376,13 +402,13 @@ bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const // 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; + 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); } @@ -411,9 +437,14 @@ bool GuiClipboard::hasInternal() const 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, l.value(i)); @@ -440,4 +471,4 @@ bool GuiClipboard::empty() const } // namespace frontend } // namespace lyx -#include "GuiClipboard_moc.cpp" +#include "moc_GuiClipboard.cpp"