1 /*
2 Copyright (c) 2003-2013 uim Project https://github.com/uim/uim
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of authors nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 SUCH DAMAGE.
30
31 */
32
33 #include <config.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <dcopobject.h>
40 #include <dcopclient.h>
41 #include <kapplication.h>
42 #include <knotifyclient.h>
43
44 #include "uim.h" // for uim_bool
45 #include "uim-notify.h"
46 #include "gettext.h"
47
48 #define UGETTEXT(str) (dgettext(GETTEXT_PACKAGE, (str)))
49
50 static uim_notify_desc uim_notify_knotify3_desc = {
51 "knotify3",
52 "Output via knotify",
53 };
54
55 const uim_notify_desc *
uim_notify_plugin_get_desc(void)56 uim_notify_plugin_get_desc(void)
57 {
58 return &uim_notify_knotify3_desc;
59 }
60
61 uim_bool
uim_notify_plugin_init()62 uim_notify_plugin_init()
63 {
64 return UIM_TRUE;
65 }
66
67 void
uim_notify_plugin_quit()68 uim_notify_plugin_quit()
69 {
70 return;
71 }
72
73 static uim_bool
send_knotify(const char * eventstr,const char * msg,int level)74 send_knotify(const char *eventstr, const char *msg, int level)
75 {
76 char body[BUFSIZ];
77 char body_short[256];
78 QByteArray data;
79 QDataStream arg(data, IO_WriteOnly);
80 QString event(eventstr), fromApp("uim"), text, sound(""), file("");
81 int present = KNotifyClient::Messagebox | level;
82
83 snprintf(body, sizeof(body), "libuim: %s", UGETTEXT(msg));
84 fprintf(stderr, "%s\n", UGETTEXT(msg));
85
86 strlcpy(body_short, body, sizeof(body_short));
87 text = body_short;
88
89 if (!kapp->dcopClient()->attach()) {
90 fprintf(stderr, "libuim: cannot connect DCOP\n");
91 return UIM_FALSE;
92 }
93 arg << event << fromApp << text << sound << file << present << level;
94 if (!kapp->dcopClient()->send("knotify", "Notify", "notify(QString,QString,QString,QString,QString,int,int)",
95 data)) {
96 fprintf(stderr, "libuim: cannot send message via DCOP\n");
97 return UIM_FALSE;
98 }
99 return UIM_TRUE;
100 }
101
102 uim_bool
uim_notify_plugin_info(const char * msg)103 uim_notify_plugin_info(const char *msg)
104 {
105 return send_knotify("Info", msg, KNotifyClient::Notification);
106 }
107
108 uim_bool
uim_notify_plugin_fatal(const char * msg)109 uim_notify_plugin_fatal(const char *msg)
110 {
111 return send_knotify("Fatal", msg, KNotifyClient::Error);
112 }
113