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 #if defined(UNIX) || defined(MACOSX)
8 #include <stdio.h>
9 #include <gtk/gtk.h>
10 #include <unistd.h>
11 #include "progressui.h"
12 #include "readstrings.h"
13 #include "errors.h"
14 #include <string.h>
15 #include "progressui_gtk_icon.h"
16 
17 #ifdef __GNUC__
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
20 #endif
21 
22 #define TIMER_INTERVAL 100
23 
24 static float    sProgressVal;  // between 0 and 100
25 static gboolean sQuit = FALSE;
26 static gboolean sEnableUI;
27 static guint    sTimerID;
28 
29 static GtkWidget *sWin;
30 static GtkWidget *sLabel;
31 static GtkWidget *sProgressBar;
32 
33 static const char *sProgramPath;
34 
35 static gboolean
UpdateDialog(gpointer)36 UpdateDialog(gpointer /*data*/)
37 {
38     if (sQuit)
39     {
40         gtk_widget_hide(sWin);
41         gtk_main_quit();
42     }
43 
44     float progress = sProgressVal;
45 
46     gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar),
47                                   progress / 100.0);
48 
49     return TRUE;
50 }
51 
52 static gboolean
OnDeleteEvent(GtkWidget *,GdkEvent *,gpointer)53 OnDeleteEvent(GtkWidget * /*widget*/, GdkEvent * /*event*/, gpointer /*user_data*/)
54 {
55     return TRUE;
56 }
57 
58 int
InitProgressUI(int * pargc,char *** pargv)59 InitProgressUI(int *pargc, char ***pargv)
60 {
61     sProgramPath = (*pargv)[0];
62 
63     sEnableUI = gtk_init_check(pargc, pargv);
64     return 0;
65 }
66 
67 int
ShowProgressUI()68 ShowProgressUI()
69 {
70     if (!sEnableUI)
71         return -1;
72 
73     // Only show the Progress UI if the process is taking a significant amount of
74     // time where a significant amount of time is defined as .5 seconds after
75     // ShowProgressUI is called sProgress is less than 70.
76     usleep(500000);
77 
78     if (sQuit || sProgressVal > 70.0f)
79         return 0;
80 
81     char ini_path[PATH_MAX];
82     snprintf(ini_path, sizeof(ini_path), "%s.ini", sProgramPath);
83 
84     StringTable strings;
85     if (ReadStrings(ini_path, &strings) != OK)
86     {
87         strcpy(strings.title, "LibreOffice Update");
88         strcpy(strings.info, "Please wait while we update your installation.");
89     }
90 
91     sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
92     if (!sWin)
93         return -1;
94 
95     static GdkPixbuf *pixbuf;
96 
97     g_signal_connect(G_OBJECT(sWin), "delete_event",
98                      G_CALLBACK(OnDeleteEvent), nullptr);
99 
100     gtk_window_set_title(GTK_WINDOW(sWin), strings.title);
101     gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG);
102     gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS);
103     gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE);
104     gtk_window_set_decorated(GTK_WINDOW(sWin), TRUE);
105     gtk_window_set_deletable(GTK_WINDOW(sWin),FALSE);
106     pixbuf = gdk_pixbuf_new_from_xpm_data (icon_data);
107     gtk_window_set_icon(GTK_WINDOW(sWin), pixbuf);
108     g_object_unref(pixbuf);
109 
110     GtkWidget *vbox = gtk_vbox_new(TRUE, 6);
111     sLabel = gtk_label_new(strings.info);
112     gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f);
113     sProgressBar = gtk_progress_bar_new();
114 
115     gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0);
116     gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0);
117 
118     sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr);
119 
120     gtk_container_set_border_width(GTK_CONTAINER(sWin), 10);
121     gtk_container_add(GTK_CONTAINER(sWin), vbox);
122     gtk_widget_show_all(sWin);
123 
124     gtk_main();
125     return 0;
126 }
127 
128 // Called on a background thread
129 void
QuitProgressUI()130 QuitProgressUI()
131 {
132     sQuit = TRUE;
133 }
134 
135 // Called on a background thread
136 void
UpdateProgressUI(float progress)137 UpdateProgressUI(float progress)
138 {
139     sProgressVal = progress;  // 32-bit writes are atomic
140 }
141 
142 #ifdef __GNUC__
143 #pragma GCC diagnostic pop
144 #endif
145 
146 #endif  // defined(UNIX) || defined(MACOSX)
147