LibreOffice Module onlineupdate (master) 1
progressui_gtk.cxx
Go to the documentation of this file.
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
24static float sProgressVal; // between 0 and 100
25static gboolean sQuit = FALSE;
26static gboolean sEnableUI;
27static guint sTimerID;
28
32
33static const char *sProgramPath;
34
35static gboolean
36UpdateDialog(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
52static gboolean
53OnDeleteEvent(GtkWidget * /*widget*/, GdkEvent * /*event*/, gpointer /*user_data*/)
54{
55 return TRUE;
56}
57
58int
59InitProgressUI(int *pargc, char ***pargv)
60{
61 sProgramPath = (*pargv)[0];
62
63 sEnableUI = gtk_init_check(pargc, pargv);
64 return 0;
65}
66
67int
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_box_new(GTK_ORIENTATION_VERTICAL, 6);
111 gtk_box_set_homogeneous(GTK_BOX(vbox), true);
112 sLabel = gtk_label_new(strings.info);
113 gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f);
114 sProgressBar = gtk_progress_bar_new();
115
116 gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0);
117 gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0);
118
119 sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr);
120
121 gtk_container_set_border_width(GTK_CONTAINER(sWin), 10);
122 gtk_container_add(GTK_CONTAINER(sWin), vbox);
123 gtk_widget_show_all(sWin);
124
125 gtk_main();
126 return 0;
127}
128
129// Called on a background thread
130void
132{
133 sQuit = TRUE;
134}
135
136// Called on a background thread
137void
138UpdateProgressUI(float progress)
139{
140 sProgressVal = progress; // 32-bit writes are atomic
141}
142
143#ifdef __GNUC__
144#pragma GCC diagnostic pop
145#endif
146
147#endif // defined(UNIX) || defined(MACOSX)
#define OK
Definition: errors.h:10
struct _GtkWidget GtkWidget
#define TRUE
#define FALSE
static gboolean UpdateDialog(gpointer)
static gboolean OnDeleteEvent(GtkWidget *, GdkEvent *, gpointer)
static GtkWidget * sLabel
#define TIMER_INTERVAL
static gboolean sEnableUI
int ShowProgressUI()
void QuitProgressUI()
static gboolean sQuit
static const char * sProgramPath
static GtkWidget * sProgressBar
void UpdateProgressUI(float progress)
static float sProgressVal
static guint sTimerID
static GtkWidget * sWin
int InitProgressUI(int *pargc, char ***pargv)
static const char * icon_data[]
int ReadStrings(const NS_tchar *path, const char *keyList, unsigned int numStrings, char results[][MAX_TEXT_LEN], const char *section)
A very basic parser for updater.ini taken mostly from nsINIParser.cpp that can be used by standalone ...
char title[MAX_TEXT_LEN]
Definition: readstrings.h:20
char info[MAX_TEXT_LEN]
Definition: readstrings.h:21