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.

986 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. getProjectType().setMissingProjectProperties (*this);
  92. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  93. addDefaultModules (false);
  94. }
  95. void Project::updateOldStyleConfigList()
  96. {
  97. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  98. if (deprecatedConfigsList.isValid())
  99. {
  100. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  101. for (Project::ExporterIterator exporter (*this); exporter.next();)
  102. {
  103. if (exporter->getNumConfigurations() == 0)
  104. {
  105. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  106. if (! exporter->isXcode())
  107. {
  108. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  109. {
  110. ValueTree config (newConfigs.getChild(j));
  111. config.removeProperty (Ids::osxSDK, nullptr);
  112. config.removeProperty (Ids::osxCompatibility, nullptr);
  113. config.removeProperty (Ids::osxArchitecture, nullptr);
  114. }
  115. }
  116. exporter->settings.addChild (newConfigs, 0, nullptr);
  117. }
  118. }
  119. }
  120. }
  121. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  122. {
  123. if (projectRoot.hasProperty (name))
  124. {
  125. for (Project::ExporterIterator exporter (*this); exporter.next();)
  126. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  127. projectRoot.removeProperty (name, nullptr);
  128. }
  129. }
  130. void Project::removeDefunctExporters()
  131. {
  132. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  133. for (;;)
  134. {
  135. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  136. if (oldVC6Exporter.isValid())
  137. exporters.removeChild (oldVC6Exporter, nullptr);
  138. else
  139. break;
  140. }
  141. }
  142. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  143. {
  144. addModule ("juce_core", shouldCopyFilesLocally);
  145. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  146. {
  147. addModule ("juce_events", shouldCopyFilesLocally);
  148. addModule ("juce_graphics", shouldCopyFilesLocally);
  149. addModule ("juce_data_structures", shouldCopyFilesLocally);
  150. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  151. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  152. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  153. addModule ("juce_cryptography", shouldCopyFilesLocally);
  154. addModule ("juce_video", shouldCopyFilesLocally);
  155. addModule ("juce_opengl", shouldCopyFilesLocally);
  156. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  157. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  158. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  159. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  160. }
  161. }
  162. //==============================================================================
  163. const String Project::loadDocument (const File& file)
  164. {
  165. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  166. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  167. return "Not a valid Jucer project!";
  168. ValueTree newTree (ValueTree::fromXml (*xml));
  169. if (! newTree.hasType (Tags::projectRoot))
  170. return "The document contains errors and couldn't be parsed!";
  171. StoredSettings::getInstance()->recentFiles.addFile (file);
  172. StoredSettings::getInstance()->flush();
  173. projectRoot = newTree;
  174. removeDefunctExporters();
  175. setMissingDefaultValues();
  176. return String::empty;
  177. }
  178. const String Project::saveDocument (const File& file)
  179. {
  180. return saveProject (file, true);
  181. }
  182. String Project::saveProject (const File& file, bool showProgressBox)
  183. {
  184. updateProjectSettings();
  185. sanitiseConfigFlags();
  186. StoredSettings::getInstance()->recentFiles.addFile (file);
  187. ProjectSaver saver (*this, file);
  188. return saver.save (showProgressBox);
  189. }
  190. String Project::saveResourcesOnly (const File& file)
  191. {
  192. ProjectSaver saver (*this, file);
  193. return saver.saveResourcesOnly();
  194. }
  195. //==============================================================================
  196. File Project::lastDocumentOpened;
  197. const File Project::getLastDocumentOpened()
  198. {
  199. return lastDocumentOpened;
  200. }
  201. void Project::setLastDocumentOpened (const File& file)
  202. {
  203. lastDocumentOpened = file;
  204. }
  205. //==============================================================================
  206. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  207. {
  208. if (property == Ids::projectType)
  209. setMissingDefaultValues();
  210. changed();
  211. }
  212. void Project::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
  213. {
  214. changed();
  215. }
  216. void Project::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved)
  217. {
  218. changed();
  219. }
  220. void Project::valueTreeChildOrderChanged (ValueTree& parentTree)
  221. {
  222. changed();
  223. }
  224. void Project::valueTreeParentChanged (ValueTree& tree)
  225. {
  226. }
  227. //==============================================================================
  228. File Project::resolveFilename (String filename) const
  229. {
  230. if (filename.isEmpty())
  231. return File::nonexistent;
  232. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  233. .replaceCharacter ('\\', '/');
  234. if (FileHelpers::isAbsolutePath (filename))
  235. return File::createFileWithoutCheckingPath (filename); // (avoid assertions for windows-style paths)
  236. return getFile().getSiblingFile (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 (getProjectName(), "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 Mac 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. props.setPreferredHeight (22);
  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 [ComponentBuilder::idProperty]; }
  341. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, 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() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  360. Project::Item Project::Item::findItemWithID (const String& targetId) const
  361. {
  362. if (state [ComponentBuilder::idProperty] == 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. OpenDocumentManager::getInstance()->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 (ComponentBuilder::idProperty))
  478. setID (createAlphaNumericUID());
  479. if (isFile())
  480. {
  481. state.setProperty (Ids::name, getFile().getFileName(), 0);
  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. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  556. jassert (canContain (group));
  557. addChild (group, insertIndex);
  558. return group;
  559. }
  560. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  561. {
  562. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  563. return false;
  564. if (file.isDirectory())
  565. {
  566. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  567. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  568. while (iter.next())
  569. {
  570. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  571. group.addFile (iter.getFile(), -1, shouldCompile);
  572. }
  573. group.sortAlphabetically (false);
  574. }
  575. else if (file.existsAsFile())
  576. {
  577. if (! project.getMainGroup().findItemForFile (file).isValid())
  578. addFileUnchecked (file, insertIndex, shouldCompile);
  579. }
  580. else
  581. {
  582. jassertfalse;
  583. }
  584. return true;
  585. }
  586. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  587. {
  588. Item item (project, ValueTree (Tags::file));
  589. item.initialiseMissingProperties();
  590. item.getNameValue() = file.getFileName();
  591. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  592. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  593. if (canContain (item))
  594. {
  595. item.setFile (file);
  596. addChild (item, insertIndex);
  597. }
  598. }
  599. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  600. {
  601. Item item (project, ValueTree (Tags::file));
  602. item.initialiseMissingProperties();
  603. item.getNameValue() = file.getFileName();
  604. item.getShouldCompileValue() = shouldCompile;
  605. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  606. if (canContain (item))
  607. {
  608. item.setFile (file);
  609. addChild (item, insertIndex);
  610. return true;
  611. }
  612. return false;
  613. }
  614. const Drawable* Project::Item::getIcon() const
  615. {
  616. if (isFile())
  617. {
  618. if (isImageFile())
  619. return StoredSettings::getInstance()->getImageFileIcon();
  620. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  621. }
  622. else if (isMainGroup())
  623. {
  624. return &(project.mainProjectIcon);
  625. }
  626. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  627. }
  628. //==============================================================================
  629. ValueTree Project::getConfigNode()
  630. {
  631. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  632. }
  633. const char* const Project::configFlagDefault = "default";
  634. const char* const Project::configFlagEnabled = "enabled";
  635. const char* const Project::configFlagDisabled = "disabled";
  636. Value Project::getConfigFlag (const String& name)
  637. {
  638. ValueTree configNode (getConfigNode());
  639. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  640. if (v.getValue().toString().isEmpty())
  641. v = configFlagDefault;
  642. return v;
  643. }
  644. bool Project::isConfigFlagEnabled (const String& name) const
  645. {
  646. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  647. }
  648. void Project::sanitiseConfigFlags()
  649. {
  650. ValueTree configNode (getConfigNode());
  651. for (int i = configNode.getNumProperties(); --i >= 0;)
  652. {
  653. const var value (configNode [configNode.getPropertyName(i)]);
  654. if (value != configFlagEnabled && value != configFlagDisabled)
  655. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  656. }
  657. }
  658. //==============================================================================
  659. ValueTree Project::getModulesNode()
  660. {
  661. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  662. }
  663. bool Project::isModuleEnabled (const String& moduleID) const
  664. {
  665. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  666. for (int i = 0; i < modules.getNumChildren(); ++i)
  667. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  668. return true;
  669. return false;
  670. }
  671. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  672. {
  673. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  674. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  675. }
  676. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  677. {
  678. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  679. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  680. }
  681. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  682. {
  683. if (! isModuleEnabled (moduleID))
  684. {
  685. ValueTree module (Tags::module);
  686. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  687. ValueTree modules (getModulesNode());
  688. modules.addChild (module, -1, getUndoManagerFor (modules));
  689. shouldShowAllModuleFilesInProject (moduleID) = true;
  690. }
  691. if (shouldCopyFilesLocally)
  692. shouldCopyModuleFilesLocally (moduleID) = true;
  693. }
  694. void Project::removeModule (const String& moduleID)
  695. {
  696. ValueTree modules (getModulesNode());
  697. for (int i = 0; i < modules.getNumChildren(); ++i)
  698. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  699. modules.removeChild (i, getUndoManagerFor (modules));
  700. }
  701. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  702. {
  703. for (int i = 0; i < availableModules.modules.size(); ++i)
  704. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  705. modules.add (availableModules.modules.getUnchecked(i)->create());
  706. }
  707. int Project::getNumModules() const
  708. {
  709. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  710. }
  711. String Project::getModuleID (int index) const
  712. {
  713. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  714. }
  715. //==============================================================================
  716. ValueTree Project::getExporters()
  717. {
  718. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  719. if (! exporters.isValid())
  720. {
  721. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  722. exporters = getExporters();
  723. }
  724. return exporters;
  725. }
  726. int Project::getNumExporters()
  727. {
  728. return getExporters().getNumChildren();
  729. }
  730. ProjectExporter* Project::createExporter (int index)
  731. {
  732. jassert (index >= 0 && index < getNumExporters());
  733. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  734. }
  735. void Project::addNewExporter (const String& exporterName)
  736. {
  737. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  738. ValueTree exporters (getExporters());
  739. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  740. }
  741. void Project::deleteExporter (int index)
  742. {
  743. ValueTree exporters (getExporters());
  744. exporters.removeChild (index, getUndoManagerFor (exporters));
  745. }
  746. void Project::createDefaultExporters()
  747. {
  748. ValueTree exporters (getExporters());
  749. exporters.removeAllChildren (getUndoManagerFor (exporters));
  750. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  751. for (int i = 0; i < exporterNames.size(); ++i)
  752. addNewExporter (exporterNames[i]);
  753. }
  754. //==============================================================================
  755. String Project::getFileTemplate (const String& templateName)
  756. {
  757. int dataSize;
  758. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  759. if (data == nullptr)
  760. {
  761. jassertfalse;
  762. return String::empty;
  763. }
  764. return String::fromUTF8 (data, dataSize);
  765. }
  766. //==============================================================================
  767. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  768. Project::ExporterIterator::~ExporterIterator() {}
  769. bool Project::ExporterIterator::next()
  770. {
  771. if (++index >= project.getNumExporters())
  772. return false;
  773. exporter = project.createExporter (index);
  774. if (exporter == nullptr)
  775. {
  776. jassertfalse; // corrupted project file?
  777. return next();
  778. }
  779. return true;
  780. }