1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #include "clipboard.h"
22 #include "code/image.h"
23 
24 #include <QApplication>
25 #include <QImage>
26 #include <QMimeData>
27 #include <QScriptValueIterator>
28 
29 namespace Code
30 {
constructor(QScriptContext * context,QScriptEngine * engine)31 	QScriptValue Clipboard::constructor(QScriptContext *context, QScriptEngine *engine)
32 	{
33 		auto clipboard = new Clipboard;
34 
35 		if(context->argumentCount() > 0)
36 		{
37 			Mode mode = static_cast<Mode>(context->argument(0).toInt32());
38 			clipboard->setModePrivate(context, engine, mode);
39 		}
40 
41 		return CodeClass::constructor(clipboard, context, engine);
42 	}
43 
Clipboard()44 	Clipboard::Clipboard()
45 		: CodeClass()
46 
47 	{
48 	}
49 
text() const50 	QString Clipboard::text() const
51 	{
52 		QClipboard *clipboard = QApplication::clipboard();
53 
54 		return clipboard->text(mMode);
55 	}
56 
image() const57 	QScriptValue Clipboard::image() const
58 	{
59 		QClipboard *clipboard = QApplication::clipboard();
60 
61         return Image::constructor(clipboard->image(mMode), engine());
62     }
63 
64 
65 
setMode(Mode mode)66 	QScriptValue Clipboard::setMode(Mode mode)
67 	{
68 		setModePrivate(context(), engine(), mode);
69 
70 		return thisObject();
71 	}
72 
setText(const QString & value) const73 	QScriptValue Clipboard::setText(const QString &value) const
74 	{
75 		QClipboard *clipboard = QApplication::clipboard();
76 
77 		clipboard->setText(value, mMode);
78 
79 		return thisObject();
80 	}
81 
setImage(const QScriptValue & data) const82 	QScriptValue Clipboard::setImage(const QScriptValue &data) const
83 	{
84 		QClipboard *clipboard = QApplication::clipboard();
85 
86 		QObject *object = data.toQObject();
87 		if(auto image = qobject_cast<Code::Image*>(object))
88 			clipboard->setImage(image->image(), mMode);
89 		else
90 			clipboard->setImage(data.toVariant().value<QImage>(), mMode);
91 
92 		return thisObject();
93 	}
94 
dataType() const95 	Clipboard::DataType Clipboard::dataType() const
96 	{
97 		QClipboard *clipboard = QApplication::clipboard();
98 		const QMimeData *mimeData = clipboard->mimeData(mMode);
99 
100 		if(mimeData->hasImage())
101 			return Image;
102 		else
103 			return Text;
104 	}
105 
setModePrivate(QScriptContext * context,QScriptEngine * engine,Mode mode)106 	void Clipboard::setModePrivate(QScriptContext *context, QScriptEngine *engine, Mode mode)
107 	{
108 		switch(mode)
109 		{
110 		case Selection:
111 			if(!QApplication::clipboard()->supportsSelection())
112 			{
113 				throwError(context, engine, QStringLiteral("UnsupportedSelectionModeError"), tr("Selection mode is not supported by your operating system"));
114 				return;
115 			}
116 			break;
117 		case FindBuffer:
118 			if(!QApplication::clipboard()->supportsFindBuffer())
119 			{
120 				throwError(context, engine, QStringLiteral("UnsupportedSelectionModeError"), tr("Find buffer mode is not supported by your operating system"));
121 				return;
122 			}
123 			break;
124 		default:
125 			break;
126 		}
127 
128 		mMode = static_cast<QClipboard::Mode>(mode);
129 	}
130 }
131