1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2002 Philip Langdale
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "PrintProgressListener.h"
#include <nsIInterfaceRequestorUtils.h>
#include <nsIPrintSettings.h>
#include <nsIRequest.h>
#include <nsIWebBrowserPrint.h>
#include <nsIWebProgress.h>
#include <nsNetError.h> // for NS_BINDING_ABORTED
#include <nsMemory.h>
#include "gul-glade.h"
#include "galeon-debug.h"
#include "egg-recent-model.h"
/* see the FIXME below */
#include <locale.h>
#include <gtk/gtkdialog.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkprogressbar.h>
#include <glib/gi18n.h>
NS_IMPL_ISUPPORTS1(GPrintListener, nsIWebProgressListener)
static void
set_progress (GtkProgressBar *progress, guint current, guint total, gint remaining)
{
gdouble fraction = (gdouble)current / total;
char *text;
if (current >= total)
{
text = g_strdup (_("Completed"));
fraction = 1.0;
}
else if (remaining >= 0)
{
text = g_strdup_printf (_("%d of %d pages printed, about %d seconds left"),
current, total, remaining);
}
else
{
text = g_strdup_printf (_("%d of %d pages printed"), current, total);
}
gtk_progress_bar_set_fraction (progress, fraction);
gtk_progress_bar_set_text (progress, text);
g_free (text);
}
GPrintListener::GPrintListener(nsIWebBrowserPrint *aPrint, nsIPrintSettings *aSettings, GtkWindow *aParent)
: mStartTime(PR_Now())
, mWeakPrint(getter_AddRefs(NS_GetWeakReference(aPrint)))
{
LOG ("GPrintListener() = %p", this);
GladeXML *gxml;
gxml = gul_glade_widget_new ("print.glade",
"print_progress_dialog", &mDialog,
NULL);
g_object_add_weak_pointer (G_OBJECT (mDialog), (gpointer*)&mDialog);
gtk_window_set_transient_for (GTK_WINDOW (mDialog), aParent);
if (!aParent->group)
{
GtkWindowGroup *group = gtk_window_group_new ();
gtk_window_group_add_window (group, aParent);
g_object_unref (group);
}
gtk_window_group_add_window (aParent->group, GTK_WINDOW (mDialog));
// Since there's no way to cancel printing (nsIWebBrowserPrint::Cancel
// always fails for me...) don't give false impression that closing the
// dialog would do it.
g_signal_connect (mDialog, "delete-event", G_CALLBACK (gtk_true), 0);
GtkWidget *document;
GtkWidget *label;
GtkWidget *destination;
gul_glade_get_widgets (gxml,
"document", &document,
"destination_label", &label,
"destination", &destination,
"progressbar", &mProgress,
NULL);
g_object_add_weak_pointer (G_OBJECT (mProgress), (gpointer*)&mProgress);
g_object_unref (gxml);
PRUnichar *wstring = nsnull;
PRBool printToFile;
aSettings->GetPrintToFile(&printToFile);
if (printToFile)
{
aSettings->GetToFileName(&wstring);
mFilename = wstring;
char *markup = g_strdup_printf ("<b>%s</b>", _("To file:"));
char *basename = g_path_get_basename (mFilename.get());
gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_label_set_text (GTK_LABEL (destination), basename);
g_free (basename);
g_free (markup);
}
else
{
aSettings->GetPrinterName(&wstring);
char *markup = g_strdup_printf ("<b>%s</b>", _("To printer:"));
gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_label_set_text (GTK_LABEL (destination), GulCString (wstring).get());
g_free (markup);
}
nsMemory::Free(wstring);
aSettings->GetTitle(&wstring);
gtk_label_set_text (GTK_LABEL (document), GulCString (wstring).get());
nsMemory::Free(wstring);
// set the progress bar text temporarily to get the size request as
// wide as it can possibly get to avoid unnecessary width changes while
// printing (the addition of time estimate)
set_progress (GTK_PROGRESS_BAR(mProgress), 998, 999, 1000);
}
GPrintListener::~GPrintListener()
{
LOG ("~GPrintListener(%p)", this);
if (mDialog)
{
gtk_widget_destroy (mDialog);
}
}
static gboolean
destroy_timeout_cb (gpointer user_data)
{
GtkWidget *widget = GTK_WIDGET(user_data);
gtk_widget_destroy (widget);
return FALSE;
}
/* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
NS_IMETHODIMP GPrintListener::OnStateChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 aStateFlags,
PRUint32 aStatus)
{
if (!(aStateFlags & nsIWebProgressListener::STATE_STOP)) return NS_OK;
LOG ("[%p] GPrintListener::OnStateChange(%x, %x (%s))", this,
aStateFlags, aStatus, NS_SUCCEEDED(aStatus) ? "SUCCEEDED" : "FAILED");
// keep the dialog visible for a while after completion to display the
// "completed" message and avoid annoying flash when the file is very
// small
if (mDialog)
{
g_object_remove_weak_pointer (G_OBJECT(mDialog), (gpointer*)&mDialog);
g_object_remove_weak_pointer (G_OBJECT(mProgress), (gpointer*)&mProgress);
gtk_window_set_destroy_with_parent (GTK_WINDOW(mDialog), FALSE);
g_timeout_add (1000, destroy_timeout_cb, mDialog);
mDialog = NULL;
}
/* FIXME(MOZILLA) ugly workaround for a mozilla problem with
* reseting the LC_* environment when printing */
setlocale(LC_ALL,"");
// add to recent files if printing to file was successful
if (NS_SUCCEEDED(aStatus) && !mFilename.IsEmpty())
{
EggRecentModel *model;
EggRecentItem *item;
model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_NONE);
item = egg_recent_item_new_from_uri (mFilename.get());
// keep in sync with ProgressListener.cpp
egg_recent_item_add_group (item, "Galeon");
egg_recent_item_add_group (item, "Web Browser");
egg_recent_model_add_full (model, item);
egg_recent_item_unref (item);
g_object_unref (model);
}
return NS_OK;
}
/* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
NS_IMETHODIMP GPrintListener::OnProgressChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRInt32 aCurSelfProgress,
PRInt32 aMaxSelfProgress,
PRInt32 aCurTotalProgress,
PRInt32 aMaxTotalProgress)
{
LOG ("[%p] GPrintListener::OnProgressChange(%d, %d, %d, %d)", this,
aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
if (!mProgress) return NS_OK;
if (mDialog) gtk_widget_show (mDialog);
PRInt32 remaining;
if (aCurTotalProgress >= 5)
{
PRTime elapsed = PR_Now() - mStartTime;
PRTime estimate = elapsed * aMaxTotalProgress / aCurTotalProgress;
remaining = (PRInt32)((estimate - elapsed) / PR_USEC_PER_SEC);
}
else
remaining = -1;
set_progress (GTK_PROGRESS_BAR(mProgress), aCurTotalProgress, aMaxTotalProgress, remaining);
return NS_OK;
}
/* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
NS_IMETHODIMP GPrintListener::OnLocationChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
nsIURI *location)
{
return NS_OK;
}
/* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
NS_IMETHODIMP GPrintListener::OnStatusChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
nsresult aStatus,
const PRUnichar *aMessage)
{
LOG ("[%p] GPrintListener::OnStatusChange(%x (%s), '%s')", this,
aStatus, NS_SUCCEEDED(aStatus) ? "SUCCEEDED" : "FAILED",
GulCString (aMessage).get());
return NS_OK;
}
/* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
NS_IMETHODIMP GPrintListener::OnSecurityChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 state)
{
return NS_OK;
}
|