]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[lyx.git] / src / frontends / qt4 / GuiClipboard.cpp
1 // -*- C++ -*-
2 /**
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.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "FileDialog.h"
16
17 #include "GuiClipboard.h"
18 #include "qt_helpers.h"
19
20 #include "Buffer.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23
24 #include "support/assert.h"
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/filetools.h"
28 #include "support/FileFilterList.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #ifdef Q_WS_MACX
32 #include "support/linkback/LinkBackProxy.h"
33 #endif // Q_WS_MACX
34
35 #include "frontends/alert.h"
36
37 #include <QApplication>
38 #include <QBuffer>
39 #include <QClipboard>
40 #include <QDataStream>
41 #include <QFile>
42 #include <QImage>
43 #include <QMacPasteboardMime>
44 #include <QMimeData>
45 #include <QString>
46 #include <QStringList>
47
48 #ifdef Q_WS_WIN
49 #include <QWindowsMime>
50 #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW)
51 #include <wtypes.h>
52 #endif
53 #include <objidl.h>
54 #endif // Q_WS_WIN
55
56 #include <memory>
57 #include <map>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 static char const * const lyx_mime_type = "application/x-lyx";
63 static char const * const pdf_mime_type = "application/pdf";
64 static char const * const emf_mime_type = "image/x-emf";
65 static char const * const wmf_mime_type = "image/x-wmf";
66
67 namespace lyx {
68
69 namespace frontend {
70
71 #ifdef Q_WS_WIN
72
73 static FORMATETC cfFromMime(QString const & mimetype)
74 {
75         FORMATETC formatetc;
76         if (mimetype == emf_mime_type) {
77                 formatetc.cfFormat = CF_ENHMETAFILE;
78                 formatetc.tymed = TYMED_ENHMF;
79         } else if (mimetype == wmf_mime_type) {
80                 formatetc.cfFormat = CF_METAFILEPICT;
81                 formatetc.tymed = TYMED_MFPICT;
82         }
83         formatetc.ptd = 0;
84         formatetc.dwAspect = DVASPECT_CONTENT;
85         formatetc.lindex = -1;
86         return formatetc;
87 }
88
89
90 class QWindowsMimeMetafile : public QWindowsMime {
91 public:
92         bool canConvertFromMime(FORMATETC const & formatetc, QMimeData const * mimedata) const;
93         bool canConvertToMime(QString const & mimetype, IDataObject * pDataObj) const;
94         bool convertFromMime(FORMATETC const & formatetc, const QMimeData * mimedata, STGMEDIUM * pmedium) const;
95         QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj, QVariant::Type preferredType) const;
96         QVector<FORMATETC> formatsForMime(QString const & mimeType, QMimeData const * mimeData) const;
97         QString mimeForFormat(FORMATETC const &) const;
98 };
99
100
101 QString QWindowsMimeMetafile::mimeForFormat(FORMATETC const & formatetc) const
102 {
103         QString f;
104         if (formatetc.cfFormat == CF_ENHMETAFILE)
105                 f = emf_mime_type; 
106         else if (formatetc.cfFormat == CF_METAFILEPICT)
107                 f = wmf_mime_type;
108         return f;
109 }
110
111
112 bool QWindowsMimeMetafile::canConvertFromMime(FORMATETC const & formatetc, 
113         QMimeData const * mimedata) const
114 {
115         return false;
116 }
117
118
119 bool QWindowsMimeMetafile::canConvertToMime(QString const & mimetype,
120         IDataObject * pDataObj) const
121 {
122         if (mimetype != emf_mime_type && mimetype != wmf_mime_type)
123                 return false;
124         FORMATETC formatetc = cfFromMime(mimetype);
125         return pDataObj->QueryGetData(&formatetc) == S_OK;
126 }
127
128
129 bool QWindowsMimeMetafile::convertFromMime(FORMATETC const & formatetc,
130         QMimeData const * mimedata, STGMEDIUM * pmedium) const
131 {
132         return false;
133 }
134
135
136 QVariant QWindowsMimeMetafile::convertToMime(QString const & mimetype,
137         IDataObject * pDataObj, QVariant::Type preferredType) const
138 {
139         QByteArray data;
140         if (!canConvertToMime(mimetype, pDataObj))
141                 return data;
142
143         FORMATETC formatetc = cfFromMime(mimetype);
144         STGMEDIUM s;
145         if (pDataObj->GetData(&formatetc, &s) != S_OK)
146                 return data;
147
148         int dataSize;
149         if (s.tymed == TYMED_ENHMF) {
150                 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, 0);
151                 data.resize(dataSize);
152                 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize, (LPBYTE)data.data());
153         } else if (s.tymed == TYMED_MFPICT) {
154                 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, 0);
155                 data.resize(dataSize);
156                 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize, (LPBYTE)data.data());
157         }
158         data.detach();
159         ReleaseStgMedium(&s);
160
161         return data;
162 }
163
164
165 QVector<FORMATETC> QWindowsMimeMetafile::formatsForMime(
166         QString const & mimetype, QMimeData const * mimedata) const
167 {
168         QVector<FORMATETC> formats;
169         formats += cfFromMime(mimetype);
170         return formats;
171 }
172
173 static std::auto_ptr<QWindowsMimeMetafile> metafileWindowsMime;
174
175 #endif // Q_WS_WIN
176
177 #ifdef Q_WS_MACX
178
179 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
180 public:
181         QMacPasteboardMimeGraphics()
182                 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
183         {}
184         QString convertorName();
185         QString flavorFor(QString const & mime);
186         QString mimeFor(QString flav);
187         bool canConvert(QString const & mime, QString flav);
188         QVariant convertToMime(QString const & mime, QList<QByteArray> data, QString flav);
189         QList<QByteArray> convertFromMime(QString const & mime, QVariant data, QString flav);
190 };
191
192
193 QString QMacPasteboardMimeGraphics::convertorName()
194 {
195         return "Graphics";
196 }
197
198
199 QString QMacPasteboardMimeGraphics::flavorFor(QString const & mime)
200 {
201         LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
202         if (mime == QLatin1String(pdf_mime_type))
203                 return QLatin1String("com.adobe.pdf");
204         return QString();
205 }
206
207
208 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
209 {
210         LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
211         if (flav == QLatin1String("com.adobe.pdf"))
212                 return QLatin1String(pdf_mime_type);
213         return QString();
214 }
215
216
217 bool QMacPasteboardMimeGraphics::canConvert(QString const & mime, QString flav)
218 {
219         return mimeFor(flav) == mime;
220 }
221
222
223 QVariant QMacPasteboardMimeGraphics::convertToMime(QString const & mime, QList<QByteArray> data, QString)
224 {
225         if(data.count() > 1)
226                 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
227         return data.first();
228 }
229
230
231 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(QString const & mime, QVariant data, QString)
232 {
233         QList<QByteArray> ret;
234         ret.append(data.toByteArray());
235         return ret;
236 }
237
238 static std::auto_ptr<QMacPasteboardMimeGraphics> graphicsPasteboardMime;
239
240 #endif // Q_WS_MACX
241
242
243 GuiClipboard::GuiClipboard()
244 {
245         connect(qApp->clipboard(), SIGNAL(dataChanged()),
246                 this, SLOT(on_dataChanged()));
247         // initialize clipboard status.
248         on_dataChanged();
249         
250 #ifdef Q_WS_MACX
251         if (!graphicsPasteboardMime.get())
252                 graphicsPasteboardMime.reset(new QMacPasteboardMimeGraphics());
253 #endif // Q_WS_MACX
254
255 #ifdef Q_WS_WIN
256         if (!metafileWindowsMime.get())
257                 metafileWindowsMime.reset(new QWindowsMimeMetafile());
258 #endif // Q_WS_WIN
259 }
260
261
262 GuiClipboard::~GuiClipboard()
263 {
264 #ifdef Q_WS_MACX
265         closeAllLinkBackLinks();
266 #endif // Q_WS_MACX
267 }
268
269
270 string const GuiClipboard::getAsLyX() const
271 {
272         LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
273         // We don't convert encodings here since the encoding of the
274         // clipboard contents is specified in the data itself
275         QMimeData const * source =
276                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
277         if (!source) {
278                 LYXERR(Debug::ACTION, "' (no QMimeData)");
279                 return string();
280         }
281
282         if (source->hasFormat(lyx_mime_type)) {
283                 // data from ourself or some other LyX instance
284                 QByteArray const ar = source->data(lyx_mime_type);
285                 string const s(ar.data(), ar.count());
286                 LYXERR(Debug::ACTION, s << "'");
287                 return s;
288         }
289         LYXERR(Debug::ACTION, "'");
290         return string();
291 }
292
293
294 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
295         Clipboard::GraphicsType & type) const
296 {
297         // create file dialog filter according to the existing types in the clipboard
298         vector<Clipboard::GraphicsType> types;
299         if (hasGraphicsContents(Clipboard::EmfGraphicsType))
300                 types.push_back(Clipboard::EmfGraphicsType);
301         if (hasGraphicsContents(Clipboard::WmfGraphicsType))
302                 types.push_back(Clipboard::WmfGraphicsType);
303         if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
304                 types.push_back(Clipboard::LinkBackGraphicsType);
305         if (hasGraphicsContents(Clipboard::PdfGraphicsType))
306                 types.push_back(Clipboard::PdfGraphicsType);
307         if (hasGraphicsContents(Clipboard::PngGraphicsType))
308                 types.push_back(Clipboard::PngGraphicsType);
309         if (hasGraphicsContents(Clipboard::JpegGraphicsType))
310                 types.push_back(Clipboard::JpegGraphicsType);
311         
312         LASSERT(!types.empty(), /**/);
313         
314         // select prefered type if AnyGraphicsType was passed
315         if (type == Clipboard::AnyGraphicsType)
316                 type = types.front();
317         
318         // which extension?
319         map<Clipboard::GraphicsType, string> extensions;
320         map<Clipboard::GraphicsType, docstring> typeNames;
321         
322         extensions[Clipboard::EmfGraphicsType] = "emf";
323         extensions[Clipboard::WmfGraphicsType] = "wmf";
324         extensions[Clipboard::LinkBackGraphicsType] = "linkback";
325         extensions[Clipboard::PdfGraphicsType] = "pdf";
326         extensions[Clipboard::PngGraphicsType] = "png";
327         extensions[Clipboard::JpegGraphicsType] = "jpeg";
328         
329         typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
330         typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
331         typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
332         typeNames[Clipboard::PdfGraphicsType] = _("PDF");
333         typeNames[Clipboard::PngGraphicsType] = _("PNG");
334         typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
335         
336         // find unused filename with primary extension
337         string document_path = cur.buffer().fileName().onlyPath().absFilename();
338         unsigned newfile_number = 0;
339         FileName filename;
340         do {
341                 ++newfile_number;
342                 filename = FileName(addName(document_path,
343                         to_utf8(_("pasted"))
344                         + convert<string>(newfile_number) + "."
345                         + extensions[type]));
346         } while (filename.isReadableFile());
347         
348         while (true) {
349                 // create file type filter, putting the prefered on to the front
350                 docstring filterSpec;
351                 for (size_t i = 0; i != types.size(); ++i) {
352                         docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
353                                 + " (*." + from_ascii(extensions[types[i]]) + ")";
354                         if (types[i] == type)
355                                 filterSpec = s + filterSpec;
356                         else
357                                 filterSpec += ";;" + s;
358                 }
359                 FileFilterList const filter(filterSpec);
360                 
361                 // show save dialog for the graphic
362                 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
363                 FileDialog::Result result =
364                 dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
365                          toqstr(filename.onlyFileName()));
366                 
367                 if (result.first == FileDialog::Later)
368                         return FileName();
369                 
370                 string newFilename = fromqstr(result.second);
371                 if (newFilename.empty()) {
372                         cur.bv().message(_("Canceled."));
373                         return FileName();
374                 }
375                 filename.set(newFilename);
376                 
377                 // check the extension (the user could have changed it)
378                 if (!suffixIs(ascii_lowercase(filename.absFilename()),
379                               "." + extensions[type])) {
380                         // the user changed the extension. Check if the type is available
381                         size_t i;
382                         for (i = 1; i != types.size(); ++i) {
383                                 if (suffixIs(ascii_lowercase(filename.absFilename()),
384                                              "." + extensions[types[i]])) {
385                                         type = types[i];
386                                         break;
387                                 }
388                         }
389                         
390                         // invalid extension found, or none at all. In the latter
391                         // case set the default extensions.
392                         if (i == types.size()
393                             && filename.onlyFileName().find('.') == string::npos) {
394                                 filename.changeExtension("." + extensions[type]);
395                         }
396                 }
397                 
398                 // check whether the file exists and warn the user
399                 if (!filename.exists())
400                         break;
401                 int ret = frontend::Alert::prompt(
402                         _("Overwrite external file?"),
403                         bformat(_("File %1$s already exists, do you want to overwrite it?"),
404                         from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
405                 if (ret == 0)
406                         // overwrite, hence break the dialog loop
407                         break;
408                 
409                 // not overwrite, hence show the dialog again (i.e. loop)
410         }
411         
412         return filename;
413 }
414
415
416 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
417 {
418         // get the filename from the user
419         FileName filename = getPastedGraphicsFileName(cur, type);
420         if (filename.empty())
421                 return FileName();
422
423         // handle image cases first
424         if (type == PngGraphicsType || type == JpegGraphicsType) {
425                 // get image from QImage from clipboard
426                 QImage image = qApp->clipboard()->image();
427                 if (image.isNull()) {
428                         LYXERR(Debug::ACTION, "No image in clipboard");
429                         return FileName();
430                 }
431
432                 // convert into graphics format
433                 QByteArray ar;
434                 QBuffer buffer(&ar);
435                 buffer.open(QIODevice::WriteOnly);
436                 if (type == PngGraphicsType)
437                         image.save(toqstr(filename.absFilename()), "PNG");
438                 else if (type == JpegGraphicsType)
439                         image.save(toqstr(filename.absFilename()), "JPEG");
440                 else
441                         LASSERT(false, /**/);
442                 
443                 return filename;
444         }
445         
446         // get mime data
447         QMimeData const * source =
448         qApp->clipboard()->mimeData(QClipboard::Clipboard);
449         if (!source) {
450                 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
451                 return FileName();
452         }
453         
454         // get mime for type
455         QString mime;
456         switch (type) {
457         case PdfGraphicsType: mime = pdf_mime_type; break;
458         case LinkBackGraphicsType: mime = pdf_mime_type; break;
459         case EmfGraphicsType: mime = emf_mime_type; break;
460         case WmfGraphicsType: mime = wmf_mime_type; break;
461         default: LASSERT(false, /**/);
462         }
463         
464         // get data
465         if (!source->hasFormat(mime))
466                 return FileName();
467         // data from ourself or some other LyX instance
468         QByteArray const ar = source->data(mime);
469         LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
470                << "length = " << ar.count());
471         
472         QFile f(toqstr(filename.absFilename()));
473         if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
474                 LYXERR(Debug::ACTION, "Error opening file "
475                        << filename.absFilename() << " for writing");
476                 return FileName();
477         }
478         
479         // write the (LinkBack) PDF data
480         f.write(ar);
481         if (type == LinkBackGraphicsType) {
482 #ifdef Q_WS_MACX
483                 void const * linkBackData;
484                 unsigned linkBackLen;
485                 getLinkBackData(&linkBackData, &linkBackLen);
486                 f.write((char *)linkBackData, linkBackLen);
487                 quint32 pdfLen = ar.size();
488                 QDataStream ds(&f);
489                 ds << pdfLen; // big endian by default
490 #else
491                 // only non-Mac this should never happen
492                 LASSERT(false, /**/);
493 #endif // Q_WS_MACX
494         }
495
496         f.close();
497         return filename;
498 }
499
500
501 docstring const GuiClipboard::getAsText() const
502 {
503         // text data from other applications
504         QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
505                                 .normalized(QString::NormalizationForm_C);
506         LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
507         if (str.isNull())
508                 return docstring();
509
510         return internalLineEnding(qstring_to_ucs4(str));
511 }
512
513
514 void GuiClipboard::put(string const & lyx, docstring const & text)
515 {
516         LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
517                               << to_utf8(text) << "')");
518         // We don't convert the encoding of lyx since the encoding of the
519         // clipboard contents is specified in the data itself
520         QMimeData * data = new QMimeData;
521         if (!lyx.empty()) {
522                 QByteArray const qlyx(lyx.c_str(), lyx.size());
523                 data->setData(lyx_mime_type, qlyx);
524         }
525         // Don't test for text.empty() since we want to be able to clear the
526         // clipboard.
527         QString const qtext = toqstr(text);
528         data->setText(qtext);
529         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
530 }
531
532
533 bool GuiClipboard::hasLyXContents() const
534 {
535         QMimeData const * const source =
536                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
537         return source && source->hasFormat(lyx_mime_type);
538 }
539
540
541 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
542 {
543         if (type == AnyGraphicsType) {
544                 return hasGraphicsContents(PdfGraphicsType)
545                         || hasGraphicsContents(PngGraphicsType)
546                         || hasGraphicsContents(JpegGraphicsType)
547                         || hasGraphicsContents(EmfGraphicsType)
548                         || hasGraphicsContents(WmfGraphicsType)
549                         || hasGraphicsContents(LinkBackGraphicsType);
550         }
551
552         QMimeData const * const source =
553         qApp->clipboard()->mimeData(QClipboard::Clipboard);
554
555         // handle image cases first
556         if (type == PngGraphicsType || type == JpegGraphicsType)
557                 return source->hasImage();
558
559         // handle LinkBack for Mac
560 #ifdef Q_WS_MACX
561         if (type == LinkBackGraphicsType)
562                 return isLinkBackDataInPasteboard();
563 #else
564         if (type == LinkBackGraphicsType)
565                 return false;
566 #endif // Q_WS_MACX
567         
568         // get mime data
569         QStringList const & formats = source->formats();
570         LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
571         for (int i = 0; i < formats.size(); ++i) {
572                 LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
573         }
574
575         // compute mime for type
576         QString mime;
577         switch (type) {
578         case EmfGraphicsType: mime = emf_mime_type; break;
579         case WmfGraphicsType: mime = wmf_mime_type; break;
580         case PdfGraphicsType: mime = pdf_mime_type; break;
581         default: LASSERT(false, /**/);
582         }
583         
584         return source && source->hasFormat(mime);
585 }
586
587
588 bool GuiClipboard::isInternal() const
589 {
590         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
591         // the preamble dialog
592         // FIXME: This does only work on X11, since ownsClipboard() is
593         // hardwired to return false on Windows and OS X.
594         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
595 }
596
597
598 bool GuiClipboard::hasInternal() const
599 {
600         // Windows and Mac OS X does not have the concept of ownership;
601         // the clipboard is a fully global resource so all applications 
602         // are notified of changes.
603 #if (defined(Q_WS_X11))
604         return true;
605 #else
606         return false;
607 #endif
608 }
609
610
611 void GuiClipboard::on_dataChanged()
612 {
613         QMimeData const * const source =
614         qApp->clipboard()->mimeData(QClipboard::Clipboard);
615         QStringList l = source->formats();
616         LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
617         for (int i = 0; i < l.count(); i++) {
618                 LYXERR(Debug::ACTION, fromqstr(l.value(i)));
619         }
620         
621         text_clipboard_empty_ = qApp->clipboard()->
622                 text(QClipboard::Clipboard).isEmpty();
623
624         has_lyx_contents_ = hasLyXContents();
625         has_graphics_contents_ = hasGraphicsContents();
626 }
627
628
629 bool GuiClipboard::empty() const
630 {
631         // We need to check both the plaintext and the LyX version of the
632         // clipboard. The plaintext version is empty if the LyX version
633         // contains only one inset, and the LyX version is empty if the
634         // clipboard does not come from LyX.
635         if (!text_clipboard_empty_)
636                 return false;
637         return !has_lyx_contents_ && !has_graphics_contents_;
638 }
639
640 } // namespace frontend
641 } // namespace lyx
642
643 #include "GuiClipboard_moc.cpp"