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.

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