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.

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