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.

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