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.

972 lines
30KB

  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().getName() = 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. getVersion() = "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 (getProjectTypeValue().toString());
  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 (getVersion(), "Project Version", 16, false),
  270. "The project's version number, This should be in the format major.minor.point");
  271. {
  272. StringArray projectTypeNames;
  273. Array<var> projectTypeCodes;
  274. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  275. for (int i = 0; i < types.size(); ++i)
  276. {
  277. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  278. projectTypeCodes.add (types.getUnchecked(i)->getType());
  279. }
  280. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  281. }
  282. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  283. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  284. getProjectType().createPropertyEditors (*this, props);
  285. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  286. "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.");
  287. props.setPreferredHeight (22);
  288. }
  289. String Project::getVersionAsHex() const
  290. {
  291. StringArray configs;
  292. configs.addTokens (getVersion().toString(), ",.", String::empty);
  293. configs.trim();
  294. configs.removeEmptyStrings();
  295. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  296. if (configs.size() >= 4)
  297. value = (value << 8) + configs[3].getIntValue();
  298. return "0x" + String::toHexString (value);
  299. }
  300. StringPairArray Project::getPreprocessorDefs() const
  301. {
  302. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  303. }
  304. //==============================================================================
  305. Project::Item Project::getMainGroup()
  306. {
  307. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  308. }
  309. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  310. {
  311. if (item.isImageFile())
  312. {
  313. found.add (new Project::Item (item));
  314. }
  315. else if (item.isGroup())
  316. {
  317. for (int i = 0; i < item.getNumChildren(); ++i)
  318. findImages (item.getChild (i), found);
  319. }
  320. }
  321. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  322. {
  323. findImages (getMainGroup(), items);
  324. }
  325. //==============================================================================
  326. Project::Item::Item (Project& project_, const ValueTree& state_)
  327. : project (project_), state (state_)
  328. {
  329. }
  330. Project::Item::Item (const Item& other)
  331. : project (other.project), state (other.state)
  332. {
  333. }
  334. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  335. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  336. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  337. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  338. Image Project::Item::loadAsImageFile() const
  339. {
  340. return isValid() ? ImageCache::getFromFile (getFile())
  341. : Image::null;
  342. }
  343. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  344. {
  345. Item group (project, ValueTree (Tags::group));
  346. group.setID (uid);
  347. group.initialiseMissingProperties();
  348. group.getName() = name;
  349. return group;
  350. }
  351. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  352. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  353. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  354. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  355. Project::Item Project::Item::findItemWithID (const String& targetId) const
  356. {
  357. if (state [ComponentBuilder::idProperty] == targetId)
  358. return *this;
  359. if (isGroup())
  360. {
  361. for (int i = getNumChildren(); --i >= 0;)
  362. {
  363. Item found (getChild(i).findItemWithID (targetId));
  364. if (found.isValid())
  365. return found;
  366. }
  367. }
  368. return Item (project, ValueTree::invalid);
  369. }
  370. bool Project::Item::canContain (const Item& child) const
  371. {
  372. if (isFile())
  373. return false;
  374. if (isGroup())
  375. return child.isFile() || child.isGroup();
  376. jassertfalse
  377. return false;
  378. }
  379. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  380. bool Project::Item::shouldBeCompiled() const { return getShouldCompileValue().getValue(); }
  381. Value Project::Item::getShouldCompileValue() const { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  382. bool Project::Item::shouldBeAddedToBinaryResources() const { return getShouldAddToResourceValue().getValue(); }
  383. Value Project::Item::getShouldAddToResourceValue() const { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  384. Value Project::Item::getShouldInhibitWarningsValue() const { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  385. Value Project::Item::getShouldUseStdCallValue() const { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  386. String Project::Item::getFilePath() const
  387. {
  388. if (isFile())
  389. return state [Ids::file].toString();
  390. else
  391. return String::empty;
  392. }
  393. File Project::Item::getFile() const
  394. {
  395. if (isFile())
  396. return project.resolveFilename (state [Ids::file].toString());
  397. else
  398. return File::nonexistent;
  399. }
  400. void Project::Item::setFile (const File& file)
  401. {
  402. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  403. jassert (getFile() == file);
  404. }
  405. void Project::Item::setFile (const RelativePath& file)
  406. {
  407. jassert (file.getRoot() == RelativePath::projectFolder);
  408. jassert (isFile());
  409. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  410. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  411. }
  412. bool Project::Item::renameFile (const File& newFile)
  413. {
  414. const File oldFile (getFile());
  415. if (oldFile.moveFileTo (newFile))
  416. {
  417. setFile (newFile);
  418. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  419. return true;
  420. }
  421. return false;
  422. }
  423. bool Project::Item::containsChildForFile (const RelativePath& file) const
  424. {
  425. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  426. }
  427. Project::Item Project::Item::findItemForFile (const File& file) const
  428. {
  429. if (getFile() == file)
  430. return *this;
  431. if (isGroup())
  432. {
  433. for (int i = getNumChildren(); --i >= 0;)
  434. {
  435. Item found (getChild(i).findItemForFile (file));
  436. if (found.isValid())
  437. return found;
  438. }
  439. }
  440. return Item (project, ValueTree::invalid);
  441. }
  442. File Project::Item::determineGroupFolder() const
  443. {
  444. jassert (isGroup());
  445. File f;
  446. for (int i = 0; i < getNumChildren(); ++i)
  447. {
  448. f = getChild(i).getFile();
  449. if (f.exists())
  450. return f.getParentDirectory();
  451. }
  452. Item parent (getParent());
  453. if (parent != *this)
  454. {
  455. f = parent.determineGroupFolder();
  456. if (f.getChildFile (getName().toString()).isDirectory())
  457. f = f.getChildFile (getName().toString());
  458. }
  459. else
  460. {
  461. f = project.getFile().getParentDirectory();
  462. if (f.getChildFile ("Source").isDirectory())
  463. f = f.getChildFile ("Source");
  464. }
  465. return f;
  466. }
  467. void Project::Item::initialiseMissingProperties()
  468. {
  469. if (! state.hasProperty (ComponentBuilder::idProperty))
  470. setID (createAlphaNumericUID());
  471. if (isFile())
  472. {
  473. state.setProperty (Ids::name, getFile().getFileName(), 0);
  474. }
  475. else if (isGroup())
  476. {
  477. for (int i = getNumChildren(); --i >= 0;)
  478. getChild(i).initialiseMissingProperties();
  479. }
  480. }
  481. Value Project::Item::getName() const
  482. {
  483. return state.getPropertyAsValue (Ids::name, getUndoManager());
  484. }
  485. void Project::Item::addChild (const Item& newChild, int insertIndex)
  486. {
  487. state.addChild (newChild.state, insertIndex, getUndoManager());
  488. }
  489. void Project::Item::removeItemFromProject()
  490. {
  491. state.getParent().removeChild (state, getUndoManager());
  492. }
  493. Project::Item Project::Item::getParent() const
  494. {
  495. if (isMainGroup() || ! isGroup())
  496. return *this;
  497. return Item (project, state.getParent());
  498. }
  499. struct ItemSorter
  500. {
  501. static int compareElements (const ValueTree& first, const ValueTree& second)
  502. {
  503. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  504. }
  505. };
  506. struct ItemSorterWithGroupsAtStart
  507. {
  508. static int compareElements (const ValueTree& first, const ValueTree& second)
  509. {
  510. const bool firstIsGroup = first.hasType (Tags::group);
  511. const bool secondIsGroup = second.hasType (Tags::group);
  512. if (firstIsGroup == secondIsGroup)
  513. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  514. else
  515. return firstIsGroup ? -1 : 1;
  516. }
  517. };
  518. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  519. {
  520. if (keepGroupsAtStart)
  521. {
  522. ItemSorterWithGroupsAtStart sorter;
  523. state.sort (sorter, getUndoManager(), true);
  524. }
  525. else
  526. {
  527. ItemSorter sorter;
  528. state.sort (sorter, getUndoManager(), true);
  529. }
  530. }
  531. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  532. {
  533. for (int i = state.getNumChildren(); --i >= 0;)
  534. {
  535. const ValueTree child (state.getChild (i));
  536. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  537. return Item (project, child);
  538. }
  539. return addNewSubGroup (name, -1);
  540. }
  541. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  542. {
  543. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  544. jassert (canContain (group));
  545. addChild (group, insertIndex);
  546. return group;
  547. }
  548. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  549. {
  550. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  551. return false;
  552. if (file.isDirectory())
  553. {
  554. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  555. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  556. while (iter.next())
  557. {
  558. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  559. group.addFile (iter.getFile(), -1, shouldCompile);
  560. }
  561. group.sortAlphabetically (false);
  562. }
  563. else if (file.existsAsFile())
  564. {
  565. if (! project.getMainGroup().findItemForFile (file).isValid())
  566. addFileUnchecked (file, insertIndex, shouldCompile);
  567. }
  568. else
  569. {
  570. jassertfalse;
  571. }
  572. return true;
  573. }
  574. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  575. {
  576. Item item (project, ValueTree (Tags::file));
  577. item.initialiseMissingProperties();
  578. item.getName() = file.getFileName();
  579. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  580. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  581. if (canContain (item))
  582. {
  583. item.setFile (file);
  584. addChild (item, insertIndex);
  585. }
  586. }
  587. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  588. {
  589. Item item (project, ValueTree (Tags::file));
  590. item.initialiseMissingProperties();
  591. item.getName() = file.getFileName();
  592. item.getShouldCompileValue() = shouldCompile;
  593. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  594. if (canContain (item))
  595. {
  596. item.setFile (file);
  597. addChild (item, insertIndex);
  598. return true;
  599. }
  600. return false;
  601. }
  602. const Drawable* Project::Item::getIcon() const
  603. {
  604. if (isFile())
  605. {
  606. if (isImageFile())
  607. return StoredSettings::getInstance()->getImageFileIcon();
  608. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  609. }
  610. else if (isMainGroup())
  611. {
  612. return &(project.mainProjectIcon);
  613. }
  614. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  615. }
  616. //==============================================================================
  617. ValueTree Project::getConfigNode()
  618. {
  619. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  620. }
  621. const char* const Project::configFlagDefault = "default";
  622. const char* const Project::configFlagEnabled = "enabled";
  623. const char* const Project::configFlagDisabled = "disabled";
  624. Value Project::getConfigFlag (const String& name)
  625. {
  626. const ValueTree configNode (getConfigNode());
  627. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  628. if (v.getValue().toString().isEmpty())
  629. v = configFlagDefault;
  630. return v;
  631. }
  632. bool Project::isConfigFlagEnabled (const String& name) const
  633. {
  634. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  635. }
  636. void Project::sanitiseConfigFlags()
  637. {
  638. ValueTree configNode (getConfigNode());
  639. for (int i = configNode.getNumProperties(); --i >= 0;)
  640. {
  641. const var value (configNode [configNode.getPropertyName(i)]);
  642. if (value != configFlagEnabled && value != configFlagDisabled)
  643. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  644. }
  645. }
  646. //==============================================================================
  647. ValueTree Project::getModulesNode()
  648. {
  649. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  650. }
  651. bool Project::isModuleEnabled (const String& moduleID) const
  652. {
  653. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  654. for (int i = 0; i < modules.getNumChildren(); ++i)
  655. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  656. return true;
  657. return false;
  658. }
  659. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  660. {
  661. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  662. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  663. }
  664. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  665. {
  666. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  667. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  668. }
  669. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  670. {
  671. if (! isModuleEnabled (moduleID))
  672. {
  673. ValueTree module (Tags::module);
  674. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  675. ValueTree modules (getModulesNode());
  676. modules.addChild (module, -1, getUndoManagerFor (modules));
  677. shouldShowAllModuleFilesInProject (moduleID) = true;
  678. }
  679. if (shouldCopyFilesLocally)
  680. shouldCopyModuleFilesLocally (moduleID) = true;
  681. }
  682. void Project::removeModule (const String& moduleID)
  683. {
  684. ValueTree modules (getModulesNode());
  685. for (int i = 0; i < modules.getNumChildren(); ++i)
  686. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  687. modules.removeChild (i, getUndoManagerFor (modules));
  688. }
  689. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  690. {
  691. for (int i = 0; i < availableModules.modules.size(); ++i)
  692. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  693. modules.add (availableModules.modules.getUnchecked(i)->create());
  694. }
  695. int Project::getNumModules() const
  696. {
  697. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  698. }
  699. String Project::getModuleID (int index) const
  700. {
  701. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  702. }
  703. //==============================================================================
  704. ValueTree Project::getExporters()
  705. {
  706. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  707. if (! exporters.isValid())
  708. {
  709. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  710. exporters = getExporters();
  711. }
  712. return exporters;
  713. }
  714. int Project::getNumExporters()
  715. {
  716. return getExporters().getNumChildren();
  717. }
  718. ProjectExporter* Project::createExporter (int index)
  719. {
  720. jassert (index >= 0 && index < getNumExporters());
  721. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  722. }
  723. void Project::addNewExporter (const String& exporterName)
  724. {
  725. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  726. ValueTree exporters (getExporters());
  727. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  728. }
  729. void Project::deleteExporter (int index)
  730. {
  731. ValueTree exporters (getExporters());
  732. exporters.removeChild (index, getUndoManagerFor (exporters));
  733. }
  734. void Project::createDefaultExporters()
  735. {
  736. ValueTree exporters (getExporters());
  737. exporters.removeAllChildren (getUndoManagerFor (exporters));
  738. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  739. for (int i = 0; i < exporterNames.size(); ++i)
  740. addNewExporter (exporterNames[i]);
  741. }
  742. //==============================================================================
  743. String Project::getFileTemplate (const String& templateName)
  744. {
  745. int dataSize;
  746. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  747. if (data == nullptr)
  748. {
  749. jassertfalse;
  750. return String::empty;
  751. }
  752. return String::fromUTF8 (data, dataSize);
  753. }
  754. //==============================================================================
  755. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  756. Project::ExporterIterator::~ExporterIterator() {}
  757. bool Project::ExporterIterator::next()
  758. {
  759. if (++index >= project.getNumExporters())
  760. return false;
  761. exporter = project.createExporter (index);
  762. if (exporter == nullptr)
  763. {
  764. jassertfalse; // corrupted project file?
  765. return next();
  766. }
  767. return true;
  768. }