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.

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