]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiClipboard.cpp
fix memory leaks
[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 "GuiClipboard.h"
16 #include "qt_helpers.h"
17
18 #include "support/debug.h"
19
20 #include <QApplication>
21 #include <QClipboard>
22 #include <QMimeData>
23 #include <QString>
24
25 #include "support/lstrings.h"
26
27 using lyx::support::internalLineEnding;
28 using lyx::support::externalLineEnding;
29
30 using std::string;
31
32 static char const * const mime_type = "application/x-lyx";
33
34
35 namespace lyx {
36 namespace frontend {
37
38 GuiClipboard::GuiClipboard()
39 {
40         connect(qApp->clipboard(), SIGNAL(dataChanged()),
41                 this, SLOT(on_dataChanged()));
42         // initialize clipboard status.
43         on_dataChanged();
44 }
45
46
47 string const GuiClipboard::getAsLyX() const
48 {
49         LYXERR(Debug::ACTION, "GuiClipboard::getAsLyX(): `");
50         // We don't convert encodings here since the encoding of the
51         // clipboard contents is specified in the data itself
52         QMimeData const * source =
53                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
54         if (!source) {
55                 LYXERR(Debug::ACTION, "' (no QMimeData)");
56                 return string();
57         }
58         if (source->hasFormat(mime_type)) {
59                 // data from ourself or some other LyX instance
60                 QByteArray const ar = source->data(mime_type);
61                 string const s(ar.data(), ar.count());
62                 LYXERR(Debug::ACTION, s << "'");
63                 return s;
64         }
65         LYXERR(Debug::ACTION, "'");
66         return string();
67 }
68
69
70 docstring const GuiClipboard::getAsText() const
71 {
72         // text data from other applications
73         QString const str = qApp->clipboard()->text(QClipboard::Clipboard)
74                                 .normalized(QString::NormalizationForm_C);
75         LYXERR(Debug::ACTION, "GuiClipboard::getAsText(): `" << fromqstr(str) << "'");
76         if (str.isNull())
77                 return docstring();
78
79         return internalLineEnding(qstring_to_ucs4(str));
80 }
81
82
83 void GuiClipboard::put(string const & lyx, docstring const & text)
84 {
85         LYXERR(Debug::ACTION, "GuiClipboard::put(`" << lyx << "' `"
86                               << to_utf8(text) << "')");
87         // We don't convert the encoding of lyx since the encoding of the
88         // clipboard contents is specified in the data itself
89         QMimeData * data = new QMimeData;
90         if (!lyx.empty()) {
91                 QByteArray const qlyx(lyx.c_str(), lyx.size());
92                 data->setData(mime_type, qlyx);
93         }
94         // Don't test for text.empty() since we want to be able to clear the
95         // clipboard.
96         QString const qtext = toqstr(text);
97         data->setText(qtext);
98         qApp->clipboard()->setMimeData(data, QClipboard::Clipboard);
99 }
100
101
102 bool GuiClipboard::hasLyXContents() const
103 {
104         QMimeData const * const source =
105                 qApp->clipboard()->mimeData(QClipboard::Clipboard);
106         return source && source->hasFormat(mime_type);
107 }
108
109
110 bool GuiClipboard::isInternal() const
111 {
112         // ownsClipboard() is also true for stuff coming from dialogs, e.g.
113         // the preamble dialog
114         // FIXME: This does only work on X11, since ownsClipboard() is
115         // hardwired to return false on Windows and OS X.
116         return qApp->clipboard()->ownsClipboard() && hasLyXContents();
117 }
118
119
120 void GuiClipboard::on_dataChanged()
121 {
122         text_clipboard_empty_ = qApp->clipboard()->
123                 text(QClipboard::Clipboard).isEmpty();
124
125         has_lyx_contents_ = hasLyXContents();
126 }
127
128
129 bool GuiClipboard::empty() const
130 {
131         // We need to check both the plaintext and the LyX version of the
132         // clipboard. The plaintext version is empty if the LyX version
133         // contains only one inset, and the LyX version is empty if the
134         // clipboard does not come from LyX.
135         if (!text_clipboard_empty_)
136                 return false;
137         return !has_lyx_contents_;
138 }
139
140 } // namespace frontend
141 } // namespace lyx
142
143 #include "GuiClipboard_moc.cpp"