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.

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