1 /* EasyTAG - Tag editor for audio files
2  * Copyright (C) 2014  David King <amigadave@amigadave.com>
3  * Copyright (C) 2000-2003  Jerome Couderc <easytag@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "config.h"
21 
22 #include "status_bar.h"
23 
24 #include <glib/gi18n.h>
25 
26 #include "charset.h"
27 
28 typedef struct
29 {
30     guint message_context;
31     guint timer_context;
32     guint timer_id;
33 } EtStatusBarPrivate;
34 
35 G_DEFINE_TYPE_WITH_PRIVATE (EtStatusBar, et_status_bar, GTK_TYPE_STATUSBAR)
36 
37 static void et_status_bar_remove_timer (EtStatusBar *self);
38 
39 static gboolean
et_status_bar_stop_timer(EtStatusBar * self)40 et_status_bar_stop_timer (EtStatusBar *self)
41 {
42     EtStatusBarPrivate *priv;
43 
44     priv = et_status_bar_get_instance_private (self);
45 
46     gtk_statusbar_pop (GTK_STATUSBAR (self), priv->timer_context);
47 
48     return G_SOURCE_REMOVE;
49 }
50 
51 static void
et_status_bar_reset_timer(EtStatusBar * self)52 et_status_bar_reset_timer (EtStatusBar *self)
53 {
54     EtStatusBarPrivate *priv;
55 
56     priv = et_status_bar_get_instance_private (self);
57 
58     priv->timer_id = 0;
59 }
60 
61 static void
et_status_bar_start_timer(EtStatusBar * self)62 et_status_bar_start_timer (EtStatusBar *self)
63 {
64     EtStatusBarPrivate *priv;
65 
66     priv = et_status_bar_get_instance_private (self);
67 
68     et_status_bar_remove_timer (self);
69     priv->timer_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, 4,
70                                                  (GSourceFunc)et_status_bar_stop_timer,
71                                                  self,
72                                                  (GDestroyNotify)et_status_bar_reset_timer);
73     g_source_set_name_by_id (priv->timer_id, "Statusbar stop timer");
74 }
75 
76 static void
et_status_bar_remove_timer(EtStatusBar * self)77 et_status_bar_remove_timer (EtStatusBar *self)
78 {
79     EtStatusBarPrivate *priv;
80 
81     priv = et_status_bar_get_instance_private (self);
82 
83     if (priv->timer_id)
84     {
85         et_status_bar_stop_timer (self);
86         g_source_remove (priv->timer_id);
87         et_status_bar_reset_timer (self);
88     }
89 }
90 
91 /*
92  * Send a message to the status bar
93  *  - with_timer: if TRUE, the message will be displayed during 4s
94  *                if FALSE, the message will be displayed up to the next posted message
95  */
96 void
et_status_bar_message(EtStatusBar * self,const gchar * message,gboolean with_timer)97 et_status_bar_message (EtStatusBar *self,
98                        const gchar *message,
99                        gboolean with_timer)
100 {
101     EtStatusBarPrivate *priv;
102     gchar *msg_temp;
103 
104     g_return_if_fail (ET_STATUS_BAR (self));
105 
106     priv = et_status_bar_get_instance_private (self);
107 
108     msg_temp = Try_To_Validate_Utf8_String (message);
109 
110     /* Push the given message */
111     if (with_timer)
112     {
113         et_status_bar_start_timer (self);
114         gtk_statusbar_push (GTK_STATUSBAR (self), priv->timer_context,
115                             msg_temp);
116     }
117     else
118     {
119         gtk_statusbar_pop (GTK_STATUSBAR (self), priv->message_context);
120         gtk_statusbar_push (GTK_STATUSBAR (self), priv->message_context,
121                             msg_temp);
122     }
123 
124     g_free (msg_temp);
125 }
126 
127 static void
create_status_bar(EtStatusBar * self)128 create_status_bar (EtStatusBar *self)
129 {
130     EtStatusBarPrivate *priv;
131 
132     priv = et_status_bar_get_instance_private (self);
133 
134     /* Specify a size to avoid statubar resizing if the message is too long */
135     gtk_widget_set_size_request (GTK_WIDGET (self), 200, -1);
136 
137     /* Create serie */
138     priv->message_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (self),
139                                                           "messages");
140     priv->timer_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (self),
141                                                         "timer");
142 
143     et_status_bar_message (self, _("Ready to start"), TRUE);
144 }
145 
146 static void
et_status_bar_init(EtStatusBar * self)147 et_status_bar_init (EtStatusBar *self)
148 {
149     create_status_bar (self);
150 }
151 
152 static void
et_status_bar_class_init(EtStatusBarClass * klass)153 et_status_bar_class_init (EtStatusBarClass *klass)
154 {
155 }
156 
157 /*
158  * et_status_bar_new:
159  *
160  * Create a new EtStatusBar instance.
161  *
162  * Returns: a new #EtStatusBar
163  */
164 GtkWidget *
et_status_bar_new(void)165 et_status_bar_new (void)
166 {
167     return g_object_new (ET_TYPE_STATUS_BAR, NULL);
168 }
169