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.

968 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. return Result::ok();
  191. }
  192. Result Project::saveDocument (const File& file)
  193. {
  194. return saveProject (file, false);
  195. }
  196. Result Project::saveProject (const File& file, bool isCommandLineApp)
  197. {
  198. updateProjectSettings();
  199. sanitiseConfigFlags();
  200. if (! isCommandLineApp)
  201. registerRecentFile (file);
  202. ProjectSaver saver (*this, file);
  203. return saver.save (! isCommandLineApp);
  204. }
  205. Result Project::saveResourcesOnly (const File& file)
  206. {
  207. ProjectSaver saver (*this, file);
  208. return saver.saveResourcesOnly();
  209. }
  210. //==============================================================================
  211. static File lastDocumentOpened;
  212. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  213. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  214. //==============================================================================
  215. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  216. {
  217. if (property == Ids::projectType)
  218. setMissingDefaultValues();
  219. changed();
  220. }
  221. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  222. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  223. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  224. void Project::valueTreeParentChanged (ValueTree&) {}
  225. //==============================================================================
  226. File Project::resolveFilename (String filename) const
  227. {
  228. if (filename.isEmpty())
  229. return File::nonexistent;
  230. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  231. .replaceCharacter ('\\', '/');
  232. if (FileHelpers::isAbsolutePath (filename))
  233. return File::createFileWithoutCheckingPath (filename); // (avoid assertions for windows-style paths)
  234. return getFile().getSiblingFile (filename);
  235. }
  236. String Project::getRelativePathForFile (const File& file) const
  237. {
  238. String filename (file.getFullPathName());
  239. File relativePathBase (getFile().getParentDirectory());
  240. String p1 (relativePathBase.getFullPathName());
  241. String p2 (file.getFullPathName());
  242. while (p1.startsWithChar (File::separator))
  243. p1 = p1.substring (1);
  244. while (p2.startsWithChar (File::separator))
  245. p2 = p2.substring (1);
  246. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  247. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  248. {
  249. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  250. }
  251. return filename;
  252. }
  253. //==============================================================================
  254. const ProjectType& Project::getProjectType() const
  255. {
  256. const ProjectType* type = ProjectType::findType (getProjectTypeString());
  257. jassert (type != nullptr);
  258. if (type == nullptr)
  259. {
  260. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  261. jassert (type != nullptr);
  262. }
  263. return *type;
  264. }
  265. //==============================================================================
  266. void Project::createPropertyEditors (PropertyListBuilder& props)
  267. {
  268. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  269. "The name of the project.");
  270. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  271. "The project's version number, This should be in the format major.minor.point");
  272. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  273. "Your company name, which will be added to the properties of the binary where possible");
  274. {
  275. StringArray projectTypeNames;
  276. Array<var> projectTypeCodes;
  277. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  278. for (int i = 0; i < types.size(); ++i)
  279. {
  280. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  281. projectTypeCodes.add (types.getUnchecked(i)->getType());
  282. }
  283. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  284. }
  285. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  286. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  287. getProjectType().createPropertyEditors (*this, props);
  288. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  289. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  290. }
  291. String Project::getVersionAsHex() const
  292. {
  293. StringArray configs;
  294. configs.addTokens (getVersionString(), ",.", String::empty);
  295. configs.trim();
  296. configs.removeEmptyStrings();
  297. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  298. if (configs.size() >= 4)
  299. value = (value << 8) + configs[3].getIntValue();
  300. return "0x" + String::toHexString (value);
  301. }
  302. StringPairArray Project::getPreprocessorDefs() const
  303. {
  304. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  305. }
  306. //==============================================================================
  307. Project::Item Project::getMainGroup()
  308. {
  309. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  310. }
  311. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  312. {
  313. if (item.isImageFile())
  314. {
  315. found.add (new Project::Item (item));
  316. }
  317. else if (item.isGroup())
  318. {
  319. for (int i = 0; i < item.getNumChildren(); ++i)
  320. findImages (item.getChild (i), found);
  321. }
  322. }
  323. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  324. {
  325. findImages (getMainGroup(), items);
  326. }
  327. //==============================================================================
  328. Project::Item::Item (Project& project_, const ValueTree& state_)
  329. : project (project_), state (state_)
  330. {
  331. }
  332. Project::Item::Item (const Item& other)
  333. : project (other.project), state (other.state)
  334. {
  335. }
  336. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  337. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  338. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  339. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  340. Image Project::Item::loadAsImageFile() const
  341. {
  342. return isValid() ? ImageCache::getFromFile (getFile())
  343. : Image::null;
  344. }
  345. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  346. {
  347. Item group (project, ValueTree (Tags::group));
  348. group.setID (uid);
  349. group.initialiseMissingProperties();
  350. group.getNameValue() = name;
  351. return group;
  352. }
  353. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  354. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  355. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  356. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  357. Project::Item Project::Item::findItemWithID (const String& targetId) const
  358. {
  359. if (state [ComponentBuilder::idProperty] == targetId)
  360. return *this;
  361. if (isGroup())
  362. {
  363. for (int i = getNumChildren(); --i >= 0;)
  364. {
  365. Item found (getChild(i).findItemWithID (targetId));
  366. if (found.isValid())
  367. return found;
  368. }
  369. }
  370. return Item (project, ValueTree::invalid);
  371. }
  372. bool Project::Item::canContain (const Item& child) const
  373. {
  374. if (isFile())
  375. return false;
  376. if (isGroup())
  377. return child.isFile() || child.isGroup();
  378. jassertfalse
  379. return false;
  380. }
  381. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  382. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  383. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  384. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  385. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  386. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  387. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  388. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  389. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  390. String Project::Item::getFilePath() const
  391. {
  392. if (isFile())
  393. return state [Ids::file].toString();
  394. else
  395. return String::empty;
  396. }
  397. File Project::Item::getFile() const
  398. {
  399. if (isFile())
  400. return project.resolveFilename (state [Ids::file].toString());
  401. else
  402. return File::nonexistent;
  403. }
  404. void Project::Item::setFile (const File& file)
  405. {
  406. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  407. jassert (getFile() == file);
  408. }
  409. void Project::Item::setFile (const RelativePath& file)
  410. {
  411. jassert (file.getRoot() == RelativePath::projectFolder);
  412. jassert (isFile());
  413. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  414. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  415. }
  416. bool Project::Item::renameFile (const File& newFile)
  417. {
  418. const File oldFile (getFile());
  419. if (oldFile.moveFileTo (newFile)
  420. || (newFile.exists() && ! oldFile.exists()))
  421. {
  422. setFile (newFile);
  423. JucerApplication::getApp()->openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  424. return true;
  425. }
  426. return false;
  427. }
  428. bool Project::Item::containsChildForFile (const RelativePath& file) const
  429. {
  430. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  431. }
  432. Project::Item Project::Item::findItemForFile (const File& file) const
  433. {
  434. if (getFile() == file)
  435. return *this;
  436. if (isGroup())
  437. {
  438. for (int i = getNumChildren(); --i >= 0;)
  439. {
  440. Item found (getChild(i).findItemForFile (file));
  441. if (found.isValid())
  442. return found;
  443. }
  444. }
  445. return Item (project, ValueTree::invalid);
  446. }
  447. File Project::Item::determineGroupFolder() const
  448. {
  449. jassert (isGroup());
  450. File f;
  451. for (int i = 0; i < getNumChildren(); ++i)
  452. {
  453. f = getChild(i).getFile();
  454. if (f.exists())
  455. return f.getParentDirectory();
  456. }
  457. Item parent (getParent());
  458. if (parent != *this)
  459. {
  460. f = parent.determineGroupFolder();
  461. if (f.getChildFile (getName()).isDirectory())
  462. f = f.getChildFile (getName());
  463. }
  464. else
  465. {
  466. f = project.getFile().getParentDirectory();
  467. if (f.getChildFile ("Source").isDirectory())
  468. f = f.getChildFile ("Source");
  469. }
  470. return f;
  471. }
  472. void Project::Item::initialiseMissingProperties()
  473. {
  474. if (! state.hasProperty (ComponentBuilder::idProperty))
  475. setID (createAlphaNumericUID());
  476. if (isFile())
  477. {
  478. state.setProperty (Ids::name, getFile().getFileName(), 0);
  479. }
  480. else if (isGroup())
  481. {
  482. for (int i = getNumChildren(); --i >= 0;)
  483. getChild(i).initialiseMissingProperties();
  484. }
  485. }
  486. Value Project::Item::getNameValue()
  487. {
  488. return state.getPropertyAsValue (Ids::name, getUndoManager());
  489. }
  490. String Project::Item::getName() const
  491. {
  492. return state [Ids::name];
  493. }
  494. void Project::Item::addChild (const Item& newChild, int insertIndex)
  495. {
  496. state.addChild (newChild.state, insertIndex, getUndoManager());
  497. }
  498. void Project::Item::removeItemFromProject()
  499. {
  500. state.getParent().removeChild (state, getUndoManager());
  501. }
  502. Project::Item Project::Item::getParent() const
  503. {
  504. if (isMainGroup() || ! isGroup())
  505. return *this;
  506. return Item (project, state.getParent());
  507. }
  508. struct ItemSorter
  509. {
  510. static int compareElements (const ValueTree& first, const ValueTree& second)
  511. {
  512. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  513. }
  514. };
  515. struct ItemSorterWithGroupsAtStart
  516. {
  517. static int compareElements (const ValueTree& first, const ValueTree& second)
  518. {
  519. const bool firstIsGroup = first.hasType (Tags::group);
  520. const bool secondIsGroup = second.hasType (Tags::group);
  521. if (firstIsGroup == secondIsGroup)
  522. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  523. else
  524. return firstIsGroup ? -1 : 1;
  525. }
  526. };
  527. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  528. {
  529. if (keepGroupsAtStart)
  530. {
  531. ItemSorterWithGroupsAtStart sorter;
  532. state.sort (sorter, getUndoManager(), true);
  533. }
  534. else
  535. {
  536. ItemSorter sorter;
  537. state.sort (sorter, getUndoManager(), true);
  538. }
  539. }
  540. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  541. {
  542. for (int i = state.getNumChildren(); --i >= 0;)
  543. {
  544. const ValueTree child (state.getChild (i));
  545. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  546. return Item (project, child);
  547. }
  548. return addNewSubGroup (name, -1);
  549. }
  550. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  551. {
  552. String newID (createGUID (getID() + name + String (getNumChildren())));
  553. int n = 0;
  554. while (findItemWithID (newID).isValid())
  555. newID = createGUID (newID + String (++n));
  556. Item group (createGroup (project, name, newID));
  557. jassert (canContain (group));
  558. addChild (group, insertIndex);
  559. return group;
  560. }
  561. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  562. {
  563. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  564. return false;
  565. if (file.isDirectory())
  566. {
  567. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  568. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  569. while (iter.next())
  570. {
  571. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  572. group.addFile (iter.getFile(), -1, shouldCompile);
  573. }
  574. group.sortAlphabetically (false);
  575. }
  576. else if (file.existsAsFile())
  577. {
  578. if (! project.getMainGroup().findItemForFile (file).isValid())
  579. addFileUnchecked (file, insertIndex, shouldCompile);
  580. }
  581. else
  582. {
  583. jassertfalse;
  584. }
  585. return true;
  586. }
  587. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  588. {
  589. Item item (project, ValueTree (Tags::file));
  590. item.initialiseMissingProperties();
  591. item.getNameValue() = file.getFileName();
  592. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  593. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  594. if (canContain (item))
  595. {
  596. item.setFile (file);
  597. addChild (item, insertIndex);
  598. }
  599. }
  600. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  601. {
  602. Item item (project, ValueTree (Tags::file));
  603. item.initialiseMissingProperties();
  604. item.getNameValue() = file.getFileName();
  605. item.getShouldCompileValue() = shouldCompile;
  606. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  607. if (canContain (item))
  608. {
  609. item.setFile (file);
  610. addChild (item, insertIndex);
  611. return true;
  612. }
  613. return false;
  614. }
  615. const Drawable* Project::Item::getIcon() const
  616. {
  617. const Icons& icons = getIcons();
  618. if (isFile())
  619. {
  620. if (isImageFile())
  621. return icons.imageDoc;
  622. return icons.document;
  623. }
  624. else if (isMainGroup())
  625. {
  626. return icons.juceLogo;
  627. }
  628. return icons.folder;
  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. return projectRoot.getOrCreateChildWithName (Tags::exporters, nullptr);
  721. }
  722. int Project::getNumExporters()
  723. {
  724. return getExporters().getNumChildren();
  725. }
  726. ProjectExporter* Project::createExporter (int index)
  727. {
  728. jassert (index >= 0 && index < getNumExporters());
  729. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  730. }
  731. void Project::addNewExporter (const String& exporterName)
  732. {
  733. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  734. ValueTree exporters (getExporters());
  735. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  736. }
  737. void Project::createExporterForCurrentPlatform()
  738. {
  739. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  740. }
  741. //==============================================================================
  742. String Project::getFileTemplate (const String& templateName)
  743. {
  744. int dataSize;
  745. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  746. if (data == nullptr)
  747. {
  748. jassertfalse;
  749. return String::empty;
  750. }
  751. return String::fromUTF8 (data, dataSize);
  752. }
  753. //==============================================================================
  754. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  755. Project::ExporterIterator::~ExporterIterator() {}
  756. bool Project::ExporterIterator::next()
  757. {
  758. if (++index >= project.getNumExporters())
  759. return false;
  760. exporter = project.createExporter (index);
  761. if (exporter == nullptr)
  762. {
  763. jassertfalse; // corrupted project file?
  764. return next();
  765. }
  766. return true;
  767. }