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.

974 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. #include "../Application/jucer_Application.h"
  24. //==============================================================================
  25. namespace Tags
  26. {
  27. const Identifier projectRoot ("JUCERPROJECT");
  28. const Identifier projectMainGroup ("MAINGROUP");
  29. const Identifier group ("GROUP");
  30. const Identifier file ("FILE");
  31. const Identifier exporters ("EXPORTFORMATS");
  32. const Identifier configGroup ("JUCEOPTIONS");
  33. const Identifier modulesGroup ("MODULES");
  34. const Identifier module ("MODULE");
  35. }
  36. const char* Project::projectFileExtension = ".jucer";
  37. //==============================================================================
  38. Project::Project (const File& file_)
  39. : FileBasedDocument (projectFileExtension,
  40. String ("*") + projectFileExtension,
  41. "Choose a Jucer project to load",
  42. "Save Jucer project"),
  43. projectRoot (Tags::projectRoot)
  44. {
  45. setFile (file_);
  46. removeDefunctExporters();
  47. setMissingDefaultValues();
  48. setChangedFlag (false);
  49. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  50. projectRoot.addListener (this);
  51. }
  52. Project::~Project()
  53. {
  54. projectRoot.removeListener (this);
  55. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  56. }
  57. //==============================================================================
  58. void Project::setTitle (const String& newTitle)
  59. {
  60. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  61. getMainGroup().getNameValue() = newTitle;
  62. }
  63. String Project::getTitle() const
  64. {
  65. return projectRoot.getChildWithName (Tags::projectMainGroup) [Ids::name];
  66. }
  67. String Project::getDocumentTitle()
  68. {
  69. return getTitle();
  70. }
  71. void Project::updateProjectSettings()
  72. {
  73. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  74. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  75. }
  76. void Project::setMissingDefaultValues()
  77. {
  78. if (! projectRoot.hasProperty (ComponentBuilder::idProperty))
  79. projectRoot.setProperty (ComponentBuilder::idProperty, createAlphaNumericUID(), nullptr);
  80. // Create main file group if missing
  81. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  82. {
  83. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  84. projectRoot.addChild (mainGroup.state, 0, 0);
  85. }
  86. getMainGroup().initialiseMissingProperties();
  87. if (getDocumentTitle().isEmpty())
  88. setTitle ("JUCE Project");
  89. if (! projectRoot.hasProperty (Ids::projectType))
  90. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  91. if (! projectRoot.hasProperty (Ids::version))
  92. getVersionValue() = "1.0.0";
  93. updateOldStyleConfigList();
  94. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  95. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  96. getProjectType().setMissingProjectProperties (*this);
  97. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  98. addDefaultModules (false);
  99. }
  100. void Project::updateOldStyleConfigList()
  101. {
  102. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  103. if (deprecatedConfigsList.isValid())
  104. {
  105. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  106. for (Project::ExporterIterator exporter (*this); exporter.next();)
  107. {
  108. if (exporter->getNumConfigurations() == 0)
  109. {
  110. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  111. if (! exporter->isXcode())
  112. {
  113. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  114. {
  115. ValueTree config (newConfigs.getChild(j));
  116. config.removeProperty (Ids::osxSDK, nullptr);
  117. config.removeProperty (Ids::osxCompatibility, nullptr);
  118. config.removeProperty (Ids::osxArchitecture, nullptr);
  119. }
  120. }
  121. exporter->settings.addChild (newConfigs, 0, nullptr);
  122. }
  123. }
  124. }
  125. }
  126. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  127. {
  128. if (projectRoot.hasProperty (name))
  129. {
  130. for (Project::ExporterIterator exporter (*this); exporter.next();)
  131. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  132. projectRoot.removeProperty (name, nullptr);
  133. }
  134. }
  135. void Project::removeDefunctExporters()
  136. {
  137. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  138. for (;;)
  139. {
  140. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  141. if (oldVC6Exporter.isValid())
  142. exporters.removeChild (oldVC6Exporter, nullptr);
  143. else
  144. break;
  145. }
  146. }
  147. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  148. {
  149. addModule ("juce_core", shouldCopyFilesLocally);
  150. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  151. {
  152. addModule ("juce_events", shouldCopyFilesLocally);
  153. addModule ("juce_graphics", shouldCopyFilesLocally);
  154. addModule ("juce_data_structures", shouldCopyFilesLocally);
  155. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  156. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  157. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  158. addModule ("juce_cryptography", shouldCopyFilesLocally);
  159. addModule ("juce_video", shouldCopyFilesLocally);
  160. addModule ("juce_opengl", shouldCopyFilesLocally);
  161. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  162. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  163. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  164. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  165. }
  166. }
  167. bool Project::isAudioPluginModuleMissing() const
  168. {
  169. return getProjectType().isAudioPlugin()
  170. && ! isModuleEnabled ("juce_audio_plugin_client");
  171. }
  172. //==============================================================================
  173. static void registerRecentFile (const File& file)
  174. {
  175. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  176. StoredSettings::getInstance()->recentFiles.addFile (file);
  177. StoredSettings::getInstance()->flush();
  178. }
  179. Result Project::loadDocument (const File& file)
  180. {
  181. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  182. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  183. return Result::fail ("Not a valid Jucer project!");
  184. ValueTree newTree (ValueTree::fromXml (*xml));
  185. if (! newTree.hasType (Tags::projectRoot))
  186. return Result::fail ("The document contains errors and couldn't be parsed!");
  187. registerRecentFile (file);
  188. projectRoot = newTree;
  189. removeDefunctExporters();
  190. setMissingDefaultValues();
  191. return Result::ok();
  192. }
  193. Result Project::saveDocument (const File& file)
  194. {
  195. return saveProject (file, false);
  196. }
  197. Result Project::saveProject (const File& file, bool isCommandLineApp)
  198. {
  199. updateProjectSettings();
  200. sanitiseConfigFlags();
  201. if (! isCommandLineApp)
  202. registerRecentFile (file);
  203. ProjectSaver saver (*this, file);
  204. return saver.save (! isCommandLineApp);
  205. }
  206. Result Project::saveResourcesOnly (const File& file)
  207. {
  208. ProjectSaver saver (*this, file);
  209. return saver.saveResourcesOnly();
  210. }
  211. //==============================================================================
  212. static File lastDocumentOpened;
  213. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  214. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  215. //==============================================================================
  216. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  217. {
  218. if (property == Ids::projectType)
  219. setMissingDefaultValues();
  220. changed();
  221. }
  222. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  223. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  224. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  225. void Project::valueTreeParentChanged (ValueTree&) {}
  226. //==============================================================================
  227. File Project::resolveFilename (String filename) const
  228. {
  229. if (filename.isEmpty())
  230. return File::nonexistent;
  231. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  232. .replaceCharacter ('\\', '/');
  233. if (FileHelpers::isAbsolutePath (filename))
  234. return File::createFileWithoutCheckingPath (filename); // (avoid assertions for windows-style paths)
  235. return getFile().getSiblingFile (filename);
  236. }
  237. String Project::getRelativePathForFile (const File& file) const
  238. {
  239. String filename (file.getFullPathName());
  240. File relativePathBase (getFile().getParentDirectory());
  241. String p1 (relativePathBase.getFullPathName());
  242. String p2 (file.getFullPathName());
  243. while (p1.startsWithChar (File::separator))
  244. p1 = p1.substring (1);
  245. while (p2.startsWithChar (File::separator))
  246. p2 = p2.substring (1);
  247. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  248. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  249. {
  250. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  251. }
  252. return filename;
  253. }
  254. //==============================================================================
  255. const ProjectType& Project::getProjectType() const
  256. {
  257. const ProjectType* type = ProjectType::findType (getProjectTypeString());
  258. jassert (type != nullptr);
  259. if (type == nullptr)
  260. {
  261. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  262. jassert (type != nullptr);
  263. }
  264. return *type;
  265. }
  266. //==============================================================================
  267. void Project::createPropertyEditors (PropertyListBuilder& props)
  268. {
  269. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  270. "The name of the project.");
  271. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  272. "The project's version number, This should be in the format major.minor.point");
  273. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  274. "Your company name, which will be added to the properties of the binary where possible");
  275. {
  276. StringArray projectTypeNames;
  277. Array<var> projectTypeCodes;
  278. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  279. for (int i = 0; i < types.size(); ++i)
  280. {
  281. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  282. projectTypeCodes.add (types.getUnchecked(i)->getType());
  283. }
  284. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  285. }
  286. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  287. "A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  288. getProjectType().createPropertyEditors (*this, props);
  289. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  290. "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.");
  291. }
  292. String Project::getVersionAsHex() const
  293. {
  294. StringArray configs;
  295. configs.addTokens (getVersionString(), ",.", String::empty);
  296. configs.trim();
  297. configs.removeEmptyStrings();
  298. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  299. if (configs.size() >= 4)
  300. value = (value << 8) + configs[3].getIntValue();
  301. return "0x" + String::toHexString (value);
  302. }
  303. StringPairArray Project::getPreprocessorDefs() const
  304. {
  305. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  306. }
  307. //==============================================================================
  308. Project::Item Project::getMainGroup()
  309. {
  310. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  311. }
  312. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  313. {
  314. if (item.isImageFile())
  315. {
  316. found.add (new Project::Item (item));
  317. }
  318. else if (item.isGroup())
  319. {
  320. for (int i = 0; i < item.getNumChildren(); ++i)
  321. findImages (item.getChild (i), found);
  322. }
  323. }
  324. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  325. {
  326. findImages (getMainGroup(), items);
  327. }
  328. //==============================================================================
  329. Project::Item::Item (Project& project_, const ValueTree& state_)
  330. : project (project_), state (state_)
  331. {
  332. }
  333. Project::Item::Item (const Item& other)
  334. : project (other.project), state (other.state)
  335. {
  336. }
  337. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  338. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  339. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  340. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  341. Image Project::Item::loadAsImageFile() const
  342. {
  343. return isValid() ? ImageCache::getFromFile (getFile())
  344. : Image::null;
  345. }
  346. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  347. {
  348. Item group (project, ValueTree (Tags::group));
  349. group.setID (uid);
  350. group.initialiseMissingProperties();
  351. group.getNameValue() = name;
  352. return group;
  353. }
  354. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  355. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  356. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  357. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  358. Project::Item Project::Item::findItemWithID (const String& targetId) const
  359. {
  360. if (state [ComponentBuilder::idProperty] == targetId)
  361. return *this;
  362. if (isGroup())
  363. {
  364. for (int i = getNumChildren(); --i >= 0;)
  365. {
  366. Item found (getChild(i).findItemWithID (targetId));
  367. if (found.isValid())
  368. return found;
  369. }
  370. }
  371. return Item (project, ValueTree::invalid);
  372. }
  373. bool Project::Item::canContain (const Item& child) const
  374. {
  375. if (isFile())
  376. return false;
  377. if (isGroup())
  378. return child.isFile() || child.isGroup();
  379. jassertfalse
  380. return false;
  381. }
  382. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  383. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  384. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  385. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  386. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  387. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  388. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  389. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  390. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  391. String Project::Item::getFilePath() const
  392. {
  393. if (isFile())
  394. return state [Ids::file].toString();
  395. else
  396. return String::empty;
  397. }
  398. File Project::Item::getFile() const
  399. {
  400. if (isFile())
  401. return project.resolveFilename (state [Ids::file].toString());
  402. else
  403. return File::nonexistent;
  404. }
  405. void Project::Item::setFile (const File& file)
  406. {
  407. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  408. jassert (getFile() == file);
  409. }
  410. void Project::Item::setFile (const RelativePath& file)
  411. {
  412. jassert (file.getRoot() == RelativePath::projectFolder);
  413. jassert (isFile());
  414. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  415. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  416. }
  417. bool Project::Item::renameFile (const File& newFile)
  418. {
  419. const File oldFile (getFile());
  420. if (oldFile.moveFileTo (newFile)
  421. || (newFile.exists() && ! oldFile.exists()))
  422. {
  423. setFile (newFile);
  424. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  425. return true;
  426. }
  427. return false;
  428. }
  429. bool Project::Item::containsChildForFile (const RelativePath& file) const
  430. {
  431. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  432. }
  433. Project::Item Project::Item::findItemForFile (const File& file) const
  434. {
  435. if (getFile() == file)
  436. return *this;
  437. if (isGroup())
  438. {
  439. for (int i = getNumChildren(); --i >= 0;)
  440. {
  441. Item found (getChild(i).findItemForFile (file));
  442. if (found.isValid())
  443. return found;
  444. }
  445. }
  446. return Item (project, ValueTree::invalid);
  447. }
  448. File Project::Item::determineGroupFolder() const
  449. {
  450. jassert (isGroup());
  451. File f;
  452. for (int i = 0; i < getNumChildren(); ++i)
  453. {
  454. f = getChild(i).getFile();
  455. if (f.exists())
  456. return f.getParentDirectory();
  457. }
  458. Item parent (getParent());
  459. if (parent != *this)
  460. {
  461. f = parent.determineGroupFolder();
  462. if (f.getChildFile (getName()).isDirectory())
  463. f = f.getChildFile (getName());
  464. }
  465. else
  466. {
  467. f = project.getFile().getParentDirectory();
  468. if (f.getChildFile ("Source").isDirectory())
  469. f = f.getChildFile ("Source");
  470. }
  471. return f;
  472. }
  473. void Project::Item::initialiseMissingProperties()
  474. {
  475. if (! state.hasProperty (ComponentBuilder::idProperty))
  476. setID (createAlphaNumericUID());
  477. if (isFile())
  478. {
  479. state.setProperty (Ids::name, getFile().getFileName(), 0);
  480. }
  481. else if (isGroup())
  482. {
  483. for (int i = getNumChildren(); --i >= 0;)
  484. getChild(i).initialiseMissingProperties();
  485. }
  486. }
  487. Value Project::Item::getNameValue()
  488. {
  489. return state.getPropertyAsValue (Ids::name, getUndoManager());
  490. }
  491. String Project::Item::getName() const
  492. {
  493. return state [Ids::name];
  494. }
  495. void Project::Item::addChild (const Item& newChild, int insertIndex)
  496. {
  497. state.addChild (newChild.state, insertIndex, getUndoManager());
  498. }
  499. void Project::Item::removeItemFromProject()
  500. {
  501. state.getParent().removeChild (state, getUndoManager());
  502. }
  503. Project::Item Project::Item::getParent() const
  504. {
  505. if (isMainGroup() || ! isGroup())
  506. return *this;
  507. return Item (project, state.getParent());
  508. }
  509. struct ItemSorter
  510. {
  511. static int compareElements (const ValueTree& first, const ValueTree& second)
  512. {
  513. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  514. }
  515. };
  516. struct ItemSorterWithGroupsAtStart
  517. {
  518. static int compareElements (const ValueTree& first, const ValueTree& second)
  519. {
  520. const bool firstIsGroup = first.hasType (Tags::group);
  521. const bool secondIsGroup = second.hasType (Tags::group);
  522. if (firstIsGroup == secondIsGroup)
  523. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  524. else
  525. return firstIsGroup ? -1 : 1;
  526. }
  527. };
  528. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  529. {
  530. if (keepGroupsAtStart)
  531. {
  532. ItemSorterWithGroupsAtStart sorter;
  533. state.sort (sorter, getUndoManager(), true);
  534. }
  535. else
  536. {
  537. ItemSorter sorter;
  538. state.sort (sorter, getUndoManager(), true);
  539. }
  540. }
  541. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  542. {
  543. for (int i = state.getNumChildren(); --i >= 0;)
  544. {
  545. const ValueTree child (state.getChild (i));
  546. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  547. return Item (project, child);
  548. }
  549. return addNewSubGroup (name, -1);
  550. }
  551. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  552. {
  553. String newID (createGUID (getID() + name + String (getNumChildren())));
  554. int n = 0;
  555. while (findItemWithID (newID).isValid())
  556. newID = createGUID (newID + String (++n));
  557. Item group (createGroup (project, name, newID));
  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. return projectRoot.getOrCreateChildWithName (Tags::exporters, nullptr);
  721. }
  722. int Project::getNumExporters()
  723. {
  724. return getExporters().getNumChildren();
  725. }
  726. ProjectExporter* Project::createExporter (int index)
  727. {
  728. jassert (index >= 0 && index < getNumExporters());
  729. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  730. }
  731. void Project::addNewExporter (const String& exporterName)
  732. {
  733. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  734. ValueTree exporters (getExporters());
  735. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  736. }
  737. void Project::createDefaultExporters()
  738. {
  739. ValueTree exporters (getExporters());
  740. exporters.removeAllChildren (getUndoManagerFor (exporters));
  741. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  742. for (int i = 0; i < exporterNames.size(); ++i)
  743. addNewExporter (exporterNames[i]);
  744. }
  745. //==============================================================================
  746. String Project::getFileTemplate (const String& templateName)
  747. {
  748. int dataSize;
  749. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  750. if (data == nullptr)
  751. {
  752. jassertfalse;
  753. return String::empty;
  754. }
  755. return String::fromUTF8 (data, dataSize);
  756. }
  757. //==============================================================================
  758. Project::ExporterIterator::ExporterIterator (Project& project_) : index (-1), project (project_) {}
  759. Project::ExporterIterator::~ExporterIterator() {}
  760. bool Project::ExporterIterator::next()
  761. {
  762. if (++index >= project.getNumExporters())
  763. return false;
  764. exporter = project.createExporter (index);
  765. if (exporter == nullptr)
  766. {
  767. jassertfalse; // corrupted project file?
  768. return next();
  769. }
  770. return true;
  771. }