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.

969 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. #include "../Application/jucer_Application.h"
  24. //==============================================================================
  25. namespace Tags
  26. {
  27. const Identifier projectRoot ("JUCERPROJECT");
  28. const Identifier projectMainGroup ("MAINGROUP");
  29. const Identifier group ("GROUP");
  30. const Identifier file ("FILE");
  31. const Identifier exporters ("EXPORTFORMATS");
  32. const Identifier configGroup ("JUCEOPTIONS");
  33. const Identifier modulesGroup ("MODULES");
  34. const Identifier module ("MODULE");
  35. }
  36. const char* Project::projectFileExtension = ".jucer";
  37. //==============================================================================
  38. Project::Project (const File& file_)
  39. : FileBasedDocument (projectFileExtension,
  40. String ("*") + projectFileExtension,
  41. "Choose a Jucer project to load",
  42. "Save Jucer project"),
  43. projectRoot (Tags::projectRoot)
  44. {
  45. setFile (file_);
  46. removeDefunctExporters();
  47. setMissingDefaultValues();
  48. setChangedFlag (false);
  49. projectRoot.addListener (this);
  50. }
  51. Project::~Project()
  52. {
  53. projectRoot.removeListener (this);
  54. JucerApplication::getApp().openDocumentManager.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. String Project::getTitle() const
  63. {
  64. return projectRoot.getChildWithName (Tags::projectMainGroup) [Ids::name];
  65. }
  66. String Project::getDocumentTitle()
  67. {
  68. return getTitle();
  69. }
  70. void Project::updateProjectSettings()
  71. {
  72. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  73. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  74. }
  75. void Project::setMissingDefaultValues()
  76. {
  77. if (! projectRoot.hasProperty (ComponentBuilder::idProperty))
  78. projectRoot.setProperty (ComponentBuilder::idProperty, createAlphaNumericUID(), nullptr);
  79. // Create main file group if missing
  80. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  81. {
  82. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  83. projectRoot.addChild (mainGroup.state, 0, 0);
  84. }
  85. getMainGroup().initialiseMissingProperties();
  86. if (getDocumentTitle().isEmpty())
  87. setTitle ("JUCE Project");
  88. if (! projectRoot.hasProperty (Ids::projectType))
  89. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  90. if (! projectRoot.hasProperty (Ids::version))
  91. getVersionValue() = "1.0.0";
  92. updateOldStyleConfigList();
  93. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  94. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  95. getProjectType().setMissingProjectProperties (*this);
  96. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  97. addDefaultModules (false);
  98. }
  99. void Project::updateOldStyleConfigList()
  100. {
  101. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  102. if (deprecatedConfigsList.isValid())
  103. {
  104. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  105. for (Project::ExporterIterator exporter (*this); exporter.next();)
  106. {
  107. if (exporter->getNumConfigurations() == 0)
  108. {
  109. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  110. if (! exporter->isXcode())
  111. {
  112. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  113. {
  114. ValueTree config (newConfigs.getChild(j));
  115. config.removeProperty (Ids::osxSDK, nullptr);
  116. config.removeProperty (Ids::osxCompatibility, nullptr);
  117. config.removeProperty (Ids::osxArchitecture, nullptr);
  118. }
  119. }
  120. exporter->settings.addChild (newConfigs, 0, nullptr);
  121. }
  122. }
  123. }
  124. }
  125. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  126. {
  127. if (projectRoot.hasProperty (name))
  128. {
  129. for (Project::ExporterIterator exporter (*this); exporter.next();)
  130. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  131. projectRoot.removeProperty (name, nullptr);
  132. }
  133. }
  134. void Project::removeDefunctExporters()
  135. {
  136. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  137. for (;;)
  138. {
  139. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  140. if (oldVC6Exporter.isValid())
  141. exporters.removeChild (oldVC6Exporter, nullptr);
  142. else
  143. break;
  144. }
  145. }
  146. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  147. {
  148. addModule ("juce_core", shouldCopyFilesLocally);
  149. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  150. {
  151. addModule ("juce_events", shouldCopyFilesLocally);
  152. addModule ("juce_graphics", shouldCopyFilesLocally);
  153. addModule ("juce_data_structures", shouldCopyFilesLocally);
  154. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  155. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  156. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  157. addModule ("juce_cryptography", shouldCopyFilesLocally);
  158. addModule ("juce_video", shouldCopyFilesLocally);
  159. addModule ("juce_opengl", shouldCopyFilesLocally);
  160. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  161. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  162. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  163. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  164. }
  165. }
  166. bool Project::isAudioPluginModuleMissing() const
  167. {
  168. return getProjectType().isAudioPlugin()
  169. && ! isModuleEnabled ("juce_audio_plugin_client");
  170. }
  171. //==============================================================================
  172. static void registerRecentFile (const File& file)
  173. {
  174. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  175. getAppSettings().recentFiles.addFile (file);
  176. getAppSettings().flush();
  177. }
  178. Result Project::loadDocument (const File& file)
  179. {
  180. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  181. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  182. return Result::fail ("Not a valid Jucer project!");
  183. ValueTree newTree (ValueTree::fromXml (*xml));
  184. if (! newTree.hasType (Tags::projectRoot))
  185. return Result::fail ("The document contains errors and couldn't be parsed!");
  186. registerRecentFile (file);
  187. projectRoot = newTree;
  188. removeDefunctExporters();
  189. setMissingDefaultValues();
  190. setChangedFlag (false);
  191. return Result::ok();
  192. }
  193. Result Project::saveDocument (const File& file)
  194. {
  195. return saveProject (file, false);
  196. }
  197. Result Project::saveProject (const File& file, bool isCommandLineApp)
  198. {
  199. updateProjectSettings();
  200. sanitiseConfigFlags();
  201. if (! isCommandLineApp)
  202. registerRecentFile (file);
  203. ProjectSaver saver (*this, file);
  204. return saver.save (! isCommandLineApp);
  205. }
  206. Result Project::saveResourcesOnly (const File& file)
  207. {
  208. ProjectSaver saver (*this, file);
  209. return saver.saveResourcesOnly();
  210. }
  211. //==============================================================================
  212. static File lastDocumentOpened;
  213. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  214. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  215. //==============================================================================
  216. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  217. {
  218. if (property == Ids::projectType)
  219. setMissingDefaultValues();
  220. changed();
  221. }
  222. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  223. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  224. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  225. void Project::valueTreeParentChanged (ValueTree&) {}
  226. //==============================================================================
  227. File Project::resolveFilename (String filename) const
  228. {
  229. if (filename.isEmpty())
  230. return File::nonexistent;
  231. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  232. .replaceCharacter ('\\', '/');
  233. if (FileHelpers::isAbsolutePath (filename))
  234. return File::createFileWithoutCheckingPath (filename); // (avoid assertions for windows-style paths)
  235. return getFile().getSiblingFile (filename);
  236. }
  237. String Project::getRelativePathForFile (const File& file) const
  238. {
  239. String filename (file.getFullPathName());
  240. File relativePathBase (getFile().getParentDirectory());
  241. String p1 (relativePathBase.getFullPathName());
  242. String p2 (file.getFullPathName());
  243. while (p1.startsWithChar (File::separator))
  244. p1 = p1.substring (1);
  245. while (p2.startsWithChar (File::separator))
  246. p2 = p2.substring (1);
  247. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  248. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  249. {
  250. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  251. }
  252. return filename;
  253. }
  254. //==============================================================================
  255. const ProjectType& Project::getProjectType() const
  256. {
  257. const ProjectType* type = ProjectType::findType (getProjectTypeString());
  258. jassert (type != nullptr);
  259. if (type == nullptr)
  260. {
  261. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  262. jassert (type != nullptr);
  263. }
  264. return *type;
  265. }
  266. //==============================================================================
  267. void Project::createPropertyEditors (PropertyListBuilder& props)
  268. {
  269. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  270. "The name of the project.");
  271. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  272. "The project's version number, This should be in the format major.minor.point");
  273. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  274. "Your company name, which will be added to the properties of the binary where possible");
  275. {
  276. StringArray projectTypeNames;
  277. Array<var> projectTypeCodes;
  278. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  279. for (int i = 0; i < types.size(); ++i)
  280. {
  281. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  282. projectTypeCodes.add (types.getUnchecked(i)->getType());
  283. }
  284. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  285. }
  286. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  287. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  288. getProjectType().createPropertyEditors (*this, props);
  289. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  290. "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.");
  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. || (newFile.exists() && ! oldFile.exists()))
  422. {
  423. setFile (newFile);
  424. JucerApplication::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  425. return true;
  426. }
  427. return false;
  428. }
  429. bool Project::Item::containsChildForFile (const RelativePath& file) const
  430. {
  431. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  432. }
  433. Project::Item Project::Item::findItemForFile (const File& file) const
  434. {
  435. if (getFile() == file)
  436. return *this;
  437. if (isGroup())
  438. {
  439. for (int i = getNumChildren(); --i >= 0;)
  440. {
  441. Item found (getChild(i).findItemForFile (file));
  442. if (found.isValid())
  443. return found;
  444. }
  445. }
  446. return Item (project, ValueTree::invalid);
  447. }
  448. File Project::Item::determineGroupFolder() const
  449. {
  450. jassert (isGroup());
  451. File f;
  452. for (int i = 0; i < getNumChildren(); ++i)
  453. {
  454. f = getChild(i).getFile();
  455. if (f.exists())
  456. return f.getParentDirectory();
  457. }
  458. Item parent (getParent());
  459. if (parent != *this)
  460. {
  461. f = parent.determineGroupFolder();
  462. if (f.getChildFile (getName()).isDirectory())
  463. f = f.getChildFile (getName());
  464. }
  465. else
  466. {
  467. f = project.getFile().getParentDirectory();
  468. if (f.getChildFile ("Source").isDirectory())
  469. f = f.getChildFile ("Source");
  470. }
  471. return f;
  472. }
  473. void Project::Item::initialiseMissingProperties()
  474. {
  475. if (! state.hasProperty (ComponentBuilder::idProperty))
  476. setID (createAlphaNumericUID());
  477. if (isFile())
  478. {
  479. state.setProperty (Ids::name, getFile().getFileName(), 0);
  480. }
  481. else if (isGroup())
  482. {
  483. for (int i = getNumChildren(); --i >= 0;)
  484. getChild(i).initialiseMissingProperties();
  485. }
  486. }
  487. Value Project::Item::getNameValue()
  488. {
  489. return state.getPropertyAsValue (Ids::name, getUndoManager());
  490. }
  491. String Project::Item::getName() const
  492. {
  493. return state [Ids::name];
  494. }
  495. void Project::Item::addChild (const Item& newChild, int insertIndex)
  496. {
  497. state.addChild (newChild.state, insertIndex, getUndoManager());
  498. }
  499. void Project::Item::removeItemFromProject()
  500. {
  501. state.getParent().removeChild (state, getUndoManager());
  502. }
  503. Project::Item Project::Item::getParent() const
  504. {
  505. if (isMainGroup() || ! isGroup())
  506. return *this;
  507. return Item (project, state.getParent());
  508. }
  509. struct ItemSorter
  510. {
  511. static int compareElements (const ValueTree& first, const ValueTree& second)
  512. {
  513. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  514. }
  515. };
  516. struct ItemSorterWithGroupsAtStart
  517. {
  518. static int compareElements (const ValueTree& first, const ValueTree& second)
  519. {
  520. const bool firstIsGroup = first.hasType (Tags::group);
  521. const bool secondIsGroup = second.hasType (Tags::group);
  522. if (firstIsGroup == secondIsGroup)
  523. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  524. else
  525. return firstIsGroup ? -1 : 1;
  526. }
  527. };
  528. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  529. {
  530. if (keepGroupsAtStart)
  531. {
  532. ItemSorterWithGroupsAtStart sorter;
  533. state.sort (sorter, getUndoManager(), true);
  534. }
  535. else
  536. {
  537. ItemSorter sorter;
  538. state.sort (sorter, getUndoManager(), true);
  539. }
  540. }
  541. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  542. {
  543. for (int i = state.getNumChildren(); --i >= 0;)
  544. {
  545. const ValueTree child (state.getChild (i));
  546. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  547. return Item (project, child);
  548. }
  549. return addNewSubGroup (name, -1);
  550. }
  551. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  552. {
  553. String newID (createGUID (getID() + name + String (getNumChildren())));
  554. int n = 0;
  555. while (findItemWithID (newID).isValid())
  556. newID = createGUID (newID + String (++n));
  557. Item group (createGroup (project, name, newID));
  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. Icon Project::Item::getIcon() const
  617. {
  618. const Icons& icons = getIcons();
  619. if (isFile())
  620. {
  621. if (isImageFile())
  622. return Icon (icons.imageDoc, Colours::blue);
  623. return Icon (icons.document, Colours::yellow);
  624. }
  625. else if (isMainGroup())
  626. {
  627. return Icon (icons.juceLogo, Colours::orange);
  628. }
  629. return Icon (icons.folder, Colours::darkgrey);
  630. }
  631. //==============================================================================
  632. ValueTree Project::getConfigNode()
  633. {
  634. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  635. }
  636. const char* const Project::configFlagDefault = "default";
  637. const char* const Project::configFlagEnabled = "enabled";
  638. const char* const Project::configFlagDisabled = "disabled";
  639. Value Project::getConfigFlag (const String& name)
  640. {
  641. ValueTree configNode (getConfigNode());
  642. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  643. if (v.getValue().toString().isEmpty())
  644. v = configFlagDefault;
  645. return v;
  646. }
  647. bool Project::isConfigFlagEnabled (const String& name) const
  648. {
  649. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  650. }
  651. void Project::sanitiseConfigFlags()
  652. {
  653. ValueTree configNode (getConfigNode());
  654. for (int i = configNode.getNumProperties(); --i >= 0;)
  655. {
  656. const var value (configNode [configNode.getPropertyName(i)]);
  657. if (value != configFlagEnabled && value != configFlagDisabled)
  658. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  659. }
  660. }
  661. //==============================================================================
  662. ValueTree Project::getModulesNode()
  663. {
  664. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  665. }
  666. bool Project::isModuleEnabled (const String& moduleID) const
  667. {
  668. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  669. for (int i = 0; i < modules.getNumChildren(); ++i)
  670. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  671. return true;
  672. return false;
  673. }
  674. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  675. {
  676. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  677. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  678. }
  679. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  680. {
  681. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  682. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  683. }
  684. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  685. {
  686. if (! isModuleEnabled (moduleID))
  687. {
  688. ValueTree module (Tags::module);
  689. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  690. ValueTree modules (getModulesNode());
  691. modules.addChild (module, -1, getUndoManagerFor (modules));
  692. shouldShowAllModuleFilesInProject (moduleID) = true;
  693. }
  694. if (shouldCopyFilesLocally)
  695. shouldCopyModuleFilesLocally (moduleID) = true;
  696. }
  697. void Project::removeModule (const String& moduleID)
  698. {
  699. ValueTree modules (getModulesNode());
  700. for (int i = 0; i < modules.getNumChildren(); ++i)
  701. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  702. modules.removeChild (i, getUndoManagerFor (modules));
  703. }
  704. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  705. {
  706. for (int i = 0; i < availableModules.modules.size(); ++i)
  707. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  708. modules.add (availableModules.modules.getUnchecked(i)->create());
  709. }
  710. int Project::getNumModules() const
  711. {
  712. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  713. }
  714. String Project::getModuleID (int index) const
  715. {
  716. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  717. }
  718. //==============================================================================
  719. ValueTree Project::getExporters()
  720. {
  721. return projectRoot.getOrCreateChildWithName (Tags::exporters, nullptr);
  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::createExporterForCurrentPlatform()
  739. {
  740. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  741. }
  742. //==============================================================================
  743. String Project::getFileTemplate (const String& templateName)
  744. {
  745. int dataSize;
  746. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  747. if (data == nullptr)
  748. {
  749. jassertfalse;
  750. return String::empty;
  751. }
  752. return String::fromUTF8 (data, dataSize);
  753. }
  754. //==============================================================================
  755. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  756. Project::ExporterIterator::~ExporterIterator() {}
  757. bool Project::ExporterIterator::next()
  758. {
  759. if (++index >= project.getNumExporters())
  760. return false;
  761. exporter = project.createExporter (index);
  762. if (exporter == nullptr)
  763. {
  764. jassertfalse; // corrupted project file?
  765. return next();
  766. }
  767. return true;
  768. }