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.

835 lines
29KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_Application.h"
  21. #include "jucer_AutoUpdater.h"
  22. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple()
  23. : major ((ProjectInfo::versionNumber & 0xff0000) >> 16),
  24. minor ((ProjectInfo::versionNumber & 0x00ff00) >> 8),
  25. build ((ProjectInfo::versionNumber & 0x0000ff) >> 0)
  26. {}
  27. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple (int juceVersionNumber)
  28. : major ((juceVersionNumber & 0xff0000) >> 16),
  29. minor ((juceVersionNumber & 0x00ff00) >> 8),
  30. build ((juceVersionNumber & 0x0000ff) >> 0)
  31. {}
  32. LatestVersionChecker::JuceVersionTriple::JuceVersionTriple (int majorInt, int minorInt, int buildNumber)
  33. : major (majorInt),
  34. minor (minorInt),
  35. build (buildNumber)
  36. {}
  37. bool LatestVersionChecker::JuceVersionTriple::fromString (const String& versionString,
  38. LatestVersionChecker::JuceVersionTriple& result)
  39. {
  40. StringArray tokenizedString = StringArray::fromTokens (versionString, ".", StringRef());
  41. if (tokenizedString.size() != 3)
  42. return false;
  43. result.major = tokenizedString [0].getIntValue();
  44. result.minor = tokenizedString [1].getIntValue();
  45. result.build = tokenizedString [2].getIntValue();
  46. return true;
  47. }
  48. String LatestVersionChecker::JuceVersionTriple::toString() const
  49. {
  50. String retval;
  51. retval << major << '.' << minor << '.' << build;
  52. return retval;
  53. }
  54. bool LatestVersionChecker::JuceVersionTriple::operator> (const LatestVersionChecker::JuceVersionTriple& b) const noexcept
  55. {
  56. if (major == b.major)
  57. {
  58. if (minor == b.minor)
  59. return build > b.build;
  60. return minor > b.minor;
  61. }
  62. return major > b.major;
  63. }
  64. //==============================================================================
  65. struct RelaunchTimer : private Timer
  66. {
  67. RelaunchTimer (const File& f) : parentFolder (f)
  68. {
  69. startTimer (1500);
  70. }
  71. void timerCallback() override
  72. {
  73. stopTimer();
  74. File app = parentFolder.getChildFile (
  75. #if JUCE_MAC
  76. "Projucer.app");
  77. #elif JUCE_WINDOWS
  78. "Projucer.exe");
  79. #elif JUCE_LINUX
  80. "Projucer");
  81. #endif
  82. JUCEApplication::quit();
  83. if (app.exists())
  84. {
  85. app.setExecutePermission (true);
  86. #if JUCE_MAC
  87. app.getChildFile ("Contents")
  88. .getChildFile ("MacOS")
  89. .getChildFile ("Projucer").setExecutePermission (true);
  90. #endif
  91. app.startAsProcess();
  92. }
  93. delete this;
  94. }
  95. File parentFolder;
  96. };
  97. //==============================================================================
  98. class DownloadNewVersionThread : public ThreadWithProgressWindow
  99. {
  100. public:
  101. DownloadNewVersionThread (LatestVersionChecker& versionChecker,URL u,
  102. const String& extraHeaders, File target)
  103. : ThreadWithProgressWindow ("Downloading New Version", true, true),
  104. owner (versionChecker),
  105. result (Result::ok()),
  106. url (u), headers (extraHeaders), targetFolder (target)
  107. {
  108. }
  109. static void performDownload (LatestVersionChecker& versionChecker, URL u,
  110. const String& extraHeaders, File targetFolder)
  111. {
  112. DownloadNewVersionThread d (versionChecker, u, extraHeaders, targetFolder);
  113. if (d.runThread())
  114. {
  115. if (d.result.failed())
  116. {
  117. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  118. "Installation Failed",
  119. d.result.getErrorMessage());
  120. }
  121. else
  122. {
  123. new RelaunchTimer (targetFolder);
  124. }
  125. }
  126. }
  127. void run() override
  128. {
  129. setProgress (-1.0);
  130. MemoryBlock zipData;
  131. result = download (zipData);
  132. if (result.wasOk() && ! threadShouldExit())
  133. {
  134. setStatusMessage ("Installing...");
  135. result = owner.performUpdate (zipData, targetFolder);
  136. }
  137. }
  138. Result download (MemoryBlock& dest)
  139. {
  140. setStatusMessage ("Downloading...");
  141. int statusCode = 302;
  142. const int maxRedirects = 5;
  143. // we need to do the redirecting manually due to inconsistencies on the way headers are handled on redirects
  144. ScopedPointer<InputStream> in;
  145. for (int redirect = 0; redirect < maxRedirects; ++redirect)
  146. {
  147. StringPairArray responseHeaders;
  148. in.reset (url.createInputStream (false, nullptr, nullptr, headers,
  149. 10000, &responseHeaders, &statusCode, 0));
  150. if (in == nullptr || statusCode != 302)
  151. break;
  152. String redirectPath = responseHeaders ["Location"];
  153. if (redirectPath.isEmpty())
  154. break;
  155. url = owner.getLatestVersionURL (headers, redirectPath);
  156. }
  157. if (in != nullptr && statusCode == 200)
  158. {
  159. int64 total = 0;
  160. MemoryOutputStream mo (dest, true);
  161. for (;;)
  162. {
  163. if (threadShouldExit())
  164. return Result::fail ("cancel");
  165. int64 written = mo.writeFromInputStream (*in, 8192);
  166. if (written == 0)
  167. break;
  168. total += written;
  169. setStatusMessage (String (TRANS ("Downloading... (123)"))
  170. .replace ("123", File::descriptionOfSizeInBytes (total)));
  171. }
  172. return Result::ok();
  173. }
  174. return Result::fail ("Failed to download from: " + url.toString (false));
  175. }
  176. LatestVersionChecker& owner;
  177. Result result;
  178. URL url;
  179. String headers;
  180. File targetFolder;
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DownloadNewVersionThread)
  182. };
  183. //==============================================================================
  184. class UpdateUserDialog : public Component,
  185. public Button::Listener
  186. {
  187. public:
  188. UpdateUserDialog (const LatestVersionChecker::JuceVersionTriple& version,
  189. const String& productName,
  190. const String& releaseNotes,
  191. const char* overwriteFolderPath)
  192. : hasOverwriteButton (overwriteFolderPath != nullptr)
  193. {
  194. addAndMakeVisible (titleLabel = new Label ("Title Label",
  195. TRANS ("Download \"123\" version 456?").replace ("123", productName)
  196. .replace ("456", version.toString())));
  197. titleLabel->setFont (Font (15.00f, Font::bold));
  198. titleLabel->setJustificationType (Justification::centredLeft);
  199. titleLabel->setEditable (false, false, false);
  200. addAndMakeVisible (contentLabel = new Label ("Content Label",
  201. TRANS ("A new version of \"123\" is available - would you like to download it?")
  202. .replace ("123", productName)));
  203. contentLabel->setFont (Font (15.00f, Font::plain));
  204. contentLabel->setJustificationType (Justification::topLeft);
  205. contentLabel->setEditable (false, false, false);
  206. addAndMakeVisible (okButton = new TextButton ("OK Button"));
  207. okButton->setButtonText (TRANS(hasOverwriteButton ? "Choose Another Folder..." : "OK"));
  208. okButton->addListener (this);
  209. addAndMakeVisible (cancelButton = new TextButton ("Cancel Button"));
  210. cancelButton->setButtonText (TRANS("Cancel"));
  211. cancelButton->addListener (this);
  212. addAndMakeVisible (changeLogLabel = new Label ("Change Log Label",
  213. TRANS("Release Notes:")));
  214. changeLogLabel->setFont (Font (15.00f, Font::plain));
  215. changeLogLabel->setJustificationType (Justification::topLeft);
  216. changeLogLabel->setEditable (false, false, false);
  217. addAndMakeVisible (changeLog = new TextEditor ("Change Log"));
  218. changeLog->setMultiLine (true);
  219. changeLog->setReturnKeyStartsNewLine (true);
  220. changeLog->setReadOnly (true);
  221. changeLog->setScrollbarsShown (true);
  222. changeLog->setCaretVisible (false);
  223. changeLog->setPopupMenuEnabled (false);
  224. changeLog->setText (releaseNotes);
  225. if (hasOverwriteButton)
  226. {
  227. addAndMakeVisible (overwriteLabel = new Label ("Overwrite Label",
  228. TRANS("Updating will overwrite everything in the following folder:")));
  229. overwriteLabel->setFont (Font (15.00f, Font::plain));
  230. overwriteLabel->setJustificationType (Justification::topLeft);
  231. overwriteLabel->setEditable (false, false, false);
  232. addAndMakeVisible (overwritePath = new Label ("Overwrite Path", overwriteFolderPath));
  233. overwritePath->setFont (Font (15.00f, Font::bold));
  234. overwritePath->setJustificationType (Justification::topLeft);
  235. overwritePath->setEditable (false, false, false);
  236. addAndMakeVisible (overwriteButton = new TextButton ("Overwrite Button"));
  237. overwriteButton->setButtonText (TRANS("Overwrite"));
  238. overwriteButton->addListener (this);
  239. }
  240. juceIcon = Drawable::createFromImageData (BinaryData::juce_icon_png,
  241. BinaryData::juce_icon_pngSize);
  242. setSize (518, overwritePath != nullptr ? 345 : 269);
  243. lookAndFeelChanged();
  244. }
  245. ~UpdateUserDialog()
  246. {
  247. titleLabel.reset();
  248. contentLabel.reset();
  249. okButton.reset();
  250. cancelButton.reset();
  251. changeLogLabel.reset();
  252. changeLog.reset();
  253. overwriteLabel.reset();
  254. overwritePath.reset();
  255. overwriteButton.reset();
  256. juceIcon.reset();
  257. }
  258. void paint (Graphics& g) override
  259. {
  260. g.fillAll (findColour (backgroundColourId));
  261. g.setColour (findColour (defaultTextColourId));
  262. if (juceIcon != nullptr)
  263. juceIcon->drawWithin (g, Rectangle<float> (20, 17, 64, 64),
  264. RectanglePlacement::stretchToFit, 1.000f);
  265. }
  266. void resized() override
  267. {
  268. titleLabel->setBounds (88, 10, 397, 24);
  269. contentLabel->setBounds (88, 40, 397, 51);
  270. changeLogLabel->setBounds (22, 92, 341, 24);
  271. changeLog->setBounds (24, 112, 476, 102);
  272. if (hasOverwriteButton)
  273. {
  274. okButton->setBounds (getWidth() - 24 - 174, getHeight() - 37, 174, 28);
  275. overwriteButton->setBounds ((getWidth() - 24 - 174) + -14 - 86, getHeight() - 37, 86, 28);
  276. cancelButton->setBounds (24, getHeight() - 37, 70, 28);
  277. overwriteLabel->setBounds (24, 238, 472, 16);
  278. overwritePath->setBounds (24, 262, 472, 40);
  279. }
  280. else
  281. {
  282. okButton->setBounds (getWidth() - 24 - 47, getHeight() - 37, 47, 28);
  283. cancelButton->setBounds ((getWidth() - 24 - 47) + -14 - 70, getHeight() - 37, 70, 28);
  284. }
  285. }
  286. void buttonClicked (Button* clickedButton) override
  287. {
  288. if (auto* parentDialog = findParentComponentOfClass<DialogWindow>())
  289. {
  290. if (clickedButton == overwriteButton.get()) parentDialog->exitModalState (1);
  291. else if (clickedButton == okButton.get()) parentDialog->exitModalState (2);
  292. else if (clickedButton == cancelButton.get()) parentDialog->exitModalState (-1);
  293. }
  294. else
  295. jassertfalse;
  296. }
  297. static DialogWindow* launch (const LatestVersionChecker::JuceVersionTriple& version,
  298. const String& productName,
  299. const String& releaseNotes,
  300. const char* overwritePath = nullptr)
  301. {
  302. OptionalScopedPointer<Component> userDialog (new UpdateUserDialog (version, productName,
  303. releaseNotes, overwritePath), true);
  304. DialogWindow::LaunchOptions lo;
  305. lo.dialogTitle = TRANS ("Download \"123\" version 456?").replace ("456", version.toString())
  306. .replace ("123", productName);
  307. lo.dialogBackgroundColour = userDialog->findColour (backgroundColourId);
  308. lo.content = userDialog;
  309. lo.componentToCentreAround = nullptr;
  310. lo.escapeKeyTriggersCloseButton = true;
  311. lo.useNativeTitleBar = true;
  312. lo.resizable = false;
  313. lo.useBottomRightCornerResizer = false;
  314. return lo.launchAsync();
  315. }
  316. private:
  317. bool hasOverwriteButton;
  318. ScopedPointer<Label> titleLabel, contentLabel, changeLogLabel, overwriteLabel, overwritePath;
  319. ScopedPointer<TextButton> okButton, cancelButton;
  320. ScopedPointer<TextEditor> changeLog;
  321. ScopedPointer<TextButton> overwriteButton;
  322. ScopedPointer<Drawable> juceIcon;
  323. void lookAndFeelChanged() override
  324. {
  325. cancelButton->setColour (TextButton::buttonColourId,
  326. findColour (secondaryButtonBackgroundColourId));
  327. changeLog->applyFontToAllText (changeLog->getFont());
  328. }
  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 (EnabledModuleList::findGlobalModulesFolder());
  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. }