1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include <stdio.h>
8 #include <gtk/gtk.h>
9 #include <unistd.h>
10 #include "mozilla/Sprintf.h"
11 #include "progressui.h"
12 #include "readstrings.h"
13 #include "errors.h"
14
15 #define TIMER_INTERVAL 100
16
17 static float sProgressVal; // between 0 and 100
18 static gboolean sQuit = FALSE;
19 static gboolean sEnableUI;
20 static guint sTimerID;
21
22 static GtkWidget *sWin;
23 static GtkWidget *sLabel;
24 static GtkWidget *sProgressBar;
25
26 static const char *sProgramPath;
27
28 static gboolean
UpdateDialog(gpointer data)29 UpdateDialog(gpointer data)
30 {
31 if (sQuit)
32 {
33 gtk_widget_hide(sWin);
34 gtk_main_quit();
35 }
36
37 float progress = sProgressVal;
38
39 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar),
40 progress / 100.0);
41
42 return TRUE;
43 }
44
45 static gboolean
OnDeleteEvent(GtkWidget * widget,GdkEvent * event,gpointer user_data)46 OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
47 {
48 return TRUE;
49 }
50
51 int
InitProgressUI(int * pargc,char *** pargv)52 InitProgressUI(int *pargc, char ***pargv)
53 {
54 sProgramPath = (*pargv)[0];
55
56 sEnableUI = gtk_init_check(pargc, pargv);
57 return 0;
58 }
59
60 int
ShowProgressUI()61 ShowProgressUI()
62 {
63 if (!sEnableUI)
64 return -1;
65
66 // Only show the Progress UI if the process is taking a significant amount of
67 // time where a significant amount of time is defined as .5 seconds after
68 // ShowProgressUI is called sProgress is less than 70.
69 usleep(500000);
70
71 if (sQuit || sProgressVal > 70.0f)
72 return 0;
73
74 char ini_path[PATH_MAX];
75 SprintfLiteral(ini_path, "%s.ini", sProgramPath);
76
77 StringTable strings;
78 if (ReadStrings(ini_path, &strings) != OK)
79 return -1;
80
81 sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
82 if (!sWin)
83 return -1;
84
85 static GdkPixbuf *pixbuf;
86 char icon_path[PATH_MAX];
87 SprintfLiteral(icon_path, "%s.png", sProgramPath);
88
89 g_signal_connect(G_OBJECT(sWin), "delete_event",
90 G_CALLBACK(OnDeleteEvent), nullptr);
91
92 gtk_window_set_title(GTK_WINDOW(sWin), strings.title);
93 gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG);
94 gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS);
95 gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE);
96 gtk_window_set_decorated(GTK_WINDOW(sWin), TRUE);
97 gtk_window_set_deletable(GTK_WINDOW(sWin),FALSE);
98 pixbuf = gdk_pixbuf_new_from_file (icon_path, nullptr);
99 gtk_window_set_icon(GTK_WINDOW(sWin), pixbuf);
100 g_object_unref(pixbuf);
101
102 GtkWidget *vbox = gtk_vbox_new(TRUE, 6);
103 sLabel = gtk_label_new(strings.info);
104 gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f);
105 sProgressBar = gtk_progress_bar_new();
106
107 gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0);
108 gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0);
109
110 sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr);
111
112 gtk_container_set_border_width(GTK_CONTAINER(sWin), 10);
113 gtk_container_add(GTK_CONTAINER(sWin), vbox);
114 gtk_widget_show_all(sWin);
115
116 gtk_main();
117 return 0;
118 }
119
120 // Called on a background thread
121 void
QuitProgressUI()122 QuitProgressUI()
123 {
124 sQuit = TRUE;
125 }
126
127 // Called on a background thread
128 void
UpdateProgressUI(float progress)129 UpdateProgressUI(float progress)
130 {
131 sProgressVal = progress; // 32-bit writes are atomic
132 }
133