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.

1005 lines
32KB

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