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.

240 lines
5.2KB

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