Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #import <Cocoa/Cocoa.h> |
michael@0 | 8 | #include <stdio.h> |
michael@0 | 9 | #include <unistd.h> |
michael@0 | 10 | #include "progressui.h" |
michael@0 | 11 | #include "readstrings.h" |
michael@0 | 12 | #include "errors.h" |
michael@0 | 13 | |
michael@0 | 14 | #define TIMER_INTERVAL 0.2 |
michael@0 | 15 | |
michael@0 | 16 | static float sProgressVal; // between 0 and 100 |
michael@0 | 17 | static BOOL sQuit = FALSE; |
michael@0 | 18 | static StringTable sLabels; |
michael@0 | 19 | static const char *sUpdatePath; |
michael@0 | 20 | |
michael@0 | 21 | @interface UpdaterUI : NSObject |
michael@0 | 22 | { |
michael@0 | 23 | IBOutlet NSProgressIndicator *progressBar; |
michael@0 | 24 | IBOutlet NSTextField *progressTextField; |
michael@0 | 25 | } |
michael@0 | 26 | @end |
michael@0 | 27 | |
michael@0 | 28 | @implementation UpdaterUI |
michael@0 | 29 | |
michael@0 | 30 | -(void)awakeFromNib |
michael@0 | 31 | { |
michael@0 | 32 | NSWindow *w = [progressBar window]; |
michael@0 | 33 | |
michael@0 | 34 | [w setTitle:[NSString stringWithUTF8String:sLabels.title]]; |
michael@0 | 35 | [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]]; |
michael@0 | 36 | |
michael@0 | 37 | NSRect origTextFrame = [progressTextField frame]; |
michael@0 | 38 | [progressTextField sizeToFit]; |
michael@0 | 39 | |
michael@0 | 40 | int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width; |
michael@0 | 41 | |
michael@0 | 42 | if (widthAdjust > 0) { |
michael@0 | 43 | NSRect f; |
michael@0 | 44 | f.size.width = w.frame.size.width + widthAdjust; |
michael@0 | 45 | f.size.height = w.frame.size.height; |
michael@0 | 46 | [w setFrame:f display:YES]; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | [w center]; |
michael@0 | 50 | |
michael@0 | 51 | [progressBar setIndeterminate:NO]; |
michael@0 | 52 | [progressBar setDoubleValue:0.0]; |
michael@0 | 53 | |
michael@0 | 54 | [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self |
michael@0 | 55 | selector:@selector(updateProgressUI:) |
michael@0 | 56 | userInfo:nil repeats:YES] retain]; |
michael@0 | 57 | |
michael@0 | 58 | // Make sure we are on top initially |
michael@0 | 59 | [NSApp activateIgnoringOtherApps:YES]; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // called when the timer goes off |
michael@0 | 63 | -(void)updateProgressUI:(NSTimer *)aTimer |
michael@0 | 64 | { |
michael@0 | 65 | if (sQuit) { |
michael@0 | 66 | [aTimer invalidate]; |
michael@0 | 67 | [aTimer release]; |
michael@0 | 68 | |
michael@0 | 69 | // It seems to be necessary to activate and hide ourselves before we stop, |
michael@0 | 70 | // otherwise the "run" method will not return until the user focuses some |
michael@0 | 71 | // other app. The activate step is necessary if we are not the active app. |
michael@0 | 72 | // This is a big hack, but it seems to do the trick. |
michael@0 | 73 | [NSApp activateIgnoringOtherApps:YES]; |
michael@0 | 74 | [NSApp hide:self]; |
michael@0 | 75 | [NSApp stop:self]; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | float progress = sProgressVal; |
michael@0 | 79 | |
michael@0 | 80 | [progressBar setDoubleValue:(double)progress]; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // leave this as returning a BOOL instead of NSApplicationTerminateReply |
michael@0 | 84 | // for backward compatibility |
michael@0 | 85 | - (BOOL)applicationShouldTerminate:(NSApplication *)sender |
michael@0 | 86 | { |
michael@0 | 87 | return sQuit; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @end |
michael@0 | 91 | |
michael@0 | 92 | int |
michael@0 | 93 | InitProgressUI(int *pargc, char ***pargv) |
michael@0 | 94 | { |
michael@0 | 95 | sUpdatePath = (*pargv)[1]; |
michael@0 | 96 | |
michael@0 | 97 | return 0; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | int |
michael@0 | 101 | ShowProgressUI() |
michael@0 | 102 | { |
michael@0 | 103 | // Only show the Progress UI if the process is taking a significant amount of |
michael@0 | 104 | // time where a significant amount of time is defined as .5 seconds after |
michael@0 | 105 | // ShowProgressUI is called sProgress is less than 70. |
michael@0 | 106 | usleep(500000); |
michael@0 | 107 | |
michael@0 | 108 | if (sQuit || sProgressVal > 70.0f) |
michael@0 | 109 | return 0; |
michael@0 | 110 | |
michael@0 | 111 | char path[PATH_MAX]; |
michael@0 | 112 | snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath); |
michael@0 | 113 | if (ReadStrings(path, &sLabels) != OK) |
michael@0 | 114 | return -1; |
michael@0 | 115 | |
michael@0 | 116 | // Continue the update without showing the Progress UI if any of the supplied |
michael@0 | 117 | // strings are larger than MAX_TEXT_LEN (Bug 628829). |
michael@0 | 118 | if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 && |
michael@0 | 119 | strlen(sLabels.info) < MAX_TEXT_LEN - 1)) |
michael@0 | 120 | return -1; |
michael@0 | 121 | |
michael@0 | 122 | [NSApplication sharedApplication]; |
michael@0 | 123 | [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; |
michael@0 | 124 | [NSApp run]; |
michael@0 | 125 | |
michael@0 | 126 | return 0; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | // Called on a background thread |
michael@0 | 130 | void |
michael@0 | 131 | QuitProgressUI() |
michael@0 | 132 | { |
michael@0 | 133 | sQuit = TRUE; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | // Called on a background thread |
michael@0 | 137 | void |
michael@0 | 138 | UpdateProgressUI(float progress) |
michael@0 | 139 | { |
michael@0 | 140 | sProgressVal = progress; // 32-bit writes are atomic |
michael@0 | 141 | } |