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.

1040 lines
32KB

  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_Project.h"
  18. #include "jucer_ProjectType.h"
  19. #include "../Project Saving/jucer_ProjectExporter.h"
  20. #include "../Project Saving/jucer_ProjectSaver.h"
  21. #include "../Application/jucer_OpenDocumentManager.h"
  22. #include "../Application/jucer_Application.h"
  23. //==============================================================================
  24. Project::Project (const File& f)
  25. : FileBasedDocument (projectFileExtension,
  26. String ("*") + projectFileExtension,
  27. "Choose a Jucer project to load",
  28. "Save Jucer project"),
  29. projectRoot (Ids::JUCERPROJECT),
  30. isSaving (false)
  31. {
  32. Logger::writeToLog ("Loading project: " + f.getFullPathName());
  33. setFile (f);
  34. removeDefunctExporters();
  35. updateOldModulePaths();
  36. setMissingDefaultValues();
  37. setChangedFlag (false);
  38. projectRoot.addListener (this);
  39. }
  40. Project::~Project()
  41. {
  42. projectRoot.removeListener (this);
  43. IntrojucerApp::getApp().openDocumentManager.closeAllDocumentsUsingProject (*this, false);
  44. }
  45. const char* Project::projectFileExtension = ".jucer";
  46. //==============================================================================
  47. void Project::setTitle (const String& newTitle)
  48. {
  49. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  50. getMainGroup().getNameValue() = newTitle;
  51. }
  52. String Project::getTitle() const
  53. {
  54. return projectRoot.getChildWithName (Ids::MAINGROUP) [Ids::name];
  55. }
  56. String Project::getDocumentTitle()
  57. {
  58. return getTitle();
  59. }
  60. void Project::updateProjectSettings()
  61. {
  62. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, nullptr);
  63. projectRoot.setProperty (Ids::name, getDocumentTitle(), nullptr);
  64. }
  65. void Project::setMissingDefaultValues()
  66. {
  67. if (! projectRoot.hasProperty (Ids::ID))
  68. projectRoot.setProperty (Ids::ID, createAlphaNumericUID(), nullptr);
  69. // Create main file group if missing
  70. if (! projectRoot.getChildWithName (Ids::MAINGROUP).isValid())
  71. {
  72. Item mainGroup (*this, ValueTree (Ids::MAINGROUP));
  73. projectRoot.addChild (mainGroup.state, 0, 0);
  74. }
  75. getMainGroup().initialiseMissingProperties();
  76. if (getDocumentTitle().isEmpty())
  77. setTitle ("JUCE Project");
  78. if (! projectRoot.hasProperty (Ids::projectType))
  79. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  80. if (! projectRoot.hasProperty (Ids::version))
  81. getVersionValue() = "1.0.0";
  82. updateOldStyleConfigList();
  83. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  84. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  85. getProjectType().setMissingProjectProperties (*this);
  86. getModules().sortAlphabetically();
  87. if (getBundleIdentifier().toString().isEmpty())
  88. getBundleIdentifier() = getDefaultBundleIdentifier();
  89. if (shouldIncludeBinaryInAppConfig() == var::null)
  90. shouldIncludeBinaryInAppConfig() = true;
  91. IntrojucerApp::getApp().updateNewlyOpenedProject (*this);
  92. }
  93. void Project::updateOldStyleConfigList()
  94. {
  95. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (Ids::CONFIGURATIONS));
  96. if (deprecatedConfigsList.isValid())
  97. {
  98. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  99. for (Project::ExporterIterator exporter (*this); exporter.next();)
  100. {
  101. if (exporter->getNumConfigurations() == 0)
  102. {
  103. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  104. if (! exporter->isXcode())
  105. {
  106. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  107. {
  108. ValueTree config (newConfigs.getChild(j));
  109. config.removeProperty (Ids::osxSDK, nullptr);
  110. config.removeProperty (Ids::osxCompatibility, nullptr);
  111. config.removeProperty (Ids::osxArchitecture, nullptr);
  112. }
  113. }
  114. exporter->settings.addChild (newConfigs, 0, nullptr);
  115. }
  116. }
  117. }
  118. }
  119. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  120. {
  121. if (projectRoot.hasProperty (name))
  122. {
  123. for (Project::ExporterIterator exporter (*this); exporter.next();)
  124. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  125. projectRoot.removeProperty (name, nullptr);
  126. }
  127. }
  128. void Project::removeDefunctExporters()
  129. {
  130. ValueTree exporters (projectRoot.getChildWithName (Ids::EXPORTFORMATS));
  131. for (;;)
  132. {
  133. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  134. if (oldVC6Exporter.isValid())
  135. exporters.removeChild (oldVC6Exporter, nullptr);
  136. else
  137. break;
  138. }
  139. }
  140. void Project::updateOldModulePaths()
  141. {
  142. for (Project::ExporterIterator exporter (*this); exporter.next();)
  143. exporter->updateOldModulePaths();
  144. }
  145. //==============================================================================
  146. static int getVersionElement (StringRef v, int index)
  147. {
  148. StringArray parts;
  149. parts.addTokens (v, "., ", StringRef());
  150. return parts [parts.size() - index - 1].getIntValue();
  151. }
  152. static int getJuceVersion (const String& v)
  153. {
  154. return getVersionElement (v, 2) * 100000
  155. + getVersionElement (v, 1) * 1000
  156. + getVersionElement (v, 0);
  157. }
  158. static int getBuiltJuceVersion()
  159. {
  160. return JUCE_MAJOR_VERSION * 100000
  161. + JUCE_MINOR_VERSION * 1000
  162. + JUCE_BUILDNUMBER;
  163. }
  164. static bool isAnyModuleNewerThanIntrojucer (const OwnedArray<ModuleDescription>& modules)
  165. {
  166. for (int i = modules.size(); --i >= 0;)
  167. {
  168. const ModuleDescription* m = modules.getUnchecked(i);
  169. if (m->getID().startsWith ("juce_")
  170. && getJuceVersion (m->getVersion()) > getBuiltJuceVersion())
  171. return true;
  172. }
  173. return false;
  174. }
  175. void Project::warnAboutOldIntrojucerVersion()
  176. {
  177. ModuleList available;
  178. available.scanAllKnownFolders (*this);
  179. if (isAnyModuleNewerThanIntrojucer (available.modules))
  180. {
  181. if (IntrojucerApp::getApp().isRunningCommandLine)
  182. std::cout << "WARNING! This version of the introjucer is out-of-date!" << std::endl;
  183. else
  184. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  185. "Introjucer",
  186. "This version of the introjucer is out-of-date!"
  187. "\n\n"
  188. "Always make sure that you're running the very latest version, "
  189. "preferably compiled directly from the JUCE repository that you're working with!");
  190. }
  191. }
  192. //==============================================================================
  193. static File lastDocumentOpened;
  194. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  195. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  196. static void registerRecentFile (const File& file)
  197. {
  198. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  199. getAppSettings().recentFiles.addFile (file);
  200. getAppSettings().flush();
  201. }
  202. //==============================================================================
  203. Result Project::loadDocument (const File& file)
  204. {
  205. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  206. if (xml == nullptr || ! xml->hasTagName (Ids::JUCERPROJECT.toString()))
  207. return Result::fail ("Not a valid Jucer project!");
  208. ValueTree newTree (ValueTree::fromXml (*xml));
  209. if (! newTree.hasType (Ids::JUCERPROJECT))
  210. return Result::fail ("The document contains errors and couldn't be parsed!");
  211. registerRecentFile (file);
  212. enabledModulesList = nullptr;
  213. projectRoot = newTree;
  214. removeDefunctExporters();
  215. setMissingDefaultValues();
  216. updateOldModulePaths();
  217. setChangedFlag (false);
  218. warnAboutOldIntrojucerVersion();
  219. return Result::ok();
  220. }
  221. Result Project::saveDocument (const File& file)
  222. {
  223. return saveProject (file, false);
  224. }
  225. Result Project::saveProject (const File& file, bool isCommandLineApp)
  226. {
  227. if (isSaving)
  228. return Result::ok();
  229. updateProjectSettings();
  230. sanitiseConfigFlags();
  231. if (! isCommandLineApp)
  232. registerRecentFile (file);
  233. const ScopedValueSetter<bool> vs (isSaving, true, false);
  234. ProjectSaver saver (*this, file);
  235. return saver.save (! isCommandLineApp);
  236. }
  237. Result Project::saveResourcesOnly (const File& file)
  238. {
  239. ProjectSaver saver (*this, file);
  240. return saver.saveResourcesOnly();
  241. }
  242. //==============================================================================
  243. void Project::valueTreePropertyChanged (ValueTree&, const Identifier& property)
  244. {
  245. if (property == Ids::projectType)
  246. setMissingDefaultValues();
  247. changed();
  248. }
  249. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  250. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&, int) { changed(); }
  251. void Project::valueTreeChildOrderChanged (ValueTree&, int, int) { changed(); }
  252. void Project::valueTreeParentChanged (ValueTree&) {}
  253. //==============================================================================
  254. File Project::resolveFilename (String filename) const
  255. {
  256. if (filename.isEmpty())
  257. return File::nonexistent;
  258. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  259. if (FileHelpers::isAbsolutePath (filename))
  260. return File::createFileWithoutCheckingPath (FileHelpers::currentOSStylePath (filename)); // (avoid assertions for windows-style paths)
  261. return getFile().getSiblingFile (FileHelpers::currentOSStylePath (filename));
  262. }
  263. String Project::getRelativePathForFile (const File& file) const
  264. {
  265. String filename (file.getFullPathName());
  266. File relativePathBase (getFile().getParentDirectory());
  267. String p1 (relativePathBase.getFullPathName());
  268. String p2 (file.getFullPathName());
  269. while (p1.startsWithChar (File::separator))
  270. p1 = p1.substring (1);
  271. while (p2.startsWithChar (File::separator))
  272. p2 = p2.substring (1);
  273. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  274. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  275. {
  276. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  277. }
  278. return filename;
  279. }
  280. //==============================================================================
  281. const ProjectType& Project::getProjectType() const
  282. {
  283. if (const ProjectType* type = ProjectType::findType (getProjectTypeString()))
  284. return *type;
  285. const ProjectType* guiType = ProjectType::findType (ProjectType::getGUIAppTypeName());
  286. jassert (guiType != nullptr);
  287. return *guiType;
  288. }
  289. //==============================================================================
  290. void Project::createPropertyEditors (PropertyListBuilder& props)
  291. {
  292. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  293. "The name of the project.");
  294. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  295. "The project's version number, This should be in the format major.minor.point[.point]");
  296. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  297. "Your company name, which will be added to the properties of the binary where possible");
  298. props.add (new TextPropertyComponent (getCompanyWebsite(), "Company Website", 256, false),
  299. "Your company website, which will be added to the properties of the binary where possible");
  300. props.add (new TextPropertyComponent (getCompanyEmail(), "Company E-mail", 256, false),
  301. "Your company e-mail, which will be added to the properties of the binary where possible");
  302. {
  303. StringArray projectTypeNames;
  304. Array<var> projectTypeCodes;
  305. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  306. for (int i = 0; i < types.size(); ++i)
  307. {
  308. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  309. projectTypeCodes.add (types.getUnchecked(i)->getType());
  310. }
  311. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  312. }
  313. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  314. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  315. getProjectType().createPropertyEditors (*this, props);
  316. {
  317. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  318. StringArray maxSizeNames;
  319. Array<var> maxSizeCodes;
  320. maxSizeNames.add (TRANS("Default"));
  321. maxSizeCodes.add (var::null);
  322. maxSizeNames.add (String::empty);
  323. maxSizeCodes.add (var::null);
  324. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  325. {
  326. const int sizeInBytes = maxSizes[i] * 1024;
  327. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  328. maxSizeCodes.add (sizeInBytes);
  329. }
  330. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  331. "When splitting binary data into multiple cpp files, the Introjucer attempts to keep the file sizes below this threshold. "
  332. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  333. }
  334. props.add (new BooleanPropertyComponent (shouldIncludeBinaryInAppConfig(), "Include Binary",
  335. "Include BinaryData.h in the AppConfig.h file"));
  336. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  337. "Global preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  338. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  339. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  340. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  341. }
  342. //==============================================================================
  343. static StringArray getVersionSegments (const Project& p)
  344. {
  345. StringArray segments;
  346. segments.addTokens (p.getVersionString(), ",.", "");
  347. segments.trim();
  348. segments.removeEmptyStrings();
  349. return segments;
  350. }
  351. int Project::getVersionAsHexInteger() const
  352. {
  353. const StringArray segments (getVersionSegments (*this));
  354. int value = (segments[0].getIntValue() << 16)
  355. + (segments[1].getIntValue() << 8)
  356. + segments[2].getIntValue();
  357. if (segments.size() >= 4)
  358. value = (value << 8) + segments[3].getIntValue();
  359. return value;
  360. }
  361. String Project::getVersionAsHex() const
  362. {
  363. return "0x" + String::toHexString (getVersionAsHexInteger());
  364. }
  365. StringPairArray Project::getPreprocessorDefs() const
  366. {
  367. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  368. }
  369. File Project::getBinaryDataCppFile (int index) const
  370. {
  371. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  372. if (index > 0)
  373. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  374. .withFileExtension (cpp.getFileExtension());
  375. return cpp;
  376. }
  377. Project::Item Project::getMainGroup()
  378. {
  379. return Item (*this, projectRoot.getChildWithName (Ids::MAINGROUP));
  380. }
  381. PropertiesFile& Project::getStoredProperties() const
  382. {
  383. return getAppSettings().getProjectProperties (getProjectUID());
  384. }
  385. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  386. {
  387. if (item.isImageFile())
  388. {
  389. found.add (new Project::Item (item));
  390. }
  391. else if (item.isGroup())
  392. {
  393. for (int i = 0; i < item.getNumChildren(); ++i)
  394. findImages (item.getChild (i), found);
  395. }
  396. }
  397. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  398. {
  399. findImages (getMainGroup(), items);
  400. }
  401. //==============================================================================
  402. Project::Item::Item (Project& p, const ValueTree& s)
  403. : project (p), state (s)
  404. {
  405. }
  406. Project::Item::Item (const Item& other)
  407. : project (other.project), state (other.state)
  408. {
  409. }
  410. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  411. String Project::Item::getID() const { return state [Ids::ID]; }
  412. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  413. Drawable* Project::Item::loadAsImageFile() const
  414. {
  415. return isValid() ? Drawable::createFromImageFile (getFile())
  416. : nullptr;
  417. }
  418. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  419. {
  420. Item group (project, ValueTree (Ids::GROUP));
  421. group.setID (uid);
  422. group.initialiseMissingProperties();
  423. group.getNameValue() = name;
  424. return group;
  425. }
  426. bool Project::Item::isFile() const { return state.hasType (Ids::FILE); }
  427. bool Project::Item::isGroup() const { return state.hasType (Ids::GROUP) || isMainGroup(); }
  428. bool Project::Item::isMainGroup() const { return state.hasType (Ids::MAINGROUP); }
  429. bool Project::Item::isImageFile() const
  430. {
  431. return isFile() && (ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr
  432. || getFile().hasFileExtension ("svg"));
  433. }
  434. Project::Item Project::Item::findItemWithID (const String& targetId) const
  435. {
  436. if (state [Ids::ID] == targetId)
  437. return *this;
  438. if (isGroup())
  439. {
  440. for (int i = getNumChildren(); --i >= 0;)
  441. {
  442. Item found (getChild(i).findItemWithID (targetId));
  443. if (found.isValid())
  444. return found;
  445. }
  446. }
  447. return Item (project, ValueTree());
  448. }
  449. bool Project::Item::canContain (const Item& child) const
  450. {
  451. if (isFile())
  452. return false;
  453. if (isGroup())
  454. return child.isFile() || child.isGroup();
  455. jassertfalse;
  456. return false;
  457. }
  458. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  459. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  460. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  461. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  462. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  463. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  464. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  465. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  466. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  467. String Project::Item::getFilePath() const
  468. {
  469. if (isFile())
  470. return state [Ids::file].toString();
  471. return String::empty;
  472. }
  473. File Project::Item::getFile() const
  474. {
  475. if (isFile())
  476. return project.resolveFilename (state [Ids::file].toString());
  477. return File::nonexistent;
  478. }
  479. void Project::Item::setFile (const File& file)
  480. {
  481. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  482. jassert (getFile() == file);
  483. }
  484. void Project::Item::setFile (const RelativePath& file)
  485. {
  486. jassert (isFile());
  487. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  488. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  489. }
  490. bool Project::Item::renameFile (const File& newFile)
  491. {
  492. const File oldFile (getFile());
  493. if (oldFile.moveFileTo (newFile)
  494. || (newFile.exists() && ! oldFile.exists()))
  495. {
  496. setFile (newFile);
  497. IntrojucerApp::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  498. return true;
  499. }
  500. return false;
  501. }
  502. bool Project::Item::containsChildForFile (const RelativePath& file) const
  503. {
  504. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  505. }
  506. Project::Item Project::Item::findItemForFile (const File& file) const
  507. {
  508. if (getFile() == file)
  509. return *this;
  510. if (isGroup())
  511. {
  512. for (int i = getNumChildren(); --i >= 0;)
  513. {
  514. Item found (getChild(i).findItemForFile (file));
  515. if (found.isValid())
  516. return found;
  517. }
  518. }
  519. return Item (project, ValueTree());
  520. }
  521. File Project::Item::determineGroupFolder() const
  522. {
  523. jassert (isGroup());
  524. File f;
  525. for (int i = 0; i < getNumChildren(); ++i)
  526. {
  527. f = getChild(i).getFile();
  528. if (f.exists())
  529. return f.getParentDirectory();
  530. }
  531. Item parent (getParent());
  532. if (parent != *this)
  533. {
  534. f = parent.determineGroupFolder();
  535. if (f.getChildFile (getName()).isDirectory())
  536. f = f.getChildFile (getName());
  537. }
  538. else
  539. {
  540. f = project.getProjectFolder();
  541. if (f.getChildFile ("Source").isDirectory())
  542. f = f.getChildFile ("Source");
  543. }
  544. return f;
  545. }
  546. void Project::Item::initialiseMissingProperties()
  547. {
  548. if (! state.hasProperty (Ids::ID))
  549. setID (createAlphaNumericUID());
  550. if (isFile())
  551. {
  552. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  553. }
  554. else if (isGroup())
  555. {
  556. for (int i = getNumChildren(); --i >= 0;)
  557. getChild(i).initialiseMissingProperties();
  558. }
  559. }
  560. Value Project::Item::getNameValue()
  561. {
  562. return state.getPropertyAsValue (Ids::name, getUndoManager());
  563. }
  564. String Project::Item::getName() const
  565. {
  566. return state [Ids::name];
  567. }
  568. void Project::Item::addChild (const Item& newChild, int insertIndex)
  569. {
  570. state.addChild (newChild.state, insertIndex, getUndoManager());
  571. }
  572. void Project::Item::removeItemFromProject()
  573. {
  574. state.getParent().removeChild (state, getUndoManager());
  575. }
  576. Project::Item Project::Item::getParent() const
  577. {
  578. if (isMainGroup() || ! isGroup())
  579. return *this;
  580. return Item (project, state.getParent());
  581. }
  582. struct ItemSorter
  583. {
  584. static int compareElements (const ValueTree& first, const ValueTree& second)
  585. {
  586. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  587. }
  588. };
  589. struct ItemSorterWithGroupsAtStart
  590. {
  591. static int compareElements (const ValueTree& first, const ValueTree& second)
  592. {
  593. const bool firstIsGroup = first.hasType (Ids::GROUP);
  594. const bool secondIsGroup = second.hasType (Ids::GROUP);
  595. if (firstIsGroup == secondIsGroup)
  596. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  597. return firstIsGroup ? -1 : 1;
  598. }
  599. };
  600. static void sortGroup (ValueTree& state, bool keepGroupsAtStart, UndoManager* undoManager)
  601. {
  602. if (keepGroupsAtStart)
  603. {
  604. ItemSorterWithGroupsAtStart sorter;
  605. state.sort (sorter, undoManager, true);
  606. }
  607. else
  608. {
  609. ItemSorter sorter;
  610. state.sort (sorter, undoManager, true);
  611. }
  612. }
  613. static bool isGroupSorted (const ValueTree& state, bool keepGroupsAtStart)
  614. {
  615. if (state.getNumChildren() == 0)
  616. return false;
  617. if (state.getNumChildren() == 1)
  618. return true;
  619. ValueTree stateCopy (state.createCopy());
  620. sortGroup (stateCopy, keepGroupsAtStart, nullptr);
  621. return stateCopy.isEquivalentTo (state);
  622. }
  623. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  624. {
  625. sortGroup (state, keepGroupsAtStart, getUndoManager());
  626. }
  627. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  628. {
  629. for (int i = state.getNumChildren(); --i >= 0;)
  630. {
  631. const ValueTree child (state.getChild (i));
  632. if (child.getProperty (Ids::name) == name && child.hasType (Ids::GROUP))
  633. return Item (project, child);
  634. }
  635. return addNewSubGroup (name, -1);
  636. }
  637. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  638. {
  639. String newID (createGUID (getID() + name + String (getNumChildren())));
  640. int n = 0;
  641. while (project.getMainGroup().findItemWithID (newID).isValid())
  642. newID = createGUID (newID + String (++n));
  643. Item group (createGroup (project, name, newID));
  644. jassert (canContain (group));
  645. addChild (group, insertIndex);
  646. return group;
  647. }
  648. bool Project::Item::addFileAtIndex (const File& file, int insertIndex, const bool shouldCompile)
  649. {
  650. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  651. return false;
  652. if (file.isDirectory())
  653. {
  654. Item group (addNewSubGroup (file.getFileName(), insertIndex));
  655. for (DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories); iter.next();)
  656. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  657. group.addFileRetainingSortOrder (iter.getFile(), shouldCompile);
  658. }
  659. else if (file.existsAsFile())
  660. {
  661. if (! project.getMainGroup().findItemForFile (file).isValid())
  662. addFileUnchecked (file, insertIndex, shouldCompile);
  663. }
  664. else
  665. {
  666. jassertfalse;
  667. }
  668. return true;
  669. }
  670. bool Project::Item::addFileRetainingSortOrder (const File& file, bool shouldCompile)
  671. {
  672. const bool wasSortedGroupsNotFirst = isGroupSorted (state, false);
  673. const bool wasSortedGroupsFirst = isGroupSorted (state, true);
  674. if (! addFileAtIndex (file, 0, shouldCompile))
  675. return false;
  676. if (wasSortedGroupsNotFirst || wasSortedGroupsFirst)
  677. sortAlphabetically (wasSortedGroupsFirst);
  678. return true;
  679. }
  680. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  681. {
  682. Item item (project, ValueTree (Ids::FILE));
  683. item.initialiseMissingProperties();
  684. item.getNameValue() = file.getFileName();
  685. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension (fileTypesToCompileByDefault);
  686. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  687. if (canContain (item))
  688. {
  689. item.setFile (file);
  690. addChild (item, insertIndex);
  691. }
  692. }
  693. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  694. {
  695. Item item (project, ValueTree (Ids::FILE));
  696. item.initialiseMissingProperties();
  697. item.getNameValue() = file.getFileName();
  698. item.getShouldCompileValue() = shouldCompile;
  699. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  700. if (canContain (item))
  701. {
  702. item.setFile (file);
  703. addChild (item, insertIndex);
  704. return true;
  705. }
  706. return false;
  707. }
  708. Icon Project::Item::getIcon() const
  709. {
  710. const Icons& icons = getIcons();
  711. if (isFile())
  712. {
  713. if (isImageFile())
  714. return Icon (icons.imageDoc, Colours::blue);
  715. return Icon (icons.document, Colours::yellow);
  716. }
  717. if (isMainGroup())
  718. return Icon (icons.juceLogo, Colours::orange);
  719. return Icon (icons.folder, Colours::darkgrey);
  720. }
  721. bool Project::Item::isIconCrossedOut() const
  722. {
  723. return isFile()
  724. && ! (shouldBeCompiled()
  725. || shouldBeAddedToBinaryResources()
  726. || getFile().hasFileExtension (headerFileExtensions));
  727. }
  728. //==============================================================================
  729. ValueTree Project::getConfigNode()
  730. {
  731. return projectRoot.getOrCreateChildWithName (Ids::JUCEOPTIONS, nullptr);
  732. }
  733. const char* const Project::configFlagDefault = "default";
  734. const char* const Project::configFlagEnabled = "enabled";
  735. const char* const Project::configFlagDisabled = "disabled";
  736. Value Project::getConfigFlag (const String& name)
  737. {
  738. ValueTree configNode (getConfigNode());
  739. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  740. if (v.getValue().toString().isEmpty())
  741. v = configFlagDefault;
  742. return v;
  743. }
  744. bool Project::isConfigFlagEnabled (const String& name) const
  745. {
  746. return projectRoot.getChildWithName (Ids::JUCEOPTIONS).getProperty (name) == configFlagEnabled;
  747. }
  748. void Project::sanitiseConfigFlags()
  749. {
  750. ValueTree configNode (getConfigNode());
  751. for (int i = configNode.getNumProperties(); --i >= 0;)
  752. {
  753. const var value (configNode [configNode.getPropertyName(i)]);
  754. if (value != configFlagEnabled && value != configFlagDisabled)
  755. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  756. }
  757. }
  758. //==============================================================================
  759. EnabledModuleList& Project::getModules()
  760. {
  761. if (enabledModulesList == nullptr)
  762. enabledModulesList = new EnabledModuleList (*this, projectRoot.getOrCreateChildWithName (Ids::MODULES, nullptr));
  763. return *enabledModulesList;
  764. }
  765. //==============================================================================
  766. ValueTree Project::getExporters()
  767. {
  768. return projectRoot.getOrCreateChildWithName (Ids::EXPORTFORMATS, nullptr);
  769. }
  770. int Project::getNumExporters()
  771. {
  772. return getExporters().getNumChildren();
  773. }
  774. ProjectExporter* Project::createExporter (int index)
  775. {
  776. jassert (index >= 0 && index < getNumExporters());
  777. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  778. }
  779. void Project::addNewExporter (const String& exporterName)
  780. {
  781. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  782. ValueTree exporters (getExporters());
  783. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  784. }
  785. void Project::createExporterForCurrentPlatform()
  786. {
  787. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  788. }
  789. //==============================================================================
  790. String Project::getFileTemplate (const String& templateName)
  791. {
  792. int dataSize;
  793. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  794. if (data == nullptr)
  795. {
  796. jassertfalse;
  797. return String::empty;
  798. }
  799. return String::fromUTF8 (data, dataSize);
  800. }
  801. //==============================================================================
  802. Project::ExporterIterator::ExporterIterator (Project& p) : index (-1), project (p) {}
  803. Project::ExporterIterator::~ExporterIterator() {}
  804. bool Project::ExporterIterator::next()
  805. {
  806. if (++index >= project.getNumExporters())
  807. return false;
  808. exporter = project.createExporter (index);
  809. if (exporter == nullptr)
  810. {
  811. jassertfalse; // corrupted project file?
  812. return next();
  813. }
  814. return true;
  815. }