1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/update/updater/progressui_gtk.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,131 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <stdio.h> 1.11 +#include <gtk/gtk.h> 1.12 +#include <unistd.h> 1.13 +#include "progressui.h" 1.14 +#include "readstrings.h" 1.15 +#include "errors.h" 1.16 + 1.17 +#define TIMER_INTERVAL 100 1.18 + 1.19 +static float sProgressVal; // between 0 and 100 1.20 +static gboolean sQuit = FALSE; 1.21 +static gboolean sEnableUI; 1.22 +static guint sTimerID; 1.23 + 1.24 +static GtkWidget *sWin; 1.25 +static GtkWidget *sLabel; 1.26 +static GtkWidget *sProgressBar; 1.27 + 1.28 +static const char *sProgramPath; 1.29 + 1.30 +static gboolean 1.31 +UpdateDialog(gpointer data) 1.32 +{ 1.33 + if (sQuit) 1.34 + { 1.35 + gtk_widget_hide(sWin); 1.36 + gtk_main_quit(); 1.37 + } 1.38 + 1.39 + float progress = sProgressVal; 1.40 + 1.41 + gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar), 1.42 + progress / 100.0); 1.43 + 1.44 + return TRUE; 1.45 +} 1.46 + 1.47 +static gboolean 1.48 +OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data) 1.49 +{ 1.50 + return TRUE; 1.51 +} 1.52 + 1.53 +int 1.54 +InitProgressUI(int *pargc, char ***pargv) 1.55 +{ 1.56 + sProgramPath = (*pargv)[0]; 1.57 + 1.58 + sEnableUI = gtk_init_check(pargc, pargv); 1.59 + return 0; 1.60 +} 1.61 + 1.62 +int 1.63 +ShowProgressUI() 1.64 +{ 1.65 + if (!sEnableUI) 1.66 + return -1; 1.67 + 1.68 + // Only show the Progress UI if the process is taking a significant amount of 1.69 + // time where a significant amount of time is defined as .5 seconds after 1.70 + // ShowProgressUI is called sProgress is less than 70. 1.71 + usleep(500000); 1.72 + 1.73 + if (sQuit || sProgressVal > 70.0f) 1.74 + return 0; 1.75 + 1.76 + char ini_path[PATH_MAX]; 1.77 + snprintf(ini_path, sizeof(ini_path), "%s.ini", sProgramPath); 1.78 + 1.79 + StringTable strings; 1.80 + if (ReadStrings(ini_path, &strings) != OK) 1.81 + return -1; 1.82 + 1.83 + sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL); 1.84 + if (!sWin) 1.85 + return -1; 1.86 + 1.87 + static GdkPixbuf *pixbuf; 1.88 + char icon_path[PATH_MAX]; 1.89 + snprintf(icon_path, sizeof(icon_path), "%s.png", sProgramPath); 1.90 + 1.91 + g_signal_connect(G_OBJECT(sWin), "delete_event", 1.92 + G_CALLBACK(OnDeleteEvent), nullptr); 1.93 + 1.94 + gtk_window_set_title(GTK_WINDOW(sWin), strings.title); 1.95 + gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG); 1.96 + gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS); 1.97 + gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE); 1.98 + gtk_window_set_decorated(GTK_WINDOW(sWin), TRUE); 1.99 + gtk_window_set_deletable(GTK_WINDOW(sWin),FALSE); 1.100 + pixbuf = gdk_pixbuf_new_from_file (icon_path, nullptr); 1.101 + gtk_window_set_icon(GTK_WINDOW(sWin), pixbuf); 1.102 + g_object_unref(pixbuf); 1.103 + 1.104 + GtkWidget *vbox = gtk_vbox_new(TRUE, 6); 1.105 + sLabel = gtk_label_new(strings.info); 1.106 + gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f); 1.107 + sProgressBar = gtk_progress_bar_new(); 1.108 + 1.109 + gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0); 1.110 + gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0); 1.111 + 1.112 + sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr); 1.113 + 1.114 + gtk_container_set_border_width(GTK_CONTAINER(sWin), 10); 1.115 + gtk_container_add(GTK_CONTAINER(sWin), vbox); 1.116 + gtk_widget_show_all(sWin); 1.117 + 1.118 + gtk_main(); 1.119 + return 0; 1.120 +} 1.121 + 1.122 +// Called on a background thread 1.123 +void 1.124 +QuitProgressUI() 1.125 +{ 1.126 + sQuit = TRUE; 1.127 +} 1.128 + 1.129 +// Called on a background thread 1.130 +void 1.131 +UpdateProgressUI(float progress) 1.132 +{ 1.133 + sProgressVal = progress; // 32-bit writes are atomic 1.134 +}