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.

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