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.

204 lines
4.6KB

  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. #include "TLE.H" // all this just for load and save...
  30. extern TLE *tle;
  31. /* FIXME: wrong place for this */
  32. #define APP_TITLE "Non-DAW"
  33. #define PROJECT_VERSION "0.28.0"
  34. #include "debug.h"
  35. char Project::_name[256];
  36. bool Project::_is_open = false;
  37. void
  38. Project::set_name ( const char *name )
  39. {
  40. strcpy( Project::_name, name );
  41. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  42. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  43. char *s = rindex( Project::_name, '/' );
  44. s = s ? s + 1 : Project::_name;
  45. memmove( Project::_name, s, strlen( s ) + 1 );
  46. for ( s = Project::_name; *s; ++s )
  47. if ( *s == '_' || *s == '-' )
  48. *s = ' ';
  49. }
  50. static int
  51. exists ( const char *name )
  52. {
  53. struct stat st;
  54. return 0 == stat( name, &st );
  55. }
  56. #include <errno.h>
  57. bool
  58. Project::close ( void )
  59. {
  60. tle->save_timeline_settings();
  61. Loggable::close();
  62. write_info();
  63. _is_open = false;
  64. return true;
  65. }
  66. bool
  67. Project::write_info ( void )
  68. {
  69. if ( ! open() )
  70. return true;
  71. FILE *fp;
  72. if ( ! ( fp = fopen( "info", "w" ) ) )
  73. {
  74. WARNING( "could not open project info file for writing." );
  75. return false;
  76. }
  77. fprintf( fp, "created by\n\t%s\nversion\n\t%s\nsample rate\n\t%lu\n",
  78. APP_TITLE " " VERSION,
  79. PROJECT_VERSION,
  80. (unsigned long)timeline->sample_rate() );
  81. fclose( fp );
  82. return true;
  83. }
  84. bool
  85. Project::read_info ( void )
  86. {
  87. FILE *fp;
  88. if ( ! ( fp = fopen( "info", "r" ) ) )
  89. {
  90. WARNING( "could not open project info file for reading." );
  91. return false;
  92. }
  93. /* TODO: something */
  94. fclose( fp );
  95. return true;
  96. }
  97. bool
  98. Project::open ( const char *name )
  99. {
  100. if ( chdir( name ) )
  101. {
  102. WARNING( "Cannot change to project dir \"%s\"", name );
  103. return false;
  104. }
  105. if ( ! exists( "history" ) ||
  106. ! exists( "sources" ) )
  107. // ! exists( "options" ) )
  108. {
  109. WARNING( "Not a Non-DAW project: \"%s\"", name );
  110. return false;
  111. }
  112. if ( ! Loggable::open( "history" ) )
  113. FATAL( "error opening journal" );
  114. set_name( name );
  115. read_info();
  116. _is_open = true;
  117. tle->load_timeline_settings();
  118. return true;
  119. }
  120. bool
  121. Project::create ( const char *name, const char *template_name )
  122. {
  123. close();
  124. if ( exists( name ) )
  125. {
  126. WARNING( "Project already exists" );
  127. return false;
  128. }
  129. if ( mkdir( name, 0777 ) )
  130. {
  131. WARNING( "Cannot create project directory" );
  132. return false;
  133. }
  134. if ( chdir( name ) )
  135. FATAL( "WTF? Cannot change to new project directory" );
  136. mkdir( "sources", 0777 );
  137. creat( "history", 0666 );
  138. /* TODO: copy template */
  139. if ( open( name ) )
  140. {
  141. /* add the bare essentials */
  142. timeline->beats_per_minute( 0, 120 );
  143. timeline->time( 0, 4, 4 );
  144. return true;
  145. }
  146. else
  147. return false;
  148. }