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.

833 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 = url.createInputStream (false, nullptr, nullptr, headers, 10000, &responseHeaders, &statusCode, 0);
  149. if (in == nullptr || statusCode != 302)
  150. break;
  151. String redirectPath = responseHeaders ["Location"];
  152. if (redirectPath.isEmpty())
  153. break;
  154. url = owner.getLatestVersionURL (headers, redirectPath);
  155. }
  156. if (in != nullptr && statusCode == 200)
  157. {
  158. int64 total = 0;
  159. MemoryOutputStream mo (dest, true);
  160. for (;;)
  161. {
  162. if (threadShouldExit())
  163. return Result::fail ("cancel");
  164. int64 written = mo.writeFromInputStream (*in, 8192);
  165. if (written == 0)
  166. break;
  167. total += written;
  168. setStatusMessage (String (TRANS ("Downloading... (123)"))
  169. .replace ("123", File::descriptionOfSizeInBytes (total)));
  170. }
  171. return Result::ok();
  172. }
  173. return Result::fail ("Failed to download from: " + url.toString (false));
  174. }
  175. LatestVersionChecker& owner;
  176. Result result;
  177. URL url;
  178. String headers;
  179. File targetFolder;
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DownloadNewVersionThread)
  181. };
  182. //==============================================================================
  183. class UpdateUserDialog : public Component,
  184. public Button::Listener
  185. {
  186. public:
  187. UpdateUserDialog (const LatestVersionChecker::JuceVersionTriple& version,
  188. const String& productName,
  189. const String& releaseNotes,
  190. const char* overwriteFolderPath)
  191. : hasOverwriteButton (overwriteFolderPath != nullptr)
  192. {
  193. addAndMakeVisible (titleLabel = new Label ("Title Label",
  194. TRANS ("Download \"123\" version 456?").replace ("123", productName)
  195. .replace ("456", version.toString())));
  196. titleLabel->setFont (Font (15.00f, Font::bold));
  197. titleLabel->setJustificationType (Justification::centredLeft);
  198. titleLabel->setEditable (false, false, false);
  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. addAndMakeVisible (okButton = new TextButton ("OK Button"));
  206. okButton->setButtonText (TRANS(hasOverwriteButton ? "Choose Another Folder..." : "OK"));
  207. okButton->addListener (this);
  208. addAndMakeVisible (cancelButton = new TextButton ("Cancel Button"));
  209. cancelButton->setButtonText (TRANS("Cancel"));
  210. cancelButton->addListener (this);
  211. addAndMakeVisible (changeLogLabel = new Label ("Change Log Label",
  212. TRANS("Release Notes:")));
  213. changeLogLabel->setFont (Font (15.00f, Font::plain));
  214. changeLogLabel->setJustificationType (Justification::topLeft);
  215. changeLogLabel->setEditable (false, false, false);
  216. addAndMakeVisible (changeLog = new TextEditor ("Change Log"));
  217. changeLog->setMultiLine (true);
  218. changeLog->setReturnKeyStartsNewLine (true);
  219. changeLog->setReadOnly (true);
  220. changeLog->setScrollbarsShown (true);
  221. changeLog->setCaretVisible (false);
  222. changeLog->setPopupMenuEnabled (false);
  223. changeLog->setText (releaseNotes);
  224. if (hasOverwriteButton)
  225. {
  226. addAndMakeVisible (overwriteLabel = new Label ("Overwrite Label",
  227. TRANS("Updating will overwrite everything in the following folder:")));
  228. overwriteLabel->setFont (Font (15.00f, Font::plain));
  229. overwriteLabel->setJustificationType (Justification::topLeft);
  230. overwriteLabel->setEditable (false, false, false);
  231. addAndMakeVisible (overwritePath = new Label ("Overwrite Path", overwriteFolderPath));
  232. overwritePath->setFont (Font (15.00f, Font::bold));
  233. overwritePath->setJustificationType (Justification::topLeft);
  234. overwritePath->setEditable (false, false, false);
  235. addAndMakeVisible (overwriteButton = new TextButton ("Overwrite Button"));
  236. overwriteButton->setButtonText (TRANS("Overwrite"));
  237. overwriteButton->addListener (this);
  238. }
  239. juceIcon = Drawable::createFromImageData (BinaryData::juce_icon_png,
  240. BinaryData::juce_icon_pngSize);
  241. setSize (518, overwritePath ? 345 : 269);
  242. lookAndFeelChanged();
  243. }
  244. ~UpdateUserDialog()
  245. {
  246. titleLabel = nullptr;
  247. contentLabel = nullptr;
  248. okButton = nullptr;
  249. cancelButton = nullptr;
  250. changeLogLabel = nullptr;
  251. changeLog = nullptr;
  252. overwriteLabel = nullptr;
  253. overwritePath = nullptr;
  254. overwriteButton = nullptr;
  255. juceIcon = nullptr;
  256. }
  257. void paint (Graphics& g) override
  258. {
  259. g.fillAll (findColour (backgroundColourId));
  260. g.setColour (findColour (defaultTextColourId));
  261. if (juceIcon != nullptr)
  262. juceIcon->drawWithin (g, Rectangle<float> (20, 17, 64, 64),
  263. RectanglePlacement::stretchToFit, 1.000f);
  264. }
  265. void resized() override
  266. {
  267. titleLabel->setBounds (88, 10, 397, 24);
  268. contentLabel->setBounds (88, 40, 397, 51);
  269. changeLogLabel->setBounds (22, 92, 341, 24);
  270. changeLog->setBounds (24, 112, 476, 102);
  271. if (hasOverwriteButton)
  272. {
  273. okButton->setBounds (getWidth() - 24 - 174, getHeight() - 37, 174, 28);
  274. overwriteButton->setBounds ((getWidth() - 24 - 174) + -14 - 86, getHeight() - 37, 86, 28);
  275. cancelButton->setBounds (24, getHeight() - 37, 70, 28);
  276. overwriteLabel->setBounds (24, 238, 472, 16);
  277. overwritePath->setBounds (24, 262, 472, 40);
  278. }
  279. else
  280. {
  281. okButton->setBounds (getWidth() - 24 - 47, getHeight() - 37, 47, 28);
  282. cancelButton->setBounds ((getWidth() - 24 - 47) + -14 - 70, getHeight() - 37, 70, 28);
  283. }
  284. }
  285. void buttonClicked (Button* clickedButton) override
  286. {
  287. if (DialogWindow* parentDialog = findParentComponentOfClass<DialogWindow>())
  288. {
  289. if (clickedButton == overwriteButton) parentDialog->exitModalState (1);
  290. else if (clickedButton == okButton) parentDialog->exitModalState (2);
  291. else if (clickedButton == cancelButton) parentDialog->exitModalState (-1);
  292. }
  293. else
  294. jassertfalse;
  295. }
  296. static DialogWindow* launch (const LatestVersionChecker::JuceVersionTriple& version,
  297. const String& productName,
  298. const String& releaseNotes,
  299. const char* overwritePath = nullptr)
  300. {
  301. OptionalScopedPointer<Component> userDialog (new UpdateUserDialog (version, productName,
  302. releaseNotes, overwritePath), true);
  303. DialogWindow::LaunchOptions lo;
  304. lo.dialogTitle = TRANS ("Download \"123\" version 456?").replace ("456", version.toString())
  305. .replace ("123", productName);
  306. lo.dialogBackgroundColour = userDialog->findColour (backgroundColourId);
  307. lo.content = userDialog;
  308. lo.componentToCentreAround = nullptr;
  309. lo.escapeKeyTriggersCloseButton = true;
  310. lo.useNativeTitleBar = true;
  311. lo.resizable = false;
  312. lo.useBottomRightCornerResizer = false;
  313. return lo.launchAsync();
  314. }
  315. private:
  316. bool hasOverwriteButton;
  317. ScopedPointer<Label> titleLabel, contentLabel, changeLogLabel, overwriteLabel, overwritePath;
  318. ScopedPointer<TextButton> okButton, cancelButton;
  319. ScopedPointer<TextEditor> changeLog;
  320. ScopedPointer<TextButton> overwriteButton;
  321. ScopedPointer<Drawable> juceIcon;
  322. void lookAndFeelChanged() override
  323. {
  324. cancelButton->setColour (TextButton::buttonColourId,
  325. findColour (secondaryButtonBackgroundColourId));
  326. changeLog->applyFontToAllText (changeLog->getFont());
  327. }
  328. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UpdateUserDialog)
  329. };
  330. //==============================================================================
  331. class UpdaterDialogModalCallback : public ModalComponentManager::Callback
  332. {
  333. public:
  334. struct DelayedCallback : private Timer
  335. {
  336. DelayedCallback (LatestVersionChecker& versionChecker,
  337. URL& newVersionToDownload,
  338. const String& extraHeaders,
  339. const File& appParentFolder,
  340. int returnValue)
  341. : parent (versionChecker), download (newVersionToDownload),
  342. headers (extraHeaders), folder (appParentFolder), result (returnValue)
  343. {
  344. startTimer (200);
  345. }
  346. private:
  347. void timerCallback() override
  348. {
  349. stopTimer();
  350. parent.modalStateFinished (result, download, headers, folder);
  351. delete this;
  352. }
  353. LatestVersionChecker& parent;
  354. URL download;
  355. String headers;
  356. File folder;
  357. int result;
  358. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DelayedCallback)
  359. };
  360. UpdaterDialogModalCallback (LatestVersionChecker& versionChecker,
  361. URL& newVersionToDownload,
  362. const String& extraHeaders,
  363. const File& appParentFolder)
  364. : parent (versionChecker), download (newVersionToDownload),
  365. headers (extraHeaders), folder (appParentFolder)
  366. {}
  367. void modalStateFinished (int returnValue) override
  368. {
  369. // the dialog window is only closed after this function exits
  370. // so we need a deferred callback to the parent. Unfortunately
  371. // our instance is also deleted after this function is used
  372. // so we can't use our own instance for a timer callback
  373. // we must allocate a new one.
  374. new DelayedCallback (parent, download, headers, folder, returnValue);
  375. }
  376. private:
  377. LatestVersionChecker& parent;
  378. URL download;
  379. String headers;
  380. File folder;
  381. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UpdaterDialogModalCallback)
  382. };
  383. //==============================================================================
  384. LatestVersionChecker::LatestVersionChecker() : Thread ("Updater"),
  385. statusCode (-1),
  386. hasAttemptedToReadWebsite (false)
  387. {
  388. startTimer (2000);
  389. }
  390. LatestVersionChecker::~LatestVersionChecker()
  391. {
  392. stopThread (20000);
  393. }
  394. String LatestVersionChecker::getOSString()
  395. {
  396. SystemStats::OperatingSystemType osType = SystemStats::getOperatingSystemType();
  397. if ((osType & SystemStats::MacOSX) != 0) return "OSX";
  398. else if ((osType & SystemStats::Windows) != 0) return "Windows";
  399. else if ((osType & SystemStats::Linux) != 0) return "Linux";
  400. else return SystemStats::getOperatingSystemName();
  401. }
  402. const LatestVersionChecker::JuceServerLocationsAndKeys& LatestVersionChecker::getJuceServerURLsAndKeys() const
  403. {
  404. static LatestVersionChecker::JuceServerLocationsAndKeys urlsAndKeys =
  405. {
  406. "https://my.roli.com",
  407. "265441b-343403c-20f6932-76361d",
  408. 1,
  409. "/software_versions/update_to/Projucer/"
  410. };
  411. return urlsAndKeys;
  412. }
  413. int LatestVersionChecker::getProductVersionNumber() const { return ProjectInfo::versionNumber; }
  414. const char* LatestVersionChecker::getProductName() const { return ProjectInfo::projectName; }
  415. bool LatestVersionChecker::allowCustomLocation() const { return true; }
  416. Result LatestVersionChecker::performUpdate (const MemoryBlock& data, File& targetFolder)
  417. {
  418. File unzipTarget;
  419. bool isUsingTempFolder = false;
  420. {
  421. MemoryInputStream input (data, false);
  422. ZipFile zip (input);
  423. if (zip.getNumEntries() == 0)
  424. return Result::fail ("The downloaded file wasn't a valid JUCE file!");
  425. unzipTarget = targetFolder;
  426. if (unzipTarget.exists())
  427. {
  428. isUsingTempFolder = true;
  429. unzipTarget = targetFolder.getNonexistentSibling();
  430. if (! unzipTarget.createDirectory())
  431. return Result::fail ("Couldn't create a folder to unzip the new version!");
  432. }
  433. Result r (zip.uncompressTo (unzipTarget));
  434. if (r.failed())
  435. {
  436. if (isUsingTempFolder)
  437. unzipTarget.deleteRecursively();
  438. return r;
  439. }
  440. }
  441. if (isUsingTempFolder)
  442. {
  443. File oldFolder (targetFolder.getSiblingFile (targetFolder.getFileNameWithoutExtension() + "_old")
  444. .getNonexistentSibling());
  445. if (! targetFolder.moveFileTo (oldFolder))
  446. {
  447. unzipTarget.deleteRecursively();
  448. return Result::fail ("Could not remove the existing folder!");
  449. }
  450. if (! unzipTarget.moveFileTo (targetFolder))
  451. {
  452. unzipTarget.deleteRecursively();
  453. return Result::fail ("Could not overwrite the existing folder!");
  454. }
  455. }
  456. return Result::ok();
  457. }
  458. URL LatestVersionChecker::getLatestVersionURL (String& headers, const String& path) const
  459. {
  460. const LatestVersionChecker::JuceServerLocationsAndKeys& urlsAndKeys = getJuceServerURLsAndKeys();
  461. String updateURL;
  462. bool isAbsolute = (path.startsWith ("http://") || path.startsWith ("https://"));
  463. bool isRedirect = path.isNotEmpty();
  464. if (isAbsolute)
  465. {
  466. updateURL = path;
  467. }
  468. else
  469. {
  470. updateURL << urlsAndKeys.updateSeverHostname
  471. << (isRedirect ? path : String (urlsAndKeys.updatePath));
  472. if (! isRedirect)
  473. {
  474. updateURL << JuceVersionTriple (getProductVersionNumber()).toString() << '/'
  475. << getOSString() << "?language=" << SystemStats::getUserLanguage();
  476. }
  477. }
  478. headers.clear();
  479. if (! isAbsolute)
  480. {
  481. headers << "X-API-Key: " << urlsAndKeys.publicAPIKey;
  482. if (! isRedirect)
  483. {
  484. headers << "\nContent-Type: application/json\n"
  485. << "Accept: application/json; version=" << urlsAndKeys.apiVersion;
  486. }
  487. }
  488. return URL (updateURL);
  489. }
  490. URL LatestVersionChecker::getLatestVersionURL (String& headers) const
  491. {
  492. String emptyString;
  493. return getLatestVersionURL (headers, emptyString);
  494. }
  495. void LatestVersionChecker::checkForNewVersion()
  496. {
  497. hasAttemptedToReadWebsite = true;
  498. {
  499. String extraHeaders;
  500. URL updateURL (getLatestVersionURL (extraHeaders));
  501. StringPairArray responseHeaders;
  502. const int numRedirects = 0;
  503. const ScopedPointer<InputStream> in (updateURL.createInputStream (false, nullptr, nullptr,
  504. extraHeaders, 0, &responseHeaders,
  505. &statusCode, numRedirects));
  506. if (threadShouldExit())
  507. return; // can't connect: fail silently.
  508. if (in != nullptr && (statusCode == 303 || statusCode == 400))
  509. {
  510. // if this doesn't fail then there is a new version available.
  511. // By leaving the scope of this function we will abort the download
  512. // to give the user a chance to cancel an update
  513. if (statusCode == 303)
  514. newRelativeDownloadPath = responseHeaders ["Location"];
  515. jsonReply = JSON::parse (in->readEntireStreamAsString());
  516. }
  517. }
  518. if (! threadShouldExit())
  519. startTimer (100);
  520. }
  521. bool LatestVersionChecker::processResult (var reply, const String& downloadPath)
  522. {
  523. if (statusCode == 303)
  524. {
  525. String versionString = reply.getProperty ("version", var()).toString();
  526. String releaseNotes = reply.getProperty ("notes", var()).toString();
  527. JuceVersionTriple version;
  528. if (versionString.isNotEmpty() && releaseNotes.isNotEmpty())
  529. {
  530. if (JuceVersionTriple::fromString (versionString, version))
  531. {
  532. String extraHeaders;
  533. URL newVersionToDownload = getLatestVersionURL (extraHeaders, downloadPath);
  534. return askUserAboutNewVersion (version, releaseNotes, newVersionToDownload, extraHeaders);
  535. }
  536. }
  537. }
  538. else if (statusCode == 400)
  539. {
  540. // In the far-distant future, this may be contacting a defunct
  541. // URL, so hopefully the website will contain a helpful message
  542. // for the user..
  543. var errorObj = reply.getDynamicObject()->getProperty ("error");
  544. if (errorObj.isObject())
  545. {
  546. String message = errorObj.getProperty ("message", var()).toString();
  547. if (message.isNotEmpty())
  548. {
  549. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  550. TRANS("JUCE Updater"),
  551. message);
  552. return false;
  553. }
  554. }
  555. }
  556. // try again
  557. return true;
  558. }
  559. bool LatestVersionChecker::askUserAboutNewVersion (const LatestVersionChecker::JuceVersionTriple& version,
  560. const String& releaseNotes,
  561. URL& newVersionToDownload,
  562. const String& extraHeaders)
  563. {
  564. JuceVersionTriple currentVersion (getProductVersionNumber());
  565. if (version > currentVersion)
  566. {
  567. File appParentFolder (File::getSpecialLocation (File::currentApplicationFile).getParentDirectory());
  568. DialogWindow* modalDialog = nullptr;
  569. if (isZipFolder (appParentFolder) && allowCustomLocation())
  570. {
  571. modalDialog = UpdateUserDialog::launch (version, getProductName(), releaseNotes,
  572. appParentFolder.getFullPathName().toRawUTF8());
  573. }
  574. else
  575. {
  576. modalDialog = UpdateUserDialog::launch (version, getProductName(), releaseNotes);
  577. }
  578. if (modalDialog != nullptr)
  579. {
  580. UpdaterDialogModalCallback* callback = new UpdaterDialogModalCallback (*this,
  581. newVersionToDownload,
  582. extraHeaders,
  583. appParentFolder);
  584. // attachCallback will delete callback
  585. if (ModalComponentManager* mm = ModalComponentManager::getInstance())
  586. mm->attachCallback (modalDialog, callback);
  587. }
  588. return false;
  589. }
  590. return true;
  591. }
  592. void LatestVersionChecker::modalStateFinished (int result,
  593. URL& newVersionToDownload,
  594. const String& extraHeaders,
  595. File appParentFolder)
  596. {
  597. if (result == 1 || result == 2)
  598. {
  599. if (result == 1 || ! allowCustomLocation())
  600. DownloadNewVersionThread::performDownload (*this, newVersionToDownload, extraHeaders, appParentFolder);
  601. else
  602. askUserForLocationToDownload (newVersionToDownload, extraHeaders);
  603. }
  604. }
  605. void LatestVersionChecker::askUserForLocationToDownload (URL& newVersionToDownload, const String& extraHeaders)
  606. {
  607. File targetFolder (EnabledModuleList::findGlobalModulesFolder());
  608. if (isJuceModulesFolder (targetFolder))
  609. targetFolder = targetFolder.getParentDirectory();
  610. FileChooser chooser (TRANS("Please select the location into which you'd like to install the new version"),
  611. targetFolder);
  612. if (chooser.browseForDirectory())
  613. {
  614. targetFolder = chooser.getResult();
  615. if (isJuceModulesFolder (targetFolder))
  616. targetFolder = targetFolder.getParentDirectory();
  617. if (targetFolder.getChildFile ("JUCE").isDirectory())
  618. targetFolder = targetFolder.getChildFile ("JUCE");
  619. if (targetFolder.getChildFile (".git").isDirectory())
  620. {
  621. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  622. TRANS ("Downloading new JUCE version"),
  623. TRANS ("This folder is a GIT repository!\n\n"
  624. "You should use a \"git pull\" to update it to the latest version. "
  625. "Or to use the Projucer to get an update, you should select an empty "
  626. "folder into which you'd like to download the new code."));
  627. return;
  628. }
  629. if (isJuceFolder (targetFolder))
  630. {
  631. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  632. TRANS("Overwrite existing JUCE folder?"),
  633. TRANS("Do you want to overwrite the folder:\n\n"
  634. "xfldrx\n\n"
  635. " ..with the latest version from juce.com?\n\n"
  636. "(Please note that this will overwrite everything in that folder!)")
  637. .replace ("xfldrx", targetFolder.getFullPathName())))
  638. {
  639. return;
  640. }
  641. }
  642. else
  643. {
  644. targetFolder = targetFolder.getChildFile ("JUCE").getNonexistentSibling();
  645. }
  646. DownloadNewVersionThread::performDownload (*this, newVersionToDownload, extraHeaders, targetFolder);
  647. }
  648. }
  649. bool LatestVersionChecker::isZipFolder (const File& f)
  650. {
  651. return f.getChildFile ("modules").isDirectory()
  652. && f.getChildFile ("extras").isDirectory()
  653. && f.getChildFile ("examples").isDirectory()
  654. && ! f.getChildFile (".git").isDirectory();
  655. }
  656. void LatestVersionChecker::timerCallback()
  657. {
  658. stopTimer();
  659. if (hasAttemptedToReadWebsite)
  660. {
  661. bool restartTimer = true;
  662. if (jsonReply.isObject())
  663. restartTimer = processResult (jsonReply, newRelativeDownloadPath);
  664. hasAttemptedToReadWebsite = false;
  665. if (restartTimer)
  666. startTimer (7200000);
  667. }
  668. else
  669. {
  670. startThread (3);
  671. }
  672. }
  673. void LatestVersionChecker::run()
  674. {
  675. checkForNewVersion();
  676. }