PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/mozapps/update/updater/progressui_osx.mm

http://github.com/zpao/v8monkey
Objective C++ | 174 lines | 91 code | 30 blank | 53 comment | 7 complexity | ad8125ec7ee3098a2b584837640d016f MD5 | raw file
  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/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is Google Inc.
 19 * Portions created by the Initial Developer are Copyright (C) 2005
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Josh Aas <josh@mozilla.com>
 24 *  Darin Fisher <darin@meer.net>
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40#import <Cocoa/Cocoa.h>
 41#include <stdio.h>
 42#include <unistd.h>
 43#include "progressui.h"
 44#include "readstrings.h"
 45#include "errors.h"
 46
 47#define TIMER_INTERVAL 0.2
 48
 49static float sProgressVal;  // between 0 and 100
 50static BOOL sQuit = FALSE;
 51static StringTable sLabels;
 52static const char *sUpdatePath;
 53
 54@interface UpdaterUI : NSObject
 55{
 56  IBOutlet NSProgressIndicator *progressBar;
 57  IBOutlet NSTextField *progressTextField;
 58}
 59@end
 60
 61@implementation UpdaterUI
 62
 63-(void)awakeFromNib
 64{
 65  NSWindow *w = [progressBar window];
 66  
 67  [w setTitle:[NSString stringWithUTF8String:sLabels.title]];
 68  [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]];
 69
 70  NSRect origTextFrame = [progressTextField frame];
 71  [progressTextField sizeToFit];
 72
 73  int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width;
 74
 75  if (widthAdjust > 0) {
 76    NSRect f;
 77    f.size.width  = w.frame.size.width + widthAdjust;
 78    f.size.height = w.frame.size.height;
 79    [w setFrame:f display:YES];
 80  }
 81
 82  [w center];
 83
 84  [progressBar setIndeterminate:NO];
 85  [progressBar setDoubleValue:0.0];
 86
 87  [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self
 88                                  selector:@selector(updateProgressUI:)
 89                                  userInfo:nil repeats:YES] retain];
 90
 91  // Make sure we are on top initially
 92  [NSApp activateIgnoringOtherApps:YES];
 93}
 94
 95// called when the timer goes off
 96-(void)updateProgressUI:(NSTimer *)aTimer
 97{
 98  if (sQuit) {
 99    [aTimer invalidate];
100    [aTimer release];
101
102    // It seems to be necessary to activate and hide ourselves before we stop,
103    // otherwise the "run" method will not return until the user focuses some
104    // other app.  The activate step is necessary if we are not the active app.
105    // This is a big hack, but it seems to do the trick.
106    [NSApp activateIgnoringOtherApps:YES];
107    [NSApp hide:self];
108    [NSApp stop:self];
109  }
110  
111  float progress = sProgressVal;
112  
113  [progressBar setDoubleValue:(double)progress];
114}
115
116// leave this as returning a BOOL instead of NSApplicationTerminateReply
117// for backward compatibility
118- (BOOL)applicationShouldTerminate:(NSApplication *)sender
119{
120  return sQuit;
121}
122
123@end
124
125int
126InitProgressUI(int *pargc, char ***pargv)
127{
128  sUpdatePath = (*pargv)[1];
129  
130  return 0;
131}
132
133int
134ShowProgressUI()
135{
136  // Only show the Progress UI if the process is taking a significant amount of
137  // time where a significant amount of time is defined as .5 seconds after
138  // ShowProgressUI is called sProgress is less than 70.
139  usleep(500000);
140  
141  if (sQuit || sProgressVal > 70.0f)
142    return 0;
143
144  char path[PATH_MAX];
145  snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath);
146  if (ReadStrings(path, &sLabels) != OK)
147    return -1;
148
149  // Continue the update without showing the Progress UI if any of the supplied
150  // strings are larger than MAX_TEXT_LEN (Bug 628829).
151  if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 &&
152        strlen(sLabels.info) < MAX_TEXT_LEN - 1))
153    return -1;
154  
155  [NSApplication sharedApplication];
156  [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
157  [NSApp run];
158
159  return 0;
160}
161
162// Called on a background thread
163void
164QuitProgressUI()
165{
166  sQuit = TRUE;
167}
168
169// Called on a background thread
170void
171UpdateProgressUI(float progress)
172{
173  sProgressVal = progress;  // 32-bit writes are atomic
174}