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.

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