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.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "FileDialog.h"
17 #include "GuiClipboard.h"
18 #include "qt_helpers.h"
21 #include "BufferView.h"
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"
32 #include "support/linkback/LinkBackProxy.h"
35 #include "frontends/alert.h"
37 #include <QApplication>
40 #include <QDataStream>
45 #include <QStringList>
51 using namespace lyx::support;
58 char const * lyx_mime_type = "application/x-lyx";
59 char const * pdf_mime_type = "application/pdf";
60 char const * emf_mime_type = "image/x-emf";
61 char const * wmf_mime_type = "image/x-wmf";
64 GuiClipboard::GuiClipboard()
66 connect(qApp->clipboard(), SIGNAL(dataChanged()),
67 this, SLOT(on_dataChanged()));
68 // initialize clipboard status.
73 string const GuiClipboard::getAsLyX() const
75 LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
76 // We don't convert encodings here since the encoding of the
77 // clipboard contents is specified in the data itself
78 QMimeData const * source =
79 qApp->clipboard()->mimeData(QClipboard::Clipboard);
81 LYXERR(Debug::ACTION, "' (no QMimeData)");
85 if (source->hasFormat(lyx_mime_type)) {
86 // data from ourself or some other LyX instance
87 QByteArray const ar = source->data(lyx_mime_type);
88 string const s(ar.data(), ar.count());
89 LYXERR(Debug::ACTION, s << "'");
92 LYXERR(Debug::ACTION, "'");
97 FileName GuiClipboard::getPastedGraphicsFileName(Cursor const & cur,
98 Clipboard::GraphicsType & type) const
100 // create file dialog filter according to the existing types in the clipboard
101 vector<Clipboard::GraphicsType> types;
102 if (hasGraphicsContents(Clipboard::EmfGraphicsType))
103 types.push_back(Clipboard::EmfGraphicsType);
104 if (hasGraphicsContents(Clipboard::WmfGraphicsType))
105 types.push_back(Clipboard::WmfGraphicsType);
106 if (hasGraphicsContents(Clipboard::LinkBackGraphicsType))
107 types.push_back(Clipboard::LinkBackGraphicsType);
108 if (hasGraphicsContents(Clipboard::PdfGraphicsType))
109 types.push_back(Clipboard::PdfGraphicsType);
110 if (hasGraphicsContents(Clipboard::PngGraphicsType))
111 types.push_back(Clipboard::PngGraphicsType);
112 if (hasGraphicsContents(Clipboard::JpegGraphicsType))
113 types.push_back(Clipboard::JpegGraphicsType);
115 LASSERT(!types.empty(), /**/);
117 // select prefered type if AnyGraphicsType was passed
118 if (type == Clipboard::AnyGraphicsType)
119 type = types.front();
122 map<Clipboard::GraphicsType, string> extensions;
123 map<Clipboard::GraphicsType, docstring> typeNames;
125 extensions[Clipboard::EmfGraphicsType] = "emf";
126 extensions[Clipboard::WmfGraphicsType] = "wmf";
127 extensions[Clipboard::LinkBackGraphicsType] = "linkback";
128 extensions[Clipboard::PdfGraphicsType] = "pdf";
129 extensions[Clipboard::PngGraphicsType] = "png";
130 extensions[Clipboard::JpegGraphicsType] = "jpeg";
132 typeNames[Clipboard::EmfGraphicsType] = _("Enhanced Metafile");
133 typeNames[Clipboard::WmfGraphicsType] = _("Windows Metafile");
134 typeNames[Clipboard::LinkBackGraphicsType] = _("LinkBack PDF");
135 typeNames[Clipboard::PdfGraphicsType] = _("PDF");
136 typeNames[Clipboard::PngGraphicsType] = _("PNG");
137 typeNames[Clipboard::JpegGraphicsType] = _("JPEG");
139 // find unused filename with primary extension
140 string document_path = cur.buffer().fileName().onlyPath().absFilename();
141 unsigned newfile_number = 0;
145 filename = FileName(addName(document_path,
147 + convert<string>(newfile_number) + "."
148 + extensions[type]));
149 } while (filename.isReadableFile());
152 // create file type filter, putting the prefered on to the front
154 for (size_t i = 0; i != types.size(); ++i) {
155 docstring s = bformat(_("%1$s Files"), typeNames[types[i]])
156 + " (*." + from_ascii(extensions[types[i]]) + ")";
157 if (types[i] == type)
158 filter.prepend(toqstr(s));
160 filter.append(toqstr(s));
162 filter = fileFilters(filter.join(";;"));
164 // show save dialog for the graphic
165 FileDialog dlg(qt_("Choose a filename to save the pasted graphic as"));
166 FileDialog::Result result =
167 dlg.save(toqstr(filename.onlyPath().absFilename()), filter,
168 toqstr(filename.onlyFileName()));
170 if (result.first == FileDialog::Later)
173 string newFilename = fromqstr(result.second);
174 if (newFilename.empty()) {
175 cur.bv().message(_("Canceled."));
178 filename.set(newFilename);
180 // check the extension (the user could have changed it)
181 if (!suffixIs(ascii_lowercase(filename.absFilename()),
182 "." + extensions[type])) {
183 // the user changed the extension. Check if the type is available
185 for (i = 1; i != types.size(); ++i) {
186 if (suffixIs(ascii_lowercase(filename.absFilename()),
187 "." + extensions[types[i]])) {
193 // invalid extension found, or none at all. In the latter
194 // case set the default extensions.
195 if (i == types.size()
196 && filename.onlyFileName().find('.') == string::npos) {
197 filename.changeExtension("." + extensions[type]);
201 // check whether the file exists and warn the user
202 if (!filename.exists())
204 int ret = frontend::Alert::prompt(
205 _("Overwrite external file?"),
206 bformat(_("File %1$s already exists, do you want to overwrite it?"),
207 from_utf8(filename.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
209 // overwrite, hence break the dialog loop
212 // not overwrite, hence show the dialog again (i.e. loop)
219 FileName GuiClipboard::getAsGraphics(Cursor const & cur, GraphicsType type) const
221 // get the filename from the user
222 FileName filename = getPastedGraphicsFileName(cur, type);
223 if (filename.empty())
226 // handle image cases first
227 if (type == PngGraphicsType || type == JpegGraphicsType) {
228 // get image from QImage from clipboard
229 QImage image = qApp->clipboard()->image();
230 if (image.isNull()) {
231 LYXERR(Debug::ACTION, "No image in clipboard");
235 // convert into graphics format
238 buffer.open(QIODevice::WriteOnly);
239 if (type == PngGraphicsType)
240 image.save(toqstr(filename.absFilename()), "PNG");
241 else if (type == JpegGraphicsType)
242 image.save(toqstr(filename.absFilename()), "JPEG");
244 LASSERT(false, /**/);
250 QMimeData const * source =
251 qApp->clipboard()->mimeData(QClipboard::Clipboard);
253 LYXERR(Debug::ACTION, "0 bytes (no QMimeData)");
260 case PdfGraphicsType: mime = pdf_mime_type; break;
261 case LinkBackGraphicsType: mime = pdf_mime_type; break;
262 case EmfGraphicsType: mime = emf_mime_type; break;
263 case WmfGraphicsType: mime = wmf_mime_type; break;
264 default: LASSERT(false, /**/);
268 if (!source->hasFormat(mime))
270 // data from ourself or some other LyX instance
271 QByteArray const ar = source->data(mime);
272 LYXERR(Debug::ACTION, "Getting from clipboard: mime = " << mime.data()
273 << "length = " << ar.count());
275 QFile f(toqstr(filename.absFilename()));
276 if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
277 LYXERR(Debug::ACTION, "Error opening file "
278 << filename.absFilename() << " for writing");
282 // write the (LinkBack) PDF data
284 if (type == LinkBackGraphicsType) {
286 void const * linkBackData;
287 unsigned linkBackLen;
288 getLinkBackData(&linkBackData, &linkBackLen);
289 f.write((char *)linkBackData, linkBackLen);
290 quint32 pdfLen = ar.size();
292 ds << pdfLen; // big endian by default
294 // only non-Mac this should never happen
295 LASSERT(false, /**/);
304 docstring const GuiClipboard::getAsText() const
306 // text data from other applications
307 QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
308 .normalized(QString::NormalizationForm_C);
309 LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << str << "'");
313 return internalLineEnding(qstring_to_ucs4(str));
317 void GuiClipboard::put(string const & lyx, docstring const & text)
319 LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
320 << to_utf8(text) << "')");
321 // We don't convert the encoding of lyx since the encoding of the
322 // clipboard contents is specified in the data itself
323 QMimeData * data = new QMimeData;
325 QByteArray const qlyx(lyx.c_str(), lyx.size());
326 data->setData(lyx_mime_type, qlyx);
328 // Don't test for text.empty() since we want to be able to clear the
330 QString const qtext = toqstr(text);
331 data->setText(qtext);
332 qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
336 bool GuiClipboard::hasLyXContents() const
338 QMimeData const * const source =
339 qApp->clipboard()->mimeData(QClipboard::Clipboard);
340 return source && source->hasFormat(lyx_mime_type);
344 bool GuiClipboard::hasGraphicsContents(Clipboard::GraphicsType type) const
346 if (type == AnyGraphicsType) {
347 return hasGraphicsContents(PdfGraphicsType)
348 || hasGraphicsContents(PngGraphicsType)
349 || hasGraphicsContents(JpegGraphicsType)
350 || hasGraphicsContents(EmfGraphicsType)
351 || hasGraphicsContents(WmfGraphicsType)
352 || hasGraphicsContents(LinkBackGraphicsType);
355 QMimeData const * const source =
356 qApp->clipboard()->mimeData(QClipboard::Clipboard);
358 // handle image cases first
359 if (type == PngGraphicsType || type == JpegGraphicsType)
360 return source->hasImage();
362 // handle LinkBack for Mac
363 if (type == LinkBackGraphicsType)
365 return isLinkBackDataInPasteboard();
371 QStringList const & formats = source->formats();
372 LYXERR(Debug::ACTION, "We found " << formats.size() << " formats");
373 for (int i = 0; i < formats.size(); ++i)
374 LYXERR(Debug::ACTION, "Found format " << formats[i]);
376 // compute mime for type
379 case EmfGraphicsType: mime = emf_mime_type; break;
380 case WmfGraphicsType: mime = wmf_mime_type; break;
381 case PdfGraphicsType: mime = pdf_mime_type; break;
382 default: LASSERT(false, /**/);
385 return source && source->hasFormat(mime);
389 bool GuiClipboard::isInternal() const
391 // ownsClipboard() is also true for stuff coming from dialogs, e.g.
392 // the preamble dialog
393 // FIXME: This does only work on X11, since ownsClipboard() is
394 // hardwired to return false on Windows and OS X.
395 return qApp->clipboard()->ownsClipboard() && hasLyXContents();
399 bool GuiClipboard::hasInternal() const
401 // Windows and Mac OS X does not have the concept of ownership;
402 // the clipboard is a fully global resource so all applications
403 // are notified of changes.
404 #if (defined(Q_WS_X11))
412 void GuiClipboard::on_dataChanged()
414 QMimeData const * const source =
415 qApp->clipboard()->mimeData(QClipboard::Clipboard);
416 QStringList l = source->formats();
417 LYXERR(Debug::ACTION, "Qt Clipboard changed. We found the following mime types:");
418 for (int i = 0; i < l.count(); i++)
419 LYXERR(Debug::ACTION, l.value(i));
421 text_clipboard_empty_ = qApp->clipboard()->
422 text(QClipboard::Clipboard).isEmpty();
424 has_lyx_contents_ = hasLyXContents();
425 has_graphics_contents_ = hasGraphicsContents();
429 bool GuiClipboard::empty() const
431 // We need to check both the plaintext and the LyX version of the
432 // clipboard. The plaintext version is empty if the LyX version
433 // contains only one inset, and the LyX version is empty if the
434 // clipboard does not come from LyX.
435 if (!text_clipboard_empty_)
437 return !has_lyx_contents_ && !has_graphics_contents_;
440 } // namespace frontend
443 #include "GuiClipboard_moc.cpp"