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