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.

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