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.

1032 lines
33KB

  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. File Project::getBinaryDataCppFile (int index) const
  176. {
  177. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  178. if (index > 0)
  179. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  180. .withFileExtension (cpp.getFileExtension());
  181. return cpp;
  182. }
  183. //==============================================================================
  184. static void registerRecentFile (const File& file)
  185. {
  186. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  187. getAppSettings().recentFiles.addFile (file);
  188. getAppSettings().flush();
  189. }
  190. Result Project::loadDocument (const File& file)
  191. {
  192. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  193. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  194. return Result::fail ("Not a valid Jucer project!");
  195. ValueTree newTree (ValueTree::fromXml (*xml));
  196. if (! newTree.hasType (Tags::projectRoot))
  197. return Result::fail ("The document contains errors and couldn't be parsed!");
  198. registerRecentFile (file);
  199. projectRoot = newTree;
  200. removeDefunctExporters();
  201. setMissingDefaultValues();
  202. setChangedFlag (false);
  203. return Result::ok();
  204. }
  205. Result Project::saveDocument (const File& file)
  206. {
  207. return saveProject (file, false);
  208. }
  209. Result Project::saveProject (const File& file, bool isCommandLineApp)
  210. {
  211. updateProjectSettings();
  212. sanitiseConfigFlags();
  213. if (! isCommandLineApp)
  214. registerRecentFile (file);
  215. ProjectSaver saver (*this, file);
  216. return saver.save (! isCommandLineApp);
  217. }
  218. Result Project::saveResourcesOnly (const File& file)
  219. {
  220. ProjectSaver saver (*this, file);
  221. return saver.saveResourcesOnly();
  222. }
  223. //==============================================================================
  224. static File lastDocumentOpened;
  225. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  226. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  227. //==============================================================================
  228. void Project::valueTreePropertyChanged (ValueTree&, const Identifier& property)
  229. {
  230. if (property == Ids::projectType)
  231. setMissingDefaultValues();
  232. changed();
  233. }
  234. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  235. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&) { changed(); }
  236. void Project::valueTreeChildOrderChanged (ValueTree&) { changed(); }
  237. void Project::valueTreeParentChanged (ValueTree&) {}
  238. //==============================================================================
  239. File Project::resolveFilename (String filename) const
  240. {
  241. if (filename.isEmpty())
  242. return File::nonexistent;
  243. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  244. if (FileHelpers::isAbsolutePath (filename))
  245. return File::createFileWithoutCheckingPath (FileHelpers::currentOSStylePath (filename)); // (avoid assertions for windows-style paths)
  246. return getFile().getSiblingFile (FileHelpers::currentOSStylePath (filename));
  247. }
  248. String Project::getRelativePathForFile (const File& file) const
  249. {
  250. String filename (file.getFullPathName());
  251. File relativePathBase (getFile().getParentDirectory());
  252. String p1 (relativePathBase.getFullPathName());
  253. String p2 (file.getFullPathName());
  254. while (p1.startsWithChar (File::separator))
  255. p1 = p1.substring (1);
  256. while (p2.startsWithChar (File::separator))
  257. p2 = p2.substring (1);
  258. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  259. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  260. {
  261. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  262. }
  263. return filename;
  264. }
  265. //==============================================================================
  266. const ProjectType& Project::getProjectType() const
  267. {
  268. if (const ProjectType* type = ProjectType::findType (getProjectTypeString()))
  269. return *type;
  270. const ProjectType* guiType = ProjectType::findType (ProjectType::getGUIAppTypeName());
  271. jassert (guiType != nullptr);
  272. return *guiType;
  273. }
  274. //==============================================================================
  275. void Project::createPropertyEditors (PropertyListBuilder& props)
  276. {
  277. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  278. "The name of the project.");
  279. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  280. "The project's version number, This should be in the format major.minor.point[.point]");
  281. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  282. "Your company name, which will be added to the properties of the binary where possible");
  283. {
  284. StringArray projectTypeNames;
  285. Array<var> projectTypeCodes;
  286. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  287. for (int i = 0; i < types.size(); ++i)
  288. {
  289. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  290. projectTypeCodes.add (types.getUnchecked(i)->getType());
  291. }
  292. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  293. }
  294. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  295. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  296. getProjectType().createPropertyEditors (*this, props);
  297. {
  298. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  299. StringArray maxSizeNames;
  300. Array<var> maxSizeCodes;
  301. maxSizeNames.add (TRANS("Default"));
  302. maxSizeCodes.add (var::null);
  303. maxSizeNames.add (String::empty);
  304. maxSizeCodes.add (var::null);
  305. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  306. {
  307. const int sizeInBytes = maxSizes[i] * 1024;
  308. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  309. maxSizeCodes.add (sizeInBytes);
  310. }
  311. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  312. "When splitting binary data into multiple cpp files, the Introjucer attempts to keep the file sizes below this threshold. "
  313. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  314. }
  315. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  316. "Global preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  317. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  318. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  319. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  320. }
  321. static StringArray getConfigs (const Project& p)
  322. {
  323. StringArray configs;
  324. configs.addTokens (p.getVersionString(), ",.", String::empty);
  325. configs.trim();
  326. configs.removeEmptyStrings();
  327. return configs;
  328. }
  329. int Project::getVersionAsHexInteger() const
  330. {
  331. const StringArray configs (getConfigs (*this));
  332. int value = (configs[0].getIntValue() << 16)
  333. + (configs[1].getIntValue() << 8)
  334. + configs[2].getIntValue();
  335. if (configs.size() >= 4)
  336. value = (value << 8) + configs[3].getIntValue();
  337. return value;
  338. }
  339. String Project::getVersionAsHex() const
  340. {
  341. return "0x" + String::toHexString (getVersionAsHexInteger());
  342. }
  343. StringPairArray Project::getPreprocessorDefs() const
  344. {
  345. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  346. }
  347. //==============================================================================
  348. Project::Item Project::getMainGroup()
  349. {
  350. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  351. }
  352. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  353. {
  354. if (item.isImageFile())
  355. {
  356. found.add (new Project::Item (item));
  357. }
  358. else if (item.isGroup())
  359. {
  360. for (int i = 0; i < item.getNumChildren(); ++i)
  361. findImages (item.getChild (i), found);
  362. }
  363. }
  364. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  365. {
  366. findImages (getMainGroup(), items);
  367. }
  368. //==============================================================================
  369. Project::Item::Item (Project& project_, const ValueTree& state_)
  370. : project (project_), state (state_)
  371. {
  372. }
  373. Project::Item::Item (const Item& other)
  374. : project (other.project), state (other.state)
  375. {
  376. }
  377. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  378. String Project::Item::getID() const { return state [Ids::ID]; }
  379. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  380. Image Project::Item::loadAsImageFile() const
  381. {
  382. return isValid() ? ImageCache::getFromFile (getFile())
  383. : Image::null;
  384. }
  385. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  386. {
  387. Item group (project, ValueTree (Tags::group));
  388. group.setID (uid);
  389. group.initialiseMissingProperties();
  390. group.getNameValue() = name;
  391. return group;
  392. }
  393. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  394. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  395. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  396. bool Project::Item::isImageFile() const { return isFile() && ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr; }
  397. Project::Item Project::Item::findItemWithID (const String& targetId) const
  398. {
  399. if (state [Ids::ID] == targetId)
  400. return *this;
  401. if (isGroup())
  402. {
  403. for (int i = getNumChildren(); --i >= 0;)
  404. {
  405. Item found (getChild(i).findItemWithID (targetId));
  406. if (found.isValid())
  407. return found;
  408. }
  409. }
  410. return Item (project, ValueTree::invalid);
  411. }
  412. bool Project::Item::canContain (const Item& child) const
  413. {
  414. if (isFile())
  415. return false;
  416. if (isGroup())
  417. return child.isFile() || child.isGroup();
  418. jassertfalse;
  419. return false;
  420. }
  421. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  422. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  423. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  424. Value Project::Item::getShouldAddToResourceValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  425. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  426. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  427. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  428. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  429. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  430. String Project::Item::getFilePath() const
  431. {
  432. if (isFile())
  433. return state [Ids::file].toString();
  434. return String::empty;
  435. }
  436. File Project::Item::getFile() const
  437. {
  438. if (isFile())
  439. return project.resolveFilename (state [Ids::file].toString());
  440. return File::nonexistent;
  441. }
  442. void Project::Item::setFile (const File& file)
  443. {
  444. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  445. jassert (getFile() == file);
  446. }
  447. void Project::Item::setFile (const RelativePath& file)
  448. {
  449. jassert (file.getRoot() == RelativePath::projectFolder);
  450. jassert (isFile());
  451. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  452. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  453. }
  454. bool Project::Item::renameFile (const File& newFile)
  455. {
  456. const File oldFile (getFile());
  457. if (oldFile.moveFileTo (newFile)
  458. || (newFile.exists() && ! oldFile.exists()))
  459. {
  460. setFile (newFile);
  461. IntrojucerApp::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  462. return true;
  463. }
  464. return false;
  465. }
  466. bool Project::Item::containsChildForFile (const RelativePath& file) const
  467. {
  468. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  469. }
  470. Project::Item Project::Item::findItemForFile (const File& file) const
  471. {
  472. if (getFile() == file)
  473. return *this;
  474. if (isGroup())
  475. {
  476. for (int i = getNumChildren(); --i >= 0;)
  477. {
  478. Item found (getChild(i).findItemForFile (file));
  479. if (found.isValid())
  480. return found;
  481. }
  482. }
  483. return Item (project, ValueTree::invalid);
  484. }
  485. File Project::Item::determineGroupFolder() const
  486. {
  487. jassert (isGroup());
  488. File f;
  489. for (int i = 0; i < getNumChildren(); ++i)
  490. {
  491. f = getChild(i).getFile();
  492. if (f.exists())
  493. return f.getParentDirectory();
  494. }
  495. Item parent (getParent());
  496. if (parent != *this)
  497. {
  498. f = parent.determineGroupFolder();
  499. if (f.getChildFile (getName()).isDirectory())
  500. f = f.getChildFile (getName());
  501. }
  502. else
  503. {
  504. f = project.getFile().getParentDirectory();
  505. if (f.getChildFile ("Source").isDirectory())
  506. f = f.getChildFile ("Source");
  507. }
  508. return f;
  509. }
  510. void Project::Item::initialiseMissingProperties()
  511. {
  512. if (! state.hasProperty (Ids::ID))
  513. setID (createAlphaNumericUID());
  514. if (isFile())
  515. {
  516. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  517. }
  518. else if (isGroup())
  519. {
  520. for (int i = getNumChildren(); --i >= 0;)
  521. getChild(i).initialiseMissingProperties();
  522. }
  523. }
  524. Value Project::Item::getNameValue()
  525. {
  526. return state.getPropertyAsValue (Ids::name, getUndoManager());
  527. }
  528. String Project::Item::getName() const
  529. {
  530. return state [Ids::name];
  531. }
  532. void Project::Item::addChild (const Item& newChild, int insertIndex)
  533. {
  534. state.addChild (newChild.state, insertIndex, getUndoManager());
  535. }
  536. void Project::Item::removeItemFromProject()
  537. {
  538. state.getParent().removeChild (state, getUndoManager());
  539. }
  540. Project::Item Project::Item::getParent() const
  541. {
  542. if (isMainGroup() || ! isGroup())
  543. return *this;
  544. return Item (project, state.getParent());
  545. }
  546. struct ItemSorter
  547. {
  548. static int compareElements (const ValueTree& first, const ValueTree& second)
  549. {
  550. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  551. }
  552. };
  553. struct ItemSorterWithGroupsAtStart
  554. {
  555. static int compareElements (const ValueTree& first, const ValueTree& second)
  556. {
  557. const bool firstIsGroup = first.hasType (Tags::group);
  558. const bool secondIsGroup = second.hasType (Tags::group);
  559. if (firstIsGroup == secondIsGroup)
  560. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  561. return firstIsGroup ? -1 : 1;
  562. }
  563. };
  564. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  565. {
  566. if (keepGroupsAtStart)
  567. {
  568. ItemSorterWithGroupsAtStart sorter;
  569. state.sort (sorter, getUndoManager(), true);
  570. }
  571. else
  572. {
  573. ItemSorter sorter;
  574. state.sort (sorter, getUndoManager(), true);
  575. }
  576. }
  577. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  578. {
  579. for (int i = state.getNumChildren(); --i >= 0;)
  580. {
  581. const ValueTree child (state.getChild (i));
  582. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  583. return Item (project, child);
  584. }
  585. return addNewSubGroup (name, -1);
  586. }
  587. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  588. {
  589. String newID (createGUID (getID() + name + String (getNumChildren())));
  590. int n = 0;
  591. while (findItemWithID (newID).isValid())
  592. newID = createGUID (newID + String (++n));
  593. Item group (createGroup (project, name, newID));
  594. jassert (canContain (group));
  595. addChild (group, insertIndex);
  596. return group;
  597. }
  598. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  599. {
  600. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  601. return false;
  602. if (file.isDirectory())
  603. {
  604. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  605. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  606. while (iter.next())
  607. {
  608. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  609. group.addFile (iter.getFile(), -1, shouldCompile);
  610. }
  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& project_) : index (-1), project (project_) {}
  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. }