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.

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