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.

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