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.

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