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.

1044 lines
33KB

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