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.

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