Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

172 lines
3.9KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* Routings for opening/closing/creation of projects. All the actual
  19. project state belongs to Timeline and other classes. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #include "Loggable.H"
  27. #include "Project.H"
  28. #include "Timeline.H" // for sample_rate();
  29. #define APP_TITLE "Non-DAW"
  30. #include "debug.h"
  31. char Project::_name[256];
  32. bool Project::_is_open = false;
  33. void
  34. Project::set_name ( const char *name )
  35. {
  36. char *s = rindex( name, '/' );
  37. strcpy( Project::_name, s ? s + 1 : name );
  38. for ( s = Project::_name; *s; ++s )
  39. if ( *s == '_' || *s == '-' )
  40. *s = ' ';
  41. }
  42. static int
  43. exists ( const char *name )
  44. {
  45. struct stat st;
  46. return 0 == stat( name, &st );
  47. }
  48. #include <errno.h>
  49. bool
  50. Project::close ( void )
  51. {
  52. Loggable::close();
  53. write_info();
  54. _is_open = false;
  55. }
  56. bool
  57. Project::write_info ( void )
  58. {
  59. if ( ! open() )
  60. return true;
  61. FILE *fp;
  62. if ( ! ( fp = fopen( "info", "w" ) ) )
  63. {
  64. WARNING( "could not open project info file for writing." );
  65. return false;
  66. }
  67. fprintf( fp, "version\n\t%s\nsample rate\n\t%lu\n", APP_TITLE " " VERSION, timeline->sample_rate() );
  68. fclose( fp );
  69. return true;
  70. }
  71. bool
  72. Project::read_info ( void )
  73. {
  74. FILE *fp;
  75. if ( ! ( fp = fopen( "info", "r" ) ) )
  76. {
  77. WARNING( "could not open project info file for reading." );
  78. return false;
  79. }
  80. /* TODO: something */
  81. fclose( fp );
  82. return true;
  83. }
  84. bool
  85. Project::open ( const char *name )
  86. {
  87. if ( chdir( name ) )
  88. {
  89. WARNING( "Cannot change to project dir \"%s\"", name );
  90. return false;
  91. }
  92. if ( ! exists( "history" ) ||
  93. ! exists( "sources" ) )
  94. // ! exists( "options" ) )
  95. {
  96. WARNING( "Not a Non-DAW project: \"%s\"", name );
  97. return false;
  98. }
  99. if ( ! Loggable::open( "history" ) )
  100. FATAL( "error opening journal" );
  101. set_name( name );
  102. read_info();
  103. _is_open = true;
  104. return true;
  105. }
  106. bool
  107. Project::create ( const char *name, const char *template_name )
  108. {
  109. close();
  110. if ( exists( name ) )
  111. {
  112. WARNING( "Project already exists" );
  113. return false;
  114. }
  115. if ( mkdir( name, 0777 ) )
  116. {
  117. WARNING( "Cannot create project directory" );
  118. return false;
  119. }
  120. if ( chdir( name ) )
  121. FATAL( "WTF? Cannot change to new project directory" );
  122. mkdir( "sources", 0777 );
  123. creat( "history", 0666 );
  124. /* TODO: copy template */
  125. return open( name );
  126. }