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.

999 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. String newID (createGUID (getID() + name + String (getNumChildren())));
  562. int n = 0;
  563. while (findItemWithID (newID).isValid())
  564. newID = createGUID (newID + String (++n));
  565. Item group (createGroup (project, name, newID));
  566. jassert (canContain (group));
  567. addChild (group, insertIndex);
  568. return group;
  569. }
  570. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  571. {
  572. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  573. return false;
  574. if (file.isDirectory())
  575. {
  576. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  577. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  578. while (iter.next())
  579. {
  580. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  581. group.addFile (iter.getFile(), -1, shouldCompile);
  582. }
  583. group.sortAlphabetically (false);
  584. }
  585. else if (file.existsAsFile())
  586. {
  587. if (! project.getMainGroup().findItemForFile (file).isValid())
  588. addFileUnchecked (file, insertIndex, shouldCompile);
  589. }
  590. else
  591. {
  592. jassertfalse;
  593. }
  594. return true;
  595. }
  596. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  597. {
  598. Item item (project, ValueTree (Tags::file));
  599. item.initialiseMissingProperties();
  600. item.getNameValue() = file.getFileName();
  601. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  602. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  603. if (canContain (item))
  604. {
  605. item.setFile (file);
  606. addChild (item, insertIndex);
  607. }
  608. }
  609. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  610. {
  611. Item item (project, ValueTree (Tags::file));
  612. item.initialiseMissingProperties();
  613. item.getNameValue() = file.getFileName();
  614. item.getShouldCompileValue() = shouldCompile;
  615. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  616. if (canContain (item))
  617. {
  618. item.setFile (file);
  619. addChild (item, insertIndex);
  620. return true;
  621. }
  622. return false;
  623. }
  624. const Drawable* Project::Item::getIcon() const
  625. {
  626. if (isFile())
  627. {
  628. if (isImageFile())
  629. return StoredSettings::getInstance()->getImageFileIcon();
  630. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  631. }
  632. else if (isMainGroup())
  633. {
  634. return &(project.mainProjectIcon);
  635. }
  636. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  637. }
  638. //==============================================================================
  639. ValueTree Project::getConfigNode()
  640. {
  641. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  642. }
  643. const char* const Project::configFlagDefault = "default";
  644. const char* const Project::configFlagEnabled = "enabled";
  645. const char* const Project::configFlagDisabled = "disabled";
  646. Value Project::getConfigFlag (const String& name)
  647. {
  648. ValueTree configNode (getConfigNode());
  649. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  650. if (v.getValue().toString().isEmpty())
  651. v = configFlagDefault;
  652. return v;
  653. }
  654. bool Project::isConfigFlagEnabled (const String& name) const
  655. {
  656. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  657. }
  658. void Project::sanitiseConfigFlags()
  659. {
  660. ValueTree configNode (getConfigNode());
  661. for (int i = configNode.getNumProperties(); --i >= 0;)
  662. {
  663. const var value (configNode [configNode.getPropertyName(i)]);
  664. if (value != configFlagEnabled && value != configFlagDisabled)
  665. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  666. }
  667. }
  668. //==============================================================================
  669. ValueTree Project::getModulesNode()
  670. {
  671. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  672. }
  673. bool Project::isModuleEnabled (const String& moduleID) const
  674. {
  675. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  676. for (int i = 0; i < modules.getNumChildren(); ++i)
  677. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  678. return true;
  679. return false;
  680. }
  681. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  682. {
  683. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  684. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  685. }
  686. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  687. {
  688. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  689. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  690. }
  691. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  692. {
  693. if (! isModuleEnabled (moduleID))
  694. {
  695. ValueTree module (Tags::module);
  696. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  697. ValueTree modules (getModulesNode());
  698. modules.addChild (module, -1, getUndoManagerFor (modules));
  699. shouldShowAllModuleFilesInProject (moduleID) = true;
  700. }
  701. if (shouldCopyFilesLocally)
  702. shouldCopyModuleFilesLocally (moduleID) = true;
  703. }
  704. void Project::removeModule (const String& moduleID)
  705. {
  706. ValueTree modules (getModulesNode());
  707. for (int i = 0; i < modules.getNumChildren(); ++i)
  708. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  709. modules.removeChild (i, getUndoManagerFor (modules));
  710. }
  711. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  712. {
  713. for (int i = 0; i < availableModules.modules.size(); ++i)
  714. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  715. modules.add (availableModules.modules.getUnchecked(i)->create());
  716. }
  717. int Project::getNumModules() const
  718. {
  719. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  720. }
  721. String Project::getModuleID (int index) const
  722. {
  723. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  724. }
  725. //==============================================================================
  726. ValueTree Project::getExporters()
  727. {
  728. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  729. if (! exporters.isValid())
  730. {
  731. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  732. exporters = getExporters();
  733. }
  734. return exporters;
  735. }
  736. int Project::getNumExporters()
  737. {
  738. return getExporters().getNumChildren();
  739. }
  740. ProjectExporter* Project::createExporter (int index)
  741. {
  742. jassert (index >= 0 && index < getNumExporters());
  743. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  744. }
  745. void Project::addNewExporter (const String& exporterName)
  746. {
  747. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  748. ValueTree exporters (getExporters());
  749. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  750. }
  751. void Project::deleteExporter (int index)
  752. {
  753. ValueTree exporters (getExporters());
  754. exporters.removeChild (index, getUndoManagerFor (exporters));
  755. }
  756. void Project::createDefaultExporters()
  757. {
  758. ValueTree exporters (getExporters());
  759. exporters.removeAllChildren (getUndoManagerFor (exporters));
  760. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  761. for (int i = 0; i < exporterNames.size(); ++i)
  762. addNewExporter (exporterNames[i]);
  763. }
  764. //==============================================================================
  765. String Project::getFileTemplate (const String& templateName)
  766. {
  767. int dataSize;
  768. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  769. if (data == nullptr)
  770. {
  771. jassertfalse;
  772. return String::empty;
  773. }
  774. return String::fromUTF8 (data, dataSize);
  775. }
  776. //==============================================================================
  777. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  778. Project::ExporterIterator::~ExporterIterator() {}
  779. bool Project::ExporterIterator::next()
  780. {
  781. if (++index >= project.getNumExporters())
  782. return false;
  783. exporter = project.createExporter (index);
  784. if (exporter == nullptr)
  785. {
  786. jassertfalse; // corrupted project file?
  787. return next();
  788. }
  789. return true;
  790. }