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.

989 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. || (newFile.exists() && ! oldFile.exists()))
  426. {
  427. setFile (newFile);
  428. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  429. return true;
  430. }
  431. return false;
  432. }
  433. bool Project::Item::containsChildForFile (const RelativePath& file) const
  434. {
  435. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  436. }
  437. Project::Item Project::Item::findItemForFile (const File& file) const
  438. {
  439. if (getFile() == file)
  440. return *this;
  441. if (isGroup())
  442. {
  443. for (int i = getNumChildren(); --i >= 0;)
  444. {
  445. Item found (getChild(i).findItemForFile (file));
  446. if (found.isValid())
  447. return found;
  448. }
  449. }
  450. return Item (project, ValueTree::invalid);
  451. }
  452. File Project::Item::determineGroupFolder() const
  453. {
  454. jassert (isGroup());
  455. File f;
  456. for (int i = 0; i < getNumChildren(); ++i)
  457. {
  458. f = getChild(i).getFile();
  459. if (f.exists())
  460. return f.getParentDirectory();
  461. }
  462. Item parent (getParent());
  463. if (parent != *this)
  464. {
  465. f = parent.determineGroupFolder();
  466. if (f.getChildFile (getName()).isDirectory())
  467. f = f.getChildFile (getName());
  468. }
  469. else
  470. {
  471. f = project.getFile().getParentDirectory();
  472. if (f.getChildFile ("Source").isDirectory())
  473. f = f.getChildFile ("Source");
  474. }
  475. return f;
  476. }
  477. void Project::Item::initialiseMissingProperties()
  478. {
  479. if (! state.hasProperty (ComponentBuilder::idProperty))
  480. setID (createAlphaNumericUID());
  481. if (isFile())
  482. {
  483. state.setProperty (Ids::name, getFile().getFileName(), 0);
  484. }
  485. else if (isGroup())
  486. {
  487. for (int i = getNumChildren(); --i >= 0;)
  488. getChild(i).initialiseMissingProperties();
  489. }
  490. }
  491. Value Project::Item::getNameValue()
  492. {
  493. return state.getPropertyAsValue (Ids::name, getUndoManager());
  494. }
  495. String Project::Item::getName() const
  496. {
  497. return state [Ids::name];
  498. }
  499. void Project::Item::addChild (const Item& newChild, int insertIndex)
  500. {
  501. state.addChild (newChild.state, insertIndex, getUndoManager());
  502. }
  503. void Project::Item::removeItemFromProject()
  504. {
  505. state.getParent().removeChild (state, getUndoManager());
  506. }
  507. Project::Item Project::Item::getParent() const
  508. {
  509. if (isMainGroup() || ! isGroup())
  510. return *this;
  511. return Item (project, state.getParent());
  512. }
  513. struct ItemSorter
  514. {
  515. static int compareElements (const ValueTree& first, const ValueTree& second)
  516. {
  517. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  518. }
  519. };
  520. struct ItemSorterWithGroupsAtStart
  521. {
  522. static int compareElements (const ValueTree& first, const ValueTree& second)
  523. {
  524. const bool firstIsGroup = first.hasType (Tags::group);
  525. const bool secondIsGroup = second.hasType (Tags::group);
  526. if (firstIsGroup == secondIsGroup)
  527. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  528. else
  529. return firstIsGroup ? -1 : 1;
  530. }
  531. };
  532. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  533. {
  534. if (keepGroupsAtStart)
  535. {
  536. ItemSorterWithGroupsAtStart sorter;
  537. state.sort (sorter, getUndoManager(), true);
  538. }
  539. else
  540. {
  541. ItemSorter sorter;
  542. state.sort (sorter, getUndoManager(), true);
  543. }
  544. }
  545. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  546. {
  547. for (int i = state.getNumChildren(); --i >= 0;)
  548. {
  549. const ValueTree child (state.getChild (i));
  550. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  551. return Item (project, child);
  552. }
  553. return addNewSubGroup (name, -1);
  554. }
  555. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  556. {
  557. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  558. jassert (canContain (group));
  559. addChild (group, insertIndex);
  560. return group;
  561. }
  562. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  563. {
  564. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  565. return false;
  566. if (file.isDirectory())
  567. {
  568. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  569. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  570. while (iter.next())
  571. {
  572. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  573. group.addFile (iter.getFile(), -1, shouldCompile);
  574. }
  575. group.sortAlphabetically (false);
  576. }
  577. else if (file.existsAsFile())
  578. {
  579. if (! project.getMainGroup().findItemForFile (file).isValid())
  580. addFileUnchecked (file, insertIndex, shouldCompile);
  581. }
  582. else
  583. {
  584. jassertfalse;
  585. }
  586. return true;
  587. }
  588. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  589. {
  590. Item item (project, ValueTree (Tags::file));
  591. item.initialiseMissingProperties();
  592. item.getNameValue() = file.getFileName();
  593. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  594. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  595. if (canContain (item))
  596. {
  597. item.setFile (file);
  598. addChild (item, insertIndex);
  599. }
  600. }
  601. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  602. {
  603. Item item (project, ValueTree (Tags::file));
  604. item.initialiseMissingProperties();
  605. item.getNameValue() = file.getFileName();
  606. item.getShouldCompileValue() = shouldCompile;
  607. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  608. if (canContain (item))
  609. {
  610. item.setFile (file);
  611. addChild (item, insertIndex);
  612. return true;
  613. }
  614. return false;
  615. }
  616. const Drawable* Project::Item::getIcon() const
  617. {
  618. if (isFile())
  619. {
  620. if (isImageFile())
  621. return StoredSettings::getInstance()->getImageFileIcon();
  622. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  623. }
  624. else if (isMainGroup())
  625. {
  626. return &(project.mainProjectIcon);
  627. }
  628. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  629. }
  630. //==============================================================================
  631. ValueTree Project::getConfigNode()
  632. {
  633. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  634. }
  635. const char* const Project::configFlagDefault = "default";
  636. const char* const Project::configFlagEnabled = "enabled";
  637. const char* const Project::configFlagDisabled = "disabled";
  638. Value Project::getConfigFlag (const String& name)
  639. {
  640. ValueTree configNode (getConfigNode());
  641. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  642. if (v.getValue().toString().isEmpty())
  643. v = configFlagDefault;
  644. return v;
  645. }
  646. bool Project::isConfigFlagEnabled (const String& name) const
  647. {
  648. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  649. }
  650. void Project::sanitiseConfigFlags()
  651. {
  652. ValueTree configNode (getConfigNode());
  653. for (int i = configNode.getNumProperties(); --i >= 0;)
  654. {
  655. const var value (configNode [configNode.getPropertyName(i)]);
  656. if (value != configFlagEnabled && value != configFlagDisabled)
  657. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  658. }
  659. }
  660. //==============================================================================
  661. ValueTree Project::getModulesNode()
  662. {
  663. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  664. }
  665. bool Project::isModuleEnabled (const String& moduleID) const
  666. {
  667. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  668. for (int i = 0; i < modules.getNumChildren(); ++i)
  669. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  670. return true;
  671. return false;
  672. }
  673. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  674. {
  675. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  676. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  677. }
  678. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  679. {
  680. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  681. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  682. }
  683. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  684. {
  685. if (! isModuleEnabled (moduleID))
  686. {
  687. ValueTree module (Tags::module);
  688. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  689. ValueTree modules (getModulesNode());
  690. modules.addChild (module, -1, getUndoManagerFor (modules));
  691. shouldShowAllModuleFilesInProject (moduleID) = true;
  692. }
  693. if (shouldCopyFilesLocally)
  694. shouldCopyModuleFilesLocally (moduleID) = true;
  695. }
  696. void Project::removeModule (const String& moduleID)
  697. {
  698. ValueTree modules (getModulesNode());
  699. for (int i = 0; i < modules.getNumChildren(); ++i)
  700. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  701. modules.removeChild (i, getUndoManagerFor (modules));
  702. }
  703. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  704. {
  705. for (int i = 0; i < availableModules.modules.size(); ++i)
  706. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  707. modules.add (availableModules.modules.getUnchecked(i)->create());
  708. }
  709. int Project::getNumModules() const
  710. {
  711. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  712. }
  713. String Project::getModuleID (int index) const
  714. {
  715. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  716. }
  717. //==============================================================================
  718. ValueTree Project::getExporters()
  719. {
  720. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  721. if (! exporters.isValid())
  722. {
  723. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  724. exporters = getExporters();
  725. }
  726. return exporters;
  727. }
  728. int Project::getNumExporters()
  729. {
  730. return getExporters().getNumChildren();
  731. }
  732. ProjectExporter* Project::createExporter (int index)
  733. {
  734. jassert (index >= 0 && index < getNumExporters());
  735. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  736. }
  737. void Project::addNewExporter (const String& exporterName)
  738. {
  739. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  740. ValueTree exporters (getExporters());
  741. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  742. }
  743. void Project::deleteExporter (int index)
  744. {
  745. ValueTree exporters (getExporters());
  746. exporters.removeChild (index, getUndoManagerFor (exporters));
  747. }
  748. void Project::createDefaultExporters()
  749. {
  750. ValueTree exporters (getExporters());
  751. exporters.removeAllChildren (getUndoManagerFor (exporters));
  752. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  753. for (int i = 0; i < exporterNames.size(); ++i)
  754. addNewExporter (exporterNames[i]);
  755. }
  756. //==============================================================================
  757. String Project::getFileTemplate (const String& templateName)
  758. {
  759. int dataSize;
  760. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  761. if (data == nullptr)
  762. {
  763. jassertfalse;
  764. return String::empty;
  765. }
  766. return String::fromUTF8 (data, dataSize);
  767. }
  768. //==============================================================================
  769. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  770. Project::ExporterIterator::~ExporterIterator() {}
  771. bool Project::ExporterIterator::next()
  772. {
  773. if (++index >= project.getNumExporters())
  774. return false;
  775. exporter = project.createExporter (index);
  776. if (exporter == nullptr)
  777. {
  778. jassertfalse; // corrupted project file?
  779. return next();
  780. }
  781. return true;
  782. }