summaryrefslogtreecommitdiff
path: root/profiles/gmp-util.c
blob: 209540189cbd05a1bc2d28bdbbf31f898359abc8 (plain)
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
/* gmp-util.c: utility functions */

/*
 * Copyright (C) 2003 Thomas Vander Stichele
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "gmp-util.h"

GtkBuilder *
gmp_util_load_builder_file (const char *filename,
			    GtkWindow  *error_dialog_parent,
			    GError **err)
{
  static GtkWidget *no_glade_dialog = NULL;
  gchar *path;
  GtkBuilder *builder;
  GError *error = NULL;

  path = g_strconcat ("./", filename, NULL);

  builder = gtk_builder_new ();

  /* Try current dir, for debugging */
  if (g_file_test (path, G_FILE_TEST_EXISTS) && gtk_builder_add_from_file (builder, path, &error))
    goto end;

  if (error != NULL) {
    g_warning ("%s", error->message);
    g_error_free (error);
    error = NULL;
  }

  g_free (path);
  path = g_build_filename (GMP_UIDIR, filename, NULL);
  if (g_file_test (path, G_FILE_TEST_EXISTS) && gtk_builder_add_from_file (builder, path, &error))
    goto end;

  gmp_util_show_error_dialog (error_dialog_parent, &no_glade_dialog,
			      _("The file \"%s\" is missing. This indicates that the application is installed incorrectly, so the dialog can't be displayed."), path);
  g_free (path);
  if (error != NULL) {
    g_propagate_error (err, error);
  }

  return builder;

 end:
  g_free (path);

  return builder;
}

void
gmp_util_run_error_dialog (GtkWindow *transient_parent, const char *message_format, ...)
{
  char *message;
  va_list args;
  GtkWidget *dialog;

  if (message_format)
  {
    va_start (args, message_format);
    message = g_strdup_vprintf (message_format, args);
    va_end (args);
  }
  else message = NULL;

  dialog = gtk_message_dialog_new (transient_parent,
                                 GTK_DIALOG_DESTROY_WITH_PARENT,
                                 GTK_MESSAGE_ERROR,
                                 GTK_BUTTONS_CLOSE,
                                 "%s",
                                 message);

  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy(GTK_WIDGET (dialog));
}

/**
 * gmp_util_show_error_dialog:
 * @transient_parent: parent of the future dialog window;
 * @weap_ptr: pointer to a #Widget pointer, to control the population.
 * @message_format: printf() style format string
 *
 * Create a #GtkMessageDialog window with the message, and present it,
 * handling its buttons.
 * If @weap_ptr is not #NULL, only create the dialog if
 * <literal>*weap_ptr</literal> is #NULL
 * (and in that case, set @weap_ptr to be a weak pointer to the new dialog),
 * otherwise just present <literal>*weak_ptr</literal>. Note that in this
 * last case, the message <emph>will</emph>  be changed.
 */

void
gmp_util_show_error_dialog (GtkWindow *transient_parent,
                            GtkWidget **weak_ptr,
                            const char *message_format, ...)
{
  char *message;
  va_list args;

  if (message_format)
  {
    va_start (args, message_format);
    message = g_strdup_vprintf (message_format, args);
    va_end (args);
  }
  else message = NULL;

  if (weak_ptr == NULL || *weak_ptr == NULL)
  {
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new (transient_parent,
                                     GTK_DIALOG_DESTROY_WITH_PARENT,
                                     GTK_MESSAGE_ERROR,
                                     GTK_BUTTONS_CLOSE,
                                     "%s",
                                     message);

    g_signal_connect (G_OBJECT (dialog), "response",
                      G_CALLBACK (gtk_widget_destroy), NULL);

    if (weak_ptr != NULL)
    {
      *weak_ptr = dialog;
      g_object_add_weak_pointer (G_OBJECT (dialog), (void**)weak_ptr);
    }

    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

    gtk_widget_show_all (dialog);
  }
  else
  {
    g_return_if_fail (GTK_IS_MESSAGE_DIALOG (*weak_ptr));

    g_object_set (*weak_ptr, "text", message, NULL);

    gtk_window_present (GTK_WINDOW (*weak_ptr));
  }
}