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/. */
7 #import <Cocoa/Cocoa.h>
10 #include "progressui.h"
11 #include "readstrings.h"
14 #define TIMER_INTERVAL 0.2
16 static float sProgressVal; // between 0 and 100
17 static BOOL sQuit = FALSE;
18 static StringTable sLabels;
19 static const char *sUpdatePath;
21 @interface UpdaterUI : NSObject
23 IBOutlet NSProgressIndicator *progressBar;
24 IBOutlet NSTextField *progressTextField;
28 @implementation UpdaterUI
32 NSWindow *w = [progressBar window];
34 [w setTitle:[NSString stringWithUTF8String:sLabels.title]];
35 [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]];
37 NSRect origTextFrame = [progressTextField frame];
38 [progressTextField sizeToFit];
40 int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width;
42 if (widthAdjust > 0) {
44 f.size.width = w.frame.size.width + widthAdjust;
45 f.size.height = w.frame.size.height;
46 [w setFrame:f display:YES];
51 [progressBar setIndeterminate:NO];
52 [progressBar setDoubleValue:0.0];
54 [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self
55 selector:@selector(updateProgressUI:)
56 userInfo:nil repeats:YES] retain];
58 // Make sure we are on top initially
59 [NSApp activateIgnoringOtherApps:YES];
62 // called when the timer goes off
63 -(void)updateProgressUI:(NSTimer *)aTimer
69 // It seems to be necessary to activate and hide ourselves before we stop,
70 // otherwise the "run" method will not return until the user focuses some
71 // other app. The activate step is necessary if we are not the active app.
72 // This is a big hack, but it seems to do the trick.
73 [NSApp activateIgnoringOtherApps:YES];
78 float progress = sProgressVal;
80 [progressBar setDoubleValue:(double)progress];
83 // leave this as returning a BOOL instead of NSApplicationTerminateReply
84 // for backward compatibility
85 - (BOOL)applicationShouldTerminate:(NSApplication *)sender
93 InitProgressUI(int *pargc, char ***pargv)
95 sUpdatePath = (*pargv)[1];
103 // Only show the Progress UI if the process is taking a significant amount of
104 // time where a significant amount of time is defined as .5 seconds after
105 // ShowProgressUI is called sProgress is less than 70.
108 if (sQuit || sProgressVal > 70.0f)
112 snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath);
113 if (ReadStrings(path, &sLabels) != OK)
116 // Continue the update without showing the Progress UI if any of the supplied
117 // strings are larger than MAX_TEXT_LEN (Bug 628829).
118 if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 &&
119 strlen(sLabels.info) < MAX_TEXT_LEN - 1))
122 [NSApplication sharedApplication];
123 [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
129 // Called on a background thread
136 // Called on a background thread
138 UpdateProgressUI(float progress)
140 sProgressVal = progress; // 32-bit writes are atomic