/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. #import <Cocoa/Cocoa.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include "progressui.h"
  43. #include "readstrings.h"
  44. #include "errors.h"
  45. #define TIMER_INTERVAL 0.2
  46. static float sProgressVal; // between 0 and 100
  47. static BOOL sQuit = FALSE;
  48. static StringTable sLabels;
  49. static const char *sUpdatePath;
  50. @interface UpdaterUI : NSObject
  51. {
  52. IBOutlet NSProgressIndicator *progressBar;
  53. IBOutlet NSTextField *progressTextField;
  54. }
  55. @end
  56. @implementation UpdaterUI
  57. -(void)awakeFromNib
  58. {
  59. NSWindow *w = [progressBar window];
  60. [w setTitle:[NSString stringWithUTF8String:sLabels.title]];
  61. [progressTextField setStringValue:[NSString stringWithUTF8String:sLabels.info]];
  62. NSRect origTextFrame = [progressTextField frame];
  63. [progressTextField sizeToFit];
  64. int widthAdjust = progressTextField.frame.size.width - origTextFrame.size.width;
  65. if (widthAdjust > 0) {
  66. NSRect f;
  67. f.size.width = w.frame.size.width + widthAdjust;
  68. f.size.height = w.frame.size.height;
  69. [w setFrame:f display:YES];
  70. }
  71. [w center];
  72. [progressBar setIndeterminate:NO];
  73. [progressBar setDoubleValue:0.0];
  74. [[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self
  75. selector:@selector(updateProgressUI:)
  76. userInfo:nil repeats:YES] retain];
  77. // Make sure we are on top initially
  78. [NSApp activateIgnoringOtherApps:YES];
  79. }
  80. // called when the timer goes off
  81. -(void)updateProgressUI:(NSTimer *)aTimer
  82. {
  83. if (sQuit) {
  84. [aTimer invalidate];
  85. [aTimer release];
  86. // It seems to be necessary to activate and hide ourselves before we stop,
  87. // otherwise the "run" method will not return until the user focuses some
  88. // other app. The activate step is necessary if we are not the active app.
  89. // This is a big hack, but it seems to do the trick.
  90. [NSApp activateIgnoringOtherApps:YES];
  91. [NSApp hide:self];
  92. [NSApp stop:self];
  93. }
  94. float progress = sProgressVal;
  95. [progressBar setDoubleValue:(double)progress];
  96. }
  97. // leave this as returning a BOOL instead of NSApplicationTerminateReply
  98. // for backward compatibility
  99. - (BOOL)applicationShouldTerminate:(NSApplication *)sender
  100. {
  101. return sQuit;
  102. }
  103. @end
  104. int
  105. InitProgressUI(int *pargc, char ***pargv)
  106. {
  107. sUpdatePath = (*pargv)[1];
  108. return 0;
  109. }
  110. int
  111. ShowProgressUI()
  112. {
  113. // Only show the Progress UI if the process is taking a significant amount of
  114. // time where a significant amount of time is defined as .5 seconds after
  115. // ShowProgressUI is called sProgress is less than 70.
  116. usleep(500000);
  117. if (sQuit || sProgressVal > 70.0f)
  118. return 0;
  119. char path[PATH_MAX];
  120. snprintf(path, sizeof(path), "%s/updater.ini", sUpdatePath);
  121. if (ReadStrings(path, &sLabels) != OK)
  122. return -1;
  123. // Continue the update without showing the Progress UI if any of the supplied
  124. // strings are larger than MAX_TEXT_LEN (Bug 628829).
  125. if (!(strlen(sLabels.title) < MAX_TEXT_LEN - 1 &&
  126. strlen(sLabels.info) < MAX_TEXT_LEN - 1))
  127. return -1;
  128. [NSApplication sharedApplication];
  129. [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
  130. [NSApp run];
  131. return 0;
  132. }
  133. // Called on a background thread
  134. void
  135. QuitProgressUI()
  136. {
  137. sQuit = TRUE;
  138. }
  139. // Called on a background thread
  140. void
  141. UpdateProgressUI(float progress)
  142. {
  143. sProgressVal = progress; // 32-bit writes are atomic
  144. }