1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/update/updater/progressui_osx.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 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 +#import <Cocoa/Cocoa.h> 1.11 +#include <stdio.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 0.2 1.18 + 1.19 +static float sProgressVal; // between 0 and 100 1.20 +static BOOL sQuit = FALSE; 1.21 +static StringTable sLabels; 1.22 +static const char *sUpdatePath; 1.23 + 1.24 +@interface UpdaterUI : NSObject 1.25 +{ 1.26 + IBOutlet NSProgressIndicator *progressBar; 1.27 + IBOutlet NSTextField *progressTextField; 1.28 +} 1.29 +@end 1.30 + 1.31 +@implementation UpdaterUI 1.32 + 1.33 +-(void)awakeFromNib 1.34 +{ 1.35 + NSWindow *w = [progressBar window]; 1.36 + 1.37 + [w setTitle:[NSString stringWithUTF8String:sLabels.title]]; 1.38 + [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]]; 1.39 + 1.40 + NSRect origTextFrame = [progressTextField frame]; 1.41 + [progressTextField sizeToFit]; 1.42 + 1.43 + int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width; 1.44 + 1.45 + if (widthAdjust > 0) { 1.46 + NSRect f; 1.47 + f.size.width = w.frame.size.width + widthAdjust; 1.48 + f.size.height = w.frame.size.height; 1.49 + [w setFrame:f display:YES]; 1.50 + } 1.51 + 1.52 + [w center]; 1.53 + 1.54 + [progressBar setIndeterminate:NO]; 1.55 + [progressBar setDoubleValue:0.0]; 1.56 + 1.57 + [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self 1.58 + selector:@selector(updateProgressUI:) 1.59 + userInfo:nil repeats:YES] retain]; 1.60 + 1.61 + // Make sure we are on top initially 1.62 + [NSApp activateIgnoringOtherApps:YES]; 1.63 +} 1.64 + 1.65 +// called when the timer goes off 1.66 +-(void)updateProgressUI:(NSTimer *)aTimer 1.67 +{ 1.68 + if (sQuit) { 1.69 + [aTimer invalidate]; 1.70 + [aTimer release]; 1.71 + 1.72 + // It seems to be necessary to activate and hide ourselves before we stop, 1.73 + // otherwise the "run" method will not return until the user focuses some 1.74 + // other app. The activate step is necessary if we are not the active app. 1.75 + // This is a big hack, but it seems to do the trick. 1.76 + [NSApp activateIgnoringOtherApps:YES]; 1.77 + [NSApp hide:self]; 1.78 + [NSApp stop:self]; 1.79 + } 1.80 + 1.81 + float progress = sProgressVal; 1.82 + 1.83 + [progressBar setDoubleValue:(double)progress]; 1.84 +} 1.85 + 1.86 +// leave this as returning a BOOL instead of NSApplicationTerminateReply 1.87 +// for backward compatibility 1.88 +- (BOOL)applicationShouldTerminate:(NSApplication *)sender 1.89 +{ 1.90 + return sQuit; 1.91 +} 1.92 + 1.93 +@end 1.94 + 1.95 +int 1.96 +InitProgressUI(int *pargc, char ***pargv) 1.97 +{ 1.98 + sUpdatePath = (*pargv)[1]; 1.99 + 1.100 + return 0; 1.101 +} 1.102 + 1.103 +int 1.104 +ShowProgressUI() 1.105 +{ 1.106 + // Only show the Progress UI if the process is taking a significant amount of 1.107 + // time where a significant amount of time is defined as .5 seconds after 1.108 + // ShowProgressUI is called sProgress is less than 70. 1.109 + usleep(500000); 1.110 + 1.111 + if (sQuit || sProgressVal > 70.0f) 1.112 + return 0; 1.113 + 1.114 + char path[PATH_MAX]; 1.115 + snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath); 1.116 + if (ReadStrings(path, &sLabels) != OK) 1.117 + return -1; 1.118 + 1.119 + // Continue the update without showing the Progress UI if any of the supplied 1.120 + // strings are larger than MAX_TEXT_LEN (Bug 628829). 1.121 + if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 && 1.122 + strlen(sLabels.info) < MAX_TEXT_LEN - 1)) 1.123 + return -1; 1.124 + 1.125 + [NSApplication sharedApplication]; 1.126 + [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; 1.127 + [NSApp run]; 1.128 + 1.129 + return 0; 1.130 +} 1.131 + 1.132 +// Called on a background thread 1.133 +void 1.134 +QuitProgressUI() 1.135 +{ 1.136 + sQuit = TRUE; 1.137 +} 1.138 + 1.139 +// Called on a background thread 1.140 +void 1.141 +UpdateProgressUI(float progress) 1.142 +{ 1.143 + sProgressVal = progress; // 32-bit writes are atomic 1.144 +}