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.

975 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().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. 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 (getVersion().toString(), ",.", 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 (getProjectPreprocessorDefs().toString());
  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.getName() = 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. bool Project::Item::shouldBeCompiled() const { return getShouldCompileValue().getValue(); }
  383. Value Project::Item::getShouldCompileValue() const { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  384. bool Project::Item::shouldBeAddedToBinaryResources() const { return getShouldAddToResourceValue().getValue(); }
  385. Value Project::Item::getShouldAddToResourceValue() const { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  386. Value Project::Item::getShouldInhibitWarningsValue() const { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  387. Value Project::Item::getShouldUseStdCallValue() const { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  388. String Project::Item::getFilePath() const
  389. {
  390. if (isFile())
  391. return state [Ids::file].toString();
  392. else
  393. return String::empty;
  394. }
  395. File Project::Item::getFile() const
  396. {
  397. if (isFile())
  398. return project.resolveFilename (state [Ids::file].toString());
  399. else
  400. return File::nonexistent;
  401. }
  402. void Project::Item::setFile (const File& file)
  403. {
  404. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  405. jassert (getFile() == file);
  406. }
  407. void Project::Item::setFile (const RelativePath& file)
  408. {
  409. jassert (file.getRoot() == RelativePath::projectFolder);
  410. jassert (isFile());
  411. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  412. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  413. }
  414. bool Project::Item::renameFile (const File& newFile)
  415. {
  416. const File oldFile (getFile());
  417. if (oldFile.moveFileTo (newFile))
  418. {
  419. setFile (newFile);
  420. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  421. return true;
  422. }
  423. return false;
  424. }
  425. bool Project::Item::containsChildForFile (const RelativePath& file) const
  426. {
  427. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  428. }
  429. Project::Item Project::Item::findItemForFile (const File& file) const
  430. {
  431. if (getFile() == file)
  432. return *this;
  433. if (isGroup())
  434. {
  435. for (int i = getNumChildren(); --i >= 0;)
  436. {
  437. Item found (getChild(i).findItemForFile (file));
  438. if (found.isValid())
  439. return found;
  440. }
  441. }
  442. return Item (project, ValueTree::invalid);
  443. }
  444. File Project::Item::determineGroupFolder() const
  445. {
  446. jassert (isGroup());
  447. File f;
  448. for (int i = 0; i < getNumChildren(); ++i)
  449. {
  450. f = getChild(i).getFile();
  451. if (f.exists())
  452. return f.getParentDirectory();
  453. }
  454. Item parent (getParent());
  455. if (parent != *this)
  456. {
  457. f = parent.determineGroupFolder();
  458. if (f.getChildFile (getName().toString()).isDirectory())
  459. f = f.getChildFile (getName().toString());
  460. }
  461. else
  462. {
  463. f = project.getFile().getParentDirectory();
  464. if (f.getChildFile ("Source").isDirectory())
  465. f = f.getChildFile ("Source");
  466. }
  467. return f;
  468. }
  469. void Project::Item::initialiseMissingProperties()
  470. {
  471. if (! state.hasProperty (ComponentBuilder::idProperty))
  472. setID (createAlphaNumericUID());
  473. if (isFile())
  474. {
  475. state.setProperty (Ids::name, getFile().getFileName(), 0);
  476. }
  477. else if (isGroup())
  478. {
  479. for (int i = getNumChildren(); --i >= 0;)
  480. getChild(i).initialiseMissingProperties();
  481. }
  482. }
  483. Value Project::Item::getName() const
  484. {
  485. return state.getPropertyAsValue (Ids::name, getUndoManager());
  486. }
  487. void Project::Item::addChild (const Item& newChild, int insertIndex)
  488. {
  489. state.addChild (newChild.state, insertIndex, getUndoManager());
  490. }
  491. void Project::Item::removeItemFromProject()
  492. {
  493. state.getParent().removeChild (state, getUndoManager());
  494. }
  495. Project::Item Project::Item::getParent() const
  496. {
  497. if (isMainGroup() || ! isGroup())
  498. return *this;
  499. return Item (project, state.getParent());
  500. }
  501. struct ItemSorter
  502. {
  503. static int compareElements (const ValueTree& first, const ValueTree& second)
  504. {
  505. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  506. }
  507. };
  508. struct ItemSorterWithGroupsAtStart
  509. {
  510. static int compareElements (const ValueTree& first, const ValueTree& second)
  511. {
  512. const bool firstIsGroup = first.hasType (Tags::group);
  513. const bool secondIsGroup = second.hasType (Tags::group);
  514. if (firstIsGroup == secondIsGroup)
  515. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  516. else
  517. return firstIsGroup ? -1 : 1;
  518. }
  519. };
  520. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  521. {
  522. if (keepGroupsAtStart)
  523. {
  524. ItemSorterWithGroupsAtStart sorter;
  525. state.sort (sorter, getUndoManager(), true);
  526. }
  527. else
  528. {
  529. ItemSorter sorter;
  530. state.sort (sorter, getUndoManager(), true);
  531. }
  532. }
  533. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  534. {
  535. for (int i = state.getNumChildren(); --i >= 0;)
  536. {
  537. const ValueTree child (state.getChild (i));
  538. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  539. return Item (project, child);
  540. }
  541. return addNewSubGroup (name, -1);
  542. }
  543. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  544. {
  545. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  546. jassert (canContain (group));
  547. addChild (group, insertIndex);
  548. return group;
  549. }
  550. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  551. {
  552. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  553. return false;
  554. if (file.isDirectory())
  555. {
  556. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  557. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  558. while (iter.next())
  559. {
  560. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  561. group.addFile (iter.getFile(), -1, shouldCompile);
  562. }
  563. group.sortAlphabetically (false);
  564. }
  565. else if (file.existsAsFile())
  566. {
  567. if (! project.getMainGroup().findItemForFile (file).isValid())
  568. addFileUnchecked (file, insertIndex, shouldCompile);
  569. }
  570. else
  571. {
  572. jassertfalse;
  573. }
  574. return true;
  575. }
  576. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  577. {
  578. Item item (project, ValueTree (Tags::file));
  579. item.initialiseMissingProperties();
  580. item.getName() = file.getFileName();
  581. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  582. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  583. if (canContain (item))
  584. {
  585. item.setFile (file);
  586. addChild (item, insertIndex);
  587. }
  588. }
  589. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  590. {
  591. Item item (project, ValueTree (Tags::file));
  592. item.initialiseMissingProperties();
  593. item.getName() = file.getFileName();
  594. item.getShouldCompileValue() = shouldCompile;
  595. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  596. if (canContain (item))
  597. {
  598. item.setFile (file);
  599. addChild (item, insertIndex);
  600. return true;
  601. }
  602. return false;
  603. }
  604. const Drawable* Project::Item::getIcon() const
  605. {
  606. if (isFile())
  607. {
  608. if (isImageFile())
  609. return StoredSettings::getInstance()->getImageFileIcon();
  610. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  611. }
  612. else if (isMainGroup())
  613. {
  614. return &(project.mainProjectIcon);
  615. }
  616. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  617. }
  618. //==============================================================================
  619. ValueTree Project::getConfigNode()
  620. {
  621. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  622. }
  623. const char* const Project::configFlagDefault = "default";
  624. const char* const Project::configFlagEnabled = "enabled";
  625. const char* const Project::configFlagDisabled = "disabled";
  626. Value Project::getConfigFlag (const String& name)
  627. {
  628. const ValueTree configNode (getConfigNode());
  629. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  630. if (v.getValue().toString().isEmpty())
  631. v = configFlagDefault;
  632. return v;
  633. }
  634. bool Project::isConfigFlagEnabled (const String& name) const
  635. {
  636. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  637. }
  638. void Project::sanitiseConfigFlags()
  639. {
  640. ValueTree configNode (getConfigNode());
  641. for (int i = configNode.getNumProperties(); --i >= 0;)
  642. {
  643. const var value (configNode [configNode.getPropertyName(i)]);
  644. if (value != configFlagEnabled && value != configFlagDisabled)
  645. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  646. }
  647. }
  648. //==============================================================================
  649. ValueTree Project::getModulesNode()
  650. {
  651. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  652. }
  653. bool Project::isModuleEnabled (const String& moduleID) const
  654. {
  655. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  656. for (int i = 0; i < modules.getNumChildren(); ++i)
  657. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  658. return true;
  659. return false;
  660. }
  661. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  662. {
  663. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  664. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  665. }
  666. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  667. {
  668. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  669. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  670. }
  671. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  672. {
  673. if (! isModuleEnabled (moduleID))
  674. {
  675. ValueTree module (Tags::module);
  676. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  677. ValueTree modules (getModulesNode());
  678. modules.addChild (module, -1, getUndoManagerFor (modules));
  679. shouldShowAllModuleFilesInProject (moduleID) = true;
  680. }
  681. if (shouldCopyFilesLocally)
  682. shouldCopyModuleFilesLocally (moduleID) = true;
  683. }
  684. void Project::removeModule (const String& moduleID)
  685. {
  686. ValueTree modules (getModulesNode());
  687. for (int i = 0; i < modules.getNumChildren(); ++i)
  688. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  689. modules.removeChild (i, getUndoManagerFor (modules));
  690. }
  691. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  692. {
  693. for (int i = 0; i < availableModules.modules.size(); ++i)
  694. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  695. modules.add (availableModules.modules.getUnchecked(i)->create());
  696. }
  697. int Project::getNumModules() const
  698. {
  699. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  700. }
  701. String Project::getModuleID (int index) const
  702. {
  703. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  704. }
  705. //==============================================================================
  706. ValueTree Project::getExporters()
  707. {
  708. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  709. if (! exporters.isValid())
  710. {
  711. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  712. exporters = getExporters();
  713. }
  714. return exporters;
  715. }
  716. int Project::getNumExporters()
  717. {
  718. return getExporters().getNumChildren();
  719. }
  720. ProjectExporter* Project::createExporter (int index)
  721. {
  722. jassert (index >= 0 && index < getNumExporters());
  723. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  724. }
  725. void Project::addNewExporter (const String& exporterName)
  726. {
  727. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  728. ValueTree exporters (getExporters());
  729. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  730. }
  731. void Project::deleteExporter (int index)
  732. {
  733. ValueTree exporters (getExporters());
  734. exporters.removeChild (index, getUndoManagerFor (exporters));
  735. }
  736. void Project::createDefaultExporters()
  737. {
  738. ValueTree exporters (getExporters());
  739. exporters.removeAllChildren (getUndoManagerFor (exporters));
  740. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  741. for (int i = 0; i < exporterNames.size(); ++i)
  742. addNewExporter (exporterNames[i]);
  743. }
  744. //==============================================================================
  745. String Project::getFileTemplate (const String& templateName)
  746. {
  747. int dataSize;
  748. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  749. if (data == nullptr)
  750. {
  751. jassertfalse;
  752. return String::empty;
  753. }
  754. return String::fromUTF8 (data, dataSize);
  755. }
  756. //==============================================================================
  757. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  758. Project::ExporterIterator::~ExporterIterator() {}
  759. bool Project::ExporterIterator::next()
  760. {
  761. if (++index >= project.getNumExporters())
  762. return false;
  763. exporter = project.createExporter (index);
  764. if (exporter == nullptr)
  765. {
  766. jassertfalse; // corrupted project file?
  767. return next();
  768. }
  769. return true;
  770. }