The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

832 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_Application.h"
  19. #include "jucer_AutoUpdater.h"
  20. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple()
  21. : major ((ProjectInfo::versionNumber & 0xff0000) >> 16),
  22. minor ((ProjectInfo::versionNumber & 0x00ff00) >> 8),
  23. build ((ProjectInfo::versionNumber & 0x0000ff) >> 0)
  24. {}
  25. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple (int juceVersionNumber)
  26. : major ((juceVersionNumber & 0xff0000) >> 16),
  27. minor ((juceVersionNumber & 0x00ff00) >> 8),
  28. build ((juceVersionNumber & 0x0000ff) >> 0)
  29. {}
  30. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple (int majorInt, int minorInt, int buildNumber)
  31. : major (majorInt),
  32. minor (minorInt),
  33. build (buildNumber)
  34. {}
  35. bool LatestVersionChecker::JuceVersionTriple::fromString (const String& versionString,
  36. LatestVersionChecker::JuceVersionTriple& result)
  37. {
  38. StringArray tokenizedString = StringArray::fromTokens (versionString, ".", StringRef());
  39. if (tokenizedString.size() != 3)
  40. return false;
  41. result.major = tokenizedString [0].getIntValue();
  42. result.minor = tokenizedString [1].getIntValue();
  43. result.build = tokenizedString [2].getIntValue();
  44. return true;
  45. }
  46. String LatestVersionChecker::JuceVersionTriple::toString() const
  47. {
  48. String retval;
  49. retval << major << '.' << minor << '.' << build;
  50. return retval;
  51. }
  52. bool LatestVersionChecker::JuceVersionTriple::operator> (const LatestVersionChecker::JuceVersionTriple& b) const noexcept
  53. {
  54. if (major == b.major)
  55. {
  56. if (minor == b.minor)
  57. return build > b.build;
  58. return minor > b.minor;
  59. }
  60. return major > b.major;
  61. }
  62. //==============================================================================
  63. struct RelaunchTimer : private Timer
  64. {
  65. RelaunchTimer (const File& f) : parentFolder (f)
  66. {
  67. startTimer (1500);
  68. }
  69. void timerCallback() override
  70. {
  71. stopTimer();
  72. File app = parentFolder.getChildFile (
  73. #if JUCE_MAC
  74. "Projucer.app");
  75. #elif JUCE_WINDOWS
  76. "Projucer.exe");
  77. #elif JUCE_LINUX
  78. "Projucer");
  79. #endif
  80. JUCEApplication::quit();
  81. if (app.exists())
  82. {
  83. app.setExecutePermission (true);
  84. #if JUCE_MAC
  85. app.getChildFile ("Contents")
  86. .getChildFile ("MacOS")
  87. .getChildFile ("Projucer").setExecutePermission (true);
  88. #endif
  89. app.startAsProcess();
  90. }
  91. delete this;
  92. }
  93. File parentFolder;
  94. };
  95. //==============================================================================
  96. class DownloadNewVersionThread : public ThreadWithProgressWindow
  97. {
  98. public:
  99. DownloadNewVersionThread (LatestVersionChecker& versionChecker,URL u,
  100. const String& extraHeaders, File target)
  101. : ThreadWithProgressWindow ("Downloading New Version", true, true),
  102. owner (versionChecker),
  103. result (Result::ok()),
  104. url (u), headers (extraHeaders), targetFolder (target)
  105. {
  106. }
  107. static void performDownload (LatestVersionChecker& versionChecker, URL u,
  108. const String& extraHeaders, File targetFolder)
  109. {
  110. DownloadNewVersionThread d (versionChecker, u, extraHeaders, targetFolder);
  111. if (d.runThread())
  112. {
  113. if (d.result.failed())
  114. {
  115. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  116. "Installation Failed",
  117. d.result.getErrorMessage());
  118. }
  119. else
  120. {
  121. new RelaunchTimer (targetFolder);
  122. }
  123. }
  124. }
  125. void run() override
  126. {
  127. setProgress (-1.0);
  128. MemoryBlock zipData;
  129. result = download (zipData);
  130. if (result.wasOk() && ! threadShouldExit())
  131. {
  132. setStatusMessage ("Installing...");
  133. result = owner.performUpdate (zipData, targetFolder);
  134. }
  135. }
  136. Result download (MemoryBlock& dest)
  137. {
  138. setStatusMessage ("Downloading...");
  139. int statusCode = 302;
  140. const int maxRedirects = 5;
  141. // we need to do the redirecting manually due to inconsistencies on the way headers are handled on redirects
  142. ScopedPointer<InputStream> in;
  143. for (int redirect = 0; redirect < maxRedirects; ++redirect)
  144. {
  145. StringPairArray responseHeaders;
  146. in = url.createInputStream (false, nullptr, nullptr, headers, 10000, &responseHeaders, &statusCode, 0);
  147. if (in == nullptr || statusCode != 302)
  148. break;
  149. String redirectPath = responseHeaders ["Location"];
  150. if (redirectPath.isEmpty())
  151. break;
  152. url = owner.getLatestVersionURL (headers, redirectPath);
  153. }
  154. if (in != nullptr && statusCode == 200)
  155. {
  156. int64 total = 0;
  157. MemoryOutputStream mo (dest, true);
  158. for (;;)
  159. {
  160. if (threadShouldExit())
  161. return Result::fail ("cancel");
  162. int64 written = mo.writeFromInputStream (*in, 8192);
  163. if (written == 0)
  164. break;
  165. total += written;
  166. setStatusMessage (String (TRANS ("Downloading... (123)"))
  167. .replace ("123", File::descriptionOfSizeInBytes (total)));
  168. }
  169. return Result::ok();
  170. }
  171. return Result::fail ("Failed to download from: " + url.toString (false));
  172. }
  173. LatestVersionChecker& owner;
  174. Result result;
  175. URL url;
  176. String headers;
  177. File targetFolder;
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DownloadNewVersionThread)
  179. };
  180. //==============================================================================
  181. class UpdateUserDialog : public Component,
  182. public ButtonListener
  183. {
  184. public:
  185. UpdateUserDialog (const LatestVersionChecker::JuceVersionTriple& version,
  186. const String& productName,
  187. const String& releaseNotes,
  188. const char* overwriteFolderPath)
  189. : hasOverwriteButton (overwriteFolderPath != nullptr)
  190. {
  191. addAndMakeVisible (titleLabel = new Label ("Title Label",
  192. TRANS ("Download \"123\" version 456?").replace ("123", productName)
  193. .replace ("456", version.toString())));
  194. titleLabel->setFont (Font (15.00f, Font::bold));
  195. titleLabel->setJustificationType (Justification::centredLeft);
  196. titleLabel->setEditable (false, false, false);
  197. titleLabel->setColour (TextEditor::textColourId, Colours::black);
  198. titleLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  199. addAndMakeVisible (contentLabel = new Label ("Content Label",
  200. TRANS ("A new version of \"123\" is available - would you like to download it?")
  201. .replace ("123", productName)));
  202. contentLabel->setFont (Font (15.00f, Font::plain));
  203. contentLabel->setJustificationType (Justification::topLeft);
  204. contentLabel->setEditable (false, false, false);
  205. contentLabel->setColour (TextEditor::textColourId, Colours::black);
  206. contentLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  207. addAndMakeVisible (okButton = new TextButton ("OK Button"));
  208. okButton->setButtonText (TRANS(hasOverwriteButton ? "Choose Another Folder..." : "OK"));
  209. okButton->addListener (this);
  210. addAndMakeVisible (cancelButton = new TextButton ("Cancel Button"));
  211. cancelButton->setButtonText (TRANS("Cancel"));
  212. cancelButton->addListener (this);
  213. addAndMakeVisible (changeLogLabel = new Label ("Change Log Label",
  214. TRANS("Release Notes:")));
  215. changeLogLabel->setFont (Font (15.00f, Font::plain));
  216. changeLogLabel->setJustificationType (Justification::topLeft);
  217. changeLogLabel->setEditable (false, false, false);
  218. changeLogLabel->setColour (TextEditor::textColourId, Colours::black);
  219. changeLogLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  220. addAndMakeVisible (changeLog = new TextEditor ("Change Log"));
  221. changeLog->setMultiLine (true);
  222. changeLog->setReturnKeyStartsNewLine (true);
  223. changeLog->setReadOnly (true);
  224. changeLog->setScrollbarsShown (true);
  225. changeLog->setCaretVisible (false);
  226. changeLog->setPopupMenuEnabled (false);
  227. changeLog->setText (releaseNotes);
  228. if (hasOverwriteButton)
  229. {
  230. addAndMakeVisible (overwriteLabel = new Label ("Overwrite Label",
  231. TRANS("Updating will overwrite everything in the following folder:")));
  232. overwriteLabel->setFont (Font (15.00f, Font::plain));
  233. overwriteLabel->setJustificationType (Justification::topLeft);
  234. overwriteLabel->setEditable (false, false, false);
  235. overwriteLabel->setColour (TextEditor::textColourId, Colours::black);
  236. overwriteLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  237. addAndMakeVisible (overwritePath = new Label ("Overwrite Path", overwriteFolderPath));
  238. overwritePath->setFont (Font (15.00f, Font::bold));
  239. overwritePath->setJustificationType (Justification::topLeft);
  240. overwritePath->setEditable (false, false, false);
  241. overwritePath->setColour (TextEditor::textColourId, Colours::black);
  242. overwritePath->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  243. addAndMakeVisible (overwriteButton = new TextButton ("Overwrite Button"));
  244. overwriteButton->setButtonText (TRANS("Overwrite"));
  245. overwriteButton->addListener (this);
  246. }
  247. juceIcon = Drawable::createFromImageData (BinaryData::juce_icon_png,
  248. BinaryData::juce_icon_pngSize);
  249. setSize (518, overwritePath ? 345 : 269);
  250. }
  251. ~UpdateUserDialog()
  252. {
  253. titleLabel = nullptr;
  254. contentLabel = nullptr;
  255. okButton = nullptr;
  256. cancelButton = nullptr;
  257. changeLogLabel = nullptr;
  258. changeLog = nullptr;
  259. overwriteLabel = nullptr;
  260. overwritePath = nullptr;
  261. overwriteButton = nullptr;
  262. juceIcon = nullptr;
  263. }
  264. void paint (Graphics& g) override
  265. {
  266. g.fillAll (Colours::lightgrey);
  267. g.setColour (Colours::black);
  268. if (juceIcon != nullptr)
  269. juceIcon->drawWithin (g, Rectangle<float> (20, 17, 64, 64),
  270. RectanglePlacement::stretchToFit, 1.000f);
  271. }
  272. void resized() override
  273. {
  274. titleLabel->setBounds (88, 10, 397, 24);
  275. contentLabel->setBounds (88, 40, 397, 51);
  276. changeLogLabel->setBounds (22, 92, 341, 24);
  277. changeLog->setBounds (24, 112, 476, 102);
  278. if (hasOverwriteButton)
  279. {
  280. okButton->setBounds (getWidth() - 24 - 174, getHeight() - 37, 174, 28);
  281. overwriteButton->setBounds ((getWidth() - 24 - 174) + -14 - 86, getHeight() - 37, 86, 28);
  282. cancelButton->setBounds (24, getHeight() - 37, 70, 28);
  283. overwriteLabel->setBounds (24, 238, 472, 16);
  284. overwritePath->setBounds (24, 262, 472, 40);
  285. }
  286. else
  287. {
  288. okButton->setBounds (getWidth() - 24 - 47, getHeight() - 37, 47, 28);
  289. cancelButton->setBounds ((getWidth() - 24 - 47) + -14 - 70, getHeight() - 37, 70, 28);
  290. }
  291. }
  292. void buttonClicked (Button* clickedButton) override
  293. {
  294. if (DialogWindow* parentDialog = findParentComponentOfClass<DialogWindow>())
  295. {
  296. if (clickedButton == overwriteButton) parentDialog->exitModalState (1);
  297. else if (clickedButton == okButton) parentDialog->exitModalState (2);
  298. else if (clickedButton == cancelButton) parentDialog->exitModalState (-1);
  299. }
  300. else
  301. jassertfalse;
  302. }
  303. static DialogWindow* launch (const LatestVersionChecker::JuceVersionTriple& version,
  304. const String& productName,
  305. const String& releaseNotes,
  306. const char* overwritePath = nullptr)
  307. {
  308. OptionalScopedPointer<Component> userDialog (new UpdateUserDialog (version, productName,
  309. releaseNotes, overwritePath), true);
  310. DialogWindow::LaunchOptions lo;
  311. lo.dialogTitle = TRANS ("Download \"123\" version 456?").replace ("456", version.toString())
  312. .replace ("123", productName);
  313. lo.dialogBackgroundColour = Colours::lightgrey;
  314. lo.content = userDialog;
  315. lo.componentToCentreAround = nullptr;
  316. lo.escapeKeyTriggersCloseButton = true;
  317. lo.useNativeTitleBar = true;
  318. lo.resizable = false;
  319. lo.useBottomRightCornerResizer = false;
  320. return lo.launchAsync();
  321. }
  322. private:
  323. bool hasOverwriteButton;
  324. ScopedPointer<Label> titleLabel, contentLabel, changeLogLabel, overwriteLabel, overwritePath;
  325. ScopedPointer<TextButton> okButton, cancelButton;
  326. ScopedPointer<TextEditor> changeLog;
  327. ScopedPointer<TextButton> overwriteButton;
  328. ScopedPointer<Drawable> juceIcon;
  329. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UpdateUserDialog)
  330. };
  331. //==============================================================================
  332. class UpdaterDialogModalCallback : public ModalComponentManager::Callback
  333. {
  334. public:
  335. struct DelayedCallback : private Timer
  336. {
  337. DelayedCallback (LatestVersionChecker& versionChecker,
  338. URL& newVersionToDownload,
  339. const String& extraHeaders,
  340. const File& appParentFolder,
  341. int returnValue)
  342. : parent (versionChecker), download (newVersionToDownload),
  343. headers (extraHeaders), folder (appParentFolder), result (returnValue)
  344. {
  345. startTimer (200);
  346. }
  347. private:
  348. void timerCallback() override
  349. {
  350. stopTimer();
  351. parent.modalStateFinished (result, download, headers, folder);
  352. delete this;
  353. }
  354. LatestVersionChecker& parent;
  355. URL download;
  356. String headers;
  357. File folder;
  358. int result;
  359. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DelayedCallback)
  360. };
  361. UpdaterDialogModalCallback (LatestVersionChecker& versionChecker,
  362. URL& newVersionToDownload,
  363. const String& extraHeaders,
  364. const File& appParentFolder)
  365. : parent (versionChecker), download (newVersionToDownload),
  366. headers (extraHeaders), folder (appParentFolder)
  367. {}
  368. void modalStateFinished (int returnValue) override
  369. {
  370. // the dialog window is only closed after this function exits
  371. // so we need a deferred callback to the parent. Unfortunately
  372. // our instance is also deleted after this function is used
  373. // so we can't use our own instance for a timer callback
  374. // we must allocate a new one.
  375. new DelayedCallback (parent, download, headers, folder, returnValue);
  376. }
  377. private:
  378. LatestVersionChecker& parent;
  379. URL download;
  380. String headers;
  381. File folder;
  382. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UpdaterDialogModalCallback)
  383. };
  384. //==============================================================================
  385. LatestVersionChecker::LatestVersionChecker() : Thread ("Updater"),
  386. statusCode (-1),
  387. hasAttemptedToReadWebsite (false)
  388. {
  389. startTimer (2000);
  390. }
  391. LatestVersionChecker::~LatestVersionChecker()
  392. {
  393. stopThread (20000);
  394. }
  395. String LatestVersionChecker::getOSString()
  396. {
  397. SystemStats::OperatingSystemType osType = SystemStats::getOperatingSystemType();
  398. if ((osType & SystemStats::MacOSX) != 0) return "OSX";
  399. else if ((osType & SystemStats::Windows) != 0) return "Windows";
  400. else if ((osType & SystemStats::Linux) != 0) return "Linux";
  401. else return SystemStats::getOperatingSystemName();
  402. }
  403. const LatestVersionChecker::JuceServerLocationsAndKeys& LatestVersionChecker::getJuceServerURLsAndKeys() const
  404. {
  405. static LatestVersionChecker::JuceServerLocationsAndKeys urlsAndKeys =
  406. {
  407. "https://my.roli.com",
  408. "265441b-343403c-20f6932-76361d",
  409. 1,
  410. "/software_versions/update_to/Projucer/"
  411. };
  412. return urlsAndKeys;
  413. }
  414. int LatestVersionChecker::getProductVersionNumber() const { return ProjectInfo::versionNumber; }
  415. const char* LatestVersionChecker::getProductName() const { return ProjectInfo::projectName; }
  416. bool LatestVersionChecker::allowCustomLocation() const { return true; }
  417. Result LatestVersionChecker::performUpdate (const MemoryBlock& data, File& targetFolder)
  418. {
  419. File unzipTarget;
  420. bool isUsingTempFolder = false;
  421. {
  422. MemoryInputStream input (data, false);
  423. ZipFile zip (input);
  424. if (zip.getNumEntries() == 0)
  425. return Result::fail ("The downloaded file wasn't a valid JUCE file!");
  426. unzipTarget = targetFolder;
  427. if (unzipTarget.exists())
  428. {
  429. isUsingTempFolder = true;
  430. unzipTarget = targetFolder.getNonexistentSibling();
  431. if (! unzipTarget.createDirectory())
  432. return Result::fail ("Couldn't create a folder to unzip the new version!");
  433. }
  434. Result r (zip.uncompressTo (unzipTarget));
  435. if (r.failed())
  436. {
  437. if (isUsingTempFolder)
  438. unzipTarget.deleteRecursively();
  439. return r;
  440. }
  441. }
  442. if (isUsingTempFolder)
  443. {
  444. File oldFolder (targetFolder.getSiblingFile (targetFolder.getFileNameWithoutExtension() + "_old")
  445. .getNonexistentSibling());
  446. if (! targetFolder.moveFileTo (oldFolder))
  447. {
  448. unzipTarget.deleteRecursively();
  449. return Result::fail ("Could not remove the existing folder!");
  450. }
  451. if (! unzipTarget.moveFileTo (targetFolder))
  452. {
  453. unzipTarget.deleteRecursively();
  454. return Result::fail ("Could not overwrite the existing folder!");
  455. }
  456. }
  457. return Result::ok();
  458. }
  459. URL LatestVersionChecker::getLatestVersionURL (String& headers, const String& path) const
  460. {
  461. const LatestVersionChecker::JuceServerLocationsAndKeys& urlsAndKeys = getJuceServerURLsAndKeys();
  462. String updateURL;
  463. bool isAbsolute = (path.startsWith ("http://") || path.startsWith ("https://"));
  464. bool isRedirect = path.isNotEmpty();
  465. if (isAbsolute)
  466. {
  467. updateURL = path;
  468. }
  469. else
  470. {
  471. updateURL << urlsAndKeys.updateSeverHostname
  472. << (isRedirect ? path : String (urlsAndKeys.updatePath));
  473. if (! isRedirect)
  474. {
  475. updateURL << JuceVersionTriple (getProductVersionNumber()).toString() << '/'
  476. << getOSString() << "?language=" << SystemStats::getUserLanguage();
  477. }
  478. }
  479. headers.clear();
  480. if (! isAbsolute)
  481. {
  482. headers << "X-API-Key: " << urlsAndKeys.publicAPIKey;
  483. if (! isRedirect)
  484. {
  485. headers << "\nContent-Type: application/json\n"
  486. << "Accept: application/json; version=" << urlsAndKeys.apiVersion;
  487. }
  488. }
  489. return URL (updateURL);
  490. }
  491. URL LatestVersionChecker::getLatestVersionURL (String& headers) const
  492. {
  493. String emptyString;
  494. return getLatestVersionURL (headers, emptyString);
  495. }
  496. void LatestVersionChecker::checkForNewVersion()
  497. {
  498. hasAttemptedToReadWebsite = true;
  499. {
  500. String extraHeaders;
  501. URL updateURL (getLatestVersionURL (extraHeaders));
  502. StringPairArray responseHeaders;
  503. const int numRedirects = 0;
  504. const ScopedPointer<InputStream> in (updateURL.createInputStream (false, nullptr, nullptr,
  505. extraHeaders, 0, &responseHeaders,
  506. &statusCode, numRedirects));
  507. if (threadShouldExit())
  508. return; // can't connect: fail silently.
  509. if (in != nullptr && (statusCode == 303 || statusCode == 400))
  510. {
  511. // if this doesn't fail then there is a new version available.
  512. // By leaving the scope of this function we will abort the download
  513. // to give the user a chance to cancel an update
  514. if (statusCode == 303)
  515. newRelativeDownloadPath = responseHeaders ["Location"];
  516. jsonReply = JSON::parse (in->readEntireStreamAsString());
  517. }
  518. }
  519. if (! threadShouldExit())
  520. startTimer (100);
  521. }
  522. bool LatestVersionChecker::processResult (var reply, const String& downloadPath)
  523. {
  524. if (statusCode == 303)
  525. {
  526. String versionString = reply.getProperty ("version", var()).toString();
  527. String releaseNotes = reply.getProperty ("notes", var()).toString();
  528. JuceVersionTriple version;
  529. if (versionString.isNotEmpty() && releaseNotes.isNotEmpty())
  530. {
  531. if (JuceVersionTriple::fromString (versionString, version))
  532. {
  533. String extraHeaders;
  534. URL newVersionToDownload = getLatestVersionURL (extraHeaders, downloadPath);
  535. return askUserAboutNewVersion (version, releaseNotes, newVersionToDownload, extraHeaders);
  536. }
  537. }
  538. }
  539. else if (statusCode == 400)
  540. {
  541. // In the far-distant future, this may be contacting a defunct
  542. // URL, so hopefully the website will contain a helpful message
  543. // for the user..
  544. var errorObj = reply.getDynamicObject()->getProperty ("error");
  545. if (errorObj.isObject())
  546. {
  547. String message = errorObj.getProperty ("message", var()).toString();
  548. if (message.isNotEmpty())
  549. {
  550. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  551. TRANS("JUCE Updater"),
  552. message);
  553. return false;
  554. }
  555. }
  556. }
  557. // try again
  558. return true;
  559. }
  560. bool LatestVersionChecker::askUserAboutNewVersion (const LatestVersionChecker::JuceVersionTriple& version,
  561. const String& releaseNotes,
  562. URL& newVersionToDownload,
  563. const String& extraHeaders)
  564. {
  565. JuceVersionTriple currentVersion (getProductVersionNumber());
  566. if (version > currentVersion)
  567. {
  568. File appParentFolder (File::getSpecialLocation (File::currentApplicationFile).getParentDirectory());
  569. DialogWindow* modalDialog = nullptr;
  570. if (isZipFolder (appParentFolder) && allowCustomLocation())
  571. {
  572. modalDialog = UpdateUserDialog::launch (version, getProductName(), releaseNotes,
  573. appParentFolder.getFullPathName().toRawUTF8());
  574. }
  575. else
  576. {
  577. modalDialog = UpdateUserDialog::launch (version, getProductName(), releaseNotes);
  578. }
  579. if (modalDialog != nullptr)
  580. {
  581. UpdaterDialogModalCallback* callback = new UpdaterDialogModalCallback (*this,
  582. newVersionToDownload,
  583. extraHeaders,
  584. appParentFolder);
  585. // attachCallback will delete callback
  586. if (ModalComponentManager* mm = ModalComponentManager::getInstance())
  587. mm->attachCallback (modalDialog, callback);
  588. }
  589. return false;
  590. }
  591. return true;
  592. }
  593. void LatestVersionChecker::modalStateFinished (int result,
  594. URL& newVersionToDownload,
  595. const String& extraHeaders,
  596. File appParentFolder)
  597. {
  598. if (result == 1 || result == 2)
  599. {
  600. if (result == 1 || ! allowCustomLocation())
  601. DownloadNewVersionThread::performDownload (*this, newVersionToDownload, extraHeaders, appParentFolder);
  602. else
  603. askUserForLocationToDownload (newVersionToDownload, extraHeaders);
  604. }
  605. }
  606. void LatestVersionChecker::askUserForLocationToDownload (URL& newVersionToDownload, const String& extraHeaders)
  607. {
  608. File targetFolder (findDefaultModulesFolder());
  609. if (isJuceModulesFolder (targetFolder))
  610. targetFolder = targetFolder.getParentDirectory();
  611. FileChooser chooser (TRANS("Please select the location into which you'd like to install the new version"),
  612. targetFolder);
  613. if (chooser.browseForDirectory())
  614. {
  615. targetFolder = chooser.getResult();
  616. if (isJuceModulesFolder (targetFolder))
  617. targetFolder = targetFolder.getParentDirectory();
  618. if (targetFolder.getChildFile ("JUCE").isDirectory())
  619. targetFolder = targetFolder.getChildFile ("JUCE");
  620. if (targetFolder.getChildFile (".git").isDirectory())
  621. {
  622. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  623. TRANS ("Downloading new JUCE version"),
  624. TRANS ("This folder is a GIT repository!\n\n"
  625. "You should use a \"git pull\" to update it to the latest version. "
  626. "Or to use the Projucer to get an update, you should select an empty "
  627. "folder into which you'd like to download the new code."));
  628. return;
  629. }
  630. if (isJuceFolder (targetFolder))
  631. {
  632. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  633. TRANS("Overwrite existing JUCE folder?"),
  634. TRANS("Do you want to overwrite the folder:\n\n"
  635. "xfldrx\n\n"
  636. " ..with the latest version from juce.com?\n\n"
  637. "(Please note that this will overwrite everything in that folder!)")
  638. .replace ("xfldrx", targetFolder.getFullPathName())))
  639. {
  640. return;
  641. }
  642. }
  643. else
  644. {
  645. targetFolder = targetFolder.getChildFile ("JUCE").getNonexistentSibling();
  646. }
  647. DownloadNewVersionThread::performDownload (*this, newVersionToDownload, extraHeaders, targetFolder);
  648. }
  649. }
  650. bool LatestVersionChecker::isZipFolder (const File& f)
  651. {
  652. return f.getChildFile ("modules").isDirectory()
  653. && f.getChildFile ("extras").isDirectory()
  654. && f.getChildFile ("examples").isDirectory()
  655. && ! f.getChildFile (".git").isDirectory();
  656. }
  657. void LatestVersionChecker::timerCallback()
  658. {
  659. stopTimer();
  660. if (hasAttemptedToReadWebsite)
  661. {
  662. bool restartTimer = true;
  663. if (jsonReply.isObject())
  664. restartTimer = processResult (jsonReply, newRelativeDownloadPath);
  665. hasAttemptedToReadWebsite = false;
  666. if (restartTimer)
  667. startTimer (7200000);
  668. }
  669. else
  670. {
  671. startThread (3);
  672. }
  673. }
  674. void LatestVersionChecker::run()
  675. {
  676. checkForNewVersion();
  677. }