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.

993 lines
31KB

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