PIKApp/app/widgets/pikaerrordialog.c

216 lines
6.1 KiB
C
Raw Permalink Normal View History

2023-09-26 00:35:21 +02:00
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikaerrordialog.c
* Copyright (C) 2004 Sven Neumann <sven@gimp.org>
*
* 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 3 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libpikawidgets/pikawidgets.h"
#include "widgets-types.h"
#include "pikaerrordialog.h"
#include "pikamessagebox.h"
#include "pika-intl.h"
#define PIKA_ERROR_DIALOG_MAX_MESSAGES 3
typedef struct
{
GtkWidget *box;
gchar *domain;
gchar *message;
} PikaErrorDialogMessage;
static void pika_error_dialog_finalize (GObject *object);
static void pika_error_dialog_response (GtkDialog *dialog,
gint response_id);
static void pika_error_dialog_message_destroy (gpointer data);
G_DEFINE_TYPE (PikaErrorDialog, pika_error_dialog, PIKA_TYPE_DIALOG)
#define parent_class pika_error_dialog_parent_class
static void
pika_error_dialog_class_init (PikaErrorDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
object_class->finalize = pika_error_dialog_finalize;
dialog_class->response = pika_error_dialog_response;
}
static void
pika_error_dialog_init (PikaErrorDialog *dialog)
{
gtk_window_set_role (GTK_WINDOW (dialog), "pika-message");
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
_("_OK"), GTK_RESPONSE_CLOSE,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
dialog->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
dialog->vbox, TRUE, TRUE, 0);
gtk_widget_show (dialog->vbox);
dialog->messages = NULL;
dialog->overflow = FALSE;
}
static void
pika_error_dialog_finalize (GObject *object)
{
PikaErrorDialog *dialog = PIKA_ERROR_DIALOG (object);
g_list_free_full (dialog->messages,
pika_error_dialog_message_destroy);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
pika_error_dialog_response (GtkDialog *dialog,
gint response_id)
{
gtk_widget_destroy (GTK_WIDGET (dialog));
}
static void
pika_error_dialog_message_destroy (gpointer data)
{
PikaErrorDialogMessage *item = (PikaErrorDialogMessage *) data;
g_free (item->domain);
g_free (item->message);
g_free (item);
}
/* public functions */
GtkWidget *
pika_error_dialog_new (const gchar *title)
{
gboolean use_header_bar;
g_return_val_if_fail (title != NULL, NULL);
g_object_get (gtk_settings_get_default (),
"gtk-dialogs-use-header", &use_header_bar,
NULL);
return g_object_new (PIKA_TYPE_ERROR_DIALOG,
"title", title,
"use-header-bar", use_header_bar,
NULL);
}
void
pika_error_dialog_add (PikaErrorDialog *dialog,
const gchar *icon_name,
const gchar *domain,
const gchar *message)
{
PikaErrorDialogMessage *item;
gboolean overflow = FALSE;
g_return_if_fail (PIKA_IS_ERROR_DIALOG (dialog));
g_return_if_fail (domain != NULL);
g_return_if_fail (message != NULL);
if (dialog->messages)
{
GList *iter = dialog->messages;
for (; iter; iter = iter->next)
{
item = iter->data;
if (strcmp (item->domain, domain) == 0 &&
strcmp (item->message, message) == 0)
{
if (pika_message_box_repeat (PIKA_MESSAGE_BOX (item->box)))
return;
}
}
}
if (g_list_length (dialog->messages) >= PIKA_ERROR_DIALOG_MAX_MESSAGES)
{
g_printerr ("%s: %s\n\n", domain, message);
overflow = TRUE;
icon_name = PIKA_ICON_MASCOT_EEK;
domain = _("Too many error messages!");
message = _("Messages are redirected to stderr.");
if (dialog->overflow)
{
/* We were already overflowing. */
return;
}
dialog->overflow = TRUE;
}
item = g_new0 (PikaErrorDialogMessage, 1);
item->box = g_object_new (PIKA_TYPE_MESSAGE_BOX,
"icon-name", icon_name,
NULL);
item->domain = g_strdup (domain);
item->message = g_strdup (message);
if (overflow)
pika_message_box_set_primary_text (PIKA_MESSAGE_BOX (item->box),
"%s", domain);
else
pika_message_box_set_primary_text (PIKA_MESSAGE_BOX (item->box),
/* %s is a message domain,
* like "PIKA Message" or
* "PNG Message"
*/
_("%s Message"), domain);
pika_message_box_set_text (PIKA_MESSAGE_BOX (item->box), "%s", message);
gtk_box_pack_start (GTK_BOX (dialog->vbox), item->box, TRUE, TRUE, 0);
gtk_widget_show (item->box);
dialog->messages = g_list_prepend (dialog->messages, item);
}