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.

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