From 21f119060181898a20ef0d2c766277e6de49d673 Mon Sep 17 00:00:00 2001 From: Nikola Pajkovsky Date: Fri, 27 Aug 2010 15:50:51 +0200 Subject: get rid of CApplet class Signed-off-by: Nikola Pajkovsky Acked-off-by: Denys Vlasenko --- src/applet/Applet.cpp | 18 +++--- src/applet/CCApplet.cpp | 154 +++++++++++++++++++++++++----------------------- src/applet/CCApplet.h | 100 +++++++++++++++---------------- 3 files changed, 136 insertions(+), 136 deletions(-) (limited to 'src') diff --git a/src/applet/Applet.cpp b/src/applet/Applet.cpp index be9c662d..2f35cab5 100644 --- a/src/applet/Applet.cpp +++ b/src/applet/Applet.cpp @@ -31,7 +31,7 @@ #include "CCApplet.h" -static CApplet* applet; +static struct applet* applet = NULL; static void Crash(DBusMessage* signal) @@ -80,8 +80,8 @@ static void Crash(DBusMessage* signal) if (package_name[0] == '\0') message = _("A crash has been detected"); //applet->AddEvent(uid, package_name); - applet->SetIconTooltip(message, package_name); - applet->ShowIcon(); + SetIconTooltip(applet, message, package_name); + ShowIcon(applet); /* If this crash seems to be repeating, do not annoy user with popup dialog. * (The icon in the tray is not suppressed) @@ -103,7 +103,7 @@ static void Crash(DBusMessage* signal) free(last_crash_id); last_crash_id = xstrdup(crash_id); - applet->CrashNotify(crash_id, message, package_name); + CrashNotify(applet, crash_id, message, package_name); } static void QuotaExceed(DBusMessage* signal) @@ -121,8 +121,8 @@ static void QuotaExceed(DBusMessage* signal) //if (m_pSessionDBus->has_name("com.redhat.abrt.gui")) // return; - applet->ShowIcon(); - applet->MessageNotify("%s", str); + ShowIcon(applet); + MessageNotify(applet, "%s", str); } static void NameOwnerChanged(DBusMessage* signal) @@ -159,7 +159,7 @@ static void NameOwnerChanged(DBusMessage* signal) // hide icon if it's visible - as NM and don't show it, if it's not if (!new_owner[0]) - applet->HideIcon(); + HideIcon(applet); } static DBusHandlerResult handle_message(DBusConnection* conn, DBusMessage* msg, void* user_data) @@ -261,7 +261,7 @@ int main(int argc, char** argv) /* Initialize GUI stuff. * Note: inside CApplet ctor, libnotify hooks session dbus * to glib main loop */ - applet = new CApplet(app_name); + applet = applet_new(app_name); /* dbus_abrt cannot handle more than one bus, and we don't really need to. * The only thing we want to do is to announce ourself on session dbus */ DBusConnection* session_conn = dbus_bus_get(DBUS_BUS_SESSION, &err); @@ -291,6 +291,6 @@ int main(int argc, char** argv) gtk_main(); gdk_threads_leave(); - delete applet; + applet_destroy(applet); return 0; } diff --git a/src/applet/CCApplet.cpp b/src/applet/CCApplet.cpp index 4f6c2c65..587f568a 100644 --- a/src/applet/CCApplet.cpp +++ b/src/applet/CCApplet.cpp @@ -48,7 +48,7 @@ static NotifyNotification *new_warn_notification() static void on_hide_cb(GtkMenuItem *menuitem, gpointer applet) { if (applet) - ((CApplet*)applet)->HideIcon(); + HideIcon((struct applet*)applet); } static void on_about_cb(GtkMenuItem *menuitem, gpointer dialog) @@ -101,7 +101,7 @@ static GtkWidget *create_about_dialog() return about_d; } -static GtkWidget *create_menu(CApplet *applet) +static GtkWidget *create_menu(struct applet *applet) { GtkWidget *menu = gtk_menu_new(); GtkWidget *b_quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL); @@ -126,59 +126,64 @@ static GtkWidget *create_menu(CApplet *applet) return menu; } -CApplet::CApplet(const char* app_name) +struct applet *applet_new(const char* app_name) { - m_bDaemonRunning = true; + struct applet *applet = (struct applet*)xmalloc(sizeof(struct applet)); + applet->m_bDaemonRunning = true; /* set-up icon buffers */ - m_iAnimator = 0; - m_iAnimationStage = ICON_DEFAULT; - m_bIconsLoaded = load_icons(); + applet->m_iAnimator = 0; + applet->m_iAnimationStage = ICON_DEFAULT; + applet->m_bIconsLoaded = load_icons(applet); /* - animation - */ - if (m_bIconsLoaded == true) + if (applet->m_bIconsLoaded == true) { //FIXME: animation is disabled for now - m_pStatusIcon = gtk_status_icon_new_from_pixbuf(icon_stages_buff[ICON_DEFAULT]); + applet->m_pStatusIcon = gtk_status_icon_new_from_pixbuf(applet->icon_stages_buff[ICON_DEFAULT]); } else { - m_pStatusIcon = gtk_status_icon_new_from_icon_name("abrt"); + applet->m_pStatusIcon = gtk_status_icon_new_from_icon_name("abrt"); } notify_init(app_name); - gtk_status_icon_set_visible(m_pStatusIcon, FALSE); + gtk_status_icon_set_visible(applet->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); + g_signal_connect(G_OBJECT(applet->m_pStatusIcon), "activate", GTK_SIGNAL_FUNC(OnAppletActivate_CB), applet); + g_signal_connect(G_OBJECT(applet->m_pStatusIcon), "popup_menu", GTK_SIGNAL_FUNC(OnMenuPopup_cb), applet); // SetIconTooltip(_("Pending events: %i"), m_mapEvents.size()); - m_pMenu = create_menu(this); + applet->m_pMenu = create_menu(applet); + return applet; } -CApplet::~CApplet() +void applet_destroy(struct applet *applet) { if (notify_is_initted()) notify_uninit(); + + free(applet); } -void CApplet::SetIconTooltip(const char *format, ...) +void SetIconTooltip(struct applet *applet, const char *format, ...) { va_list args; int n; char *buf; + // xvasprintf? va_start(args, format); buf = NULL; n = vasprintf(&buf, format, args); va_end(args); - gtk_status_icon_set_tooltip_text(m_pStatusIcon, (n >= 0 && buf) ? buf : ""); + gtk_status_icon_set_tooltip_text(applet->m_pStatusIcon, (n >= 0 && buf) ? buf : ""); free(buf); } -void CApplet::action_report(NotifyNotification *notification, gchar *action, gpointer user_data) +void action_report(NotifyNotification *notification, gchar *action, gpointer user_data) { - CApplet *applet = (CApplet *)user_data; + struct applet *applet = (struct applet *)user_data; if (applet->m_bDaemonRunning) { pid_t pid = vfork(); @@ -202,13 +207,13 @@ void CApplet::action_report(NotifyNotification *notification, gchar *action, gpo g_error_free(err); } gtk_status_icon_set_visible(applet->m_pStatusIcon, false); - applet->stop_animate_icon(); + stop_animate_icon(applet); } } -void CApplet::action_open_gui(NotifyNotification *notification, gchar *action, gpointer user_data) +void action_open_gui(NotifyNotification *notification, gchar *action, gpointer user_data) { - CApplet *applet = (CApplet *)user_data; + struct applet *applet = (struct applet*)user_data; if (applet->m_bDaemonRunning) { pid_t pid = vfork(); @@ -231,13 +236,13 @@ void CApplet::action_open_gui(NotifyNotification *notification, gchar *action, g g_error_free(err); } gtk_status_icon_set_visible(applet->m_pStatusIcon, false); - applet->stop_animate_icon(); + stop_animate_icon(applet); } } -void CApplet::CrashNotify(const char* crash_id, const char *format, ...) +void CrashNotify(struct applet *applet, const char* crash_id, const char *format, ...) { - m_pLastCrashID = crash_id; + applet->m_pLastCrashID = crash_id; va_list args; va_start(args, format); char *buf = xvasprintf(format, args); @@ -245,11 +250,11 @@ void CApplet::CrashNotify(const char* crash_id, const char *format, ...) NotifyNotification *notification = new_warn_notification(); notify_notification_add_action(notification, "REPORT", _("Report"), - NOTIFY_ACTION_CALLBACK(CApplet::action_report), - this, NULL); + NOTIFY_ACTION_CALLBACK(action_report), + applet, NULL); notify_notification_add_action(notification, "OPEN_MAIN_WINDOW", _("Open ABRT"), - NOTIFY_ACTION_CALLBACK(CApplet::action_open_gui), - this, NULL); + NOTIFY_ACTION_CALLBACK(action_open_gui), + applet, NULL); notify_notification_update(notification, _("Warning"), buf, NULL); free(buf); @@ -262,7 +267,7 @@ void CApplet::CrashNotify(const char* crash_id, const char *format, ...) } } -void CApplet::MessageNotify(const char *format, ...) +void MessageNotify(struct applet *applet, const char *format, ...) { va_list args; @@ -276,8 +281,8 @@ void CApplet::MessageNotify(const char *format, ...) */ NotifyNotification *notification = new_warn_notification(); notify_notification_add_action(notification, "OPEN_MAIN_WINDOW", _("Open ABRT"), - NOTIFY_ACTION_CALLBACK(CApplet::action_open_gui), - this, NULL); + NOTIFY_ACTION_CALLBACK(action_open_gui), + applet, NULL); notify_notification_update(notification, _("Warning"), buf, NULL); free(buf); GError *err = NULL; @@ -289,9 +294,9 @@ void CApplet::MessageNotify(const char *format, ...) } } -void CApplet::OnAppletActivate_CB(GtkStatusIcon *status_icon, gpointer user_data) +void OnAppletActivate_CB(GtkStatusIcon *status_icon, gpointer user_data) { - CApplet *applet = (CApplet *)user_data; + struct applet *applet = (struct applet*)user_data; if (applet->m_bDaemonRunning) { pid_t pid = vfork(); @@ -307,48 +312,48 @@ void CApplet::OnAppletActivate_CB(GtkStatusIcon *status_icon, gpointer user_data perror_msg_and_die("Can't execute abrt-gui"); } gtk_status_icon_set_visible(applet->m_pStatusIcon, false); - applet->stop_animate_icon(); + stop_animate_icon(applet); } } -void CApplet::OnMenuPopup_cb(GtkStatusIcon *status_icon, - guint button, - guint activate_time, - gpointer user_data) +void OnMenuPopup_cb(GtkStatusIcon *status_icon, + guint button, + guint activate_time, + gpointer user_data) { - CApplet *applet = (CApplet *)user_data; + struct applet *applet = (struct applet*)user_data; /* stop the animation */ - applet->stop_animate_icon(); + stop_animate_icon(applet); if (applet->m_pMenu != NULL) { - gtk_menu_popup(GTK_MENU(((CApplet *)user_data)->m_pMenu), + gtk_menu_popup(GTK_MENU(applet->m_pMenu), NULL, NULL, gtk_status_icon_position_menu, status_icon, button, activate_time); } } -void CApplet::ShowIcon() +void ShowIcon(struct applet *applet) { - gtk_status_icon_set_visible(m_pStatusIcon, true); + gtk_status_icon_set_visible(applet->m_pStatusIcon, true); /* only animate if all icons are loaded, use the "gtk-warning" instead */ - if (m_bIconsLoaded) - animate_icon(); + if (applet->m_bIconsLoaded) + animate_icon(applet); } -void CApplet::HideIcon() +void HideIcon(struct applet *applet) { - gtk_status_icon_set_visible(m_pStatusIcon, false); - stop_animate_icon(); + gtk_status_icon_set_visible(applet->m_pStatusIcon, false); + stop_animate_icon(applet); } -void CApplet::Disable(const char *reason) +void Disable(struct applet *applet, const char *reason) { /* FIXME: once we have our icon */ - m_bDaemonRunning = false; + applet->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); @@ -356,27 +361,28 @@ void CApplet::Disable(const char *reason) { gray_scaled = gdk_pixbuf_copy(pixbuf); gdk_pixbuf_saturate_and_pixelate(pixbuf, gray_scaled, 0.0, false); - gtk_status_icon_set_from_pixbuf(m_pStatusIcon, gray_scaled); + gtk_status_icon_set_from_pixbuf(applet->m_pStatusIcon, gray_scaled); //do we need to free pixbufs nere? } else error_msg("Can't load icon"); - SetIconTooltip(reason); - ShowIcon(); + SetIconTooltip(applet, reason); + ShowIcon(applet); } -void CApplet::Enable(const char *reason) +void Enable(struct applet *applet, 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(); + applet->m_bDaemonRunning = true; + SetIconTooltip(applet, reason); + gtk_status_icon_set_from_stock(applet->m_pStatusIcon, GTK_STOCK_DIALOG_WARNING); + ShowIcon(applet); } -gboolean CApplet::update_icon(void *user_data) +// why it is not named with suffix _cb when it is callback for g_timeout_add? +gboolean update_icon(void *user_data) { - CApplet* applet = (CApplet*)user_data; + struct applet *applet = (struct applet*)user_data; if (applet->m_pStatusIcon && applet->m_iAnimationStage < ICON_STAGE_LAST) { gtk_status_icon_set_from_pixbuf(applet->m_pStatusIcon, @@ -388,32 +394,34 @@ gboolean CApplet::update_icon(void *user_data) } if (--applet->m_iAnimCountdown == 0) { - applet->stop_animate_icon(); + stop_animate_icon(applet); } return true; } -void CApplet::animate_icon() +void animate_icon(struct applet* applet) { - if (m_iAnimator == 0) + if (applet->m_iAnimator == 0) { - m_iAnimator = g_timeout_add(100, update_icon, this); - m_iAnimCountdown = 10 * 3; /* 3 sec */ + applet->m_iAnimator = g_timeout_add(100, update_icon, applet); + applet->m_iAnimCountdown = 10 * 3; /* 3 sec */ } } -void CApplet::stop_animate_icon() +void stop_animate_icon(struct applet *applet) { /* animator should be 0 if icons are not loaded, so this should be safe */ - if (m_iAnimator != 0) + if (applet->m_iAnimator != 0) { - g_source_remove(m_iAnimator); - gtk_status_icon_set_from_pixbuf(m_pStatusIcon, icon_stages_buff[ICON_DEFAULT]); - m_iAnimator = 0; + g_source_remove(applet->m_iAnimator); + gtk_status_icon_set_from_pixbuf(applet->m_pStatusIcon, + applet->icon_stages_buff[ICON_DEFAULT] + ); + applet->m_iAnimator = 0; } } -bool CApplet::load_icons() +bool load_icons(struct applet *applet) { //FIXME: just a tmp workaround return false; @@ -424,7 +432,7 @@ bool CApplet::load_icons() GError *error = NULL; if (snprintf(name, sizeof(ICON_DIR"/abrt%02d.png"), ICON_DIR"/abrt%02d.png", stage) > 0) { - icon_stages_buff[stage] = gdk_pixbuf_new_from_file(name, &error); + applet->icon_stages_buff[stage] = gdk_pixbuf_new_from_file(name, &error); if (error != NULL) { error_msg("Can't load pixbuf from %s, animation is disabled", name); diff --git a/src/applet/CCApplet.h b/src/applet/CCApplet.h index a58ec687..ebcf78da 100644 --- a/src/applet/CCApplet.h +++ b/src/applet/CCApplet.h @@ -24,65 +24,57 @@ #include #include -class CApplet +enum ICON_STAGES { - private: - GtkStatusIcon* m_pStatusIcon; - GtkWidget *m_pMenu; + ICON_DEFAULT, + ICON_STAGE1, + ICON_STAGE2, + ICON_STAGE3, + ICON_STAGE4, + ICON_STAGE5, + /* this must be always the last */ + ICON_STAGE_LAST +}; + +struct applet { + GtkStatusIcon* m_pStatusIcon; + GtkWidget *m_pMenu; // std::map m_mapEvents; - bool m_bDaemonRunning; - int m_iAnimationStage; - guint m_iAnimator; - unsigned m_iAnimCountdown; - bool m_bIconsLoaded; - const char *m_pLastCrashID; + bool m_bDaemonRunning; + int m_iAnimationStage; + guint m_iAnimator; + unsigned m_iAnimCountdown; + bool m_bIconsLoaded; + const char *m_pLastCrashID; - enum ICON_STAGES - { - ICON_DEFAULT, - ICON_STAGE1, - ICON_STAGE2, - ICON_STAGE3, - ICON_STAGE4, - ICON_STAGE5, - /* this must be always the last */ - ICON_STAGE_LAST - } icon_stages; - GdkPixbuf *icon_stages_buff[ICON_STAGE_LAST]; + GdkPixbuf *icon_stages_buff[ICON_STAGE_LAST]; +}; - public: - CApplet(const char* app_name); - ~CApplet(); - void ShowIcon(); - void HideIcon(); - void SetIconTooltip(const char *format, ...); - void CrashNotify(const char* crash_id, const char *format, ...); - void MessageNotify(const char *format, ...); - void Disable(const char *reason); - void Enable(const char *reason); - // create some event storage, to let user choose - // or ask the daemon every time? - // maybe just events which occured during current session - // map:: -// int AddEvent(int pUUID, const char *pProgname); -// int RemoveEvent(int pUUID); +struct applet* applet_new(const char *app_name); +void applet_destroy(struct applet *applet); - protected: - //@@TODO applet menus - static void OnAppletActivate_CB(GtkStatusIcon *status_icon, gpointer user_data); - //this action should open the reporter dialog directly, without showing the main window - static void action_report(NotifyNotification *notification, gchar *action, gpointer user_data); - //this action should open the main window - static void action_open_gui(NotifyNotification *notification, gchar *action, gpointer user_data); - static void OnMenuPopup_cb(GtkStatusIcon *status_icon, - guint button, - guint activate_time, - gpointer user_data); - static gboolean update_icon(void *data); - void animate_icon(); - void stop_animate_icon(); - bool load_icons(); -}; +void ShowIcon(struct applet *applet); +void HideIcon(struct applet *applet); +void SetIconTooltip(struct applet *applet, const char *format, ...); +void CrashNotify(struct applet *applet, const char* crash_id, const char *format, ...); +void MessageNotify(struct applet *applet, const char *format, ...); +void Disable(struct applet *applet, const char *reason); +void Enable(struct applet *applet, const char *reason); + +// static in next patch +void OnAppletActivate_CB(GtkStatusIcon *status_icon, gpointer user_data); +//this action should open the reporter dialog directly, without showing the main window +void action_report(NotifyNotification *notification, gchar *action, gpointer user_data); +//this action should open the main window +void action_open_gui(NotifyNotification *notification, gchar *action, gpointer user_data); +void OnMenuPopup_cb(GtkStatusIcon *status_icon, + guint button, + guint activate_time, + gpointer user_data); +gboolean update_icon(void *data); +void animate_icon(struct applet *applet); +void stop_animate_icon(struct applet *applet); +bool load_icons(struct applet *applet); #endif -- cgit