]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.cpp
header cleanup
[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 "frontends/alert.h"
18
19 #include "Buffer.h"
20 #include "BufferView.h"
21 #include "Cursor.h"
22 #include "GuiClipboard.h"
23 #include "qt_helpers.h"
24
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
32 #include <QApplication>
33 #include <QBuffer>
34 #include <QClipboard>
35 #include <QDataStream>
36 #include <QFile>
37 #include <QImage>
38 #include <QMacPasteboardMime>
39 #include <QMimeData>
40 #include <QString>
41 #include <QStringList>
42
43 #include "boost/assert.hpp"
44
45 #include <map>
46
47 #ifdef Q_WS_MACX
48 #include "support/linkback/LinkBackProxy.h"
49 #endif // Q_WS_MACX
50
51 using namespace std;
52 using namespace lyx::support;
53
54 static char const * const lyx_mime_type = "application/x-lyx";
55 static char const * const pdf_mime_type = "application/pdf";
56
57 namespace lyx {
58
59 namespace frontend {
60
61 #ifdef Q_WS_MACX
62
63 class QMacPasteboardMimeGraphics : public QMacPasteboardMime {
64 public:
65         QMacPasteboardMimeGraphics()
66         : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL) {}
67         ~QMacPasteboardMimeGraphics() {}
68         QString convertorName();
69         QString flavorFor(const QString &mime);
70         QString mimeFor(QString flav);
71         bool canConvert(const QString &mime, QString flav);
72         QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav);
73         QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav);
74 };
75
76
77 QString QMacPasteboardMimeGraphics::convertorName()
78 {
79         return "Graphics";
80 }
81
82
83 QString QMacPasteboardMimeGraphics::flavorFor(const QString &mime)
84 {
85         LYXERR(Debug::ACTION, "flavorFor " << fromqstr(mime));
86         if (mime == QLatin1String(pdf_mime_type))
87                 return QLatin1String("com.adobe.pdf");
88         return QString();
89 }
90
91
92 QString QMacPasteboardMimeGraphics::mimeFor(QString flav)
93 {
94         LYXERR(Debug::ACTION, "mimeFor " << fromqstr(flav));
95         if (flav == QLatin1String("com.adobe.pdf"))
96                 return QLatin1String(pdf_mime_type);
97         return QString();
98 }
99
100
101 bool QMacPasteboardMimeGraphics::canConvert(const QString &mime, QString flav)
102 {
103         return mimeFor(flav) == mime;
104 }
105
106
107 QVariant QMacPasteboardMimeGraphics::convertToMime(const QString &mime, QList<QByteArray> data, QString)
108 {
109         if(data.count() > 1)
110                 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
111         return data.first();
112 }
113
114
115 QList<QByteArray> QMacPasteboardMimeGraphics::convertFromMime(const QString &mime, QVariant data, QString)
116 {
117         QList<QByteArray> ret;
118         ret.append(data.toByteArray());
119         return ret;
120 }
121
122 static QMacPasteboardMimeGraphics * graphicsPasteboardMime;
123
124 #endif // Q_WS_MACX
125
126
127 GuiClipboard::GuiClipboard()
128 {
129         connect(qApp->clipboard(), SIGNAL(dataChanged()),
130                 this, SLOT(on_dataChanged()));
131         // initialize clipboard status.
132         on_dataChanged();
133         
134 #ifdef Q_WS_MACX
135         if (!graphicsPasteboardMime)
136                 graphicsPasteboardMime = new QMacPasteboardMimeGraphics();
137 #endif // Q_WS_MACX
138 }
139
140
141 GuiClipboard::~GuiClipboard()
142 {
143 #ifdef Q_WS_MACX
144         closeAllLinkBackLinks();
145 #endif // Q_WS_MACX
146 }
147
148
149 string const GuiClipboard::getAsLyX() const
150 {
151         LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
152         // We don't convert encodings here since the encoding of the
153         // clipboard contents is specified in the data itself
154         QMimeData const * source =
155                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
156         if (!source) {
157                 LYXERR(Debug::ACTION, "' (no QMimeData)");
158                 return string();
159         }
160
161         if (source->hasFormat(lyx_mime_type)) {
162                 // data from ourself or some other LyX instance
163                 QByteArray const ar = source->data(lyx_mime_type);
164                 string const s(ar.data(), ar.count());
165                 LYXERR(Debug::ACTION, s << "'");
166                 return s;
167         }
168         LYXERR(Debug::ACTION, "'");
169         return string();
170 }
171
172
173 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
174         Clipboard::GraphicsType & type) const
175 {
176         // create file dialog filter according to the existing types in the clipboard
177         vector<Clipboard::GraphicsType> types;
178         if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
179                 types.push_back(Clipboard::LinkBackGraphicsType);
180         if (hasGraphicsContents(Clipboard::PdfGraphicsType))
181                 types.push_back(Clipboard::PdfGraphicsType);
182         if (hasGraphicsContents(Clipboard::PngGraphicsType))
183                 types.push_back(Clipboard::PngGraphicsType);
184         if (hasGraphicsContents(Clipboard::JpegGraphicsType))
185                 types.push_back(Clipboard::JpegGraphicsType);
186         
187         BOOST_ASSERT(!types.empty());
188         
189         // select prefered type if AnyGraphicsType was passed
190         if (type == Clipboard::AnyGraphicsType)
191                 type = types.front();
192         
193         // which extension?
194         map<Clipboard::GraphicsType, string> extensions;
195         map<Clipboard::GraphicsType, docstring> typeNames;
196         
197         extensions[Clipboard::LinkBackGraphicsType] = "linkback";
198         extensions[Clipboard::PdfGraphicsType] = "pdf";
199         extensions[Clipboard::PngGraphicsType] = "png";
200         extensions[Clipboard::JpegGraphicsType] = "jpeg";
201         
202         typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
203         typeNames[Clipboard::PdfGraphicsType] = _("PDF");
204         typeNames[Clipboard::PngGraphicsType] = _("PNG");
205         typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
206         
207         // find unused filename with primary extension
208         string document_path = cur.buffer().fileName().onlyPath().absFilename();
209         unsigned newfile_number = 0;
210         FileName filename;
211         do {
212                 ++newfile_number;
213                 filename
214                 = FileName(addName(document_path,
215                         to_utf8(_("pasted"))
216                         + convert<string>(newfile_number) + "."
217                         + extensions[type]));
218         } while (filename.isReadableFile());
219         
220         while (true) {
221                 // create file type filter, putting the prefered on to the front
222                 docstring filterSpec;
223                 for (unsigned i = 0; i < types.size(); ++i) {
224                         docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
225                         + " (*." + from_ascii(extensions[types[i]]) + ")";
226                         if (types[i] == type)
227                                 filterSpec = s + filterSpec;
228                         else
229                                 filterSpec += ";;" + s;
230                 }
231                 FileFilterList const filter(filterSpec);
232                 
233                 // show save dialog for the graphic
234                 FileDialog dlg(_("Choose a filename to save the pasted graphic as"));
235                 FileDialog::Result result =
236                 dlg.save(from_utf8(filename.onlyPath().absFilename()), filter,
237                          from_utf8(filename.onlyFileName()));
238                 
239                 if (result.first == FileDialog::Later)
240                         return FileName();
241                 
242                 string newFilename = to_utf8(result.second);
243                 if (newFilename.empty()) {
244                         cur.bv().message(_("Canceled."));
245                         return FileName();
246                 }
247                 filename.set(newFilename);
248                 
249                 // check the extension (the user could have changed it)
250                 if (!suffixIs(ascii_lowercase(filename.absFilename()),
251                               "." + extensions[type])) {
252                         // the user changed the extension. Check if the type is available
253                         unsigned i;
254                         for (i = 1; i < types.size(); ++i) {
255                                 if (suffixIs(ascii_lowercase(filename.absFilename()),
256                                              "." + extensions[types[i]])) {
257                                         type = types[i];
258                                         break;
259                                 }
260                         }
261                         
262                         // invalid extension found, or none at all. In the latter
263                         // case set the default extensions.
264                         if (i == types.size()
265                             && filename.onlyFileName().find('.') == string::npos) {
266                                 filename.changeExtension("." + extensions[type]);
267                         }
268                 }
269                 
270                 // check whether the file exists and warn the user
271                 if (!filename.exists())
272                         break;
273                 int ret = frontend::Alert::prompt(
274                         _("Overwrite external file?"),
275                         bformat(_("File %1$s already exists, do you want to overwrite it"),
276                         from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
277                 if (ret == 0)
278                         // overwrite, hence break the dialog loop
279                         break;
280                 
281                 // not overwrite, hence show the dialog again (i.e. loop)
282         }
283         
284         return filename;
285 }
286
287
288 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
289 {
290         // get the filename from the user
291         FileName filename = getPastedGraphicsFileName(cur, type);
292         if (filename.empty())
293                 return FileName();
294
295         // handle image cases first
296         if (type == PngGraphicsType || type == JpegGraphicsType) {
297                 // get image from QImage from clipboard
298                 QImage image = qApp->clipboard()->image();
299                 if (image.isNull()) {
300                         LYXERR(Debug::ACTION, "No image in clipboard");
301                         return FileName();
302                 }
303
304                 // convert into graphics format
305                 QByteArray ar;
306                 QBuffer buffer(&ar);
307                 buffer.open(QIODevice::WriteOnly);
308                 if (type == PngGraphicsType)
309                         image.save(toqstr(filename.absFilename()), "PNG");
310                 else if (type == JpegGraphicsType)
311                         image.save(toqstr(filename.absFilename()), "JPEG");
312                 else
313                         BOOST_ASSERT(false);
314                 
315                 return filename;
316         }
317         
318         // get mime data
319         QMimeData const * source =
320         qApp->clipboard()->mimeData(QClipboard::Clipboard);
321         if (!source) {
322                 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
323                 return FileName();
324         }
325         
326         // get mime for type
327         QString mime;
328         switch (type) {
329         case PdfGraphicsType: mime = pdf_mime_type; break;
330         case LinkBackGraphicsType: mime = pdf_mime_type; break;
331         default: BOOST_ASSERT(false);
332         }
333         
334         // get data
335         if (!source->hasFormat(mime))
336                 return FileName();
337         // data from ourself or some other LyX instance
338         QByteArray const ar = source->data(mime);
339         LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
340                << "length = " << ar.count());
341         
342         QFile f(toqstr(filename.absFilename()));
343         if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
344                 LYXERR(Debug::ACTION, "Error opening file "
345                        << filename.absFilename() << " for writing");
346                 return FileName();
347         }
348         
349         // write the (LinkBack) PDF data
350         f.write(ar);
351         if (type == LinkBackGraphicsType) {
352 #ifdef Q_WS_MACX
353                 void const * linkBackData;
354                 unsigned linkBackLen;
355                 getLinkBackData(&linkBackData, &linkBackLen);
356                 f.write((char *)linkBackData, linkBackLen);
357                 quint32 pdfLen = ar.size();
358                 QDataStream ds(&f);
359                 ds << pdfLen; // big endian by default
360 #else
361                 // only non-Mac this should never happen
362                 BOOST_ASSERT(false);
363 #endif // Q_WS_MACX
364         }
365
366         f.close();
367         return filename;
368 }
369
370
371 docstring const GuiClipboard::getAsText() const
372 {
373         // text data from other applications
374         QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
375                                 .normalized(QString::NormalizationForm_C);
376         LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
377         if (str.isNull())
378                 return docstring();
379
380         return internalLineEnding(qstring_to_ucs4(str));
381 }
382
383
384 void GuiClipboard::put(string const & lyx, docstring const & text)
385 {
386         LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
387                               << to_utf8(text) << "')");
388         // We don't convert the encoding of lyx since the encoding of the
389         // clipboard contents is specified in the data itself
390         QMimeData * data = new QMimeData;
391         if (!lyx.empty()) {
392                 QByteArray const qlyx(lyx.c_str(), lyx.size());
393                 data->setData(lyx_mime_type, qlyx);
394         }
395         // Don't test for text.empty() since we want to be able to clear the
396         // clipboard.
397         QString const qtext = toqstr(text);
398         data->setText(qtext);
399         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
400 }
401
402
403 bool GuiClipboard::hasLyXContents() const
404 {
405         QMimeData const * const source =
406                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
407         return source && source->hasFormat(lyx_mime_type);
408 }
409
410
411 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
412 {
413         if (type == AnyGraphicsType) {
414                 return hasGraphicsContents(PdfGraphicsType)
415                         || hasGraphicsContents(PngGraphicsType)
416                         || hasGraphicsContents(JpegGraphicsType)
417                         || hasGraphicsContents(LinkBackGraphicsType);
418         }
419
420         QMimeData const * const source =
421         qApp->clipboard()->mimeData(QClipboard::Clipboard);
422         
423         // handle image cases first
424         if (type == PngGraphicsType || type == JpegGraphicsType)
425                 return source->hasImage();
426
427         // handle LinkBack for Mac
428 #ifdef Q_WS_MACX
429         if (type == LinkBackGraphicsType)
430                 return isLinkBackDataInPasteboard();
431 #else
432         if (type == LinkBackGraphicsType)
433                 return false;
434 #endif // Q_WS_MACX
435         
436         // get mime data
437         QStringList const & formats = source->formats();
438         LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
439         for (int i = 0; i < formats.size(); ++i) {
440                 LYXERR(Debug::ACTION, "Found format " << fromqstr(formats[i]));
441         }
442
443         // compute mime for type
444         QString mime;
445         switch (type) {
446         case PdfGraphicsType: mime = pdf_mime_type; break;
447         default: BOOST_ASSERT(false);
448         }
449         
450         return source && source->hasFormat(mime);
451 }
452
453
454 bool GuiClipboard::isInternal() const
455 {
456         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
457         // the preamble dialog
458         // FIXME: This does only work on X11, since ownsClipboard() is
459         // hardwired to return false on Windows and OS X.
460         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
461 }
462
463
464 bool GuiClipboard::hasInternal() const
465 {
466         // Windows and Mac OS X does not have the concept of ownership;
467         // the clipboard is a fully global resource so all applications 
468         // are notified of changes.
469 #if (defined(Q_WS_X11))
470         return true;
471 #else
472         return false;
473 #endif
474 }
475
476
477 void GuiClipboard::on_dataChanged()
478 {
479         QMimeData const * const source =
480         qApp->clipboard()->mimeData(QClipboard::Clipboard);
481         QStringList l = source->formats();
482         LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
483         for (int i = 0; i < l.count(); i++) {
484                 LYXERR(Debug::ACTION, fromqstr(l.value(i)));
485         }
486         
487         text_clipboard_empty_ = qApp->clipboard()->
488                 text(QClipboard::Clipboard).isEmpty();
489
490         has_lyx_contents_ = hasLyXContents();
491         has_graphics_contents_ = hasGraphicsContents();
492 }
493
494
495 bool GuiClipboard::empty() const
496 {
497         // We need to check both the plaintext and the LyX version of the
498         // clipboard. The plaintext version is empty if the LyX version
499         // contains only one inset, and the LyX version is empty if the
500         // clipboard does not come from LyX.
501         if (!text_clipboard_empty_)
502                 return false;
503         return !has_lyx_contents_ && !has_graphics_contents_;
504 }
505
506 } // namespace frontend
507 } // namespace lyx
508
509 #include "GuiClipboard_moc.cpp"