michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #import michael@0: #include michael@0: #include michael@0: #include "progressui.h" michael@0: #include "readstrings.h" michael@0: #include "errors.h" michael@0: michael@0: #define TIMER_INTERVAL 0.2 michael@0: michael@0: static float sProgressVal; // between 0 and 100 michael@0: static BOOL sQuit = FALSE; michael@0: static StringTable sLabels; michael@0: static const char *sUpdatePath; michael@0: michael@0: @interface UpdaterUI : NSObject michael@0: { michael@0: IBOutlet NSProgressIndicator *progressBar; michael@0: IBOutlet NSTextField *progressTextField; michael@0: } michael@0: @end michael@0: michael@0: @implementation UpdaterUI michael@0: michael@0: -(void)awakeFromNib michael@0: { michael@0: NSWindow *w = [progressBar window]; michael@0: michael@0: [w setTitle:[NSString stringWithUTF8String:sLabels.title]]; michael@0: [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]]; michael@0: michael@0: NSRect origTextFrame = [progressTextField frame]; michael@0: [progressTextField sizeToFit]; michael@0: michael@0: int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width; michael@0: michael@0: if (widthAdjust > 0) { michael@0: NSRect f; michael@0: f.size.width = w.frame.size.width + widthAdjust; michael@0: f.size.height = w.frame.size.height; michael@0: [w setFrame:f display:YES]; michael@0: } michael@0: michael@0: [w center]; michael@0: michael@0: [progressBar setIndeterminate:NO]; michael@0: [progressBar setDoubleValue:0.0]; michael@0: michael@0: [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self michael@0: selector:@selector(updateProgressUI:) michael@0: userInfo:nil repeats:YES] retain]; michael@0: michael@0: // Make sure we are on top initially michael@0: [NSApp activateIgnoringOtherApps:YES]; michael@0: } michael@0: michael@0: // called when the timer goes off michael@0: -(void)updateProgressUI:(NSTimer *)aTimer michael@0: { michael@0: if (sQuit) { michael@0: [aTimer invalidate]; michael@0: [aTimer release]; michael@0: michael@0: // It seems to be necessary to activate and hide ourselves before we stop, michael@0: // otherwise the "run" method will not return until the user focuses some michael@0: // other app. The activate step is necessary if we are not the active app. michael@0: // This is a big hack, but it seems to do the trick. michael@0: [NSApp activateIgnoringOtherApps:YES]; michael@0: [NSApp hide:self]; michael@0: [NSApp stop:self]; michael@0: } michael@0: michael@0: float progress = sProgressVal; michael@0: michael@0: [progressBar setDoubleValue:(double)progress]; michael@0: } michael@0: michael@0: // leave this as returning a BOOL instead of NSApplicationTerminateReply michael@0: // for backward compatibility michael@0: - (BOOL)applicationShouldTerminate:(NSApplication *)sender michael@0: { michael@0: return sQuit; michael@0: } michael@0: michael@0: @end michael@0: michael@0: int michael@0: InitProgressUI(int *pargc, char ***pargv) michael@0: { michael@0: sUpdatePath = (*pargv)[1]; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: ShowProgressUI() michael@0: { michael@0: // Only show the Progress UI if the process is taking a significant amount of michael@0: // time where a significant amount of time is defined as .5 seconds after michael@0: // ShowProgressUI is called sProgress is less than 70. michael@0: usleep(500000); michael@0: michael@0: if (sQuit || sProgressVal > 70.0f) michael@0: return 0; michael@0: michael@0: char path[PATH_MAX]; michael@0: snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath); michael@0: if (ReadStrings(path, &sLabels) != OK) michael@0: return -1; michael@0: michael@0: // Continue the update without showing the Progress UI if any of the supplied michael@0: // strings are larger than MAX_TEXT_LEN (Bug 628829). michael@0: if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 && michael@0: strlen(sLabels.info) < MAX_TEXT_LEN - 1)) michael@0: return -1; michael@0: michael@0: [NSApplication sharedApplication]; michael@0: [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; michael@0: [NSApp run]; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // Called on a background thread michael@0: void michael@0: QuitProgressUI() michael@0: { michael@0: sQuit = TRUE; michael@0: } michael@0: michael@0: // Called on a background thread michael@0: void michael@0: UpdateProgressUI(float progress) michael@0: { michael@0: sProgressVal = progress; // 32-bit writes are atomic michael@0: }