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.

1034 lines
33KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "jucer_Project.h"
  18. #include "jucer_ProjectType.h"
  19. #include "../Project Saving/jucer_ProjectExporter.h"
  20. #include "../Project Saving/jucer_ProjectSaver.h"
  21. #include "../Application/jucer_OpenDocumentManager.h"
  22. #include "../Application/jucer_Application.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier exporters ("EXPORTFORMATS");
  31. const Identifier configGroup ("JUCEOPTIONS");
  32. const Identifier modulesGroup ("MODULES");
  33. const Identifier module ("MODULE");
  34. }
  35. const char* Project::projectFileExtension = ".jucer";
  36. //==============================================================================
  37. Project::Project (const File& f)
  38. : FileBasedDocument (projectFileExtension,
  39. String ("*") + projectFileExtension,
  40. "Choose a Jucer project to load",
  41. "Save Jucer project"),
  42. projectRoot (Tags::projectRoot)
  43. {
  44. Logger::writeToLog ("Loading project: " + f.getFullPathName());
  45. setFile (f);
  46. removeDefunctExporters();
  47. setMissingDefaultValues();
  48. setChangedFlag (false);
  49. projectRoot.addListener (this);
  50. }
  51. Project::~Project()
  52. {
  53. projectRoot.removeListener (this);
  54. IntrojucerApp::getApp().openDocumentManager.closeAllDocumentsUsingProject (*this, false);
  55. }
  56. //==============================================================================
  57. void Project::setTitle (const String& newTitle)
  58. {
  59. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  60. getMainGroup().getNameValue() = newTitle;
  61. }
  62. String Project::getTitle() const
  63. {
  64. return projectRoot.getChildWithName (Tags::projectMainGroup) [Ids::name];
  65. }
  66. String Project::getDocumentTitle()
  67. {
  68. return getTitle();
  69. }
  70. void Project::updateProjectSettings()
  71. {
  72. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, nullptr);
  73. projectRoot.setProperty (Ids::name, getDocumentTitle(), nullptr);
  74. }
  75. void Project::setMissingDefaultValues()
  76. {
  77. if (! projectRoot.hasProperty (Ids::ID))
  78. projectRoot.setProperty (Ids::ID, createAlphaNumericUID(), nullptr);
  79. // Create main file group if missing
  80. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  81. {
  82. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  83. projectRoot.addChild (mainGroup.state, 0, 0);
  84. }
  85. getMainGroup().initialiseMissingProperties();
  86. if (getDocumentTitle().isEmpty())
  87. setTitle ("JUCE Project");
  88. if (! projectRoot.hasProperty (Ids::projectType))
  89. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  90. if (! projectRoot.hasProperty (Ids::version))
  91. getVersionValue() = "1.0.0";
  92. updateOldStyleConfigList();
  93. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  94. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  95. getProjectType().setMissingProjectProperties (*this);
  96. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  97. addDefaultModules (false);
  98. if (getBundleIdentifier().toString().isEmpty())
  99. getBundleIdentifier() = getDefaultBundleIdentifier();
  100. if (shouldIncludeBinaryInAppConfig() == var::null)
  101. shouldIncludeBinaryInAppConfig() = true;
  102. IntrojucerApp::getApp().updateNewlyOpenedProject (*this);
  103. }
  104. void Project::updateOldStyleConfigList()
  105. {
  106. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (ProjectExporter::configurations));
  107. if (deprecatedConfigsList.isValid())
  108. {
  109. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  110. for (Project::ExporterIterator exporter (*this); exporter.next();)
  111. {
  112. if (exporter->getNumConfigurations() == 0)
  113. {
  114. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  115. if (! exporter->isXcode())
  116. {
  117. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  118. {
  119. ValueTree config (newConfigs.getChild(j));
  120. config.removeProperty (Ids::osxSDK, nullptr);
  121. config.removeProperty (Ids::osxCompatibility, nullptr);
  122. config.removeProperty (Ids::osxArchitecture, nullptr);
  123. }
  124. }
  125. exporter->settings.addChild (newConfigs, 0, nullptr);
  126. }
  127. }
  128. }
  129. }
  130. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  131. {
  132. if (projectRoot.hasProperty (name))
  133. {
  134. for (Project::ExporterIterator exporter (*this); exporter.next();)
  135. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  136. projectRoot.removeProperty (name, nullptr);
  137. }
  138. }
  139. void Project::removeDefunctExporters()
  140. {
  141. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  142. for (;;)
  143. {
  144. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  145. if (oldVC6Exporter.isValid())
  146. exporters.removeChild (oldVC6Exporter, nullptr);
  147. else
  148. break;
  149. }
  150. }
  151. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  152. {
  153. addModule ("juce_core", shouldCopyFilesLocally);
  154. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  155. {
  156. addModule ("juce_events", shouldCopyFilesLocally);
  157. addModule ("juce_graphics", shouldCopyFilesLocally);
  158. addModule ("juce_data_structures", shouldCopyFilesLocally);
  159. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  160. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  161. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  162. addModule ("juce_cryptography", shouldCopyFilesLocally);
  163. addModule ("juce_video", shouldCopyFilesLocally);
  164. addModule ("juce_opengl", shouldCopyFilesLocally);
  165. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  166. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  167. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  168. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  169. }
  170. }
  171. bool Project::isAudioPluginModuleMissing() const
  172. {
  173. return getProjectType().isAudioPlugin()
  174. && ! isModuleEnabled ("juce_audio_plugin_client");
  175. }
  176. File Project::getBinaryDataCppFile (int index) const
  177. {
  178. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  179. if (index > 0)
  180. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  181. .withFileExtension (cpp.getFileExtension());
  182. return cpp;
  183. }
  184. //==============================================================================
  185. static void registerRecentFile (const File& file)
  186. {
  187. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  188. getAppSettings().recentFiles.addFile (file);
  189. getAppSettings().flush();
  190. }
  191. Result Project::loadDocument (const File& file)
  192. {
  193. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  194. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  195. return Result::fail ("Not a valid Jucer project!");
  196. ValueTree newTree (ValueTree::fromXml (*xml));
  197. if (! newTree.hasType (Tags::projectRoot))
  198. return Result::fail ("The document contains errors and couldn't be parsed!");
  199. registerRecentFile (file);
  200. projectRoot = newTree;
  201. removeDefunctExporters();
  202. setMissingDefaultValues();
  203. setChangedFlag (false);
  204. return Result::ok();
  205. }
  206. Result Project::saveDocument (const File& file)
  207. {
  208. return saveProject (file, false);
  209. }
  210. Result Project::saveProject (const File& file, bool isCommandLineApp)
  211. {
  212. updateProjectSettings();
  213. sanitiseConfigFlags();
  214. if (! isCommandLineApp)
  215. registerRecentFile (file);
  216. ProjectSaver saver (*this, file);
  217. return saver.save (! isCommandLineApp);
  218. }
  219. Result Project::saveResourcesOnly (const File& file)
  220. {
  221. ProjectSaver saver (*this, file);
  222. return saver.saveResourcesOnly();
  223. }
  224. //==============================================================================
  225. static File lastDocumentOpened;
  226. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  227. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  228. //==============================================================================
  229. void Project::valueTreePropertyChanged (ValueTree&, const Identifier& property)
  230. {
  231. if (property == Ids::projectType)
  232. setMissingDefaultValues();
  233. changed();
  234. }
  235. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  236. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  237. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  238. void Project::valueTreeParentChanged (ValueTree&) {}
  239. //==============================================================================
  240. File Project::resolveFilename (String filename) const
  241. {
  242. if (filename.isEmpty())
  243. return File::nonexistent;
  244. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  245. if (FileHelpers::isAbsolutePath (filename))
  246. return File::createFileWithoutCheckingPath (FileHelpers::currentOSStylePath (filename)); // (avoid assertions for windows-style paths)
  247. return getFile().getSiblingFile (FileHelpers::currentOSStylePath (filename));
  248. }
  249. String Project::getRelativePathForFile (const File& file) const
  250. {
  251. String filename (file.getFullPathName());
  252. File relativePathBase (getFile().getParentDirectory());
  253. String p1 (relativePathBase.getFullPathName());
  254. String p2 (file.getFullPathName());
  255. while (p1.startsWithChar (File::separator))
  256. p1 = p1.substring (1);
  257. while (p2.startsWithChar (File::separator))
  258. p2 = p2.substring (1);
  259. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  260. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  261. {
  262. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  263. }
  264. return filename;
  265. }
  266. //==============================================================================
  267. const ProjectType& Project::getProjectType() const
  268. {
  269. if (const ProjectType* type = ProjectType::findType (getProjectTypeString()))
  270. return *type;
  271. const ProjectType* guiType = ProjectType::findType (ProjectType::getGUIAppTypeName());
  272. jassert (guiType != nullptr);
  273. return *guiType;
  274. }
  275. //==============================================================================
  276. void Project::createPropertyEditors (PropertyListBuilder& props)
  277. {
  278. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  279. "The name of the project.");
  280. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  281. "The project's version number, This should be in the format major.minor.point[.point]");
  282. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  283. "Your company name, which will be added to the properties of the binary where possible");
  284. {
  285. StringArray projectTypeNames;
  286. Array<var> projectTypeCodes;
  287. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  288. for (int i = 0; i < types.size(); ++i)
  289. {
  290. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  291. projectTypeCodes.add (types.getUnchecked(i)->getType());
  292. }
  293. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  294. }
  295. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  296. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  297. getProjectType().createPropertyEditors (*this, props);
  298. {
  299. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  300. StringArray maxSizeNames;
  301. Array<var> maxSizeCodes;
  302. maxSizeNames.add (TRANS("Default"));
  303. maxSizeCodes.add (var::null);
  304. maxSizeNames.add (String::empty);
  305. maxSizeCodes.add (var::null);
  306. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  307. {
  308. const int sizeInBytes = maxSizes[i] * 1024;
  309. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  310. maxSizeCodes.add (sizeInBytes);
  311. }
  312. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  313. "When splitting binary data into multiple cpp files, the Introjucer attempts to keep the file sizes below this threshold. "
  314. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  315. }
  316. props.add (new BooleanPropertyComponent (shouldIncludeBinaryInAppConfig(), "Include Binary",
  317. "Include BinaryData.h in the AppConfig.h file"));
  318. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  319. "Global preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  320. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  321. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  322. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  323. }
  324. static StringArray getConfigs (const Project& p)
  325. {
  326. StringArray configs;
  327. configs.addTokens (p.getVersionString(), ",.", String::empty);
  328. configs.trim();
  329. configs.removeEmptyStrings();
  330. return configs;
  331. }
  332. int Project::getVersionAsHexInteger() const
  333. {
  334. const StringArray configs (getConfigs (*this));
  335. int value = (configs[0].getIntValue() << 16)
  336. + (configs[1].getIntValue() << 8)
  337. + configs[2].getIntValue();
  338. if (configs.size() >= 4)
  339. value = (value << 8) + configs[3].getIntValue();
  340. return value;
  341. }
  342. String Project::getVersionAsHex() const
  343. {
  344. return "0x" + String::toHexString (getVersionAsHexInteger());
  345. }
  346. StringPairArray Project::getPreprocessorDefs() const
  347. {
  348. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  349. }
  350. //==============================================================================
  351. Project::Item Project::getMainGroup()
  352. {
  353. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  354. }
  355. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  356. {
  357. if (item.isImageFile())
  358. {
  359. found.add (new Project::Item (item));
  360. }
  361. else if (item.isGroup())
  362. {
  363. for (int i = 0; i < item.getNumChildren(); ++i)
  364. findImages (item.getChild (i), found);
  365. }
  366. }
  367. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  368. {
  369. findImages (getMainGroup(), items);
  370. }
  371. //==============================================================================
  372. Project::Item::Item (Project& p, const ValueTree& s)
  373. : project (p), state (s)
  374. {
  375. }
  376. Project::Item::Item (const Item& other)
  377. : project (other.project), state (other.state)
  378. {
  379. }
  380. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  381. String Project::Item::getID() const { return state [Ids::ID]; }
  382. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  383. Image Project::Item::loadAsImageFile() const
  384. {
  385. return isValid() ? ImageCache::getFromFile (getFile())
  386. : Image::null;
  387. }
  388. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  389. {
  390. Item group (project, ValueTree (Tags::group));
  391. group.setID (uid);
  392. group.initialiseMissingProperties();
  393. group.getNameValue() = name;
  394. return group;
  395. }
  396. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  397. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  398. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  399. bool Project::Item::isImageFile() const { return isFile() && ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr; }
  400. Project::Item Project::Item::findItemWithID (const String& targetId) const
  401. {
  402. if (state [Ids::ID] == targetId)
  403. return *this;
  404. if (isGroup())
  405. {
  406. for (int i = getNumChildren(); --i >= 0;)
  407. {
  408. Item found (getChild(i).findItemWithID (targetId));
  409. if (found.isValid())
  410. return found;
  411. }
  412. }
  413. return Item (project, ValueTree::invalid);
  414. }
  415. bool Project::Item::canContain (const Item& child) const
  416. {
  417. if (isFile())
  418. return false;
  419. if (isGroup())
  420. return child.isFile() || child.isGroup();
  421. jassertfalse;
  422. return false;
  423. }
  424. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  425. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  426. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  427. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  428. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  429. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  430. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  431. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  432. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  433. String Project::Item::getFilePath() const
  434. {
  435. if (isFile())
  436. return state [Ids::file].toString();
  437. return String::empty;
  438. }
  439. File Project::Item::getFile() const
  440. {
  441. if (isFile())
  442. return project.resolveFilename (state [Ids::file].toString());
  443. return File::nonexistent;
  444. }
  445. void Project::Item::setFile (const File& file)
  446. {
  447. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  448. jassert (getFile() == file);
  449. }
  450. void Project::Item::setFile (const RelativePath& file)
  451. {
  452. jassert (file.getRoot() == RelativePath::projectFolder);
  453. jassert (isFile());
  454. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  455. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  456. }
  457. bool Project::Item::renameFile (const File& newFile)
  458. {
  459. const File oldFile (getFile());
  460. if (oldFile.moveFileTo (newFile)
  461. || (newFile.exists() && ! oldFile.exists()))
  462. {
  463. setFile (newFile);
  464. IntrojucerApp::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  465. return true;
  466. }
  467. return false;
  468. }
  469. bool Project::Item::containsChildForFile (const RelativePath& file) const
  470. {
  471. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  472. }
  473. Project::Item Project::Item::findItemForFile (const File& file) const
  474. {
  475. if (getFile() == file)
  476. return *this;
  477. if (isGroup())
  478. {
  479. for (int i = getNumChildren(); --i >= 0;)
  480. {
  481. Item found (getChild(i).findItemForFile (file));
  482. if (found.isValid())
  483. return found;
  484. }
  485. }
  486. return Item (project, ValueTree::invalid);
  487. }
  488. File Project::Item::determineGroupFolder() const
  489. {
  490. jassert (isGroup());
  491. File f;
  492. for (int i = 0; i < getNumChildren(); ++i)
  493. {
  494. f = getChild(i).getFile();
  495. if (f.exists())
  496. return f.getParentDirectory();
  497. }
  498. Item parent (getParent());
  499. if (parent != *this)
  500. {
  501. f = parent.determineGroupFolder();
  502. if (f.getChildFile (getName()).isDirectory())
  503. f = f.getChildFile (getName());
  504. }
  505. else
  506. {
  507. f = project.getFile().getParentDirectory();
  508. if (f.getChildFile ("Source").isDirectory())
  509. f = f.getChildFile ("Source");
  510. }
  511. return f;
  512. }
  513. void Project::Item::initialiseMissingProperties()
  514. {
  515. if (! state.hasProperty (Ids::ID))
  516. setID (createAlphaNumericUID());
  517. if (isFile())
  518. {
  519. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  520. }
  521. else if (isGroup())
  522. {
  523. for (int i = getNumChildren(); --i >= 0;)
  524. getChild(i).initialiseMissingProperties();
  525. }
  526. }
  527. Value Project::Item::getNameValue()
  528. {
  529. return state.getPropertyAsValue (Ids::name, getUndoManager());
  530. }
  531. String Project::Item::getName() const
  532. {
  533. return state [Ids::name];
  534. }
  535. void Project::Item::addChild (const Item& newChild, int insertIndex)
  536. {
  537. state.addChild (newChild.state, insertIndex, getUndoManager());
  538. }
  539. void Project::Item::removeItemFromProject()
  540. {
  541. state.getParent().removeChild (state, getUndoManager());
  542. }
  543. Project::Item Project::Item::getParent() const
  544. {
  545. if (isMainGroup() || ! isGroup())
  546. return *this;
  547. return Item (project, state.getParent());
  548. }
  549. struct ItemSorter
  550. {
  551. static int compareElements (const ValueTree& first, const ValueTree& second)
  552. {
  553. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  554. }
  555. };
  556. struct ItemSorterWithGroupsAtStart
  557. {
  558. static int compareElements (const ValueTree& first, const ValueTree& second)
  559. {
  560. const bool firstIsGroup = first.hasType (Tags::group);
  561. const bool secondIsGroup = second.hasType (Tags::group);
  562. if (firstIsGroup == secondIsGroup)
  563. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  564. return firstIsGroup ? -1 : 1;
  565. }
  566. };
  567. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  568. {
  569. if (keepGroupsAtStart)
  570. {
  571. ItemSorterWithGroupsAtStart sorter;
  572. state.sort (sorter, getUndoManager(), true);
  573. }
  574. else
  575. {
  576. ItemSorter sorter;
  577. state.sort (sorter, getUndoManager(), true);
  578. }
  579. }
  580. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  581. {
  582. for (int i = state.getNumChildren(); --i >= 0;)
  583. {
  584. const ValueTree child (state.getChild (i));
  585. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  586. return Item (project, child);
  587. }
  588. return addNewSubGroup (name, -1);
  589. }
  590. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  591. {
  592. String newID (createGUID (getID() + name + String (getNumChildren())));
  593. int n = 0;
  594. while (project.getMainGroup().findItemWithID (newID).isValid())
  595. newID = createGUID (newID + String (++n));
  596. Item group (createGroup (project, name, newID));
  597. jassert (canContain (group));
  598. addChild (group, insertIndex);
  599. return group;
  600. }
  601. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  602. {
  603. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  604. return false;
  605. if (file.isDirectory())
  606. {
  607. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  608. for (DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories); iter.next();)
  609. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  610. group.addFile (iter.getFile(), -1, shouldCompile);
  611. group.sortAlphabetically (false);
  612. }
  613. else if (file.existsAsFile())
  614. {
  615. if (! project.getMainGroup().findItemForFile (file).isValid())
  616. addFileUnchecked (file, insertIndex, shouldCompile);
  617. }
  618. else
  619. {
  620. jassertfalse;
  621. }
  622. return true;
  623. }
  624. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  625. {
  626. Item item (project, ValueTree (Tags::file));
  627. item.initialiseMissingProperties();
  628. item.getNameValue() = file.getFileName();
  629. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  630. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  631. if (canContain (item))
  632. {
  633. item.setFile (file);
  634. addChild (item, insertIndex);
  635. }
  636. }
  637. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  638. {
  639. Item item (project, ValueTree (Tags::file));
  640. item.initialiseMissingProperties();
  641. item.getNameValue() = file.getFileName();
  642. item.getShouldCompileValue() = shouldCompile;
  643. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  644. if (canContain (item))
  645. {
  646. item.setFile (file);
  647. addChild (item, insertIndex);
  648. return true;
  649. }
  650. return false;
  651. }
  652. Icon Project::Item::getIcon() const
  653. {
  654. const Icons& icons = getIcons();
  655. if (isFile())
  656. {
  657. if (isImageFile())
  658. return Icon (icons.imageDoc, Colours::blue);
  659. return Icon (icons.document, Colours::yellow);
  660. }
  661. if (isMainGroup())
  662. return Icon (icons.juceLogo, Colours::orange);
  663. return Icon (icons.folder, Colours::darkgrey);
  664. }
  665. bool Project::Item::isIconCrossedOut() const
  666. {
  667. return isFile()
  668. && ! (shouldBeCompiled()
  669. || shouldBeAddedToBinaryResources()
  670. || getFile().hasFileExtension (headerFileExtensions));
  671. }
  672. //==============================================================================
  673. ValueTree Project::getConfigNode()
  674. {
  675. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  676. }
  677. const char* const Project::configFlagDefault = "default";
  678. const char* const Project::configFlagEnabled = "enabled";
  679. const char* const Project::configFlagDisabled = "disabled";
  680. Value Project::getConfigFlag (const String& name)
  681. {
  682. ValueTree configNode (getConfigNode());
  683. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  684. if (v.getValue().toString().isEmpty())
  685. v = configFlagDefault;
  686. return v;
  687. }
  688. bool Project::isConfigFlagEnabled (const String& name) const
  689. {
  690. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  691. }
  692. void Project::sanitiseConfigFlags()
  693. {
  694. ValueTree configNode (getConfigNode());
  695. for (int i = configNode.getNumProperties(); --i >= 0;)
  696. {
  697. const var value (configNode [configNode.getPropertyName(i)]);
  698. if (value != configFlagEnabled && value != configFlagDisabled)
  699. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  700. }
  701. }
  702. //==============================================================================
  703. ValueTree Project::getModulesNode()
  704. {
  705. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  706. }
  707. bool Project::isModuleEnabled (const String& moduleID) const
  708. {
  709. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  710. for (int i = 0; i < modules.getNumChildren(); ++i)
  711. if (modules.getChild(i) [Ids::ID] == moduleID)
  712. return true;
  713. return false;
  714. }
  715. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  716. {
  717. return getModulesNode().getChildWithProperty (Ids::ID, moduleID)
  718. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  719. }
  720. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  721. {
  722. return getModulesNode().getChildWithProperty (Ids::ID, moduleID)
  723. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  724. }
  725. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  726. {
  727. if (! isModuleEnabled (moduleID))
  728. {
  729. ValueTree module (Tags::module);
  730. module.setProperty (Ids::ID, moduleID, nullptr);
  731. ValueTree modules (getModulesNode());
  732. modules.addChild (module, -1, getUndoManagerFor (modules));
  733. shouldShowAllModuleFilesInProject (moduleID) = true;
  734. }
  735. if (shouldCopyFilesLocally)
  736. shouldCopyModuleFilesLocally (moduleID) = true;
  737. }
  738. void Project::removeModule (const String& moduleID)
  739. {
  740. ValueTree modules (getModulesNode());
  741. for (int i = 0; i < modules.getNumChildren(); ++i)
  742. if (modules.getChild(i) [Ids::ID] == moduleID)
  743. modules.removeChild (i, getUndoManagerFor (modules));
  744. }
  745. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  746. {
  747. for (int i = 0; i < availableModules.modules.size(); ++i)
  748. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  749. modules.add (availableModules.modules.getUnchecked(i)->create());
  750. }
  751. int Project::getNumModules() const
  752. {
  753. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  754. }
  755. String Project::getModuleID (int index) const
  756. {
  757. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [Ids::ID].toString();
  758. }
  759. //==============================================================================
  760. ValueTree Project::getExporters()
  761. {
  762. return projectRoot.getOrCreateChildWithName (Tags::exporters, nullptr);
  763. }
  764. int Project::getNumExporters()
  765. {
  766. return getExporters().getNumChildren();
  767. }
  768. ProjectExporter* Project::createExporter (int index)
  769. {
  770. jassert (index >= 0 && index < getNumExporters());
  771. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  772. }
  773. void Project::addNewExporter (const String& exporterName)
  774. {
  775. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  776. ValueTree exporters (getExporters());
  777. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  778. }
  779. void Project::createExporterForCurrentPlatform()
  780. {
  781. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  782. }
  783. //==============================================================================
  784. String Project::getFileTemplate (const String& templateName)
  785. {
  786. int dataSize;
  787. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  788. if (data == nullptr)
  789. {
  790. jassertfalse;
  791. return String::empty;
  792. }
  793. return String::fromUTF8 (data, dataSize);
  794. }
  795. //==============================================================================
  796. Project::ExporterIterator::ExporterIterator (Project& p) : index (-1), project (p) {}
  797. Project::ExporterIterator::~ExporterIterator() {}
  798. bool Project::ExporterIterator::next()
  799. {
  800. if (++index >= project.getNumExporters())
  801. return false;
  802. exporter = project.createExporter (index);
  803. if (exporter == nullptr)
  804. {
  805. jassertfalse; // corrupted project file?
  806. return next();
  807. }
  808. return true;
  809. }
  810. PropertiesFile& Project::getStoredProperties() const
  811. {
  812. return getAppSettings().getProjectProperties (getProjectUID());
  813. }