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.

983 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_Project.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier exporters ("EXPORTFORMATS");
  31. const Identifier configGroup ("JUCEOPTIONS");
  32. const Identifier modulesGroup ("MODULES");
  33. const Identifier module ("MODULE");
  34. }
  35. const char* Project::projectFileExtension = ".jucer";
  36. //==============================================================================
  37. Project::Project (const File& file_)
  38. : FileBasedDocument (projectFileExtension,
  39. String ("*") + projectFileExtension,
  40. "Choose a Jucer project to load",
  41. "Save Jucer project"),
  42. projectRoot (Tags::projectRoot)
  43. {
  44. setFile (file_);
  45. removeDefunctExporters();
  46. setMissingDefaultValues();
  47. setChangedFlag (false);
  48. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  49. projectRoot.addListener (this);
  50. }
  51. Project::~Project()
  52. {
  53. projectRoot.removeListener (this);
  54. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  55. }
  56. //==============================================================================
  57. void Project::setTitle (const String& newTitle)
  58. {
  59. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  60. getMainGroup().getNameValue() = newTitle;
  61. }
  62. const String Project::getDocumentTitle()
  63. {
  64. return getProjectName().toString();
  65. }
  66. void Project::updateProjectSettings()
  67. {
  68. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  69. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  70. }
  71. void Project::setMissingDefaultValues()
  72. {
  73. if (! projectRoot.hasProperty (ComponentBuilder::idProperty))
  74. projectRoot.setProperty (ComponentBuilder::idProperty, createAlphaNumericUID(), nullptr);
  75. // Create main file group if missing
  76. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  77. {
  78. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  79. projectRoot.addChild (mainGroup.state, 0, 0);
  80. }
  81. getMainGroup().initialiseMissingProperties();
  82. if (getDocumentTitle().isEmpty())
  83. setTitle ("Juce Project");
  84. if (! projectRoot.hasProperty (Ids::projectType))
  85. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  86. if (! projectRoot.hasProperty (Ids::version))
  87. getVersionValue() = "1.0.0";
  88. updateOldStyleConfigList();
  89. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  90. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  91. for (Project::ExporterIterator exporter (*this); exporter.next();)
  92. if (exporter->getNumConfigurations() == 0)
  93. exporter->createDefaultConfigs();
  94. if (! projectRoot.getChildWithName (Tags::exporters).isValid())
  95. createDefaultExporters();
  96. getProjectType().setMissingProjectProperties (*this);
  97. if (! projectRoot.hasProperty (Ids::bundleIdentifier))
  98. setBundleIdentifierToDefault();
  99. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  100. addDefaultModules (false);
  101. }
  102. void Project::updateOldStyleConfigList()
  103. {
  104. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  105. if (deprecatedConfigsList.isValid())
  106. {
  107. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  108. for (Project::ExporterIterator exporter (*this); exporter.next();)
  109. {
  110. if (exporter->getNumConfigurations() == 0)
  111. {
  112. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  113. if (! exporter->isXcode())
  114. {
  115. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  116. {
  117. ValueTree config (newConfigs.getChild(j));
  118. config.removeProperty (Ids::osxSDK, nullptr);
  119. config.removeProperty (Ids::osxCompatibility, nullptr);
  120. config.removeProperty (Ids::osxArchitecture, nullptr);
  121. }
  122. }
  123. exporter->settings.addChild (newConfigs, 0, nullptr);
  124. }
  125. }
  126. }
  127. }
  128. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  129. {
  130. if (projectRoot.hasProperty (name))
  131. {
  132. for (Project::ExporterIterator exporter (*this); exporter.next();)
  133. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  134. projectRoot.removeProperty (name, nullptr);
  135. }
  136. }
  137. void Project::removeDefunctExporters()
  138. {
  139. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  140. for (;;)
  141. {
  142. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  143. if (oldVC6Exporter.isValid())
  144. exporters.removeChild (oldVC6Exporter, nullptr);
  145. else
  146. break;
  147. }
  148. }
  149. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  150. {
  151. addModule ("juce_core", shouldCopyFilesLocally);
  152. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  153. {
  154. addModule ("juce_events", shouldCopyFilesLocally);
  155. addModule ("juce_graphics", shouldCopyFilesLocally);
  156. addModule ("juce_data_structures", shouldCopyFilesLocally);
  157. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  158. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  159. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  160. addModule ("juce_cryptography", shouldCopyFilesLocally);
  161. addModule ("juce_video", shouldCopyFilesLocally);
  162. addModule ("juce_opengl", shouldCopyFilesLocally);
  163. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  164. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  165. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  166. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  167. }
  168. }
  169. //==============================================================================
  170. const String Project::loadDocument (const File& file)
  171. {
  172. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  173. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  174. return "Not a valid Jucer project!";
  175. ValueTree newTree (ValueTree::fromXml (*xml));
  176. if (! newTree.hasType (Tags::projectRoot))
  177. return "The document contains errors and couldn't be parsed!";
  178. StoredSettings::getInstance()->recentFiles.addFile (file);
  179. StoredSettings::getInstance()->flush();
  180. projectRoot = newTree;
  181. setMissingDefaultValues();
  182. return String::empty;
  183. }
  184. const String Project::saveDocument (const File& file)
  185. {
  186. updateProjectSettings();
  187. sanitiseConfigFlags();
  188. StoredSettings::getInstance()->recentFiles.addFile (file);
  189. ProjectSaver saver (*this, file);
  190. return saver.save();
  191. }
  192. //==============================================================================
  193. File Project::lastDocumentOpened;
  194. const File Project::getLastDocumentOpened()
  195. {
  196. return lastDocumentOpened;
  197. }
  198. void Project::setLastDocumentOpened (const File& file)
  199. {
  200. lastDocumentOpened = file;
  201. }
  202. //==============================================================================
  203. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  204. {
  205. if (property == Ids::projectType)
  206. setMissingDefaultValues();
  207. changed();
  208. }
  209. void Project::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
  210. {
  211. changed();
  212. }
  213. void Project::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved)
  214. {
  215. changed();
  216. }
  217. void Project::valueTreeChildOrderChanged (ValueTree& parentTree)
  218. {
  219. changed();
  220. }
  221. void Project::valueTreeParentChanged (ValueTree& tree)
  222. {
  223. }
  224. //==============================================================================
  225. File Project::resolveFilename (String filename) const
  226. {
  227. if (filename.isEmpty())
  228. return File::nonexistent;
  229. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  230. .replaceCharacter ('\\', '/');
  231. if (File::isAbsolutePath (filename))
  232. return File (filename);
  233. return getFile().getSiblingFile (filename);
  234. }
  235. String Project::getRelativePathForFile (const File& file) const
  236. {
  237. String filename (file.getFullPathName());
  238. File relativePathBase (getFile().getParentDirectory());
  239. String p1 (relativePathBase.getFullPathName());
  240. String p2 (file.getFullPathName());
  241. while (p1.startsWithChar (File::separator))
  242. p1 = p1.substring (1);
  243. while (p2.startsWithChar (File::separator))
  244. p2 = p2.substring (1);
  245. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  246. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  247. {
  248. filename = file.getRelativePathFrom (relativePathBase);
  249. }
  250. return filename;
  251. }
  252. //==============================================================================
  253. const ProjectType& Project::getProjectType() const
  254. {
  255. const ProjectType* type = ProjectType::findType (getProjectTypeString());
  256. jassert (type != nullptr);
  257. if (type == nullptr)
  258. {
  259. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  260. jassert (type != nullptr);
  261. }
  262. return *type;
  263. }
  264. //==============================================================================
  265. void Project::createPropertyEditors (PropertyListBuilder& props)
  266. {
  267. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false),
  268. "The name of the project.");
  269. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  270. "The project's version number, This should be in the format major.minor.point");
  271. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  272. "Your company name, which will be added to the properties of the binary where possible");
  273. {
  274. StringArray projectTypeNames;
  275. Array<var> projectTypeCodes;
  276. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  277. for (int i = 0; i < types.size(); ++i)
  278. {
  279. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  280. projectTypeCodes.add (types.getUnchecked(i)->getType());
  281. }
  282. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  283. }
  284. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  285. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  286. getProjectType().createPropertyEditors (*this, props);
  287. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  288. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  289. props.setPreferredHeight (22);
  290. }
  291. String Project::getVersionAsHex() const
  292. {
  293. StringArray configs;
  294. configs.addTokens (getVersionString(), ",.", String::empty);
  295. configs.trim();
  296. configs.removeEmptyStrings();
  297. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  298. if (configs.size() >= 4)
  299. value = (value << 8) + configs[3].getIntValue();
  300. return "0x" + String::toHexString (value);
  301. }
  302. StringPairArray Project::getPreprocessorDefs() const
  303. {
  304. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  305. }
  306. //==============================================================================
  307. Project::Item Project::getMainGroup()
  308. {
  309. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  310. }
  311. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  312. {
  313. if (item.isImageFile())
  314. {
  315. found.add (new Project::Item (item));
  316. }
  317. else if (item.isGroup())
  318. {
  319. for (int i = 0; i < item.getNumChildren(); ++i)
  320. findImages (item.getChild (i), found);
  321. }
  322. }
  323. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  324. {
  325. findImages (getMainGroup(), items);
  326. }
  327. //==============================================================================
  328. Project::Item::Item (Project& project_, const ValueTree& state_)
  329. : project (project_), state (state_)
  330. {
  331. }
  332. Project::Item::Item (const Item& other)
  333. : project (other.project), state (other.state)
  334. {
  335. }
  336. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  337. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  338. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  339. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  340. Image Project::Item::loadAsImageFile() const
  341. {
  342. return isValid() ? ImageCache::getFromFile (getFile())
  343. : Image::null;
  344. }
  345. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  346. {
  347. Item group (project, ValueTree (Tags::group));
  348. group.setID (uid);
  349. group.initialiseMissingProperties();
  350. group.getNameValue() = name;
  351. return group;
  352. }
  353. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  354. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  355. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  356. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  357. Project::Item Project::Item::findItemWithID (const String& targetId) const
  358. {
  359. if (state [ComponentBuilder::idProperty] == targetId)
  360. return *this;
  361. if (isGroup())
  362. {
  363. for (int i = getNumChildren(); --i >= 0;)
  364. {
  365. Item found (getChild(i).findItemWithID (targetId));
  366. if (found.isValid())
  367. return found;
  368. }
  369. }
  370. return Item (project, ValueTree::invalid);
  371. }
  372. bool Project::Item::canContain (const Item& child) const
  373. {
  374. if (isFile())
  375. return false;
  376. if (isGroup())
  377. return child.isFile() || child.isGroup();
  378. jassertfalse
  379. return false;
  380. }
  381. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  382. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  383. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  384. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  385. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  386. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  387. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  388. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  389. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  390. String Project::Item::getFilePath() const
  391. {
  392. if (isFile())
  393. return state [Ids::file].toString();
  394. else
  395. return String::empty;
  396. }
  397. File Project::Item::getFile() const
  398. {
  399. if (isFile())
  400. return project.resolveFilename (state [Ids::file].toString());
  401. else
  402. return File::nonexistent;
  403. }
  404. void Project::Item::setFile (const File& file)
  405. {
  406. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  407. jassert (getFile() == file);
  408. }
  409. void Project::Item::setFile (const RelativePath& file)
  410. {
  411. jassert (file.getRoot() == RelativePath::projectFolder);
  412. jassert (isFile());
  413. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  414. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  415. }
  416. bool Project::Item::renameFile (const File& newFile)
  417. {
  418. const File oldFile (getFile());
  419. if (oldFile.moveFileTo (newFile))
  420. {
  421. setFile (newFile);
  422. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  423. return true;
  424. }
  425. return false;
  426. }
  427. bool Project::Item::containsChildForFile (const RelativePath& file) const
  428. {
  429. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  430. }
  431. Project::Item Project::Item::findItemForFile (const File& file) const
  432. {
  433. if (getFile() == file)
  434. return *this;
  435. if (isGroup())
  436. {
  437. for (int i = getNumChildren(); --i >= 0;)
  438. {
  439. Item found (getChild(i).findItemForFile (file));
  440. if (found.isValid())
  441. return found;
  442. }
  443. }
  444. return Item (project, ValueTree::invalid);
  445. }
  446. File Project::Item::determineGroupFolder() const
  447. {
  448. jassert (isGroup());
  449. File f;
  450. for (int i = 0; i < getNumChildren(); ++i)
  451. {
  452. f = getChild(i).getFile();
  453. if (f.exists())
  454. return f.getParentDirectory();
  455. }
  456. Item parent (getParent());
  457. if (parent != *this)
  458. {
  459. f = parent.determineGroupFolder();
  460. if (f.getChildFile (getName()).isDirectory())
  461. f = f.getChildFile (getName());
  462. }
  463. else
  464. {
  465. f = project.getFile().getParentDirectory();
  466. if (f.getChildFile ("Source").isDirectory())
  467. f = f.getChildFile ("Source");
  468. }
  469. return f;
  470. }
  471. void Project::Item::initialiseMissingProperties()
  472. {
  473. if (! state.hasProperty (ComponentBuilder::idProperty))
  474. setID (createAlphaNumericUID());
  475. if (isFile())
  476. {
  477. state.setProperty (Ids::name, getFile().getFileName(), 0);
  478. }
  479. else if (isGroup())
  480. {
  481. for (int i = getNumChildren(); --i >= 0;)
  482. getChild(i).initialiseMissingProperties();
  483. }
  484. }
  485. Value Project::Item::getNameValue()
  486. {
  487. return state.getPropertyAsValue (Ids::name, getUndoManager());
  488. }
  489. String Project::Item::getName() const
  490. {
  491. return state [Ids::name];
  492. }
  493. void Project::Item::addChild (const Item& newChild, int insertIndex)
  494. {
  495. state.addChild (newChild.state, insertIndex, getUndoManager());
  496. }
  497. void Project::Item::removeItemFromProject()
  498. {
  499. state.getParent().removeChild (state, getUndoManager());
  500. }
  501. Project::Item Project::Item::getParent() const
  502. {
  503. if (isMainGroup() || ! isGroup())
  504. return *this;
  505. return Item (project, state.getParent());
  506. }
  507. struct ItemSorter
  508. {
  509. static int compareElements (const ValueTree& first, const ValueTree& second)
  510. {
  511. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  512. }
  513. };
  514. struct ItemSorterWithGroupsAtStart
  515. {
  516. static int compareElements (const ValueTree& first, const ValueTree& second)
  517. {
  518. const bool firstIsGroup = first.hasType (Tags::group);
  519. const bool secondIsGroup = second.hasType (Tags::group);
  520. if (firstIsGroup == secondIsGroup)
  521. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  522. else
  523. return firstIsGroup ? -1 : 1;
  524. }
  525. };
  526. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  527. {
  528. if (keepGroupsAtStart)
  529. {
  530. ItemSorterWithGroupsAtStart sorter;
  531. state.sort (sorter, getUndoManager(), true);
  532. }
  533. else
  534. {
  535. ItemSorter sorter;
  536. state.sort (sorter, getUndoManager(), true);
  537. }
  538. }
  539. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  540. {
  541. for (int i = state.getNumChildren(); --i >= 0;)
  542. {
  543. const ValueTree child (state.getChild (i));
  544. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  545. return Item (project, child);
  546. }
  547. return addNewSubGroup (name, -1);
  548. }
  549. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  550. {
  551. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  552. jassert (canContain (group));
  553. addChild (group, insertIndex);
  554. return group;
  555. }
  556. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  557. {
  558. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  559. return false;
  560. if (file.isDirectory())
  561. {
  562. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  563. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  564. while (iter.next())
  565. {
  566. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  567. group.addFile (iter.getFile(), -1, shouldCompile);
  568. }
  569. group.sortAlphabetically (false);
  570. }
  571. else if (file.existsAsFile())
  572. {
  573. if (! project.getMainGroup().findItemForFile (file).isValid())
  574. addFileUnchecked (file, insertIndex, shouldCompile);
  575. }
  576. else
  577. {
  578. jassertfalse;
  579. }
  580. return true;
  581. }
  582. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  583. {
  584. Item item (project, ValueTree (Tags::file));
  585. item.initialiseMissingProperties();
  586. item.getNameValue() = file.getFileName();
  587. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  588. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  589. if (canContain (item))
  590. {
  591. item.setFile (file);
  592. addChild (item, insertIndex);
  593. }
  594. }
  595. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  596. {
  597. Item item (project, ValueTree (Tags::file));
  598. item.initialiseMissingProperties();
  599. item.getNameValue() = file.getFileName();
  600. item.getShouldCompileValue() = shouldCompile;
  601. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  602. if (canContain (item))
  603. {
  604. item.setFile (file);
  605. addChild (item, insertIndex);
  606. return true;
  607. }
  608. return false;
  609. }
  610. const Drawable* Project::Item::getIcon() const
  611. {
  612. if (isFile())
  613. {
  614. if (isImageFile())
  615. return StoredSettings::getInstance()->getImageFileIcon();
  616. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  617. }
  618. else if (isMainGroup())
  619. {
  620. return &(project.mainProjectIcon);
  621. }
  622. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  623. }
  624. //==============================================================================
  625. ValueTree Project::getConfigNode()
  626. {
  627. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  628. }
  629. const char* const Project::configFlagDefault = "default";
  630. const char* const Project::configFlagEnabled = "enabled";
  631. const char* const Project::configFlagDisabled = "disabled";
  632. Value Project::getConfigFlag (const String& name)
  633. {
  634. ValueTree configNode (getConfigNode());
  635. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  636. if (v.getValue().toString().isEmpty())
  637. v = configFlagDefault;
  638. return v;
  639. }
  640. bool Project::isConfigFlagEnabled (const String& name) const
  641. {
  642. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  643. }
  644. void Project::sanitiseConfigFlags()
  645. {
  646. ValueTree configNode (getConfigNode());
  647. for (int i = configNode.getNumProperties(); --i >= 0;)
  648. {
  649. const var value (configNode [configNode.getPropertyName(i)]);
  650. if (value != configFlagEnabled && value != configFlagDisabled)
  651. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  652. }
  653. }
  654. //==============================================================================
  655. ValueTree Project::getModulesNode()
  656. {
  657. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  658. }
  659. bool Project::isModuleEnabled (const String& moduleID) const
  660. {
  661. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  662. for (int i = 0; i < modules.getNumChildren(); ++i)
  663. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  664. return true;
  665. return false;
  666. }
  667. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  668. {
  669. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  670. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  671. }
  672. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  673. {
  674. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  675. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  676. }
  677. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  678. {
  679. if (! isModuleEnabled (moduleID))
  680. {
  681. ValueTree module (Tags::module);
  682. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  683. ValueTree modules (getModulesNode());
  684. modules.addChild (module, -1, getUndoManagerFor (modules));
  685. shouldShowAllModuleFilesInProject (moduleID) = true;
  686. }
  687. if (shouldCopyFilesLocally)
  688. shouldCopyModuleFilesLocally (moduleID) = true;
  689. }
  690. void Project::removeModule (const String& moduleID)
  691. {
  692. ValueTree modules (getModulesNode());
  693. for (int i = 0; i < modules.getNumChildren(); ++i)
  694. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  695. modules.removeChild (i, getUndoManagerFor (modules));
  696. }
  697. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  698. {
  699. for (int i = 0; i < availableModules.modules.size(); ++i)
  700. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  701. modules.add (availableModules.modules.getUnchecked(i)->create());
  702. }
  703. int Project::getNumModules() const
  704. {
  705. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  706. }
  707. String Project::getModuleID (int index) const
  708. {
  709. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  710. }
  711. //==============================================================================
  712. ValueTree Project::getExporters()
  713. {
  714. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  715. if (! exporters.isValid())
  716. {
  717. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  718. exporters = getExporters();
  719. }
  720. return exporters;
  721. }
  722. int Project::getNumExporters()
  723. {
  724. return getExporters().getNumChildren();
  725. }
  726. ProjectExporter* Project::createExporter (int index)
  727. {
  728. jassert (index >= 0 && index < getNumExporters());
  729. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  730. }
  731. void Project::addNewExporter (const String& exporterName)
  732. {
  733. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  734. ValueTree exporters (getExporters());
  735. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  736. }
  737. void Project::deleteExporter (int index)
  738. {
  739. ValueTree exporters (getExporters());
  740. exporters.removeChild (index, getUndoManagerFor (exporters));
  741. }
  742. void Project::createDefaultExporters()
  743. {
  744. ValueTree exporters (getExporters());
  745. exporters.removeAllChildren (getUndoManagerFor (exporters));
  746. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  747. for (int i = 0; i < exporterNames.size(); ++i)
  748. addNewExporter (exporterNames[i]);
  749. }
  750. //==============================================================================
  751. String Project::getFileTemplate (const String& templateName)
  752. {
  753. int dataSize;
  754. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  755. if (data == nullptr)
  756. {
  757. jassertfalse;
  758. return String::empty;
  759. }
  760. return String::fromUTF8 (data, dataSize);
  761. }
  762. //==============================================================================
  763. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  764. Project::ExporterIterator::~ExporterIterator() {}
  765. bool Project::ExporterIterator::next()
  766. {
  767. if (++index >= project.getNumExporters())
  768. return false;
  769. exporter = project.createExporter (index);
  770. if (exporter == nullptr)
  771. {
  772. jassertfalse; // corrupted project file?
  773. return next();
  774. }
  775. return true;
  776. }