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 "notify.h"
22 
23 #include <QScriptValueIterator>
24 
25 #ifdef Q_OS_UNIX
26 #undef signals
27 #include <libnotify/notify.h>
28 #define signals
29 #endif
30 
31 #ifndef NOTIFY_CHECK_VERSION
32 #define NOTIFY_CHECK_VERSION(x,y,z) 0
33 #endif
34 
35 namespace Code
36 {
constructor(QScriptContext * context,QScriptEngine * engine)37 	QScriptValue Notify::constructor(QScriptContext *context, QScriptEngine *engine)
38 	{
39 		auto notify = new Notify;
40 
41 		QScriptValueIterator it(context->argument(0));
42 
43 		while(it.hasNext())
44 		{
45 			it.next();
46 
47 			if(it.name() == QLatin1String("title"))
48 				notify->mTitle = it.value().toString();
49 			else if(it.name() == QLatin1String("text"))
50 				notify->mText = it.value().toString();
51 			else if(it.name() == QLatin1String("icon"))
52 				notify->mIcon = it.value().toString();
53 			else if(it.name() == QLatin1String("timeout"))
54 				notify->mTimeout = it.value().toInt32();
55 		}
56 
57 		return CodeClass::constructor(notify, context, engine);
58 	}
59 
Notify()60 	Notify::Notify()
61 		: CodeClass()
62 
63 	{
64 	}
65 
~Notify()66 	Notify::~Notify()
67 	{
68 #ifdef Q_OS_UNIX
69 		if(mNotification)
70 		{
71 			notify_notification_close(mNotification, nullptr);
72 			g_object_unref(mNotification);
73 		}
74 #endif
75 	}
76 
show()77 	QScriptValue Notify::show()
78 	{
79 #ifdef Q_OS_UNIX
80 		QScriptValueIterator it(context()->argument(0));
81 
82 		while(it.hasNext())
83 		{
84 			it.next();
85 
86 			if(it.name() == QLatin1String("title"))
87 				mTitle = it.value().toString();
88 			else if(it.name() == QLatin1String("text"))
89 				mText = it.value().toString();
90 			else if(it.name() == QLatin1String("icon"))
91 				mIcon = it.value().toString();
92 			else if(it.name() == QLatin1String("timeout"))
93 				mTimeout = it.value().toInt32();
94 		}
95 
96 		if(!mNotification)
97 			mNotification = notify_notification_new(mTitle.toUtf8().constData(), mText.toUtf8().constData(), mIcon.toUtf8().constData()
98 		#if NOTIFY_CHECK_VERSION (0, 7, 0)
99 		);
100 		#else
101 		, 0);
102 		#endif
103 		else
104 			notify_notification_update(mNotification, mTitle.toUtf8().constData(), mText.toUtf8().constData(), mIcon.toUtf8().constData());
105 
106 		notify_notification_set_timeout(mNotification, mTimeout);
107 
108 		if(!notify_notification_show(mNotification, nullptr))
109 			throwError(QStringLiteral("NotificationError"), tr("Unable to show the notification"));
110 #endif
111 		return thisObject();
112 	}
113 }
114