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.

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