1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
Copyright (C) 2009 Jiri Moskovcak (jmoskovc@redhat.com)
Copyright (C) 2009 RedHat inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "abrtlib.h"
#include "CCApplet.h"
#include <iostream>
#include <cstdarg>
#include <sstream>
#include <cstdio>
#include <unistd.h>
#if HAVE_CONFIG_H
#include <config.h>
#endif
#if ENABLE_NLS
#include <libintl.h>
#define _(S) gettext(S)
#else
#define _(S) (S)
#endif
static const char *DBUS_SERVICE_NAME = "org.freedesktop.DBus";
static const char *DBUS_SERVICE_PATH = "/org/freedesktop/DBus";
const gchar *CApplet::menu_xml =
"<?xml version=\"1.0\"?>\
<interface>\
<requires lib=\"gtk+\" version=\"2.16\"/>\
<!-- interface-naming-policy project-wide -->\
<object class=\"GtkMenu\" id=\"popup_menu\">\
<property name=\"visible\">True</property>\
<child>\
<object class=\"GtkMenuItem\" id=\"miHide\">\
<property name=\"visible\">True</property>\
<property name=\"label\" translatable=\"yes\">Hide</property>\
</object>\
</child>\
<child>\
<object class=\"GtkImageMenuItem\" id=\"miQuit\">\
<property name=\"label\">gtk-quit</property>\
<property name=\"visible\">True</property>\
<property name=\"use_underline\">True</property>\
<property name=\"use_stock\">True</property>\
<property name=\"always_show_image\">True</property>\
</object>\
</child>\
<child>\
<object class=\"GtkSeparatorMenuItem\" id=\"miSep1\">\
<property name=\"visible\">True</property>\
</object>\
</child>\
<child>\
<object class=\"GtkImageMenuItem\" id=\"miAbout\">\
<property name=\"label\">gtk-about</property>\
<property name=\"visible\">True</property>\
<property name=\"use_underline\">True</property>\
<property name=\"use_stock\">True</property>\
<property name=\"always_show_image\">True</property>\
</object>\
</child>\
</object>\
<object class=\"GtkAboutDialog\" id=\"aboutdialog\">\
<property name=\"border_width\">5</property>\
<property name=\"type_hint\">normal</property>\
<property name=\"has_separator\">False</property>\
<property name=\"program_name\">Automatic Bug Reporting Tool</property>\
<property name=\"copyright\" translatable=\"yes\">Copyright © 2009 Red Hat, Inc</property>\
<property name=\"website\">https://fedorahosted.org/abrt/</property>\
<property name=\"website_label\" translatable=\"yes\">Website</property>\
<property name=\"license\" translatable=\"yes\">This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.\
\
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\
\
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.</property>\
<property name=\"authors\">Jiri Moskovcak <jmoskovc@redhat.com></property>\
<child internal-child=\"vbox\">\
<object class=\"GtkVBox\" id=\"dialog-vbox1\">\
<property name=\"visible\">True</property>\
<property name=\"orientation\">vertical</property>\
<property name=\"spacing\">2</property>\
<child>\
<placeholder/>\
</child>\
<child internal-child=\"action_area\">\
<object class=\"GtkHButtonBox\" id=\"dialog-action_area1\">\
<property name=\"visible\">True</property>\
<property name=\"layout_style\">end</property>\
</object>\
<packing>\
<property name=\"expand\">False</property>\
<property name=\"pack_type\">end</property>\
<property name=\"position\">0</property>\
</packing>\
</child>\
</object>\
</child>\
</object>\
</interface>";
CApplet::CApplet(DBus::Connection &system, DBus::Connection &session, const char *path, const char *name)
: DBus::ObjectProxy(system, path, name)
{
m_pSessionDBus = &session;
m_pDaemonWatcher = new DaemonWatcher(system, DBUS_SERVICE_PATH, DBUS_SERVICE_NAME);
m_pDaemonWatcher->ConnectStateChangeHandler(DaemonStateChange_cb, this);
m_pStatusIcon = gtk_status_icon_new_from_stock(GTK_STOCK_DIALOG_WARNING);
m_bDaemonRunning = true;
notify_init("ABRT");
m_pNotification = notify_notification_new_with_status_icon("Warning", NULL, NULL, m_pStatusIcon);
notify_notification_set_urgency(m_pNotification, NOTIFY_URGENCY_CRITICAL);
notify_notification_set_timeout(m_pNotification, 5000);
gtk_status_icon_set_visible(m_pStatusIcon, FALSE);
g_signal_connect(G_OBJECT(m_pStatusIcon), "activate", GTK_SIGNAL_FUNC(CApplet::OnAppletActivate_CB), this);
g_signal_connect(G_OBJECT(m_pStatusIcon), "popup_menu", GTK_SIGNAL_FUNC(CApplet::OnMenuPopup_cb), this);
SetIconTooltip(_("Pending events: %i"), m_mapEvents.size());
m_pBuilder = gtk_builder_new();
if(gtk_builder_add_from_string(m_pBuilder, menu_xml, strlen(menu_xml), NULL))
//if(gtk_builder_add_from_file(m_pBuilder, "popup.GtkBuilder", NULL))
{
m_pMenu = gtk_builder_get_object(m_pBuilder, "popup_menu");
//gtk_menu_attach_to_widget(GTK_MENU(m_pMenu), GTK_WIDGET(m_pStatusIcon), NULL);
m_pmiHide = gtk_builder_get_object(m_pBuilder, "miHide");
if(m_pmiHide != NULL)
{
g_signal_connect(m_pmiHide,"activate", G_CALLBACK(CApplet::onHide_cb), this);
}
m_pmiQuit = gtk_builder_get_object(m_pBuilder, "miQuit");
if(m_pmiQuit != NULL)
{
g_signal_connect(m_pmiQuit,"activate",G_CALLBACK(gtk_main_quit),NULL);
}
m_pAboutDialog = gtk_builder_get_object(m_pBuilder, "aboutdialog");
m_pmiAbout = gtk_builder_get_object(m_pBuilder, "miAbout");
{
g_signal_connect(m_pmiAbout,"activate",G_CALLBACK(CApplet::onAbout_cb),m_pAboutDialog);
}
}
else
{
fprintf(stderr,_("Can't create menu from the description, popup won't be available!\n"));
}
}
CApplet::~CApplet()
{
delete m_pDaemonWatcher;
}
/* dbus related */
void CApplet::Crash(const std::string& progname, const std::string& uid )
{
if(m_pSessionDBus->has_name("com.redhat.abrt.gui"))
{
return;
}
else
{
if (m_pCrashHandler)
{
std::istringstream input_string(uid);
uid_t num;
input_string >> num;
if( (num == getuid()) )
m_pCrashHandler(progname.c_str());
}
else
{
std::cout << _("This is default handler, you should register your own with ConnectCrashHandler") << std::endl;
std::cout.flush();
}
}
}
void CApplet::DaemonStateChange_cb(bool running, void* data)
{
CApplet *applet = (CApplet *)data;
if (!running)
{
applet->Disable(_("ABRT service is not running"));
}
else
{
applet->Enable(_("ABRT service has been started"));
}
}
void CApplet::ConnectCrashHandler(void (*pCrashHandler)(const char *progname))
{
m_pCrashHandler = pCrashHandler;
}
void CApplet::SetIconTooltip(const char *format, ...)
{
va_list args;
int n;
char *buf;
va_start(args, format);
buf = NULL;
n = vasprintf(&buf, format, args);
va_end(args);
if (n >= 0 && buf)
{
gtk_status_icon_set_tooltip_text(m_pStatusIcon, buf);
free(buf);
}
else
{
gtk_status_icon_set_tooltip_text(m_pStatusIcon, _("Out of memory"));
}
}
void CApplet::CrashNotify(const char *format, ...)
{
va_list args;
char *buf;
int n;
GError *err = NULL;
va_start(args, format);
buf = NULL;
n = vasprintf(&buf, format, args);
va_end(args);
notify_notification_update(m_pNotification, _("Warning"), buf, NULL);
if(gtk_status_icon_is_embedded (m_pStatusIcon))
notify_notification_show(m_pNotification, &err);
if(err != NULL)
g_print(err->message);
}
void CApplet::OnAppletActivate_CB(GtkStatusIcon *status_icon,gpointer user_data)
{
CApplet *applet = (CApplet *)user_data;
if (applet->m_bDaemonRunning)
{
pid_t pid = vfork();
if (pid < 0)
std::cerr << "vfork failed\n";
if (pid == 0)
{ /* child */
signal(SIGCHLD, SIG_DFL); /* undo SIG_IGN in abrt-applet */
execl(BIN_DIR"/abrt-gui", "abrt-gui", (char*) NULL);
/* Did not find abrt-gui in installation directory. Oh well */
/* Trying to find it in PATH */
execlp("abrt-gui", "abrt-gui", (char*) NULL);
std::cerr << "can't exec abrt-gui\n";
exit(1);
}
gtk_status_icon_set_visible(applet->m_pStatusIcon, false);
}
}
void CApplet::OnMenuPopup_cb(GtkStatusIcon *status_icon,
guint button,
guint activate_time,
gpointer user_data)
{
if(((CApplet *)user_data)->m_pMenu != NULL)
{
gtk_menu_popup(GTK_MENU(((CApplet *)user_data)->m_pMenu),NULL,NULL,gtk_status_icon_position_menu,status_icon,button,activate_time);
}
}
void CApplet::ShowIcon()
{
gtk_status_icon_set_visible(m_pStatusIcon, true);
//Active wait for icon to be REALLY visible in status area
//while(!gtk_status_icon_is_embedded (m_pStatusIcon));
}
void CApplet::onHide_cb(GtkMenuItem *menuitem, gpointer applet)
{
((CApplet*)applet)->HideIcon();
}
void CApplet::onAbout_cb(GtkMenuItem *menuitem, gpointer dialog)
{
if(dialog)
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_hide(GTK_WIDGET(dialog));
}
void CApplet::HideIcon()
{
gtk_status_icon_set_visible(m_pStatusIcon, false);
}
void CApplet::Disable(const char *reason)
{
/*
FIXME: once we have our icon
*/
m_bDaemonRunning = false;
GdkPixbuf *gray_scaled;
GdkPixbuf *pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), GTK_STOCK_DIALOG_WARNING, 24, GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
gray_scaled = gdk_pixbuf_copy(pixbuf);
if (pixbuf)
{
gdk_pixbuf_saturate_and_pixelate(pixbuf, gray_scaled, 0.0, NULL);
gtk_status_icon_set_from_pixbuf(m_pStatusIcon, gray_scaled);
}
else
std::cerr << "Cannot load icon!" << std::endl;
SetIconTooltip(reason);
ShowIcon();
}
void CApplet::Enable(const char *reason)
{
/* restore the original icon */
m_bDaemonRunning = true;
SetIconTooltip(reason);
gtk_status_icon_set_from_stock(m_pStatusIcon, GTK_STOCK_DIALOG_WARNING);
ShowIcon();
}
int CApplet::AddEvent(int pUUID, const std::string& pProgname)
{
m_mapEvents[pUUID] = "pProgname";
SetIconTooltip(_("Pending events: %i"), m_mapEvents.size());
return 0;
}
int CApplet::RemoveEvent(int pUUID)
{
m_mapEvents.erase(pUUID);
return 0;
}
void CApplet::BlinkIcon(bool pBlink)
{
gtk_status_icon_set_blinking(m_pStatusIcon, pBlink);
}
|